blob: 81555f92d902c79169c61c46728ca5ecf82f9f6d [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 Tarreau63617db2021-10-06 18:23:40 +020014#include <import/ebmbtree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.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>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020018#include <haproxy/dynbuf.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 Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/istbuf.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020026#include <haproxy/log.h>
Willy Tarreau6c0fadf2022-09-12 19:07:51 +020027#include <haproxy/mux_h2-t.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020028#include <haproxy/net_helper.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010029#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020030#include <haproxy/session-t.h>
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +010031#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020032#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020034#include <haproxy/trace.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020035
36
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010037/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020038static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010039static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010040static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020041static const struct h2s *h2_idle_stream;
42
Willy Tarreau5ab6b572017-09-22 08:05:00 +020043
Willy Tarreau6c0fadf2022-09-12 19:07:51 +020044/**** H2 connection descriptor ****/
Willy Tarreau5ab6b572017-09-22 08:05:00 +020045struct h2c {
46 struct connection *conn;
47
48 enum h2_cs st0; /* mux state */
49 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
50
51 /* 16 bit hole here */
52 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010053 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020054 int32_t max_id; /* highest ID known on this connection, <0 before preface */
55 uint32_t rcvd_c; /* newly received data to ACK for the connection */
Willy Tarreau617592c2022-06-08 16:32:22 +020056 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) or zero */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020057
58 /* states for the demux direction */
59 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020060 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020061
62 int32_t dsi; /* demux stream ID (<0 = idle) */
63 int32_t dfl; /* demux frame length (if dsi >= 0) */
64 int8_t dft; /* demux frame type (if dsi >= 0) */
65 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010066 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
67 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
69
70 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +020071 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020072 int32_t miw; /* mux initial window size for all new streams */
73 int32_t mws; /* mux window size. Can be negative. */
74 int32_t mfs; /* mux's max frame size */
75
Willy Tarreauea392822017-10-31 10:02:25 +010076 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +010077 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreauf279a2f2023-05-30 15:42:35 +020078 int idle_start; /* date of the last time the connection went idle (no stream + empty mbuf), or the start of current http req */
Willy Tarreau15a47332022-03-18 15:57:34 +010079 /* 32-bit hole here */
Willy Tarreau49745612017-12-03 18:56:02 +010080 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau36c22322022-05-27 10:41:24 +020081 unsigned int nb_sc; /* number of attached stream connectors */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +010082 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +010083 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +020084 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +010085 struct task *task; /* timeout management task */
Amaury Denoyellec92697d2020-10-27 17:16:01 +010086 struct h2_counters *px_counters; /* h2 counters attached to proxy */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020087 struct eb_root streams_by_id; /* all active streams by their ID */
88 struct list send_list; /* list of blocked streams requesting to send */
89 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau9edf6db2019-10-02 10:49:59 +020090 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Willy Tarreau44e973f2018-03-01 17:49:30 +010091 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +020092 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020093};
94
Willy Tarreau2c249eb2019-05-13 18:06:17 +020095
Willy Tarreau18312642017-10-11 07:57:07 +020096/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
Christopher Fauletfafd1b02020-11-03 18:25:52 +010097 * it is being processed in the internal HTTP representation (HTX).
Willy Tarreau18312642017-10-11 07:57:07 +020098 */
99struct h2s {
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200100 struct sedesc *sd;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100101 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200102 struct h2c *h2c;
Willy Tarreau18312642017-10-11 07:57:07 +0200103 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200104 int32_t id; /* stream ID */
105 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200106 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200107 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
108 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200109 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100110 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200111 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200112 struct wait_event *subs; /* recv wait_event the stream connector associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200113 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau5723f292020-01-10 15:16:57 +0100114 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to send an RST after we failed to,
115 * in case there's no other subscription to do it */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100116
117 char upgrade_protocol[16]; /* rfc 8441: requested protocol on Extended CONNECT */
Willy Tarreau18312642017-10-11 07:57:07 +0200118};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119
Willy Tarreauc6405142017-09-21 20:23:50 +0200120/* descriptor for an h2 frame header */
121struct h2_fh {
122 uint32_t len; /* length, host order, 24 bits */
123 uint32_t sid; /* stream id, host order, 31 bits */
124 uint8_t ft; /* frame type */
125 uint8_t ff; /* frame flags */
126};
127
Willy Tarreau12ae2122019-08-08 18:23:12 +0200128/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200129static void h2_trace(enum trace_level level, uint64_t mask, \
130 const struct trace_source *src,
131 const struct ist where, const struct ist func,
132 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200133
134/* The event representation is split like this :
135 * strm - application layer
136 * h2s - internal H2 stream
137 * h2c - internal H2 connection
138 * conn - external connection
139 *
140 */
141static const struct trace_event h2_trace_events[] = {
142#define H2_EV_H2C_NEW (1ULL << 0)
Willy Tarreau87951942019-08-30 07:34:36 +0200143 { .mask = H2_EV_H2C_NEW, .name = "h2c_new", .desc = "new H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200144#define H2_EV_H2C_RECV (1ULL << 1)
Willy Tarreau87951942019-08-30 07:34:36 +0200145 { .mask = H2_EV_H2C_RECV, .name = "h2c_recv", .desc = "Rx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200146#define H2_EV_H2C_SEND (1ULL << 2)
Willy Tarreau87951942019-08-30 07:34:36 +0200147 { .mask = H2_EV_H2C_SEND, .name = "h2c_send", .desc = "Tx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200148#define H2_EV_H2C_FCTL (1ULL << 3)
Willy Tarreau87951942019-08-30 07:34:36 +0200149 { .mask = H2_EV_H2C_FCTL, .name = "h2c_fctl", .desc = "H2 connection flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200150#define H2_EV_H2C_BLK (1ULL << 4)
Willy Tarreau87951942019-08-30 07:34:36 +0200151 { .mask = H2_EV_H2C_BLK, .name = "h2c_blk", .desc = "H2 connection blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200152#define H2_EV_H2C_WAKE (1ULL << 5)
Willy Tarreau87951942019-08-30 07:34:36 +0200153 { .mask = H2_EV_H2C_WAKE, .name = "h2c_wake", .desc = "H2 connection woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200154#define H2_EV_H2C_END (1ULL << 6)
Willy Tarreau87951942019-08-30 07:34:36 +0200155 { .mask = H2_EV_H2C_END, .name = "h2c_end", .desc = "H2 connection terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200156#define H2_EV_H2C_ERR (1ULL << 7)
Willy Tarreau87951942019-08-30 07:34:36 +0200157 { .mask = H2_EV_H2C_ERR, .name = "h2c_err", .desc = "error on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200158#define H2_EV_RX_FHDR (1ULL << 8)
Willy Tarreau87951942019-08-30 07:34:36 +0200159 { .mask = H2_EV_RX_FHDR, .name = "rx_fhdr", .desc = "H2 frame header received" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200160#define H2_EV_RX_FRAME (1ULL << 9)
Willy Tarreau87951942019-08-30 07:34:36 +0200161 { .mask = H2_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200162#define H2_EV_RX_EOI (1ULL << 10)
Willy Tarreau87951942019-08-30 07:34:36 +0200163 { .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 +0200164#define H2_EV_RX_PREFACE (1ULL << 11)
Willy Tarreau87951942019-08-30 07:34:36 +0200165 { .mask = H2_EV_RX_PREFACE, .name = "rx_preface", .desc = "receipt of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200166#define H2_EV_RX_DATA (1ULL << 12)
Willy Tarreau87951942019-08-30 07:34:36 +0200167 { .mask = H2_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200168#define H2_EV_RX_HDR (1ULL << 13)
Willy Tarreau87951942019-08-30 07:34:36 +0200169 { .mask = H2_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200170#define H2_EV_RX_PRIO (1ULL << 14)
Willy Tarreau87951942019-08-30 07:34:36 +0200171 { .mask = H2_EV_RX_PRIO, .name = "rx_prio", .desc = "receipt of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200172#define H2_EV_RX_RST (1ULL << 15)
Willy Tarreau87951942019-08-30 07:34:36 +0200173 { .mask = H2_EV_RX_RST, .name = "rx_rst", .desc = "receipt of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200174#define H2_EV_RX_SETTINGS (1ULL << 16)
Willy Tarreau87951942019-08-30 07:34:36 +0200175 { .mask = H2_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200176#define H2_EV_RX_PUSH (1ULL << 17)
Willy Tarreau87951942019-08-30 07:34:36 +0200177 { .mask = H2_EV_RX_PUSH, .name = "rx_push", .desc = "receipt of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200178#define H2_EV_RX_PING (1ULL << 18)
Willy Tarreau87951942019-08-30 07:34:36 +0200179 { .mask = H2_EV_RX_PING, .name = "rx_ping", .desc = "receipt of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200180#define H2_EV_RX_GOAWAY (1ULL << 19)
Willy Tarreau87951942019-08-30 07:34:36 +0200181 { .mask = H2_EV_RX_GOAWAY, .name = "rx_goaway", .desc = "receipt of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200182#define H2_EV_RX_WU (1ULL << 20)
Willy Tarreau87951942019-08-30 07:34:36 +0200183 { .mask = H2_EV_RX_WU, .name = "rx_wu", .desc = "receipt of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200184#define H2_EV_RX_CONT (1ULL << 21)
Willy Tarreau87951942019-08-30 07:34:36 +0200185 { .mask = H2_EV_RX_CONT, .name = "rx_cont", .desc = "receipt of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200186#define H2_EV_TX_FRAME (1ULL << 22)
Willy Tarreau87951942019-08-30 07:34:36 +0200187 { .mask = H2_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200188#define H2_EV_TX_EOI (1ULL << 23)
Willy Tarreau87951942019-08-30 07:34:36 +0200189 { .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 +0200190#define H2_EV_TX_PREFACE (1ULL << 24)
Willy Tarreau87951942019-08-30 07:34:36 +0200191 { .mask = H2_EV_TX_PREFACE, .name = "tx_preface", .desc = "transmission of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200192#define H2_EV_TX_DATA (1ULL << 25)
Willy Tarreau87951942019-08-30 07:34:36 +0200193 { .mask = H2_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200194#define H2_EV_TX_HDR (1ULL << 26)
Willy Tarreau87951942019-08-30 07:34:36 +0200195 { .mask = H2_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200196#define H2_EV_TX_PRIO (1ULL << 27)
Willy Tarreau87951942019-08-30 07:34:36 +0200197 { .mask = H2_EV_TX_PRIO, .name = "tx_prio", .desc = "transmission of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200198#define H2_EV_TX_RST (1ULL << 28)
Willy Tarreau87951942019-08-30 07:34:36 +0200199 { .mask = H2_EV_TX_RST, .name = "tx_rst", .desc = "transmission of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200200#define H2_EV_TX_SETTINGS (1ULL << 29)
Willy Tarreau87951942019-08-30 07:34:36 +0200201 { .mask = H2_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200202#define H2_EV_TX_PUSH (1ULL << 30)
Willy Tarreau87951942019-08-30 07:34:36 +0200203 { .mask = H2_EV_TX_PUSH, .name = "tx_push", .desc = "transmission of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200204#define H2_EV_TX_PING (1ULL << 31)
Willy Tarreau87951942019-08-30 07:34:36 +0200205 { .mask = H2_EV_TX_PING, .name = "tx_ping", .desc = "transmission of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200206#define H2_EV_TX_GOAWAY (1ULL << 32)
Willy Tarreau87951942019-08-30 07:34:36 +0200207 { .mask = H2_EV_TX_GOAWAY, .name = "tx_goaway", .desc = "transmission of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200208#define H2_EV_TX_WU (1ULL << 33)
Willy Tarreau87951942019-08-30 07:34:36 +0200209 { .mask = H2_EV_TX_WU, .name = "tx_wu", .desc = "transmission of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200210#define H2_EV_TX_CONT (1ULL << 34)
Willy Tarreau87951942019-08-30 07:34:36 +0200211 { .mask = H2_EV_TX_CONT, .name = "tx_cont", .desc = "transmission of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200212#define H2_EV_H2S_NEW (1ULL << 35)
Willy Tarreau87951942019-08-30 07:34:36 +0200213 { .mask = H2_EV_H2S_NEW, .name = "h2s_new", .desc = "new H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200214#define H2_EV_H2S_RECV (1ULL << 36)
Willy Tarreau87951942019-08-30 07:34:36 +0200215 { .mask = H2_EV_H2S_RECV, .name = "h2s_recv", .desc = "Rx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200216#define H2_EV_H2S_SEND (1ULL << 37)
Willy Tarreau87951942019-08-30 07:34:36 +0200217 { .mask = H2_EV_H2S_SEND, .name = "h2s_send", .desc = "Tx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200218#define H2_EV_H2S_FCTL (1ULL << 38)
Willy Tarreau87951942019-08-30 07:34:36 +0200219 { .mask = H2_EV_H2S_FCTL, .name = "h2s_fctl", .desc = "H2 stream flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200220#define H2_EV_H2S_BLK (1ULL << 39)
Willy Tarreau87951942019-08-30 07:34:36 +0200221 { .mask = H2_EV_H2S_BLK, .name = "h2s_blk", .desc = "H2 stream blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200222#define H2_EV_H2S_WAKE (1ULL << 40)
Willy Tarreau87951942019-08-30 07:34:36 +0200223 { .mask = H2_EV_H2S_WAKE, .name = "h2s_wake", .desc = "H2 stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200224#define H2_EV_H2S_END (1ULL << 41)
Willy Tarreau87951942019-08-30 07:34:36 +0200225 { .mask = H2_EV_H2S_END, .name = "h2s_end", .desc = "H2 stream terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200226#define H2_EV_H2S_ERR (1ULL << 42)
Willy Tarreau87951942019-08-30 07:34:36 +0200227 { .mask = H2_EV_H2S_ERR, .name = "h2s_err", .desc = "error on H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200228#define H2_EV_STRM_NEW (1ULL << 43)
Willy Tarreau87951942019-08-30 07:34:36 +0200229 { .mask = H2_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200230#define H2_EV_STRM_RECV (1ULL << 44)
Willy Tarreau87951942019-08-30 07:34:36 +0200231 { .mask = H2_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200232#define H2_EV_STRM_SEND (1ULL << 45)
Willy Tarreau87951942019-08-30 07:34:36 +0200233 { .mask = H2_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200234#define H2_EV_STRM_FULL (1ULL << 46)
Willy Tarreau87951942019-08-30 07:34:36 +0200235 { .mask = H2_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200236#define H2_EV_STRM_WAKE (1ULL << 47)
Willy Tarreau87951942019-08-30 07:34:36 +0200237 { .mask = H2_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200238#define H2_EV_STRM_SHUT (1ULL << 48)
Willy Tarreau87951942019-08-30 07:34:36 +0200239 { .mask = H2_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200240#define H2_EV_STRM_END (1ULL << 49)
Willy Tarreau87951942019-08-30 07:34:36 +0200241 { .mask = H2_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200242#define H2_EV_STRM_ERR (1ULL << 50)
Willy Tarreau87951942019-08-30 07:34:36 +0200243 { .mask = H2_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200244#define H2_EV_PROTO_ERR (1ULL << 51)
Willy Tarreau87951942019-08-30 07:34:36 +0200245 { .mask = H2_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200246 { }
247};
248
249static const struct name_desc h2_trace_lockon_args[4] = {
250 /* arg1 */ { /* already used by the connection */ },
251 /* arg2 */ { .name="h2s", .desc="H2 stream" },
252 /* arg3 */ { },
253 /* arg4 */ { }
254};
255
256static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200257#define H2_VERB_CLEAN 1
258 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
259#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200260 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200261#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200262 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200263#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200264 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200265#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200266 { .name="complete", .desc="add full data dump when available" },
267 { /* end */ }
268};
269
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200270static struct trace_source trace_h2 __read_mostly = {
Willy Tarreau12ae2122019-08-08 18:23:12 +0200271 .name = IST("h2"),
272 .desc = "HTTP/2 multiplexer",
273 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200274 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200275 .known_events = h2_trace_events,
276 .lockon_args = h2_trace_lockon_args,
277 .decoding = h2_trace_decoding,
278 .report_events = ~0, // report everything by default
279};
280
281#define TRACE_SOURCE &trace_h2
282INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
283
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100284/* h2 stats module */
285enum {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100286 H2_ST_HEADERS_RCVD,
287 H2_ST_DATA_RCVD,
288 H2_ST_SETTINGS_RCVD,
289 H2_ST_RST_STREAM_RCVD,
290 H2_ST_GOAWAY_RCVD,
291
Amaury Denoyellea8879232020-10-27 17:16:03 +0100292 H2_ST_CONN_PROTO_ERR,
293 H2_ST_STRM_PROTO_ERR,
294 H2_ST_RST_STREAM_RESP,
295 H2_ST_GOAWAY_RESP,
296
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100297 H2_ST_OPEN_CONN,
298 H2_ST_OPEN_STREAM,
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100299 H2_ST_TOTAL_CONN,
300 H2_ST_TOTAL_STREAM,
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100301
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100302 H2_STATS_COUNT /* must be the last member of the enum */
303};
304
305static struct name_desc h2_stats[] = {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100306 [H2_ST_HEADERS_RCVD] = { .name = "h2_headers_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100307 .desc = "Total number of received HEADERS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100308 [H2_ST_DATA_RCVD] = { .name = "h2_data_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100309 .desc = "Total number of received DATA frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100310 [H2_ST_SETTINGS_RCVD] = { .name = "h2_settings_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100311 .desc = "Total number of received SETTINGS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100312 [H2_ST_RST_STREAM_RCVD] = { .name = "h2_rst_stream_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100313 .desc = "Total number of received RST_STREAM frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100314 [H2_ST_GOAWAY_RCVD] = { .name = "h2_goaway_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100315 .desc = "Total number of received GOAWAY frames" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100316
317 [H2_ST_CONN_PROTO_ERR] = { .name = "h2_detected_conn_protocol_errors",
318 .desc = "Total number of connection protocol errors" },
319 [H2_ST_STRM_PROTO_ERR] = { .name = "h2_detected_strm_protocol_errors",
320 .desc = "Total number of stream protocol errors" },
321 [H2_ST_RST_STREAM_RESP] = { .name = "h2_rst_stream_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100322 .desc = "Total number of RST_STREAM sent on detected error" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100323 [H2_ST_GOAWAY_RESP] = { .name = "h2_goaway_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100324 .desc = "Total number of GOAWAY sent on detected error" },
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100325
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100326 [H2_ST_OPEN_CONN] = { .name = "h2_open_connections",
327 .desc = "Count of currently open connections" },
328 [H2_ST_OPEN_STREAM] = { .name = "h2_backend_open_streams",
329 .desc = "Count of currently open streams" },
Amaury Denoyelle377d8782021-02-03 16:27:22 +0100330 [H2_ST_TOTAL_CONN] = { .name = "h2_total_connections",
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100331 .desc = "Total number of connections" },
Amaury Denoyelle377d8782021-02-03 16:27:22 +0100332 [H2_ST_TOTAL_STREAM] = { .name = "h2_backend_total_streams",
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100333 .desc = "Total number of streams" },
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100334};
335
336static struct h2_counters {
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100337 long long headers_rcvd; /* total number of HEADERS frame received */
338 long long data_rcvd; /* total number of DATA frame received */
339 long long settings_rcvd; /* total number of SETTINGS frame received */
340 long long rst_stream_rcvd; /* total number of RST_STREAM frame received */
341 long long goaway_rcvd; /* total number of GOAWAY frame received */
Amaury Denoyellea8879232020-10-27 17:16:03 +0100342
343 long long conn_proto_err; /* total number of protocol errors detected */
344 long long strm_proto_err; /* total number of protocol errors detected */
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100345 long long rst_stream_resp; /* total number of RST_STREAM frame sent on error */
346 long long goaway_resp; /* total number of GOAWAY frame sent on error */
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100347
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100348 long long open_conns; /* count of currently open connections */
349 long long open_streams; /* count of currently open streams */
350 long long total_conns; /* total number of connections */
351 long long total_streams; /* total number of streams */
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100352} h2_counters;
353
354static void h2_fill_stats(void *data, struct field *stats)
355{
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100356 struct h2_counters *counters = data;
357
358 stats[H2_ST_HEADERS_RCVD] = mkf_u64(FN_COUNTER, counters->headers_rcvd);
359 stats[H2_ST_DATA_RCVD] = mkf_u64(FN_COUNTER, counters->data_rcvd);
360 stats[H2_ST_SETTINGS_RCVD] = mkf_u64(FN_COUNTER, counters->settings_rcvd);
361 stats[H2_ST_RST_STREAM_RCVD] = mkf_u64(FN_COUNTER, counters->rst_stream_rcvd);
362 stats[H2_ST_GOAWAY_RCVD] = mkf_u64(FN_COUNTER, counters->goaway_rcvd);
Amaury Denoyellea8879232020-10-27 17:16:03 +0100363
364 stats[H2_ST_CONN_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->conn_proto_err);
365 stats[H2_ST_STRM_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->strm_proto_err);
366 stats[H2_ST_RST_STREAM_RESP] = mkf_u64(FN_COUNTER, counters->rst_stream_resp);
367 stats[H2_ST_GOAWAY_RESP] = mkf_u64(FN_COUNTER, counters->goaway_resp);
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100368
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100369 stats[H2_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
370 stats[H2_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
371 stats[H2_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
372 stats[H2_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100373}
374
375static struct stats_module h2_stats_module = {
376 .name = "h2",
377 .fill_stats = h2_fill_stats,
378 .stats = h2_stats,
379 .stats_count = H2_STATS_COUNT,
380 .counters = &h2_counters,
381 .counters_size = sizeof(h2_counters),
382 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
383 .clearable = 1,
384};
385
386INITCALL1(STG_REGISTER, stats_register_module, &h2_stats_module);
387
Willy Tarreau8ceae722018-11-26 11:58:30 +0100388/* the h2c connection pool */
389DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
390
391/* the h2s stream pool */
392DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
393
Willy Tarreaudc572362018-12-12 08:08:05 +0100394/* The default connection window size is 65535, it may only be enlarged using
395 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
396 * we'll pretend we already received the difference between the two to send
397 * an equivalent window update to enlarge it to 2G-1.
398 */
399#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
400
Willy Tarreau455d5682019-05-24 19:42:18 +0200401/* maximum amount of data we're OK with re-aligning for buffer optimizations */
402#define MAX_DATA_REALIGN 1024
403
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200404/* a few settings from the global section */
405static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreau9d7abda2023-04-17 15:04:34 +0200406static int h2_settings_initial_window_size = 65536; /* default initial value */
407static int h2_be_settings_initial_window_size = 0; /* backend's default initial value */
408static int h2_fe_settings_initial_window_size = 0; /* frontend's default initial value */
Willy Tarreauca1027c2023-04-18 15:57:03 +0200409static unsigned int h2_settings_max_concurrent_streams = 100; /* default value */
410static unsigned int h2_be_settings_max_concurrent_streams = 0; /* backend value */
411static unsigned int h2_fe_settings_max_concurrent_streams = 0; /* frontend value */
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100412static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200413
Willy Tarreaub22b5f02022-05-10 14:57:16 +0200414/* a dummy closed endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +0200415static const struct sedesc closed_ep = {
Willy Tarreauc1054922022-05-18 07:43:52 +0200416 .sc = NULL,
Willy Tarreaub605c422022-05-17 17:04:55 +0200417 .flags = SE_FL_DETACHED,
Willy Tarreaub22b5f02022-05-10 14:57:16 +0200418};
419
Willy Tarreau2a856182017-05-16 15:20:39 +0200420/* a dmumy closed stream */
421static const struct h2s *h2_closed_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200422 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau2a856182017-05-16 15:20:39 +0200423 .h2c = NULL,
424 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100425 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100426 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200427 .id = 0,
428};
429
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100430/* a dmumy closed stream returning a PROTOCOL_ERROR error */
431static const struct h2s *h2_error_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200432 .sd = (struct sedesc *)&closed_ep,
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100433 .h2c = NULL,
434 .st = H2_SS_CLOSED,
435 .errcode = H2_ERR_PROTOCOL_ERROR,
436 .flags = 0,
437 .id = 0,
438};
439
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100440/* a dmumy closed stream returning a REFUSED_STREAM error */
441static const struct h2s *h2_refused_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200442 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100443 .h2c = NULL,
444 .st = H2_SS_CLOSED,
445 .errcode = H2_ERR_REFUSED_STREAM,
446 .flags = 0,
447 .id = 0,
448};
449
Willy Tarreau2a856182017-05-16 15:20:39 +0200450/* and a dummy idle stream for use with any unannounced stream */
451static const struct h2s *h2_idle_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200452 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau2a856182017-05-16 15:20:39 +0200453 .h2c = NULL,
454 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100455 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 .id = 0,
457};
458
Willy Tarreaude4a5382023-10-17 08:25:19 +0200459
Willy Tarreau144f84a2021-03-02 16:09:26 +0100460struct task *h2_timeout_task(struct task *t, void *context, unsigned int state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200461static int h2_send(struct h2c *h2c);
462static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200463static int h2_process(struct h2c *h2c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100464/* h2_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100465struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100466static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau7cfbb812023-01-26 16:02:01 +0100467static int h2c_dec_hdrs(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len, char *upgrade_protocol);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100468static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100469struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state);
Willy Tarreau36c22322022-05-27 10:41:24 +0200470static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct stconn *sc, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100471static void h2s_alert(struct h2s *h2s);
Willy Tarreaude4a5382023-10-17 08:25:19 +0200472static inline void h2_remove_from_list(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200473
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200474/* returns the stconn associated to the H2 stream */
475static forceinline struct stconn *h2s_sc(const struct h2s *h2s)
476{
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200477 return h2s->sd->sc;
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200478}
479
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200480/* the H2 traces always expect that arg1, if non-null, is of type connection
481 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
482 * that arg3, if non-null, is either of type htx for tx headers, or of type
483 * buffer for everything else.
484 */
485static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
486 const struct ist where, const struct ist func,
487 const void *a1, const void *a2, const void *a3, const void *a4)
488{
489 const struct connection *conn = a1;
490 const struct h2c *h2c = conn ? conn->ctx : NULL;
491 const struct h2s *h2s = a2;
492 const struct buffer *buf = a3;
493 const struct htx *htx;
494 int pos;
495
496 if (!h2c) // nothing to add
497 return;
498
Willy Tarreau17104d42019-08-30 07:12:55 +0200499 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau73db4342019-09-25 07:28:44 +0200500 chunk_appendf(&trace_buf, " : h2c=%p(%c,%s)", h2c, conn_is_back(conn) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
501
Willy Tarreau8e6f7492021-06-16 17:47:24 +0200502 if (mask & H2_EV_H2C_NEW) // inside h2_init, otherwise it's hard to match conn & h2c
503 conn_append_debug_info(&trace_buf, conn, " : ");
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 Tarreau0f458712022-08-18 11:19:57 +0200508 if (h2c->flags & H2_CF_DEM_IN_PROGRESS && // frame processing has started, type and length are valid
Willy Tarreau73db4342019-09-25 07:28:44 +0200509 (mask & (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) == (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) {
Willy Tarreau8520d872020-09-18 07:39:29 +0200510 chunk_appendf(&trace_buf, " dft=%s/%02x dfl=%d", h2_ft_str(h2c->dft), h2c->dff, h2c->dfl);
Willy Tarreau73db4342019-09-25 07:28:44 +0200511 }
512
513 if (h2s) {
514 if (h2s->id <= 0)
515 chunk_appendf(&trace_buf, " dsi=%d", h2c->dsi);
Willy Tarreauf9f44992023-02-20 16:57:47 +0100516 if (h2s == h2_idle_stream)
517 chunk_appendf(&trace_buf, " h2s=IDL");
518 else if (h2s != h2_closed_stream)
519 chunk_appendf(&trace_buf, " h2s=%p(%d,%s)", h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100520 if (h2s->id && h2s->errcode)
521 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2s->errcode), h2s->errcode);
Willy Tarreau73db4342019-09-25 07:28:44 +0200522 }
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200523 }
524
525 /* Let's dump decoded requests and responses right after parsing. They
526 * are traced at level USER with a few recognizable flags.
527 */
528 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
529 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
530 htx = htxbuf(buf); // recv req/res
531 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
532 htx = a3; // send req/res
533 else
534 htx = NULL;
535
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200536 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200537 const struct htx_blk *blk = htx_get_blk(htx, pos);
538 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
539 enum htx_blk_type type = htx_get_blk_type(blk);
540
541 if (type == HTX_BLK_REQ_SL)
542 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200543 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200544 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
545 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
546 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
547 else if (type == HTX_BLK_RES_SL)
548 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200549 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200550 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
551 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
552 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
553 }
554}
555
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200556
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100557/* Detect a pending read0 for a H2 connection. It happens if a read0 was
558 * already reported on a previous xprt->rcvbuf() AND a frame parser failed
559 * to parse pending data, confirming no more progress is possible because
560 * we're facing a truncated frame. The function returns 1 to report a read0
561 * or 0 otherwise.
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200562 */
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100563static inline int h2c_read0_pending(struct h2c *h2c)
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200564{
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100565 return !!(h2c->flags & H2_CF_END_REACHED);
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200566}
567
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200568/* returns true if the connection is allowed to expire, false otherwise. A
Willy Tarreau34395832022-03-18 14:59:54 +0100569 * connection may expire when it has no attached streams. As long as streams
570 * are attached, the application layer is responsible for timeout management,
571 * and each layer will detach when it doesn't want to wait anymore. When the
572 * last one leaves, the connection must take over timeout management.
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200573 */
574static inline int h2c_may_expire(const struct h2c *h2c)
575{
Willy Tarreau36c22322022-05-27 10:41:24 +0200576 return !h2c->nb_sc;
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200577}
578
Willy Tarreauca1027c2023-04-18 15:57:03 +0200579/* returns the number of max concurrent streams permitted on a connection,
580 * depending on its side (frontend or backend), falling back to the default
581 * h2_settings_max_concurrent_streams. It may even be zero.
582 */
583static inline int h2c_max_concurrent_streams(const struct h2c *h2c)
584{
585 int ret;
586
587 ret = (h2c->flags & H2_CF_IS_BACK) ?
588 h2_be_settings_max_concurrent_streams :
589 h2_fe_settings_max_concurrent_streams;
590
591 ret = ret ? ret : h2_settings_max_concurrent_streams;
592 return ret;
593}
594
595
Willy Tarreau15a47332022-03-18 15:57:34 +0100596/* update h2c timeout if needed */
597static void h2c_update_timeout(struct h2c *h2c)
598{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200599 int is_idle_conn = 0;
600
Willy Tarreau15a47332022-03-18 15:57:34 +0100601 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
602
603 if (!h2c->task)
604 goto leave;
605
606 if (h2c_may_expire(h2c)) {
607 /* no more streams attached */
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200608 if (br_data(h2c->mbuf)) {
Willy Tarreau15a47332022-03-18 15:57:34 +0100609 /* pending output data: always the regular data timeout */
610 h2c->task->expire = tick_add_ifset(now_ms, h2c->timeout);
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200611 } else {
612 /* no stream, no output data */
613 if (!(h2c->flags & H2_CF_IS_BACK)) {
614 int to;
Willy Tarreau86b08a32022-04-13 17:40:28 +0200615
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200616 if (h2c->max_id > 0 && !b_data(&h2c->dbuf) &&
617 tick_isset(h2c->proxy->timeout.httpka)) {
618 /* idle after having seen one stream => keep-alive */
619 to = h2c->proxy->timeout.httpka;
620 } else {
621 /* before first request, or started to deserialize a
622 * new req => http-request.
623 */
624 to = h2c->proxy->timeout.httpreq;
625 }
Willy Tarreau86b08a32022-04-13 17:40:28 +0200626
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200627 h2c->task->expire = tick_add_ifset(h2c->idle_start, to);
628 is_idle_conn = 1;
629 }
630
631 if (h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) {
632 /* GOAWAY sent (or failed), closing in progress */
633 int exp = tick_add_ifset(now_ms, h2c->shut_timeout);
634
635 h2c->task->expire = tick_first(h2c->task->expire, exp);
636 is_idle_conn = 1;
637 }
638
639 /* if a timeout above was not set, fall back to the default one */
640 if (!tick_isset(h2c->task->expire))
641 h2c->task->expire = tick_add_ifset(now_ms, h2c->timeout);
Willy Tarreau15a47332022-03-18 15:57:34 +0100642 }
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200643
644 if ((h2c->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
645 is_idle_conn && tick_isset(global.close_spread_end)) {
646 /* If a soft-stop is in progress and a close-spread-time
647 * is set, we want to spread idle connection closing roughly
648 * evenly across the defined window. This should only
649 * act on idle frontend connections.
650 * If the window end is already in the past, we wake the
651 * timeout task up immediately so that it can be closed.
652 */
653 int remaining_window = tick_remain(now_ms, global.close_spread_end);
654 if (remaining_window) {
655 /* We don't need to reset the expire if it would
656 * already happen before the close window end.
657 */
658 if (tick_isset(h2c->task->expire) &&
659 tick_is_le(global.close_spread_end, h2c->task->expire)) {
660 /* Set an expire value shorter than the current value
661 * because the close spread window end comes earlier.
662 */
663 h2c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
664 }
665 }
666 else {
667 /* We are past the soft close window end, wake the timeout
668 * task up immediately.
669 */
670 task_wakeup(h2c->task, TASK_WOKEN_TIMER);
671 }
672 }
673
Willy Tarreau15a47332022-03-18 15:57:34 +0100674 } else {
675 h2c->task->expire = TICK_ETERNITY;
676 }
677 task_queue(h2c->task);
678 leave:
679 TRACE_LEAVE(H2_EV_H2C_WAKE);
680}
681
Olivier Houchard7a977432019-03-21 15:47:13 +0100682static __inline int
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200683h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100684{
685 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
Christopher Fauletff7925d2022-10-11 19:12:40 +0200686 ((h2c->flags & H2_CF_ERROR) || /* errors close immediately */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +0200687 (h2c->flags & H2_CF_ERR_PENDING && h2c->st0 < H2_CS_FRAME_H) || /* early error during connect */
Olivier Houchard7a977432019-03-21 15:47:13 +0100688 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
689 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200690 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Christopher Fauletff7925d2022-10-11 19:12:40 +0200691 ((h2c->flags & H2_CF_RCVD_SHUT) ||
Olivier Houchard7a977432019-03-21 15:47:13 +0100692 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
693 return 1;
694
695 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100696}
697
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200698/*****************************************************/
699/* functions below are for dynamic buffer management */
700/*****************************************************/
701
Willy Tarreau315d8072017-12-10 22:17:57 +0100702/* indicates whether or not the we may call the h2_recv() function to attempt
703 * to receive data into the buffer and/or demux pending data. The condition is
704 * a bit complex due to some API limits for now. The rules are the following :
705 * - if an error or a shutdown was detected on the connection and the buffer
706 * is empty, we must not attempt to receive
707 * - if the demux buf failed to be allocated, we must not try to receive and
708 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100709 * - if no flag indicates a blocking condition, we may attempt to receive,
710 * regardless of whether the demux buffer is full or not, so that only
711 * de demux part decides whether or not to block. This is needed because
712 * the connection API indeed prevents us from re-enabling receipt that is
713 * already enabled in a polled state, so we must always immediately stop
714 * as soon as the demux can't proceed so as never to hit an end of read
715 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100716 * - otherwise must may not attempt
717 */
718static inline int h2_recv_allowed(const struct h2c *h2c)
719{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200720 if (b_data(&h2c->dbuf) == 0 &&
Christopher Fauletff7925d2022-10-11 19:12:40 +0200721 ((h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR)) || h2c->st0 >= H2_CS_ERROR))
Willy Tarreau315d8072017-12-10 22:17:57 +0100722 return 0;
723
724 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100725 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100726 return 1;
727
728 return 0;
729}
730
Willy Tarreau47b515a2018-12-21 16:09:41 +0100731/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200732static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100733{
734 if (!h2_recv_allowed(h2c))
735 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200736 if ((!consider_buffer || !b_data(&h2c->dbuf))
737 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100738 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200739 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100740}
741
742
Willy Tarreau4596fe22022-05-17 19:07:51 +0200743/* returns true if the front connection has too many stream connectors attached */
Willy Tarreau36c22322022-05-27 10:41:24 +0200744static inline int h2_frt_has_too_many_sc(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200745{
Willy Tarreauca1027c2023-04-18 15:57:03 +0200746 return h2c->nb_sc > h2c_max_concurrent_streams(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +0200747}
748
Willy Tarreau44e973f2018-03-01 17:49:30 +0100749/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
750 * flags are used to figure what buffer was requested. It returns 1 if the
751 * allocation succeeds, in which case the connection is woken up, or 0 if it's
752 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200753 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100754static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200755{
756 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100757 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200758
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100759 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc(&h2c->dbuf)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200760 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200761 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200762 return 1;
763 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200764
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100765 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc(br_tail(h2c->mbuf))) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100766 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200767
768 if (h2c->flags & H2_CF_DEM_MROOM) {
769 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200770 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200771 }
Willy Tarreau14398122017-09-22 14:26:04 +0200772 return 1;
773 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100774
775 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200776 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s_sc(h2s) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100777 b_alloc(&h2s->rxbuf)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100778 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200779 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100780 return 1;
781 }
782
Willy Tarreau14398122017-09-22 14:26:04 +0200783 return 0;
784}
785
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200786static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200787{
788 struct buffer *buf = NULL;
789
Willy Tarreau2b718102021-04-21 07:32:39 +0200790 if (likely(!LIST_INLIST(&h2c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100791 unlikely((buf = b_alloc(bptr)) == NULL)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100792 h2c->buf_wait.target = h2c;
793 h2c->buf_wait.wakeup_cb = h2_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200794 LIST_APPEND(&th_ctx->buffer_wq, &h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200795 }
796 return buf;
797}
798
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200799static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200800{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200801 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100802 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100803 offer_buffers(NULL, 1);
Willy Tarreau14398122017-09-22 14:26:04 +0200804 }
805}
806
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200807static inline void h2_release_mbuf(struct h2c *h2c)
808{
809 struct buffer *buf;
810 unsigned int count = 0;
811
812 while (b_size(buf = br_head_pick(h2c->mbuf))) {
813 b_free(buf);
814 count++;
815 }
816 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100817 offer_buffers(NULL, count);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200818}
819
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100820/* returns the number of allocatable outgoing streams for the connection taking
821 * the last_sid and the reserved ones into account.
822 */
823static inline int h2_streams_left(const struct h2c *h2c)
824{
825 int ret;
826
827 /* consider the number of outgoing streams we're allowed to create before
828 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
829 * nb_reserved is the number of streams which don't yet have an ID.
830 */
831 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
832 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
833 if (ret < 0)
834 ret = 0;
835 return ret;
836}
837
Willy Tarreau00f18a32019-01-26 12:19:01 +0100838/* returns the number of streams in use on a connection to figure if it's
Willy Tarreau36c22322022-05-27 10:41:24 +0200839 * idle or not. We check nb_sc and not nb_streams as the caller will want
Willy Tarreau00f18a32019-01-26 12:19:01 +0100840 * to know if it was the last one after a detach().
841 */
842static int h2_used_streams(struct connection *conn)
843{
844 struct h2c *h2c = conn->ctx;
845
Willy Tarreau36c22322022-05-27 10:41:24 +0200846 return h2c->nb_sc;
Willy Tarreau00f18a32019-01-26 12:19:01 +0100847}
848
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100849/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100850static int h2_avail_streams(struct connection *conn)
851{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100852 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100853 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100854 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100855
Willy Tarreau6afec462019-01-28 06:40:19 +0100856 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
857 * streams on the connection.
858 */
859 if (h2c->last_sid >= 0)
860 return 0;
861
Willy Tarreauc61966f2019-10-31 15:10:03 +0100862 if (h2c->st0 >= H2_CS_ERROR)
863 return 0;
864
Willy Tarreau86949782019-01-31 10:42:05 +0100865 /* note: may be negative if a SETTINGS frame changes the limit */
866 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100867
868 /* we must also consider the limit imposed by stream IDs */
869 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100870 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100871 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100872 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
873 ret1 = MIN(ret1, ret2);
874 }
875 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100876}
877
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200878/* Unconditionally produce a trace of the header. Please do not call this one
Willy Tarreau11e8a8c2023-01-24 19:43:11 +0100879 * and use h2_trace_header() instead which first checks if traces are enabled.
880 */
881void _h2_trace_header(const struct ist hn, const struct ist hv,
882 uint64_t mask, const struct ist trc_loc, const char *func,
883 const struct h2c *h2c, const struct h2s *h2s)
884{
885 struct ist n_ist, v_ist;
886 const char *c_str, *s_str;
887
888 chunk_reset(&trash);
889 c_str = chunk_newstr(&trash);
890 if (h2c) {
891 chunk_appendf(&trash, "h2c=%p(%c,%s) ",
892 h2c, (h2c->flags & H2_CF_IS_BACK) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
893 }
894
895 s_str = chunk_newstr(&trash);
896 if (h2s) {
897 if (h2s->id <= 0)
898 chunk_appendf(&trash, "dsi=%d ", h2s->h2c->dsi);
899 chunk_appendf(&trash, "h2s=%p(%d,%s) ", h2s, h2s->id, h2s_st_to_str(h2s->st));
900 }
901 else if (h2c)
902 chunk_appendf(&trash, "dsi=%d ", h2c->dsi);
903
904 n_ist = ist2(chunk_newstr(&trash), 0);
905 istscpy(&n_ist, hn, 256);
906 trash.data += n_ist.len;
907 if (n_ist.len != hn.len)
908 chunk_appendf(&trash, " (... +%ld)", (long)(hn.len - n_ist.len));
909
910 v_ist = ist2(chunk_newstr(&trash), 0);
911 istscpy(&v_ist, hv, 1024);
912 trash.data += v_ist.len;
913 if (v_ist.len != hv.len)
914 chunk_appendf(&trash, " (... +%ld)", (long)(hv.len - v_ist.len));
915
916 TRACE_PRINTF_LOC(TRACE_LEVEL_USER, mask, trc_loc, func,
Christopher Fauletc2545162023-01-30 08:26:09 +0100917 (h2c ? h2c->conn : 0), 0, 0, 0,
Willy Tarreau11e8a8c2023-01-24 19:43:11 +0100918 "%s%s%s %s: %s", c_str, s_str,
919 (mask & H2_EV_TX_HDR) ? "sndh" : "rcvh",
920 n_ist.ptr, v_ist.ptr);
921}
922
923/* produce a trace of the header after checking that tracing is enabled */
924static inline void h2_trace_header(const struct ist hn, const struct ist hv,
925 uint64_t mask, const struct ist trc_loc, const char *func,
926 const struct h2c *h2c, const struct h2s *h2s)
927{
928 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
929 TRACE_ENABLED(TRACE_LEVEL_USER, mask, h2c ? h2c->conn : 0, h2s, 0, 0))
930 _h2_trace_header(hn, hv, mask, trc_loc, func, h2c, h2s);
931}
932
933/* hpack-encode header name <hn> and value <hv>, possibly emitting a trace if
934 * currently enabled. This is done on behalf of function <func> at <trc_loc>
935 * passed as ist(TRC_LOC), h2c <h2c>, and h2s <h2s>, all of which may be NULL.
936 * The trace is only emitted if the header is emitted (in which case non-zero
937 * is returned). The trash is modified. In the traces, the header's name will
938 * be truncated to 256 chars and the header's value to 1024 chars.
939 */
940static inline int h2_encode_header(struct buffer *buf, const struct ist hn, const struct ist hv,
941 uint64_t mask, const struct ist trc_loc, const char *func,
942 const struct h2c *h2c, const struct h2s *h2s)
943{
944 int ret;
945
946 ret = hpack_encode_header(buf, hn, hv);
947 if (ret)
948 h2_trace_header(hn, hv, mask, trc_loc, func, h2c, h2s);
949
950 return ret;
951}
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200952
Willy Tarreau62f52692017-10-08 23:01:42 +0200953/*****************************************************************/
954/* functions below are dedicated to the mux setup and management */
955/*****************************************************************/
956
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200957/* Initialize the mux once it's attached. For outgoing connections, the context
958 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200959 * connections from the fact that the context is still NULL (even during mux
960 * upgrades). <input> is always used as Input buffer and may contain data. It is
961 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200962 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200963static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
964 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200965{
966 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100967 struct task *t = NULL;
Christopher Fauletf81ef032019-10-04 15:19:43 +0200968 void *conn_ctx = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200969
Christopher Fauletf81ef032019-10-04 15:19:43 +0200970 TRACE_ENTER(H2_EV_H2C_NEW);
Willy Tarreau7838a792019-08-12 18:42:03 +0200971
Willy Tarreaubafbe012017-11-24 17:34:44 +0100972 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200973 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200974 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200975
Christopher Faulete9b70722019-04-08 10:46:02 +0200976 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200977 h2c->flags = H2_CF_IS_BACK;
978 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
979 if (tick_isset(prx->timeout.serverfin))
980 h2c->shut_timeout = prx->timeout.serverfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100981
982 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
983 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200984 } else {
985 h2c->flags = H2_CF_NONE;
986 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
987 if (tick_isset(prx->timeout.clientfin))
988 h2c->shut_timeout = prx->timeout.clientfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100989
990 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
991 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200992 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100993
Willy Tarreau0b37d652018-10-03 10:33:02 +0200994 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100995 h2c->task = NULL;
Willy Tarreau389ab0d2023-03-20 19:16:04 +0100996 h2c->wait_event.tasklet = NULL;
Willy Tarreau15a47332022-03-18 15:57:34 +0100997 h2c->idle_start = now_ms;
Willy Tarreau3f133572017-10-31 19:21:06 +0100998 if (tick_isset(h2c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200999 t = task_new_here();
Willy Tarreau3f133572017-10-31 19:21:06 +01001000 if (!t)
1001 goto fail;
1002
1003 h2c->task = t;
1004 t->process = h2_timeout_task;
1005 t->context = h2c;
1006 t->expire = tick_add(now_ms, h2c->timeout);
1007 }
Willy Tarreauea392822017-10-31 10:02:25 +01001008
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001009 h2c->wait_event.tasklet = tasklet_new();
1010 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001011 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001012 h2c->wait_event.tasklet->process = h2_io_cb;
1013 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001014 h2c->wait_event.events = 0;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001015 if (!conn_is_back(conn)) {
1016 /* Connection might already be in the stopping_list if subject
1017 * to h1->h2 upgrade.
1018 */
1019 if (!LIST_INLIST(&conn->stopping_list)) {
1020 LIST_APPEND(&mux_stopping_data[tid].list,
1021 &conn->stopping_list);
1022 }
1023 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001024
Willy Tarreau2bdcc702020-05-19 11:31:11 +02001025 h2c->ddht = hpack_dht_alloc();
Willy Tarreau32218eb2017-09-22 08:07:25 +02001026 if (!h2c->ddht)
1027 goto fail;
1028
1029 /* Initialise the context. */
1030 h2c->st0 = H2_CS_PREFACE;
1031 h2c->conn = conn;
Willy Tarreauca1027c2023-04-18 15:57:03 +02001032 h2c->streams_limit = h2c_max_concurrent_streams(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001033 h2c->max_id = -1;
1034 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +01001035 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001036 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +01001037 h2c->nb_streams = 0;
Willy Tarreau36c22322022-05-27 10:41:24 +02001038 h2c->nb_sc = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001039 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001040 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001041
Christopher Faulet51f73eb2019-04-08 11:22:47 +02001042 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001043 h2c->dsi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001044
Willy Tarreau32218eb2017-09-22 08:07:25 +02001045 h2c->last_sid = -1;
1046
Willy Tarreau51330962019-05-26 09:38:07 +02001047 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +02001048 h2c->miw = 65535; /* mux initial window size */
1049 h2c->mws = 65535; /* mux window size */
1050 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +02001051 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001052 LIST_INIT(&h2c->send_list);
1053 LIST_INIT(&h2c->fctl_list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +02001054 LIST_INIT(&h2c->blocked_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +01001055 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001056
Christopher Fauletf81ef032019-10-04 15:19:43 +02001057 conn->ctx = h2c;
1058
Willy Tarreau8e6f7492021-06-16 17:47:24 +02001059 TRACE_USER("new H2 connection", H2_EV_H2C_NEW, conn);
1060
Willy Tarreau3f133572017-10-31 19:21:06 +01001061 if (t)
1062 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +01001063
Willy Tarreau01b44822018-10-03 14:26:37 +02001064 if (h2c->flags & H2_CF_IS_BACK) {
1065 /* FIXME: this is temporary, for outgoing connections we need
1066 * to immediately allocate a stream until the code is modified
Willy Tarreau36c22322022-05-27 10:41:24 +02001067 * so that the caller calls ->attach(). For now the outgoing sc
Christopher Fauletf81ef032019-10-04 15:19:43 +02001068 * is stored as conn->ctx by the caller and saved in conn_ctx.
Willy Tarreau01b44822018-10-03 14:26:37 +02001069 */
1070 struct h2s *h2s;
1071
Christopher Fauletf81ef032019-10-04 15:19:43 +02001072 h2s = h2c_bck_stream_new(h2c, conn_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +02001073 if (!h2s)
1074 goto fail_stream;
1075 }
1076
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001077 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 2);
Willy Tarreau4781b152021-04-06 13:53:36 +02001078 HA_ATOMIC_INC(&h2c->px_counters->open_conns);
1079 HA_ATOMIC_INC(&h2c->px_counters->total_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001080
Willy Tarreau0f383582018-10-03 14:22:21 +02001081 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02001082 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001083 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001084 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +02001085 fail_stream:
1086 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +02001087 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001088 task_destroy(t);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02001089 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001090 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +02001091 fail_no_h2c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001092 if (!conn_is_back(conn))
1093 LIST_DEL_INIT(&conn->stopping_list);
Christopher Fauletf81ef032019-10-04 15:19:43 +02001094 conn->ctx = conn_ctx; /* restore saved ctx */
1095 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001096 return -1;
1097}
1098
Willy Tarreau751f2d02018-10-05 09:35:00 +02001099/* returns the next allocatable outgoing stream ID for the H2 connection, or
1100 * -1 if no more is allocatable.
1101 */
1102static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
1103{
1104 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +01001105
1106 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +02001107 id = -1;
1108 return id;
1109}
1110
Willy Tarreau2373acc2017-10-12 17:35:14 +02001111/* returns the stream associated with id <id> or NULL if not found */
1112static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
1113{
1114 struct eb32_node *node;
1115
Willy Tarreau751f2d02018-10-05 09:35:00 +02001116 if (id == 0)
1117 return (struct h2s *)h2_closed_stream;
1118
Willy Tarreau2a856182017-05-16 15:20:39 +02001119 if (id > h2c->max_id)
1120 return (struct h2s *)h2_idle_stream;
1121
Willy Tarreau2373acc2017-10-12 17:35:14 +02001122 node = eb32_lookup(&h2c->streams_by_id, id);
1123 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +02001124 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +02001125
1126 return container_of(node, struct h2s, by_id);
1127}
1128
Christopher Faulet73c12072019-04-08 11:23:22 +02001129/* release function. This one should be called to free all resources allocated
1130 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +02001131 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001132static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02001133{
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001134 struct connection *conn = h2c->conn;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001135
Willy Tarreau7838a792019-08-12 18:42:03 +02001136 TRACE_ENTER(H2_EV_H2C_END);
1137
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001138 hpack_dht_free(h2c->ddht);
Christopher Faulet61840e72019-04-15 09:33:32 +02001139
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001140 if (LIST_INLIST(&h2c->buf_wait.list))
1141 LIST_DEL_INIT(&h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +02001142
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001143 h2_release_buf(h2c, &h2c->dbuf);
1144 h2_release_mbuf(h2c);
Willy Tarreau14398122017-09-22 14:26:04 +02001145
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001146 if (h2c->task) {
1147 h2c->task->context = NULL;
1148 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
1149 h2c->task = NULL;
1150 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02001151 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001152 if (conn && h2c->wait_event.events != 0)
1153 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
1154 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +01001155
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001156 HA_ATOMIC_DEC(&h2c->px_counters->open_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001157
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001158 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001159
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001160 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001161 if (!conn_is_back(conn))
1162 LIST_DEL_INIT(&conn->stopping_list);
1163
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001164 conn->mux = NULL;
1165 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02001166 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001167
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001168 conn_stop_tracking(conn);
Willy Tarreau0b222472021-10-21 22:24:31 +02001169
1170 /* there might be a GOAWAY frame still pending in the TCP
1171 * stack, and if the peer continues to send (i.e. window
1172 * updates etc), this can result in losing the GOAWAY. For
1173 * this reason we try to drain anything received in between.
1174 */
1175 conn->flags |= CO_FL_WANT_DRAIN;
1176
1177 conn_xprt_shutw(conn);
1178 conn_xprt_close(conn);
1179 conn_sock_shutw(conn, !conn_is_back(conn));
1180 conn_ctrl_close(conn);
1181
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001182 if (conn->destroy_cb)
1183 conn->destroy_cb(conn);
1184 conn_free(conn);
1185 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001186
1187 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +02001188}
1189
1190
Willy Tarreau71681172017-10-23 14:39:06 +02001191/******************************************************/
1192/* functions below are for the H2 protocol processing */
1193/******************************************************/
1194
1195/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +01001196static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +02001197{
1198 return h2s ? h2s->id : 0;
1199}
1200
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001201/* returns the sum of the stream's own window size and the mux's initial
1202 * window, which together form the stream's effective window size.
1203 */
1204static inline int h2s_mws(const struct h2s *h2s)
1205{
1206 return h2s->sws + h2s->h2c->miw;
1207}
1208
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001209/* marks an error on the connection. Before settings are sent, we must not send
1210 * a GOAWAY frame, and the error state will prevent h2c_send_goaway_error()
1211 * from verifying this so we set H2_CF_GOAWAY_FAILED to make sure it will not
1212 * even try.
1213 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001214static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +02001215{
Willy Tarreau022e5e52020-09-10 09:33:15 +02001216 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn, 0, 0, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +02001217 h2c->errcode = err;
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001218 if (h2c->st0 < H2_CS_SETTINGS1)
1219 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau741d6df2017-10-17 08:00:59 +02001220 h2c->st0 = H2_CS_ERROR;
1221}
1222
Willy Tarreau175cebb2019-01-24 10:02:24 +01001223/* marks an error on the stream. It may also update an already closed stream
1224 * (e.g. to report an error after an RST was received).
1225 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001226static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +02001227{
Willy Tarreau175cebb2019-01-24 10:02:24 +01001228 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001229 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s, 0, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +02001230 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +01001231 if (h2s->st < H2_SS_ERROR)
1232 h2s->st = H2_SS_ERROR;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001233 se_fl_set_error(h2s->sd);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001234 }
1235}
1236
Willy Tarreau7e094452018-12-19 18:08:52 +01001237/* attempt to notify the data layer of recv availability */
1238static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1239{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001240 if (h2s->subs && h2s->subs->events & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001241 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001242 tasklet_wakeup(h2s->subs->tasklet);
1243 h2s->subs->events &= ~SUB_RETRY_RECV;
1244 if (!h2s->subs->events)
1245 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001246 }
1247}
1248
1249/* attempt to notify the data layer of send availability */
1250static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1251{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001252 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001253 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01001254 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01001255 tasklet_wakeup(h2s->subs->tasklet);
1256 h2s->subs->events &= ~SUB_RETRY_SEND;
1257 if (!h2s->subs->events)
1258 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001259 }
Willy Tarreau5723f292020-01-10 15:16:57 +01001260 else if (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) {
1261 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
1262 tasklet_wakeup(h2s->shut_tl);
1263 }
Willy Tarreau7e094452018-12-19 18:08:52 +01001264}
1265
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001266/* alerts the data layer, trying to wake it up by all means, following
1267 * this sequence :
1268 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1269 * - if its subscribed to send, then it's woken up for send
1270 * - if it was subscribed to neither, its ->wake() callback is called
1271 * It is safe to call this function with a closed stream which doesn't have a
Willy Tarreau4596fe22022-05-17 19:07:51 +02001272 * stream connector anymore.
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001273 */
1274static void __maybe_unused h2s_alert(struct h2s *h2s)
1275{
Willy Tarreau7838a792019-08-12 18:42:03 +02001276 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1277
Willy Tarreauf96508a2020-01-10 11:12:48 +01001278 if (h2s->subs ||
Willy Tarreau5723f292020-01-10 15:16:57 +01001279 (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW))) {
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001280 h2s_notify_recv(h2s);
1281 h2s_notify_send(h2s);
1282 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02001283 else if (h2s_sc(h2s) && h2s_sc(h2s)->app_ops->wake != NULL) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001284 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02001285 h2s_sc(h2s)->app_ops->wake(h2s_sc(h2s));
Willy Tarreau7838a792019-08-12 18:42:03 +02001286 }
1287
1288 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001289}
1290
Willy Tarreaue4820742017-07-27 13:37:23 +02001291/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001292static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001293{
1294 uint8_t *out = frame;
1295
1296 *out = len >> 16;
1297 write_n16(out + 1, len);
1298}
1299
Willy Tarreau54c15062017-10-10 17:10:03 +02001300/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1301 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1302 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001303 * available in the buffer's input prior to calling this function. The buffer
1304 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001305 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001306static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001307 const struct buffer *b, int o)
1308{
Willy Tarreau591d4452018-06-15 17:21:00 +02001309 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 +02001310}
1311
Willy Tarreau1f094672017-11-20 21:27:45 +01001312static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001313{
Willy Tarreau591d4452018-06-15 17:21:00 +02001314 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001315}
1316
Willy Tarreau1f094672017-11-20 21:27:45 +01001317static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001318{
Willy Tarreau591d4452018-06-15 17:21:00 +02001319 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001320}
1321
Willy Tarreau1f094672017-11-20 21:27:45 +01001322static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001323{
Willy Tarreau591d4452018-06-15 17:21:00 +02001324 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001325}
1326
1327
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001328/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1329 * The algorithm is not obvious. It turns out that H2 headers are neither
1330 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1331 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001332 *
1333 * b0 b1 b2 b3 b4 b5..b8
1334 * +----------+---------+--------+----+----+----------------------+
1335 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1336 * +----------+---------+--------+----+----+----------------------+
1337 *
1338 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1339 * we get the sid properly aligned and ordered, and 16 bits of len properly
1340 * ordered as well. The type and flags can be extracted using bit shifts from
1341 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001342 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1343 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001344 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001345static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001346{
1347 uint64_t w;
1348
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001349 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001350 return 0;
1351
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001352 w = h2_get_n64(b, o + 1);
1353 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001354 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1355 h->ff = w >> 32;
1356 h->ft = w >> 40;
1357 h->len += w >> 48;
1358 return 1;
1359}
1360
1361/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1362 * h2_peek_frame_hdr() above.
1363 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001364static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001365{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001366 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001367}
1368
1369/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001370static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001371{
1372 int ret;
1373
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001374 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001375 if (ret > 0)
1376 h2_skip_frame_hdr(b);
1377 return ret;
1378}
1379
Willy Tarreaucb985a42019-10-07 16:56:34 +02001380
1381/* try to fragment the headers frame present at the beginning of buffer <b>,
1382 * enforcing a limit of <mfs> bytes per frame. Returns 0 on failure, 1 on
1383 * success. Typical causes of failure include a buffer not large enough to
1384 * add extra frame headers. The existing frame size is read in the current
1385 * frame. Its EH flag will be cleared if CONTINUATION frames need to be added,
1386 * and its length will be adjusted. The stream ID for continuation frames will
1387 * be copied from the initial frame's.
1388 */
1389static int h2_fragment_headers(struct buffer *b, uint32_t mfs)
1390{
1391 size_t remain = b->data - 9;
1392 int extra_frames = (remain - 1) / mfs;
1393 size_t fsize;
1394 char *fptr;
1395 int frame;
1396
1397 if (b->data <= mfs + 9)
1398 return 1;
1399
1400 /* Too large a frame, we need to fragment it using CONTINUATION
1401 * frames. We start from the end and move tails as needed.
1402 */
1403 if (b->data + extra_frames * 9 > b->size)
1404 return 0;
1405
1406 for (frame = extra_frames; frame; frame--) {
1407 fsize = ((remain - 1) % mfs) + 1;
1408 remain -= fsize;
1409
1410 /* move data */
1411 fptr = b->area + 9 + remain + (frame - 1) * 9;
1412 memmove(fptr + 9, b->area + 9 + remain, fsize);
1413 b->data += 9;
1414
1415 /* write new frame header */
1416 h2_set_frame_size(fptr, fsize);
1417 fptr[3] = H2_FT_CONTINUATION;
1418 fptr[4] = (frame == extra_frames) ? H2_F_HEADERS_END_HEADERS : 0;
1419 write_n32(fptr + 5, read_n32(b->area + 5));
1420 }
1421
1422 b->area[4] &= ~H2_F_HEADERS_END_HEADERS;
1423 h2_set_frame_size(b->area, remain);
1424 return 1;
1425}
1426
1427
Willy Tarreau00dd0782018-03-01 16:31:34 +01001428/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1429 * its connection if the stream was not yet closed. Please use this exclusively
Willy Tarreaubcdc6cc2023-03-20 19:14:47 +01001430 * before closing a stream to ensure stream count is well maintained. Note that
1431 * it does explicitly support being called with a partially initialized h2s
1432 * (e.g. sd==NULL).
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001433 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001434static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001435{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001436 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001437 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001438 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001439 if (!h2s->id)
1440 h2s->h2c->nb_reserved--;
Willy Tarreaubcdc6cc2023-03-20 19:14:47 +01001441 if (h2s->sd && h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001442 if (!se_fl_test(h2s->sd, SE_FL_EOS) && !b_data(&h2s->rxbuf))
Willy Tarreaua27db382019-03-25 18:13:16 +01001443 h2s_notify_recv(h2s);
1444 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001445 HA_ATOMIC_DEC(&h2s->h2c->px_counters->open_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001446
Willy Tarreau7838a792019-08-12 18:42:03 +02001447 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001448 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001449 h2s->st = H2_SS_CLOSED;
1450}
1451
Christopher Faulet531dd052023-05-24 11:14:38 +02001452/* Check h2c and h2s flags to evaluate if EOI/EOS/ERR_PENDING/ERROR flags must
1453 * be set on the SE.
1454 */
1455static inline void h2s_propagate_term_flags(struct h2c *h2c, struct h2s *h2s)
1456{
1457 if (h2s->flags & H2_SF_ES_RCVD) {
1458 se_fl_set(h2s->sd, SE_FL_EOI);
1459 /* Add EOS flag for tunnel */
1460 if (h2s->flags & H2_SF_BODY_TUNNEL)
1461 se_fl_set(h2s->sd, SE_FL_EOS);
1462 }
1463 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED) {
1464 se_fl_set(h2s->sd, SE_FL_EOS);
1465 if (!se_fl_test(h2s->sd, SE_FL_EOI))
1466 se_fl_set(h2s->sd, SE_FL_ERROR);
1467 }
1468 if (se_fl_test(h2s->sd, SE_FL_ERR_PENDING))
1469 se_fl_set(h2s->sd, SE_FL_ERROR);
1470}
1471
Willy Tarreau71049cc2018-03-28 13:56:39 +02001472/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001473/* h2s_destroy should only ever be called by the thread that owns the stream,
1474 * that means that a tasklet should be used if we want to destroy the h2s
1475 * from another thread
1476 */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001477static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001478{
Willy Tarreau7838a792019-08-12 18:42:03 +02001479 struct connection *conn = h2s->h2c->conn;
1480
1481 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1482
Willy Tarreau0a10de62018-03-01 16:27:53 +01001483 h2s_close(h2s);
1484 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001485 if (b_size(&h2s->rxbuf)) {
1486 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001487 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02001488 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001489
1490 if (h2s->subs)
1491 h2s->subs->events = 0;
1492
Joseph Herlantd77575d2018-11-25 10:54:45 -08001493 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001494 * reference left would be in the h2c send_list/fctl_list, and if
1495 * we're in it, we're getting out anyway
1496 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02001497 h2_remove_from_list(h2s);
Willy Tarreau5723f292020-01-10 15:16:57 +01001498
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001499 /* ditto, calling tasklet_free() here should be ok */
Willy Tarreau5723f292020-01-10 15:16:57 +01001500 tasklet_free(h2s->shut_tl);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001501 BUG_ON(h2s->sd && !se_fl_test(h2s->sd, SE_FL_ORPHAN));
1502 sedesc_free(h2s->sd);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001503 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001504
1505 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001506}
1507
Willy Tarreaua8e49542018-10-03 18:53:55 +02001508/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1509 * stream tree. In case of error, nothing is added and NULL is returned. The
1510 * causes of errors can be any failed memory allocation. The caller is
1511 * responsible for checking if the connection may support an extra stream
1512 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001513 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001514static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001515{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001516 struct h2s *h2s;
1517
Willy Tarreau7838a792019-08-12 18:42:03 +02001518 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1519
Willy Tarreaubafbe012017-11-24 17:34:44 +01001520 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001521 if (!h2s)
1522 goto out;
1523
Willy Tarreau5723f292020-01-10 15:16:57 +01001524 h2s->shut_tl = tasklet_new();
1525 if (!h2s->shut_tl) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001526 pool_free(pool_head_h2s, h2s);
1527 goto out;
1528 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001529 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01001530 h2s->shut_tl->process = h2_deferred_shut;
1531 h2s->shut_tl->context = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001532 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001533 h2s->h2c = h2c;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001534 h2s->sd = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001535 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001536 h2s->flags = H2_SF_NONE;
1537 h2s->errcode = H2_ERR_NO_ERROR;
1538 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001539 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001540 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001541 h2s->rxbuf = BUF_NULL;
Amaury Denoyelle74162742020-12-11 17:53:05 +01001542 memset(h2s->upgrade_protocol, 0, sizeof(h2s->upgrade_protocol));
Willy Tarreau751f2d02018-10-05 09:35:00 +02001543
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001544 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001545 if (id > 0)
1546 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001547 else
1548 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001549
1550 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001551 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001552 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001553
Willy Tarreau4781b152021-04-06 13:53:36 +02001554 HA_ATOMIC_INC(&h2c->px_counters->open_streams);
1555 HA_ATOMIC_INC(&h2c->px_counters->total_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001556
Willy Tarreau7838a792019-08-12 18:42:03 +02001557 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001558 return h2s;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001559 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001560 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001561 return NULL;
1562}
1563
1564/* creates a new stream <id> on the h2c connection and returns it, or NULL in
Christopher Faulet7d013e72020-12-15 16:56:50 +01001565 * case of memory allocation error. <input> is used as input buffer for the new
1566 * stream. On success, it is transferred to the stream and the mux is no longer
1567 * responsible of it. On error, <input> is unchanged, thus the mux must still
1568 * take care of it.
Willy Tarreaua8e49542018-10-03 18:53:55 +02001569 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02001570static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id, struct buffer *input, uint32_t flags)
Willy Tarreaua8e49542018-10-03 18:53:55 +02001571{
1572 struct session *sess = h2c->conn->owner;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001573 struct h2s *h2s;
1574
Willy Tarreau7838a792019-08-12 18:42:03 +02001575 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1576
Willy Tarreauca1027c2023-04-18 15:57:03 +02001577 if (h2c->nb_streams >= h2c_max_concurrent_streams(h2c)) {
Willy Tarreaue872f752022-05-12 09:24:41 +02001578 TRACE_ERROR("HEADERS frame causing MAX_CONCURRENT_STREAMS to be exceeded", H2_EV_H2S_NEW|H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02001579 session_inc_http_req_ctr(sess);
1580 session_inc_http_err_ctr(sess);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001581 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001582 }
Willy Tarreaua8e49542018-10-03 18:53:55 +02001583
1584 h2s = h2s_new(h2c, id);
1585 if (!h2s)
Willy Tarreaue872f752022-05-12 09:24:41 +02001586 goto out_alloc;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001587
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001588 h2s->sd = sedesc_new();
1589 if (!h2s->sd)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001590 goto out_close;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001591 h2s->sd->se = h2s;
1592 h2s->sd->conn = h2c->conn;
1593 se_fl_set(h2s->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
Christopher Faulet4403cdf2023-05-04 15:49:12 +02001594 /* The request is not finished, don't expect data from the opposite side
1595 * yet
1596 */
Christopher Faulet78b1eb22023-05-24 11:44:53 +02001597 if (!(h2c->dff & (H2_F_HEADERS_END_STREAM| H2_F_DATA_END_STREAM)) && !(flags & H2_SF_BODY_TUNNEL))
Christopher Faulet4403cdf2023-05-04 15:49:12 +02001598 se_expect_no_data(h2s->sd);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001599
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02001600 /* FIXME wrong analogy between ext-connect and websocket, this need to
1601 * be refine.
1602 */
1603 if (flags & H2_SF_EXT_CONNECT_RCVD)
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001604 se_fl_set(h2s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +01001605
Willy Tarreaud0de6772022-02-04 09:05:37 +01001606 /* The stream will record the request's accept date (which is either the
1607 * end of the connection's or the date immediately after the previous
1608 * request) and the idle time, which is the delay since the previous
1609 * request. We can set the value now, it will be copied by stream_new().
1610 */
Willy Tarreau69530f52023-04-28 09:16:15 +02001611 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001612
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001613 if (!sc_new_from_endp(h2s->sd, sess, input))
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001614 goto out_close;
Willy Tarreaucd6bb1a2022-05-10 15:00:03 +02001615
Willy Tarreau36c22322022-05-27 10:41:24 +02001616 h2c->nb_sc++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001617
Willy Tarreau590a0512018-09-05 11:56:48 +02001618 /* We want the accept date presented to the next stream to be the one
1619 * we have now, the handshake time to be null (since the next stream
1620 * is not delayed by a handshake), and the idle time to count since
1621 * right now.
1622 */
1623 sess->accept_date = date;
Willy Tarreau69530f52023-04-28 09:16:15 +02001624 sess->accept_ts = now_ns;
Willy Tarreau590a0512018-09-05 11:56:48 +02001625 sess->t_handshake = 0;
Willy Tarreaud0de6772022-02-04 09:05:37 +01001626 sess->t_idle = 0;
Willy Tarreau590a0512018-09-05 11:56:48 +02001627
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001628 /* OK done, the stream lives its own life now */
Willy Tarreau36c22322022-05-27 10:41:24 +02001629 if (h2_frt_has_too_many_sc(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001630 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001631 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001632 return h2s;
1633
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001634 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001635 h2s_destroy(h2s);
Willy Tarreaue872f752022-05-12 09:24:41 +02001636 out_alloc:
1637 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW|H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001638 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001639 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001640 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001641 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001642}
1643
Willy Tarreau36c22322022-05-27 10:41:24 +02001644/* allocates a new stream associated to stream connector <sc> on the h2c
Willy Tarreau4596fe22022-05-17 19:07:51 +02001645 * connection and returns it, or NULL in case of memory allocation error or if
1646 * the highest possible stream ID was reached.
Willy Tarreau751f2d02018-10-05 09:35:00 +02001647 */
Willy Tarreau36c22322022-05-27 10:41:24 +02001648static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct stconn *sc, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001649{
1650 struct h2s *h2s = NULL;
1651
Willy Tarreau7838a792019-08-12 18:42:03 +02001652 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1653
Willy Tarreaue872f752022-05-12 09:24:41 +02001654 if (h2c->nb_streams >= h2c->streams_limit) {
1655 TRACE_ERROR("Aborting stream since negotiated limit is too low", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001656 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001657 }
Willy Tarreau751f2d02018-10-05 09:35:00 +02001658
Willy Tarreaue872f752022-05-12 09:24:41 +02001659 if (h2_streams_left(h2c) < 1) {
1660 TRACE_ERROR("Aborting stream since no more streams left", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreaua80dca82019-01-24 17:08:28 +01001661 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001662 }
Willy Tarreaua80dca82019-01-24 17:08:28 +01001663
Willy Tarreau751f2d02018-10-05 09:35:00 +02001664 /* Defer choosing the ID until we send the first message to create the stream */
1665 h2s = h2s_new(h2c, 0);
Willy Tarreaue872f752022-05-12 09:24:41 +02001666 if (!h2s) {
1667 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001668 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001669 }
Willy Tarreau751f2d02018-10-05 09:35:00 +02001670
Willy Tarreau36c22322022-05-27 10:41:24 +02001671 if (sc_attach_mux(sc, h2s, h2c->conn) < 0) {
Willy Tarreaue872f752022-05-12 09:24:41 +02001672 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW, h2c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +02001673 h2s_destroy(h2s);
1674 h2s = NULL;
1675 goto out;
1676 }
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001677 h2s->sd = sc->sedesc;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001678 h2s->sess = sess;
Willy Tarreau36c22322022-05-27 10:41:24 +02001679 h2c->nb_sc++;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001680
Willy Tarreau751f2d02018-10-05 09:35:00 +02001681 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001682 if (likely(h2s))
1683 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1684 else
1685 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001686 return h2s;
1687}
1688
Willy Tarreaube5b7152017-09-25 16:25:39 +02001689/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1690 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1691 * the various settings codes.
1692 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001693static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001694{
1695 struct buffer *res;
1696 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001697 struct buffer buf;
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001698 int iws;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001699 int mfs;
Willy Tarreauca1027c2023-04-18 15:57:03 +02001700 int mcs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001701 int ret = 0;
1702
1703 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001704
Willy Tarreaube5b7152017-09-25 16:25:39 +02001705 chunk_init(&buf, buf_data, sizeof(buf_data));
1706 chunk_memcpy(&buf,
1707 "\x00\x00\x00" /* length : 0 for now */
1708 "\x04\x00" /* type : 4 (settings), flags : 0 */
1709 "\x00\x00\x00\x00", /* stream ID : 0 */
1710 9);
1711
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001712 if (h2c->flags & H2_CF_IS_BACK) {
1713 /* send settings_enable_push=0 */
1714 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1715 }
1716
Amaury Denoyellebefeae82021-07-09 17:14:30 +02001717 /* rfc 8441 #3 SETTINGS_ENABLE_CONNECT_PROTOCOL=1,
1718 * sent automatically unless disabled in the global config */
1719 if (!(global.tune.options & GTUNE_DISABLE_H2_WEBSOCKET))
1720 chunk_memcat(&buf, "\x00\x08\x00\x00\x00\x01", 6);
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01001721
Willy Tarreaube5b7152017-09-25 16:25:39 +02001722 if (h2_settings_header_table_size != 4096) {
1723 char str[6] = "\x00\x01"; /* header_table_size */
1724
1725 write_n32(str + 2, h2_settings_header_table_size);
1726 chunk_memcat(&buf, str, 6);
1727 }
1728
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001729 iws = (h2c->flags & H2_CF_IS_BACK) ?
1730 h2_be_settings_initial_window_size:
1731 h2_fe_settings_initial_window_size;
1732 iws = iws ? iws : h2_settings_initial_window_size;
1733
1734 if (iws != 65535) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001735 char str[6] = "\x00\x04"; /* initial_window_size */
1736
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001737 write_n32(str + 2, iws);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001738 chunk_memcat(&buf, str, 6);
1739 }
1740
Willy Tarreauca1027c2023-04-18 15:57:03 +02001741 mcs = h2c_max_concurrent_streams(h2c);
1742 if (mcs != 0) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001743 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1744
1745 /* Note: 0 means "unlimited" for haproxy's config but not for
1746 * the protocol, so never send this value!
1747 */
Willy Tarreauca1027c2023-04-18 15:57:03 +02001748 write_n32(str + 2, mcs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001749 chunk_memcat(&buf, str, 6);
1750 }
1751
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001752 mfs = h2_settings_max_frame_size;
1753 if (mfs > global.tune.bufsize)
1754 mfs = global.tune.bufsize;
1755
1756 if (!mfs)
1757 mfs = global.tune.bufsize;
1758
1759 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001760 char str[6] = "\x00\x05"; /* max_frame_size */
1761
1762 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1763 * match bufsize - rewrite size, but at the moment it seems
1764 * that clients don't take care of it.
1765 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001766 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001767 chunk_memcat(&buf, str, 6);
1768 }
1769
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001770 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001771
1772 res = br_tail(h2c->mbuf);
1773 retry:
1774 if (!h2_get_buf(h2c, res)) {
1775 h2c->flags |= H2_CF_MUX_MALLOC;
1776 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001777 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001778 }
1779
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001780 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001781 if (unlikely(ret <= 0)) {
1782 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001783 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1784 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001785 h2c->flags |= H2_CF_MUX_MFULL;
1786 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001787 }
1788 else {
1789 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001790 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001791 }
1792 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001793 out:
1794 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001795 return ret;
1796}
1797
Willy Tarreau52eed752017-09-22 15:05:09 +02001798/* Try to receive a connection preface, then upon success try to send our
1799 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1800 * missing data. It may return an error in h2c.
1801 */
1802static int h2c_frt_recv_preface(struct h2c *h2c)
1803{
1804 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001805 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001806
Willy Tarreau7838a792019-08-12 18:42:03 +02001807 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1808
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001809 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001810
1811 if (unlikely(ret1 <= 0)) {
Christopher Fauletb5f7b522021-07-26 12:06:53 +02001812 if (!ret1)
1813 h2c->flags |= H2_CF_DEM_SHORT_READ;
Christopher Fauletff7925d2022-10-11 19:12:40 +02001814 if (ret1 < 0 || (h2c->flags & H2_CF_RCVD_SHUT)) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01001815 TRACE_ERROR("I/O error or short read", H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02001816 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauee4684f2021-06-17 08:08:48 +02001817 if (b_data(&h2c->dbuf) ||
1818 !(((const struct session *)h2c->conn->owner)->fe->options & PR_O_IGNORE_PRB))
1819 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01001820 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001821 ret2 = 0;
1822 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001823 }
1824
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001825 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001826 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001827 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001828 out:
1829 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001830 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001831}
1832
Willy Tarreau01b44822018-10-03 14:26:37 +02001833/* Try to send a connection preface, then upon success try to send our
1834 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1835 * missing data. It may return an error in h2c.
1836 */
1837static int h2c_bck_send_preface(struct h2c *h2c)
1838{
1839 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001840 int ret = 0;
1841
1842 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001843
Willy Tarreaubcc45952019-05-26 10:05:50 +02001844 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001845 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001846 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001847 h2c->flags |= H2_CF_MUX_MALLOC;
1848 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001849 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001850 }
1851
1852 if (!b_data(res)) {
1853 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001854 ret = b_istput(res, ist(H2_CONN_PREFACE));
1855 if (unlikely(ret <= 0)) {
1856 if (!ret) {
1857 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1858 goto retry;
1859 h2c->flags |= H2_CF_MUX_MFULL;
1860 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001861 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001862 }
1863 else {
1864 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001865 ret = 0;
1866 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001867 }
1868 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001869 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001870 ret = h2c_send_settings(h2c);
1871 out:
1872 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1873 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001874}
1875
Willy Tarreau081d4722017-05-16 21:51:05 +02001876/* try to send a GOAWAY frame on the connection to report an error or a graceful
1877 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1878 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1879 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1880 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1881 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1882 * on unrecoverable failure. It will not attempt to send one again in this last
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001883 * case, nor will it send one if settings were not sent (e.g. still waiting for
1884 * a preface) so that it is safe to use h2c_error() to report such errors.
Willy Tarreau081d4722017-05-16 21:51:05 +02001885 */
1886static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1887{
1888 struct buffer *res;
1889 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001890 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001891
Willy Tarreau7838a792019-08-12 18:42:03 +02001892 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1893
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001894 if ((h2c->flags & H2_CF_GOAWAY_FAILED) || h2c->st0 < H2_CS_SETTINGS1) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001895 ret = 1; // claim that it worked
1896 goto out;
1897 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001898
Willy Tarreau9c218e72019-05-26 10:08:28 +02001899 /* len: 8, type: 7, flags: none, sid: 0 */
1900 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1901
1902 if (h2c->last_sid < 0)
1903 h2c->last_sid = h2c->max_id;
1904
1905 write_n32(str + 9, h2c->last_sid);
1906 write_n32(str + 13, h2c->errcode);
1907
Willy Tarreaubcc45952019-05-26 10:05:50 +02001908 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001909 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001910 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001911 h2c->flags |= H2_CF_MUX_MALLOC;
1912 if (h2s)
1913 h2s->flags |= H2_SF_BLK_MROOM;
1914 else
1915 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001916 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001917 }
1918
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001919 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001920 if (unlikely(ret <= 0)) {
1921 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001922 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1923 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001924 h2c->flags |= H2_CF_MUX_MFULL;
1925 if (h2s)
1926 h2s->flags |= H2_SF_BLK_MROOM;
1927 else
1928 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001929 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001930 }
1931 else {
1932 /* we cannot report this error using GOAWAY, so we mark
1933 * it and claim a success.
1934 */
1935 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1936 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001937 ret = 1;
1938 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001939 }
1940 }
1941 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001942
1943 /* some codes are not for real errors, just attempts to close cleanly */
1944 switch (h2c->errcode) {
1945 case H2_ERR_NO_ERROR:
1946 case H2_ERR_ENHANCE_YOUR_CALM:
1947 case H2_ERR_REFUSED_STREAM:
1948 case H2_ERR_CANCEL:
1949 break;
1950 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001951 HA_ATOMIC_INC(&h2c->px_counters->goaway_resp);
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001952 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001953 out:
1954 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001955 return ret;
1956}
1957
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001958/* Try to send an RST_STREAM frame on the connection for the indicated stream
1959 * during mux operations. This stream must be valid and cannot be closed
1960 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1961 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1962 * not yet.
1963 *
1964 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1965 * to write the message, it subscribes the stream to future notifications.
1966 */
1967static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1968{
1969 struct buffer *res;
1970 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001971 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001972
Willy Tarreau7838a792019-08-12 18:42:03 +02001973 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1974
1975 if (!h2s || h2s->st == H2_SS_CLOSED) {
1976 ret = 1;
1977 goto out;
1978 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001979
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001980 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1981 * RST_STREAM in response to a RST_STREAM frame.
1982 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001983 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001984 ret = 1;
1985 goto ignore;
1986 }
1987
Willy Tarreau9c218e72019-05-26 10:08:28 +02001988 /* len: 4, type: 3, flags: none */
1989 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1990 write_n32(str + 5, h2s->id);
1991 write_n32(str + 9, h2s->errcode);
1992
Willy Tarreaubcc45952019-05-26 10:05:50 +02001993 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001994 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001995 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001996 h2c->flags |= H2_CF_MUX_MALLOC;
1997 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001998 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001999 }
2000
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002001 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002002 if (unlikely(ret <= 0)) {
2003 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002004 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2005 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002006 h2c->flags |= H2_CF_MUX_MFULL;
2007 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002008 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002009 }
2010 else {
2011 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002012 ret = 0;
2013 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002014 }
2015 }
2016
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002017 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002018 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01002019 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002020 out:
2021 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002022 return ret;
2023}
2024
2025/* Try to send an RST_STREAM frame on the connection for the stream being
2026 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01002027 * error code, even if the stream is one of the dummy ones, and will update
2028 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002029 *
2030 * Returns > 0 on success or zero if nothing was done. In case of lack of room
2031 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08002032 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002033 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02002034 */
2035static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
2036{
2037 struct buffer *res;
2038 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002039 int ret = 0;
2040
2041 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002042
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002043 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
2044 * RST_STREAM in response to a RST_STREAM frame.
2045 */
2046 if (h2c->dft == H2_FT_RST_STREAM) {
2047 ret = 1;
2048 goto ignore;
2049 }
2050
Willy Tarreau9c218e72019-05-26 10:08:28 +02002051 /* len: 4, type: 3, flags: none */
2052 memcpy(str, "\x00\x00\x04\x03\x00", 5);
2053
2054 write_n32(str + 5, h2c->dsi);
2055 write_n32(str + 9, h2s->errcode);
2056
Willy Tarreaubcc45952019-05-26 10:05:50 +02002057 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002058 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002059 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02002060 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002061 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002062 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002063 }
2064
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002065 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02002066 if (unlikely(ret <= 0)) {
2067 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002068 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2069 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002070 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002071 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002072 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002073 }
2074 else {
2075 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002076 ret = 0;
2077 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002078 }
2079 }
2080
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002081 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02002082 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02002083 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01002084 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002085 }
2086
Willy Tarreau7838a792019-08-12 18:42:03 +02002087 out:
Willy Tarreau4781b152021-04-06 13:53:36 +02002088 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_resp);
Willy Tarreau7838a792019-08-12 18:42:03 +02002089 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002090 return ret;
2091}
2092
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002093/* try to send an empty DATA frame with the ES flag set to notify about the
2094 * end of stream and match a shutdown(write). If an ES was already sent as
2095 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
2096 * on success or zero if nothing was done. In case of lack of room to write the
2097 * message, it subscribes the requesting stream to future notifications.
2098 */
2099static int h2_send_empty_data_es(struct h2s *h2s)
2100{
2101 struct h2c *h2c = h2s->h2c;
2102 struct buffer *res;
2103 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002104 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002105
Willy Tarreau7838a792019-08-12 18:42:03 +02002106 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
2107
2108 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
2109 ret = 1;
2110 goto out;
2111 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002112
Willy Tarreau9c218e72019-05-26 10:08:28 +02002113 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
2114 memcpy(str, "\x00\x00\x00\x00\x01", 5);
2115 write_n32(str + 5, h2s->id);
2116
Willy Tarreaubcc45952019-05-26 10:05:50 +02002117 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002118 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002119 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002120 h2c->flags |= H2_CF_MUX_MALLOC;
2121 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002122 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002123 }
2124
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002125 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002126 if (likely(ret > 0)) {
2127 h2s->flags |= H2_SF_ES_SENT;
2128 }
2129 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002130 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2131 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002132 h2c->flags |= H2_CF_MUX_MFULL;
2133 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002134 }
2135 else {
2136 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002137 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002138 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002139 out:
2140 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002141 return ret;
2142}
2143
Willy Tarreau4596fe22022-05-17 19:07:51 +02002144/* wake a specific stream and assign its stream connector some SE_FL_* flags
2145 * among SE_FL_ERR_PENDING and SE_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002146 * is automatically updated accordingly. If the stream is orphaned, it is
2147 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01002148 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002149static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01002150{
Willy Tarreau7838a792019-08-12 18:42:03 +02002151 struct h2c *h2c = h2s->h2c;
2152
2153 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
2154
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002155 if (!h2s_sc(h2s)) {
Christopher Fauletf02ca002019-03-07 16:21:34 +01002156 /* this stream was already orphaned */
2157 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002158 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002159 return;
2160 }
2161
Christopher Fauletaade4ed2020-10-08 15:38:41 +02002162 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002163 if (h2s->st == H2_SS_OPEN)
2164 h2s->st = H2_SS_HREM;
2165 else if (h2s->st == H2_SS_HLOC)
2166 h2s_close(h2s);
2167 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002168
Christopher Fauletff7925d2022-10-11 19:12:40 +02002169 if (h2s->h2c->st0 >= H2_CS_ERROR || (h2s->h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) ||
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002170 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02002171 se_fl_set_error(h2s->sd);
Willy Tarreau23482912019-05-07 15:23:14 +02002172
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002173 if (h2s->st < H2_SS_ERROR)
2174 h2s->st = H2_SS_ERROR;
2175 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01002176
2177 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002178 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002179}
2180
2181/* wake the streams attached to the connection, whose id is greater than <last>
2182 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002183 */
Willy Tarreau23482912019-05-07 15:23:14 +02002184static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002185{
2186 struct eb32_node *node;
2187 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002188
Willy Tarreau7838a792019-08-12 18:42:03 +02002189 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
2190
Christopher Fauletf02ca002019-03-07 16:21:34 +01002191 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002192 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
2193 while (node) {
2194 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002195 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002196 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002197 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002198
Christopher Fauletf02ca002019-03-07 16:21:34 +01002199 /* Wake all streams with unassigned ID (ID == 0) */
2200 node = eb32_lookup(&h2c->streams_by_id, 0);
2201 while (node) {
2202 h2s = container_of(node, struct h2s, by_id);
2203 if (h2s->id > 0)
2204 break;
2205 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002206 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002207 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002208
2209 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002210}
2211
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002212/* Wake up all blocked streams whose window size has become positive after the
2213 * mux's initial window was adjusted. This should be done after having processed
2214 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002215 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002216static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02002217{
2218 struct h2s *h2s;
2219 struct eb32_node *node;
2220
Willy Tarreau7838a792019-08-12 18:42:03 +02002221 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2222
Willy Tarreau3421aba2017-07-27 15:41:03 +02002223 node = eb32_first(&h2c->streams_by_id);
2224 while (node) {
2225 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002226 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002227 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002228 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002229 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2230 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002231 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002232 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002233 node = eb32_next(node);
2234 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002235
2236 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002237}
2238
2239/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
2240 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01002241 * return an error in h2c. The caller must have already verified frame length
2242 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002243 */
2244static int h2c_handle_settings(struct h2c *h2c)
2245{
2246 unsigned int offset;
2247 int error;
2248
Willy Tarreau7838a792019-08-12 18:42:03 +02002249 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
2250
Willy Tarreau3421aba2017-07-27 15:41:03 +02002251 if (h2c->dff & H2_F_SETTINGS_ACK) {
2252 if (h2c->dfl) {
2253 error = H2_ERR_FRAME_SIZE_ERROR;
2254 goto fail;
2255 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002256 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002257 }
2258
Willy Tarreau3421aba2017-07-27 15:41:03 +02002259 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002260 if (b_data(&h2c->dbuf) < h2c->dfl) {
2261 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002262 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002263 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002264
2265 /* parse the frame */
2266 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002267 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2268 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002269
2270 switch (type) {
2271 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2272 /* we need to update all existing streams with the
2273 * difference from the previous iws.
2274 */
2275 if (arg < 0) { // RFC7540#6.5.2
2276 error = H2_ERR_FLOW_CONTROL_ERROR;
2277 goto fail;
2278 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002279 h2c->miw = arg;
2280 break;
2281 case H2_SETTINGS_MAX_FRAME_SIZE:
2282 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002283 TRACE_ERROR("MAX_FRAME_SIZE out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002284 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002285 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002286 goto fail;
2287 }
2288 h2c->mfs = arg;
2289 break;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01002290 case H2_SETTINGS_HEADER_TABLE_SIZE:
2291 h2c->flags |= H2_CF_SHTS_UPDATED;
2292 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002293 case H2_SETTINGS_ENABLE_PUSH:
2294 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002295 TRACE_ERROR("ENABLE_PUSH out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002296 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002297 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002298 goto fail;
2299 }
2300 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002301 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2302 if (h2c->flags & H2_CF_IS_BACK) {
2303 /* the limit is only for the backend; for the frontend it is our limit */
Willy Tarreauca1027c2023-04-18 15:57:03 +02002304 if ((unsigned int)arg > h2c_max_concurrent_streams(h2c))
2305 arg = h2c_max_concurrent_streams(h2c);
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002306 h2c->streams_limit = arg;
2307 }
2308 break;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002309 case H2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
Amaury Denoyelle0df04362021-10-18 09:43:29 +02002310 if (arg == 1)
2311 h2c->flags |= H2_CF_RCVD_RFC8441;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002312 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002313 }
2314 }
2315
2316 /* need to ACK this frame now */
2317 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002318 done:
2319 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002320 return 1;
2321 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002322 if (!(h2c->flags & H2_CF_IS_BACK))
2323 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002324 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002325 out0:
2326 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 +02002327 return 0;
2328}
2329
2330/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2331 * success or one of the h2_status values.
2332 */
2333static int h2c_ack_settings(struct h2c *h2c)
2334{
2335 struct buffer *res;
2336 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002337 int ret = 0;
2338
2339 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002340
Willy Tarreau9c218e72019-05-26 10:08:28 +02002341 memcpy(str,
2342 "\x00\x00\x00" /* length : 0 (no data) */
2343 "\x04" "\x01" /* type : 4, flags : ACK */
2344 "\x00\x00\x00\x00" /* stream ID */, 9);
2345
Willy Tarreaubcc45952019-05-26 10:05:50 +02002346 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002347 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002348 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002349 h2c->flags |= H2_CF_MUX_MALLOC;
2350 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002351 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002352 }
2353
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002354 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002355 if (unlikely(ret <= 0)) {
2356 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002357 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2358 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002359 h2c->flags |= H2_CF_MUX_MFULL;
2360 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002361 }
2362 else {
2363 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002364 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002365 }
2366 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002367 out:
2368 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002369 return ret;
2370}
2371
Willy Tarreaucf68c782017-10-10 17:11:41 +02002372/* processes a PING frame and schedules an ACK if needed. The caller must pass
2373 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002374 * missing data. The caller must have already verified frame length
2375 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002376 */
2377static int h2c_handle_ping(struct h2c *h2c)
2378{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002379 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002380 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002381 h2c->st0 = H2_CS_FRAME_A;
2382 return 1;
2383}
2384
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002385/* Try to send a window update for stream id <sid> and value <increment>.
2386 * Returns > 0 on success or zero on missing room or failure. It may return an
2387 * error in h2c.
2388 */
2389static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2390{
2391 struct buffer *res;
2392 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002393 int ret = 0;
2394
2395 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002396
Willy Tarreau9c218e72019-05-26 10:08:28 +02002397 /* length: 4, type: 8, flags: none */
2398 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2399 write_n32(str + 5, sid);
2400 write_n32(str + 9, increment);
2401
Willy Tarreaubcc45952019-05-26 10:05:50 +02002402 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002403 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002404 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002405 h2c->flags |= H2_CF_MUX_MALLOC;
2406 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002407 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002408 }
2409
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002410 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002411 if (unlikely(ret <= 0)) {
2412 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002413 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2414 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002415 h2c->flags |= H2_CF_MUX_MFULL;
2416 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002417 }
2418 else {
2419 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002420 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002421 }
2422 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002423 out:
2424 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002425 return ret;
2426}
2427
2428/* try to send pending window update for the connection. It's safe to call it
2429 * with no pending updates. Returns > 0 on success or zero on missing room or
2430 * failure. It may return an error in h2c.
2431 */
2432static int h2c_send_conn_wu(struct h2c *h2c)
2433{
2434 int ret = 1;
2435
Willy Tarreau7838a792019-08-12 18:42:03 +02002436 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2437
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002438 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002439 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002440
Willy Tarreau97aaa672018-12-23 09:49:04 +01002441 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2442 /* increase the advertised connection window to 2G on
2443 * first update.
2444 */
2445 h2c->flags |= H2_CF_WINDOW_OPENED;
2446 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2447 }
2448
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002449 /* send WU for the connection */
2450 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2451 if (ret > 0)
2452 h2c->rcvd_c = 0;
2453
Willy Tarreau7838a792019-08-12 18:42:03 +02002454 out:
2455 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002456 return ret;
2457}
2458
2459/* try to send pending window update for the current dmux stream. It's safe to
2460 * call it with no pending updates. Returns > 0 on success or zero on missing
2461 * room or failure. It may return an error in h2c.
2462 */
2463static int h2c_send_strm_wu(struct h2c *h2c)
2464{
2465 int ret = 1;
2466
Willy Tarreau7838a792019-08-12 18:42:03 +02002467 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2468
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002469 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002470 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002471
2472 /* send WU for the stream */
2473 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2474 if (ret > 0)
2475 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002476 out:
2477 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002478 return ret;
2479}
2480
Willy Tarreaucf68c782017-10-10 17:11:41 +02002481/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2482 * success, 0 on missing data or one of the h2_status values.
2483 */
2484static int h2c_ack_ping(struct h2c *h2c)
2485{
2486 struct buffer *res;
2487 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002488 int ret = 0;
2489
2490 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002491
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002492 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002493 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002494
Willy Tarreaucf68c782017-10-10 17:11:41 +02002495 memcpy(str,
2496 "\x00\x00\x08" /* length : 8 (same payload) */
2497 "\x06" "\x01" /* type : 6, flags : ACK */
2498 "\x00\x00\x00\x00" /* stream ID */, 9);
2499
2500 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002501 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002502
Willy Tarreau9c218e72019-05-26 10:08:28 +02002503 res = br_tail(h2c->mbuf);
2504 retry:
2505 if (!h2_get_buf(h2c, res)) {
2506 h2c->flags |= H2_CF_MUX_MALLOC;
2507 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002508 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002509 }
2510
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002511 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002512 if (unlikely(ret <= 0)) {
2513 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002514 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2515 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002516 h2c->flags |= H2_CF_MUX_MFULL;
2517 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002518 }
2519 else {
2520 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002521 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002522 }
2523 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002524 out:
2525 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002526 return ret;
2527}
2528
Willy Tarreau26f95952017-07-27 17:18:30 +02002529/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2530 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002531 * h2c or h2s. The caller must have already verified frame length and stream ID
2532 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002533 */
2534static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2535{
2536 int32_t inc;
2537 int error;
2538
Willy Tarreau7838a792019-08-12 18:42:03 +02002539 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2540
Willy Tarreau26f95952017-07-27 17:18:30 +02002541 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002542 if (b_data(&h2c->dbuf) < h2c->dfl) {
2543 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002544 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002545 }
Willy Tarreau26f95952017-07-27 17:18:30 +02002546
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002547 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002548
2549 if (h2c->dsi != 0) {
2550 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002551
2552 /* it's not an error to receive WU on a closed stream */
2553 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002554 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002555
2556 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002557 TRACE_ERROR("stream WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn, h2s);
Willy Tarreau26f95952017-07-27 17:18:30 +02002558 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002559 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002560 goto strm_err;
2561 }
2562
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002563 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002564 TRACE_ERROR("stream WINDOW_UPDATE inc<0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn, h2s);
Willy Tarreau26f95952017-07-27 17:18:30 +02002565 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002566 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002567 goto strm_err;
2568 }
2569
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002570 h2s->sws += inc;
2571 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002572 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002573 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002574 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2575 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002576 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002577 }
2578 }
2579 else {
2580 /* connection window update */
2581 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002582 TRACE_ERROR("conn WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002583 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002584 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002585 goto conn_err;
2586 }
2587
2588 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2589 error = H2_ERR_FLOW_CONTROL_ERROR;
2590 goto conn_err;
2591 }
2592
2593 h2c->mws += inc;
2594 }
2595
Willy Tarreau7838a792019-08-12 18:42:03 +02002596 done:
2597 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002598 return 1;
2599
2600 conn_err:
2601 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002602 out0:
2603 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 +02002604 return 0;
2605
2606 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002607 h2s_error(h2s, error);
2608 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002609 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002610 return 0;
2611}
2612
Willy Tarreaue96b0922017-10-30 00:28:29 +01002613/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002614 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2615 * have already verified frame length and stream ID validity. Described in
2616 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002617 */
2618static int h2c_handle_goaway(struct h2c *h2c)
2619{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002620 int last;
2621
Willy Tarreau7838a792019-08-12 18:42:03 +02002622 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002623 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002624 if (b_data(&h2c->dbuf) < h2c->dfl) {
2625 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002626 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002627 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002628 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002629
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002630 last = h2_get_n32(&h2c->dbuf, 0);
2631 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002632 if (h2c->last_sid < 0)
2633 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002634 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002635 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002636 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002637}
2638
Willy Tarreau92153fc2017-12-03 19:46:19 +01002639/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002640 * invalid. Returns > 0 on success or zero on missing data. It may return an
2641 * error in h2c. The caller must have already verified frame length and stream
2642 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002643 */
2644static int h2c_handle_priority(struct h2c *h2c)
2645{
Willy Tarreau7838a792019-08-12 18:42:03 +02002646 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2647
Willy Tarreau92153fc2017-12-03 19:46:19 +01002648 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002649 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002650 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002651 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002652 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002653 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002654
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002656 /* 7540#5.3 : can't depend on itself */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002657 TRACE_ERROR("PRIORITY depends on itself", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002658 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02002659 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002660 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002661 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002662 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002663 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002664 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002665}
2666
Willy Tarreaucd234e92017-08-18 10:59:39 +02002667/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002668 * Returns > 0 on success or zero on missing data. The caller must have already
2669 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002670 */
2671static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2672{
Willy Tarreau7838a792019-08-12 18:42:03 +02002673 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2674
Willy Tarreaucd234e92017-08-18 10:59:39 +02002675 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002676 if (b_data(&h2c->dbuf) < h2c->dfl) {
2677 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002678 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002679 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002680 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002681
2682 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002683 if (h2s->st == H2_SS_CLOSED) {
2684 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 +02002685 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002686 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002687
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002688 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002689 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002690
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002691 if (h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02002692 se_fl_set_error(h2s->sd);
Willy Tarreauf830f012018-12-19 17:44:55 +01002693 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002694 }
2695
2696 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002697 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002698 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002699}
2700
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002701/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2702 * It may return an error in h2c or h2s. The caller must consider that the
2703 * return value is the new h2s in case one was allocated (most common case).
2704 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002705 * errors here are reported as connection errors since it's impossible to
2706 * recover from such errors after the compression context has been altered.
2707 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002708static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002709{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002710 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002711 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002712 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002713 int error;
2714
Willy Tarreau7838a792019-08-12 18:42:03 +02002715 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2716
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002717 if (!b_size(&h2c->dbuf)) {
2718 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002719 goto out; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002720 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002721
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002722 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2723 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002724 goto out; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002725 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002726
2727 /* now either the frame is complete or the buffer is complete */
2728 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002729 /* The stream exists/existed, this must be a trailers frame */
2730 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002731 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &body_len, NULL);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002732 /* unrecoverable error ? */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002733 if (h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau17c630b2023-01-19 23:58:11 +01002734 TRACE_USER("Unrecoverable error decoding H2 trailers", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, 0, &rxbuf);
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002735 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002736 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002737 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002738
Christopher Faulet485da0b2021-10-08 08:56:00 +02002739 if (error == 0) {
2740 /* Demux not blocked because of the stream, it is an incomplete frame */
2741 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2742 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002743 goto out; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002744 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002745
2746 if (error < 0) {
2747 /* Failed to decode this frame (e.g. too large request)
2748 * but the HPACK decompressor is still synchronized.
2749 */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002750 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002751 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau17c630b2023-01-19 23:58:11 +01002752 TRACE_USER("Stream error decoding H2 trailers", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, 0, &rxbuf);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002753 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002754 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002755 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002756 goto done;
2757 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002758 /* the connection was already killed by an RST, let's consume
2759 * the data and send another RST.
2760 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002761 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002762 sess_log(h2c->conn->owner);
Willy Tarreau1f035502019-01-30 11:44:07 +01002763 h2s = (struct h2s*)h2_error_stream;
2764 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002765 }
2766 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2767 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2768 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002769 TRACE_ERROR("HEADERS on invalid stream ID", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau4781b152021-04-06 13:53:36 +02002770 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002771 sess_log(h2c->conn->owner);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02002772 session_inc_http_req_ctr(h2c->conn->owner);
2773 session_inc_http_err_ctr(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002774 goto conn_err;
2775 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002776 else if (h2c->flags & H2_CF_DEM_TOOMANY)
Willy Tarreau36c22322022-05-27 10:41:24 +02002777 goto out; // IDLE but too many sc still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002778
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002779 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002780
Willy Tarreau25919232019-01-03 14:48:18 +01002781 /* unrecoverable error ? */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002782 if (h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau17c630b2023-01-19 23:58:11 +01002783 TRACE_USER("Unrecoverable error decoding H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, 0, &rxbuf);
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002784 sess_log(h2c->conn->owner);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02002785 session_inc_http_req_ctr(h2c->conn->owner);
2786 session_inc_http_err_ctr(h2c->conn->owner);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002787 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002788 }
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002789
Willy Tarreau25919232019-01-03 14:48:18 +01002790 if (error <= 0) {
Christopher Faulet485da0b2021-10-08 08:56:00 +02002791 if (error == 0) {
2792 /* Demux not blocked because of the stream, it is an incomplete frame */
2793 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2794 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau25919232019-01-03 14:48:18 +01002795 goto out; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002796 }
Willy Tarreau25919232019-01-03 14:48:18 +01002797
2798 /* Failed to decode this stream (e.g. too large request)
2799 * but the HPACK decompressor is still synchronized.
2800 */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002801 sess_log(h2c->conn->owner);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02002802 session_inc_http_req_ctr(h2c->conn->owner);
2803 session_inc_http_err_ctr(h2c->conn->owner);
2804
Willy Tarreau25919232019-01-03 14:48:18 +01002805 h2s = (struct h2s*)h2_error_stream;
Willy Tarreauf69d53e2023-10-20 17:51:12 +02002806
2807 /* This stream ID is now opened anyway until we send the RST on
2808 * it, it must not be reused.
2809 */
2810 if (h2c->dsi > h2c->max_id)
2811 h2c->max_id = h2c->dsi;
2812
Willy Tarreau25919232019-01-03 14:48:18 +01002813 goto send_rst;
2814 }
2815
Willy Tarreau29268e92021-06-17 08:29:14 +02002816 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn, 0, &rxbuf);
2817
Willy Tarreau198b5072022-05-12 09:08:51 +02002818 /* Now we cannot roll back and we won't come back here anymore for this
2819 * stream, this stream ID is open.
2820 */
2821 if (h2c->dsi > h2c->max_id)
2822 h2c->max_id = h2c->dsi;
2823
Willy Tarreau22de8d32018-09-05 19:55:58 +02002824 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002825 * positively from h2c_frt_stream_new(), the stream will report the error,
2826 * and if we return in error, h2c_frt_stream_new() will emit the error.
Christopher Faulet7d013e72020-12-15 16:56:50 +01002827 *
2828 * Xfer the rxbuf to the stream. On success, the new stream owns the
2829 * rxbuf. On error, it is released here.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002830 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02002831 h2s = h2c_frt_stream_new(h2c, h2c->dsi, &rxbuf, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002832 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002833 h2s = (struct h2s*)h2_refused_stream;
2834 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002835 }
2836
2837 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002838 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002839 h2s->body_len = body_len;
Christopher Fauletc2f1d0e2023-05-24 11:34:45 +02002840 h2s_propagate_term_flags(h2c, h2s);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002841
Willy Tarreau88d138e2019-01-02 19:38:14 +01002842 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002843 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002844 if (h2s->st == H2_SS_OPEN)
2845 h2s->st = H2_SS_HREM;
2846 else
2847 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002848 }
Willy Tarreau0d6e5d22023-02-20 17:05:10 +01002849 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002850 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002851
2852 conn_err:
2853 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002854 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002855
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002856 out:
2857 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002858 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 +01002859 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002860
2861 send_rst:
2862 /* make the demux send an RST for the current stream. We may only
2863 * do this if we're certain that the HEADERS frame was properly
2864 * decompressed so that the HPACK decoder is still kept up to date.
2865 */
2866 h2_release_buf(h2c, &rxbuf);
2867 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002868
Willy Tarreau022e5e52020-09-10 09:33:15 +02002869 TRACE_USER("rejected H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, 0, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002870 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002871 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002872}
2873
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002874/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2875 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2876 * errors here are reported as connection errors since it's impossible to
2877 * recover from such errors after the compression context has been altered.
2878 */
2879static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2880{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002881 struct buffer rxbuf = BUF_NULL;
2882 unsigned long long body_len = 0;
2883 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002884 int error;
2885
Willy Tarreau7838a792019-08-12 18:42:03 +02002886 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2887
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002888 if (!b_size(&h2c->dbuf)) {
2889 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002890 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002891 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002892
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002893 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2894 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002895 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002896 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002897
Christopher Faulet6884aa32019-09-23 15:28:20 +02002898 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002899 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len, h2s->upgrade_protocol);
Christopher Faulet6884aa32019-09-23 15:28:20 +02002900 }
2901 else {
2902 /* the connection was already killed by an RST, let's consume
2903 * the data and send another RST.
2904 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002905 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Christopher Fauletea7a7782019-09-26 16:19:13 +02002906 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002907 h2c->st0 = H2_CS_FRAME_E;
2908 goto send_rst;
2909 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002910
Willy Tarreau25919232019-01-03 14:48:18 +01002911 /* unrecoverable error ? */
Willy Tarreau17c630b2023-01-19 23:58:11 +01002912 if (h2c->st0 >= H2_CS_ERROR) {
2913 TRACE_USER("Unrecoverable error decoding H2 HEADERS", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002914 goto fail;
Willy Tarreau17c630b2023-01-19 23:58:11 +01002915 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002916
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002917 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2918 /* RFC7540#5.1 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002919 TRACE_ERROR("response HEADERS in invalid state", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002920 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2921 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02002922 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002923 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002924 }
2925
Willy Tarreau25919232019-01-03 14:48:18 +01002926 if (error <= 0) {
Christopher Faulet485da0b2021-10-08 08:56:00 +02002927 if (error == 0) {
2928 /* Demux not blocked because of the stream, it is an incomplete frame */
2929 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2930 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002931 goto fail; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002932 }
Willy Tarreau25919232019-01-03 14:48:18 +01002933
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002934 /* stream error : send RST_STREAM */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002935 TRACE_ERROR("couldn't decode response HEADERS", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau25919232019-01-03 14:48:18 +01002936 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002937 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02002938 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002939 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002940 }
2941
Willy Tarreau95acc8b2022-05-27 16:14:10 +02002942 if (se_fl_test(h2s->sd, SE_FL_ERROR) && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002943 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002944 else if (h2s->flags & H2_SF_ES_RCVD) {
2945 if (h2s->st == H2_SS_OPEN)
2946 h2s->st = H2_SS_HREM;
2947 else if (h2s->st == H2_SS_HLOC)
2948 h2s_close(h2s);
2949 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002950
Christopher Fauletf95f8762021-01-22 11:59:07 +01002951 /* Unblock busy server h2s waiting for the response headers to validate
2952 * the tunnel establishment or the end of the response of an oborted
2953 * tunnel
2954 */
2955 if ((h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY)) == (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY) ||
2956 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_RCVD|H2_SF_BLK_MBUSY)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_RCVD|H2_SF_BLK_MBUSY)) {
2957 TRACE_STATE("Unblock h2s blocked on tunnel establishment/abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2958 h2s->flags &= ~H2_SF_BLK_MBUSY;
2959 }
2960
Willy Tarreau9abb3172021-06-16 18:32:42 +02002961 TRACE_USER("rcvd H2 response ", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, 0, &h2s->rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002962 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002963 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02002964 fail:
2965 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2966 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002967
2968 send_rst:
2969 /* make the demux send an RST for the current stream. We may only
2970 * do this if we're certain that the HEADERS frame was properly
2971 * decompressed so that the HPACK decoder is still kept up to date.
2972 */
2973 h2_release_buf(h2c, &rxbuf);
2974 h2c->st0 = H2_CS_FRAME_E;
2975
Willy Tarreau022e5e52020-09-10 09:33:15 +02002976 TRACE_USER("rejected H2 response", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, 0, &rxbuf);
Christopher Faulet6884aa32019-09-23 15:28:20 +02002977 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2978 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002979}
2980
Willy Tarreau454f9052017-10-26 19:40:35 +02002981/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2982 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2983 */
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01002984static int h2c_handle_data(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002985{
2986 int error;
2987
Willy Tarreau7838a792019-08-12 18:42:03 +02002988 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2989
Willy Tarreau454f9052017-10-26 19:40:35 +02002990 /* note that empty DATA frames are perfectly valid and sometimes used
2991 * to signal an end of stream (with the ES flag).
2992 */
2993
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002994 if (!b_size(&h2c->dbuf) && h2c->dfl) {
2995 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002996 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002997 }
Willy Tarreau454f9052017-10-26 19:40:35 +02002998
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002999 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
3000 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003001 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003002 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003003
3004 /* now either the frame is complete or the buffer is complete */
3005
Willy Tarreau454f9052017-10-26 19:40:35 +02003006 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
3007 /* RFC7540#6.1 */
3008 error = H2_ERR_STREAM_CLOSED;
3009 goto strm_err;
3010 }
3011
Christopher Faulet35feba62023-09-13 16:21:58 +02003012 if (!(h2s->flags & H2_SF_HEADERS_RCVD)) {
3013 /* RFC9113#8.1: The header section must be received before the message content */
3014 TRACE_ERROR("Unexpected DATA frame before the message headers", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3015 error = H2_ERR_PROTOCOL_ERROR;
3016 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
3017 goto strm_err;
3018 }
Christopher Faulet4f09ec82019-06-19 09:25:58 +02003019 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003020 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003021 TRACE_ERROR("DATA frame larger than content-length", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003022 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003023 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003024 goto strm_err;
3025 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003026 if (!(h2c->flags & H2_CF_IS_BACK) &&
3027 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT) &&
3028 ((h2c->dfl - h2c->dpl) || !(h2c->dff & H2_F_DATA_END_STREAM))) {
3029 /* a tunnel attempt was aborted but the client still try to send some raw data.
3030 * Thus the stream is closed with the CANCEL error. Here we take care it is not
3031 * an empty DATA Frame with the ES flag. The error is only handled if ES was
3032 * already sent to the client because depending on the scheduling, these data may
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003033 * have been sent before the server response but not handle here.
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003034 */
3035 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3036 error = H2_ERR_CANCEL;
3037 goto strm_err;
3038 }
Willy Tarreau1915ca22019-01-24 11:49:37 +01003039
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003040 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02003041 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003042
Willy Tarreau454f9052017-10-26 19:40:35 +02003043 /* call the upper layers to process the frame, then let the upper layer
3044 * notify the stream about any change.
3045 */
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003046 if (!h2s_sc(h2s)) {
Willy Tarreau082c4572019-08-06 10:11:02 +02003047 /* The upper layer has already closed, this may happen on
3048 * 4xx/redirects during POST, or when receiving a response
3049 * from an H2 server after the client has aborted.
3050 */
3051 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003052 goto strm_err;
3053 }
3054
Willy Tarreau8f650c32017-11-21 19:36:21 +01003055 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003056 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01003057
Willy Tarreau721c9742017-11-07 11:05:42 +01003058 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003059 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01003060 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02003061 }
3062
3063 /* check for completion : the callee will change this to FRAME_A or
3064 * FRAME_H once done.
3065 */
3066 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02003067 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02003068
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003069 /* last frame */
3070 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02003071 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01003072 if (h2s->st == H2_SS_OPEN)
3073 h2s->st = H2_SS_HREM;
3074 else
3075 h2s_close(h2s);
3076
Willy Tarreau1915ca22019-01-24 11:49:37 +01003077 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
3078 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003079 TRACE_ERROR("ES on DATA frame before content-length", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003080 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003081 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003082 goto strm_err;
3083 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003084 }
3085
Christopher Fauletf95f8762021-01-22 11:59:07 +01003086 /* Unblock busy server h2s waiting for the end of the response for an
3087 * aborted tunnel
3088 */
3089 if ((h2c->flags & H2_CF_IS_BACK) &&
3090 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_RCVD|H2_SF_BLK_MBUSY)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_RCVD|H2_SF_BLK_MBUSY)) {
3091 TRACE_STATE("Unblock h2s blocked on tunnel abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3092 h2s->flags &= ~H2_SF_BLK_MBUSY;
3093 }
3094
Willy Tarreau7838a792019-08-12 18:42:03 +02003095 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003096 return 1;
3097
Willy Tarreau454f9052017-10-26 19:40:35 +02003098 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01003099 h2s_error(h2s, error);
3100 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003101 fail:
3102 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 +02003103 return 0;
3104}
3105
Willy Tarreau63864812019-08-07 14:25:20 +02003106/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
3107 * valid for the current stream state. This is needed only after parsing the
3108 * frame header but in practice it can be performed at any time during
3109 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
3110 * or 0 in case of error, in which case either h2s or h2c will carry an error.
3111 */
3112static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
3113{
Willy Tarreau7838a792019-08-12 18:42:03 +02003114 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
3115
Willy Tarreau63864812019-08-07 14:25:20 +02003116 if (h2s->st == H2_SS_IDLE &&
3117 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
3118 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
3119 * this state MUST be treated as a connection error
3120 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003121 TRACE_ERROR("invalid frame type for IDLE state", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003122 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003123 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02003124 /* only log if no other stream can report the error */
3125 sess_log(h2c->conn->owner);
3126 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003127 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003128 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 +02003129 return 0;
3130 }
3131
Willy Tarreau57a18162019-11-24 14:57:53 +01003132 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
3133 /* only PUSH_PROMISE would be permitted here */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003134 TRACE_ERROR("invalid frame type for IDLE state (back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau57a18162019-11-24 14:57:53 +01003135 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003136 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau57a18162019-11-24 14:57:53 +01003137 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
3138 return 0;
3139 }
3140
Willy Tarreau63864812019-08-07 14:25:20 +02003141 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
3142 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
3143 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
3144 * this state MUST be treated as a stream error.
3145 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
3146 * PUSH_PROMISE/CONTINUATION cause connection errors.
3147 */
Amaury Denoyellea8879232020-10-27 17:16:03 +01003148 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003149 TRACE_ERROR("invalid frame type for HREM state", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003150 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003151 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003152 }
3153 else {
Willy Tarreau63864812019-08-07 14:25:20 +02003154 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003155 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003156 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 +02003157 return 0;
3158 }
3159
3160 /* Below the management of frames received in closed state is a
3161 * bit hackish because the spec makes strong differences between
3162 * streams closed by receiving RST, sending RST, and seeing ES
3163 * in both directions. In addition to this, the creation of a
3164 * new stream reusing the identifier of a closed one will be
3165 * detected here. Given that we cannot keep track of all closed
3166 * streams forever, we consider that unknown closed streams were
3167 * closed on RST received, which allows us to respond with an
3168 * RST without breaking the connection (eg: to abort a transfer).
3169 * Some frames have to be silently ignored as well.
3170 */
3171 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
3172 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
3173 /* #5.1.1: The identifier of a newly
3174 * established stream MUST be numerically
3175 * greater than all streams that the initiating
3176 * endpoint has opened or reserved. This
3177 * governs streams that are opened using a
3178 * HEADERS frame and streams that are reserved
3179 * using PUSH_PROMISE. An endpoint that
3180 * receives an unexpected stream identifier
3181 * MUST respond with a connection error.
3182 */
3183 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003184 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 +02003185 return 0;
3186 }
3187
Willy Tarreau4c08f122019-09-26 08:47:15 +02003188 if (h2s->flags & H2_SF_RST_RCVD &&
3189 !(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 +02003190 /* RFC7540#5.1:closed: an endpoint that
3191 * receives any frame other than PRIORITY after
3192 * receiving a RST_STREAM MUST treat that as a
3193 * stream error of type STREAM_CLOSED.
3194 *
3195 * Note that old streams fall into this category
3196 * and will lead to an RST being sent.
3197 *
3198 * However, we cannot generalize this to all frame types. Those
3199 * carrying compression state must still be processed before
3200 * being dropped or we'll desynchronize the decoder. This can
3201 * happen with request trailers received after sending an
3202 * RST_STREAM, or with header/trailers responses received after
3203 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02003204 *
3205 * In addition, since our CLOSED streams always carry the
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003206 * RST_RCVD bit, we don't want to accidentally catch valid
Willy Tarreau4c08f122019-09-26 08:47:15 +02003207 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02003208 */
3209 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3210 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003211 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 +02003212 return 0;
3213 }
3214
3215 /* RFC7540#5.1:closed: if this state is reached as a
3216 * result of sending a RST_STREAM frame, the peer that
3217 * receives the RST_STREAM might have already sent
3218 * frames on the stream that cannot be withdrawn. An
3219 * endpoint MUST ignore frames that it receives on
3220 * closed streams after it has sent a RST_STREAM
3221 * frame. An endpoint MAY choose to limit the period
3222 * over which it ignores frames and treat frames that
3223 * arrive after this time as being in error.
3224 */
3225 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
3226 /* RFC7540#5.1:closed: any frame other than
3227 * PRIO/WU/RST in this state MUST be treated as
3228 * a connection error
3229 */
3230 if (h2c->dft != H2_FT_RST_STREAM &&
3231 h2c->dft != H2_FT_PRIORITY &&
3232 h2c->dft != H2_FT_WINDOW_UPDATE) {
3233 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003234 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 +02003235 return 0;
3236 }
3237 }
3238 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003239 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003240 return 1;
3241}
3242
Willy Tarreaubc933932017-10-09 16:21:43 +02003243/* process Rx frames to be demultiplexed */
3244static void h2_process_demux(struct h2c *h2c)
3245{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003246 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003247 struct h2_fh hdr;
3248 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003249 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02003250
Willy Tarreau7838a792019-08-12 18:42:03 +02003251 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3252
Willy Tarreau081d4722017-05-16 21:51:05 +02003253 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003254 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02003255
3256 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3257 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003258 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02003259 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02003260 goto out;
3261
Willy Tarreau52eed752017-09-22 15:05:09 +02003262 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
3263 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003264 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003265 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003266 h2c->st0 = H2_CS_ERROR2;
Willy Tarreauee4684f2021-06-17 08:08:48 +02003267 if (b_data(&h2c->dbuf) ||
Christopher Faulet3f35da22021-07-26 10:18:35 +02003268 !(((const struct session *)h2c->conn->owner)->fe->options & (PR_O_NULLNOLOG|PR_O_IGNORE_PRB)))
Willy Tarreauee4684f2021-06-17 08:08:48 +02003269 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003270 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003271 goto done;
Willy Tarreau52eed752017-09-22 15:05:09 +02003272 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003273 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003274
3275 h2c->max_id = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02003276 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaued2b9d92022-08-18 15:30:41 +02003277 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau52eed752017-09-22 15:05:09 +02003278 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003279
3280 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003281 /* ensure that what is pending is a valid SETTINGS frame
3282 * without an ACK.
3283 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003284 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 +02003285 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003286 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003287 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau22de8d32018-09-05 19:55:58 +02003288 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003289 TRACE_ERROR("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 +02003290 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003291 if (!(h2c->flags & H2_CF_IS_BACK))
3292 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003293 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003294 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003295 }
3296
3297 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
3298 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003299 TRACE_ERROR("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 +02003300 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3301 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003302 if (!(h2c->flags & H2_CF_IS_BACK))
3303 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003304 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003305 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003306 }
3307
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003308 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003309 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003310 TRACE_ERROR("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 +02003311 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3312 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003313 if (!(h2c->flags & H2_CF_IS_BACK))
3314 sess_log(h2c->conn->owner);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003315 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003316 }
3317
Willy Tarreau3bf69182018-12-21 15:34:50 +01003318 /* that's OK, switch to FRAME_P to process it. This is
3319 * a SETTINGS frame whose header has already been
3320 * deleted above.
3321 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01003322 padlen = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +02003323 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003324 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003325 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003326 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003327
3328 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02003329 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02003330 int ret = 0;
3331
Willy Tarreau7838a792019-08-12 18:42:03 +02003332 if (!b_data(&h2c->dbuf)) {
3333 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003334 h2c->flags |= H2_CF_DEM_SHORT_READ;
3335 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003336 }
3337
3338 if (h2c->st0 >= H2_CS_ERROR) {
3339 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003340 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003341 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003342
3343 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003344 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003345 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) {
3346 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003347 break;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003348 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003349
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003350 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003351 TRACE_ERROR("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 +02003352 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003353 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02003354 /* only log if no other stream can report the error */
3355 sess_log(h2c->conn->owner);
3356 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003357 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003358 break;
3359 }
3360
Willy Tarreau617592c2022-06-08 16:32:22 +02003361 if (h2c->rcvd_s && h2c->dsi != hdr.sid) {
3362 /* changed stream with a pending WU, need to
3363 * send it now.
3364 */
3365 TRACE_PROTO("sending stream WINDOW_UPDATE frame on stream switch", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
3366 ret = h2c_send_strm_wu(h2c);
3367 if (ret <= 0)
3368 break;
3369 }
3370
Christopher Fauletdd2a5622019-06-18 12:22:38 +02003371 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003372 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
3373 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
3374 * we read the pad length and drop it from the remaining
3375 * payload (one byte + the 9 remaining ones = 10 total
3376 * removed), so we have a frame payload starting after the
3377 * pad len. Flow controlled frames (DATA) also count the
3378 * padlen in the flow control, so it must be adjusted.
3379 */
3380 if (hdr.len < 1) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003381 TRACE_ERROR("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 +01003382 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003383 if (!(h2c->flags & H2_CF_IS_BACK))
3384 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003385 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003386 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003387 }
3388 hdr.len--;
3389
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003390 if (b_data(&h2c->dbuf) < 10) {
3391 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003392 break; // missing padlen
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003393 }
Willy Tarreau3bf69182018-12-21 15:34:50 +01003394
3395 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3396
3397 if (padlen > hdr.len) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003398 TRACE_ERROR("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 +01003399 /* RFC7540#6.1 : pad length = length of
3400 * frame payload or greater => error.
3401 */
3402 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003403 if (!(h2c->flags & H2_CF_IS_BACK))
3404 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003405 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003406 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003407 }
3408
3409 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3410 h2c->rcvd_c++;
3411 h2c->rcvd_s++;
3412 }
3413 b_del(&h2c->dbuf, 1);
3414 }
3415 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003416
3417 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003418 h2c->dfl = hdr.len;
3419 h2c->dsi = hdr.sid;
3420 h2c->dft = hdr.ft;
3421 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003422 h2c->dpl = padlen;
Willy Tarreau0f458712022-08-18 11:19:57 +02003423 h2c->flags |= H2_CF_DEM_IN_PROGRESS;
Willy Tarreau73db4342019-09-25 07:28:44 +02003424 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 +02003425 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003426
3427 /* check for minimum basic frame format validity */
3428 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3429 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003430 TRACE_ERROR("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 +01003431 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003432 if (!(h2c->flags & H2_CF_IS_BACK))
3433 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003434 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003435 goto done;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003436 }
Willy Tarreau15a47332022-03-18 15:57:34 +01003437
3438 /* transition to HEADERS frame ends the keep-alive idle
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003439 * timer and starts the http-request idle delay. It uses
3440 * the idle_start timer as well.
Willy Tarreau15a47332022-03-18 15:57:34 +01003441 */
3442 if (hdr.ft == H2_FT_HEADERS)
3443 h2c->idle_start = now_ms;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003444 }
3445
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003446 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3447 * H2_CS_FRAME_P indicates an incomplete previous operation
3448 * (most often the first attempt) and requires some validity
3449 * checks for the frame and the current state. The two other
3450 * ones are set after completion (or abortion) and must skip
3451 * validity checks.
3452 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003453 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3454
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003455 if (tmp_h2s != h2s && h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003456 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003457 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003458 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003459 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003460 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003461 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003462 TRACE_DEVEL("notifying stream before switching SID", H2_EV_RX_FRAME|H2_EV_STRM_WAKE, h2c->conn, h2s);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003463 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003464 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003465 }
3466 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003467
Willy Tarreau63864812019-08-07 14:25:20 +02003468 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003469 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3470 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003471 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003472 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003473
Willy Tarreau7e98c052017-10-10 15:56:59 +02003474 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003475 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003476 if (h2c->st0 == H2_CS_FRAME_P) {
3477 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003478 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003479 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003480 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003481
Willy Tarreau7838a792019-08-12 18:42:03 +02003482 if (h2c->st0 == H2_CS_FRAME_A) {
3483 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 +02003484 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003485 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003486 break;
3487
Willy Tarreaucf68c782017-10-10 17:11:41 +02003488 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003489 if (h2c->st0 == H2_CS_FRAME_P) {
3490 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003491 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003492 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003493
Willy Tarreau7838a792019-08-12 18:42:03 +02003494 if (h2c->st0 == H2_CS_FRAME_A) {
3495 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 +02003496 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003497 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003498 break;
3499
Willy Tarreau26f95952017-07-27 17:18:30 +02003500 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003501 if (h2c->st0 == H2_CS_FRAME_P) {
3502 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 +02003503 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003504 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003505 break;
3506
Willy Tarreau61290ec2017-10-17 08:19:21 +02003507 case H2_FT_CONTINUATION:
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003508 /* RFC7540#6.10: CONTINUATION may only be preceded by
Willy Tarreauea18f862018-12-22 20:19:26 +01003509 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3510 * frames' parsers consume all following CONTINUATION
3511 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003512 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003513 TRACE_ERROR("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 +01003514 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003515 if (!(h2c->flags & H2_CF_IS_BACK))
3516 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003517 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003518 goto done;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003519
Willy Tarreau13278b42017-10-13 19:23:14 +02003520 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003521 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003522 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003523 if (h2c->flags & H2_CF_IS_BACK)
3524 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3525 else
3526 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003527 if (tmp_h2s) {
3528 h2s = tmp_h2s;
3529 ret = 1;
3530 }
3531 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003532 HA_ATOMIC_INC(&h2c->px_counters->headers_rcvd);
Willy Tarreau13278b42017-10-13 19:23:14 +02003533 break;
3534
Willy Tarreau454f9052017-10-26 19:40:35 +02003535 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003536 if (h2c->st0 == H2_CS_FRAME_P) {
3537 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003538 ret = h2c_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003539 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003540 HA_ATOMIC_INC(&h2c->px_counters->data_rcvd);
Willy Tarreau454f9052017-10-26 19:40:35 +02003541
Willy Tarreau7838a792019-08-12 18:42:03 +02003542 if (h2c->st0 == H2_CS_FRAME_A) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003543 /* rcvd_s will suffice to trigger the sending of a WU */
3544 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7838a792019-08-12 18:42:03 +02003545 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003546 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003547
Willy Tarreau92153fc2017-12-03 19:46:19 +01003548 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003549 if (h2c->st0 == H2_CS_FRAME_P) {
3550 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003551 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003552 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003553 break;
3554
Willy Tarreaucd234e92017-08-18 10:59:39 +02003555 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003556 if (h2c->st0 == H2_CS_FRAME_P) {
3557 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 +02003558 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003559 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003560 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_rcvd);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003561 break;
3562
Willy Tarreaue96b0922017-10-30 00:28:29 +01003563 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003564 if (h2c->st0 == H2_CS_FRAME_P) {
3565 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003566 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003567 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003568 HA_ATOMIC_INC(&h2c->px_counters->goaway_rcvd);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003569 break;
3570
Willy Tarreau1c661982017-10-30 13:52:01 +01003571 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003572 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003573 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003574 /* drop frames that we ignore. They may be larger than
3575 * the buffer so we drain all of their contents until
3576 * we reach the end.
3577 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003578 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3579 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003580 h2c->dfl -= ret;
3581 ret = h2c->dfl == 0;
3582 }
3583
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003584 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003585 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003586 if (h2s->st == H2_SS_ERROR) {
3587 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 +01003588 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003589 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003590
Willy Tarreau7838a792019-08-12 18:42:03 +02003591 if (h2c->st0 == H2_CS_FRAME_E) {
3592 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 +01003593 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003594 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003595
Willy Tarreau7e98c052017-10-10 15:56:59 +02003596 /* error or missing data condition met above ? */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003597 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02003598 break;
3599
3600 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaubba7a4d2020-09-18 07:41:28 +02003601 if (h2c->dfl)
3602 TRACE_DEVEL("skipping remaining frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
Christopher Faulet5112a602019-09-26 16:38:28 +02003603 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3604 b_del(&h2c->dbuf, ret);
3605 h2c->dfl -= ret;
3606 if (!h2c->dfl) {
Willy Tarreau0f458712022-08-18 11:19:57 +02003607 h2c->flags &= ~H2_CF_DEM_IN_PROGRESS;
Christopher Faulet5112a602019-09-26 16:38:28 +02003608 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3609 h2c->st0 = H2_CS_FRAME_H;
Christopher Faulet5112a602019-09-26 16:38:28 +02003610 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003611 }
3612 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003613
Willy Tarreau617592c2022-06-08 16:32:22 +02003614 if (h2c->rcvd_s > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003615 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003616 TRACE_PROTO("sending stream WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn, h2s);
3617 h2c_send_strm_wu(h2c);
3618 }
3619
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003620 if (h2c->rcvd_c > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003621 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003622 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003623 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003624 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003625
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003626 done:
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003627 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_DEM_SHORT_READ)) {
3628 if (h2c->flags & H2_CF_RCVD_SHUT)
3629 h2c->flags |= H2_CF_END_REACHED;
3630 }
3631
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003632 if (h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003633 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003634 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003635 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003636 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003637 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003638 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003639 TRACE_DEVEL("notifying stream before switching SID", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn, h2s);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003640 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003641 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003642 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003643
Willy Tarreau7838a792019-08-12 18:42:03 +02003644 if (old_iw != h2c->miw) {
3645 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003646 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003647 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003648
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003649 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003650 out:
3651 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003652 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02003653}
3654
Willy Tarreau989539b2020-01-10 17:01:29 +01003655/* resume each h2s eligible for sending in list head <head> */
3656static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3657{
3658 struct h2s *h2s, *h2s_back;
3659
3660 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3661
3662 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3663 if (h2c->mws <= 0 ||
3664 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3665 h2c->st0 >= H2_CS_ERROR)
3666 break;
3667
3668 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003669
Willy Tarreaud9464162020-01-10 18:25:07 +01003670 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003671 continue;
3672
Willy Tarreau5723f292020-01-10 15:16:57 +01003673 /* If the sender changed his mind and unsubscribed, let's just
3674 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003675 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003676 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3677 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003678 LIST_DEL_INIT(&h2s->list);
3679 continue;
3680 }
3681
Willy Tarreauf96508a2020-01-10 11:12:48 +01003682 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003683 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003684 tasklet_wakeup(h2s->subs->tasklet);
3685 h2s->subs->events &= ~SUB_RETRY_SEND;
3686 if (!h2s->subs->events)
3687 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003688 }
3689 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3690 tasklet_wakeup(h2s->shut_tl);
3691 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003692 }
3693
3694 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3695}
3696
Willy Tarreaude4a5382023-10-17 08:25:19 +02003697/* removes a stream from the list it may be in. If a stream has recently been
3698 * appended to the send_list, it might have been waiting on this one when
3699 * entering h2_snd_buf() and expecting it to complete before starting to send
3700 * in turn. For this reason we check (and clear) H2_CF_WAIT_INLIST to detect
3701 * this condition, and we try to resume sending streams if it happens. Note
3702 * that we don't need to do it for fctl_list as this list is relevant before
3703 * (only consulted after) a window update on the connection, and not because
3704 * of any competition with other streams.
3705 */
3706static inline void h2_remove_from_list(struct h2s *h2s)
3707{
3708 struct h2c *h2c = h2s->h2c;
3709
3710 if (!LIST_INLIST(&h2s->list))
3711 return;
3712
3713 LIST_DEL_INIT(&h2s->list);
3714 if (h2c->flags & H2_CF_WAIT_INLIST) {
3715 h2c->flags &= ~H2_CF_WAIT_INLIST;
3716 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
3717 }
3718}
3719
Willy Tarreaubc933932017-10-09 16:21:43 +02003720/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3721 * the end.
3722 */
3723static int h2_process_mux(struct h2c *h2c)
3724{
Willy Tarreau7838a792019-08-12 18:42:03 +02003725 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3726
Willy Tarreau01b44822018-10-03 14:26:37 +02003727 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3728 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3729 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3730 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003731 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003732 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003733 goto fail;
3734 }
3735 h2c->st0 = H2_CS_SETTINGS1;
3736 }
3737 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003738 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003739 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003740 }
3741
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003742 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003743 if (h2c->rcvd_s > 0 &&
3744 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3745 h2c_send_strm_wu(h2c) < 0)
3746 goto fail;
3747
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003748 if (h2c->rcvd_c > 0 &&
3749 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3750 h2c_send_conn_wu(h2c) < 0)
3751 goto fail;
3752
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003753 /* First we always process the flow control list because the streams
3754 * waiting there were already elected for immediate emission but were
3755 * blocked just on this.
3756 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02003757 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01003758 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3759 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003760
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003761 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003762 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003763 if (h2c->st0 == H2_CS_ERROR) {
3764 if (h2c->max_id >= 0) {
3765 h2c_send_goaway_error(h2c, NULL);
3766 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003767 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003768 }
3769
3770 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3771 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003772 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003773 done:
3774 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3775 return 1;
3776 out0:
3777 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3778 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003779}
3780
Willy Tarreau62f52692017-10-08 23:01:42 +02003781
Willy Tarreau479998a2018-11-18 06:30:59 +01003782/* Attempt to read data, and subscribe if none available.
3783 * The function returns 1 if data has been received, otherwise zero.
3784 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003785static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003786{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003787 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003788 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003789 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003790 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003791
Willy Tarreau7838a792019-08-12 18:42:03 +02003792 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3793
3794 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3795 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003796 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003797 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003798
Willy Tarreau7838a792019-08-12 18:42:03 +02003799 if (!h2_recv_allowed(h2c)) {
3800 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003801 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003802 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003803
Willy Tarreau44e973f2018-03-01 17:49:30 +01003804 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003805 if (!buf) {
3806 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003807 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003808 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003809 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003810
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003811 if (!b_data(buf)) {
3812 /* try to pre-align the buffer like the
3813 * rxbufs will be to optimize memory copies. We'll make
3814 * sure that the frame header lands at the end of the
3815 * HTX block to alias it upon recv. We cannot use the
3816 * head because rcv_buf() will realign the buffer if
3817 * it's empty. Thus we cheat and pretend we already
3818 * have a few bytes there.
3819 */
3820 max = buf_room_for_htx_data(buf) + 9;
3821 buf->head = sizeof(struct htx) - 9;
3822 }
3823 else
3824 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003825
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003826 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003827
Christopher Fauletde9d6052021-04-23 12:25:18 +02003828 if (max && !ret && h2_recv_allowed(h2c)) {
3829 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
3830 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003831 } else if (ret) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02003832 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn, 0, 0, (void*)(long)ret);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003833 h2c->flags &= ~H2_CF_DEM_SHORT_READ;
3834 }
Olivier Houchard81a15af2018-10-19 17:26:49 +02003835
Christopher Fauletde9d6052021-04-23 12:25:18 +02003836 if (conn_xprt_read0_pending(h2c->conn)) {
3837 TRACE_DATA("received read0", H2_EV_H2C_RECV, h2c->conn);
3838 h2c->flags |= H2_CF_RCVD_SHUT;
3839 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02003840 if (h2c->conn->flags & CO_FL_ERROR) {
3841 TRACE_DATA("connection error", H2_EV_H2C_RECV, h2c->conn);
3842 h2c->flags |= H2_CF_ERROR;
3843 }
Christopher Fauletde9d6052021-04-23 12:25:18 +02003844
Olivier Houcharda1411e62018-08-17 18:42:48 +02003845 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003846 h2_release_buf(h2c, &h2c->dbuf);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003847 goto end;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003848 }
3849
Willy Tarreau7838a792019-08-12 18:42:03 +02003850 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003851 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003852 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003853 }
3854
Christopher Fauletff7925d2022-10-11 19:12:40 +02003855 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02003856 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003857 return !!ret || (h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR));
Willy Tarreau62f52692017-10-08 23:01:42 +02003858}
3859
Willy Tarreau479998a2018-11-18 06:30:59 +01003860/* Try to send data if possible.
3861 * The function returns 1 if data have been sent, otherwise zero.
3862 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003863static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003864{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003865 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003866 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003867 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003868
Willy Tarreau7838a792019-08-12 18:42:03 +02003869 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003870
Christopher Fauletff7925d2022-10-11 19:12:40 +02003871 if (h2c->flags & (H2_CF_ERROR|H2_CF_ERR_PENDING)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003872 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003873 if (h2c->flags & H2_CF_RCVD_SHUT)
3874 h2c->flags |= H2_CF_ERROR;
3875 b_reset(br_tail(h2c->mbuf));
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003876 h2c->idle_start = now_ms;
Willy Tarreau7838a792019-08-12 18:42:03 +02003877 return 1;
3878 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003879
Willy Tarreaubc933932017-10-09 16:21:43 +02003880 /* This loop is quite simple : it tries to fill as much as it can from
3881 * pending streams into the existing buffer until it's reportedly full
3882 * or the end of send requests is reached. Then it tries to send this
3883 * buffer's contents out, marks it not full if at least one byte could
3884 * be sent, and tries again.
3885 *
3886 * The snd_buf() function normally takes a "flags" argument which may
3887 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3888 * data immediately comes and CO_SFL_STREAMER to indicate that the
3889 * connection is streaming lots of data (used to increase TLS record
3890 * size at the expense of latency). The former can be sent any time
3891 * there's a buffer full flag, as it indicates at least one stream
3892 * attempted to send and failed so there are pending data. An
3893 * alternative would be to set it as long as there's an active stream
3894 * but that would be problematic for ACKs until we have an absolute
3895 * guarantee that all waiters have at least one byte to send. The
3896 * latter should possibly not be set for now.
3897 */
3898
3899 done = 0;
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02003900 while (!(conn->flags & CO_FL_WAIT_XPRT) && !done) {
Willy Tarreaubc933932017-10-09 16:21:43 +02003901 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003902 unsigned int released = 0;
3903 struct buffer *buf;
Willy Tarreau464fa062023-03-17 16:09:14 +01003904 uint to_send;
Willy Tarreaubc933932017-10-09 16:21:43 +02003905
3906 /* fill as much as we can into the current buffer */
3907 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3908 done = h2_process_mux(h2c);
3909
Olivier Houchard2b094432019-01-29 18:28:36 +01003910 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003911 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003912
Christopher Faulet9a3d3fc2020-10-22 16:24:58 +02003913 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
Willy Tarreaue6dc7a02021-10-21 17:30:06 +02003914 (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02003915 break;
3916
Christopher Faulet68ee7842022-10-12 10:21:33 +02003917 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))
Willy Tarreaubc933932017-10-09 16:21:43 +02003918 flags |= CO_SFL_MSG_MORE;
3919
Willy Tarreau464fa062023-03-17 16:09:14 +01003920 to_send = br_count(h2c->mbuf);
3921 if (to_send > 1) {
Willy Tarreau14ea98a2023-03-16 17:30:30 +01003922 /* usually we want to emit small TLS records to speed
3923 * up the decoding on the client. That's what is being
3924 * done by default. However if there is more than one
3925 * buffer being allocated, we're streaming large data
3926 * so we stich to large records.
3927 */
3928 flags |= CO_SFL_STREAMER;
3929 }
3930
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003931 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3932 if (b_data(buf)) {
Willy Tarreau464fa062023-03-17 16:09:14 +01003933 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf),
3934 flags | (to_send > 1 ? CO_SFL_MSG_MORE : 0));
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003935 if (!ret) {
3936 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003937 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003938 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003939 sent = 1;
Willy Tarreau464fa062023-03-17 16:09:14 +01003940 to_send--;
Willy Tarreau022e5e52020-09-10 09:33:15 +02003941 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn, 0, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003942 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003943 if (b_data(buf)) {
3944 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003945 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003946 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003947 }
3948 b_free(buf);
3949 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003950 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003951
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003952 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003953 offer_buffers(NULL, released);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003954
Willy Tarreau93c55112023-03-16 16:47:44 +01003955 /* Normally if wrote at least one byte, the buffer is not full
3956 * anymore. However, if it was marked full because all of its
3957 * buffers were used, we don't want to instantly wake up many
3958 * streams because we'd create a thundering herd effect, notably
3959 * when data are flushed in small chunks. Instead we wait for
3960 * the buffer to be decongested again before allowing to send
3961 * again. It also has the added benefit of not pumping more
3962 * data from the other side when it's known that this one is
3963 * still congested.
3964 */
3965 if (sent && br_single(h2c->mbuf))
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02003966 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02003967 }
3968
Christopher Fauletff7925d2022-10-11 19:12:40 +02003969 if (conn->flags & CO_FL_ERROR) {
3970 h2c->flags |= H2_CF_ERR_PENDING;
3971 if (h2c->flags & H2_CF_RCVD_SHUT)
3972 h2c->flags |= H2_CF_ERROR;
Willy Tarreau51330962019-05-26 09:38:07 +02003973 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003974 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02003975
Olivier Houchard6ff20392018-07-17 18:46:31 +02003976 /* We're not full anymore, so we can wake any task that are waiting
3977 * for us.
3978 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02003979 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
3980 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01003981 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02003982 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01003983
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003984 /* We're done, no more to send */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02003985 if (!(conn->flags & CO_FL_WAIT_XPRT) && !br_data(h2c->mbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003986 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Willy Tarreau936db562023-10-18 11:39:43 +02003987 if (h2c->flags & H2_CF_MBUF_HAS_DATA && !h2c->nb_sc) {
3988 h2c->flags &= ~H2_CF_MBUF_HAS_DATA;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003989 h2c->idle_start = now_ms;
Willy Tarreau936db562023-10-18 11:39:43 +02003990 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02003991 goto end;
Willy Tarreau7838a792019-08-12 18:42:03 +02003992 }
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02003993
Willy Tarreau7838a792019-08-12 18:42:03 +02003994 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
3995 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003996 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003997 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003998 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003999end:
4000 return sent || (h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR));
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004001}
4002
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004003/* this is the tasklet referenced in h2c->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004004struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004005{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004006 struct connection *conn;
4007 struct tasklet *tl = (struct tasklet *)t;
4008 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004009 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02004010 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004011
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004012 if (state & TASK_F_USR1) {
4013 /* the tasklet was idling on an idle connection, it might have
4014 * been stolen, let's be careful!
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004015 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004016 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4017 if (t->context == NULL) {
4018 /* The connection has been taken over by another thread,
4019 * we're no longer responsible for it, so just free the
4020 * tasklet, and do nothing.
4021 */
4022 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4023 tasklet_free(tl);
Willy Tarreau74163142021-03-13 11:30:19 +01004024 t = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004025 goto leave;
4026 }
4027 conn = h2c->conn;
4028 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004029
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004030 /* Remove the connection from the list, to be sure nobody attempts
4031 * to use it while we handle the I/O events
4032 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004033 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004034 if (conn_in_list)
4035 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004036
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004037 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4038 } else {
4039 /* we're certain the connection was not in an idle list */
4040 conn = h2c->conn;
4041 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4042 conn_in_list = 0;
4043 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004044
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004045 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02004046 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004047 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02004048 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01004049 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004050 ret = h2_process(h2c);
4051
4052 /* If we were in an idle list, we want to add it back into it,
4053 * unless h2_process() returned -1, which mean it has destroyed
4054 * the connection (testing !ret is enough, if h2_process() wasn't
4055 * called then ret will be 0 anyway.
4056 */
Willy Tarreau74163142021-03-13 11:30:19 +01004057 if (ret < 0)
4058 t = NULL;
4059
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004060 if (!ret && conn_in_list) {
4061 struct server *srv = objt_server(conn->target);
4062
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004063 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004064 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02004065 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004066 else
Willy Tarreau85223482022-09-29 20:32:43 +02004067 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004068 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004069 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004070
Willy Tarreau38468772020-06-28 00:31:13 +02004071leave:
Willy Tarreau7838a792019-08-12 18:42:03 +02004072 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreau74163142021-03-13 11:30:19 +01004073 return t;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004074}
Willy Tarreaua2af5122017-10-09 11:56:46 +02004075
Willy Tarreau62f52692017-10-08 23:01:42 +02004076/* callback called on any event by the connection handler.
4077 * It applies changes and returns zero, or < 0 if it wants immediate
4078 * destruction of the connection (which normally doesn not happen in h2).
4079 */
Olivier Houchard7505f942018-08-21 18:10:44 +02004080static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02004081{
Olivier Houchard7505f942018-08-21 18:10:44 +02004082 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02004083
Willy Tarreau7838a792019-08-12 18:42:03 +02004084 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4085
Willy Tarreauf0961222021-02-05 11:41:46 +01004086 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) &&
4087 (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01004088 h2_process_demux(h2c);
4089
Christopher Fauletff7925d2022-10-11 19:12:40 +02004090 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_ERROR))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004091 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004092
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004093 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01004094 h2c->flags &= ~H2_CF_DEM_DFULL;
4095 }
Olivier Houchard7505f942018-08-21 18:10:44 +02004096 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004097
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004098 if (unlikely(h2c->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) && !(h2c->flags & H2_CF_IS_BACK)) {
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02004099 int send_goaway = 1;
4100 /* If a close-spread-time option is set, we want to avoid
4101 * closing all the active HTTP2 connections at once so we add a
4102 * random factor that will spread the closing.
4103 */
4104 if (tick_isset(global.close_spread_end)) {
4105 int remaining_window = tick_remain(now_ms, global.close_spread_end);
4106 if (remaining_window) {
4107 /* This should increase the closing rate the
4108 * further along the window we are. */
4109 send_goaway = (remaining_window <= statistical_prng_range(global.close_spread_time));
4110 }
4111 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02004112 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
4113 send_goaway = 0; /* let the client close his connection himself */
Willy Tarreau8ec14062017-12-30 18:08:13 +01004114 /* frontend is stopping, reload likely in progress, let's try
4115 * to announce a graceful shutdown if not yet done. We don't
4116 * care if it fails, it will be tried again later.
4117 */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02004118 if (send_goaway) {
4119 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
4120 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
4121 if (h2c->last_sid < 0)
4122 h2c->last_sid = (1U << 31) - 1;
4123 h2c_send_goaway_error(h2c, NULL);
4124 }
Willy Tarreau8ec14062017-12-30 18:08:13 +01004125 }
4126 }
4127
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004128 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004129 * If we received early data, and the handshake is done, wake
4130 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004131 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004132 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01004133 (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 +01004134 struct eb32_node *node;
4135 struct h2s *h2s;
4136
4137 h2c->flags |= H2_CF_WAIT_FOR_HS;
4138 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
4139
4140 while (node) {
4141 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004142 if (se_fl_test(h2s->sd, SE_FL_WAIT_FOR_HS))
Willy Tarreau7e094452018-12-19 18:08:52 +01004143 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004144 node = eb32_next(node);
4145 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004146 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004147
Christopher Fauletff7925d2022-10-11 19:12:40 +02004148 if ((h2c->flags & H2_CF_ERROR) || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01004149 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
4150 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
4151 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02004152 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004153
4154 if (eb_is_empty(&h2c->streams_by_id)) {
4155 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02004156 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004157 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004158 return -1;
4159 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004160
4161 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004162 if (conn->flags & CO_FL_LIST_MASK) {
4163 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004164 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004165 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4166 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004167 }
4168 else if (h2c->st0 == H2_CS_ERROR) {
4169 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004170 if (conn->flags & CO_FL_LIST_MASK) {
4171 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004172 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004173 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4174 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004175 }
4176
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004177 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01004178 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004179
Christopher Fauletff7925d2022-10-11 19:12:40 +02004180 if (h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Olivier Houchard53216e72018-10-10 15:46:36 +02004181 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02004182 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02004183 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
4184 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02004185 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02004186
Willy Tarreau15a47332022-03-18 15:57:34 +01004187 h2c_update_timeout(h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02004188 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004189 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004190 return 0;
4191}
4192
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004193/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004194static int h2_wake(struct connection *conn)
4195{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004196 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02004197 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004198
Willy Tarreau7838a792019-08-12 18:42:03 +02004199 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4200 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01004201 if (ret >= 0)
4202 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02004203 TRACE_LEAVE(H2_EV_H2C_WAKE);
4204 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004205}
4206
Willy Tarreauea392822017-10-31 10:02:25 +01004207/* Connection timeout management. The principle is that if there's no receipt
4208 * nor sending for a certain amount of time, the connection is closed. If the
4209 * MUX buffer still has lying data or is not allocatable, the connection is
4210 * immediately killed. If it's allocatable and empty, we attempt to send a
4211 * GOAWAY frame.
4212 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004213struct task *h2_timeout_task(struct task *t, void *context, unsigned int state)
Willy Tarreauea392822017-10-31 10:02:25 +01004214{
Olivier Houchard9f6af332018-05-25 14:04:04 +02004215 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01004216 int expired = tick_is_expired(t->expire, now_ms);
4217
Willy Tarreau7838a792019-08-12 18:42:03 +02004218 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
4219
Willy Tarreaubd42e922020-06-30 11:19:23 +02004220 if (h2c) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004221 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004222 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004223
4224 /* Somebody already stole the connection from us, so we should not
4225 * free it, we just have to free the task.
4226 */
4227 if (!t->context) {
4228 h2c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004229 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004230 goto do_leave;
4231 }
4232
4233
Willy Tarreaubd42e922020-06-30 11:19:23 +02004234 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004235 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004236 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
4237 return t;
4238 }
Willy Tarreauea392822017-10-31 10:02:25 +01004239
Willy Tarreaubd42e922020-06-30 11:19:23 +02004240 if (!h2c_may_expire(h2c)) {
4241 /* we do still have streams but all of them are idle, waiting
4242 * for the data layer, so we must not enforce the timeout here.
4243 */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004244 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004245 t->expire = TICK_ETERNITY;
4246 return t;
4247 }
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004248
Willy Tarreaubd42e922020-06-30 11:19:23 +02004249 /* We're about to destroy the connection, so make sure nobody attempts
4250 * to steal it from us.
4251 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004252 if (h2c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004253 conn_delete_from_tree(&h2c->conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004254
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004255 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004256 }
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004257
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004258do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02004259 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02004260
4261 if (!h2c) {
4262 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02004263 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02004264 return NULL;
4265 }
4266
4267 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01004268 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02004269 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01004270
Willy Tarreau662fafc2019-05-26 09:43:07 +02004271 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01004272 /* don't even try to send a GOAWAY, the buffer is stuck */
4273 h2c->flags |= H2_CF_GOAWAY_FAILED;
4274 }
4275
4276 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01004277 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01004278 if (h2c_send_goaway_error(h2c, NULL) <= 0)
4279 h2c->flags |= H2_CF_GOAWAY_FAILED;
4280
Willy Tarreau662fafc2019-05-26 09:43:07 +02004281 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004282 unsigned int released = 0;
4283 struct buffer *buf;
4284
4285 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4286 if (b_data(buf)) {
4287 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
4288 if (!ret)
4289 break;
4290 b_del(buf, ret);
4291 if (b_data(buf))
4292 break;
4293 b_free(buf);
4294 released++;
4295 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02004296 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004297
4298 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004299 offer_buffers(NULL, released);
Willy Tarreau787db9a2018-06-14 18:31:46 +02004300 }
Willy Tarreauea392822017-10-31 10:02:25 +01004301
Willy Tarreau4481e262019-10-31 15:36:30 +01004302 /* in any case this connection must not be considered idle anymore */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004303 if (h2c->conn->flags & CO_FL_LIST_MASK) {
4304 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004305 conn_delete_from_tree(&h2c->conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004306 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4307 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004308
Willy Tarreau0975f112018-03-29 15:22:59 +02004309 /* either we can release everything now or it will be done later once
4310 * the last stream closes.
4311 */
4312 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02004313 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01004314
Willy Tarreau7838a792019-08-12 18:42:03 +02004315 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01004316 return NULL;
4317}
4318
4319
Willy Tarreau62f52692017-10-08 23:01:42 +02004320/*******************************************/
4321/* functions below are used by the streams */
4322/*******************************************/
4323
4324/*
4325 * Attach a new stream to a connection
4326 * (Used for outgoing connections)
4327 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004328static int h2_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02004329{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004330 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004331 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004332
Willy Tarreau7838a792019-08-12 18:42:03 +02004333 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004334 h2s = h2c_bck_stream_new(h2c, sd->sc, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004335 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004336 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01004337 return -1;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004338 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004339
4340 /* the connection is not idle anymore, let's mark this */
4341 HA_ATOMIC_AND(&h2c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004342 xprt_set_used(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004343
Willy Tarreau7838a792019-08-12 18:42:03 +02004344 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Christopher Faulete00ad352021-12-16 14:44:31 +01004345 return 0;
Willy Tarreau62f52692017-10-08 23:01:42 +02004346}
4347
Willy Tarreau4596fe22022-05-17 19:07:51 +02004348/* Retrieves the first valid stream connector from this connection, or returns
4349 * NULL. We have to scan because we may have some orphan streams. It might be
Willy Tarreaufafd3982018-11-18 21:29:20 +01004350 * beneficial to scan backwards from the end to reduce the likeliness to find
4351 * orphans.
4352 */
Willy Tarreaud1373532022-05-27 11:00:59 +02004353static struct stconn *h2_get_first_sc(const struct connection *conn)
Willy Tarreaufafd3982018-11-18 21:29:20 +01004354{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004355 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01004356 struct h2s *h2s;
4357 struct eb32_node *node;
4358
4359 node = eb32_first(&h2c->streams_by_id);
4360 while (node) {
4361 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004362 if (h2s_sc(h2s))
4363 return h2s_sc(h2s);
Willy Tarreaufafd3982018-11-18 21:29:20 +01004364 node = eb32_next(node);
4365 }
4366 return NULL;
4367}
4368
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004369static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4370{
4371 int ret = 0;
4372 struct h2c *h2c = conn->ctx;
4373
4374 switch (mux_ctl) {
4375 case MUX_STATUS:
4376 /* Only consider the mux to be ready if we're done with
4377 * the preface and settings, and we had no error.
4378 */
4379 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
4380 ret |= MUX_STATUS_READY;
4381 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02004382 case MUX_EXIT_STATUS:
4383 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004384 default:
4385 return -1;
4386 }
4387}
4388
Willy Tarreau62f52692017-10-08 23:01:42 +02004389/*
Olivier Houchard060ed432018-11-06 16:32:42 +01004390 * Destroy the mux and the associated connection, if it is no longer used
4391 */
Christopher Faulet73c12072019-04-08 11:23:22 +02004392static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01004393{
Christopher Faulet73c12072019-04-08 11:23:22 +02004394 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01004395
Willy Tarreau7838a792019-08-12 18:42:03 +02004396 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02004397 if (eb_is_empty(&h2c->streams_by_id)) {
4398 BUG_ON(h2c->conn->ctx != h2c);
Christopher Faulet73c12072019-04-08 11:23:22 +02004399 h2_release(h2c);
Christopher Faulet4e610962022-04-14 11:23:50 +02004400 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004401 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01004402}
4403
4404/*
Willy Tarreau62f52692017-10-08 23:01:42 +02004405 * Detach the stream from the connection and possibly release the connection.
4406 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004407static void h2_detach(struct sedesc *sd)
Willy Tarreau62f52692017-10-08 23:01:42 +02004408{
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004409 struct h2s *h2s = sd->se;
Willy Tarreau60935142017-10-16 18:11:19 +02004410 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01004411 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004412
Willy Tarreau7838a792019-08-12 18:42:03 +02004413 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
4414
Willy Tarreau7838a792019-08-12 18:42:03 +02004415 if (!h2s) {
4416 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02004417 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004418 }
Willy Tarreau60935142017-10-16 18:11:19 +02004419
Willy Tarreaud9464162020-01-10 18:25:07 +01004420 /* there's no txbuf so we're certain not to be able to send anything */
4421 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02004422
Olivier Houchardf502aca2018-12-14 19:42:40 +01004423 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004424 h2c = h2s->h2c;
Willy Tarreau36c22322022-05-27 10:41:24 +02004425 h2c->nb_sc--;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004426 if (!h2c->nb_sc && !br_data(h2c->mbuf))
Willy Tarreau15a47332022-03-18 15:57:34 +01004427 h2c->idle_start = now_ms;
4428
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004429 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
Willy Tarreau36c22322022-05-27 10:41:24 +02004430 !h2_frt_has_too_many_sc(h2c)) {
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004431 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02004432 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004433 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02004434 }
Willy Tarreau60935142017-10-16 18:11:19 +02004435
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004436 /* this stream may be blocked waiting for some data to leave (possibly
4437 * an ES or RST frame), so orphan it in this case.
4438 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02004439 if (!(h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02004440 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01004441 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01004442 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004443 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau15a47332022-03-18 15:57:34 +01004444 /* refresh the timeout if none was active, so that the last
4445 * leaving stream may arm it.
4446 */
Willy Tarreau3fb2c6d2023-03-16 18:06:19 +01004447 if (h2c->task && !tick_isset(h2c->task->expire))
Willy Tarreau15a47332022-03-18 15:57:34 +01004448 h2c_update_timeout(h2c);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004449 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004450 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004451
Christopher Faulet68ee7842022-10-12 10:21:33 +02004452 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi)) {
Willy Tarreau45f752e2017-10-30 15:44:59 +01004453 /* unblock the connection if it was blocked on this
4454 * stream.
4455 */
4456 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
4457 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004458 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01004459 }
4460
Willy Tarreau71049cc2018-03-28 13:56:39 +02004461 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02004462
Christopher Faulet9b79a102019-07-15 11:22:56 +02004463 if (h2c->flags & H2_CF_IS_BACK) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02004464 if (!(h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERR_PENDING|H2_CF_ERROR))) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004465 if (h2c->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02004466 /* Add the connection in the session server list, if not already done */
4467 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
4468 h2c->conn->owner = NULL;
4469 if (eb_is_empty(&h2c->streams_by_id)) {
4470 h2c->conn->mux->destroy(h2c);
4471 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4472 return;
Christopher Fauletc5579d12020-07-01 15:45:41 +02004473 }
4474 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02004475 if (eb_is_empty(&h2c->streams_by_id)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004476 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
4477 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
4478 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houchard351411f2018-12-27 17:20:54 +01004479 return;
4480 }
4481 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004482 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004483 else {
4484 if (eb_is_empty(&h2c->streams_by_id)) {
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004485 /* If the connection is owned by the session, first remove it
4486 * from its list
4487 */
4488 if (h2c->conn->owner) {
4489 session_unown_conn(h2c->conn->owner, h2c->conn);
4490 h2c->conn->owner = NULL;
4491 }
4492
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004493 /* mark that the tasklet may lose its context to another thread and
4494 * that the handler needs to check it under the idle conns lock.
4495 */
4496 HA_ATOMIC_OR(&h2c->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004497 xprt_set_idle(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
4498
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004499 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01004500 /* The server doesn't want it, let's kill the connection right away */
4501 h2c->conn->mux->destroy(h2c);
4502 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4503 return;
4504 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01004505 /* At this point, the connection has been added to the
4506 * server idle list, so another thread may already have
4507 * hijacked it, so we can't do anything with it.
4508 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01004509 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
4510 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01004511
Olivier Houchard8a786902018-12-15 16:05:40 +01004512 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004513 else if (!h2c->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004514 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02004515 !LIST_INLIST(&h2c->conn->session_list)) {
Willy Tarreau85223482022-09-29 20:32:43 +02004516 eb64_insert(&__objt_server(h2c->conn->target)->per_thr[tid].avail_conns,
4517 &h2c->conn->hash_node->node);
Christopher Fauletc5579d12020-07-01 15:45:41 +02004518 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004519 }
4520 }
4521 }
4522
Willy Tarreaue323f342018-03-28 13:51:45 +02004523 /* We don't want to close right now unless we're removing the
4524 * last stream, and either the connection is in error, or it
4525 * reached the ID already specified in a GOAWAY frame received
4526 * or sent (as seen by last_sid >= 0).
4527 */
Olivier Houchard7a977432019-03-21 15:47:13 +01004528 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02004529 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02004530 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02004531 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02004532 }
4533 else if (h2c->task) {
Willy Tarreau15a47332022-03-18 15:57:34 +01004534 h2c_update_timeout(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004535 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02004536 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004537 else
4538 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004539}
4540
Willy Tarreau88bdba32019-05-13 18:17:53 +02004541/* Performs a synchronous or asynchronous shutr(). */
4542static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004543{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004544 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004545
Willy Tarreauf983d002019-05-14 10:40:21 +02004546 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004547 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004548
Willy Tarreau7838a792019-08-12 18:42:03 +02004549 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4550
Christopher Faulet24b85802023-10-16 19:30:02 +02004551 if (h2s->flags & H2_SF_WANT_SHUTW)
4552 goto add_to_list;
4553
Willy Tarreau18059042019-01-31 19:12:48 +01004554 /* a connstream may require us to immediately kill the whole connection
4555 * for example because of a "tcp-request content reject" rule that is
4556 * normally used to limit abuse. In this case we schedule a goaway to
4557 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004558 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004559 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004560 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004561 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004562 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4563 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4564 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004565 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4566 /* Nothing was never sent for this stream, so reset with
4567 * REFUSED_STREAM error to let the client retry the
4568 * request.
4569 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004570 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004571 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4572 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004573 else {
4574 /* a final response was already provided, we don't want this
4575 * stream anymore. This may happen when the server responds
4576 * before the end of an upload and closes quickly (redirect,
4577 * deny, ...)
4578 */
4579 h2s_error(h2s, H2_ERR_CANCEL);
4580 }
Willy Tarreau18059042019-01-31 19:12:48 +01004581
Willy Tarreau90c32322017-11-24 08:00:30 +01004582 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004583 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004584 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004585
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004586 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004587 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004588 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004589 done:
4590 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004591 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004592 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004593add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004594 /* Let the handler know we want to shutr, and add ourselves to the
4595 * most relevant list if not yet done. h2_deferred_shut() will be
4596 * automatically called via the shut_tl tasklet when there's room
4597 * again.
4598 */
4599 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau2b718102021-04-21 07:32:39 +02004600 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004601 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004602 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004603 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004604 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004605 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004606 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004607 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004608}
4609
Willy Tarreau88bdba32019-05-13 18:17:53 +02004610/* Performs a synchronous or asynchronous shutw(). */
4611static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004612{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004613 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004614
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004615 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004616 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004617
Willy Tarreau7838a792019-08-12 18:42:03 +02004618 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4619
Willy Tarreau473e0e52022-08-18 16:12:15 +02004620 if (h2s->st != H2_SS_ERROR &&
4621 (h2s->flags & (H2_SF_HEADERS_SENT | H2_SF_MORE_HTX_DATA)) == H2_SF_HEADERS_SENT) {
4622 /* we can cleanly close using an empty data frame only after headers
4623 * and if no more data is expected to be sent.
4624 */
Willy Tarreau58e32082017-11-07 14:41:09 +01004625 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4626 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004627 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004628
4629 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004630 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004631 else
4632 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004633 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004634 /* a connstream may require us to immediately kill the whole connection
4635 * for example because of a "tcp-request content reject" rule that is
4636 * normally used to limit abuse. In this case we schedule a goaway to
4637 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004638 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004639 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004640 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004641 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004642 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4643 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4644 }
Willy Tarreau473e0e52022-08-18 16:12:15 +02004645 else if (h2s->flags & H2_SF_MORE_HTX_DATA) {
4646 /* some unsent data were pending (e.g. abort during an upload),
4647 * let's send a CANCEL.
4648 */
4649 TRACE_STATE("shutw before end of data, sending CANCEL", H2_EV_STRM_SHUT, h2c->conn, h2s);
4650 h2s_error(h2s, H2_ERR_CANCEL);
4651 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004652 else {
4653 /* Nothing was never sent for this stream, so reset with
4654 * REFUSED_STREAM error to let the client retry the
4655 * request.
4656 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004657 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004658 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4659 }
Willy Tarreau18059042019-01-31 19:12:48 +01004660
Willy Tarreau90c32322017-11-24 08:00:30 +01004661 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004662 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004663 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004664
Willy Tarreau00dd0782018-03-01 16:31:34 +01004665 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004666 }
4667
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004668 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004669 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004670
4671 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4672
Willy Tarreau88bdba32019-05-13 18:17:53 +02004673 done:
4674 h2s->flags &= ~H2_SF_WANT_SHUTW;
4675 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004676
4677 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004678 /* Let the handler know we want to shutw, and add ourselves to the
4679 * most relevant list if not yet done. h2_deferred_shut() will be
4680 * automatically called via the shut_tl tasklet when there's room
4681 * again.
4682 */
4683 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau2b718102021-04-21 07:32:39 +02004684 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004685 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004686 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004687 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004688 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004689 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004690 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004691 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004692}
4693
Willy Tarreau5723f292020-01-10 15:16:57 +01004694/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004695 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4696 * and prevented the last frame from being emitted.
4697 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004698struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004699{
4700 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004701 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004702
Willy Tarreau7838a792019-08-12 18:42:03 +02004703 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4704
Willy Tarreau5723f292020-01-10 15:16:57 +01004705 if (h2s->flags & H2_SF_NOTIFIED) {
4706 /* some data processing remains to be done first */
4707 goto end;
4708 }
4709
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004710 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004711 h2_do_shutw(h2s);
4712
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004713 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004714 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004715
Willy Tarreau88bdba32019-05-13 18:17:53 +02004716 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004717 /* We're done trying to send, remove ourself from the send_list */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004718 h2_remove_from_list(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01004719
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004720 if (!h2s_sc(h2s)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004721 h2s_destroy(h2s);
Willy Tarreau74163142021-03-13 11:30:19 +01004722 if (h2c_is_dead(h2c)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004723 h2_release(h2c);
Willy Tarreau74163142021-03-13 11:30:19 +01004724 t = NULL;
4725 }
Willy Tarreau88bdba32019-05-13 18:17:53 +02004726 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004727 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004728 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004729 TRACE_LEAVE(H2_EV_STRM_SHUT);
Willy Tarreau74163142021-03-13 11:30:19 +01004730 return t;
Willy Tarreau62f52692017-10-08 23:01:42 +02004731}
4732
Willy Tarreau4596fe22022-05-17 19:07:51 +02004733/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004734static void h2_shutr(struct stconn *sc, enum co_shr_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004735{
Willy Tarreau36c22322022-05-27 10:41:24 +02004736 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004737
Willy Tarreau7838a792019-08-12 18:42:03 +02004738 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004739 if (mode)
4740 h2_do_shutr(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004741 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004742}
4743
Willy Tarreau4596fe22022-05-17 19:07:51 +02004744/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004745static void h2_shutw(struct stconn *sc, enum co_shw_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004746{
Willy Tarreau36c22322022-05-27 10:41:24 +02004747 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004748
Willy Tarreau7838a792019-08-12 18:42:03 +02004749 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004750 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004751 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004752}
4753
Christopher Faulet9b79a102019-07-15 11:22:56 +02004754/* Decode the payload of a HEADERS frame and produce the HTX request or response
4755 * depending on the connection's side. Returns a positive value on success, a
4756 * negative value on failure, or 0 if it couldn't proceed. May report connection
4757 * errors in h2c->errcode if the frame is non-decodable and the connection
4758 * unrecoverable. In absence of connection error when a failure is reported, the
4759 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004760 *
4761 * The function may fold CONTINUATION frames into the initial HEADERS frame
4762 * by removing padding and next frame header, then moving the CONTINUATION
4763 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4764 * leaving a hole between the main frame and the beginning of the next one.
4765 * The possibly remaining incomplete or next frame at the end may be moved
4766 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4767 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4768 *
4769 * A buffer at the beginning of processing may look like this :
4770 *
4771 * ,---.---------.-----.--------------.--------------.------.---.
4772 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4773 * `---^---------^-----^--------------^--------------^------^---'
4774 * | | <-----> | |
4775 * area | dpl | wrap
4776 * |<--------------> |
4777 * | dfl |
4778 * |<-------------------------------------------------->|
4779 * head data
4780 *
4781 * Padding is automatically overwritten when folding, participating to the
4782 * hole size after dfl :
4783 *
4784 * ,---.------------------------.-----.--------------.------.---.
4785 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4786 * `---^------------------------^-----^--------------^------^---'
4787 * | | <-----> | |
4788 * area | hole | wrap
4789 * |<-----------------------> |
4790 * | dfl |
4791 * |<-------------------------------------------------->|
4792 * head data
4793 *
4794 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4795 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4796 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004797 *
4798 * The <flags> field must point to either the stream's flags or to a copy of it
4799 * so that the function can update the following flags :
4800 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004801 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004802 *
4803 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4804 * decoding, in order to detect if we're dealing with a headers or a trailers
4805 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004806 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01004807static int h2c_dec_hdrs(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len, char *upgrade_protocol)
Willy Tarreau13278b42017-10-13 19:23:14 +02004808{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004809 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004810 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004811 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004812 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004813 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004814 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004815 int flen; // header frame len
4816 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004817 int ret = 0;
4818 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004819 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004820
Willy Tarreau7838a792019-08-12 18:42:03 +02004821 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4822
Willy Tarreauea18f862018-12-22 20:19:26 +01004823next_frame:
4824 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4825 goto leave; // incomplete input frame
4826
4827 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4828 * this case, we'll try to paste it immediately after the initial
4829 * HEADERS frame payload and kill any possible padding. The initial
4830 * frame's length will be increased to represent the concatenation
4831 * of the two frames. The next frame is read from position <tlen>
4832 * and written at position <flen> (minus padding if some is present).
4833 */
4834 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4835 struct h2_fh hdr;
4836 int clen; // CONTINUATION frame's payload length
4837
Willy Tarreau7838a792019-08-12 18:42:03 +02004838 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 +01004839 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4840 /* no more data, the buffer may be full, either due to
4841 * too large a frame or because of too large a hole that
4842 * we're going to compact at the end.
4843 */
4844 goto leave;
4845 }
4846
4847 if (hdr.ft != H2_FT_CONTINUATION) {
4848 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004849 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 +01004850 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004851 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004852 goto fail;
4853 }
4854
4855 if (hdr.sid != h2c->dsi) {
4856 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004857 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 +01004858 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004859 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004860 goto fail;
4861 }
4862
4863 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4864 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004865 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 +01004866 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4867 goto fail;
4868 }
4869
4870 /* detect when we must stop aggragating frames */
4871 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4872
4873 /* Take as much as we can of the CONTINUATION frame's payload */
4874 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4875 if (clen > hdr.len)
4876 clen = hdr.len;
4877
4878 /* Move the frame's payload over the padding, hole and frame
4879 * header. At least one of hole or dpl is null (see diagrams
4880 * above). The hole moves after the new aggragated frame.
4881 */
4882 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
Christopher Fauletcb1847c2021-04-21 11:11:21 +02004883 h2c->dfl += hdr.len - h2c->dpl;
Willy Tarreauea18f862018-12-22 20:19:26 +01004884 hole += h2c->dpl + 9;
4885 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004886 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 +01004887 goto next_frame;
4888 }
4889
4890 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004891
Willy Tarreau13278b42017-10-13 19:23:14 +02004892 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004893 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004894 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004895 copy = alloc_trash_chunk();
4896 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004897 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 +02004898 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4899 goto fail;
4900 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004901 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4902 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4903 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004904 }
4905
Willy Tarreau13278b42017-10-13 19:23:14 +02004906 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4907 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004908 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004909 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004910 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 +01004911 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004912 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004913 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004914 }
4915
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004916 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004917 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 +01004918 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4919 goto fail;
4920 }
4921
Willy Tarreau13278b42017-10-13 19:23:14 +02004922 hdrs += 5; // stream dep = 4, weight = 1
4923 flen -= 5;
4924 }
4925
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004926 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004927 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 +01004928 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004929 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004930 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004931
Willy Tarreau937f7602018-02-26 15:22:17 +01004932 /* we can't retry a failed decompression operation so we must be very
4933 * careful not to take any risks. In practice the output buffer is
4934 * always empty except maybe for trailers, in which case we simply have
4935 * to wait for the upper layer to finish consuming what is available.
4936 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004937 htx = htx_from_buf(rxbuf);
4938 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004939 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 +02004940 h2c->flags |= H2_CF_DEM_SFULL;
4941 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004942 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004943
Willy Tarreau25919232019-01-03 14:48:18 +01004944 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004945 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4946 sizeof(list)/sizeof(list[0]), tmp);
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01004947
4948 if (outlen > 0 &&
4949 (TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
4950 TRACE_ENABLED(TRACE_LEVEL_USER, H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, 0, 0, 0)) {
4951 struct ist n;
4952 int i;
4953
4954 for (i = 0; list[i].n.len; i++) {
4955 n = list[i].n;
4956
4957 if (!isttest(n)) {
4958 /* this is in fact a pseudo header whose number is in n.len */
4959 n = h2_phdr_to_ist(n.len);
4960 }
4961
4962 h2_trace_header(n, list[i].v, H2_EV_RX_FRAME|H2_EV_RX_HDR,
4963 ist(TRC_LOC), __FUNCTION__, h2c, NULL);
4964 }
4965 }
4966
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004967 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004968 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 +01004969 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
4970 goto fail;
4971 }
4972
Willy Tarreau25919232019-01-03 14:48:18 +01004973 /* The PACK decompressor was updated, let's update the input buffer and
4974 * the parser's state to commit these changes and allow us to later
4975 * fail solely on the stream if needed.
4976 */
4977 b_del(&h2c->dbuf, h2c->dfl + hole);
4978 h2c->dfl = hole = 0;
4979 h2c->st0 = H2_CS_FRAME_H;
4980
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004981 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01004982 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Christopher Fauletd0db4232021-01-22 11:46:30 +01004983 msgf |= (*flags & H2_SF_BODY_TUNNEL) ? H2_MSGF_BODY_TUNNEL: 0;
Amaury Denoyelle74162742020-12-11 17:53:05 +01004984 /* If an Extended CONNECT has been sent on this stream, set message flag
Ilya Shipitsinacf84592021-02-06 22:29:08 +05004985 * to convert 200 response to 101 htx response */
Amaury Denoyelle74162742020-12-11 17:53:05 +01004986 msgf |= (*flags & H2_SF_EXT_CONNECT_SENT) ? H2_MSGF_EXT_CONNECT: 0;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004987
Willy Tarreau88d138e2019-01-02 19:38:14 +01004988 if (*flags & H2_SF_HEADERS_RCVD)
4989 goto trailers;
4990
4991 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004992 if (h2c->flags & H2_CF_IS_BACK)
Amaury Denoyelle74162742020-12-11 17:53:05 +01004993 outlen = h2_make_htx_response(list, htx, &msgf, body_len, upgrade_protocol);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004994 else
Willy Tarreaub6be1a42023-08-08 15:38:28 +02004995 outlen = h2_make_htx_request(list, htx, &msgf, body_len,
4996 !!(((const struct session *)h2c->conn->owner)->fe->options2 & PR_O2_REQBUG_OK));
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004997
Christopher Faulet3d875582021-04-26 17:46:13 +02004998 if (outlen < 0 || htx_free_space(htx) < global.tune.maxrewrite) {
Willy Tarreau25919232019-01-03 14:48:18 +01004999 /* too large headers? this is a stream error only */
Christopher Faulet3d875582021-04-26 17:46:13 +02005000 TRACE_STATE("message headers too large", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR|H2_EV_PROTO_ERR, h2c->conn);
5001 htx->flags |= HTX_FL_PARSING_ERROR;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005002 goto fail;
5003 }
Willy Tarreau13278b42017-10-13 19:23:14 +02005004
Willy Tarreau174b06a2018-04-25 18:13:58 +02005005 if (msgf & H2_MSGF_BODY) {
5006 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005007 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005008 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005009 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005010 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02005011 }
Christopher Faulet7d247f02020-12-02 14:26:36 +01005012 if (msgf & H2_MSGF_BODYLESS_RSP)
5013 *flags |= H2_SF_BODYLESS_RESP;
Willy Tarreau174b06a2018-04-25 18:13:58 +02005014
Christopher Fauletd0db4232021-01-22 11:46:30 +01005015 if (msgf & H2_MSGF_BODY_TUNNEL)
5016 *flags |= H2_SF_BODY_TUNNEL;
5017 else {
5018 /* Abort the tunnel attempt, if any */
5019 if (*flags & H2_SF_BODY_TUNNEL)
5020 *flags |= H2_SF_TUNNEL_ABRT;
5021 *flags &= ~H2_SF_BODY_TUNNEL;
5022 }
5023
Willy Tarreau88d138e2019-01-02 19:38:14 +01005024 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01005025 /* indicate that a HEADERS frame was received for this stream, except
5026 * for 1xx responses. For 1xx responses, another HEADERS frame is
5027 * expected.
5028 */
5029 if (!(msgf & H2_MSGF_RSP_1XX))
5030 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01005031
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005032 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Christopher Faulet827a6292022-12-22 09:47:01 +01005033 if (msgf & H2_MSGF_RSP_1XX) {
5034 /* RFC9113#8.1 : HEADERS frame with the ES flag set that carries an informational status code is malformed */
5035 TRACE_STATE("invalid interim response with ES flag!", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
5036 goto fail;
5037 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005038 /* no more data are expected for this message */
5039 htx->flags |= HTX_FL_EOM;
Christopher Faulet1a60a662023-05-24 11:02:50 +02005040 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005041 }
Willy Tarreau937f7602018-02-26 15:22:17 +01005042
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005043 if (msgf & H2_MSGF_EXT_CONNECT)
5044 *flags |= H2_SF_EXT_CONNECT_RCVD;
5045
Willy Tarreau86277d42019-01-02 15:36:11 +01005046 /* success */
5047 ret = 1;
5048
Willy Tarreau68dd9852017-07-03 14:44:26 +02005049 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01005050 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01005051 * move the remaining data over it.
5052 */
5053 if (hole) {
5054 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
5055 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
5056 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
5057 b_sub(&h2c->dbuf, hole);
5058 }
5059
Christopher Faulete3ec2172023-12-13 15:36:52 +01005060 if (b_full(&h2c->dbuf) && h2c->dfl && (!htx || htx_is_empty(htx))) {
Willy Tarreauea18f862018-12-22 20:19:26 +01005061 /* too large frames */
5062 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01005063 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01005064 }
5065
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005066 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005067 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02005068 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02005069 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01005070 return ret;
5071
Willy Tarreau68dd9852017-07-03 14:44:26 +02005072 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01005073 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02005074 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005075
5076 trailers:
5077 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01005078 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
5079 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02005080 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 +01005081 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005082 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau88d138e2019-01-02 19:38:14 +01005083 goto fail;
5084 }
5085
Christopher Faulet9b79a102019-07-15 11:22:56 +02005086 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02005087 if (h2_make_htx_trailers(list, htx) <= 0) {
5088 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 +02005089 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005090 }
Christopher Faulet1a60a662023-05-24 11:02:50 +02005091 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005092 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02005093}
5094
Christopher Faulet9b79a102019-07-15 11:22:56 +02005095/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005096 * parser state is automatically updated. Returns > 0 if it could completely
5097 * send the current frame, 0 if it couldn't complete, in which case
Willy Tarreaub605c422022-05-17 17:04:55 +02005098 * SE_FL_RCV_MORE must be checked to know if some data remain pending (an empty
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005099 * DATA frame can return 0 as a valid result). Stream errors are reported in
5100 * h2s->errcode and connection errors in h2c->errcode. The caller must already
5101 * have checked the frame header and ensured that the frame was complete or the
5102 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02005103 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01005104static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02005105{
5106 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005107 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005108 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005109 struct htx *htx = NULL;
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005110 struct buffer *scbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005111 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02005112
Willy Tarreau7838a792019-08-12 18:42:03 +02005113 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5114
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005115 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02005116
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005117 scbuf = h2_get_buf(h2c, &h2s->rxbuf);
5118 if (!scbuf) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005119 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02005120 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 +01005121 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005122 }
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005123 htx = htx_from_buf(scbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01005124
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005125try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005126 flen = h2c->dfl - h2c->dpl;
5127 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01005128 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005129
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005130 if (flen > b_data(&h2c->dbuf)) {
5131 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005132 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01005133 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005134 }
5135
Christopher Faulet9b79a102019-07-15 11:22:56 +02005136 block = htx_free_data_space(htx);
5137 if (!block) {
5138 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005139 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 +02005140 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005141 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005142 if (flen > block)
5143 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005144
Christopher Faulet9b79a102019-07-15 11:22:56 +02005145 /* here, flen is the max we can copy into the output buffer */
5146 block = b_contig_data(&h2c->dbuf, 0);
5147 if (flen > block)
5148 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005149
Christopher Faulet9b79a102019-07-15 11:22:56 +02005150 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau022e5e52020-09-10 09:33:15 +02005151 TRACE_DATA("move some data to h2s rxbuf", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s, 0, (void *)(long)sent);
Willy Tarreau454f9052017-10-26 19:40:35 +02005152
Christopher Faulet9b79a102019-07-15 11:22:56 +02005153 b_del(&h2c->dbuf, sent);
5154 h2c->dfl -= sent;
5155 h2c->rcvd_c += sent;
5156 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02005157
Christopher Faulet9b79a102019-07-15 11:22:56 +02005158 if (h2s->flags & H2_SF_DATA_CLEN) {
5159 h2s->body_len -= sent;
5160 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005161 }
5162
Christopher Faulet9b79a102019-07-15 11:22:56 +02005163 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005164 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005165 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 +01005166 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005167 }
5168
Christopher Faulet9b79a102019-07-15 11:22:56 +02005169 goto try_again;
5170
Willy Tarreau4a28da12018-01-04 14:41:00 +01005171 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005172 /* here we're done with the frame, all the payload (except padding) was
5173 * transferred.
5174 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02005175
Christopher Faulet5be651d2021-01-22 15:28:03 +01005176 if (!(h2s->flags & H2_SF_BODY_TUNNEL) && (h2c->dff & H2_F_DATA_END_STREAM)) {
5177 /* no more data are expected for this message. This add the EOM
5178 * flag but only on the response path or if no tunnel attempt
5179 * was aborted. Otherwise (request path + tunnel abrted), the
5180 * EOM was already reported.
5181 */
Christopher Faulet33724322021-02-10 09:04:59 +01005182 if ((h2c->flags & H2_CF_IS_BACK) || !(h2s->flags & H2_SF_TUNNEL_ABRT)) {
Amaury Denoyelle25cf19d2023-05-11 16:50:04 +02005183 /* htx may be empty if receiving an empty DATA frame. */
5184 if (!htx_set_eom(htx))
5185 goto fail;
Christopher Faulet33724322021-02-10 09:04:59 +01005186 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02005187 }
5188
Willy Tarreaud1023bb2018-03-22 16:53:12 +01005189 h2c->rcvd_c += h2c->dpl;
5190 h2c->rcvd_s += h2c->dpl;
5191 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005192 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005193 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005194 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005195 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01005196 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005197 if (htx)
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005198 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005199 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01005200 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005201}
5202
Willy Tarreau115e83b2018-12-01 19:17:53 +01005203/* Try to send a HEADERS frame matching HTX response present in HTX message
5204 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5205 * must check the stream's status to detect any error which might have happened
5206 * subsequently to a successful send. The htx blocks are automatically removed
5207 * from the message. The htx message is assumed to be valid since produced from
5208 * the internal code, hence it contains a start line, an optional series of
5209 * header blocks and an end of header, otherwise an invalid frame could be
5210 * emitted and the resulting htx message could be left in an inconsistent state.
5211 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005212static size_t h2s_snd_fhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005213{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005214 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01005215 struct h2c *h2c = h2s->h2c;
5216 struct htx_blk *blk;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005217 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005218 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005219 struct htx_sl *sl;
5220 enum htx_blk_type type;
5221 int es_now = 0;
5222 int ret = 0;
5223 int hdr;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005224
Willy Tarreau7838a792019-08-12 18:42:03 +02005225 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5226
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005227 /* get the start line (we do have one) and the rest of the headers,
5228 * that we dump starting at header 0 */
5229 sl = NULL;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005230 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005231 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005232 type = htx_get_blk_type(blk);
5233
5234 if (type == HTX_BLK_UNUSED)
5235 continue;
5236
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005237 if (type == HTX_BLK_EOH)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005238 break;
5239
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005240 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005241 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005242 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5243 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5244 goto fail;
5245 }
5246
5247 list[hdr].n = htx_get_blk_name(htx, blk);
5248 list[hdr].v = htx_get_blk_value(htx, blk);
5249 hdr++;
5250 }
5251 else if (type == HTX_BLK_RES_SL) {
Christopher Faulet56498132021-01-29 11:39:43 +01005252 BUG_ON(sl); /* Only one start-line expected */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005253 sl = htx_get_blk_ptr(htx, blk);
5254 h2s->status = sl->info.res.status;
Christopher Faulet7d247f02020-12-02 14:26:36 +01005255 if (h2s->status == 204 || h2s->status == 304)
5256 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005257 if (h2s->status < 100 || h2s->status > 999) {
5258 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5259 goto fail;
5260 }
5261 else if (h2s->status == 101) {
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005262 if (unlikely(h2s->flags & H2_SF_EXT_CONNECT_RCVD)) {
5263 /* If an Extended CONNECT has been received, we need to convert 101 to 200 */
5264 h2s->status = 200;
5265 h2s->flags &= ~H2_SF_EXT_CONNECT_RCVD;
5266 }
5267 else {
5268 /* Otherwise, 101 responses are not supported in H2, so return a error (RFC7540#8.1.1) */
5269 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5270 goto fail;
5271 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005272 }
5273 else if ((h2s->flags & H2_SF_BODY_TUNNEL) && h2s->status >= 300) {
5274 /* Abort the tunnel attempt */
5275 h2s->flags &= ~H2_SF_BODY_TUNNEL;
5276 h2s->flags |= H2_SF_TUNNEL_ABRT;
5277 }
5278 }
5279 else {
5280 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005281 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005282 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005283 }
5284
Christopher Faulet56498132021-01-29 11:39:43 +01005285 /* The start-line me be defined */
5286 BUG_ON(!sl);
5287
Willy Tarreau115e83b2018-12-01 19:17:53 +01005288 /* marker for end of headers */
5289 list[hdr].n = ist("");
5290
Willy Tarreau9c218e72019-05-26 10:08:28 +02005291 mbuf = br_tail(h2c->mbuf);
5292 retry:
5293 if (!h2_get_buf(h2c, mbuf)) {
5294 h2c->flags |= H2_CF_MUX_MALLOC;
5295 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005296 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 +02005297 return 0;
5298 }
5299
Willy Tarreau115e83b2018-12-01 19:17:53 +01005300 chunk_reset(&outbuf);
5301
5302 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005303 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5304 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005305 break;
5306 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005307 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01005308 }
5309
5310 if (outbuf.size < 9)
5311 goto full;
5312
5313 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5314 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5315 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5316 outbuf.data = 9;
5317
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005318 if ((h2c->flags & (H2_CF_SHTS_UPDATED|H2_CF_DTSU_EMITTED)) == H2_CF_SHTS_UPDATED) {
5319 /* SETTINGS_HEADER_TABLE_SIZE changed, we must send an HPACK
5320 * dynamic table size update so that some clients are not
5321 * confused. In practice we only need to send the DTSU when the
5322 * advertised size is lower than the current one, and since we
5323 * don't use it and don't care about the default 4096 bytes,
5324 * we only ack it with a zero size thus we at most have to deal
5325 * with this once. See RFC7541#4.2 and #6.3 for the spec, and
5326 * below for the whole context and interoperability risks:
5327 * https://lists.w3.org/Archives/Public/ietf-http-wg/2021OctDec/0235.html
5328 */
5329 if (b_room(&outbuf) < 1)
5330 goto full;
5331 outbuf.area[outbuf.data++] = 0x20; // HPACK DTSU 0 bytes
5332
5333 /* let's not update the flags now but only once the buffer is
5334 * really committed.
5335 */
5336 }
5337
Willy Tarreau115e83b2018-12-01 19:17:53 +01005338 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01005339 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005340 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005341 goto realign_again;
5342 goto full;
5343 }
5344
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005345 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED) {
5346 char sts[4];
5347
5348 h2_trace_header(ist(":status"), ist(ultoa_r(h2s->status, sts, sizeof(sts))),
5349 H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__,
5350 h2c, h2s);
5351 }
5352
Willy Tarreau115e83b2018-12-01 19:17:53 +01005353 /* encode all headers, stop at empty name */
5354 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
5355 /* these ones do not exist in H2 and must be dropped. */
5356 if (isteq(list[hdr].n, ist("connection")) ||
5357 isteq(list[hdr].n, ist("proxy-connection")) ||
5358 isteq(list[hdr].n, ist("keep-alive")) ||
5359 isteq(list[hdr].n, ist("upgrade")) ||
5360 isteq(list[hdr].n, ist("transfer-encoding")))
5361 continue;
5362
Christopher Faulet86d144c2019-08-14 16:32:25 +02005363 /* Skip all pseudo-headers */
5364 if (*(list[hdr].n.ptr) == ':')
5365 continue;
5366
Willy Tarreau115e83b2018-12-01 19:17:53 +01005367 if (isteq(list[hdr].n, ist("")))
5368 break; // end
5369
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005370 if (!h2_encode_header(&outbuf, list[hdr].n, list[hdr].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5371 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005372 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005373 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005374 goto realign_again;
5375 goto full;
5376 }
5377 }
5378
Willy Tarreaucb985a42019-10-07 16:56:34 +02005379 /* update the frame's size */
5380 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5381
5382 if (outbuf.data > h2c->mfs + 9) {
5383 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5384 /* output full */
5385 if (b_space_wraps(mbuf))
5386 goto realign_again;
5387 goto full;
5388 }
5389 }
5390
Willy Tarreau3a537072021-06-17 08:40:04 +02005391 TRACE_USER("sent H2 response ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5392
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005393 /* remove all header blocks including the EOH and compute the
5394 * corresponding size.
Willy Tarreau115e83b2018-12-01 19:17:53 +01005395 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005396 ret = 0;
5397 blk = htx_get_head_blk(htx);
5398 while (blk) {
5399 type = htx_get_blk_type(blk);
5400 ret += htx_get_blksz(blk);
5401 blk = htx_remove_blk(htx, blk);
5402 /* The removed block is the EOH */
5403 if (type == HTX_BLK_EOH)
5404 break;
Christopher Faulet5be651d2021-01-22 15:28:03 +01005405 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005406
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005407 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005408 /* Response already closed: add END_STREAM */
5409 es_now = 1;
5410 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005411 else if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && h2s->status >= 200) {
5412 /* EOM+empty: we may need to add END_STREAM except for 1xx
Christopher Faulet991febd2020-12-02 15:17:31 +01005413 * responses and tunneled response.
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005414 */
Christopher Faulet991febd2020-12-02 15:17:31 +01005415 if (!(h2s->flags & H2_SF_BODY_TUNNEL) || h2s->status >= 300)
5416 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005417 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005418
Willy Tarreau115e83b2018-12-01 19:17:53 +01005419 if (es_now)
5420 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5421
5422 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005423 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02005424 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Faulet0b465482019-02-19 15:14:23 +01005425
5426 /* indicates the HEADERS frame was sent, except for 1xx responses. For
5427 * 1xx responses, another HEADERS frame is expected.
5428 */
Christopher Faulet89899422020-12-07 18:24:43 +01005429 if (h2s->status >= 200)
Christopher Faulet0b465482019-02-19 15:14:23 +01005430 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005431
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005432 if (h2c->flags & H2_CF_SHTS_UPDATED) {
5433 /* was sent above */
5434 h2c->flags |= H2_CF_DTSU_EMITTED;
Willy Tarreauc7d85482022-02-16 14:28:14 +01005435 h2c->flags &= ~H2_CF_SHTS_UPDATED;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005436 }
5437
Willy Tarreau115e83b2018-12-01 19:17:53 +01005438 if (es_now) {
5439 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005440 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 +01005441 if (h2s->st == H2_SS_OPEN)
5442 h2s->st = H2_SS_HLOC;
5443 else
5444 h2s_close(h2s);
5445 }
5446
5447 /* OK we could properly deliver the response */
Willy Tarreau115e83b2018-12-01 19:17:53 +01005448 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005449 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005450 return ret;
5451 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005452 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5453 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005454 h2c->flags |= H2_CF_MUX_MFULL;
5455 h2s->flags |= H2_SF_BLK_MROOM;
5456 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005457 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 +01005458 goto end;
5459 fail:
5460 /* unparsable HTX messages, too large ones to be produced in the local
5461 * list etc go here (unrecoverable errors).
5462 */
5463 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5464 ret = 0;
5465 goto end;
5466}
5467
Willy Tarreau80739692018-10-05 11:35:57 +02005468/* Try to send a HEADERS frame matching HTX request present in HTX message
5469 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5470 * must check the stream's status to detect any error which might have happened
5471 * subsequently to a successful send. The htx blocks are automatically removed
5472 * from the message. The htx message is assumed to be valid since produced from
5473 * the internal code, hence it contains a start line, an optional series of
5474 * header blocks and an end of header, otherwise an invalid frame could be
5475 * emitted and the resulting htx message could be left in an inconsistent state.
5476 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005477static size_t h2s_snd_bhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02005478{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005479 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02005480 struct h2c *h2c = h2s->h2c;
5481 struct htx_blk *blk;
Willy Tarreau80739692018-10-05 11:35:57 +02005482 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005483 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02005484 struct htx_sl *sl;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005485 struct ist meth, uri, auth, host = IST_NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005486 enum htx_blk_type type;
5487 int es_now = 0;
5488 int ret = 0;
5489 int hdr;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005490 int extended_connect = 0;
Willy Tarreau80739692018-10-05 11:35:57 +02005491
Willy Tarreau7838a792019-08-12 18:42:03 +02005492 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5493
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005494 /* get the start line (we do have one) and the rest of the headers,
5495 * that we dump starting at header 0 */
5496 sl = NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005497 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005498 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005499 type = htx_get_blk_type(blk);
5500
5501 if (type == HTX_BLK_UNUSED)
5502 continue;
5503
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005504 if (type == HTX_BLK_EOH)
Willy Tarreau80739692018-10-05 11:35:57 +02005505 break;
5506
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005507 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005508 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005509 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5510 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5511 goto fail;
5512 }
Willy Tarreau80739692018-10-05 11:35:57 +02005513
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005514 list[hdr].n = htx_get_blk_name(htx, blk);
5515 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02005516
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005517 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005518 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name) &&
5519 isteq(list[hdr].n, h2c->proxy->server_id_hdr_name))
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005520 continue;
Christopher Faulet67d58092019-10-02 10:51:38 +02005521
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005522 /* Convert connection: upgrade to Extended connect from rfc 8441 */
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005523 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteqi(list[hdr].n, ist("connection"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005524 /* rfc 7230 #6.1 Connection = list of tokens */
5525 struct ist connection_ist = list[hdr].v;
5526 do {
5527 if (isteqi(iststop(connection_ist, ','),
5528 ist("upgrade"))) {
Amaury Denoyelle0df04362021-10-18 09:43:29 +02005529 if (!(h2c->flags & H2_CF_RCVD_RFC8441)) {
5530 TRACE_STATE("reject upgrade because of no RFC8441 support", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5531 goto fail;
5532 }
5533
Amaury Denoyellee0c258c2021-10-18 10:05:16 +02005534 TRACE_STATE("convert upgrade to extended connect method", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005535 h2s->flags |= (H2_SF_BODY_TUNNEL|H2_SF_EXT_CONNECT_SENT);
5536 sl->info.req.meth = HTTP_METH_CONNECT;
5537 meth = ist("CONNECT");
5538
5539 extended_connect = 1;
5540 break;
5541 }
5542
5543 connection_ist = istadv(istfind(connection_ist, ','), 1);
5544 } while (istlen(connection_ist));
5545 }
5546
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005547 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteq(list[hdr].n, ist("upgrade"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005548 /* rfc 7230 #6.7 Upgrade = list of protocols
5549 * rfc 8441 #4 Extended connect = :protocol is single-valued
5550 *
5551 * only first HTTP/1 protocol is preserved
5552 */
5553 const struct ist protocol = iststop(list[hdr].v, ',');
5554 /* upgrade_protocol field is 16 bytes long in h2s */
5555 istpad(h2s->upgrade_protocol, isttrim(protocol, 15));
5556 }
5557
5558 if (isteq(list[hdr].n, ist("host")))
5559 host = list[hdr].v;
5560
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005561 hdr++;
5562 }
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005563 else if (type == HTX_BLK_REQ_SL) {
5564 BUG_ON(sl); /* Only one start-line expected */
5565 sl = htx_get_blk_ptr(htx, blk);
5566 meth = htx_sl_req_meth(sl);
5567 uri = htx_sl_req_uri(sl);
5568 if (sl->info.req.meth == HTTP_METH_HEAD)
5569 h2s->flags |= H2_SF_BODYLESS_RESP;
5570 if (unlikely(uri.len == 0)) {
5571 TRACE_ERROR("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5572 goto fail;
5573 }
5574 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005575 else {
5576 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5577 goto fail;
5578 }
Willy Tarreau80739692018-10-05 11:35:57 +02005579 }
5580
Christopher Faulet56498132021-01-29 11:39:43 +01005581 /* The start-line me be defined */
5582 BUG_ON(!sl);
5583
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005584 /* Now add the server name to a header (if requested) */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005585 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005586 struct server *srv = objt_server(h2c->conn->target);
5587
5588 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005589 list[hdr].n = h2c->proxy->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005590 list[hdr].v = ist(srv->id);
5591 hdr++;
5592 }
5593 }
5594
Willy Tarreau80739692018-10-05 11:35:57 +02005595 /* marker for end of headers */
5596 list[hdr].n = ist("");
5597
Willy Tarreau9c218e72019-05-26 10:08:28 +02005598 mbuf = br_tail(h2c->mbuf);
5599 retry:
5600 if (!h2_get_buf(h2c, mbuf)) {
5601 h2c->flags |= H2_CF_MUX_MALLOC;
5602 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005603 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 +02005604 return 0;
5605 }
5606
Willy Tarreau80739692018-10-05 11:35:57 +02005607 chunk_reset(&outbuf);
5608
5609 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005610 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5611 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005612 break;
5613 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005614 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02005615 }
5616
5617 if (outbuf.size < 9)
5618 goto full;
5619
5620 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5621 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5622 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5623 outbuf.data = 9;
5624
5625 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01005626 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005627 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005628 goto realign_again;
5629 goto full;
5630 }
5631
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005632 h2_trace_header(ist(":method"), meth, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5633
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005634 auth = ist(NULL);
5635
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005636 /* RFC7540 #8.3: the CONNECT method must have :
5637 * - :authority set to the URI part (host:port)
5638 * - :method set to CONNECT
5639 * - :scheme and :path omitted
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005640 *
5641 * Note that this is not applicable in case of the Extended CONNECT
5642 * protocol from rfc 8441.
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005643 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005644 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT) && !extended_connect) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005645 auth = uri;
5646
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005647 if (!h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5648 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005649 /* output full */
5650 if (b_space_wraps(mbuf))
5651 goto realign_again;
5652 goto full;
5653 }
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005654
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005655 h2s->flags |= H2_SF_BODY_TUNNEL;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005656 } else {
5657 /* other methods need a :scheme. If an authority is known from
5658 * the request line, it must be sent, otherwise only host is
5659 * sent. Host is never sent as the authority.
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005660 *
5661 * This code is also applicable for Extended CONNECT protocol
5662 * from rfc 8441.
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005663 */
5664 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02005665
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005666 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
5667 /* the URI seems to start with a scheme */
5668 int len = 1;
5669
5670 while (len < uri.len && uri.ptr[len] != ':')
5671 len++;
5672
5673 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5674 /* make the uri start at the authority now */
Tim Duesterhus9f75ed12021-03-02 18:57:26 +01005675 scheme = ist2(uri.ptr, len);
Tim Duesterhus154374c2021-03-02 18:57:27 +01005676 uri = istadv(uri, len + 3);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005677
5678 /* find the auth part of the URI */
Tim Duesterhus92c696e2021-02-28 16:11:36 +01005679 auth = ist2(uri.ptr, 0);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005680 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5681 auth.len++;
5682
Tim Duesterhus154374c2021-03-02 18:57:27 +01005683 uri = istadv(uri, auth.len);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005684 }
5685 }
5686
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005687 /* For Extended CONNECT, the :authority must be present.
5688 * Use host value for it.
5689 */
5690 if (unlikely(extended_connect) && isttest(host))
5691 auth = host;
5692
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005693 if (!scheme.len) {
5694 /* no explicit scheme, we're using an origin-form URI,
5695 * probably from an H1 request transcoded to H2 via an
5696 * external layer, then received as H2 without authority.
5697 * So we have to look up the scheme from the HTX flags.
5698 * In such a case only http and https are possible, and
5699 * https is the default (sent by browsers).
5700 */
5701 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5702 scheme = ist("http");
5703 else
5704 scheme = ist("https");
5705 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005706
5707 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005708 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005709 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005710 goto realign_again;
5711 goto full;
5712 }
Willy Tarreau80739692018-10-05 11:35:57 +02005713
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005714 if (auth.len &&
5715 !h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5716 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005717 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005718 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005719 goto realign_again;
5720 goto full;
5721 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005722
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005723 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5724 * be sent as '/' or '*'.
5725 */
5726 if (unlikely(!uri.len)) {
5727 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5728 uri = ist("*");
5729 else
5730 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005731 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005732
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005733 if (!hpack_encode_path(&outbuf, uri)) {
5734 /* output full */
5735 if (b_space_wraps(mbuf))
5736 goto realign_again;
5737 goto full;
5738 }
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005739
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005740 h2_trace_header(ist(":path"), uri, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5741
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005742 /* encode the pseudo-header protocol from rfc8441 if using
5743 * Extended CONNECT method.
5744 */
5745 if (unlikely(extended_connect)) {
5746 const struct ist protocol = ist(h2s->upgrade_protocol);
5747 if (isttest(protocol)) {
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005748 if (!h2_encode_header(&outbuf, ist(":protocol"), protocol, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5749 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005750 /* output full */
5751 if (b_space_wraps(mbuf))
5752 goto realign_again;
5753 goto full;
5754 }
5755 }
5756 }
Willy Tarreau80739692018-10-05 11:35:57 +02005757 }
5758
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005759 /* encode all headers, stop at empty name. Host is only sent if we
5760 * do not provide an authority.
5761 */
Willy Tarreau80739692018-10-05 11:35:57 +02005762 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005763 struct ist n = list[hdr].n;
5764 struct ist v = list[hdr].v;
5765
Willy Tarreau80739692018-10-05 11:35:57 +02005766 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005767 if (isteq(n, ist("connection")) ||
5768 (auth.len && isteq(n, ist("host"))) ||
5769 isteq(n, ist("proxy-connection")) ||
5770 isteq(n, ist("keep-alive")) ||
5771 isteq(n, ist("upgrade")) ||
5772 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005773 continue;
5774
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005775 if (isteq(n, ist("te"))) {
5776 /* "te" may only be sent with "trailers" if this value
5777 * is present, otherwise it must be deleted.
5778 */
5779 v = istist(v, ist("trailers"));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01005780 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005781 continue;
5782 v = ist("trailers");
5783 }
5784
Christopher Faulet86d144c2019-08-14 16:32:25 +02005785 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005786 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005787 continue;
5788
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005789 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005790 break; // end
5791
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005792 if (!h2_encode_header(&outbuf, n, v, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005793 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005794 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005795 goto realign_again;
5796 goto full;
5797 }
5798 }
5799
Willy Tarreaucb985a42019-10-07 16:56:34 +02005800 /* update the frame's size */
5801 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5802
5803 if (outbuf.data > h2c->mfs + 9) {
5804 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5805 /* output full */
5806 if (b_space_wraps(mbuf))
5807 goto realign_again;
5808 goto full;
5809 }
5810 }
5811
Willy Tarreau3a537072021-06-17 08:40:04 +02005812 TRACE_USER("sent H2 request ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5813
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005814 /* remove all header blocks including the EOH and compute the
5815 * corresponding size.
Willy Tarreau80739692018-10-05 11:35:57 +02005816 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005817 ret = 0;
5818 blk = htx_get_head_blk(htx);
5819 while (blk) {
5820 type = htx_get_blk_type(blk);
5821 ret += htx_get_blksz(blk);
5822 blk = htx_remove_blk(htx, blk);
5823 /* The removed block is the EOH */
5824 if (type == HTX_BLK_EOH)
5825 break;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005826 }
Willy Tarreau80739692018-10-05 11:35:57 +02005827
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005828 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005829 /* Request already closed: add END_STREAM */
Willy Tarreau80739692018-10-05 11:35:57 +02005830 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005831 }
5832 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5833 /* EOM+empty: we may need to add END_STREAM (except for CONNECT
5834 * request)
5835 */
5836 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5837 es_now = 1;
5838 }
Willy Tarreau80739692018-10-05 11:35:57 +02005839
Willy Tarreau80739692018-10-05 11:35:57 +02005840 if (es_now)
5841 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5842
5843 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005844 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02005845 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau80739692018-10-05 11:35:57 +02005846 h2s->flags |= H2_SF_HEADERS_SENT;
5847 h2s->st = H2_SS_OPEN;
5848
Willy Tarreau80739692018-10-05 11:35:57 +02005849 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005850 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 +02005851 // trim any possibly pending data (eg: inconsistent content-length)
5852 h2s->flags |= H2_SF_ES_SENT;
5853 h2s->st = H2_SS_HLOC;
5854 }
5855
Willy Tarreau80739692018-10-05 11:35:57 +02005856 end:
5857 return ret;
5858 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005859 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5860 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02005861 h2c->flags |= H2_CF_MUX_MFULL;
5862 h2s->flags |= H2_SF_BLK_MROOM;
5863 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005864 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 +02005865 goto end;
5866 fail:
5867 /* unparsable HTX messages, too large ones to be produced in the local
5868 * list etc go here (unrecoverable errors).
5869 */
5870 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5871 ret = 0;
5872 goto end;
5873}
5874
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005875/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005876 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5877 * caller must check the stream's status to detect any error which might have
5878 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005879 * consumed, or zero if nothing done.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005880 */
Christopher Faulet142854b2020-12-02 15:12:40 +01005881static size_t h2s_make_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005882{
5883 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005884 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005885 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005886 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005887 size_t total = 0;
5888 int es_now = 0;
5889 int bsize; /* htx block size */
5890 int fsize; /* h2 frame size */
5891 struct htx_blk *blk;
5892 enum htx_blk_type type;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005893 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005894
Willy Tarreau7838a792019-08-12 18:42:03 +02005895 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5896
Willy Tarreau98de12a2018-12-12 07:03:00 +01005897 htx = htx_from_buf(buf);
5898
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005899 /* We only come here with HTX_BLK_DATA blocks */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005900
5901 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005902 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005903 goto end;
5904
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005905 if ((h2c->flags & H2_CF_IS_BACK) &&
Christopher Fauletf95f8762021-01-22 11:59:07 +01005906 (h2s->flags & (H2_SF_HEADERS_RCVD|H2_SF_BODY_TUNNEL)) == H2_SF_BODY_TUNNEL) {
5907 /* The response HEADERS frame not received yet. Thus the tunnel
5908 * is not fully established yet. In this situation, we block
5909 * data sending.
5910 */
5911 h2s->flags |= H2_SF_BLK_MBUSY;
5912 TRACE_STATE("Request DATA frame blocked waiting for tunnel establishment", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5913 goto end;
5914 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01005915 else if ((h2c->flags & H2_CF_IS_BACK) && (h2s->flags & H2_SF_TUNNEL_ABRT)) {
5916 /* a tunnel attempt was aborted but the is pending raw data to xfer to the server.
5917 * Thus the stream is closed with the CANCEL error. The error will be reported to
5918 * the upper layer as aserver abort. But at this stage there is nothing more we can
5919 * do. We just wait for the end of the response to be sure to not truncate it.
5920 */
5921 if (!(h2s->flags & H2_SF_ES_RCVD)) {
5922 TRACE_STATE("Request DATA frame blocked waiting end of aborted tunnel", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5923 h2s->flags |= H2_SF_BLK_MBUSY;
5924 }
5925 else {
5926 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5927 h2s_error(h2s, H2_ERR_CANCEL);
5928 }
5929 goto end;
5930 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01005931
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005932 blk = htx_get_head_blk(htx);
5933 type = htx_get_blk_type(blk);
5934 bsize = htx_get_blksz(blk);
5935 fsize = bsize;
5936 trunc_out = 0;
5937 if (type != HTX_BLK_DATA)
5938 goto end;
5939
Willy Tarreau9c218e72019-05-26 10:08:28 +02005940 mbuf = br_tail(h2c->mbuf);
5941 retry:
5942 if (!h2_get_buf(h2c, mbuf)) {
5943 h2c->flags |= H2_CF_MUX_MALLOC;
5944 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005945 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 +02005946 goto end;
5947 }
5948
Willy Tarreau98de12a2018-12-12 07:03:00 +01005949 /* Perform some optimizations to reduce the number of buffer copies.
5950 * First, if the mux's buffer is empty and the htx area contains
5951 * exactly one data block of the same size as the requested count, and
5952 * this count fits within the frame size, the stream's window size, and
5953 * the connection's window size, then it's possible to simply swap the
5954 * caller's buffer with the mux's output buffer and adjust offsets and
5955 * length to match the entire DATA HTX block in the middle. In this
5956 * case we perform a true zero-copy operation from end-to-end. This is
5957 * the situation that happens all the time with large files. Second, if
5958 * this is not possible, but the mux's output buffer is empty, we still
5959 * have an opportunity to avoid the copy to the intermediary buffer, by
5960 * making the intermediary buffer's area point to the output buffer's
5961 * area. In this case we want to skip the HTX header to make sure that
5962 * copies remain aligned and that this operation remains possible all
5963 * the time. This goes for headers, data blocks and any data extracted
5964 * from the HTX blocks.
5965 */
5966 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02005967 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005968 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005969 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005970
Willy Tarreaubcc45952019-05-26 10:05:50 +02005971 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005972 /* Too bad there are data left there. We're willing to memcpy/memmove
5973 * up to 1/4 of the buffer, which means that it's OK to copy a large
5974 * frame into a buffer containing few data if it needs to be realigned,
5975 * and that it's also OK to copy few data without realigning. Otherwise
5976 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005977 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005978 if (fsize + 9 <= b_room(mbuf) &&
5979 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005980 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
5981 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 +01005982 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02005983 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005984
Willy Tarreau9c218e72019-05-26 10:08:28 +02005985 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5986 goto retry;
5987
Willy Tarreau98de12a2018-12-12 07:03:00 +01005988 h2c->flags |= H2_CF_MUX_MFULL;
5989 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005990 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 +01005991 goto end;
5992 }
5993
Christopher Faulet925abdf2021-04-27 22:51:07 +02005994 if (htx->flags & HTX_FL_EOM) {
5995 /* EOM+empty: we may need to add END_STREAM (except for tunneled
5996 * message)
5997 */
5998 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5999 es_now = 1;
6000 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01006001 /* map an H2 frame to the HTX block so that we can put the
6002 * frame header there.
6003 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006004 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
6005 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01006006
6007 /* prepend an H2 DATA frame header just before the DATA block */
6008 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6009 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
Christopher Faulet925abdf2021-04-27 22:51:07 +02006010 if (es_now)
6011 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006012 h2_set_frame_size(outbuf.area, fsize);
6013
6014 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006015 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006016 h2c->mws -= fsize;
6017
6018 /* and exchange with our old area */
6019 buf->area = old_area;
6020 buf->data = buf->head = 0;
6021 total += fsize;
Christopher Faulet925abdf2021-04-27 22:51:07 +02006022 fsize = 0;
Willy Tarreau936db562023-10-18 11:39:43 +02006023 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau7838a792019-08-12 18:42:03 +02006024
6025 TRACE_PROTO("sent H2 DATA frame (zero-copy)", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Christopher Faulet925abdf2021-04-27 22:51:07 +02006026 goto out;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006027 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01006028
Willy Tarreau98de12a2018-12-12 07:03:00 +01006029 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006030 /* for DATA and EOM we'll have to emit a frame, even if empty */
6031
6032 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006033 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6034 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006035 break;
6036 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006037 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006038 }
6039
6040 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02006041 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6042 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006043 h2c->flags |= H2_CF_MUX_MFULL;
6044 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006045 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006046 goto end;
6047 }
6048
6049 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
6050 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6051 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6052 outbuf.data = 9;
6053
6054 /* we have in <fsize> the exact number of bytes we need to copy from
6055 * the HTX buffer. We need to check this against the connection's and
6056 * the stream's send windows, and to ensure that this fits in the max
6057 * frame size and in the buffer's available space minus 9 bytes (for
6058 * the frame header). The connection's flow control is applied last so
6059 * that we can use a separate list of streams which are immediately
6060 * unblocked on window opening. Note: we don't implement padding.
6061 */
6062
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006063 if (!fsize)
6064 goto send_empty;
6065
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006066 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006067 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreau2b718102021-04-21 07:32:39 +02006068 if (LIST_INLIST(&h2s->list))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006069 h2_remove_from_list(h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006070 LIST_APPEND(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02006071 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 +01006072 goto end;
6073 }
6074
Willy Tarreauee573762018-12-04 15:25:57 +01006075 if (fsize > count)
6076 fsize = count;
6077
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006078 if (fsize > h2s_mws(h2s))
6079 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006080
6081 if (h2c->mfs && fsize > h2c->mfs)
6082 fsize = h2c->mfs; // >0
6083
6084 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02006085 /* It doesn't fit at once. If it at least fits once split and
6086 * the amount of data to move is low, let's defragment the
6087 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006088 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006089 if (b_space_wraps(mbuf) &&
6090 (fsize + 9 <= b_room(mbuf)) &&
6091 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006092 goto realign_again;
6093 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01006094 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006095
6096 if (fsize <= 0) {
6097 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02006098 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6099 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006100 h2c->flags |= H2_CF_MUX_MFULL;
6101 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006102 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006103 goto end;
6104 }
6105 }
6106
6107 if (h2c->mws <= 0) {
6108 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02006109 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 +01006110 goto end;
6111 }
6112
6113 if (fsize > h2c->mws)
6114 fsize = h2c->mws;
6115
6116 /* now let's copy this this into the output buffer */
6117 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006118 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01006119 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01006120 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006121
6122 send_empty:
6123 /* update the frame's size */
6124 h2_set_frame_size(outbuf.area, fsize);
6125
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006126 /* consume incoming HTX block */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006127 total += fsize;
6128 if (fsize == bsize) {
6129 htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006130 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
6131 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6132 * message)
6133 */
6134 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6135 es_now = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02006136 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006137 }
6138 else {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006139 /* we've truncated this block */
6140 htx_cut_data_blk(htx, blk, fsize);
6141 }
6142
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006143 if (es_now)
6144 outbuf.area[4] |= H2_F_DATA_END_STREAM;
6145
6146 /* commit the H2 response */
6147 b_add(mbuf, fsize + 9);
Willy Tarreau936db562023-10-18 11:39:43 +02006148 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006149
Christopher Faulet925abdf2021-04-27 22:51:07 +02006150 out:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006151 if (es_now) {
6152 if (h2s->st == H2_SS_OPEN)
6153 h2s->st = H2_SS_HLOC;
6154 else
6155 h2s_close(h2s);
6156
6157 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02006158 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 +01006159 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006160 else if (fsize) {
6161 if (fsize == bsize) {
6162 TRACE_DEVEL("more data may be available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6163 goto new_frame;
6164 }
6165 else if (trunc_out) {
6166 /* we've truncated this block */
6167 goto new_frame;
6168 }
6169 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006170
6171 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006172 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006173 return total;
6174}
6175
Christopher Faulet991febd2020-12-02 15:17:31 +01006176/* Skip the message payload (DATA blocks) and emit an empty DATA frame with the
6177 * ES flag set for stream <h2s>. This function is called for response known to
6178 * have no payload. Only DATA blocks are skipped. This means the trailers are
Ilya Shipitsinacf84592021-02-06 22:29:08 +05006179 * still emitted. The caller must check the stream's status to detect any error
Christopher Faulet991febd2020-12-02 15:17:31 +01006180 * which might have happened subsequently to a successful send. Returns the
6181 * number of data bytes consumed, or zero if nothing done.
6182 */
6183static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count)
6184{
6185 struct h2c *h2c = h2s->h2c;
6186 struct htx *htx;
6187 int bsize; /* htx block size */
6188 int fsize; /* h2 frame size */
6189 struct htx_blk *blk;
6190 enum htx_blk_type type;
6191 size_t total = 0;
6192
6193 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6194
Christopher Faulet991febd2020-12-02 15:17:31 +01006195 htx = htx_from_buf(buf);
6196
6197 next_data:
6198 if (!count || htx_is_empty(htx))
6199 goto end;
6200 blk = htx_get_head_blk(htx);
6201 type = htx_get_blk_type(blk);
6202 bsize = htx_get_blksz(blk);
6203 fsize = bsize;
6204 if (type != HTX_BLK_DATA)
6205 goto end;
6206
6207 if (fsize > count)
6208 fsize = count;
6209
6210 if (fsize != bsize)
6211 goto skip_data;
6212
6213 if (!(htx->flags & HTX_FL_EOM) || !htx_is_unique_blk(htx, blk))
6214 goto skip_data;
6215
6216 /* Here, it is the last block and it is also the end of the message. So
6217 * we can emit an empty DATA frame with the ES flag set
6218 */
6219 if (h2_send_empty_data_es(h2s) <= 0)
6220 goto end;
6221
6222 if (h2s->st == H2_SS_OPEN)
6223 h2s->st = H2_SS_HLOC;
6224 else
6225 h2s_close(h2s);
6226
6227 skip_data:
6228 /* consume incoming HTX block */
6229 total += fsize;
6230 if (fsize == bsize) {
6231 TRACE_DEVEL("more data may be available, trying to skip another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6232 htx_remove_blk(htx, blk);
6233 goto next_data;
6234 }
6235 else {
6236 /* we've truncated this block */
6237 htx_cut_data_blk(htx, blk, fsize);
6238 }
6239
6240 end:
6241 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6242 return total;
6243}
6244
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006245/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
6246 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
6247 * processed. The caller must check the stream's status to detect any error
6248 * which might have happened subsequently to a successful send. The htx blocks
6249 * are automatically removed from the message. The htx message is assumed to be
6250 * valid since produced from the internal code. Processing stops when meeting
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006251 * the EOT, which *is* removed. All trailers are processed at once and sent as a
6252 * single frame. The ES flag is always set.
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006253 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006254static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006255{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02006256 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006257 struct h2c *h2c = h2s->h2c;
6258 struct htx_blk *blk;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006259 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006260 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006261 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006262 int ret = 0;
6263 int hdr;
6264 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006265
Willy Tarreau7838a792019-08-12 18:42:03 +02006266 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
6267
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006268 /* get trailers. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006269 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006270 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006271 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006272
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006273 if (type == HTX_BLK_UNUSED)
6274 continue;
6275
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006276 if (type == HTX_BLK_EOT)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006277 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006278 if (type == HTX_BLK_TLR) {
6279 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
6280 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
6281 goto fail;
6282 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006283
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006284 list[hdr].n = htx_get_blk_name(htx, blk);
6285 list[hdr].v = htx_get_blk_value(htx, blk);
6286 hdr++;
6287 }
6288 else {
6289 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006290 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02006291 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006292 }
6293
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006294 /* marker for end of trailers */
6295 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006296
Willy Tarreau9c218e72019-05-26 10:08:28 +02006297 mbuf = br_tail(h2c->mbuf);
6298 retry:
6299 if (!h2_get_buf(h2c, mbuf)) {
6300 h2c->flags |= H2_CF_MUX_MALLOC;
6301 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006302 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 +02006303 goto end;
6304 }
6305
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006306 chunk_reset(&outbuf);
6307
6308 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006309 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6310 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006311 break;
6312 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006313 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006314 }
6315
6316 if (outbuf.size < 9)
6317 goto full;
6318
6319 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
6320 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
6321 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6322 outbuf.data = 9;
6323
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006324 /* encode all headers */
6325 for (idx = 0; idx < hdr; idx++) {
6326 /* these ones do not exist in H2 or must not appear in
6327 * trailers and must be dropped.
6328 */
6329 if (isteq(list[idx].n, ist("host")) ||
6330 isteq(list[idx].n, ist("content-length")) ||
6331 isteq(list[idx].n, ist("connection")) ||
6332 isteq(list[idx].n, ist("proxy-connection")) ||
6333 isteq(list[idx].n, ist("keep-alive")) ||
6334 isteq(list[idx].n, ist("upgrade")) ||
6335 isteq(list[idx].n, ist("te")) ||
6336 isteq(list[idx].n, ist("transfer-encoding")))
6337 continue;
6338
Christopher Faulet86d144c2019-08-14 16:32:25 +02006339 /* Skip all pseudo-headers */
6340 if (*(list[idx].n.ptr) == ':')
6341 continue;
6342
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01006343 if (!h2_encode_header(&outbuf, list[idx].n, list[idx].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
6344 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006345 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006346 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006347 goto realign_again;
6348 goto full;
6349 }
6350 }
6351
Willy Tarreau5121e5d2019-05-06 15:13:41 +02006352 if (outbuf.data == 9) {
6353 /* here we have a problem, we have nothing to emit (either we
6354 * received an empty trailers block followed or we removed its
6355 * contents above). Because of this we can't send a HEADERS
6356 * frame, so we have to cheat and instead send an empty DATA
6357 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01006358 */
6359 outbuf.area[3] = H2_FT_DATA;
6360 outbuf.area[4] = H2_F_DATA_END_STREAM;
6361 }
6362
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006363 /* update the frame's size */
6364 h2_set_frame_size(outbuf.area, outbuf.data - 9);
6365
Willy Tarreau572d9f52019-10-11 16:58:37 +02006366 if (outbuf.data > h2c->mfs + 9) {
6367 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
6368 /* output full */
6369 if (b_space_wraps(mbuf))
6370 goto realign_again;
6371 goto full;
6372 }
6373 }
6374
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006375 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02006376 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 +02006377 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02006378 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006379 h2s->flags |= H2_SF_ES_SENT;
6380
6381 if (h2s->st == H2_SS_OPEN)
6382 h2s->st = H2_SS_HLOC;
6383 else
6384 h2s_close(h2s);
6385
6386 /* OK we could properly deliver the response */
6387 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02006388 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006389 ret = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006390 blk = htx_get_head_blk(htx);
6391 while (blk) {
6392 type = htx_get_blk_type(blk);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006393 ret += htx_get_blksz(blk);
6394 blk = htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006395 /* The removed block is the EOT */
6396 if (type == HTX_BLK_EOT)
6397 break;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006398 }
6399
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006400 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006401 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006402 return ret;
6403 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006404 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6405 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006406 h2c->flags |= H2_CF_MUX_MFULL;
6407 h2s->flags |= H2_SF_BLK_MROOM;
6408 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006409 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 +01006410 goto end;
6411 fail:
6412 /* unparsable HTX messages, too large ones to be produced in the local
6413 * list etc go here (unrecoverable errors).
6414 */
6415 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6416 ret = 0;
6417 goto end;
6418}
6419
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006420/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6421 * event subscriber <es> is not allowed to change from a previous call as long
6422 * as at least one event is still subscribed. The <event_type> must only be a
6423 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006424 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006425static int h2_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02006426{
Willy Tarreau36c22322022-05-27 10:41:24 +02006427 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02006428 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006429
Willy Tarreau7838a792019-08-12 18:42:03 +02006430 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006431
6432 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006433 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006434
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006435 es->events |= event_type;
6436 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006437
6438 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006439 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006440
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006441 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006442 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02006443 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02006444 !LIST_INLIST(&h2s->list)) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006445 if (h2s->flags & H2_SF_BLK_MFCTL) {
6446 TRACE_DEVEL("Adding to fctl list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006447 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006448 }
6449 else {
6450 TRACE_DEVEL("Adding to send list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006451 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006452 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02006453 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02006454 }
Willy Tarreau7838a792019-08-12 18:42:03 +02006455 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006456 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006457}
6458
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006459/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6460 * The <es> pointer is not allowed to differ from the one passed to the
6461 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006462 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006463static int h2_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006464{
Willy Tarreau36c22322022-05-27 10:41:24 +02006465 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006466
Willy Tarreau7838a792019-08-12 18:42:03 +02006467 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006468
6469 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006470 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006471
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006472 es->events &= ~event_type;
6473 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01006474 h2s->subs = NULL;
6475
6476 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006477 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006478
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006479 if (event_type & SUB_RETRY_SEND) {
Frédéric Lécaille67fda162022-06-30 12:01:54 +02006480 TRACE_DEVEL("unsubscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006481 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006482 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006483 h2_remove_from_list(h2s);
Olivier Houchardd846c262018-10-19 17:24:29 +02006484 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01006485
Willy Tarreau7838a792019-08-12 18:42:03 +02006486 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006487 return 0;
6488}
6489
6490
Christopher Faulet564e39c2021-09-21 15:50:55 +02006491/* Called from the upper layer, to receive data
6492 *
6493 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
6494 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
6495 * means the caller wants to flush input data (from the mux buffer and the
6496 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
6497 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
6498 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
6499 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
6500 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
6501 * copy as much data as possible.
6502 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006503static size_t h2_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Olivier Houchard511efea2018-08-16 15:30:32 +02006504{
Willy Tarreau36c22322022-05-27 10:41:24 +02006505 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau082f5592018-11-25 08:03:32 +01006506 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01006507 struct htx *h2s_htx = NULL;
6508 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02006509 size_t ret = 0;
6510
Willy Tarreau7838a792019-08-12 18:42:03 +02006511 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
6512
Olivier Houchard511efea2018-08-16 15:30:32 +02006513 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006514 h2s_htx = htx_from_buf(&h2s->rxbuf);
Christopher Fauletec361bb2022-02-21 15:12:54 +01006515 if (htx_is_empty(h2s_htx) && !(h2s_htx->flags & HTX_FL_PARSING_ERROR)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02006516 /* Here htx_to_buf() will set buffer data to 0 because
6517 * the HTX is empty.
6518 */
6519 htx_to_buf(h2s_htx, &h2s->rxbuf);
6520 goto end;
6521 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006522 ret = h2s_htx->data;
6523 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01006524
Christopher Faulet9b79a102019-07-15 11:22:56 +02006525 /* <buf> is empty and the message is small enough, swap the
6526 * buffers. */
6527 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01006528 htx_to_buf(buf_htx, buf);
6529 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006530 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
6531 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01006532 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006533
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006534 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_UNUSED);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006535
6536 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
6537 buf_htx->flags |= HTX_FL_PARSING_ERROR;
6538 if (htx_is_empty(buf_htx))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006539 se_fl_set(h2s->sd, SE_FL_EOI);
Willy Tarreau86724e22018-12-01 23:19:43 +01006540 }
Christopher Faulet72722c02023-02-23 14:26:34 +01006541 else if (htx_is_empty(h2s_htx)) {
Christopher Faulet42432f32020-11-20 17:43:16 +01006542 buf_htx->flags |= (h2s_htx->flags & HTX_FL_EOM);
Christopher Faulet72722c02023-02-23 14:26:34 +01006543 }
6544
Christopher Faulet9b79a102019-07-15 11:22:56 +02006545 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
6546 htx_to_buf(buf_htx, buf);
6547 htx_to_buf(h2s_htx, &h2s->rxbuf);
6548 ret -= h2s_htx->data;
6549
Christopher Faulet37070b22019-02-14 15:12:14 +01006550 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02006551 if (b_data(&h2s->rxbuf))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006552 se_fl_set(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006553 else {
Christopher Faulet34f81d52023-05-04 16:41:37 +02006554 if (!(h2c->flags & H2_CF_IS_BACK) && (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_ES_RCVD))) {
Christopher Faulet4403cdf2023-05-04 15:49:12 +02006555 /* If request ES is reported to the upper layer, it means the
6556 * H2S now expects data from the opposite side.
6557 */
6558 se_expect_data(h2s->sd);
6559 }
6560
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006561 se_fl_clr(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet531dd052023-05-24 11:14:38 +02006562 h2s_propagate_term_flags(h2c, h2s);
Olivier Houchard638b7992018-08-16 15:41:52 +02006563 if (b_size(&h2s->rxbuf)) {
6564 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01006565 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02006566 }
Olivier Houchard511efea2018-08-16 15:30:32 +02006567 }
6568
Willy Tarreau082f5592018-11-25 08:03:32 +01006569 if (ret && h2c->dsi == h2s->id) {
6570 /* demux is blocking on this stream's buffer */
6571 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02006572 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01006573 }
Christopher Faulet37070b22019-02-14 15:12:14 +01006574
Willy Tarreau7838a792019-08-12 18:42:03 +02006575 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02006576 return ret;
6577}
6578
Olivier Houchardd846c262018-10-19 17:24:29 +02006579
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006580/* Called from the upper layer, to send data from buffer <buf> for no more than
6581 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02006582 * flags may be updated on the stream connector.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006583 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006584static size_t h2_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02006585{
Willy Tarreau36c22322022-05-27 10:41:24 +02006586 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau1dc41e72018-06-14 13:21:28 +02006587 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02006588 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006589 struct htx *htx;
6590 struct htx_blk *blk;
6591 enum htx_blk_type btype;
6592 uint32_t bsize;
6593 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006594
Willy Tarreau7838a792019-08-12 18:42:03 +02006595 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
6596
Olivier Houchardd360ac62019-03-22 17:37:16 +01006597 /* If we were not just woken because we wanted to send but couldn't,
6598 * and there's somebody else that is waiting to send, do nothing,
6599 * we will subscribe later and be put at the end of the list
6600 */
Willy Tarreaud9464162020-01-10 18:25:07 +01006601 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02006602 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006603 if (LIST_INLIST(&h2s->list))
6604 TRACE_DEVEL("stream already waiting, leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6605 else {
6606 TRACE_DEVEL("other streams already waiting, going to the queue and leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6607 h2s->h2c->flags |= H2_CF_WAIT_INLIST;
6608 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01006609 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006610 }
Willy Tarreaud9464162020-01-10 18:25:07 +01006611 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02006612
Willy Tarreau7838a792019-08-12 18:42:03 +02006613 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
6614 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 +02006615 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006616 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02006617
Willy Tarreaucab22952019-10-31 15:48:18 +01006618 if (h2s->h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006619 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreaucab22952019-10-31 15:48:18 +01006620 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);
6621 return 0;
6622 }
6623
Christopher Faulet9b79a102019-07-15 11:22:56 +02006624 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006625
Willy Tarreau0bad0432018-06-14 16:54:01 +02006626 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01006627 h2s->flags |= H2_SF_OUTGOING_DATA;
6628
Christopher Faulet2e47e3a2023-01-13 11:40:24 +01006629 if (htx->extra && htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH)
Willy Tarreau48770452022-08-18 16:03:51 +02006630 h2s->flags |= H2_SF_MORE_HTX_DATA;
6631 else
6632 h2s->flags &= ~H2_SF_MORE_HTX_DATA;
6633
Willy Tarreau751f2d02018-10-05 09:35:00 +02006634 if (h2s->id == 0) {
6635 int32_t id = h2c_get_next_sid(h2s->h2c);
6636
6637 if (id < 0) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006638 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02006639 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 +02006640 return 0;
6641 }
6642
6643 eb32_delete(&h2s->by_id);
6644 h2s->by_id.key = h2s->id = id;
6645 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01006646 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02006647 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
6648 }
6649
Christopher Faulet9b79a102019-07-15 11:22:56 +02006650 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
6651 count && !htx_is_empty(htx)) {
6652 idx = htx_get_head(htx);
6653 blk = htx_get_blk(htx, idx);
6654 btype = htx_get_blk_type(blk);
6655 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006656
Christopher Faulet9b79a102019-07-15 11:22:56 +02006657 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02006658 case HTX_BLK_REQ_SL:
6659 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006660 ret = h2s_snd_bhdrs(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02006661 if (ret > 0) {
6662 total += ret;
6663 count -= ret;
6664 if (ret < bsize)
6665 goto done;
6666 }
6667 break;
6668
Willy Tarreau115e83b2018-12-01 19:17:53 +01006669 case HTX_BLK_RES_SL:
6670 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006671 ret = h2s_snd_fhdrs(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01006672 if (ret > 0) {
6673 total += ret;
6674 count -= ret;
6675 if (ret < bsize)
6676 goto done;
6677 }
6678 break;
6679
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006680 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006681 /* all these cause the emission of a DATA frame (possibly empty) */
Christopher Faulet991febd2020-12-02 15:17:31 +01006682 if (!(h2s->h2c->flags & H2_CF_IS_BACK) &&
6683 (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BODYLESS_RESP)) == H2_SF_BODYLESS_RESP)
6684 ret = h2s_skip_data(h2s, buf, count);
6685 else
6686 ret = h2s_make_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006687 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01006688 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006689 total += ret;
6690 count -= ret;
6691 if (ret < bsize)
6692 goto done;
6693 }
6694 break;
6695
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006696 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006697 case HTX_BLK_EOT:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006698 /* This is the first trailers block, all the subsequent ones */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006699 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006700 if (ret > 0) {
6701 total += ret;
6702 count -= ret;
6703 if (ret < bsize)
6704 goto done;
6705 }
6706 break;
6707
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006708 default:
6709 htx_remove_blk(htx, blk);
6710 total += bsize;
6711 count -= bsize;
6712 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006713 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006714 }
6715
Christopher Faulet9b79a102019-07-15 11:22:56 +02006716 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02006717 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02006718 /* trim any possibly pending data after we close (extra CR-LF,
6719 * unprocessed trailers, abnormal extra data, ...)
6720 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02006721 total += count;
6722 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02006723 }
6724
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006725 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01006726 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006727 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 Tarreau95acc8b2022-05-27 16:14:10 +02006728 se_fl_set_error(h2s->sd);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01006729 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01006730 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006731 }
6732
Christopher Faulet9b79a102019-07-15 11:22:56 +02006733 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02006734
Olivier Houchard7505f942018-08-21 18:10:44 +02006735 if (total > 0) {
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006736 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006737 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 +02006738 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006739 }
Olivier Houchardd846c262018-10-19 17:24:29 +02006740
Olivier Houchard7505f942018-08-21 18:10:44 +02006741 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006742 /* If we're waiting for flow control, and we got a shutr on the
6743 * connection, we will never be unlocked, so add an error on
Willy Tarreau4596fe22022-05-17 19:07:51 +02006744 * the stream connector.
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006745 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02006746 if ((h2s->h2c->flags & H2_CF_RCVD_SHUT) &&
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006747 !b_data(&h2s->h2c->dbuf) &&
6748 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006749 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);
Willy Tarreau35c4dd02023-01-17 16:25:29 +01006750 se_fl_set_error(h2s->sd);
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006751 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006752
Willy Tarreau5723f292020-01-10 15:16:57 +01006753 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
6754 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006755 /* Ok we managed to send something, leave the send_list if we were still there */
Willy Tarreaude4a5382023-10-17 08:25:19 +02006756 h2_remove_from_list(h2s);
6757 TRACE_DEVEL("Removed from h2s list", H2_EV_H2S_SEND|H2_EV_H2C_SEND, h2s->h2c->conn, h2s);
Olivier Houchardd360ac62019-03-22 17:37:16 +01006758 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006759
Willy Tarreau7838a792019-08-12 18:42:03 +02006760 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006761 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02006762}
6763
Willy Tarreau90bffa22022-09-01 19:06:44 +02006764/* appends some info about stream <h2s> to buffer <msg>, or does nothing if
Willy Tarreau7051f732022-09-02 15:22:12 +02006765 * <h2s> is NULL. Returns non-zero if the stream is considered suspicious. May
6766 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
6767 * NULL, otherwise a single line is used.
Willy Tarreau90bffa22022-09-01 19:06:44 +02006768 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006769static int h2_dump_h2s_info(struct buffer *msg, const struct h2s *h2s, const char *pfx)
Willy Tarreau90bffa22022-09-01 19:06:44 +02006770{
6771 int ret = 0;
6772
6773 if (!h2s)
6774 return ret;
6775
Willy Tarreau7051f732022-09-02 15:22:12 +02006776 chunk_appendf(msg, " h2s.id=%d .st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u",
Willy Tarreau90bffa22022-09-01 19:06:44 +02006777 h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
6778 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02006779 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf));
6780
6781 if (pfx)
6782 chunk_appendf(msg, "\n%s", pfx);
6783
6784 chunk_appendf(msg, " .sc=%p", h2s_sc(h2s));
Willy Tarreau90bffa22022-09-01 19:06:44 +02006785 if (h2s_sc(h2s))
6786 chunk_appendf(msg, "(.flg=0x%08x .app=%p)",
6787 h2s_sc(h2s)->flags, h2s_sc(h2s)->app);
6788
Willy Tarreau7051f732022-09-02 15:22:12 +02006789 chunk_appendf(msg, " .sd=%p", h2s->sd);
Willy Tarreau90bffa22022-09-01 19:06:44 +02006790 chunk_appendf(msg, "(.flg=0x%08x)", se_fl_get(h2s->sd));
6791
Willy Tarreau7051f732022-09-02 15:22:12 +02006792 if (pfx)
6793 chunk_appendf(msg, "\n%s", pfx);
6794
Willy Tarreau90bffa22022-09-01 19:06:44 +02006795 chunk_appendf(msg, " .subs=%p", h2s->subs);
6796 if (h2s->subs) {
6797 chunk_appendf(msg, "(ev=%d tl=%p", h2s->subs->events, h2s->subs->tasklet);
6798 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
6799 h2s->subs->tasklet->calls,
6800 h2s->subs->tasklet->context);
6801 if (h2s->subs->tasklet->calls >= 1000000)
6802 ret = 1;
6803 resolve_sym_name(msg, NULL, h2s->subs->tasklet->process);
6804 chunk_appendf(msg, ")");
6805 }
6806 return ret;
6807}
6808
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006809/* appends some info about connection <h2c> to buffer <msg>, or does nothing if
6810 * <h2c> is NULL. Returns non-zero if the connection is considered suspicious.
Willy Tarreau7051f732022-09-02 15:22:12 +02006811 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
6812 * not NULL, otherwise a single line is used.
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006813 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006814static int h2_dump_h2c_info(struct buffer *msg, struct h2c *h2c, const char *pfx)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006815{
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006816 const struct buffer *hmbuf, *tmbuf;
6817 const struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006818 struct eb32_node *node;
6819 int fctl_cnt = 0;
6820 int send_cnt = 0;
6821 int tree_cnt = 0;
6822 int orph_cnt = 0;
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006823 int ret = 0;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006824
6825 if (!h2c)
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006826 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006827
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006828 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006829 fctl_cnt++;
6830
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006831 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006832 send_cnt++;
6833
6834 node = eb32_first(&h2c->streams_by_id);
6835 while (node) {
6836 h2s = container_of(node, struct h2s, by_id);
6837 tree_cnt++;
Willy Tarreau7be4ee02022-05-18 07:31:41 +02006838 if (!h2s_sc(h2s))
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006839 orph_cnt++;
6840 node = eb32_next(node);
6841 }
6842
Willy Tarreau60f62682019-05-26 11:32:27 +02006843 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02006844 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02006845 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau7051f732022-09-02 15:22:12 +02006846 " .nbst=%u .nbsc=%u",
Willy Tarreauab2ec452019-08-30 07:07:08 +02006847 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau7051f732022-09-02 15:22:12 +02006848 h2c->nb_streams, h2c->nb_sc);
6849
6850 if (pfx)
6851 chunk_appendf(msg, "\n%s", pfx);
6852
6853 chunk_appendf(msg, " .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
6854 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u",
6855 fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006856 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01006857 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02006858 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf));
6859
6860 if (pfx)
6861 chunk_appendf(msg, "\n%s", pfx);
6862
Christopher Faulet68ee7842022-10-12 10:21:33 +02006863 chunk_appendf(msg, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau60f62682019-05-26 11:32:27 +02006864 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
6865 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
6866 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02006867 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
6868 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01006869
Willy Tarreauf8c77092022-11-29 15:26:43 +01006870 chunk_appendf(msg, " .task=%p", h2c->task);
6871 if (h2c->task) {
6872 chunk_appendf(msg, " .exp=%s",
6873 h2c->task->expire ? tick_is_expired(h2c->task->expire, now_ms) ? "<PAST>" :
6874 human_time(TICKS_TO_MS(h2c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>");
6875 }
Willy Tarreau7051f732022-09-02 15:22:12 +02006876
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006877 return ret;
6878}
6879
6880/* for debugging with CLI's "show fd" command */
6881static int h2_show_fd(struct buffer *msg, struct connection *conn)
6882{
6883 struct h2c *h2c = conn->ctx;
6884 const struct h2s *h2s;
6885 struct eb32_node *node;
6886 int ret = 0;
6887
6888 if (!h2c)
6889 return ret;
6890
Willy Tarreau7051f732022-09-02 15:22:12 +02006891 ret |= h2_dump_h2c_info(msg, h2c, NULL);
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006892
6893 node = eb32_last(&h2c->streams_by_id);
6894 if (node) {
6895 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau90bffa22022-09-01 19:06:44 +02006896 chunk_appendf(msg, " last_h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02006897 ret |= h2_dump_h2s_info(msg, h2s, NULL);
Willy Tarreau987c0632018-12-18 10:32:05 +01006898 }
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006899
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006900 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006901}
Willy Tarreau62f52692017-10-08 23:01:42 +02006902
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02006903/* for debugging with CLI's "show sess" command. May emit multiple lines, each
6904 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
6905 * line is used. Each field starts with a space so it's safe to print it after
6906 * existing fields.
6907 */
6908static int h2_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
6909{
6910 struct h2s *h2s = sd->se;
6911 int ret = 0;
6912
6913 if (!h2s)
6914 return ret;
6915
6916 chunk_appendf(msg, " h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02006917 ret |= h2_dump_h2s_info(msg, h2s, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02006918 if (pfx)
6919 chunk_appendf(msg, "\n%s", pfx);
6920 chunk_appendf(msg, " h2c=%p", h2s->h2c);
Willy Tarreau7051f732022-09-02 15:22:12 +02006921 ret |= h2_dump_h2c_info(msg, h2s->h2c, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02006922 return ret;
6923}
6924
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006925/* Migrate the the connection to the current thread.
6926 * Return 0 if successful, non-zero otherwise.
6927 * Expected to be called with the old thread lock held.
6928 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02006929static int h2_takeover(struct connection *conn, int orig_tid)
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006930{
6931 struct h2c *h2c = conn->ctx;
Willy Tarreau617e80f2020-07-01 16:39:33 +02006932 struct task *task;
Willy Tarreau960f37c2023-11-17 10:56:33 +01006933 struct task *new_task;
6934 struct tasklet *new_tasklet;
6935
6936 /* Pre-allocate tasks so that we don't have to roll back after the xprt
6937 * has been migrated.
6938 */
6939 new_task = task_new_here();
6940 new_tasklet = tasklet_new();
6941 if (!new_task || !new_tasklet)
6942 goto fail;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006943
6944 if (fd_takeover(conn->handle.fd, conn) != 0)
Willy Tarreau960f37c2023-11-17 10:56:33 +01006945 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006946
6947 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
6948 /* We failed to takeover the xprt, even if the connection may
6949 * still be valid, flag it as error'd, as we have already
6950 * taken over the fd, and wake the tasklet, so that it will
6951 * destroy it.
6952 */
6953 conn->flags |= CO_FL_ERROR;
6954 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
Willy Tarreau960f37c2023-11-17 10:56:33 +01006955 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006956 }
6957
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006958 if (h2c->wait_event.events)
6959 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
6960 h2c->wait_event.events, &h2c->wait_event);
Willy Tarreau617e80f2020-07-01 16:39:33 +02006961
6962 task = h2c->task;
6963 if (task) {
Willy Tarreau960f37c2023-11-17 10:56:33 +01006964 /* only assign a task if there was already one, otherwise
6965 * the preallocated new task will be released.
6966 */
Willy Tarreau617e80f2020-07-01 16:39:33 +02006967 task->context = NULL;
6968 h2c->task = NULL;
6969 __ha_barrier_store();
6970 task_kill(task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006971
Willy Tarreau960f37c2023-11-17 10:56:33 +01006972 h2c->task = new_task;
6973 new_task = NULL;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006974 h2c->task->process = h2_timeout_task;
6975 h2c->task->context = h2c;
6976 }
Willy Tarreau960f37c2023-11-17 10:56:33 +01006977
6978 /* To let the tasklet know it should free itself, and do nothing else,
6979 * set its context to NULL.
6980 */
6981 h2c->wait_event.tasklet->context = NULL;
6982 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
6983
6984 h2c->wait_event.tasklet = new_tasklet;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006985 h2c->wait_event.tasklet->process = h2_io_cb;
6986 h2c->wait_event.tasklet->context = h2c;
6987 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
6988 SUB_RETRY_RECV, &h2c->wait_event);
6989
Willy Tarreau960f37c2023-11-17 10:56:33 +01006990 if (new_task)
6991 __task_free(new_task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006992 return 0;
Willy Tarreau960f37c2023-11-17 10:56:33 +01006993 fail:
6994 if (new_task)
6995 __task_free(new_task);
6996 tasklet_free(new_tasklet);
6997 return -1;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006998}
6999
Willy Tarreau62f52692017-10-08 23:01:42 +02007000/*******************************************************/
7001/* functions below are dedicated to the config parsers */
7002/*******************************************************/
7003
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007004/* config parser for global "tune.h2.header-table-size" */
7005static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007006 const struct proxy *defpx, const char *file, int line,
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007007 char **err)
7008{
7009 if (too_many_args(1, args, err, NULL))
7010 return -1;
7011
7012 h2_settings_header_table_size = atoi(args[1]);
7013 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
7014 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
7015 return -1;
7016 }
7017 return 0;
7018}
Willy Tarreau62f52692017-10-08 23:01:42 +02007019
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007020/* config parser for global "tune.h2.{be.,fe.,}initial-window-size" */
Willy Tarreaue6baec02017-07-27 11:45:11 +02007021static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007022 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue6baec02017-07-27 11:45:11 +02007023 char **err)
7024{
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007025 int *vptr;
7026
Willy Tarreaue6baec02017-07-27 11:45:11 +02007027 if (too_many_args(1, args, err, NULL))
7028 return -1;
7029
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007030 /* backend/frontend/default */
7031 vptr = (args[0][8] == 'b') ? &h2_be_settings_initial_window_size :
7032 (args[0][8] == 'f') ? &h2_fe_settings_initial_window_size :
7033 &h2_settings_initial_window_size;
7034
7035 *vptr = atoi(args[1]);
7036 if (*vptr < 0) {
Willy Tarreaue6baec02017-07-27 11:45:11 +02007037 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7038 return -1;
7039 }
7040 return 0;
7041}
7042
Willy Tarreauca1027c2023-04-18 15:57:03 +02007043/* config parser for global "tune.h2.{be.,fe.,}max-concurrent-streams" */
Willy Tarreau5242ef82017-07-27 11:47:28 +02007044static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007045 const struct proxy *defpx, const char *file, int line,
Willy Tarreau5242ef82017-07-27 11:47:28 +02007046 char **err)
7047{
Willy Tarreauca1027c2023-04-18 15:57:03 +02007048 uint *vptr;
7049
Willy Tarreau5242ef82017-07-27 11:47:28 +02007050 if (too_many_args(1, args, err, NULL))
7051 return -1;
7052
Willy Tarreauca1027c2023-04-18 15:57:03 +02007053 /* backend/frontend/default */
7054 vptr = (args[0][8] == 'b') ? &h2_be_settings_max_concurrent_streams :
7055 (args[0][8] == 'f') ? &h2_fe_settings_max_concurrent_streams :
7056 &h2_settings_max_concurrent_streams;
7057
7058 *vptr = atoi(args[1]);
7059 if ((int)*vptr < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02007060 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7061 return -1;
7062 }
7063 return 0;
7064}
7065
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007066/* config parser for global "tune.h2.max-frame-size" */
7067static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007068 const struct proxy *defpx, const char *file, int line,
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007069 char **err)
7070{
7071 if (too_many_args(1, args, err, NULL))
7072 return -1;
7073
7074 h2_settings_max_frame_size = atoi(args[1]);
7075 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
7076 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
7077 return -1;
7078 }
7079 return 0;
7080}
7081
Willy Tarreau62f52692017-10-08 23:01:42 +02007082
7083/****************************************/
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05007084/* MUX initialization and instantiation */
Willy Tarreau62f52692017-10-08 23:01:42 +02007085/***************************************/
7086
7087/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01007088static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02007089 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02007090 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02007091 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02007092 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02007093 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02007094 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02007095 .attach = h2_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02007096 .get_first_sc = h2_get_first_sc,
Willy Tarreau62f52692017-10-08 23:01:42 +02007097 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01007098 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01007099 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01007100 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02007101 .shutr = h2_shutr,
7102 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02007103 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007104 .show_fd = h2_show_fd,
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007105 .show_sd = h2_show_sd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007106 .takeover = h2_takeover,
Christopher Fauleta97cced2022-04-12 18:04:10 +02007107 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Willy Tarreau62f52692017-10-08 23:01:42 +02007108 .name = "H2",
7109};
7110
Christopher Faulet32f61c02018-04-10 14:33:41 +02007111static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02007112 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02007113
Willy Tarreau0108d902018-11-25 19:14:37 +01007114INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
7115
Willy Tarreau62f52692017-10-08 23:01:42 +02007116/* config keyword parsers */
7117static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007118 { CFG_GLOBAL, "tune.h2.be.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007119 { CFG_GLOBAL, "tune.h2.be.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007120 { CFG_GLOBAL, "tune.h2.fe.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007121 { CFG_GLOBAL, "tune.h2.fe.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007122 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02007123 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02007124 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007125 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02007126 { 0, NULL, NULL }
7127}};
7128
Willy Tarreau0108d902018-11-25 19:14:37 +01007129INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007130
7131/* initialize internal structs after the config is parsed.
7132 * Returns zero on success, non-zero on error.
7133 */
7134static int init_h2()
7135{
7136 pool_head_hpack_tbl = create_pool("hpack_tbl",
7137 h2_settings_header_table_size,
7138 MEM_F_SHARED|MEM_F_EXACT);
Christopher Faulet52140992020-11-06 15:23:39 +01007139 if (!pool_head_hpack_tbl) {
7140 ha_alert("failed to allocate hpack_tbl memory pool\n");
7141 return (ERR_ALERT | ERR_FATAL);
7142 }
7143 return ERR_NONE;
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007144}
7145
7146REGISTER_POST_CHECK(init_h2);