blob: 1408c074ee16f363b75beb8234b1a046402a6099 [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 Tarreau4869ed52023-10-13 18:11:59 +0200414/* other non-protocol settings */
415static unsigned int h2_fe_max_total_streams = 0; /* frontend value */
416
Willy Tarreaub22b5f02022-05-10 14:57:16 +0200417/* a dummy closed endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +0200418static const struct sedesc closed_ep = {
Willy Tarreauc1054922022-05-18 07:43:52 +0200419 .sc = NULL,
Willy Tarreaub605c422022-05-17 17:04:55 +0200420 .flags = SE_FL_DETACHED,
Willy Tarreaub22b5f02022-05-10 14:57:16 +0200421};
422
Willy Tarreau2a856182017-05-16 15:20:39 +0200423/* a dmumy closed stream */
424static const struct h2s *h2_closed_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200425 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau2a856182017-05-16 15:20:39 +0200426 .h2c = NULL,
427 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100428 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100429 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200430 .id = 0,
431};
432
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100433/* a dmumy closed stream returning a PROTOCOL_ERROR error */
434static const struct h2s *h2_error_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200435 .sd = (struct sedesc *)&closed_ep,
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100436 .h2c = NULL,
437 .st = H2_SS_CLOSED,
438 .errcode = H2_ERR_PROTOCOL_ERROR,
439 .flags = 0,
440 .id = 0,
441};
442
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100443/* a dmumy closed stream returning a REFUSED_STREAM error */
444static const struct h2s *h2_refused_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200445 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100446 .h2c = NULL,
447 .st = H2_SS_CLOSED,
448 .errcode = H2_ERR_REFUSED_STREAM,
449 .flags = 0,
450 .id = 0,
451};
452
Willy Tarreau2a856182017-05-16 15:20:39 +0200453/* and a dummy idle stream for use with any unannounced stream */
454static const struct h2s *h2_idle_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200455 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 .h2c = NULL,
457 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100458 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200459 .id = 0,
460};
461
Willy Tarreaude4a5382023-10-17 08:25:19 +0200462
Willy Tarreau144f84a2021-03-02 16:09:26 +0100463struct task *h2_timeout_task(struct task *t, void *context, unsigned int state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200464static int h2_send(struct h2c *h2c);
465static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200466static int h2_process(struct h2c *h2c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100467/* h2_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100468struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100469static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau7cfbb812023-01-26 16:02:01 +0100470static 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 +0100471static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100472struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state);
Willy Tarreau36c22322022-05-27 10:41:24 +0200473static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct stconn *sc, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100474static void h2s_alert(struct h2s *h2s);
Willy Tarreaude4a5382023-10-17 08:25:19 +0200475static inline void h2_remove_from_list(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200476
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200477/* returns the stconn associated to the H2 stream */
478static forceinline struct stconn *h2s_sc(const struct h2s *h2s)
479{
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200480 return h2s->sd->sc;
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200481}
482
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200483/* the H2 traces always expect that arg1, if non-null, is of type connection
484 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
485 * that arg3, if non-null, is either of type htx for tx headers, or of type
486 * buffer for everything else.
487 */
488static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
489 const struct ist where, const struct ist func,
490 const void *a1, const void *a2, const void *a3, const void *a4)
491{
492 const struct connection *conn = a1;
493 const struct h2c *h2c = conn ? conn->ctx : NULL;
494 const struct h2s *h2s = a2;
495 const struct buffer *buf = a3;
496 const struct htx *htx;
497 int pos;
498
499 if (!h2c) // nothing to add
500 return;
501
Willy Tarreau17104d42019-08-30 07:12:55 +0200502 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau73db4342019-09-25 07:28:44 +0200503 chunk_appendf(&trace_buf, " : h2c=%p(%c,%s)", h2c, conn_is_back(conn) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
504
Willy Tarreau8e6f7492021-06-16 17:47:24 +0200505 if (mask & H2_EV_H2C_NEW) // inside h2_init, otherwise it's hard to match conn & h2c
506 conn_append_debug_info(&trace_buf, conn, " : ");
507
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100508 if (h2c->errcode)
509 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2c->errcode), h2c->errcode);
510
Willy Tarreau0f458712022-08-18 11:19:57 +0200511 if (h2c->flags & H2_CF_DEM_IN_PROGRESS && // frame processing has started, type and length are valid
Willy Tarreau73db4342019-09-25 07:28:44 +0200512 (mask & (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) == (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) {
Willy Tarreau8520d872020-09-18 07:39:29 +0200513 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 +0200514 }
515
516 if (h2s) {
517 if (h2s->id <= 0)
518 chunk_appendf(&trace_buf, " dsi=%d", h2c->dsi);
Willy Tarreauf9f44992023-02-20 16:57:47 +0100519 if (h2s == h2_idle_stream)
520 chunk_appendf(&trace_buf, " h2s=IDL");
521 else if (h2s != h2_closed_stream)
522 chunk_appendf(&trace_buf, " h2s=%p(%d,%s)", h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100523 if (h2s->id && h2s->errcode)
524 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2s->errcode), h2s->errcode);
Willy Tarreau73db4342019-09-25 07:28:44 +0200525 }
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200526 }
527
528 /* Let's dump decoded requests and responses right after parsing. They
529 * are traced at level USER with a few recognizable flags.
530 */
531 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
532 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
533 htx = htxbuf(buf); // recv req/res
534 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
535 htx = a3; // send req/res
536 else
537 htx = NULL;
538
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200539 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200540 const struct htx_blk *blk = htx_get_blk(htx, pos);
541 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
542 enum htx_blk_type type = htx_get_blk_type(blk);
543
544 if (type == HTX_BLK_REQ_SL)
545 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200546 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200547 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
548 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
549 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
550 else if (type == HTX_BLK_RES_SL)
551 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200552 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200553 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
554 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
555 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
556 }
557}
558
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200559
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100560/* Detect a pending read0 for a H2 connection. It happens if a read0 was
561 * already reported on a previous xprt->rcvbuf() AND a frame parser failed
562 * to parse pending data, confirming no more progress is possible because
563 * we're facing a truncated frame. The function returns 1 to report a read0
564 * or 0 otherwise.
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200565 */
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100566static inline int h2c_read0_pending(struct h2c *h2c)
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200567{
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100568 return !!(h2c->flags & H2_CF_END_REACHED);
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200569}
570
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200571/* returns true if the connection is allowed to expire, false otherwise. A
Willy Tarreau34395832022-03-18 14:59:54 +0100572 * connection may expire when it has no attached streams. As long as streams
573 * are attached, the application layer is responsible for timeout management,
574 * and each layer will detach when it doesn't want to wait anymore. When the
575 * last one leaves, the connection must take over timeout management.
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200576 */
577static inline int h2c_may_expire(const struct h2c *h2c)
578{
Willy Tarreau36c22322022-05-27 10:41:24 +0200579 return !h2c->nb_sc;
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200580}
581
Willy Tarreauca1027c2023-04-18 15:57:03 +0200582/* returns the number of max concurrent streams permitted on a connection,
583 * depending on its side (frontend or backend), falling back to the default
584 * h2_settings_max_concurrent_streams. It may even be zero.
585 */
586static inline int h2c_max_concurrent_streams(const struct h2c *h2c)
587{
588 int ret;
589
590 ret = (h2c->flags & H2_CF_IS_BACK) ?
591 h2_be_settings_max_concurrent_streams :
592 h2_fe_settings_max_concurrent_streams;
593
594 ret = ret ? ret : h2_settings_max_concurrent_streams;
595 return ret;
596}
597
598
Willy Tarreau15a47332022-03-18 15:57:34 +0100599/* update h2c timeout if needed */
600static void h2c_update_timeout(struct h2c *h2c)
601{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200602 int is_idle_conn = 0;
603
Willy Tarreau15a47332022-03-18 15:57:34 +0100604 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
605
606 if (!h2c->task)
607 goto leave;
608
609 if (h2c_may_expire(h2c)) {
610 /* no more streams attached */
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200611 if (br_data(h2c->mbuf)) {
Willy Tarreau15a47332022-03-18 15:57:34 +0100612 /* pending output data: always the regular data timeout */
613 h2c->task->expire = tick_add_ifset(now_ms, h2c->timeout);
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200614 } else {
615 /* no stream, no output data */
616 if (!(h2c->flags & H2_CF_IS_BACK)) {
617 int to;
Willy Tarreau86b08a32022-04-13 17:40:28 +0200618
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200619 if (h2c->max_id > 0 && !b_data(&h2c->dbuf) &&
620 tick_isset(h2c->proxy->timeout.httpka)) {
621 /* idle after having seen one stream => keep-alive */
622 to = h2c->proxy->timeout.httpka;
623 } else {
624 /* before first request, or started to deserialize a
625 * new req => http-request.
626 */
627 to = h2c->proxy->timeout.httpreq;
628 }
Willy Tarreau86b08a32022-04-13 17:40:28 +0200629
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200630 h2c->task->expire = tick_add_ifset(h2c->idle_start, to);
631 is_idle_conn = 1;
632 }
633
634 if (h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) {
635 /* GOAWAY sent (or failed), closing in progress */
636 int exp = tick_add_ifset(now_ms, h2c->shut_timeout);
637
638 h2c->task->expire = tick_first(h2c->task->expire, exp);
639 is_idle_conn = 1;
640 }
641
642 /* if a timeout above was not set, fall back to the default one */
643 if (!tick_isset(h2c->task->expire))
644 h2c->task->expire = tick_add_ifset(now_ms, h2c->timeout);
Willy Tarreau15a47332022-03-18 15:57:34 +0100645 }
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200646
647 if ((h2c->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
648 is_idle_conn && tick_isset(global.close_spread_end)) {
649 /* If a soft-stop is in progress and a close-spread-time
650 * is set, we want to spread idle connection closing roughly
651 * evenly across the defined window. This should only
652 * act on idle frontend connections.
653 * If the window end is already in the past, we wake the
654 * timeout task up immediately so that it can be closed.
655 */
656 int remaining_window = tick_remain(now_ms, global.close_spread_end);
657 if (remaining_window) {
658 /* We don't need to reset the expire if it would
659 * already happen before the close window end.
660 */
661 if (tick_isset(h2c->task->expire) &&
662 tick_is_le(global.close_spread_end, h2c->task->expire)) {
663 /* Set an expire value shorter than the current value
664 * because the close spread window end comes earlier.
665 */
666 h2c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
667 }
668 }
669 else {
670 /* We are past the soft close window end, wake the timeout
671 * task up immediately.
672 */
673 task_wakeup(h2c->task, TASK_WOKEN_TIMER);
674 }
675 }
676
Willy Tarreau15a47332022-03-18 15:57:34 +0100677 } else {
678 h2c->task->expire = TICK_ETERNITY;
679 }
680 task_queue(h2c->task);
681 leave:
682 TRACE_LEAVE(H2_EV_H2C_WAKE);
683}
684
Olivier Houchard7a977432019-03-21 15:47:13 +0100685static __inline int
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200686h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100687{
688 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
Christopher Fauletff7925d2022-10-11 19:12:40 +0200689 ((h2c->flags & H2_CF_ERROR) || /* errors close immediately */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +0200690 (h2c->flags & H2_CF_ERR_PENDING && h2c->st0 < H2_CS_FRAME_H) || /* early error during connect */
Olivier Houchard7a977432019-03-21 15:47:13 +0100691 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
692 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200693 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Christopher Fauletff7925d2022-10-11 19:12:40 +0200694 ((h2c->flags & H2_CF_RCVD_SHUT) ||
Olivier Houchard7a977432019-03-21 15:47:13 +0100695 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
696 return 1;
697
698 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100699}
700
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200701/*****************************************************/
702/* functions below are for dynamic buffer management */
703/*****************************************************/
704
Willy Tarreau315d8072017-12-10 22:17:57 +0100705/* indicates whether or not the we may call the h2_recv() function to attempt
706 * to receive data into the buffer and/or demux pending data. The condition is
707 * a bit complex due to some API limits for now. The rules are the following :
708 * - if an error or a shutdown was detected on the connection and the buffer
709 * is empty, we must not attempt to receive
710 * - if the demux buf failed to be allocated, we must not try to receive and
711 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100712 * - if no flag indicates a blocking condition, we may attempt to receive,
713 * regardless of whether the demux buffer is full or not, so that only
714 * de demux part decides whether or not to block. This is needed because
715 * the connection API indeed prevents us from re-enabling receipt that is
716 * already enabled in a polled state, so we must always immediately stop
717 * as soon as the demux can't proceed so as never to hit an end of read
718 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100719 * - otherwise must may not attempt
720 */
721static inline int h2_recv_allowed(const struct h2c *h2c)
722{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200723 if (b_data(&h2c->dbuf) == 0 &&
Christopher Fauletff7925d2022-10-11 19:12:40 +0200724 ((h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR)) || h2c->st0 >= H2_CS_ERROR))
Willy Tarreau315d8072017-12-10 22:17:57 +0100725 return 0;
726
727 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100728 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100729 return 1;
730
731 return 0;
732}
733
Willy Tarreau47b515a2018-12-21 16:09:41 +0100734/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200735static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100736{
737 if (!h2_recv_allowed(h2c))
738 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200739 if ((!consider_buffer || !b_data(&h2c->dbuf))
740 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100741 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200742 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100743}
744
745
Willy Tarreau4596fe22022-05-17 19:07:51 +0200746/* returns true if the front connection has too many stream connectors attached */
Willy Tarreau36c22322022-05-27 10:41:24 +0200747static inline int h2_frt_has_too_many_sc(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200748{
Willy Tarreauca1027c2023-04-18 15:57:03 +0200749 return h2c->nb_sc > h2c_max_concurrent_streams(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +0200750}
751
Willy Tarreau44e973f2018-03-01 17:49:30 +0100752/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
753 * flags are used to figure what buffer was requested. It returns 1 if the
754 * allocation succeeds, in which case the connection is woken up, or 0 if it's
755 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200756 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100757static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200758{
759 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100760 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200761
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100762 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc(&h2c->dbuf)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200763 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200764 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200765 return 1;
766 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200767
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100768 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc(br_tail(h2c->mbuf))) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100769 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200770
771 if (h2c->flags & H2_CF_DEM_MROOM) {
772 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200773 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200774 }
Willy Tarreau14398122017-09-22 14:26:04 +0200775 return 1;
776 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100777
778 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200779 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s_sc(h2s) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100780 b_alloc(&h2s->rxbuf)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100781 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200782 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100783 return 1;
784 }
785
Willy Tarreau14398122017-09-22 14:26:04 +0200786 return 0;
787}
788
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200789static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200790{
791 struct buffer *buf = NULL;
792
Willy Tarreau2b718102021-04-21 07:32:39 +0200793 if (likely(!LIST_INLIST(&h2c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100794 unlikely((buf = b_alloc(bptr)) == NULL)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100795 h2c->buf_wait.target = h2c;
796 h2c->buf_wait.wakeup_cb = h2_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200797 LIST_APPEND(&th_ctx->buffer_wq, &h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200798 }
799 return buf;
800}
801
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200802static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200803{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200804 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100805 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100806 offer_buffers(NULL, 1);
Willy Tarreau14398122017-09-22 14:26:04 +0200807 }
808}
809
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200810static inline void h2_release_mbuf(struct h2c *h2c)
811{
812 struct buffer *buf;
813 unsigned int count = 0;
814
815 while (b_size(buf = br_head_pick(h2c->mbuf))) {
816 b_free(buf);
817 count++;
818 }
819 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100820 offer_buffers(NULL, count);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200821}
822
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100823/* returns the number of allocatable outgoing streams for the connection taking
824 * the last_sid and the reserved ones into account.
825 */
826static inline int h2_streams_left(const struct h2c *h2c)
827{
828 int ret;
829
830 /* consider the number of outgoing streams we're allowed to create before
831 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
832 * nb_reserved is the number of streams which don't yet have an ID.
833 */
834 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
835 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
836 if (ret < 0)
837 ret = 0;
838 return ret;
839}
840
Willy Tarreau00f18a32019-01-26 12:19:01 +0100841/* returns the number of streams in use on a connection to figure if it's
Willy Tarreau36c22322022-05-27 10:41:24 +0200842 * idle or not. We check nb_sc and not nb_streams as the caller will want
Willy Tarreau00f18a32019-01-26 12:19:01 +0100843 * to know if it was the last one after a detach().
844 */
845static int h2_used_streams(struct connection *conn)
846{
847 struct h2c *h2c = conn->ctx;
848
Willy Tarreau36c22322022-05-27 10:41:24 +0200849 return h2c->nb_sc;
Willy Tarreau00f18a32019-01-26 12:19:01 +0100850}
851
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100852/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100853static int h2_avail_streams(struct connection *conn)
854{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100855 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100856 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100857 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100858
Willy Tarreau6afec462019-01-28 06:40:19 +0100859 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
860 * streams on the connection.
861 */
862 if (h2c->last_sid >= 0)
863 return 0;
864
Willy Tarreauc61966f2019-10-31 15:10:03 +0100865 if (h2c->st0 >= H2_CS_ERROR)
866 return 0;
867
Willy Tarreau86949782019-01-31 10:42:05 +0100868 /* note: may be negative if a SETTINGS frame changes the limit */
869 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100870
871 /* we must also consider the limit imposed by stream IDs */
872 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100873 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100874 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100875 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
876 ret1 = MIN(ret1, ret2);
877 }
878 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100879}
880
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200881/* Unconditionally produce a trace of the header. Please do not call this one
Willy Tarreau11e8a8c2023-01-24 19:43:11 +0100882 * and use h2_trace_header() instead which first checks if traces are enabled.
883 */
884void _h2_trace_header(const struct ist hn, const struct ist hv,
885 uint64_t mask, const struct ist trc_loc, const char *func,
886 const struct h2c *h2c, const struct h2s *h2s)
887{
888 struct ist n_ist, v_ist;
889 const char *c_str, *s_str;
890
891 chunk_reset(&trash);
892 c_str = chunk_newstr(&trash);
893 if (h2c) {
894 chunk_appendf(&trash, "h2c=%p(%c,%s) ",
895 h2c, (h2c->flags & H2_CF_IS_BACK) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
896 }
897
898 s_str = chunk_newstr(&trash);
899 if (h2s) {
900 if (h2s->id <= 0)
901 chunk_appendf(&trash, "dsi=%d ", h2s->h2c->dsi);
902 chunk_appendf(&trash, "h2s=%p(%d,%s) ", h2s, h2s->id, h2s_st_to_str(h2s->st));
903 }
904 else if (h2c)
905 chunk_appendf(&trash, "dsi=%d ", h2c->dsi);
906
907 n_ist = ist2(chunk_newstr(&trash), 0);
908 istscpy(&n_ist, hn, 256);
909 trash.data += n_ist.len;
910 if (n_ist.len != hn.len)
911 chunk_appendf(&trash, " (... +%ld)", (long)(hn.len - n_ist.len));
912
913 v_ist = ist2(chunk_newstr(&trash), 0);
914 istscpy(&v_ist, hv, 1024);
915 trash.data += v_ist.len;
916 if (v_ist.len != hv.len)
917 chunk_appendf(&trash, " (... +%ld)", (long)(hv.len - v_ist.len));
918
919 TRACE_PRINTF_LOC(TRACE_LEVEL_USER, mask, trc_loc, func,
Christopher Fauletc2545162023-01-30 08:26:09 +0100920 (h2c ? h2c->conn : 0), 0, 0, 0,
Willy Tarreau11e8a8c2023-01-24 19:43:11 +0100921 "%s%s%s %s: %s", c_str, s_str,
922 (mask & H2_EV_TX_HDR) ? "sndh" : "rcvh",
923 n_ist.ptr, v_ist.ptr);
924}
925
926/* produce a trace of the header after checking that tracing is enabled */
927static inline void h2_trace_header(const struct ist hn, const struct ist hv,
928 uint64_t mask, const struct ist trc_loc, const char *func,
929 const struct h2c *h2c, const struct h2s *h2s)
930{
931 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
932 TRACE_ENABLED(TRACE_LEVEL_USER, mask, h2c ? h2c->conn : 0, h2s, 0, 0))
933 _h2_trace_header(hn, hv, mask, trc_loc, func, h2c, h2s);
934}
935
936/* hpack-encode header name <hn> and value <hv>, possibly emitting a trace if
937 * currently enabled. This is done on behalf of function <func> at <trc_loc>
938 * passed as ist(TRC_LOC), h2c <h2c>, and h2s <h2s>, all of which may be NULL.
939 * The trace is only emitted if the header is emitted (in which case non-zero
940 * is returned). The trash is modified. In the traces, the header's name will
941 * be truncated to 256 chars and the header's value to 1024 chars.
942 */
943static inline int h2_encode_header(struct buffer *buf, const struct ist hn, const struct ist hv,
944 uint64_t mask, const struct ist trc_loc, const char *func,
945 const struct h2c *h2c, const struct h2s *h2s)
946{
947 int ret;
948
949 ret = hpack_encode_header(buf, hn, hv);
950 if (ret)
951 h2_trace_header(hn, hv, mask, trc_loc, func, h2c, h2s);
952
953 return ret;
954}
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200955
Willy Tarreau62f52692017-10-08 23:01:42 +0200956/*****************************************************************/
957/* functions below are dedicated to the mux setup and management */
958/*****************************************************************/
959
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200960/* Initialize the mux once it's attached. For outgoing connections, the context
961 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200962 * connections from the fact that the context is still NULL (even during mux
963 * upgrades). <input> is always used as Input buffer and may contain data. It is
964 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200965 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200966static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
967 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200968{
969 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100970 struct task *t = NULL;
Christopher Fauletf81ef032019-10-04 15:19:43 +0200971 void *conn_ctx = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200972
Christopher Fauletf81ef032019-10-04 15:19:43 +0200973 TRACE_ENTER(H2_EV_H2C_NEW);
Willy Tarreau7838a792019-08-12 18:42:03 +0200974
Willy Tarreaubafbe012017-11-24 17:34:44 +0100975 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200976 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200977 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200978
Christopher Faulete9b70722019-04-08 10:46:02 +0200979 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200980 h2c->flags = H2_CF_IS_BACK;
981 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
982 if (tick_isset(prx->timeout.serverfin))
983 h2c->shut_timeout = prx->timeout.serverfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100984
985 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
986 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200987 } else {
988 h2c->flags = H2_CF_NONE;
989 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
990 if (tick_isset(prx->timeout.clientfin))
991 h2c->shut_timeout = prx->timeout.clientfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100992
993 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
994 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200995 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100996
Willy Tarreau0b37d652018-10-03 10:33:02 +0200997 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100998 h2c->task = NULL;
Willy Tarreau389ab0d2023-03-20 19:16:04 +0100999 h2c->wait_event.tasklet = NULL;
Willy Tarreau15a47332022-03-18 15:57:34 +01001000 h2c->idle_start = now_ms;
Willy Tarreau3f133572017-10-31 19:21:06 +01001001 if (tick_isset(h2c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001002 t = task_new_here();
Willy Tarreau3f133572017-10-31 19:21:06 +01001003 if (!t)
1004 goto fail;
1005
1006 h2c->task = t;
1007 t->process = h2_timeout_task;
1008 t->context = h2c;
1009 t->expire = tick_add(now_ms, h2c->timeout);
1010 }
Willy Tarreauea392822017-10-31 10:02:25 +01001011
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001012 h2c->wait_event.tasklet = tasklet_new();
1013 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001014 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001015 h2c->wait_event.tasklet->process = h2_io_cb;
1016 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001017 h2c->wait_event.events = 0;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001018 if (!conn_is_back(conn)) {
1019 /* Connection might already be in the stopping_list if subject
1020 * to h1->h2 upgrade.
1021 */
1022 if (!LIST_INLIST(&conn->stopping_list)) {
1023 LIST_APPEND(&mux_stopping_data[tid].list,
1024 &conn->stopping_list);
1025 }
1026 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001027
Willy Tarreau2bdcc702020-05-19 11:31:11 +02001028 h2c->ddht = hpack_dht_alloc();
Willy Tarreau32218eb2017-09-22 08:07:25 +02001029 if (!h2c->ddht)
1030 goto fail;
1031
1032 /* Initialise the context. */
1033 h2c->st0 = H2_CS_PREFACE;
1034 h2c->conn = conn;
Willy Tarreauca1027c2023-04-18 15:57:03 +02001035 h2c->streams_limit = h2c_max_concurrent_streams(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001036 h2c->max_id = -1;
1037 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +01001038 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001039 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +01001040 h2c->nb_streams = 0;
Willy Tarreau36c22322022-05-27 10:41:24 +02001041 h2c->nb_sc = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001042 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001043 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001044
Christopher Faulet51f73eb2019-04-08 11:22:47 +02001045 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001046 h2c->dsi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001047
Willy Tarreau32218eb2017-09-22 08:07:25 +02001048 h2c->last_sid = -1;
1049
Willy Tarreau51330962019-05-26 09:38:07 +02001050 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +02001051 h2c->miw = 65535; /* mux initial window size */
1052 h2c->mws = 65535; /* mux window size */
1053 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +02001054 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001055 LIST_INIT(&h2c->send_list);
1056 LIST_INIT(&h2c->fctl_list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +02001057 LIST_INIT(&h2c->blocked_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +01001058 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001059
Christopher Fauletf81ef032019-10-04 15:19:43 +02001060 conn->ctx = h2c;
1061
Willy Tarreau8e6f7492021-06-16 17:47:24 +02001062 TRACE_USER("new H2 connection", H2_EV_H2C_NEW, conn);
1063
Willy Tarreau3f133572017-10-31 19:21:06 +01001064 if (t)
1065 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +01001066
Willy Tarreau01b44822018-10-03 14:26:37 +02001067 if (h2c->flags & H2_CF_IS_BACK) {
1068 /* FIXME: this is temporary, for outgoing connections we need
1069 * to immediately allocate a stream until the code is modified
Willy Tarreau36c22322022-05-27 10:41:24 +02001070 * so that the caller calls ->attach(). For now the outgoing sc
Christopher Fauletf81ef032019-10-04 15:19:43 +02001071 * is stored as conn->ctx by the caller and saved in conn_ctx.
Willy Tarreau01b44822018-10-03 14:26:37 +02001072 */
1073 struct h2s *h2s;
1074
Christopher Fauletf81ef032019-10-04 15:19:43 +02001075 h2s = h2c_bck_stream_new(h2c, conn_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +02001076 if (!h2s)
1077 goto fail_stream;
1078 }
1079
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001080 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 2);
Willy Tarreau4781b152021-04-06 13:53:36 +02001081 HA_ATOMIC_INC(&h2c->px_counters->open_conns);
1082 HA_ATOMIC_INC(&h2c->px_counters->total_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001083
Willy Tarreau0f383582018-10-03 14:22:21 +02001084 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02001085 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001086 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001087 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +02001088 fail_stream:
1089 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +02001090 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001091 task_destroy(t);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02001092 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001093 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +02001094 fail_no_h2c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001095 if (!conn_is_back(conn))
1096 LIST_DEL_INIT(&conn->stopping_list);
Christopher Fauletf81ef032019-10-04 15:19:43 +02001097 conn->ctx = conn_ctx; /* restore saved ctx */
1098 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001099 return -1;
1100}
1101
Willy Tarreau751f2d02018-10-05 09:35:00 +02001102/* returns the next allocatable outgoing stream ID for the H2 connection, or
1103 * -1 if no more is allocatable.
1104 */
1105static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
1106{
1107 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +01001108
1109 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +02001110 id = -1;
1111 return id;
1112}
1113
Willy Tarreau2373acc2017-10-12 17:35:14 +02001114/* returns the stream associated with id <id> or NULL if not found */
1115static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
1116{
1117 struct eb32_node *node;
1118
Willy Tarreau751f2d02018-10-05 09:35:00 +02001119 if (id == 0)
1120 return (struct h2s *)h2_closed_stream;
1121
Willy Tarreau2a856182017-05-16 15:20:39 +02001122 if (id > h2c->max_id)
1123 return (struct h2s *)h2_idle_stream;
1124
Willy Tarreau2373acc2017-10-12 17:35:14 +02001125 node = eb32_lookup(&h2c->streams_by_id, id);
1126 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +02001127 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +02001128
1129 return container_of(node, struct h2s, by_id);
1130}
1131
Christopher Faulet73c12072019-04-08 11:23:22 +02001132/* release function. This one should be called to free all resources allocated
1133 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +02001134 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001135static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02001136{
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001137 struct connection *conn = h2c->conn;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001138
Willy Tarreau7838a792019-08-12 18:42:03 +02001139 TRACE_ENTER(H2_EV_H2C_END);
1140
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001141 hpack_dht_free(h2c->ddht);
Christopher Faulet61840e72019-04-15 09:33:32 +02001142
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001143 if (LIST_INLIST(&h2c->buf_wait.list))
1144 LIST_DEL_INIT(&h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +02001145
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001146 h2_release_buf(h2c, &h2c->dbuf);
1147 h2_release_mbuf(h2c);
Willy Tarreau14398122017-09-22 14:26:04 +02001148
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001149 if (h2c->task) {
1150 h2c->task->context = NULL;
1151 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
1152 h2c->task = NULL;
1153 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02001154 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001155 if (conn && h2c->wait_event.events != 0)
1156 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
1157 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +01001158
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001159 HA_ATOMIC_DEC(&h2c->px_counters->open_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001160
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001161 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001162
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001163 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001164 if (!conn_is_back(conn))
1165 LIST_DEL_INIT(&conn->stopping_list);
1166
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001167 conn->mux = NULL;
1168 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02001169 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001170
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001171 conn_stop_tracking(conn);
Willy Tarreau0b222472021-10-21 22:24:31 +02001172
1173 /* there might be a GOAWAY frame still pending in the TCP
1174 * stack, and if the peer continues to send (i.e. window
1175 * updates etc), this can result in losing the GOAWAY. For
1176 * this reason we try to drain anything received in between.
1177 */
1178 conn->flags |= CO_FL_WANT_DRAIN;
1179
1180 conn_xprt_shutw(conn);
1181 conn_xprt_close(conn);
1182 conn_sock_shutw(conn, !conn_is_back(conn));
1183 conn_ctrl_close(conn);
1184
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001185 if (conn->destroy_cb)
1186 conn->destroy_cb(conn);
1187 conn_free(conn);
1188 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001189
1190 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +02001191}
1192
1193
Willy Tarreau71681172017-10-23 14:39:06 +02001194/******************************************************/
1195/* functions below are for the H2 protocol processing */
1196/******************************************************/
1197
1198/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +01001199static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +02001200{
1201 return h2s ? h2s->id : 0;
1202}
1203
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001204/* returns the sum of the stream's own window size and the mux's initial
1205 * window, which together form the stream's effective window size.
1206 */
1207static inline int h2s_mws(const struct h2s *h2s)
1208{
1209 return h2s->sws + h2s->h2c->miw;
1210}
1211
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001212/* marks an error on the connection. Before settings are sent, we must not send
1213 * a GOAWAY frame, and the error state will prevent h2c_send_goaway_error()
1214 * from verifying this so we set H2_CF_GOAWAY_FAILED to make sure it will not
1215 * even try.
1216 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001217static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +02001218{
Willy Tarreau022e5e52020-09-10 09:33:15 +02001219 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn, 0, 0, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +02001220 h2c->errcode = err;
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001221 if (h2c->st0 < H2_CS_SETTINGS1)
1222 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau741d6df2017-10-17 08:00:59 +02001223 h2c->st0 = H2_CS_ERROR;
1224}
1225
Willy Tarreau175cebb2019-01-24 10:02:24 +01001226/* marks an error on the stream. It may also update an already closed stream
1227 * (e.g. to report an error after an RST was received).
1228 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001229static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +02001230{
Willy Tarreau175cebb2019-01-24 10:02:24 +01001231 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001232 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s, 0, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +02001233 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +01001234 if (h2s->st < H2_SS_ERROR)
1235 h2s->st = H2_SS_ERROR;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001236 se_fl_set_error(h2s->sd);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001237 }
1238}
1239
Willy Tarreau7e094452018-12-19 18:08:52 +01001240/* attempt to notify the data layer of recv availability */
1241static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1242{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001243 if (h2s->subs && h2s->subs->events & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001244 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001245 tasklet_wakeup(h2s->subs->tasklet);
1246 h2s->subs->events &= ~SUB_RETRY_RECV;
1247 if (!h2s->subs->events)
1248 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001249 }
1250}
1251
1252/* attempt to notify the data layer of send availability */
1253static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1254{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001255 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001256 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01001257 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01001258 tasklet_wakeup(h2s->subs->tasklet);
1259 h2s->subs->events &= ~SUB_RETRY_SEND;
1260 if (!h2s->subs->events)
1261 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001262 }
Willy Tarreau5723f292020-01-10 15:16:57 +01001263 else if (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) {
1264 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
1265 tasklet_wakeup(h2s->shut_tl);
1266 }
Willy Tarreau7e094452018-12-19 18:08:52 +01001267}
1268
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001269/* alerts the data layer, trying to wake it up by all means, following
1270 * this sequence :
1271 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1272 * - if its subscribed to send, then it's woken up for send
1273 * - if it was subscribed to neither, its ->wake() callback is called
1274 * It is safe to call this function with a closed stream which doesn't have a
Willy Tarreau4596fe22022-05-17 19:07:51 +02001275 * stream connector anymore.
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001276 */
1277static void __maybe_unused h2s_alert(struct h2s *h2s)
1278{
Willy Tarreau7838a792019-08-12 18:42:03 +02001279 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1280
Willy Tarreauf96508a2020-01-10 11:12:48 +01001281 if (h2s->subs ||
Willy Tarreau5723f292020-01-10 15:16:57 +01001282 (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW))) {
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001283 h2s_notify_recv(h2s);
1284 h2s_notify_send(h2s);
1285 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02001286 else if (h2s_sc(h2s) && h2s_sc(h2s)->app_ops->wake != NULL) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001287 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02001288 h2s_sc(h2s)->app_ops->wake(h2s_sc(h2s));
Willy Tarreau7838a792019-08-12 18:42:03 +02001289 }
1290
1291 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001292}
1293
Willy Tarreaue4820742017-07-27 13:37:23 +02001294/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001295static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001296{
1297 uint8_t *out = frame;
1298
1299 *out = len >> 16;
1300 write_n16(out + 1, len);
1301}
1302
Willy Tarreau54c15062017-10-10 17:10:03 +02001303/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1304 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1305 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001306 * available in the buffer's input prior to calling this function. The buffer
1307 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001308 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001309static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001310 const struct buffer *b, int o)
1311{
Willy Tarreau591d4452018-06-15 17:21:00 +02001312 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 +02001313}
1314
Willy Tarreau1f094672017-11-20 21:27:45 +01001315static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001316{
Willy Tarreau591d4452018-06-15 17:21:00 +02001317 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001318}
1319
Willy Tarreau1f094672017-11-20 21:27:45 +01001320static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001321{
Willy Tarreau591d4452018-06-15 17:21:00 +02001322 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001323}
1324
Willy Tarreau1f094672017-11-20 21:27:45 +01001325static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001326{
Willy Tarreau591d4452018-06-15 17:21:00 +02001327 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001328}
1329
1330
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001331/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1332 * The algorithm is not obvious. It turns out that H2 headers are neither
1333 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1334 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001335 *
1336 * b0 b1 b2 b3 b4 b5..b8
1337 * +----------+---------+--------+----+----+----------------------+
1338 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1339 * +----------+---------+--------+----+----+----------------------+
1340 *
1341 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1342 * we get the sid properly aligned and ordered, and 16 bits of len properly
1343 * ordered as well. The type and flags can be extracted using bit shifts from
1344 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001345 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1346 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001347 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001348static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001349{
1350 uint64_t w;
1351
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001352 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001353 return 0;
1354
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001355 w = h2_get_n64(b, o + 1);
1356 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001357 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1358 h->ff = w >> 32;
1359 h->ft = w >> 40;
1360 h->len += w >> 48;
1361 return 1;
1362}
1363
1364/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1365 * h2_peek_frame_hdr() above.
1366 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001367static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001368{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001369 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001370}
1371
1372/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001373static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001374{
1375 int ret;
1376
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001377 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001378 if (ret > 0)
1379 h2_skip_frame_hdr(b);
1380 return ret;
1381}
1382
Willy Tarreaucb985a42019-10-07 16:56:34 +02001383
1384/* try to fragment the headers frame present at the beginning of buffer <b>,
1385 * enforcing a limit of <mfs> bytes per frame. Returns 0 on failure, 1 on
1386 * success. Typical causes of failure include a buffer not large enough to
1387 * add extra frame headers. The existing frame size is read in the current
1388 * frame. Its EH flag will be cleared if CONTINUATION frames need to be added,
1389 * and its length will be adjusted. The stream ID for continuation frames will
1390 * be copied from the initial frame's.
1391 */
1392static int h2_fragment_headers(struct buffer *b, uint32_t mfs)
1393{
1394 size_t remain = b->data - 9;
1395 int extra_frames = (remain - 1) / mfs;
1396 size_t fsize;
1397 char *fptr;
1398 int frame;
1399
1400 if (b->data <= mfs + 9)
1401 return 1;
1402
1403 /* Too large a frame, we need to fragment it using CONTINUATION
1404 * frames. We start from the end and move tails as needed.
1405 */
1406 if (b->data + extra_frames * 9 > b->size)
1407 return 0;
1408
1409 for (frame = extra_frames; frame; frame--) {
1410 fsize = ((remain - 1) % mfs) + 1;
1411 remain -= fsize;
1412
1413 /* move data */
1414 fptr = b->area + 9 + remain + (frame - 1) * 9;
1415 memmove(fptr + 9, b->area + 9 + remain, fsize);
1416 b->data += 9;
1417
1418 /* write new frame header */
1419 h2_set_frame_size(fptr, fsize);
1420 fptr[3] = H2_FT_CONTINUATION;
1421 fptr[4] = (frame == extra_frames) ? H2_F_HEADERS_END_HEADERS : 0;
1422 write_n32(fptr + 5, read_n32(b->area + 5));
1423 }
1424
1425 b->area[4] &= ~H2_F_HEADERS_END_HEADERS;
1426 h2_set_frame_size(b->area, remain);
1427 return 1;
1428}
1429
1430
Willy Tarreau00dd0782018-03-01 16:31:34 +01001431/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1432 * its connection if the stream was not yet closed. Please use this exclusively
Willy Tarreaubcdc6cc2023-03-20 19:14:47 +01001433 * before closing a stream to ensure stream count is well maintained. Note that
1434 * it does explicitly support being called with a partially initialized h2s
1435 * (e.g. sd==NULL).
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001436 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001437static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001438{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001439 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001440 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001441 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001442 if (!h2s->id)
1443 h2s->h2c->nb_reserved--;
Willy Tarreaubcdc6cc2023-03-20 19:14:47 +01001444 if (h2s->sd && h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001445 if (!se_fl_test(h2s->sd, SE_FL_EOS) && !b_data(&h2s->rxbuf))
Willy Tarreaua27db382019-03-25 18:13:16 +01001446 h2s_notify_recv(h2s);
1447 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001448 HA_ATOMIC_DEC(&h2s->h2c->px_counters->open_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001449
Willy Tarreau7838a792019-08-12 18:42:03 +02001450 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001451 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001452 h2s->st = H2_SS_CLOSED;
1453}
1454
Christopher Faulet531dd052023-05-24 11:14:38 +02001455/* Check h2c and h2s flags to evaluate if EOI/EOS/ERR_PENDING/ERROR flags must
1456 * be set on the SE.
1457 */
1458static inline void h2s_propagate_term_flags(struct h2c *h2c, struct h2s *h2s)
1459{
1460 if (h2s->flags & H2_SF_ES_RCVD) {
1461 se_fl_set(h2s->sd, SE_FL_EOI);
1462 /* Add EOS flag for tunnel */
1463 if (h2s->flags & H2_SF_BODY_TUNNEL)
1464 se_fl_set(h2s->sd, SE_FL_EOS);
1465 }
1466 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED) {
1467 se_fl_set(h2s->sd, SE_FL_EOS);
1468 if (!se_fl_test(h2s->sd, SE_FL_EOI))
1469 se_fl_set(h2s->sd, SE_FL_ERROR);
1470 }
1471 if (se_fl_test(h2s->sd, SE_FL_ERR_PENDING))
1472 se_fl_set(h2s->sd, SE_FL_ERROR);
1473}
1474
Willy Tarreau71049cc2018-03-28 13:56:39 +02001475/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001476/* h2s_destroy should only ever be called by the thread that owns the stream,
1477 * that means that a tasklet should be used if we want to destroy the h2s
1478 * from another thread
1479 */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001480static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001481{
Willy Tarreau7838a792019-08-12 18:42:03 +02001482 struct connection *conn = h2s->h2c->conn;
1483
1484 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1485
Willy Tarreau0a10de62018-03-01 16:27:53 +01001486 h2s_close(h2s);
1487 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001488 if (b_size(&h2s->rxbuf)) {
1489 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001490 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02001491 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001492
1493 if (h2s->subs)
1494 h2s->subs->events = 0;
1495
Joseph Herlantd77575d2018-11-25 10:54:45 -08001496 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001497 * reference left would be in the h2c send_list/fctl_list, and if
1498 * we're in it, we're getting out anyway
1499 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02001500 h2_remove_from_list(h2s);
Willy Tarreau5723f292020-01-10 15:16:57 +01001501
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001502 /* ditto, calling tasklet_free() here should be ok */
Willy Tarreau5723f292020-01-10 15:16:57 +01001503 tasklet_free(h2s->shut_tl);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001504 BUG_ON(h2s->sd && !se_fl_test(h2s->sd, SE_FL_ORPHAN));
1505 sedesc_free(h2s->sd);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001506 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001507
1508 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001509}
1510
Willy Tarreaua8e49542018-10-03 18:53:55 +02001511/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1512 * stream tree. In case of error, nothing is added and NULL is returned. The
1513 * causes of errors can be any failed memory allocation. The caller is
1514 * responsible for checking if the connection may support an extra stream
1515 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001516 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001517static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001518{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001519 struct h2s *h2s;
1520
Willy Tarreau7838a792019-08-12 18:42:03 +02001521 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1522
Willy Tarreaubafbe012017-11-24 17:34:44 +01001523 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001524 if (!h2s)
1525 goto out;
1526
Willy Tarreau5723f292020-01-10 15:16:57 +01001527 h2s->shut_tl = tasklet_new();
1528 if (!h2s->shut_tl) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001529 pool_free(pool_head_h2s, h2s);
1530 goto out;
1531 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001532 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01001533 h2s->shut_tl->process = h2_deferred_shut;
1534 h2s->shut_tl->context = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001535 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001536 h2s->h2c = h2c;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001537 h2s->sd = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001538 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001539 h2s->flags = H2_SF_NONE;
1540 h2s->errcode = H2_ERR_NO_ERROR;
1541 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001542 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001543 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001544 h2s->rxbuf = BUF_NULL;
Amaury Denoyelle74162742020-12-11 17:53:05 +01001545 memset(h2s->upgrade_protocol, 0, sizeof(h2s->upgrade_protocol));
Willy Tarreau751f2d02018-10-05 09:35:00 +02001546
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001547 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001548 if (id > 0)
1549 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001550 else
1551 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001552
1553 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001554 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001555
Willy Tarreau4781b152021-04-06 13:53:36 +02001556 HA_ATOMIC_INC(&h2c->px_counters->open_streams);
1557 HA_ATOMIC_INC(&h2c->px_counters->total_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001558
Willy Tarreau7838a792019-08-12 18:42:03 +02001559 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001560 return h2s;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001561 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001562 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001563 return NULL;
1564}
1565
1566/* creates a new stream <id> on the h2c connection and returns it, or NULL in
Christopher Faulet7d013e72020-12-15 16:56:50 +01001567 * case of memory allocation error. <input> is used as input buffer for the new
1568 * stream. On success, it is transferred to the stream and the mux is no longer
1569 * responsible of it. On error, <input> is unchanged, thus the mux must still
1570 * take care of it.
Willy Tarreaua8e49542018-10-03 18:53:55 +02001571 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02001572static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id, struct buffer *input, uint32_t flags)
Willy Tarreaua8e49542018-10-03 18:53:55 +02001573{
1574 struct session *sess = h2c->conn->owner;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001575 struct h2s *h2s;
1576
Willy Tarreau7838a792019-08-12 18:42:03 +02001577 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1578
Willy Tarreauca1027c2023-04-18 15:57:03 +02001579 if (h2c->nb_streams >= h2c_max_concurrent_streams(h2c)) {
Willy Tarreaue872f752022-05-12 09:24:41 +02001580 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 +02001581 session_inc_http_req_ctr(sess);
1582 session_inc_http_err_ctr(sess);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001583 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001584 }
Willy Tarreaua8e49542018-10-03 18:53:55 +02001585
1586 h2s = h2s_new(h2c, id);
1587 if (!h2s)
Willy Tarreaue872f752022-05-12 09:24:41 +02001588 goto out_alloc;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001589
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001590 h2s->sd = sedesc_new();
1591 if (!h2s->sd)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001592 goto out_close;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001593 h2s->sd->se = h2s;
1594 h2s->sd->conn = h2c->conn;
1595 se_fl_set(h2s->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
Christopher Faulet4403cdf2023-05-04 15:49:12 +02001596 /* The request is not finished, don't expect data from the opposite side
1597 * yet
1598 */
Christopher Faulet78b1eb22023-05-24 11:44:53 +02001599 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 +02001600 se_expect_no_data(h2s->sd);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001601
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02001602 /* FIXME wrong analogy between ext-connect and websocket, this need to
1603 * be refine.
1604 */
1605 if (flags & H2_SF_EXT_CONNECT_RCVD)
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001606 se_fl_set(h2s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +01001607
Willy Tarreaud0de6772022-02-04 09:05:37 +01001608 /* The stream will record the request's accept date (which is either the
1609 * end of the connection's or the date immediately after the previous
1610 * request) and the idle time, which is the delay since the previous
1611 * request. We can set the value now, it will be copied by stream_new().
1612 */
Willy Tarreau69530f52023-04-28 09:16:15 +02001613 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001614
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001615 if (!sc_new_from_endp(h2s->sd, sess, input))
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001616 goto out_close;
Willy Tarreaucd6bb1a2022-05-10 15:00:03 +02001617
Willy Tarreau36c22322022-05-27 10:41:24 +02001618 h2c->nb_sc++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001619
Willy Tarreau590a0512018-09-05 11:56:48 +02001620 /* We want the accept date presented to the next stream to be the one
1621 * we have now, the handshake time to be null (since the next stream
1622 * is not delayed by a handshake), and the idle time to count since
1623 * right now.
1624 */
1625 sess->accept_date = date;
Willy Tarreau69530f52023-04-28 09:16:15 +02001626 sess->accept_ts = now_ns;
Willy Tarreau590a0512018-09-05 11:56:48 +02001627 sess->t_handshake = 0;
Willy Tarreaud0de6772022-02-04 09:05:37 +01001628 sess->t_idle = 0;
Willy Tarreau590a0512018-09-05 11:56:48 +02001629
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001630 /* OK done, the stream lives its own life now */
Willy Tarreau36c22322022-05-27 10:41:24 +02001631 if (h2_frt_has_too_many_sc(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001632 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001633 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001634 return h2s;
1635
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001636 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001637 h2s_destroy(h2s);
Willy Tarreaue872f752022-05-12 09:24:41 +02001638 out_alloc:
1639 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 +02001640 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001641 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001642 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001643 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001644}
1645
Willy Tarreau36c22322022-05-27 10:41:24 +02001646/* allocates a new stream associated to stream connector <sc> on the h2c
Willy Tarreau4596fe22022-05-17 19:07:51 +02001647 * connection and returns it, or NULL in case of memory allocation error or if
1648 * the highest possible stream ID was reached.
Willy Tarreau751f2d02018-10-05 09:35:00 +02001649 */
Willy Tarreau36c22322022-05-27 10:41:24 +02001650static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct stconn *sc, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001651{
1652 struct h2s *h2s = NULL;
1653
Willy Tarreau7838a792019-08-12 18:42:03 +02001654 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1655
Willy Tarreaue872f752022-05-12 09:24:41 +02001656 if (h2c->nb_streams >= h2c->streams_limit) {
1657 TRACE_ERROR("Aborting stream since negotiated limit is too low", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001658 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001659 }
Willy Tarreau751f2d02018-10-05 09:35:00 +02001660
Willy Tarreaue872f752022-05-12 09:24:41 +02001661 if (h2_streams_left(h2c) < 1) {
1662 TRACE_ERROR("Aborting stream since no more streams left", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreaua80dca82019-01-24 17:08:28 +01001663 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001664 }
Willy Tarreaua80dca82019-01-24 17:08:28 +01001665
Willy Tarreau751f2d02018-10-05 09:35:00 +02001666 /* Defer choosing the ID until we send the first message to create the stream */
1667 h2s = h2s_new(h2c, 0);
Willy Tarreaue872f752022-05-12 09:24:41 +02001668 if (!h2s) {
1669 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001670 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001671 }
Willy Tarreau751f2d02018-10-05 09:35:00 +02001672
Willy Tarreau36c22322022-05-27 10:41:24 +02001673 if (sc_attach_mux(sc, h2s, h2c->conn) < 0) {
Willy Tarreaue872f752022-05-12 09:24:41 +02001674 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW, h2c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +02001675 h2s_destroy(h2s);
1676 h2s = NULL;
1677 goto out;
1678 }
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001679 h2s->sd = sc->sedesc;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001680 h2s->sess = sess;
Willy Tarreau36c22322022-05-27 10:41:24 +02001681 h2c->nb_sc++;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001682
Willy Tarreau54310dc2024-01-12 18:36:57 +01001683 /* on the backend we can afford to only count total streams upon success */
1684 h2c->stream_cnt++;
1685
Willy Tarreau751f2d02018-10-05 09:35:00 +02001686 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001687 if (likely(h2s))
1688 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1689 else
1690 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001691 return h2s;
1692}
1693
Willy Tarreaube5b7152017-09-25 16:25:39 +02001694/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1695 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1696 * the various settings codes.
1697 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001698static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001699{
1700 struct buffer *res;
1701 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001702 struct buffer buf;
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001703 int iws;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001704 int mfs;
Willy Tarreauca1027c2023-04-18 15:57:03 +02001705 int mcs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001706 int ret = 0;
1707
1708 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001709
Willy Tarreaube5b7152017-09-25 16:25:39 +02001710 chunk_init(&buf, buf_data, sizeof(buf_data));
1711 chunk_memcpy(&buf,
1712 "\x00\x00\x00" /* length : 0 for now */
1713 "\x04\x00" /* type : 4 (settings), flags : 0 */
1714 "\x00\x00\x00\x00", /* stream ID : 0 */
1715 9);
1716
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001717 if (h2c->flags & H2_CF_IS_BACK) {
1718 /* send settings_enable_push=0 */
1719 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1720 }
1721
Amaury Denoyellebefeae82021-07-09 17:14:30 +02001722 /* rfc 8441 #3 SETTINGS_ENABLE_CONNECT_PROTOCOL=1,
1723 * sent automatically unless disabled in the global config */
1724 if (!(global.tune.options & GTUNE_DISABLE_H2_WEBSOCKET))
1725 chunk_memcat(&buf, "\x00\x08\x00\x00\x00\x01", 6);
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01001726
Willy Tarreaube5b7152017-09-25 16:25:39 +02001727 if (h2_settings_header_table_size != 4096) {
1728 char str[6] = "\x00\x01"; /* header_table_size */
1729
1730 write_n32(str + 2, h2_settings_header_table_size);
1731 chunk_memcat(&buf, str, 6);
1732 }
1733
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001734 iws = (h2c->flags & H2_CF_IS_BACK) ?
1735 h2_be_settings_initial_window_size:
1736 h2_fe_settings_initial_window_size;
1737 iws = iws ? iws : h2_settings_initial_window_size;
1738
1739 if (iws != 65535) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001740 char str[6] = "\x00\x04"; /* initial_window_size */
1741
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001742 write_n32(str + 2, iws);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001743 chunk_memcat(&buf, str, 6);
1744 }
1745
Willy Tarreauca1027c2023-04-18 15:57:03 +02001746 mcs = h2c_max_concurrent_streams(h2c);
1747 if (mcs != 0) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001748 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1749
1750 /* Note: 0 means "unlimited" for haproxy's config but not for
1751 * the protocol, so never send this value!
1752 */
Willy Tarreauca1027c2023-04-18 15:57:03 +02001753 write_n32(str + 2, mcs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001754 chunk_memcat(&buf, str, 6);
1755 }
1756
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001757 mfs = h2_settings_max_frame_size;
1758 if (mfs > global.tune.bufsize)
1759 mfs = global.tune.bufsize;
1760
1761 if (!mfs)
1762 mfs = global.tune.bufsize;
1763
1764 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001765 char str[6] = "\x00\x05"; /* max_frame_size */
1766
1767 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1768 * match bufsize - rewrite size, but at the moment it seems
1769 * that clients don't take care of it.
1770 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001771 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001772 chunk_memcat(&buf, str, 6);
1773 }
1774
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001775 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001776
1777 res = br_tail(h2c->mbuf);
1778 retry:
1779 if (!h2_get_buf(h2c, res)) {
1780 h2c->flags |= H2_CF_MUX_MALLOC;
1781 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001782 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001783 }
1784
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001785 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001786 if (unlikely(ret <= 0)) {
1787 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001788 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1789 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001790 h2c->flags |= H2_CF_MUX_MFULL;
1791 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001792 }
1793 else {
1794 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001795 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001796 }
1797 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001798 out:
1799 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001800 return ret;
1801}
1802
Willy Tarreau52eed752017-09-22 15:05:09 +02001803/* Try to receive a connection preface, then upon success try to send our
1804 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1805 * missing data. It may return an error in h2c.
1806 */
1807static int h2c_frt_recv_preface(struct h2c *h2c)
1808{
1809 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001810 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001811
Willy Tarreau7838a792019-08-12 18:42:03 +02001812 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1813
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001814 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001815
1816 if (unlikely(ret1 <= 0)) {
Christopher Fauletb5f7b522021-07-26 12:06:53 +02001817 if (!ret1)
1818 h2c->flags |= H2_CF_DEM_SHORT_READ;
Christopher Fauletff7925d2022-10-11 19:12:40 +02001819 if (ret1 < 0 || (h2c->flags & H2_CF_RCVD_SHUT)) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01001820 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 +02001821 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauee4684f2021-06-17 08:08:48 +02001822 if (b_data(&h2c->dbuf) ||
1823 !(((const struct session *)h2c->conn->owner)->fe->options & PR_O_IGNORE_PRB))
1824 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01001825 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001826 ret2 = 0;
1827 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001828 }
1829
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001830 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001831 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001832 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001833 out:
1834 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001835 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001836}
1837
Willy Tarreau01b44822018-10-03 14:26:37 +02001838/* Try to send a connection preface, then upon success try to send our
1839 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1840 * missing data. It may return an error in h2c.
1841 */
1842static int h2c_bck_send_preface(struct h2c *h2c)
1843{
1844 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001845 int ret = 0;
1846
1847 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001848
Willy Tarreaubcc45952019-05-26 10:05:50 +02001849 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001850 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001851 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001852 h2c->flags |= H2_CF_MUX_MALLOC;
1853 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001854 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001855 }
1856
1857 if (!b_data(res)) {
1858 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001859 ret = b_istput(res, ist(H2_CONN_PREFACE));
1860 if (unlikely(ret <= 0)) {
1861 if (!ret) {
1862 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1863 goto retry;
1864 h2c->flags |= H2_CF_MUX_MFULL;
1865 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001866 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001867 }
1868 else {
1869 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001870 ret = 0;
1871 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001872 }
1873 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001874 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001875 ret = h2c_send_settings(h2c);
1876 out:
1877 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1878 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001879}
1880
Willy Tarreau081d4722017-05-16 21:51:05 +02001881/* try to send a GOAWAY frame on the connection to report an error or a graceful
1882 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1883 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1884 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1885 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1886 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1887 * on unrecoverable failure. It will not attempt to send one again in this last
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001888 * case, nor will it send one if settings were not sent (e.g. still waiting for
1889 * a preface) so that it is safe to use h2c_error() to report such errors.
Willy Tarreau081d4722017-05-16 21:51:05 +02001890 */
1891static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1892{
1893 struct buffer *res;
1894 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001895 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001896
Willy Tarreau7838a792019-08-12 18:42:03 +02001897 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1898
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001899 if ((h2c->flags & H2_CF_GOAWAY_FAILED) || h2c->st0 < H2_CS_SETTINGS1) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001900 ret = 1; // claim that it worked
1901 goto out;
1902 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001903
Willy Tarreau9c218e72019-05-26 10:08:28 +02001904 /* len: 8, type: 7, flags: none, sid: 0 */
1905 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1906
1907 if (h2c->last_sid < 0)
1908 h2c->last_sid = h2c->max_id;
1909
1910 write_n32(str + 9, h2c->last_sid);
1911 write_n32(str + 13, h2c->errcode);
1912
Willy Tarreaubcc45952019-05-26 10:05:50 +02001913 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001914 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001915 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001916 h2c->flags |= H2_CF_MUX_MALLOC;
1917 if (h2s)
1918 h2s->flags |= H2_SF_BLK_MROOM;
1919 else
1920 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001921 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001922 }
1923
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001924 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001925 if (unlikely(ret <= 0)) {
1926 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001927 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1928 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001929 h2c->flags |= H2_CF_MUX_MFULL;
1930 if (h2s)
1931 h2s->flags |= H2_SF_BLK_MROOM;
1932 else
1933 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001934 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001935 }
1936 else {
1937 /* we cannot report this error using GOAWAY, so we mark
1938 * it and claim a success.
1939 */
1940 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1941 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001942 ret = 1;
1943 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001944 }
1945 }
1946 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001947
1948 /* some codes are not for real errors, just attempts to close cleanly */
1949 switch (h2c->errcode) {
1950 case H2_ERR_NO_ERROR:
1951 case H2_ERR_ENHANCE_YOUR_CALM:
1952 case H2_ERR_REFUSED_STREAM:
1953 case H2_ERR_CANCEL:
1954 break;
1955 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001956 HA_ATOMIC_INC(&h2c->px_counters->goaway_resp);
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001957 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001958 out:
1959 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001960 return ret;
1961}
1962
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001963/* Try to send an RST_STREAM frame on the connection for the indicated stream
1964 * during mux operations. This stream must be valid and cannot be closed
1965 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1966 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1967 * not yet.
1968 *
1969 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1970 * to write the message, it subscribes the stream to future notifications.
1971 */
1972static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1973{
1974 struct buffer *res;
1975 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001976 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001977
Willy Tarreau7838a792019-08-12 18:42:03 +02001978 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1979
1980 if (!h2s || h2s->st == H2_SS_CLOSED) {
1981 ret = 1;
1982 goto out;
1983 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001984
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001985 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1986 * RST_STREAM in response to a RST_STREAM frame.
1987 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001988 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001989 ret = 1;
1990 goto ignore;
1991 }
1992
Willy Tarreau9c218e72019-05-26 10:08:28 +02001993 /* len: 4, type: 3, flags: none */
1994 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1995 write_n32(str + 5, h2s->id);
1996 write_n32(str + 9, h2s->errcode);
1997
Willy Tarreaubcc45952019-05-26 10:05:50 +02001998 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001999 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002000 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002001 h2c->flags |= H2_CF_MUX_MALLOC;
2002 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002003 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002004 }
2005
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002006 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002007 if (unlikely(ret <= 0)) {
2008 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002009 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2010 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002011 h2c->flags |= H2_CF_MUX_MFULL;
2012 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002013 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002014 }
2015 else {
2016 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002017 ret = 0;
2018 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002019 }
2020 }
2021
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002022 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002023 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01002024 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002025 out:
2026 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002027 return ret;
2028}
2029
2030/* Try to send an RST_STREAM frame on the connection for the stream being
2031 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01002032 * error code, even if the stream is one of the dummy ones, and will update
2033 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002034 *
2035 * Returns > 0 on success or zero if nothing was done. In case of lack of room
2036 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08002037 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002038 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02002039 */
2040static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
2041{
2042 struct buffer *res;
2043 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002044 int ret = 0;
2045
2046 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002047
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002048 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
2049 * RST_STREAM in response to a RST_STREAM frame.
2050 */
2051 if (h2c->dft == H2_FT_RST_STREAM) {
2052 ret = 1;
2053 goto ignore;
2054 }
2055
Willy Tarreau9c218e72019-05-26 10:08:28 +02002056 /* len: 4, type: 3, flags: none */
2057 memcpy(str, "\x00\x00\x04\x03\x00", 5);
2058
2059 write_n32(str + 5, h2c->dsi);
2060 write_n32(str + 9, h2s->errcode);
2061
Willy Tarreaubcc45952019-05-26 10:05:50 +02002062 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002063 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002064 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02002065 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002066 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002067 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002068 }
2069
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002070 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02002071 if (unlikely(ret <= 0)) {
2072 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002073 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2074 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002075 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002076 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002077 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002078 }
2079 else {
2080 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002081 ret = 0;
2082 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002083 }
2084 }
2085
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002086 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02002087 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02002088 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01002089 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002090 }
2091
Willy Tarreau7838a792019-08-12 18:42:03 +02002092 out:
Willy Tarreau4781b152021-04-06 13:53:36 +02002093 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_resp);
Willy Tarreau7838a792019-08-12 18:42:03 +02002094 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002095 return ret;
2096}
2097
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002098/* try to send an empty DATA frame with the ES flag set to notify about the
2099 * end of stream and match a shutdown(write). If an ES was already sent as
2100 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
2101 * on success or zero if nothing was done. In case of lack of room to write the
2102 * message, it subscribes the requesting stream to future notifications.
2103 */
2104static int h2_send_empty_data_es(struct h2s *h2s)
2105{
2106 struct h2c *h2c = h2s->h2c;
2107 struct buffer *res;
2108 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002109 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002110
Willy Tarreau7838a792019-08-12 18:42:03 +02002111 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
2112
2113 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
2114 ret = 1;
2115 goto out;
2116 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002117
Willy Tarreau9c218e72019-05-26 10:08:28 +02002118 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
2119 memcpy(str, "\x00\x00\x00\x00\x01", 5);
2120 write_n32(str + 5, h2s->id);
2121
Willy Tarreaubcc45952019-05-26 10:05:50 +02002122 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002123 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002124 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002125 h2c->flags |= H2_CF_MUX_MALLOC;
2126 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002127 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002128 }
2129
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002130 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002131 if (likely(ret > 0)) {
2132 h2s->flags |= H2_SF_ES_SENT;
2133 }
2134 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002135 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2136 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002137 h2c->flags |= H2_CF_MUX_MFULL;
2138 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002139 }
2140 else {
2141 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002142 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002143 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002144 out:
2145 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002146 return ret;
2147}
2148
Willy Tarreau4596fe22022-05-17 19:07:51 +02002149/* wake a specific stream and assign its stream connector some SE_FL_* flags
2150 * among SE_FL_ERR_PENDING and SE_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002151 * is automatically updated accordingly. If the stream is orphaned, it is
2152 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01002153 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002154static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01002155{
Willy Tarreau7838a792019-08-12 18:42:03 +02002156 struct h2c *h2c = h2s->h2c;
2157
2158 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
2159
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002160 if (!h2s_sc(h2s)) {
Christopher Fauletf02ca002019-03-07 16:21:34 +01002161 /* this stream was already orphaned */
2162 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002163 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002164 return;
2165 }
2166
Christopher Fauletaade4ed2020-10-08 15:38:41 +02002167 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002168 if (h2s->st == H2_SS_OPEN)
2169 h2s->st = H2_SS_HREM;
2170 else if (h2s->st == H2_SS_HLOC)
2171 h2s_close(h2s);
2172 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002173
Christopher Fauletff7925d2022-10-11 19:12:40 +02002174 if (h2s->h2c->st0 >= H2_CS_ERROR || (h2s->h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) ||
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002175 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02002176 se_fl_set_error(h2s->sd);
Willy Tarreau23482912019-05-07 15:23:14 +02002177
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002178 if (h2s->st < H2_SS_ERROR)
2179 h2s->st = H2_SS_ERROR;
2180 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01002181
2182 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002183 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002184}
2185
2186/* wake the streams attached to the connection, whose id is greater than <last>
2187 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002188 */
Willy Tarreau23482912019-05-07 15:23:14 +02002189static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002190{
2191 struct eb32_node *node;
2192 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002193
Willy Tarreau7838a792019-08-12 18:42:03 +02002194 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
2195
Christopher Fauletf02ca002019-03-07 16:21:34 +01002196 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002197 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
2198 while (node) {
2199 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002200 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002201 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002202 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002203
Christopher Fauletf02ca002019-03-07 16:21:34 +01002204 /* Wake all streams with unassigned ID (ID == 0) */
2205 node = eb32_lookup(&h2c->streams_by_id, 0);
2206 while (node) {
2207 h2s = container_of(node, struct h2s, by_id);
2208 if (h2s->id > 0)
2209 break;
2210 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002211 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002212 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002213
2214 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002215}
2216
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002217/* Wake up all blocked streams whose window size has become positive after the
2218 * mux's initial window was adjusted. This should be done after having processed
2219 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002220 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002221static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02002222{
2223 struct h2s *h2s;
2224 struct eb32_node *node;
2225
Willy Tarreau7838a792019-08-12 18:42:03 +02002226 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2227
Willy Tarreau3421aba2017-07-27 15:41:03 +02002228 node = eb32_first(&h2c->streams_by_id);
2229 while (node) {
2230 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002231 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002232 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002233 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002234 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2235 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002236 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002237 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002238 node = eb32_next(node);
2239 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002240
2241 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002242}
2243
2244/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
2245 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01002246 * return an error in h2c. The caller must have already verified frame length
2247 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002248 */
2249static int h2c_handle_settings(struct h2c *h2c)
2250{
2251 unsigned int offset;
2252 int error;
2253
Willy Tarreau7838a792019-08-12 18:42:03 +02002254 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
2255
Willy Tarreau3421aba2017-07-27 15:41:03 +02002256 if (h2c->dff & H2_F_SETTINGS_ACK) {
2257 if (h2c->dfl) {
2258 error = H2_ERR_FRAME_SIZE_ERROR;
2259 goto fail;
2260 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002261 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002262 }
2263
Willy Tarreau3421aba2017-07-27 15:41:03 +02002264 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002265 if (b_data(&h2c->dbuf) < h2c->dfl) {
2266 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002267 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002268 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002269
2270 /* parse the frame */
2271 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002272 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2273 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002274
2275 switch (type) {
2276 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2277 /* we need to update all existing streams with the
2278 * difference from the previous iws.
2279 */
2280 if (arg < 0) { // RFC7540#6.5.2
2281 error = H2_ERR_FLOW_CONTROL_ERROR;
2282 goto fail;
2283 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002284 h2c->miw = arg;
2285 break;
2286 case H2_SETTINGS_MAX_FRAME_SIZE:
2287 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002288 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 +02002289 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002290 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002291 goto fail;
2292 }
2293 h2c->mfs = arg;
2294 break;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01002295 case H2_SETTINGS_HEADER_TABLE_SIZE:
2296 h2c->flags |= H2_CF_SHTS_UPDATED;
2297 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002298 case H2_SETTINGS_ENABLE_PUSH:
2299 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002300 TRACE_ERROR("ENABLE_PUSH out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002301 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002302 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002303 goto fail;
2304 }
2305 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002306 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2307 if (h2c->flags & H2_CF_IS_BACK) {
2308 /* the limit is only for the backend; for the frontend it is our limit */
Willy Tarreauca1027c2023-04-18 15:57:03 +02002309 if ((unsigned int)arg > h2c_max_concurrent_streams(h2c))
2310 arg = h2c_max_concurrent_streams(h2c);
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002311 h2c->streams_limit = arg;
2312 }
2313 break;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002314 case H2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
Amaury Denoyelle0df04362021-10-18 09:43:29 +02002315 if (arg == 1)
2316 h2c->flags |= H2_CF_RCVD_RFC8441;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002317 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002318 }
2319 }
2320
2321 /* need to ACK this frame now */
2322 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002323 done:
2324 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002325 return 1;
2326 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002327 if (!(h2c->flags & H2_CF_IS_BACK))
2328 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002329 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002330 out0:
2331 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 +02002332 return 0;
2333}
2334
2335/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2336 * success or one of the h2_status values.
2337 */
2338static int h2c_ack_settings(struct h2c *h2c)
2339{
2340 struct buffer *res;
2341 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002342 int ret = 0;
2343
2344 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002345
Willy Tarreau9c218e72019-05-26 10:08:28 +02002346 memcpy(str,
2347 "\x00\x00\x00" /* length : 0 (no data) */
2348 "\x04" "\x01" /* type : 4, flags : ACK */
2349 "\x00\x00\x00\x00" /* stream ID */, 9);
2350
Willy Tarreaubcc45952019-05-26 10:05:50 +02002351 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002352 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002353 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002354 h2c->flags |= H2_CF_MUX_MALLOC;
2355 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002356 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002357 }
2358
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002359 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002360 if (unlikely(ret <= 0)) {
2361 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002362 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2363 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002364 h2c->flags |= H2_CF_MUX_MFULL;
2365 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002366 }
2367 else {
2368 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002369 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002370 }
2371 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002372 out:
2373 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002374 return ret;
2375}
2376
Willy Tarreaucf68c782017-10-10 17:11:41 +02002377/* processes a PING frame and schedules an ACK if needed. The caller must pass
2378 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002379 * missing data. The caller must have already verified frame length
2380 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002381 */
2382static int h2c_handle_ping(struct h2c *h2c)
2383{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002384 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002385 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002386 h2c->st0 = H2_CS_FRAME_A;
2387 return 1;
2388}
2389
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002390/* Try to send a window update for stream id <sid> and value <increment>.
2391 * Returns > 0 on success or zero on missing room or failure. It may return an
2392 * error in h2c.
2393 */
2394static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2395{
2396 struct buffer *res;
2397 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002398 int ret = 0;
2399
2400 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002401
Willy Tarreau9c218e72019-05-26 10:08:28 +02002402 /* length: 4, type: 8, flags: none */
2403 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2404 write_n32(str + 5, sid);
2405 write_n32(str + 9, increment);
2406
Willy Tarreaubcc45952019-05-26 10:05:50 +02002407 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002408 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002409 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002410 h2c->flags |= H2_CF_MUX_MALLOC;
2411 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002412 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002413 }
2414
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002415 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002416 if (unlikely(ret <= 0)) {
2417 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002418 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2419 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002420 h2c->flags |= H2_CF_MUX_MFULL;
2421 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002422 }
2423 else {
2424 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002425 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002426 }
2427 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002428 out:
2429 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002430 return ret;
2431}
2432
2433/* try to send pending window update for the connection. It's safe to call it
2434 * with no pending updates. Returns > 0 on success or zero on missing room or
2435 * failure. It may return an error in h2c.
2436 */
2437static int h2c_send_conn_wu(struct h2c *h2c)
2438{
2439 int ret = 1;
2440
Willy Tarreau7838a792019-08-12 18:42:03 +02002441 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2442
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002443 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002444 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002445
Willy Tarreau97aaa672018-12-23 09:49:04 +01002446 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2447 /* increase the advertised connection window to 2G on
2448 * first update.
2449 */
2450 h2c->flags |= H2_CF_WINDOW_OPENED;
2451 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2452 }
2453
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002454 /* send WU for the connection */
2455 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2456 if (ret > 0)
2457 h2c->rcvd_c = 0;
2458
Willy Tarreau7838a792019-08-12 18:42:03 +02002459 out:
2460 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002461 return ret;
2462}
2463
2464/* try to send pending window update for the current dmux stream. It's safe to
2465 * call it with no pending updates. Returns > 0 on success or zero on missing
2466 * room or failure. It may return an error in h2c.
2467 */
2468static int h2c_send_strm_wu(struct h2c *h2c)
2469{
2470 int ret = 1;
2471
Willy Tarreau7838a792019-08-12 18:42:03 +02002472 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2473
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002474 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002475 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002476
2477 /* send WU for the stream */
2478 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2479 if (ret > 0)
2480 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002481 out:
2482 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002483 return ret;
2484}
2485
Willy Tarreaucf68c782017-10-10 17:11:41 +02002486/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2487 * success, 0 on missing data or one of the h2_status values.
2488 */
2489static int h2c_ack_ping(struct h2c *h2c)
2490{
2491 struct buffer *res;
2492 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002493 int ret = 0;
2494
2495 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002496
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002497 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002498 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002499
Willy Tarreaucf68c782017-10-10 17:11:41 +02002500 memcpy(str,
2501 "\x00\x00\x08" /* length : 8 (same payload) */
2502 "\x06" "\x01" /* type : 6, flags : ACK */
2503 "\x00\x00\x00\x00" /* stream ID */, 9);
2504
2505 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002506 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002507
Willy Tarreau9c218e72019-05-26 10:08:28 +02002508 res = br_tail(h2c->mbuf);
2509 retry:
2510 if (!h2_get_buf(h2c, res)) {
2511 h2c->flags |= H2_CF_MUX_MALLOC;
2512 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002513 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002514 }
2515
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002516 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002517 if (unlikely(ret <= 0)) {
2518 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002519 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2520 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002521 h2c->flags |= H2_CF_MUX_MFULL;
2522 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002523 }
2524 else {
2525 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002526 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002527 }
2528 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002529 out:
2530 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002531 return ret;
2532}
2533
Willy Tarreau26f95952017-07-27 17:18:30 +02002534/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2535 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002536 * h2c or h2s. The caller must have already verified frame length and stream ID
2537 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002538 */
2539static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2540{
2541 int32_t inc;
2542 int error;
2543
Willy Tarreau7838a792019-08-12 18:42:03 +02002544 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2545
Willy Tarreau26f95952017-07-27 17:18:30 +02002546 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002547 if (b_data(&h2c->dbuf) < h2c->dfl) {
2548 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002549 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002550 }
Willy Tarreau26f95952017-07-27 17:18:30 +02002551
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002552 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002553
2554 if (h2c->dsi != 0) {
2555 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002556
2557 /* it's not an error to receive WU on a closed stream */
2558 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002559 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002560
2561 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002562 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 +02002563 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002564 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002565 goto strm_err;
2566 }
2567
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002568 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002569 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 +02002570 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002571 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002572 goto strm_err;
2573 }
2574
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002575 h2s->sws += inc;
2576 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002577 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002578 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002579 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2580 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002581 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002582 }
2583 }
2584 else {
2585 /* connection window update */
2586 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002587 TRACE_ERROR("conn WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002588 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002589 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002590 goto conn_err;
2591 }
2592
2593 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2594 error = H2_ERR_FLOW_CONTROL_ERROR;
2595 goto conn_err;
2596 }
2597
2598 h2c->mws += inc;
2599 }
2600
Willy Tarreau7838a792019-08-12 18:42:03 +02002601 done:
2602 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002603 return 1;
2604
2605 conn_err:
2606 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002607 out0:
2608 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 +02002609 return 0;
2610
2611 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002612 h2s_error(h2s, error);
2613 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002614 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002615 return 0;
2616}
2617
Willy Tarreaue96b0922017-10-30 00:28:29 +01002618/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002619 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2620 * have already verified frame length and stream ID validity. Described in
2621 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002622 */
2623static int h2c_handle_goaway(struct h2c *h2c)
2624{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002625 int last;
2626
Willy Tarreau7838a792019-08-12 18:42:03 +02002627 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002628 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002629 if (b_data(&h2c->dbuf) < h2c->dfl) {
2630 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002631 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002632 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002633 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002634
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002635 last = h2_get_n32(&h2c->dbuf, 0);
2636 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002637 if (h2c->last_sid < 0)
2638 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002639 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002640 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002641 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002642}
2643
Willy Tarreau92153fc2017-12-03 19:46:19 +01002644/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002645 * invalid. Returns > 0 on success or zero on missing data. It may return an
2646 * error in h2c. The caller must have already verified frame length and stream
2647 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002648 */
2649static int h2c_handle_priority(struct h2c *h2c)
2650{
Willy Tarreau7838a792019-08-12 18:42:03 +02002651 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2652
Willy Tarreau92153fc2017-12-03 19:46:19 +01002653 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002654 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002655 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002656 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002657 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002658 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002659
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002660 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002661 /* 7540#5.3 : can't depend on itself */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002662 TRACE_ERROR("PRIORITY depends on itself", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002663 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02002664 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002665 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002666 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002667 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002668 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002669 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002670}
2671
Willy Tarreaucd234e92017-08-18 10:59:39 +02002672/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002673 * Returns > 0 on success or zero on missing data. The caller must have already
2674 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002675 */
2676static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2677{
Willy Tarreau7838a792019-08-12 18:42:03 +02002678 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2679
Willy Tarreaucd234e92017-08-18 10:59:39 +02002680 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002681 if (b_data(&h2c->dbuf) < h2c->dfl) {
2682 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 +02002683 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002684 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002685 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002686
2687 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002688 if (h2s->st == H2_SS_CLOSED) {
2689 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 +02002690 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002691 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002692
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002693 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002694 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002695
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002696 if (h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02002697 se_fl_set_error(h2s->sd);
Willy Tarreauf830f012018-12-19 17:44:55 +01002698 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002699 }
2700
2701 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002702 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002703 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002704}
2705
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002706/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2707 * It may return an error in h2c or h2s. The caller must consider that the
2708 * return value is the new h2s in case one was allocated (most common case).
2709 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002710 * errors here are reported as connection errors since it's impossible to
2711 * recover from such errors after the compression context has been altered.
2712 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002713static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002714{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002715 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002716 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002717 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002718 int error;
2719
Willy Tarreau7838a792019-08-12 18:42:03 +02002720 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2721
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002722 if (!b_size(&h2c->dbuf)) {
2723 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002724 goto out; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002725 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002726
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002727 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2728 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002729 goto out; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002730 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002731
2732 /* now either the frame is complete or the buffer is complete */
2733 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002734 /* The stream exists/existed, this must be a trailers frame */
2735 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002736 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &body_len, NULL);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002737 /* unrecoverable error ? */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002738 if (h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau17c630b2023-01-19 23:58:11 +01002739 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 +01002740 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002741 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002742 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002743
Christopher Faulet485da0b2021-10-08 08:56:00 +02002744 if (error == 0) {
2745 /* Demux not blocked because of the stream, it is an incomplete frame */
2746 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2747 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002748 goto out; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002749 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002750
2751 if (error < 0) {
2752 /* Failed to decode this frame (e.g. too large request)
2753 * but the HPACK decompressor is still synchronized.
2754 */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002755 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002756 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau17c630b2023-01-19 23:58:11 +01002757 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 +02002758 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002759 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002760 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002761 goto done;
2762 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002763 /* the connection was already killed by an RST, let's consume
2764 * the data and send another RST.
2765 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002766 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002767 sess_log(h2c->conn->owner);
Willy Tarreau1f035502019-01-30 11:44:07 +01002768 h2s = (struct h2s*)h2_error_stream;
2769 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002770 }
2771 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2772 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2773 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002774 TRACE_ERROR("HEADERS on invalid stream ID", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau4781b152021-04-06 13:53:36 +02002775 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002776 sess_log(h2c->conn->owner);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02002777 session_inc_http_req_ctr(h2c->conn->owner);
2778 session_inc_http_err_ctr(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002779 goto conn_err;
2780 }
Willy Tarreau4869ed52023-10-13 18:11:59 +02002781 else if (h2c->flags & H2_CF_DEM_TOOMANY) {
Willy Tarreau36c22322022-05-27 10:41:24 +02002782 goto out; // IDLE but too many sc still present
Willy Tarreau4869ed52023-10-13 18:11:59 +02002783 }
2784 else if (h2_fe_max_total_streams &&
2785 h2c->stream_cnt >= h2_fe_max_total_streams + h2c_max_concurrent_streams(h2c)) {
2786 /* We've already told this client we were going to close a
2787 * while ago and apparently it didn't care, so it's time to
2788 * stop processing its requests for real.
2789 */
2790 error = H2_ERR_ENHANCE_YOUR_CALM;
2791 TRACE_STATE("Stream limit violated", H2_EV_STRM_SHUT, h2c->conn);
2792 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
2793 sess_log(h2c->conn->owner);
2794 session_inc_http_req_ctr(h2c->conn->owner);
2795 session_inc_http_err_ctr(h2c->conn->owner);
2796 goto conn_err;
2797 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002798
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002799 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002800
Willy Tarreau25919232019-01-03 14:48:18 +01002801 /* unrecoverable error ? */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002802 if (h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau54310dc2024-01-12 18:36:57 +01002803 /* This is mainly used for completeness, but a stream error is
2804 * currently not set by h2c_dec_hdrs().
2805 */
Willy Tarreau17c630b2023-01-19 23:58:11 +01002806 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 Tarreau54310dc2024-01-12 18:36:57 +01002807 goto strm_err;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002808 }
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002809
Willy Tarreau25919232019-01-03 14:48:18 +01002810 if (error <= 0) {
Christopher Faulet485da0b2021-10-08 08:56:00 +02002811 if (error == 0) {
2812 /* Demux not blocked because of the stream, it is an incomplete frame */
2813 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2814 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau25919232019-01-03 14:48:18 +01002815 goto out; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002816 }
Willy Tarreau25919232019-01-03 14:48:18 +01002817
2818 /* Failed to decode this stream (e.g. too large request)
2819 * but the HPACK decompressor is still synchronized.
2820 */
Willy Tarreau54310dc2024-01-12 18:36:57 +01002821 goto strm_err;
Willy Tarreau25919232019-01-03 14:48:18 +01002822 }
2823
Willy Tarreau29268e92021-06-17 08:29:14 +02002824 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn, 0, &rxbuf);
2825
Willy Tarreau198b5072022-05-12 09:08:51 +02002826 /* Now we cannot roll back and we won't come back here anymore for this
2827 * stream, this stream ID is open.
2828 */
2829 if (h2c->dsi > h2c->max_id)
2830 h2c->max_id = h2c->dsi;
Willy Tarreau54310dc2024-01-12 18:36:57 +01002831 h2c->stream_cnt++;
Willy Tarreau198b5072022-05-12 09:08:51 +02002832
Willy Tarreau22de8d32018-09-05 19:55:58 +02002833 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002834 * positively from h2c_frt_stream_new(), the stream will report the error,
2835 * and if we return in error, h2c_frt_stream_new() will emit the error.
Christopher Faulet7d013e72020-12-15 16:56:50 +01002836 *
2837 * Xfer the rxbuf to the stream. On success, the new stream owns the
2838 * rxbuf. On error, it is released here.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002839 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02002840 h2s = h2c_frt_stream_new(h2c, h2c->dsi, &rxbuf, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002841 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002842 h2s = (struct h2s*)h2_refused_stream;
2843 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002844 }
2845
2846 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002847 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002848 h2s->body_len = body_len;
Christopher Fauletc2f1d0e2023-05-24 11:34:45 +02002849 h2s_propagate_term_flags(h2c, h2s);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002850
Willy Tarreau88d138e2019-01-02 19:38:14 +01002851 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002852 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002853 if (h2s->st == H2_SS_OPEN)
2854 h2s->st = H2_SS_HREM;
2855 else
2856 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002857 }
Willy Tarreau0d6e5d22023-02-20 17:05:10 +01002858 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002859 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002860
2861 conn_err:
2862 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002863 out:
2864 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002865 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002866 h2s = NULL;
2867 goto leave;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002868
Willy Tarreau54310dc2024-01-12 18:36:57 +01002869 strm_err:
2870 sess_log(h2c->conn->owner);
2871 session_inc_http_req_ctr(h2c->conn->owner);
2872 session_inc_http_err_ctr(h2c->conn->owner);
2873
2874 h2s = (struct h2s*)h2_error_stream;
2875
2876 /* This stream ID is now opened anyway until we send the RST on
2877 * it, it must not be reused.
2878 */
2879 if (h2c->dsi > h2c->max_id)
2880 h2c->max_id = h2c->dsi;
2881 h2c->stream_cnt++;
2882
Willy Tarreau96a10c22018-12-23 18:30:44 +01002883 send_rst:
2884 /* make the demux send an RST for the current stream. We may only
2885 * do this if we're certain that the HEADERS frame was properly
2886 * decompressed so that the HPACK decoder is still kept up to date.
2887 */
2888 h2_release_buf(h2c, &rxbuf);
2889 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002890
Willy Tarreau022e5e52020-09-10 09:33:15 +02002891 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 +02002892 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002893
2894 leave:
2895 if (h2_fe_max_total_streams && h2c->stream_cnt >= h2_fe_max_total_streams) {
2896 /* we've had enough streams on this connection, time to renew it.
2897 * In order to gracefully do this, we'll advertise a stream limit
2898 * of the current one plus the max concurrent streams value in the
2899 * GOAWAY frame, so that we're certain that the client is aware of
2900 * the limit before creating a new stream, but knows we won't harm
2901 * the streams in flight. Remember that client stream IDs are odd
2902 * so we apply twice the concurrent streams value to the current
2903 * ID.
2904 */
2905 if (h2c->last_sid <= 0 ||
2906 h2c->last_sid > h2c->max_id + 2 * h2c_max_concurrent_streams(h2c)) {
2907 /* not set yet or was too high */
2908 h2c->last_sid = h2c->max_id + 2 * h2c_max_concurrent_streams(h2c);
2909 h2c_send_goaway_error(h2c, NULL);
2910 }
2911 }
2912
Willy Tarreau96a10c22018-12-23 18:30:44 +01002913 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002914}
2915
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002916/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2917 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2918 * errors here are reported as connection errors since it's impossible to
2919 * recover from such errors after the compression context has been altered.
2920 */
2921static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2922{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002923 struct buffer rxbuf = BUF_NULL;
2924 unsigned long long body_len = 0;
2925 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002926 int error;
2927
Willy Tarreau7838a792019-08-12 18:42:03 +02002928 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2929
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002930 if (!b_size(&h2c->dbuf)) {
2931 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002932 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002933 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002934
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002935 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2936 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002937 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002938 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002939
Christopher Faulet6884aa32019-09-23 15:28:20 +02002940 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002941 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len, h2s->upgrade_protocol);
Christopher Faulet6884aa32019-09-23 15:28:20 +02002942 }
2943 else {
2944 /* the connection was already killed by an RST, let's consume
2945 * the data and send another RST.
2946 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002947 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Christopher Fauletea7a7782019-09-26 16:19:13 +02002948 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002949 h2c->st0 = H2_CS_FRAME_E;
2950 goto send_rst;
2951 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002952
Willy Tarreau25919232019-01-03 14:48:18 +01002953 /* unrecoverable error ? */
Willy Tarreau17c630b2023-01-19 23:58:11 +01002954 if (h2c->st0 >= H2_CS_ERROR) {
2955 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 +02002956 goto fail;
Willy Tarreau17c630b2023-01-19 23:58:11 +01002957 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002958
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002959 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2960 /* RFC7540#5.1 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002961 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 +01002962 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2963 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02002964 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002965 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002966 }
2967
Willy Tarreau25919232019-01-03 14:48:18 +01002968 if (error <= 0) {
Christopher Faulet485da0b2021-10-08 08:56:00 +02002969 if (error == 0) {
2970 /* Demux not blocked because of the stream, it is an incomplete frame */
2971 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2972 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002973 goto fail; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002974 }
Willy Tarreau25919232019-01-03 14:48:18 +01002975
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002976 /* stream error : send RST_STREAM */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002977 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 +01002978 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002979 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02002980 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002981 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002982 }
2983
Willy Tarreau95acc8b2022-05-27 16:14:10 +02002984 if (se_fl_test(h2s->sd, SE_FL_ERROR) && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002985 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002986 else if (h2s->flags & H2_SF_ES_RCVD) {
2987 if (h2s->st == H2_SS_OPEN)
2988 h2s->st = H2_SS_HREM;
2989 else if (h2s->st == H2_SS_HLOC)
2990 h2s_close(h2s);
2991 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002992
Christopher Fauletf95f8762021-01-22 11:59:07 +01002993 /* Unblock busy server h2s waiting for the response headers to validate
2994 * the tunnel establishment or the end of the response of an oborted
2995 * tunnel
2996 */
2997 if ((h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY)) == (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY) ||
2998 (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)) {
2999 TRACE_STATE("Unblock h2s blocked on tunnel establishment/abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3000 h2s->flags &= ~H2_SF_BLK_MBUSY;
3001 }
3002
Willy Tarreau9abb3172021-06-16 18:32:42 +02003003 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 +02003004 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003005 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02003006 fail:
3007 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
3008 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003009
3010 send_rst:
3011 /* make the demux send an RST for the current stream. We may only
3012 * do this if we're certain that the HEADERS frame was properly
3013 * decompressed so that the HPACK decoder is still kept up to date.
3014 */
3015 h2_release_buf(h2c, &rxbuf);
3016 h2c->st0 = H2_CS_FRAME_E;
3017
Willy Tarreau022e5e52020-09-10 09:33:15 +02003018 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 +02003019 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
3020 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003021}
3022
Willy Tarreau454f9052017-10-26 19:40:35 +02003023/* processes a DATA frame. Returns > 0 on success or zero on missing data.
3024 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
3025 */
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003026static int h2c_handle_data(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003027{
3028 int error;
3029
Willy Tarreau7838a792019-08-12 18:42:03 +02003030 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3031
Willy Tarreau454f9052017-10-26 19:40:35 +02003032 /* note that empty DATA frames are perfectly valid and sometimes used
3033 * to signal an end of stream (with the ES flag).
3034 */
3035
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003036 if (!b_size(&h2c->dbuf) && h2c->dfl) {
3037 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003038 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003039 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003040
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003041 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
3042 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003043 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003044 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003045
3046 /* now either the frame is complete or the buffer is complete */
3047
Willy Tarreau454f9052017-10-26 19:40:35 +02003048 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
3049 /* RFC7540#6.1 */
3050 error = H2_ERR_STREAM_CLOSED;
3051 goto strm_err;
3052 }
3053
Christopher Faulet35feba62023-09-13 16:21:58 +02003054 if (!(h2s->flags & H2_SF_HEADERS_RCVD)) {
3055 /* RFC9113#8.1: The header section must be received before the message content */
3056 TRACE_ERROR("Unexpected DATA frame before the message headers", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3057 error = H2_ERR_PROTOCOL_ERROR;
3058 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
3059 goto strm_err;
3060 }
Christopher Faulet4f09ec82019-06-19 09:25:58 +02003061 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003062 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003063 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 +01003064 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003065 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003066 goto strm_err;
3067 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003068 if (!(h2c->flags & H2_CF_IS_BACK) &&
3069 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT) &&
3070 ((h2c->dfl - h2c->dpl) || !(h2c->dff & H2_F_DATA_END_STREAM))) {
3071 /* a tunnel attempt was aborted but the client still try to send some raw data.
3072 * Thus the stream is closed with the CANCEL error. Here we take care it is not
3073 * an empty DATA Frame with the ES flag. The error is only handled if ES was
3074 * already sent to the client because depending on the scheduling, these data may
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003075 * have been sent before the server response but not handle here.
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003076 */
3077 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3078 error = H2_ERR_CANCEL;
3079 goto strm_err;
3080 }
Willy Tarreau1915ca22019-01-24 11:49:37 +01003081
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003082 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02003083 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003084
Willy Tarreau454f9052017-10-26 19:40:35 +02003085 /* call the upper layers to process the frame, then let the upper layer
3086 * notify the stream about any change.
3087 */
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003088 if (!h2s_sc(h2s)) {
Willy Tarreau082c4572019-08-06 10:11:02 +02003089 /* The upper layer has already closed, this may happen on
3090 * 4xx/redirects during POST, or when receiving a response
3091 * from an H2 server after the client has aborted.
3092 */
3093 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003094 goto strm_err;
3095 }
3096
Willy Tarreau8f650c32017-11-21 19:36:21 +01003097 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003098 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01003099
Willy Tarreau721c9742017-11-07 11:05:42 +01003100 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003101 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01003102 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02003103 }
3104
3105 /* check for completion : the callee will change this to FRAME_A or
3106 * FRAME_H once done.
3107 */
3108 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02003109 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02003110
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003111 /* last frame */
3112 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02003113 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01003114 if (h2s->st == H2_SS_OPEN)
3115 h2s->st = H2_SS_HREM;
3116 else
3117 h2s_close(h2s);
3118
Willy Tarreau1915ca22019-01-24 11:49:37 +01003119 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
3120 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003121 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 +01003122 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003123 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003124 goto strm_err;
3125 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003126 }
3127
Christopher Fauletf95f8762021-01-22 11:59:07 +01003128 /* Unblock busy server h2s waiting for the end of the response for an
3129 * aborted tunnel
3130 */
3131 if ((h2c->flags & H2_CF_IS_BACK) &&
3132 (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)) {
3133 TRACE_STATE("Unblock h2s blocked on tunnel abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3134 h2s->flags &= ~H2_SF_BLK_MBUSY;
3135 }
3136
Willy Tarreau7838a792019-08-12 18:42:03 +02003137 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003138 return 1;
3139
Willy Tarreau454f9052017-10-26 19:40:35 +02003140 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01003141 h2s_error(h2s, error);
3142 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003143 fail:
3144 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 +02003145 return 0;
3146}
3147
Willy Tarreau63864812019-08-07 14:25:20 +02003148/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
3149 * valid for the current stream state. This is needed only after parsing the
3150 * frame header but in practice it can be performed at any time during
3151 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
3152 * or 0 in case of error, in which case either h2s or h2c will carry an error.
3153 */
3154static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
3155{
Willy Tarreau7838a792019-08-12 18:42:03 +02003156 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
3157
Willy Tarreau63864812019-08-07 14:25:20 +02003158 if (h2s->st == H2_SS_IDLE &&
3159 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
3160 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
3161 * this state MUST be treated as a connection error
3162 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003163 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 +02003164 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003165 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02003166 /* only log if no other stream can report the error */
3167 sess_log(h2c->conn->owner);
3168 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003169 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003170 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 +02003171 return 0;
3172 }
3173
Willy Tarreau57a18162019-11-24 14:57:53 +01003174 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
3175 /* only PUSH_PROMISE would be permitted here */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003176 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 +01003177 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003178 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau57a18162019-11-24 14:57:53 +01003179 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
3180 return 0;
3181 }
3182
Willy Tarreau63864812019-08-07 14:25:20 +02003183 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
3184 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
3185 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
3186 * this state MUST be treated as a stream error.
3187 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
3188 * PUSH_PROMISE/CONTINUATION cause connection errors.
3189 */
Amaury Denoyellea8879232020-10-27 17:16:03 +01003190 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003191 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 +02003192 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003193 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003194 }
3195 else {
Willy Tarreau63864812019-08-07 14:25:20 +02003196 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003197 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003198 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 +02003199 return 0;
3200 }
3201
3202 /* Below the management of frames received in closed state is a
3203 * bit hackish because the spec makes strong differences between
3204 * streams closed by receiving RST, sending RST, and seeing ES
3205 * in both directions. In addition to this, the creation of a
3206 * new stream reusing the identifier of a closed one will be
3207 * detected here. Given that we cannot keep track of all closed
3208 * streams forever, we consider that unknown closed streams were
3209 * closed on RST received, which allows us to respond with an
3210 * RST without breaking the connection (eg: to abort a transfer).
3211 * Some frames have to be silently ignored as well.
3212 */
3213 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
3214 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
3215 /* #5.1.1: The identifier of a newly
3216 * established stream MUST be numerically
3217 * greater than all streams that the initiating
3218 * endpoint has opened or reserved. This
3219 * governs streams that are opened using a
3220 * HEADERS frame and streams that are reserved
3221 * using PUSH_PROMISE. An endpoint that
3222 * receives an unexpected stream identifier
3223 * MUST respond with a connection error.
3224 */
3225 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003226 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 +02003227 return 0;
3228 }
3229
Willy Tarreau4c08f122019-09-26 08:47:15 +02003230 if (h2s->flags & H2_SF_RST_RCVD &&
3231 !(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 +02003232 /* RFC7540#5.1:closed: an endpoint that
3233 * receives any frame other than PRIORITY after
3234 * receiving a RST_STREAM MUST treat that as a
3235 * stream error of type STREAM_CLOSED.
3236 *
3237 * Note that old streams fall into this category
3238 * and will lead to an RST being sent.
3239 *
3240 * However, we cannot generalize this to all frame types. Those
3241 * carrying compression state must still be processed before
3242 * being dropped or we'll desynchronize the decoder. This can
3243 * happen with request trailers received after sending an
3244 * RST_STREAM, or with header/trailers responses received after
3245 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02003246 *
3247 * In addition, since our CLOSED streams always carry the
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003248 * RST_RCVD bit, we don't want to accidentally catch valid
Willy Tarreau4c08f122019-09-26 08:47:15 +02003249 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02003250 */
3251 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3252 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003253 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 +02003254 return 0;
3255 }
3256
3257 /* RFC7540#5.1:closed: if this state is reached as a
3258 * result of sending a RST_STREAM frame, the peer that
3259 * receives the RST_STREAM might have already sent
3260 * frames on the stream that cannot be withdrawn. An
3261 * endpoint MUST ignore frames that it receives on
3262 * closed streams after it has sent a RST_STREAM
3263 * frame. An endpoint MAY choose to limit the period
3264 * over which it ignores frames and treat frames that
3265 * arrive after this time as being in error.
3266 */
3267 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
3268 /* RFC7540#5.1:closed: any frame other than
3269 * PRIO/WU/RST in this state MUST be treated as
3270 * a connection error
3271 */
3272 if (h2c->dft != H2_FT_RST_STREAM &&
3273 h2c->dft != H2_FT_PRIORITY &&
3274 h2c->dft != H2_FT_WINDOW_UPDATE) {
3275 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003276 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 +02003277 return 0;
3278 }
3279 }
3280 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003281 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003282 return 1;
3283}
3284
Willy Tarreaubc933932017-10-09 16:21:43 +02003285/* process Rx frames to be demultiplexed */
3286static void h2_process_demux(struct h2c *h2c)
3287{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003288 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003289 struct h2_fh hdr;
3290 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003291 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02003292
Willy Tarreau7838a792019-08-12 18:42:03 +02003293 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3294
Willy Tarreau081d4722017-05-16 21:51:05 +02003295 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003296 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02003297
3298 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3299 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003300 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02003301 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02003302 goto out;
3303
Willy Tarreau52eed752017-09-22 15:05:09 +02003304 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
3305 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003306 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003307 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003308 h2c->st0 = H2_CS_ERROR2;
Willy Tarreauee4684f2021-06-17 08:08:48 +02003309 if (b_data(&h2c->dbuf) ||
Christopher Faulet3f35da22021-07-26 10:18:35 +02003310 !(((const struct session *)h2c->conn->owner)->fe->options & (PR_O_NULLNOLOG|PR_O_IGNORE_PRB)))
Willy Tarreauee4684f2021-06-17 08:08:48 +02003311 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003312 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003313 goto done;
Willy Tarreau52eed752017-09-22 15:05:09 +02003314 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003315 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003316
3317 h2c->max_id = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02003318 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaued2b9d92022-08-18 15:30:41 +02003319 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau52eed752017-09-22 15:05:09 +02003320 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003321
3322 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003323 /* ensure that what is pending is a valid SETTINGS frame
3324 * without an ACK.
3325 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003326 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 +02003327 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003328 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003329 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau22de8d32018-09-05 19:55:58 +02003330 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003331 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 +02003332 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003333 if (!(h2c->flags & H2_CF_IS_BACK))
3334 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003335 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003336 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003337 }
3338
3339 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
3340 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003341 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 +02003342 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3343 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003344 if (!(h2c->flags & H2_CF_IS_BACK))
3345 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003346 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003347 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003348 }
3349
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003350 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003351 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003352 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 +02003353 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3354 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003355 if (!(h2c->flags & H2_CF_IS_BACK))
3356 sess_log(h2c->conn->owner);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003357 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003358 }
3359
Willy Tarreau3bf69182018-12-21 15:34:50 +01003360 /* that's OK, switch to FRAME_P to process it. This is
3361 * a SETTINGS frame whose header has already been
3362 * deleted above.
3363 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01003364 padlen = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +02003365 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003366 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003367 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003368 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003369
3370 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02003371 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02003372 int ret = 0;
3373
Willy Tarreau7838a792019-08-12 18:42:03 +02003374 if (!b_data(&h2c->dbuf)) {
3375 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003376 h2c->flags |= H2_CF_DEM_SHORT_READ;
3377 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003378 }
3379
3380 if (h2c->st0 >= H2_CS_ERROR) {
3381 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003382 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003383 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003384
3385 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003386 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003387 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) {
3388 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003389 break;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003390 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003391
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003392 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003393 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 +02003394 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003395 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02003396 /* only log if no other stream can report the error */
3397 sess_log(h2c->conn->owner);
3398 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003399 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003400 break;
3401 }
3402
Willy Tarreau617592c2022-06-08 16:32:22 +02003403 if (h2c->rcvd_s && h2c->dsi != hdr.sid) {
3404 /* changed stream with a pending WU, need to
3405 * send it now.
3406 */
3407 TRACE_PROTO("sending stream WINDOW_UPDATE frame on stream switch", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
3408 ret = h2c_send_strm_wu(h2c);
3409 if (ret <= 0)
3410 break;
3411 }
3412
Christopher Fauletdd2a5622019-06-18 12:22:38 +02003413 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003414 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
3415 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
3416 * we read the pad length and drop it from the remaining
3417 * payload (one byte + the 9 remaining ones = 10 total
3418 * removed), so we have a frame payload starting after the
3419 * pad len. Flow controlled frames (DATA) also count the
3420 * padlen in the flow control, so it must be adjusted.
3421 */
3422 if (hdr.len < 1) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003423 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 +01003424 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003425 if (!(h2c->flags & H2_CF_IS_BACK))
3426 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003427 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003428 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003429 }
3430 hdr.len--;
3431
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003432 if (b_data(&h2c->dbuf) < 10) {
3433 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003434 break; // missing padlen
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003435 }
Willy Tarreau3bf69182018-12-21 15:34:50 +01003436
3437 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3438
3439 if (padlen > hdr.len) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003440 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 +01003441 /* RFC7540#6.1 : pad length = length of
3442 * frame payload or greater => error.
3443 */
3444 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003445 if (!(h2c->flags & H2_CF_IS_BACK))
3446 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003447 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003448 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003449 }
3450
3451 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3452 h2c->rcvd_c++;
3453 h2c->rcvd_s++;
3454 }
3455 b_del(&h2c->dbuf, 1);
3456 }
3457 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003458
3459 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003460 h2c->dfl = hdr.len;
3461 h2c->dsi = hdr.sid;
3462 h2c->dft = hdr.ft;
3463 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003464 h2c->dpl = padlen;
Willy Tarreau0f458712022-08-18 11:19:57 +02003465 h2c->flags |= H2_CF_DEM_IN_PROGRESS;
Willy Tarreau73db4342019-09-25 07:28:44 +02003466 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 +02003467 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003468
3469 /* check for minimum basic frame format validity */
3470 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3471 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003472 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 +01003473 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003474 if (!(h2c->flags & H2_CF_IS_BACK))
3475 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003476 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003477 goto done;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003478 }
Willy Tarreau15a47332022-03-18 15:57:34 +01003479
3480 /* transition to HEADERS frame ends the keep-alive idle
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003481 * timer and starts the http-request idle delay. It uses
3482 * the idle_start timer as well.
Willy Tarreau15a47332022-03-18 15:57:34 +01003483 */
3484 if (hdr.ft == H2_FT_HEADERS)
3485 h2c->idle_start = now_ms;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003486 }
3487
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003488 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3489 * H2_CS_FRAME_P indicates an incomplete previous operation
3490 * (most often the first attempt) and requires some validity
3491 * checks for the frame and the current state. The two other
3492 * ones are set after completion (or abortion) and must skip
3493 * validity checks.
3494 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003495 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3496
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003497 if (tmp_h2s != h2s && h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003498 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003499 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003500 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003501 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003502 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003503 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003504 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 +02003505 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003506 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003507 }
3508 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003509
Willy Tarreau63864812019-08-07 14:25:20 +02003510 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003511 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3512 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003513 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003514 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003515
Willy Tarreau7e98c052017-10-10 15:56:59 +02003516 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003517 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003518 if (h2c->st0 == H2_CS_FRAME_P) {
3519 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003520 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003521 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003522 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003523
Willy Tarreau7838a792019-08-12 18:42:03 +02003524 if (h2c->st0 == H2_CS_FRAME_A) {
3525 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 +02003526 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003527 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003528 break;
3529
Willy Tarreaucf68c782017-10-10 17:11:41 +02003530 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003531 if (h2c->st0 == H2_CS_FRAME_P) {
3532 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003533 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003534 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003535
Willy Tarreau7838a792019-08-12 18:42:03 +02003536 if (h2c->st0 == H2_CS_FRAME_A) {
3537 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 +02003538 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003539 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003540 break;
3541
Willy Tarreau26f95952017-07-27 17:18:30 +02003542 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003543 if (h2c->st0 == H2_CS_FRAME_P) {
3544 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 +02003545 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003546 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003547 break;
3548
Willy Tarreau61290ec2017-10-17 08:19:21 +02003549 case H2_FT_CONTINUATION:
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003550 /* RFC7540#6.10: CONTINUATION may only be preceded by
Willy Tarreauea18f862018-12-22 20:19:26 +01003551 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3552 * frames' parsers consume all following CONTINUATION
3553 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003554 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003555 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 +01003556 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003557 if (!(h2c->flags & H2_CF_IS_BACK))
3558 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003559 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003560 goto done;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003561
Willy Tarreau13278b42017-10-13 19:23:14 +02003562 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003563 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003564 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003565 if (h2c->flags & H2_CF_IS_BACK)
3566 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3567 else
3568 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003569 if (tmp_h2s) {
3570 h2s = tmp_h2s;
3571 ret = 1;
3572 }
3573 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003574 HA_ATOMIC_INC(&h2c->px_counters->headers_rcvd);
Willy Tarreau13278b42017-10-13 19:23:14 +02003575 break;
3576
Willy Tarreau454f9052017-10-26 19:40:35 +02003577 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003578 if (h2c->st0 == H2_CS_FRAME_P) {
3579 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003580 ret = h2c_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003581 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003582 HA_ATOMIC_INC(&h2c->px_counters->data_rcvd);
Willy Tarreau454f9052017-10-26 19:40:35 +02003583
Willy Tarreau7838a792019-08-12 18:42:03 +02003584 if (h2c->st0 == H2_CS_FRAME_A) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003585 /* rcvd_s will suffice to trigger the sending of a WU */
3586 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7838a792019-08-12 18:42:03 +02003587 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003588 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003589
Willy Tarreau92153fc2017-12-03 19:46:19 +01003590 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003591 if (h2c->st0 == H2_CS_FRAME_P) {
3592 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003593 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003594 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003595 break;
3596
Willy Tarreaucd234e92017-08-18 10:59:39 +02003597 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003598 if (h2c->st0 == H2_CS_FRAME_P) {
3599 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 +02003600 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003601 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003602 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_rcvd);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003603 break;
3604
Willy Tarreaue96b0922017-10-30 00:28:29 +01003605 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003606 if (h2c->st0 == H2_CS_FRAME_P) {
3607 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003608 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003609 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003610 HA_ATOMIC_INC(&h2c->px_counters->goaway_rcvd);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003611 break;
3612
Willy Tarreau1c661982017-10-30 13:52:01 +01003613 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003614 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003615 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003616 /* drop frames that we ignore. They may be larger than
3617 * the buffer so we drain all of their contents until
3618 * we reach the end.
3619 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003620 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3621 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003622 h2c->dfl -= ret;
3623 ret = h2c->dfl == 0;
3624 }
3625
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003626 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003627 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003628 if (h2s->st == H2_SS_ERROR) {
3629 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 +01003630 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003631 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003632
Willy Tarreau7838a792019-08-12 18:42:03 +02003633 if (h2c->st0 == H2_CS_FRAME_E) {
3634 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 +01003635 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003636 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003637
Willy Tarreau7e98c052017-10-10 15:56:59 +02003638 /* error or missing data condition met above ? */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003639 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02003640 break;
3641
3642 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaubba7a4d2020-09-18 07:41:28 +02003643 if (h2c->dfl)
3644 TRACE_DEVEL("skipping remaining frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
Christopher Faulet5112a602019-09-26 16:38:28 +02003645 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3646 b_del(&h2c->dbuf, ret);
3647 h2c->dfl -= ret;
3648 if (!h2c->dfl) {
Willy Tarreau0f458712022-08-18 11:19:57 +02003649 h2c->flags &= ~H2_CF_DEM_IN_PROGRESS;
Christopher Faulet5112a602019-09-26 16:38:28 +02003650 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3651 h2c->st0 = H2_CS_FRAME_H;
Christopher Faulet5112a602019-09-26 16:38:28 +02003652 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003653 }
3654 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003655
Willy Tarreau617592c2022-06-08 16:32:22 +02003656 if (h2c->rcvd_s > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003657 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003658 TRACE_PROTO("sending stream WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn, h2s);
3659 h2c_send_strm_wu(h2c);
3660 }
3661
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003662 if (h2c->rcvd_c > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003663 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003664 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003665 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003666 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003667
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003668 done:
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003669 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_DEM_SHORT_READ)) {
3670 if (h2c->flags & H2_CF_RCVD_SHUT)
3671 h2c->flags |= H2_CF_END_REACHED;
3672 }
3673
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003674 if (h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003675 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003676 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003677 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003678 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003679 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003680 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003681 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 +02003682 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003683 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003684 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003685
Willy Tarreau7838a792019-08-12 18:42:03 +02003686 if (old_iw != h2c->miw) {
3687 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003688 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003689 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003690
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003691 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003692 out:
3693 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003694 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02003695}
3696
Willy Tarreau989539b2020-01-10 17:01:29 +01003697/* resume each h2s eligible for sending in list head <head> */
3698static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3699{
3700 struct h2s *h2s, *h2s_back;
3701
3702 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3703
3704 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3705 if (h2c->mws <= 0 ||
3706 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3707 h2c->st0 >= H2_CS_ERROR)
3708 break;
3709
3710 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003711
Willy Tarreaud9464162020-01-10 18:25:07 +01003712 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003713 continue;
3714
Willy Tarreau5723f292020-01-10 15:16:57 +01003715 /* If the sender changed his mind and unsubscribed, let's just
3716 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003717 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003718 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3719 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003720 LIST_DEL_INIT(&h2s->list);
3721 continue;
3722 }
3723
Willy Tarreauf96508a2020-01-10 11:12:48 +01003724 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003725 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003726 tasklet_wakeup(h2s->subs->tasklet);
3727 h2s->subs->events &= ~SUB_RETRY_SEND;
3728 if (!h2s->subs->events)
3729 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003730 }
3731 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3732 tasklet_wakeup(h2s->shut_tl);
3733 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003734 }
3735
3736 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3737}
3738
Willy Tarreaude4a5382023-10-17 08:25:19 +02003739/* removes a stream from the list it may be in. If a stream has recently been
3740 * appended to the send_list, it might have been waiting on this one when
3741 * entering h2_snd_buf() and expecting it to complete before starting to send
3742 * in turn. For this reason we check (and clear) H2_CF_WAIT_INLIST to detect
3743 * this condition, and we try to resume sending streams if it happens. Note
3744 * that we don't need to do it for fctl_list as this list is relevant before
3745 * (only consulted after) a window update on the connection, and not because
3746 * of any competition with other streams.
3747 */
3748static inline void h2_remove_from_list(struct h2s *h2s)
3749{
3750 struct h2c *h2c = h2s->h2c;
3751
3752 if (!LIST_INLIST(&h2s->list))
3753 return;
3754
3755 LIST_DEL_INIT(&h2s->list);
3756 if (h2c->flags & H2_CF_WAIT_INLIST) {
3757 h2c->flags &= ~H2_CF_WAIT_INLIST;
3758 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
3759 }
3760}
3761
Willy Tarreaubc933932017-10-09 16:21:43 +02003762/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3763 * the end.
3764 */
3765static int h2_process_mux(struct h2c *h2c)
3766{
Willy Tarreau7838a792019-08-12 18:42:03 +02003767 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3768
Willy Tarreau01b44822018-10-03 14:26:37 +02003769 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3770 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3771 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3772 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003773 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003774 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003775 goto fail;
3776 }
3777 h2c->st0 = H2_CS_SETTINGS1;
3778 }
3779 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003780 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003781 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003782 }
3783
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003784 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003785 if (h2c->rcvd_s > 0 &&
3786 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3787 h2c_send_strm_wu(h2c) < 0)
3788 goto fail;
3789
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003790 if (h2c->rcvd_c > 0 &&
3791 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3792 h2c_send_conn_wu(h2c) < 0)
3793 goto fail;
3794
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003795 /* First we always process the flow control list because the streams
3796 * waiting there were already elected for immediate emission but were
3797 * blocked just on this.
3798 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02003799 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01003800 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3801 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003802
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003803 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003804 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003805 if (h2c->st0 == H2_CS_ERROR) {
3806 if (h2c->max_id >= 0) {
3807 h2c_send_goaway_error(h2c, NULL);
3808 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003809 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003810 }
3811
3812 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3813 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003814 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003815 done:
3816 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3817 return 1;
3818 out0:
3819 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3820 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003821}
3822
Willy Tarreau62f52692017-10-08 23:01:42 +02003823
Willy Tarreau479998a2018-11-18 06:30:59 +01003824/* Attempt to read data, and subscribe if none available.
3825 * The function returns 1 if data has been received, otherwise zero.
3826 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003827static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003828{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003829 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003830 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003831 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003832 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003833
Willy Tarreau7838a792019-08-12 18:42:03 +02003834 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3835
3836 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3837 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003838 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003839 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003840
Willy Tarreau7838a792019-08-12 18:42:03 +02003841 if (!h2_recv_allowed(h2c)) {
3842 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003843 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003844 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003845
Willy Tarreau44e973f2018-03-01 17:49:30 +01003846 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003847 if (!buf) {
3848 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003849 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003850 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003851 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003852
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003853 if (!b_data(buf)) {
3854 /* try to pre-align the buffer like the
3855 * rxbufs will be to optimize memory copies. We'll make
3856 * sure that the frame header lands at the end of the
3857 * HTX block to alias it upon recv. We cannot use the
3858 * head because rcv_buf() will realign the buffer if
3859 * it's empty. Thus we cheat and pretend we already
3860 * have a few bytes there.
3861 */
3862 max = buf_room_for_htx_data(buf) + 9;
3863 buf->head = sizeof(struct htx) - 9;
3864 }
3865 else
3866 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003867
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003868 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003869
Christopher Fauletde9d6052021-04-23 12:25:18 +02003870 if (max && !ret && h2_recv_allowed(h2c)) {
3871 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
3872 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003873 } else if (ret) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02003874 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn, 0, 0, (void*)(long)ret);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003875 h2c->flags &= ~H2_CF_DEM_SHORT_READ;
3876 }
Olivier Houchard81a15af2018-10-19 17:26:49 +02003877
Christopher Fauletde9d6052021-04-23 12:25:18 +02003878 if (conn_xprt_read0_pending(h2c->conn)) {
3879 TRACE_DATA("received read0", H2_EV_H2C_RECV, h2c->conn);
3880 h2c->flags |= H2_CF_RCVD_SHUT;
3881 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02003882 if (h2c->conn->flags & CO_FL_ERROR) {
3883 TRACE_DATA("connection error", H2_EV_H2C_RECV, h2c->conn);
3884 h2c->flags |= H2_CF_ERROR;
3885 }
Christopher Fauletde9d6052021-04-23 12:25:18 +02003886
Olivier Houcharda1411e62018-08-17 18:42:48 +02003887 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003888 h2_release_buf(h2c, &h2c->dbuf);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003889 goto end;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003890 }
3891
Willy Tarreau7838a792019-08-12 18:42:03 +02003892 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003893 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003894 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003895 }
3896
Christopher Fauletff7925d2022-10-11 19:12:40 +02003897 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02003898 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003899 return !!ret || (h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR));
Willy Tarreau62f52692017-10-08 23:01:42 +02003900}
3901
Willy Tarreau479998a2018-11-18 06:30:59 +01003902/* Try to send data if possible.
3903 * The function returns 1 if data have been sent, otherwise zero.
3904 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003905static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003906{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003907 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003908 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003909 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003910
Willy Tarreau7838a792019-08-12 18:42:03 +02003911 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003912
Christopher Fauletff7925d2022-10-11 19:12:40 +02003913 if (h2c->flags & (H2_CF_ERROR|H2_CF_ERR_PENDING)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003914 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003915 if (h2c->flags & H2_CF_RCVD_SHUT)
3916 h2c->flags |= H2_CF_ERROR;
3917 b_reset(br_tail(h2c->mbuf));
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003918 h2c->idle_start = now_ms;
Willy Tarreau7838a792019-08-12 18:42:03 +02003919 return 1;
3920 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003921
Willy Tarreaubc933932017-10-09 16:21:43 +02003922 /* This loop is quite simple : it tries to fill as much as it can from
3923 * pending streams into the existing buffer until it's reportedly full
3924 * or the end of send requests is reached. Then it tries to send this
3925 * buffer's contents out, marks it not full if at least one byte could
3926 * be sent, and tries again.
3927 *
3928 * The snd_buf() function normally takes a "flags" argument which may
3929 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3930 * data immediately comes and CO_SFL_STREAMER to indicate that the
3931 * connection is streaming lots of data (used to increase TLS record
3932 * size at the expense of latency). The former can be sent any time
3933 * there's a buffer full flag, as it indicates at least one stream
3934 * attempted to send and failed so there are pending data. An
3935 * alternative would be to set it as long as there's an active stream
3936 * but that would be problematic for ACKs until we have an absolute
3937 * guarantee that all waiters have at least one byte to send. The
3938 * latter should possibly not be set for now.
3939 */
3940
3941 done = 0;
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02003942 while (!(conn->flags & CO_FL_WAIT_XPRT) && !done) {
Willy Tarreaubc933932017-10-09 16:21:43 +02003943 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003944 unsigned int released = 0;
3945 struct buffer *buf;
Willy Tarreau464fa062023-03-17 16:09:14 +01003946 uint to_send;
Willy Tarreaubc933932017-10-09 16:21:43 +02003947
3948 /* fill as much as we can into the current buffer */
3949 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3950 done = h2_process_mux(h2c);
3951
Olivier Houchard2b094432019-01-29 18:28:36 +01003952 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003953 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003954
Christopher Faulet9a3d3fc2020-10-22 16:24:58 +02003955 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
Willy Tarreaue6dc7a02021-10-21 17:30:06 +02003956 (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02003957 break;
3958
Christopher Faulet68ee7842022-10-12 10:21:33 +02003959 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))
Willy Tarreaubc933932017-10-09 16:21:43 +02003960 flags |= CO_SFL_MSG_MORE;
3961
Willy Tarreau464fa062023-03-17 16:09:14 +01003962 to_send = br_count(h2c->mbuf);
3963 if (to_send > 1) {
Willy Tarreau14ea98a2023-03-16 17:30:30 +01003964 /* usually we want to emit small TLS records to speed
3965 * up the decoding on the client. That's what is being
3966 * done by default. However if there is more than one
3967 * buffer being allocated, we're streaming large data
3968 * so we stich to large records.
3969 */
3970 flags |= CO_SFL_STREAMER;
3971 }
3972
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003973 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3974 if (b_data(buf)) {
Willy Tarreau464fa062023-03-17 16:09:14 +01003975 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf),
3976 flags | (to_send > 1 ? CO_SFL_MSG_MORE : 0));
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003977 if (!ret) {
3978 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003979 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003980 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003981 sent = 1;
Willy Tarreau464fa062023-03-17 16:09:14 +01003982 to_send--;
Willy Tarreau022e5e52020-09-10 09:33:15 +02003983 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn, 0, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003984 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003985 if (b_data(buf)) {
3986 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003987 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003988 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003989 }
3990 b_free(buf);
3991 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003992 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003993
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003994 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003995 offer_buffers(NULL, released);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003996
Willy Tarreau93c55112023-03-16 16:47:44 +01003997 /* Normally if wrote at least one byte, the buffer is not full
3998 * anymore. However, if it was marked full because all of its
3999 * buffers were used, we don't want to instantly wake up many
4000 * streams because we'd create a thundering herd effect, notably
4001 * when data are flushed in small chunks. Instead we wait for
4002 * the buffer to be decongested again before allowing to send
4003 * again. It also has the added benefit of not pumping more
4004 * data from the other side when it's known that this one is
4005 * still congested.
4006 */
4007 if (sent && br_single(h2c->mbuf))
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02004008 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02004009 }
4010
Christopher Fauletff7925d2022-10-11 19:12:40 +02004011 if (conn->flags & CO_FL_ERROR) {
4012 h2c->flags |= H2_CF_ERR_PENDING;
4013 if (h2c->flags & H2_CF_RCVD_SHUT)
4014 h2c->flags |= H2_CF_ERROR;
Willy Tarreau51330962019-05-26 09:38:07 +02004015 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02004016 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02004017
Olivier Houchard6ff20392018-07-17 18:46:31 +02004018 /* We're not full anymore, so we can wake any task that are waiting
4019 * for us.
4020 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004021 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
4022 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01004023 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02004024 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01004025
Olivier Houchard910b2bc2018-07-17 18:49:38 +02004026 /* We're done, no more to send */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004027 if (!(conn->flags & CO_FL_WAIT_XPRT) && !br_data(h2c->mbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004028 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Willy Tarreau936db562023-10-18 11:39:43 +02004029 if (h2c->flags & H2_CF_MBUF_HAS_DATA && !h2c->nb_sc) {
4030 h2c->flags &= ~H2_CF_MBUF_HAS_DATA;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004031 h2c->idle_start = now_ms;
Willy Tarreau936db562023-10-18 11:39:43 +02004032 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02004033 goto end;
Willy Tarreau7838a792019-08-12 18:42:03 +02004034 }
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004035
Willy Tarreau7838a792019-08-12 18:42:03 +02004036 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
4037 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004038 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02004039 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004040 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02004041end:
4042 return sent || (h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR));
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004043}
4044
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004045/* this is the tasklet referenced in h2c->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004046struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004047{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004048 struct connection *conn;
4049 struct tasklet *tl = (struct tasklet *)t;
4050 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004051 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02004052 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004053
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004054 if (state & TASK_F_USR1) {
4055 /* the tasklet was idling on an idle connection, it might have
4056 * been stolen, let's be careful!
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004057 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004058 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4059 if (t->context == NULL) {
4060 /* The connection has been taken over by another thread,
4061 * we're no longer responsible for it, so just free the
4062 * tasklet, and do nothing.
4063 */
4064 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4065 tasklet_free(tl);
Willy Tarreau74163142021-03-13 11:30:19 +01004066 t = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004067 goto leave;
4068 }
4069 conn = h2c->conn;
4070 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004071
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004072 /* Remove the connection from the list, to be sure nobody attempts
4073 * to use it while we handle the I/O events
4074 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004075 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004076 if (conn_in_list)
4077 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004078
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004079 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4080 } else {
4081 /* we're certain the connection was not in an idle list */
4082 conn = h2c->conn;
4083 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4084 conn_in_list = 0;
4085 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004086
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004087 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02004088 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004089 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02004090 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01004091 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004092 ret = h2_process(h2c);
4093
4094 /* If we were in an idle list, we want to add it back into it,
4095 * unless h2_process() returned -1, which mean it has destroyed
4096 * the connection (testing !ret is enough, if h2_process() wasn't
4097 * called then ret will be 0 anyway.
4098 */
Willy Tarreau74163142021-03-13 11:30:19 +01004099 if (ret < 0)
4100 t = NULL;
4101
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004102 if (!ret && conn_in_list) {
4103 struct server *srv = objt_server(conn->target);
4104
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004105 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004106 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02004107 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004108 else
Willy Tarreau85223482022-09-29 20:32:43 +02004109 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004110 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004111 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004112
Willy Tarreau38468772020-06-28 00:31:13 +02004113leave:
Willy Tarreau7838a792019-08-12 18:42:03 +02004114 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreau74163142021-03-13 11:30:19 +01004115 return t;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004116}
Willy Tarreaua2af5122017-10-09 11:56:46 +02004117
Willy Tarreau62f52692017-10-08 23:01:42 +02004118/* callback called on any event by the connection handler.
4119 * It applies changes and returns zero, or < 0 if it wants immediate
4120 * destruction of the connection (which normally doesn not happen in h2).
4121 */
Olivier Houchard7505f942018-08-21 18:10:44 +02004122static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02004123{
Olivier Houchard7505f942018-08-21 18:10:44 +02004124 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02004125
Willy Tarreau7838a792019-08-12 18:42:03 +02004126 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4127
Willy Tarreauf0961222021-02-05 11:41:46 +01004128 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) &&
4129 (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01004130 h2_process_demux(h2c);
4131
Christopher Fauletff7925d2022-10-11 19:12:40 +02004132 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_ERROR))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004133 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004134
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004135 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01004136 h2c->flags &= ~H2_CF_DEM_DFULL;
4137 }
Olivier Houchard7505f942018-08-21 18:10:44 +02004138 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004139
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004140 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 +02004141 int send_goaway = 1;
4142 /* If a close-spread-time option is set, we want to avoid
4143 * closing all the active HTTP2 connections at once so we add a
4144 * random factor that will spread the closing.
4145 */
4146 if (tick_isset(global.close_spread_end)) {
4147 int remaining_window = tick_remain(now_ms, global.close_spread_end);
4148 if (remaining_window) {
4149 /* This should increase the closing rate the
4150 * further along the window we are. */
4151 send_goaway = (remaining_window <= statistical_prng_range(global.close_spread_time));
4152 }
4153 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02004154 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
4155 send_goaway = 0; /* let the client close his connection himself */
Willy Tarreau8ec14062017-12-30 18:08:13 +01004156 /* frontend is stopping, reload likely in progress, let's try
4157 * to announce a graceful shutdown if not yet done. We don't
4158 * care if it fails, it will be tried again later.
4159 */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02004160 if (send_goaway) {
4161 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
4162 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
4163 if (h2c->last_sid < 0)
4164 h2c->last_sid = (1U << 31) - 1;
4165 h2c_send_goaway_error(h2c, NULL);
4166 }
Willy Tarreau8ec14062017-12-30 18:08:13 +01004167 }
4168 }
4169
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004170 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004171 * If we received early data, and the handshake is done, wake
4172 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004173 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004174 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01004175 (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 +01004176 struct eb32_node *node;
4177 struct h2s *h2s;
4178
4179 h2c->flags |= H2_CF_WAIT_FOR_HS;
4180 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
4181
4182 while (node) {
4183 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004184 if (se_fl_test(h2s->sd, SE_FL_WAIT_FOR_HS))
Willy Tarreau7e094452018-12-19 18:08:52 +01004185 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004186 node = eb32_next(node);
4187 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004188 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004189
Christopher Fauletff7925d2022-10-11 19:12:40 +02004190 if ((h2c->flags & H2_CF_ERROR) || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01004191 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
4192 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
4193 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02004194 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004195
4196 if (eb_is_empty(&h2c->streams_by_id)) {
4197 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02004198 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004199 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004200 return -1;
4201 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004202
4203 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004204 if (conn->flags & CO_FL_LIST_MASK) {
4205 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004206 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004207 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4208 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004209 }
4210 else if (h2c->st0 == H2_CS_ERROR) {
4211 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004212 if (conn->flags & CO_FL_LIST_MASK) {
4213 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004214 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004215 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4216 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004217 }
4218
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004219 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01004220 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004221
Christopher Fauletff7925d2022-10-11 19:12:40 +02004222 if (h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Olivier Houchard53216e72018-10-10 15:46:36 +02004223 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02004224 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02004225 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
4226 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02004227 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02004228
Willy Tarreau15a47332022-03-18 15:57:34 +01004229 h2c_update_timeout(h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02004230 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004231 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004232 return 0;
4233}
4234
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004235/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004236static int h2_wake(struct connection *conn)
4237{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004238 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02004239 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004240
Willy Tarreau7838a792019-08-12 18:42:03 +02004241 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4242 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01004243 if (ret >= 0)
4244 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02004245 TRACE_LEAVE(H2_EV_H2C_WAKE);
4246 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004247}
4248
Willy Tarreauea392822017-10-31 10:02:25 +01004249/* Connection timeout management. The principle is that if there's no receipt
4250 * nor sending for a certain amount of time, the connection is closed. If the
4251 * MUX buffer still has lying data or is not allocatable, the connection is
4252 * immediately killed. If it's allocatable and empty, we attempt to send a
4253 * GOAWAY frame.
4254 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004255struct task *h2_timeout_task(struct task *t, void *context, unsigned int state)
Willy Tarreauea392822017-10-31 10:02:25 +01004256{
Olivier Houchard9f6af332018-05-25 14:04:04 +02004257 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01004258 int expired = tick_is_expired(t->expire, now_ms);
4259
Willy Tarreau7838a792019-08-12 18:42:03 +02004260 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
4261
Willy Tarreaubd42e922020-06-30 11:19:23 +02004262 if (h2c) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004263 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004264 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004265
4266 /* Somebody already stole the connection from us, so we should not
4267 * free it, we just have to free the task.
4268 */
4269 if (!t->context) {
4270 h2c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004271 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004272 goto do_leave;
4273 }
4274
4275
Willy Tarreaubd42e922020-06-30 11:19:23 +02004276 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004277 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004278 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
4279 return t;
4280 }
Willy Tarreauea392822017-10-31 10:02:25 +01004281
Willy Tarreaubd42e922020-06-30 11:19:23 +02004282 if (!h2c_may_expire(h2c)) {
4283 /* we do still have streams but all of them are idle, waiting
4284 * for the data layer, so we must not enforce the timeout here.
4285 */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004286 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004287 t->expire = TICK_ETERNITY;
4288 return t;
4289 }
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004290
Willy Tarreaubd42e922020-06-30 11:19:23 +02004291 /* We're about to destroy the connection, so make sure nobody attempts
4292 * to steal it from us.
4293 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004294 if (h2c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004295 conn_delete_from_tree(&h2c->conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004296
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004297 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004298 }
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004299
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004300do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02004301 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02004302
4303 if (!h2c) {
4304 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02004305 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02004306 return NULL;
4307 }
4308
4309 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01004310 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02004311 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01004312
Willy Tarreau662fafc2019-05-26 09:43:07 +02004313 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01004314 /* don't even try to send a GOAWAY, the buffer is stuck */
4315 h2c->flags |= H2_CF_GOAWAY_FAILED;
4316 }
4317
4318 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01004319 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01004320 if (h2c_send_goaway_error(h2c, NULL) <= 0)
4321 h2c->flags |= H2_CF_GOAWAY_FAILED;
4322
Willy Tarreau662fafc2019-05-26 09:43:07 +02004323 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004324 unsigned int released = 0;
4325 struct buffer *buf;
4326
4327 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4328 if (b_data(buf)) {
4329 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
4330 if (!ret)
4331 break;
4332 b_del(buf, ret);
4333 if (b_data(buf))
4334 break;
4335 b_free(buf);
4336 released++;
4337 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02004338 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004339
4340 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004341 offer_buffers(NULL, released);
Willy Tarreau787db9a2018-06-14 18:31:46 +02004342 }
Willy Tarreauea392822017-10-31 10:02:25 +01004343
Willy Tarreau4481e262019-10-31 15:36:30 +01004344 /* in any case this connection must not be considered idle anymore */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004345 if (h2c->conn->flags & CO_FL_LIST_MASK) {
4346 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004347 conn_delete_from_tree(&h2c->conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004348 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4349 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004350
Willy Tarreau0975f112018-03-29 15:22:59 +02004351 /* either we can release everything now or it will be done later once
4352 * the last stream closes.
4353 */
4354 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02004355 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01004356
Willy Tarreau7838a792019-08-12 18:42:03 +02004357 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01004358 return NULL;
4359}
4360
4361
Willy Tarreau62f52692017-10-08 23:01:42 +02004362/*******************************************/
4363/* functions below are used by the streams */
4364/*******************************************/
4365
4366/*
4367 * Attach a new stream to a connection
4368 * (Used for outgoing connections)
4369 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004370static int h2_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02004371{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004372 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004373 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004374
Willy Tarreau7838a792019-08-12 18:42:03 +02004375 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004376 h2s = h2c_bck_stream_new(h2c, sd->sc, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004377 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004378 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01004379 return -1;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004380 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004381
4382 /* the connection is not idle anymore, let's mark this */
4383 HA_ATOMIC_AND(&h2c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004384 xprt_set_used(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004385
Willy Tarreau7838a792019-08-12 18:42:03 +02004386 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Christopher Faulete00ad352021-12-16 14:44:31 +01004387 return 0;
Willy Tarreau62f52692017-10-08 23:01:42 +02004388}
4389
Willy Tarreau4596fe22022-05-17 19:07:51 +02004390/* Retrieves the first valid stream connector from this connection, or returns
4391 * NULL. We have to scan because we may have some orphan streams. It might be
Willy Tarreaufafd3982018-11-18 21:29:20 +01004392 * beneficial to scan backwards from the end to reduce the likeliness to find
4393 * orphans.
4394 */
Willy Tarreaud1373532022-05-27 11:00:59 +02004395static struct stconn *h2_get_first_sc(const struct connection *conn)
Willy Tarreaufafd3982018-11-18 21:29:20 +01004396{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004397 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01004398 struct h2s *h2s;
4399 struct eb32_node *node;
4400
4401 node = eb32_first(&h2c->streams_by_id);
4402 while (node) {
4403 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004404 if (h2s_sc(h2s))
4405 return h2s_sc(h2s);
Willy Tarreaufafd3982018-11-18 21:29:20 +01004406 node = eb32_next(node);
4407 }
4408 return NULL;
4409}
4410
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004411static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4412{
4413 int ret = 0;
4414 struct h2c *h2c = conn->ctx;
4415
4416 switch (mux_ctl) {
4417 case MUX_STATUS:
4418 /* Only consider the mux to be ready if we're done with
4419 * the preface and settings, and we had no error.
4420 */
4421 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
4422 ret |= MUX_STATUS_READY;
4423 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02004424 case MUX_EXIT_STATUS:
4425 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004426 default:
4427 return -1;
4428 }
4429}
4430
Willy Tarreau62f52692017-10-08 23:01:42 +02004431/*
Olivier Houchard060ed432018-11-06 16:32:42 +01004432 * Destroy the mux and the associated connection, if it is no longer used
4433 */
Christopher Faulet73c12072019-04-08 11:23:22 +02004434static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01004435{
Christopher Faulet73c12072019-04-08 11:23:22 +02004436 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01004437
Willy Tarreau7838a792019-08-12 18:42:03 +02004438 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02004439 if (eb_is_empty(&h2c->streams_by_id)) {
4440 BUG_ON(h2c->conn->ctx != h2c);
Christopher Faulet73c12072019-04-08 11:23:22 +02004441 h2_release(h2c);
Christopher Faulet4e610962022-04-14 11:23:50 +02004442 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004443 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01004444}
4445
4446/*
Willy Tarreau62f52692017-10-08 23:01:42 +02004447 * Detach the stream from the connection and possibly release the connection.
4448 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004449static void h2_detach(struct sedesc *sd)
Willy Tarreau62f52692017-10-08 23:01:42 +02004450{
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004451 struct h2s *h2s = sd->se;
Willy Tarreau60935142017-10-16 18:11:19 +02004452 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01004453 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004454
Willy Tarreau7838a792019-08-12 18:42:03 +02004455 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
4456
Willy Tarreau7838a792019-08-12 18:42:03 +02004457 if (!h2s) {
4458 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02004459 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004460 }
Willy Tarreau60935142017-10-16 18:11:19 +02004461
Willy Tarreaud9464162020-01-10 18:25:07 +01004462 /* there's no txbuf so we're certain not to be able to send anything */
4463 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02004464
Olivier Houchardf502aca2018-12-14 19:42:40 +01004465 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004466 h2c = h2s->h2c;
Willy Tarreau36c22322022-05-27 10:41:24 +02004467 h2c->nb_sc--;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004468 if (!h2c->nb_sc && !br_data(h2c->mbuf))
Willy Tarreau15a47332022-03-18 15:57:34 +01004469 h2c->idle_start = now_ms;
4470
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004471 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
Willy Tarreau36c22322022-05-27 10:41:24 +02004472 !h2_frt_has_too_many_sc(h2c)) {
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004473 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02004474 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004475 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02004476 }
Willy Tarreau60935142017-10-16 18:11:19 +02004477
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004478 /* this stream may be blocked waiting for some data to leave (possibly
4479 * an ES or RST frame), so orphan it in this case.
4480 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02004481 if (!(h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02004482 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01004483 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01004484 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004485 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau15a47332022-03-18 15:57:34 +01004486 /* refresh the timeout if none was active, so that the last
4487 * leaving stream may arm it.
4488 */
Willy Tarreau3fb2c6d2023-03-16 18:06:19 +01004489 if (h2c->task && !tick_isset(h2c->task->expire))
Willy Tarreau15a47332022-03-18 15:57:34 +01004490 h2c_update_timeout(h2c);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004491 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004492 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004493
Christopher Faulet68ee7842022-10-12 10:21:33 +02004494 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi)) {
Willy Tarreau45f752e2017-10-30 15:44:59 +01004495 /* unblock the connection if it was blocked on this
4496 * stream.
4497 */
4498 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
4499 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004500 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01004501 }
4502
Willy Tarreau71049cc2018-03-28 13:56:39 +02004503 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02004504
Christopher Faulet9b79a102019-07-15 11:22:56 +02004505 if (h2c->flags & H2_CF_IS_BACK) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02004506 if (!(h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERR_PENDING|H2_CF_ERROR))) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004507 if (h2c->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02004508 /* Add the connection in the session server list, if not already done */
4509 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
4510 h2c->conn->owner = NULL;
4511 if (eb_is_empty(&h2c->streams_by_id)) {
4512 h2c->conn->mux->destroy(h2c);
4513 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4514 return;
Christopher Fauletc5579d12020-07-01 15:45:41 +02004515 }
4516 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02004517 if (eb_is_empty(&h2c->streams_by_id)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004518 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
4519 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
4520 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houchard351411f2018-12-27 17:20:54 +01004521 return;
4522 }
4523 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004524 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004525 else {
4526 if (eb_is_empty(&h2c->streams_by_id)) {
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004527 /* If the connection is owned by the session, first remove it
4528 * from its list
4529 */
4530 if (h2c->conn->owner) {
4531 session_unown_conn(h2c->conn->owner, h2c->conn);
4532 h2c->conn->owner = NULL;
4533 }
4534
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004535 /* mark that the tasklet may lose its context to another thread and
4536 * that the handler needs to check it under the idle conns lock.
4537 */
4538 HA_ATOMIC_OR(&h2c->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004539 xprt_set_idle(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
4540
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004541 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01004542 /* The server doesn't want it, let's kill the connection right away */
4543 h2c->conn->mux->destroy(h2c);
4544 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4545 return;
4546 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01004547 /* At this point, the connection has been added to the
4548 * server idle list, so another thread may already have
4549 * hijacked it, so we can't do anything with it.
4550 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01004551 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
4552 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01004553
Olivier Houchard8a786902018-12-15 16:05:40 +01004554 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004555 else if (!h2c->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004556 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02004557 !LIST_INLIST(&h2c->conn->session_list)) {
Willy Tarreau85223482022-09-29 20:32:43 +02004558 eb64_insert(&__objt_server(h2c->conn->target)->per_thr[tid].avail_conns,
4559 &h2c->conn->hash_node->node);
Christopher Fauletc5579d12020-07-01 15:45:41 +02004560 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004561 }
4562 }
4563 }
4564
Willy Tarreaue323f342018-03-28 13:51:45 +02004565 /* We don't want to close right now unless we're removing the
4566 * last stream, and either the connection is in error, or it
4567 * reached the ID already specified in a GOAWAY frame received
4568 * or sent (as seen by last_sid >= 0).
4569 */
Olivier Houchard7a977432019-03-21 15:47:13 +01004570 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02004571 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02004572 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02004573 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02004574 }
4575 else if (h2c->task) {
Willy Tarreau15a47332022-03-18 15:57:34 +01004576 h2c_update_timeout(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004577 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02004578 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004579 else
4580 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004581}
4582
Willy Tarreau88bdba32019-05-13 18:17:53 +02004583/* Performs a synchronous or asynchronous shutr(). */
4584static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004585{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004586 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004587
Willy Tarreauf983d002019-05-14 10:40:21 +02004588 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004589 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004590
Willy Tarreau7838a792019-08-12 18:42:03 +02004591 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4592
Christopher Faulet24b85802023-10-16 19:30:02 +02004593 if (h2s->flags & H2_SF_WANT_SHUTW)
4594 goto add_to_list;
4595
Willy Tarreau18059042019-01-31 19:12:48 +01004596 /* a connstream may require us to immediately kill the whole connection
4597 * for example because of a "tcp-request content reject" rule that is
4598 * normally used to limit abuse. In this case we schedule a goaway to
4599 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004600 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004601 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004602 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004603 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004604 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4605 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4606 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004607 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4608 /* Nothing was never sent for this stream, so reset with
4609 * REFUSED_STREAM error to let the client retry the
4610 * request.
4611 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004612 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004613 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4614 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004615 else {
4616 /* a final response was already provided, we don't want this
4617 * stream anymore. This may happen when the server responds
4618 * before the end of an upload and closes quickly (redirect,
4619 * deny, ...)
4620 */
4621 h2s_error(h2s, H2_ERR_CANCEL);
4622 }
Willy Tarreau18059042019-01-31 19:12:48 +01004623
Willy Tarreau90c32322017-11-24 08:00:30 +01004624 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004625 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004626 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004627
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004628 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004629 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004630 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004631 done:
4632 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004633 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004634 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004635add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004636 /* Let the handler know we want to shutr, and add ourselves to the
4637 * most relevant list if not yet done. h2_deferred_shut() will be
4638 * automatically called via the shut_tl tasklet when there's room
4639 * again.
4640 */
4641 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau2b718102021-04-21 07:32:39 +02004642 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004643 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004644 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004645 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004646 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004647 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004648 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004649 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004650}
4651
Willy Tarreau88bdba32019-05-13 18:17:53 +02004652/* Performs a synchronous or asynchronous shutw(). */
4653static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004654{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004655 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004656
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004657 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004658 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004659
Willy Tarreau7838a792019-08-12 18:42:03 +02004660 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4661
Willy Tarreau473e0e52022-08-18 16:12:15 +02004662 if (h2s->st != H2_SS_ERROR &&
4663 (h2s->flags & (H2_SF_HEADERS_SENT | H2_SF_MORE_HTX_DATA)) == H2_SF_HEADERS_SENT) {
4664 /* we can cleanly close using an empty data frame only after headers
4665 * and if no more data is expected to be sent.
4666 */
Willy Tarreau58e32082017-11-07 14:41:09 +01004667 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4668 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004669 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004670
4671 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004672 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004673 else
4674 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004675 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004676 /* a connstream may require us to immediately kill the whole connection
4677 * for example because of a "tcp-request content reject" rule that is
4678 * normally used to limit abuse. In this case we schedule a goaway to
4679 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004680 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004681 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004682 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004683 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004684 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4685 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4686 }
Willy Tarreau473e0e52022-08-18 16:12:15 +02004687 else if (h2s->flags & H2_SF_MORE_HTX_DATA) {
4688 /* some unsent data were pending (e.g. abort during an upload),
4689 * let's send a CANCEL.
4690 */
4691 TRACE_STATE("shutw before end of data, sending CANCEL", H2_EV_STRM_SHUT, h2c->conn, h2s);
4692 h2s_error(h2s, H2_ERR_CANCEL);
4693 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004694 else {
4695 /* Nothing was never sent for this stream, so reset with
4696 * REFUSED_STREAM error to let the client retry the
4697 * request.
4698 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004699 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004700 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4701 }
Willy Tarreau18059042019-01-31 19:12:48 +01004702
Willy Tarreau90c32322017-11-24 08:00:30 +01004703 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004704 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004705 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004706
Willy Tarreau00dd0782018-03-01 16:31:34 +01004707 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004708 }
4709
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004710 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004711 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004712
4713 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4714
Willy Tarreau88bdba32019-05-13 18:17:53 +02004715 done:
4716 h2s->flags &= ~H2_SF_WANT_SHUTW;
4717 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004718
4719 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004720 /* Let the handler know we want to shutw, and add ourselves to the
4721 * most relevant list if not yet done. h2_deferred_shut() will be
4722 * automatically called via the shut_tl tasklet when there's room
4723 * again.
4724 */
4725 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau2b718102021-04-21 07:32:39 +02004726 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004727 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004728 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004729 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004730 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004731 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004732 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004733 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004734}
4735
Willy Tarreau5723f292020-01-10 15:16:57 +01004736/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004737 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4738 * and prevented the last frame from being emitted.
4739 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004740struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004741{
4742 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004743 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004744
Willy Tarreau7838a792019-08-12 18:42:03 +02004745 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4746
Willy Tarreau5723f292020-01-10 15:16:57 +01004747 if (h2s->flags & H2_SF_NOTIFIED) {
4748 /* some data processing remains to be done first */
4749 goto end;
4750 }
4751
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004752 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004753 h2_do_shutw(h2s);
4754
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004755 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004756 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004757
Willy Tarreau88bdba32019-05-13 18:17:53 +02004758 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004759 /* We're done trying to send, remove ourself from the send_list */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004760 h2_remove_from_list(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01004761
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004762 if (!h2s_sc(h2s)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004763 h2s_destroy(h2s);
Willy Tarreau74163142021-03-13 11:30:19 +01004764 if (h2c_is_dead(h2c)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004765 h2_release(h2c);
Willy Tarreau74163142021-03-13 11:30:19 +01004766 t = NULL;
4767 }
Willy Tarreau88bdba32019-05-13 18:17:53 +02004768 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004769 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004770 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004771 TRACE_LEAVE(H2_EV_STRM_SHUT);
Willy Tarreau74163142021-03-13 11:30:19 +01004772 return t;
Willy Tarreau62f52692017-10-08 23:01:42 +02004773}
4774
Willy Tarreau4596fe22022-05-17 19:07:51 +02004775/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004776static void h2_shutr(struct stconn *sc, enum co_shr_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004777{
Willy Tarreau36c22322022-05-27 10:41:24 +02004778 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004779
Willy Tarreau7838a792019-08-12 18:42:03 +02004780 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004781 if (mode)
4782 h2_do_shutr(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004783 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004784}
4785
Willy Tarreau4596fe22022-05-17 19:07:51 +02004786/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004787static void h2_shutw(struct stconn *sc, enum co_shw_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004788{
Willy Tarreau36c22322022-05-27 10:41:24 +02004789 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004790
Willy Tarreau7838a792019-08-12 18:42:03 +02004791 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004792 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004793 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004794}
4795
Christopher Faulet9b79a102019-07-15 11:22:56 +02004796/* Decode the payload of a HEADERS frame and produce the HTX request or response
4797 * depending on the connection's side. Returns a positive value on success, a
4798 * negative value on failure, or 0 if it couldn't proceed. May report connection
4799 * errors in h2c->errcode if the frame is non-decodable and the connection
4800 * unrecoverable. In absence of connection error when a failure is reported, the
4801 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004802 *
4803 * The function may fold CONTINUATION frames into the initial HEADERS frame
4804 * by removing padding and next frame header, then moving the CONTINUATION
4805 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4806 * leaving a hole between the main frame and the beginning of the next one.
4807 * The possibly remaining incomplete or next frame at the end may be moved
4808 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4809 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4810 *
4811 * A buffer at the beginning of processing may look like this :
4812 *
4813 * ,---.---------.-----.--------------.--------------.------.---.
4814 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4815 * `---^---------^-----^--------------^--------------^------^---'
4816 * | | <-----> | |
4817 * area | dpl | wrap
4818 * |<--------------> |
4819 * | dfl |
4820 * |<-------------------------------------------------->|
4821 * head data
4822 *
4823 * Padding is automatically overwritten when folding, participating to the
4824 * hole size after dfl :
4825 *
4826 * ,---.------------------------.-----.--------------.------.---.
4827 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4828 * `---^------------------------^-----^--------------^------^---'
4829 * | | <-----> | |
4830 * area | hole | wrap
4831 * |<-----------------------> |
4832 * | dfl |
4833 * |<-------------------------------------------------->|
4834 * head data
4835 *
4836 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4837 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4838 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004839 *
4840 * The <flags> field must point to either the stream's flags or to a copy of it
4841 * so that the function can update the following flags :
4842 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004843 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004844 *
4845 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4846 * decoding, in order to detect if we're dealing with a headers or a trailers
4847 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004848 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01004849static 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 +02004850{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004851 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004852 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004853 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004854 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004855 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004856 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004857 int flen; // header frame len
4858 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004859 int ret = 0;
4860 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004861 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004862
Willy Tarreau7838a792019-08-12 18:42:03 +02004863 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4864
Willy Tarreauea18f862018-12-22 20:19:26 +01004865next_frame:
4866 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4867 goto leave; // incomplete input frame
4868
4869 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4870 * this case, we'll try to paste it immediately after the initial
4871 * HEADERS frame payload and kill any possible padding. The initial
4872 * frame's length will be increased to represent the concatenation
4873 * of the two frames. The next frame is read from position <tlen>
4874 * and written at position <flen> (minus padding if some is present).
4875 */
4876 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4877 struct h2_fh hdr;
4878 int clen; // CONTINUATION frame's payload length
4879
Willy Tarreau7838a792019-08-12 18:42:03 +02004880 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 +01004881 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4882 /* no more data, the buffer may be full, either due to
4883 * too large a frame or because of too large a hole that
4884 * we're going to compact at the end.
4885 */
4886 goto leave;
4887 }
4888
4889 if (hdr.ft != H2_FT_CONTINUATION) {
4890 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004891 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 +01004892 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004893 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004894 goto fail;
4895 }
4896
4897 if (hdr.sid != h2c->dsi) {
4898 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004899 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 +01004900 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004901 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004902 goto fail;
4903 }
4904
4905 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4906 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004907 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 +01004908 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4909 goto fail;
4910 }
4911
4912 /* detect when we must stop aggragating frames */
4913 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4914
4915 /* Take as much as we can of the CONTINUATION frame's payload */
4916 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4917 if (clen > hdr.len)
4918 clen = hdr.len;
4919
4920 /* Move the frame's payload over the padding, hole and frame
4921 * header. At least one of hole or dpl is null (see diagrams
4922 * above). The hole moves after the new aggragated frame.
4923 */
4924 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 +02004925 h2c->dfl += hdr.len - h2c->dpl;
Willy Tarreauea18f862018-12-22 20:19:26 +01004926 hole += h2c->dpl + 9;
4927 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004928 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 +01004929 goto next_frame;
4930 }
4931
4932 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004933
Willy Tarreau13278b42017-10-13 19:23:14 +02004934 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004935 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004936 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004937 copy = alloc_trash_chunk();
4938 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004939 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 +02004940 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4941 goto fail;
4942 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004943 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4944 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4945 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004946 }
4947
Willy Tarreau13278b42017-10-13 19:23:14 +02004948 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4949 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004950 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004951 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004952 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 +01004953 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004954 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004955 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004956 }
4957
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004958 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004959 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 +01004960 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4961 goto fail;
4962 }
4963
Willy Tarreau13278b42017-10-13 19:23:14 +02004964 hdrs += 5; // stream dep = 4, weight = 1
4965 flen -= 5;
4966 }
4967
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004968 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004969 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 +01004970 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004971 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004972 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004973
Willy Tarreau937f7602018-02-26 15:22:17 +01004974 /* we can't retry a failed decompression operation so we must be very
4975 * careful not to take any risks. In practice the output buffer is
4976 * always empty except maybe for trailers, in which case we simply have
4977 * to wait for the upper layer to finish consuming what is available.
4978 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004979 htx = htx_from_buf(rxbuf);
4980 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004981 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 +02004982 h2c->flags |= H2_CF_DEM_SFULL;
4983 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004984 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004985
Willy Tarreau25919232019-01-03 14:48:18 +01004986 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004987 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4988 sizeof(list)/sizeof(list[0]), tmp);
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01004989
4990 if (outlen > 0 &&
4991 (TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
4992 TRACE_ENABLED(TRACE_LEVEL_USER, H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, 0, 0, 0)) {
4993 struct ist n;
4994 int i;
4995
4996 for (i = 0; list[i].n.len; i++) {
4997 n = list[i].n;
4998
4999 if (!isttest(n)) {
5000 /* this is in fact a pseudo header whose number is in n.len */
5001 n = h2_phdr_to_ist(n.len);
5002 }
5003
5004 h2_trace_header(n, list[i].v, H2_EV_RX_FRAME|H2_EV_RX_HDR,
5005 ist(TRC_LOC), __FUNCTION__, h2c, NULL);
5006 }
5007 }
5008
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005009 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005010 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 +01005011 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
5012 goto fail;
5013 }
5014
Willy Tarreau25919232019-01-03 14:48:18 +01005015 /* The PACK decompressor was updated, let's update the input buffer and
5016 * the parser's state to commit these changes and allow us to later
5017 * fail solely on the stream if needed.
5018 */
5019 b_del(&h2c->dbuf, h2c->dfl + hole);
5020 h2c->dfl = hole = 0;
5021 h2c->st0 = H2_CS_FRAME_H;
5022
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005023 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01005024 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005025 msgf |= (*flags & H2_SF_BODY_TUNNEL) ? H2_MSGF_BODY_TUNNEL: 0;
Amaury Denoyelle74162742020-12-11 17:53:05 +01005026 /* If an Extended CONNECT has been sent on this stream, set message flag
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005027 * to convert 200 response to 101 htx response */
Amaury Denoyelle74162742020-12-11 17:53:05 +01005028 msgf |= (*flags & H2_SF_EXT_CONNECT_SENT) ? H2_MSGF_EXT_CONNECT: 0;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01005029
Willy Tarreau88d138e2019-01-02 19:38:14 +01005030 if (*flags & H2_SF_HEADERS_RCVD)
5031 goto trailers;
5032
5033 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005034 if (h2c->flags & H2_CF_IS_BACK)
Amaury Denoyelle74162742020-12-11 17:53:05 +01005035 outlen = h2_make_htx_response(list, htx, &msgf, body_len, upgrade_protocol);
Christopher Faulet9b79a102019-07-15 11:22:56 +02005036 else
Willy Tarreaub6be1a42023-08-08 15:38:28 +02005037 outlen = h2_make_htx_request(list, htx, &msgf, body_len,
5038 !!(((const struct session *)h2c->conn->owner)->fe->options2 & PR_O2_REQBUG_OK));
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005039
Christopher Faulet3d875582021-04-26 17:46:13 +02005040 if (outlen < 0 || htx_free_space(htx) < global.tune.maxrewrite) {
Willy Tarreau25919232019-01-03 14:48:18 +01005041 /* too large headers? this is a stream error only */
Christopher Faulet3d875582021-04-26 17:46:13 +02005042 TRACE_STATE("message headers too large", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR|H2_EV_PROTO_ERR, h2c->conn);
5043 htx->flags |= HTX_FL_PARSING_ERROR;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005044 goto fail;
5045 }
Willy Tarreau13278b42017-10-13 19:23:14 +02005046
Willy Tarreau174b06a2018-04-25 18:13:58 +02005047 if (msgf & H2_MSGF_BODY) {
5048 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005049 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005050 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005051 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005052 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02005053 }
Christopher Faulet7d247f02020-12-02 14:26:36 +01005054 if (msgf & H2_MSGF_BODYLESS_RSP)
5055 *flags |= H2_SF_BODYLESS_RESP;
Willy Tarreau174b06a2018-04-25 18:13:58 +02005056
Christopher Fauletd0db4232021-01-22 11:46:30 +01005057 if (msgf & H2_MSGF_BODY_TUNNEL)
5058 *flags |= H2_SF_BODY_TUNNEL;
5059 else {
5060 /* Abort the tunnel attempt, if any */
5061 if (*flags & H2_SF_BODY_TUNNEL)
5062 *flags |= H2_SF_TUNNEL_ABRT;
5063 *flags &= ~H2_SF_BODY_TUNNEL;
5064 }
5065
Willy Tarreau88d138e2019-01-02 19:38:14 +01005066 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01005067 /* indicate that a HEADERS frame was received for this stream, except
5068 * for 1xx responses. For 1xx responses, another HEADERS frame is
5069 * expected.
5070 */
5071 if (!(msgf & H2_MSGF_RSP_1XX))
5072 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01005073
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005074 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Christopher Faulet827a6292022-12-22 09:47:01 +01005075 if (msgf & H2_MSGF_RSP_1XX) {
5076 /* RFC9113#8.1 : HEADERS frame with the ES flag set that carries an informational status code is malformed */
5077 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);
5078 goto fail;
5079 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005080 /* no more data are expected for this message */
5081 htx->flags |= HTX_FL_EOM;
Christopher Faulet1a60a662023-05-24 11:02:50 +02005082 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005083 }
Willy Tarreau937f7602018-02-26 15:22:17 +01005084
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005085 if (msgf & H2_MSGF_EXT_CONNECT)
5086 *flags |= H2_SF_EXT_CONNECT_RCVD;
5087
Willy Tarreau86277d42019-01-02 15:36:11 +01005088 /* success */
5089 ret = 1;
5090
Willy Tarreau68dd9852017-07-03 14:44:26 +02005091 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01005092 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01005093 * move the remaining data over it.
5094 */
5095 if (hole) {
5096 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
5097 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
5098 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
5099 b_sub(&h2c->dbuf, hole);
5100 }
5101
Christopher Faulete3ec2172023-12-13 15:36:52 +01005102 if (b_full(&h2c->dbuf) && h2c->dfl && (!htx || htx_is_empty(htx))) {
Willy Tarreauea18f862018-12-22 20:19:26 +01005103 /* too large frames */
5104 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01005105 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01005106 }
5107
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005108 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005109 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02005110 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02005111 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01005112 return ret;
5113
Willy Tarreau68dd9852017-07-03 14:44:26 +02005114 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01005115 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02005116 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005117
5118 trailers:
5119 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01005120 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
5121 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02005122 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 +01005123 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005124 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau88d138e2019-01-02 19:38:14 +01005125 goto fail;
5126 }
5127
Christopher Faulet9b79a102019-07-15 11:22:56 +02005128 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02005129 if (h2_make_htx_trailers(list, htx) <= 0) {
5130 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 +02005131 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005132 }
Christopher Faulet1a60a662023-05-24 11:02:50 +02005133 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005134 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02005135}
5136
Christopher Faulet9b79a102019-07-15 11:22:56 +02005137/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005138 * parser state is automatically updated. Returns > 0 if it could completely
5139 * send the current frame, 0 if it couldn't complete, in which case
Willy Tarreaub605c422022-05-17 17:04:55 +02005140 * SE_FL_RCV_MORE must be checked to know if some data remain pending (an empty
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005141 * DATA frame can return 0 as a valid result). Stream errors are reported in
5142 * h2s->errcode and connection errors in h2c->errcode. The caller must already
5143 * have checked the frame header and ensured that the frame was complete or the
5144 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02005145 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01005146static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02005147{
5148 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005149 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005150 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005151 struct htx *htx = NULL;
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005152 struct buffer *scbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005153 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02005154
Willy Tarreau7838a792019-08-12 18:42:03 +02005155 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5156
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005157 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02005158
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005159 scbuf = h2_get_buf(h2c, &h2s->rxbuf);
5160 if (!scbuf) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005161 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02005162 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 +01005163 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005164 }
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005165 htx = htx_from_buf(scbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01005166
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005167try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005168 flen = h2c->dfl - h2c->dpl;
5169 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01005170 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005171
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005172 if (flen > b_data(&h2c->dbuf)) {
5173 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005174 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01005175 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005176 }
5177
Christopher Faulet9b79a102019-07-15 11:22:56 +02005178 block = htx_free_data_space(htx);
5179 if (!block) {
5180 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005181 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 +02005182 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005183 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005184 if (flen > block)
5185 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005186
Christopher Faulet9b79a102019-07-15 11:22:56 +02005187 /* here, flen is the max we can copy into the output buffer */
5188 block = b_contig_data(&h2c->dbuf, 0);
5189 if (flen > block)
5190 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005191
Christopher Faulet9b79a102019-07-15 11:22:56 +02005192 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau022e5e52020-09-10 09:33:15 +02005193 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 +02005194
Christopher Faulet9b79a102019-07-15 11:22:56 +02005195 b_del(&h2c->dbuf, sent);
5196 h2c->dfl -= sent;
5197 h2c->rcvd_c += sent;
5198 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02005199
Christopher Faulet9b79a102019-07-15 11:22:56 +02005200 if (h2s->flags & H2_SF_DATA_CLEN) {
5201 h2s->body_len -= sent;
5202 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005203 }
5204
Christopher Faulet9b79a102019-07-15 11:22:56 +02005205 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005206 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005207 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 +01005208 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005209 }
5210
Christopher Faulet9b79a102019-07-15 11:22:56 +02005211 goto try_again;
5212
Willy Tarreau4a28da12018-01-04 14:41:00 +01005213 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005214 /* here we're done with the frame, all the payload (except padding) was
5215 * transferred.
5216 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02005217
Christopher Faulet5be651d2021-01-22 15:28:03 +01005218 if (!(h2s->flags & H2_SF_BODY_TUNNEL) && (h2c->dff & H2_F_DATA_END_STREAM)) {
5219 /* no more data are expected for this message. This add the EOM
5220 * flag but only on the response path or if no tunnel attempt
5221 * was aborted. Otherwise (request path + tunnel abrted), the
5222 * EOM was already reported.
5223 */
Christopher Faulet33724322021-02-10 09:04:59 +01005224 if ((h2c->flags & H2_CF_IS_BACK) || !(h2s->flags & H2_SF_TUNNEL_ABRT)) {
Amaury Denoyelle25cf19d2023-05-11 16:50:04 +02005225 /* htx may be empty if receiving an empty DATA frame. */
5226 if (!htx_set_eom(htx))
5227 goto fail;
Christopher Faulet33724322021-02-10 09:04:59 +01005228 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02005229 }
5230
Willy Tarreaud1023bb2018-03-22 16:53:12 +01005231 h2c->rcvd_c += h2c->dpl;
5232 h2c->rcvd_s += h2c->dpl;
5233 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005234 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005235 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005236 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005237 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01005238 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005239 if (htx)
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005240 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005241 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01005242 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005243}
5244
Willy Tarreau115e83b2018-12-01 19:17:53 +01005245/* Try to send a HEADERS frame matching HTX response present in HTX message
5246 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5247 * must check the stream's status to detect any error which might have happened
5248 * subsequently to a successful send. The htx blocks are automatically removed
5249 * from the message. The htx message is assumed to be valid since produced from
5250 * the internal code, hence it contains a start line, an optional series of
5251 * header blocks and an end of header, otherwise an invalid frame could be
5252 * emitted and the resulting htx message could be left in an inconsistent state.
5253 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005254static size_t h2s_snd_fhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005255{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005256 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01005257 struct h2c *h2c = h2s->h2c;
5258 struct htx_blk *blk;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005259 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005260 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005261 struct htx_sl *sl;
5262 enum htx_blk_type type;
5263 int es_now = 0;
5264 int ret = 0;
5265 int hdr;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005266
Willy Tarreau7838a792019-08-12 18:42:03 +02005267 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5268
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005269 /* get the start line (we do have one) and the rest of the headers,
5270 * that we dump starting at header 0 */
5271 sl = NULL;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005272 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005273 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005274 type = htx_get_blk_type(blk);
5275
5276 if (type == HTX_BLK_UNUSED)
5277 continue;
5278
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005279 if (type == HTX_BLK_EOH)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005280 break;
5281
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005282 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005283 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005284 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5285 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5286 goto fail;
5287 }
5288
5289 list[hdr].n = htx_get_blk_name(htx, blk);
5290 list[hdr].v = htx_get_blk_value(htx, blk);
5291 hdr++;
5292 }
5293 else if (type == HTX_BLK_RES_SL) {
Christopher Faulet56498132021-01-29 11:39:43 +01005294 BUG_ON(sl); /* Only one start-line expected */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005295 sl = htx_get_blk_ptr(htx, blk);
5296 h2s->status = sl->info.res.status;
Christopher Faulet7d247f02020-12-02 14:26:36 +01005297 if (h2s->status == 204 || h2s->status == 304)
5298 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005299 if (h2s->status < 100 || h2s->status > 999) {
5300 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5301 goto fail;
5302 }
5303 else if (h2s->status == 101) {
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005304 if (unlikely(h2s->flags & H2_SF_EXT_CONNECT_RCVD)) {
5305 /* If an Extended CONNECT has been received, we need to convert 101 to 200 */
5306 h2s->status = 200;
5307 h2s->flags &= ~H2_SF_EXT_CONNECT_RCVD;
5308 }
5309 else {
5310 /* Otherwise, 101 responses are not supported in H2, so return a error (RFC7540#8.1.1) */
5311 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5312 goto fail;
5313 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005314 }
5315 else if ((h2s->flags & H2_SF_BODY_TUNNEL) && h2s->status >= 300) {
5316 /* Abort the tunnel attempt */
5317 h2s->flags &= ~H2_SF_BODY_TUNNEL;
5318 h2s->flags |= H2_SF_TUNNEL_ABRT;
5319 }
5320 }
5321 else {
5322 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 +01005323 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005324 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005325 }
5326
Christopher Faulet56498132021-01-29 11:39:43 +01005327 /* The start-line me be defined */
5328 BUG_ON(!sl);
5329
Willy Tarreau115e83b2018-12-01 19:17:53 +01005330 /* marker for end of headers */
5331 list[hdr].n = ist("");
5332
Willy Tarreau9c218e72019-05-26 10:08:28 +02005333 mbuf = br_tail(h2c->mbuf);
5334 retry:
5335 if (!h2_get_buf(h2c, mbuf)) {
5336 h2c->flags |= H2_CF_MUX_MALLOC;
5337 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005338 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 +02005339 return 0;
5340 }
5341
Willy Tarreau115e83b2018-12-01 19:17:53 +01005342 chunk_reset(&outbuf);
5343
5344 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005345 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5346 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005347 break;
5348 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005349 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01005350 }
5351
5352 if (outbuf.size < 9)
5353 goto full;
5354
5355 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5356 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5357 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5358 outbuf.data = 9;
5359
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005360 if ((h2c->flags & (H2_CF_SHTS_UPDATED|H2_CF_DTSU_EMITTED)) == H2_CF_SHTS_UPDATED) {
5361 /* SETTINGS_HEADER_TABLE_SIZE changed, we must send an HPACK
5362 * dynamic table size update so that some clients are not
5363 * confused. In practice we only need to send the DTSU when the
5364 * advertised size is lower than the current one, and since we
5365 * don't use it and don't care about the default 4096 bytes,
5366 * we only ack it with a zero size thus we at most have to deal
5367 * with this once. See RFC7541#4.2 and #6.3 for the spec, and
5368 * below for the whole context and interoperability risks:
5369 * https://lists.w3.org/Archives/Public/ietf-http-wg/2021OctDec/0235.html
5370 */
5371 if (b_room(&outbuf) < 1)
5372 goto full;
5373 outbuf.area[outbuf.data++] = 0x20; // HPACK DTSU 0 bytes
5374
5375 /* let's not update the flags now but only once the buffer is
5376 * really committed.
5377 */
5378 }
5379
Willy Tarreau115e83b2018-12-01 19:17:53 +01005380 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01005381 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005382 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005383 goto realign_again;
5384 goto full;
5385 }
5386
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005387 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED) {
5388 char sts[4];
5389
5390 h2_trace_header(ist(":status"), ist(ultoa_r(h2s->status, sts, sizeof(sts))),
5391 H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__,
5392 h2c, h2s);
5393 }
5394
Willy Tarreau115e83b2018-12-01 19:17:53 +01005395 /* encode all headers, stop at empty name */
5396 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
5397 /* these ones do not exist in H2 and must be dropped. */
5398 if (isteq(list[hdr].n, ist("connection")) ||
5399 isteq(list[hdr].n, ist("proxy-connection")) ||
5400 isteq(list[hdr].n, ist("keep-alive")) ||
5401 isteq(list[hdr].n, ist("upgrade")) ||
5402 isteq(list[hdr].n, ist("transfer-encoding")))
5403 continue;
5404
Christopher Faulet86d144c2019-08-14 16:32:25 +02005405 /* Skip all pseudo-headers */
5406 if (*(list[hdr].n.ptr) == ':')
5407 continue;
5408
Willy Tarreau115e83b2018-12-01 19:17:53 +01005409 if (isteq(list[hdr].n, ist("")))
5410 break; // end
5411
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005412 if (!h2_encode_header(&outbuf, list[hdr].n, list[hdr].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5413 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005414 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005415 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005416 goto realign_again;
5417 goto full;
5418 }
5419 }
5420
Willy Tarreaucb985a42019-10-07 16:56:34 +02005421 /* update the frame's size */
5422 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5423
5424 if (outbuf.data > h2c->mfs + 9) {
5425 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5426 /* output full */
5427 if (b_space_wraps(mbuf))
5428 goto realign_again;
5429 goto full;
5430 }
5431 }
5432
Willy Tarreau3a537072021-06-17 08:40:04 +02005433 TRACE_USER("sent H2 response ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5434
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005435 /* remove all header blocks including the EOH and compute the
5436 * corresponding size.
Willy Tarreau115e83b2018-12-01 19:17:53 +01005437 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005438 ret = 0;
5439 blk = htx_get_head_blk(htx);
5440 while (blk) {
5441 type = htx_get_blk_type(blk);
5442 ret += htx_get_blksz(blk);
5443 blk = htx_remove_blk(htx, blk);
5444 /* The removed block is the EOH */
5445 if (type == HTX_BLK_EOH)
5446 break;
Christopher Faulet5be651d2021-01-22 15:28:03 +01005447 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005448
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005449 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005450 /* Response already closed: add END_STREAM */
5451 es_now = 1;
5452 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005453 else if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && h2s->status >= 200) {
5454 /* EOM+empty: we may need to add END_STREAM except for 1xx
Christopher Faulet991febd2020-12-02 15:17:31 +01005455 * responses and tunneled response.
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005456 */
Christopher Faulet991febd2020-12-02 15:17:31 +01005457 if (!(h2s->flags & H2_SF_BODY_TUNNEL) || h2s->status >= 300)
5458 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005459 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005460
Willy Tarreau115e83b2018-12-01 19:17:53 +01005461 if (es_now)
5462 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5463
5464 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005465 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02005466 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Faulet0b465482019-02-19 15:14:23 +01005467
5468 /* indicates the HEADERS frame was sent, except for 1xx responses. For
5469 * 1xx responses, another HEADERS frame is expected.
5470 */
Christopher Faulet89899422020-12-07 18:24:43 +01005471 if (h2s->status >= 200)
Christopher Faulet0b465482019-02-19 15:14:23 +01005472 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005473
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005474 if (h2c->flags & H2_CF_SHTS_UPDATED) {
5475 /* was sent above */
5476 h2c->flags |= H2_CF_DTSU_EMITTED;
Willy Tarreauc7d85482022-02-16 14:28:14 +01005477 h2c->flags &= ~H2_CF_SHTS_UPDATED;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005478 }
5479
Willy Tarreau115e83b2018-12-01 19:17:53 +01005480 if (es_now) {
5481 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005482 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 +01005483 if (h2s->st == H2_SS_OPEN)
5484 h2s->st = H2_SS_HLOC;
5485 else
5486 h2s_close(h2s);
5487 }
5488
5489 /* OK we could properly deliver the response */
Willy Tarreau115e83b2018-12-01 19:17:53 +01005490 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005491 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005492 return ret;
5493 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005494 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5495 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005496 h2c->flags |= H2_CF_MUX_MFULL;
5497 h2s->flags |= H2_SF_BLK_MROOM;
5498 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005499 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 +01005500 goto end;
5501 fail:
5502 /* unparsable HTX messages, too large ones to be produced in the local
5503 * list etc go here (unrecoverable errors).
5504 */
5505 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5506 ret = 0;
5507 goto end;
5508}
5509
Willy Tarreau80739692018-10-05 11:35:57 +02005510/* Try to send a HEADERS frame matching HTX request present in HTX message
5511 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5512 * must check the stream's status to detect any error which might have happened
5513 * subsequently to a successful send. The htx blocks are automatically removed
5514 * from the message. The htx message is assumed to be valid since produced from
5515 * the internal code, hence it contains a start line, an optional series of
5516 * header blocks and an end of header, otherwise an invalid frame could be
5517 * emitted and the resulting htx message could be left in an inconsistent state.
5518 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005519static size_t h2s_snd_bhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02005520{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005521 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02005522 struct h2c *h2c = h2s->h2c;
5523 struct htx_blk *blk;
Willy Tarreau80739692018-10-05 11:35:57 +02005524 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005525 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02005526 struct htx_sl *sl;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005527 struct ist meth, uri, auth, host = IST_NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005528 enum htx_blk_type type;
5529 int es_now = 0;
5530 int ret = 0;
5531 int hdr;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005532 int extended_connect = 0;
Willy Tarreau80739692018-10-05 11:35:57 +02005533
Willy Tarreau7838a792019-08-12 18:42:03 +02005534 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5535
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005536 /* get the start line (we do have one) and the rest of the headers,
5537 * that we dump starting at header 0 */
5538 sl = NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005539 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005540 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005541 type = htx_get_blk_type(blk);
5542
5543 if (type == HTX_BLK_UNUSED)
5544 continue;
5545
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005546 if (type == HTX_BLK_EOH)
Willy Tarreau80739692018-10-05 11:35:57 +02005547 break;
5548
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005549 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005550 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005551 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5552 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5553 goto fail;
5554 }
Willy Tarreau80739692018-10-05 11:35:57 +02005555
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005556 list[hdr].n = htx_get_blk_name(htx, blk);
5557 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02005558
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005559 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005560 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name) &&
5561 isteq(list[hdr].n, h2c->proxy->server_id_hdr_name))
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005562 continue;
Christopher Faulet67d58092019-10-02 10:51:38 +02005563
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005564 /* Convert connection: upgrade to Extended connect from rfc 8441 */
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005565 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteqi(list[hdr].n, ist("connection"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005566 /* rfc 7230 #6.1 Connection = list of tokens */
5567 struct ist connection_ist = list[hdr].v;
5568 do {
5569 if (isteqi(iststop(connection_ist, ','),
5570 ist("upgrade"))) {
Amaury Denoyelle0df04362021-10-18 09:43:29 +02005571 if (!(h2c->flags & H2_CF_RCVD_RFC8441)) {
5572 TRACE_STATE("reject upgrade because of no RFC8441 support", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5573 goto fail;
5574 }
5575
Amaury Denoyellee0c258c2021-10-18 10:05:16 +02005576 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 +01005577 h2s->flags |= (H2_SF_BODY_TUNNEL|H2_SF_EXT_CONNECT_SENT);
5578 sl->info.req.meth = HTTP_METH_CONNECT;
5579 meth = ist("CONNECT");
5580
5581 extended_connect = 1;
5582 break;
5583 }
5584
5585 connection_ist = istadv(istfind(connection_ist, ','), 1);
5586 } while (istlen(connection_ist));
5587 }
5588
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005589 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteq(list[hdr].n, ist("upgrade"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005590 /* rfc 7230 #6.7 Upgrade = list of protocols
5591 * rfc 8441 #4 Extended connect = :protocol is single-valued
5592 *
5593 * only first HTTP/1 protocol is preserved
5594 */
5595 const struct ist protocol = iststop(list[hdr].v, ',');
5596 /* upgrade_protocol field is 16 bytes long in h2s */
5597 istpad(h2s->upgrade_protocol, isttrim(protocol, 15));
5598 }
5599
5600 if (isteq(list[hdr].n, ist("host")))
5601 host = list[hdr].v;
5602
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005603 hdr++;
5604 }
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005605 else if (type == HTX_BLK_REQ_SL) {
5606 BUG_ON(sl); /* Only one start-line expected */
5607 sl = htx_get_blk_ptr(htx, blk);
5608 meth = htx_sl_req_meth(sl);
5609 uri = htx_sl_req_uri(sl);
5610 if (sl->info.req.meth == HTTP_METH_HEAD)
5611 h2s->flags |= H2_SF_BODYLESS_RESP;
5612 if (unlikely(uri.len == 0)) {
5613 TRACE_ERROR("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5614 goto fail;
5615 }
5616 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005617 else {
5618 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5619 goto fail;
5620 }
Willy Tarreau80739692018-10-05 11:35:57 +02005621 }
5622
Christopher Faulet56498132021-01-29 11:39:43 +01005623 /* The start-line me be defined */
5624 BUG_ON(!sl);
5625
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005626 /* Now add the server name to a header (if requested) */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005627 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005628 struct server *srv = objt_server(h2c->conn->target);
5629
5630 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005631 list[hdr].n = h2c->proxy->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005632 list[hdr].v = ist(srv->id);
5633 hdr++;
5634 }
5635 }
5636
Willy Tarreau80739692018-10-05 11:35:57 +02005637 /* marker for end of headers */
5638 list[hdr].n = ist("");
5639
Willy Tarreau9c218e72019-05-26 10:08:28 +02005640 mbuf = br_tail(h2c->mbuf);
5641 retry:
5642 if (!h2_get_buf(h2c, mbuf)) {
5643 h2c->flags |= H2_CF_MUX_MALLOC;
5644 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005645 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 +02005646 return 0;
5647 }
5648
Willy Tarreau80739692018-10-05 11:35:57 +02005649 chunk_reset(&outbuf);
5650
5651 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005652 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5653 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005654 break;
5655 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005656 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02005657 }
5658
5659 if (outbuf.size < 9)
5660 goto full;
5661
5662 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5663 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5664 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5665 outbuf.data = 9;
5666
5667 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01005668 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005669 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005670 goto realign_again;
5671 goto full;
5672 }
5673
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005674 h2_trace_header(ist(":method"), meth, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5675
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005676 auth = ist(NULL);
5677
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005678 /* RFC7540 #8.3: the CONNECT method must have :
5679 * - :authority set to the URI part (host:port)
5680 * - :method set to CONNECT
5681 * - :scheme and :path omitted
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005682 *
5683 * Note that this is not applicable in case of the Extended CONNECT
5684 * protocol from rfc 8441.
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005685 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005686 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT) && !extended_connect) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005687 auth = uri;
5688
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005689 if (!h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5690 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005691 /* output full */
5692 if (b_space_wraps(mbuf))
5693 goto realign_again;
5694 goto full;
5695 }
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005696
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005697 h2s->flags |= H2_SF_BODY_TUNNEL;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005698 } else {
5699 /* other methods need a :scheme. If an authority is known from
5700 * the request line, it must be sent, otherwise only host is
5701 * sent. Host is never sent as the authority.
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005702 *
5703 * This code is also applicable for Extended CONNECT protocol
5704 * from rfc 8441.
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005705 */
5706 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02005707
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005708 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
5709 /* the URI seems to start with a scheme */
5710 int len = 1;
5711
5712 while (len < uri.len && uri.ptr[len] != ':')
5713 len++;
5714
5715 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5716 /* make the uri start at the authority now */
Tim Duesterhus9f75ed12021-03-02 18:57:26 +01005717 scheme = ist2(uri.ptr, len);
Tim Duesterhus154374c2021-03-02 18:57:27 +01005718 uri = istadv(uri, len + 3);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005719
5720 /* find the auth part of the URI */
Tim Duesterhus92c696e2021-02-28 16:11:36 +01005721 auth = ist2(uri.ptr, 0);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005722 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5723 auth.len++;
5724
Tim Duesterhus154374c2021-03-02 18:57:27 +01005725 uri = istadv(uri, auth.len);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005726 }
5727 }
5728
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005729 /* For Extended CONNECT, the :authority must be present.
5730 * Use host value for it.
5731 */
5732 if (unlikely(extended_connect) && isttest(host))
5733 auth = host;
5734
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005735 if (!scheme.len) {
5736 /* no explicit scheme, we're using an origin-form URI,
5737 * probably from an H1 request transcoded to H2 via an
5738 * external layer, then received as H2 without authority.
5739 * So we have to look up the scheme from the HTX flags.
5740 * In such a case only http and https are possible, and
5741 * https is the default (sent by browsers).
5742 */
5743 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5744 scheme = ist("http");
5745 else
5746 scheme = ist("https");
5747 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005748
5749 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005750 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005751 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005752 goto realign_again;
5753 goto full;
5754 }
Willy Tarreau80739692018-10-05 11:35:57 +02005755
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005756 if (auth.len &&
5757 !h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5758 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005759 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005760 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005761 goto realign_again;
5762 goto full;
5763 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005764
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005765 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5766 * be sent as '/' or '*'.
5767 */
5768 if (unlikely(!uri.len)) {
5769 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5770 uri = ist("*");
5771 else
5772 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005773 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005774
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005775 if (!hpack_encode_path(&outbuf, uri)) {
5776 /* output full */
5777 if (b_space_wraps(mbuf))
5778 goto realign_again;
5779 goto full;
5780 }
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005781
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005782 h2_trace_header(ist(":path"), uri, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5783
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005784 /* encode the pseudo-header protocol from rfc8441 if using
5785 * Extended CONNECT method.
5786 */
5787 if (unlikely(extended_connect)) {
5788 const struct ist protocol = ist(h2s->upgrade_protocol);
5789 if (isttest(protocol)) {
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005790 if (!h2_encode_header(&outbuf, ist(":protocol"), protocol, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5791 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005792 /* output full */
5793 if (b_space_wraps(mbuf))
5794 goto realign_again;
5795 goto full;
5796 }
5797 }
5798 }
Willy Tarreau80739692018-10-05 11:35:57 +02005799 }
5800
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005801 /* encode all headers, stop at empty name. Host is only sent if we
5802 * do not provide an authority.
5803 */
Willy Tarreau80739692018-10-05 11:35:57 +02005804 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005805 struct ist n = list[hdr].n;
5806 struct ist v = list[hdr].v;
5807
Willy Tarreau80739692018-10-05 11:35:57 +02005808 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005809 if (isteq(n, ist("connection")) ||
5810 (auth.len && isteq(n, ist("host"))) ||
5811 isteq(n, ist("proxy-connection")) ||
5812 isteq(n, ist("keep-alive")) ||
5813 isteq(n, ist("upgrade")) ||
5814 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005815 continue;
5816
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005817 if (isteq(n, ist("te"))) {
5818 /* "te" may only be sent with "trailers" if this value
5819 * is present, otherwise it must be deleted.
5820 */
5821 v = istist(v, ist("trailers"));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01005822 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005823 continue;
5824 v = ist("trailers");
5825 }
5826
Christopher Faulet86d144c2019-08-14 16:32:25 +02005827 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005828 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005829 continue;
5830
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005831 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005832 break; // end
5833
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005834 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 +02005835 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005836 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005837 goto realign_again;
5838 goto full;
5839 }
5840 }
5841
Willy Tarreaucb985a42019-10-07 16:56:34 +02005842 /* update the frame's size */
5843 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5844
5845 if (outbuf.data > h2c->mfs + 9) {
5846 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5847 /* output full */
5848 if (b_space_wraps(mbuf))
5849 goto realign_again;
5850 goto full;
5851 }
5852 }
5853
Willy Tarreau3a537072021-06-17 08:40:04 +02005854 TRACE_USER("sent H2 request ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5855
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005856 /* remove all header blocks including the EOH and compute the
5857 * corresponding size.
Willy Tarreau80739692018-10-05 11:35:57 +02005858 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005859 ret = 0;
5860 blk = htx_get_head_blk(htx);
5861 while (blk) {
5862 type = htx_get_blk_type(blk);
5863 ret += htx_get_blksz(blk);
5864 blk = htx_remove_blk(htx, blk);
5865 /* The removed block is the EOH */
5866 if (type == HTX_BLK_EOH)
5867 break;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005868 }
Willy Tarreau80739692018-10-05 11:35:57 +02005869
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005870 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005871 /* Request already closed: add END_STREAM */
Willy Tarreau80739692018-10-05 11:35:57 +02005872 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005873 }
5874 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5875 /* EOM+empty: we may need to add END_STREAM (except for CONNECT
5876 * request)
5877 */
5878 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5879 es_now = 1;
5880 }
Willy Tarreau80739692018-10-05 11:35:57 +02005881
Willy Tarreau80739692018-10-05 11:35:57 +02005882 if (es_now)
5883 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5884
5885 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005886 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02005887 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau80739692018-10-05 11:35:57 +02005888 h2s->flags |= H2_SF_HEADERS_SENT;
5889 h2s->st = H2_SS_OPEN;
5890
Willy Tarreau80739692018-10-05 11:35:57 +02005891 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005892 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 +02005893 // trim any possibly pending data (eg: inconsistent content-length)
5894 h2s->flags |= H2_SF_ES_SENT;
5895 h2s->st = H2_SS_HLOC;
5896 }
5897
Willy Tarreau80739692018-10-05 11:35:57 +02005898 end:
5899 return ret;
5900 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005901 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5902 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02005903 h2c->flags |= H2_CF_MUX_MFULL;
5904 h2s->flags |= H2_SF_BLK_MROOM;
5905 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005906 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 +02005907 goto end;
5908 fail:
5909 /* unparsable HTX messages, too large ones to be produced in the local
5910 * list etc go here (unrecoverable errors).
5911 */
5912 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5913 ret = 0;
5914 goto end;
5915}
5916
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005917/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005918 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5919 * caller must check the stream's status to detect any error which might have
5920 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005921 * consumed, or zero if nothing done.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005922 */
Christopher Faulet142854b2020-12-02 15:12:40 +01005923static size_t h2s_make_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005924{
5925 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005926 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005927 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005928 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005929 size_t total = 0;
5930 int es_now = 0;
5931 int bsize; /* htx block size */
5932 int fsize; /* h2 frame size */
5933 struct htx_blk *blk;
5934 enum htx_blk_type type;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005935 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005936
Willy Tarreau7838a792019-08-12 18:42:03 +02005937 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5938
Willy Tarreau98de12a2018-12-12 07:03:00 +01005939 htx = htx_from_buf(buf);
5940
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005941 /* We only come here with HTX_BLK_DATA blocks */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005942
5943 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005944 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005945 goto end;
5946
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005947 if ((h2c->flags & H2_CF_IS_BACK) &&
Christopher Fauletf95f8762021-01-22 11:59:07 +01005948 (h2s->flags & (H2_SF_HEADERS_RCVD|H2_SF_BODY_TUNNEL)) == H2_SF_BODY_TUNNEL) {
5949 /* The response HEADERS frame not received yet. Thus the tunnel
5950 * is not fully established yet. In this situation, we block
5951 * data sending.
5952 */
5953 h2s->flags |= H2_SF_BLK_MBUSY;
5954 TRACE_STATE("Request DATA frame blocked waiting for tunnel establishment", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5955 goto end;
5956 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01005957 else if ((h2c->flags & H2_CF_IS_BACK) && (h2s->flags & H2_SF_TUNNEL_ABRT)) {
5958 /* a tunnel attempt was aborted but the is pending raw data to xfer to the server.
5959 * Thus the stream is closed with the CANCEL error. The error will be reported to
5960 * the upper layer as aserver abort. But at this stage there is nothing more we can
5961 * do. We just wait for the end of the response to be sure to not truncate it.
5962 */
5963 if (!(h2s->flags & H2_SF_ES_RCVD)) {
5964 TRACE_STATE("Request DATA frame blocked waiting end of aborted tunnel", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5965 h2s->flags |= H2_SF_BLK_MBUSY;
5966 }
5967 else {
5968 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5969 h2s_error(h2s, H2_ERR_CANCEL);
5970 }
5971 goto end;
5972 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01005973
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005974 blk = htx_get_head_blk(htx);
5975 type = htx_get_blk_type(blk);
5976 bsize = htx_get_blksz(blk);
5977 fsize = bsize;
5978 trunc_out = 0;
5979 if (type != HTX_BLK_DATA)
5980 goto end;
5981
Willy Tarreau9c218e72019-05-26 10:08:28 +02005982 mbuf = br_tail(h2c->mbuf);
5983 retry:
5984 if (!h2_get_buf(h2c, mbuf)) {
5985 h2c->flags |= H2_CF_MUX_MALLOC;
5986 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005987 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 +02005988 goto end;
5989 }
5990
Willy Tarreau98de12a2018-12-12 07:03:00 +01005991 /* Perform some optimizations to reduce the number of buffer copies.
5992 * First, if the mux's buffer is empty and the htx area contains
5993 * exactly one data block of the same size as the requested count, and
5994 * this count fits within the frame size, the stream's window size, and
5995 * the connection's window size, then it's possible to simply swap the
5996 * caller's buffer with the mux's output buffer and adjust offsets and
5997 * length to match the entire DATA HTX block in the middle. In this
5998 * case we perform a true zero-copy operation from end-to-end. This is
5999 * the situation that happens all the time with large files. Second, if
6000 * this is not possible, but the mux's output buffer is empty, we still
6001 * have an opportunity to avoid the copy to the intermediary buffer, by
6002 * making the intermediary buffer's area point to the output buffer's
6003 * area. In this case we want to skip the HTX header to make sure that
6004 * copies remain aligned and that this operation remains possible all
6005 * the time. This goes for headers, data blocks and any data extracted
6006 * from the HTX blocks.
6007 */
6008 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02006009 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006010 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006011 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006012
Willy Tarreaubcc45952019-05-26 10:05:50 +02006013 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01006014 /* Too bad there are data left there. We're willing to memcpy/memmove
6015 * up to 1/4 of the buffer, which means that it's OK to copy a large
6016 * frame into a buffer containing few data if it needs to be realigned,
6017 * and that it's also OK to copy few data without realigning. Otherwise
6018 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01006019 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006020 if (fsize + 9 <= b_room(mbuf) &&
6021 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02006022 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
6023 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 +01006024 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02006025 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01006026
Willy Tarreau9c218e72019-05-26 10:08:28 +02006027 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6028 goto retry;
6029
Willy Tarreau98de12a2018-12-12 07:03:00 +01006030 h2c->flags |= H2_CF_MUX_MFULL;
6031 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006032 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 +01006033 goto end;
6034 }
6035
Christopher Faulet925abdf2021-04-27 22:51:07 +02006036 if (htx->flags & HTX_FL_EOM) {
6037 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6038 * message)
6039 */
6040 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6041 es_now = 1;
6042 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01006043 /* map an H2 frame to the HTX block so that we can put the
6044 * frame header there.
6045 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006046 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
6047 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01006048
6049 /* prepend an H2 DATA frame header just before the DATA block */
6050 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6051 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
Christopher Faulet925abdf2021-04-27 22:51:07 +02006052 if (es_now)
6053 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006054 h2_set_frame_size(outbuf.area, fsize);
6055
6056 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006057 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006058 h2c->mws -= fsize;
6059
6060 /* and exchange with our old area */
6061 buf->area = old_area;
6062 buf->data = buf->head = 0;
6063 total += fsize;
Christopher Faulet925abdf2021-04-27 22:51:07 +02006064 fsize = 0;
Willy Tarreau936db562023-10-18 11:39:43 +02006065 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau7838a792019-08-12 18:42:03 +02006066
6067 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 +02006068 goto out;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006069 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01006070
Willy Tarreau98de12a2018-12-12 07:03:00 +01006071 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006072 /* for DATA and EOM we'll have to emit a frame, even if empty */
6073
6074 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006075 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6076 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006077 break;
6078 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006079 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006080 }
6081
6082 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02006083 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6084 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006085 h2c->flags |= H2_CF_MUX_MFULL;
6086 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006087 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006088 goto end;
6089 }
6090
6091 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
6092 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6093 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6094 outbuf.data = 9;
6095
6096 /* we have in <fsize> the exact number of bytes we need to copy from
6097 * the HTX buffer. We need to check this against the connection's and
6098 * the stream's send windows, and to ensure that this fits in the max
6099 * frame size and in the buffer's available space minus 9 bytes (for
6100 * the frame header). The connection's flow control is applied last so
6101 * that we can use a separate list of streams which are immediately
6102 * unblocked on window opening. Note: we don't implement padding.
6103 */
6104
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006105 if (!fsize)
6106 goto send_empty;
6107
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006108 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006109 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreau2b718102021-04-21 07:32:39 +02006110 if (LIST_INLIST(&h2s->list))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006111 h2_remove_from_list(h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006112 LIST_APPEND(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02006113 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 +01006114 goto end;
6115 }
6116
Willy Tarreauee573762018-12-04 15:25:57 +01006117 if (fsize > count)
6118 fsize = count;
6119
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006120 if (fsize > h2s_mws(h2s))
6121 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006122
6123 if (h2c->mfs && fsize > h2c->mfs)
6124 fsize = h2c->mfs; // >0
6125
6126 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02006127 /* It doesn't fit at once. If it at least fits once split and
6128 * the amount of data to move is low, let's defragment the
6129 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006130 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006131 if (b_space_wraps(mbuf) &&
6132 (fsize + 9 <= b_room(mbuf)) &&
6133 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006134 goto realign_again;
6135 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01006136 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006137
6138 if (fsize <= 0) {
6139 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02006140 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6141 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006142 h2c->flags |= H2_CF_MUX_MFULL;
6143 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006144 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006145 goto end;
6146 }
6147 }
6148
6149 if (h2c->mws <= 0) {
6150 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02006151 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 +01006152 goto end;
6153 }
6154
6155 if (fsize > h2c->mws)
6156 fsize = h2c->mws;
6157
6158 /* now let's copy this this into the output buffer */
6159 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006160 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01006161 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01006162 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006163
6164 send_empty:
6165 /* update the frame's size */
6166 h2_set_frame_size(outbuf.area, fsize);
6167
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006168 /* consume incoming HTX block */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006169 total += fsize;
6170 if (fsize == bsize) {
6171 htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006172 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
6173 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6174 * message)
6175 */
6176 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6177 es_now = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02006178 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006179 }
6180 else {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006181 /* we've truncated this block */
6182 htx_cut_data_blk(htx, blk, fsize);
6183 }
6184
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006185 if (es_now)
6186 outbuf.area[4] |= H2_F_DATA_END_STREAM;
6187
6188 /* commit the H2 response */
6189 b_add(mbuf, fsize + 9);
Willy Tarreau936db562023-10-18 11:39:43 +02006190 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006191
Christopher Faulet925abdf2021-04-27 22:51:07 +02006192 out:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006193 if (es_now) {
6194 if (h2s->st == H2_SS_OPEN)
6195 h2s->st = H2_SS_HLOC;
6196 else
6197 h2s_close(h2s);
6198
6199 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02006200 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 +01006201 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006202 else if (fsize) {
6203 if (fsize == bsize) {
6204 TRACE_DEVEL("more data may be available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6205 goto new_frame;
6206 }
6207 else if (trunc_out) {
6208 /* we've truncated this block */
6209 goto new_frame;
6210 }
6211 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006212
6213 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006214 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006215 return total;
6216}
6217
Christopher Faulet991febd2020-12-02 15:17:31 +01006218/* Skip the message payload (DATA blocks) and emit an empty DATA frame with the
6219 * ES flag set for stream <h2s>. This function is called for response known to
6220 * have no payload. Only DATA blocks are skipped. This means the trailers are
Ilya Shipitsinacf84592021-02-06 22:29:08 +05006221 * still emitted. The caller must check the stream's status to detect any error
Christopher Faulet991febd2020-12-02 15:17:31 +01006222 * which might have happened subsequently to a successful send. Returns the
6223 * number of data bytes consumed, or zero if nothing done.
6224 */
6225static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count)
6226{
6227 struct h2c *h2c = h2s->h2c;
6228 struct htx *htx;
6229 int bsize; /* htx block size */
6230 int fsize; /* h2 frame size */
6231 struct htx_blk *blk;
6232 enum htx_blk_type type;
6233 size_t total = 0;
6234
6235 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6236
Christopher Faulet991febd2020-12-02 15:17:31 +01006237 htx = htx_from_buf(buf);
6238
6239 next_data:
6240 if (!count || htx_is_empty(htx))
6241 goto end;
6242 blk = htx_get_head_blk(htx);
6243 type = htx_get_blk_type(blk);
6244 bsize = htx_get_blksz(blk);
6245 fsize = bsize;
6246 if (type != HTX_BLK_DATA)
6247 goto end;
6248
6249 if (fsize > count)
6250 fsize = count;
6251
6252 if (fsize != bsize)
6253 goto skip_data;
6254
6255 if (!(htx->flags & HTX_FL_EOM) || !htx_is_unique_blk(htx, blk))
6256 goto skip_data;
6257
6258 /* Here, it is the last block and it is also the end of the message. So
6259 * we can emit an empty DATA frame with the ES flag set
6260 */
6261 if (h2_send_empty_data_es(h2s) <= 0)
6262 goto end;
6263
6264 if (h2s->st == H2_SS_OPEN)
6265 h2s->st = H2_SS_HLOC;
6266 else
6267 h2s_close(h2s);
6268
6269 skip_data:
6270 /* consume incoming HTX block */
6271 total += fsize;
6272 if (fsize == bsize) {
6273 TRACE_DEVEL("more data may be available, trying to skip another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6274 htx_remove_blk(htx, blk);
6275 goto next_data;
6276 }
6277 else {
6278 /* we've truncated this block */
6279 htx_cut_data_blk(htx, blk, fsize);
6280 }
6281
6282 end:
6283 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6284 return total;
6285}
6286
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006287/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
6288 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
6289 * processed. The caller must check the stream's status to detect any error
6290 * which might have happened subsequently to a successful send. The htx blocks
6291 * are automatically removed from the message. The htx message is assumed to be
6292 * valid since produced from the internal code. Processing stops when meeting
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006293 * the EOT, which *is* removed. All trailers are processed at once and sent as a
6294 * single frame. The ES flag is always set.
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006295 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006296static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006297{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02006298 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006299 struct h2c *h2c = h2s->h2c;
6300 struct htx_blk *blk;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006301 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006302 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006303 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006304 int ret = 0;
6305 int hdr;
6306 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006307
Willy Tarreau7838a792019-08-12 18:42:03 +02006308 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
6309
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006310 /* get trailers. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006311 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006312 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006313 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006314
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006315 if (type == HTX_BLK_UNUSED)
6316 continue;
6317
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006318 if (type == HTX_BLK_EOT)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006319 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006320 if (type == HTX_BLK_TLR) {
6321 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
6322 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
6323 goto fail;
6324 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006325
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006326 list[hdr].n = htx_get_blk_name(htx, blk);
6327 list[hdr].v = htx_get_blk_value(htx, blk);
6328 hdr++;
6329 }
6330 else {
6331 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 +01006332 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02006333 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006334 }
6335
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006336 /* marker for end of trailers */
6337 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006338
Willy Tarreau9c218e72019-05-26 10:08:28 +02006339 mbuf = br_tail(h2c->mbuf);
6340 retry:
6341 if (!h2_get_buf(h2c, mbuf)) {
6342 h2c->flags |= H2_CF_MUX_MALLOC;
6343 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006344 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 +02006345 goto end;
6346 }
6347
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006348 chunk_reset(&outbuf);
6349
6350 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006351 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6352 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006353 break;
6354 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006355 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006356 }
6357
6358 if (outbuf.size < 9)
6359 goto full;
6360
6361 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
6362 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
6363 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6364 outbuf.data = 9;
6365
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006366 /* encode all headers */
6367 for (idx = 0; idx < hdr; idx++) {
6368 /* these ones do not exist in H2 or must not appear in
6369 * trailers and must be dropped.
6370 */
6371 if (isteq(list[idx].n, ist("host")) ||
6372 isteq(list[idx].n, ist("content-length")) ||
6373 isteq(list[idx].n, ist("connection")) ||
6374 isteq(list[idx].n, ist("proxy-connection")) ||
6375 isteq(list[idx].n, ist("keep-alive")) ||
6376 isteq(list[idx].n, ist("upgrade")) ||
6377 isteq(list[idx].n, ist("te")) ||
6378 isteq(list[idx].n, ist("transfer-encoding")))
6379 continue;
6380
Christopher Faulet86d144c2019-08-14 16:32:25 +02006381 /* Skip all pseudo-headers */
6382 if (*(list[idx].n.ptr) == ':')
6383 continue;
6384
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01006385 if (!h2_encode_header(&outbuf, list[idx].n, list[idx].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
6386 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006387 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006388 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006389 goto realign_again;
6390 goto full;
6391 }
6392 }
6393
Willy Tarreau5121e5d2019-05-06 15:13:41 +02006394 if (outbuf.data == 9) {
6395 /* here we have a problem, we have nothing to emit (either we
6396 * received an empty trailers block followed or we removed its
6397 * contents above). Because of this we can't send a HEADERS
6398 * frame, so we have to cheat and instead send an empty DATA
6399 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01006400 */
6401 outbuf.area[3] = H2_FT_DATA;
6402 outbuf.area[4] = H2_F_DATA_END_STREAM;
6403 }
6404
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006405 /* update the frame's size */
6406 h2_set_frame_size(outbuf.area, outbuf.data - 9);
6407
Willy Tarreau572d9f52019-10-11 16:58:37 +02006408 if (outbuf.data > h2c->mfs + 9) {
6409 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
6410 /* output full */
6411 if (b_space_wraps(mbuf))
6412 goto realign_again;
6413 goto full;
6414 }
6415 }
6416
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006417 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02006418 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 +02006419 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02006420 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006421 h2s->flags |= H2_SF_ES_SENT;
6422
6423 if (h2s->st == H2_SS_OPEN)
6424 h2s->st = H2_SS_HLOC;
6425 else
6426 h2s_close(h2s);
6427
6428 /* OK we could properly deliver the response */
6429 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02006430 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006431 ret = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006432 blk = htx_get_head_blk(htx);
6433 while (blk) {
6434 type = htx_get_blk_type(blk);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006435 ret += htx_get_blksz(blk);
6436 blk = htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006437 /* The removed block is the EOT */
6438 if (type == HTX_BLK_EOT)
6439 break;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006440 }
6441
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006442 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006443 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006444 return ret;
6445 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006446 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6447 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006448 h2c->flags |= H2_CF_MUX_MFULL;
6449 h2s->flags |= H2_SF_BLK_MROOM;
6450 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006451 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 +01006452 goto end;
6453 fail:
6454 /* unparsable HTX messages, too large ones to be produced in the local
6455 * list etc go here (unrecoverable errors).
6456 */
6457 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6458 ret = 0;
6459 goto end;
6460}
6461
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006462/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6463 * event subscriber <es> is not allowed to change from a previous call as long
6464 * as at least one event is still subscribed. The <event_type> must only be a
6465 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006466 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006467static int h2_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02006468{
Willy Tarreau36c22322022-05-27 10:41:24 +02006469 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02006470 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006471
Willy Tarreau7838a792019-08-12 18:42:03 +02006472 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006473
6474 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006475 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006476
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006477 es->events |= event_type;
6478 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006479
6480 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006481 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006482
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006483 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006484 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02006485 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02006486 !LIST_INLIST(&h2s->list)) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006487 if (h2s->flags & H2_SF_BLK_MFCTL) {
6488 TRACE_DEVEL("Adding to fctl list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006489 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006490 }
6491 else {
6492 TRACE_DEVEL("Adding to send list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006493 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006494 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02006495 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02006496 }
Willy Tarreau7838a792019-08-12 18:42:03 +02006497 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006498 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006499}
6500
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006501/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6502 * The <es> pointer is not allowed to differ from the one passed to the
6503 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006504 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006505static int h2_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006506{
Willy Tarreau36c22322022-05-27 10:41:24 +02006507 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006508
Willy Tarreau7838a792019-08-12 18:42:03 +02006509 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006510
6511 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006512 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006513
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006514 es->events &= ~event_type;
6515 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01006516 h2s->subs = NULL;
6517
6518 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006519 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006520
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006521 if (event_type & SUB_RETRY_SEND) {
Frédéric Lécaille67fda162022-06-30 12:01:54 +02006522 TRACE_DEVEL("unsubscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006523 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006524 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006525 h2_remove_from_list(h2s);
Olivier Houchardd846c262018-10-19 17:24:29 +02006526 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01006527
Willy Tarreau7838a792019-08-12 18:42:03 +02006528 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006529 return 0;
6530}
6531
6532
Christopher Faulet564e39c2021-09-21 15:50:55 +02006533/* Called from the upper layer, to receive data
6534 *
6535 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
6536 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
6537 * means the caller wants to flush input data (from the mux buffer and the
6538 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
6539 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
6540 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
6541 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
6542 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
6543 * copy as much data as possible.
6544 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006545static size_t h2_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Olivier Houchard511efea2018-08-16 15:30:32 +02006546{
Willy Tarreau36c22322022-05-27 10:41:24 +02006547 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau082f5592018-11-25 08:03:32 +01006548 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01006549 struct htx *h2s_htx = NULL;
6550 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02006551 size_t ret = 0;
6552
Willy Tarreau7838a792019-08-12 18:42:03 +02006553 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
6554
Olivier Houchard511efea2018-08-16 15:30:32 +02006555 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006556 h2s_htx = htx_from_buf(&h2s->rxbuf);
Christopher Fauletec361bb2022-02-21 15:12:54 +01006557 if (htx_is_empty(h2s_htx) && !(h2s_htx->flags & HTX_FL_PARSING_ERROR)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02006558 /* Here htx_to_buf() will set buffer data to 0 because
6559 * the HTX is empty.
6560 */
6561 htx_to_buf(h2s_htx, &h2s->rxbuf);
6562 goto end;
6563 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006564 ret = h2s_htx->data;
6565 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01006566
Christopher Faulet9b79a102019-07-15 11:22:56 +02006567 /* <buf> is empty and the message is small enough, swap the
6568 * buffers. */
6569 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01006570 htx_to_buf(buf_htx, buf);
6571 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006572 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
6573 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01006574 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006575
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006576 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_UNUSED);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006577
6578 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
6579 buf_htx->flags |= HTX_FL_PARSING_ERROR;
6580 if (htx_is_empty(buf_htx))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006581 se_fl_set(h2s->sd, SE_FL_EOI);
Willy Tarreau86724e22018-12-01 23:19:43 +01006582 }
Christopher Faulet72722c02023-02-23 14:26:34 +01006583 else if (htx_is_empty(h2s_htx)) {
Christopher Faulet42432f32020-11-20 17:43:16 +01006584 buf_htx->flags |= (h2s_htx->flags & HTX_FL_EOM);
Christopher Faulet72722c02023-02-23 14:26:34 +01006585 }
6586
Christopher Faulet9b79a102019-07-15 11:22:56 +02006587 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
6588 htx_to_buf(buf_htx, buf);
6589 htx_to_buf(h2s_htx, &h2s->rxbuf);
6590 ret -= h2s_htx->data;
6591
Christopher Faulet37070b22019-02-14 15:12:14 +01006592 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02006593 if (b_data(&h2s->rxbuf))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006594 se_fl_set(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006595 else {
Christopher Faulet34f81d52023-05-04 16:41:37 +02006596 if (!(h2c->flags & H2_CF_IS_BACK) && (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_ES_RCVD))) {
Christopher Faulet4403cdf2023-05-04 15:49:12 +02006597 /* If request ES is reported to the upper layer, it means the
6598 * H2S now expects data from the opposite side.
6599 */
6600 se_expect_data(h2s->sd);
6601 }
6602
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006603 se_fl_clr(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet531dd052023-05-24 11:14:38 +02006604 h2s_propagate_term_flags(h2c, h2s);
Olivier Houchard638b7992018-08-16 15:41:52 +02006605 if (b_size(&h2s->rxbuf)) {
6606 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01006607 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02006608 }
Olivier Houchard511efea2018-08-16 15:30:32 +02006609 }
6610
Willy Tarreau082f5592018-11-25 08:03:32 +01006611 if (ret && h2c->dsi == h2s->id) {
6612 /* demux is blocking on this stream's buffer */
6613 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02006614 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01006615 }
Christopher Faulet37070b22019-02-14 15:12:14 +01006616
Willy Tarreau7838a792019-08-12 18:42:03 +02006617 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02006618 return ret;
6619}
6620
Olivier Houchardd846c262018-10-19 17:24:29 +02006621
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006622/* Called from the upper layer, to send data from buffer <buf> for no more than
6623 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02006624 * flags may be updated on the stream connector.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006625 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006626static size_t h2_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02006627{
Willy Tarreau36c22322022-05-27 10:41:24 +02006628 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau1dc41e72018-06-14 13:21:28 +02006629 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02006630 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006631 struct htx *htx;
6632 struct htx_blk *blk;
6633 enum htx_blk_type btype;
6634 uint32_t bsize;
6635 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006636
Willy Tarreau7838a792019-08-12 18:42:03 +02006637 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
6638
Olivier Houchardd360ac62019-03-22 17:37:16 +01006639 /* If we were not just woken because we wanted to send but couldn't,
6640 * and there's somebody else that is waiting to send, do nothing,
6641 * we will subscribe later and be put at the end of the list
6642 */
Willy Tarreaud9464162020-01-10 18:25:07 +01006643 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02006644 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006645 if (LIST_INLIST(&h2s->list))
6646 TRACE_DEVEL("stream already waiting, leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6647 else {
6648 TRACE_DEVEL("other streams already waiting, going to the queue and leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6649 h2s->h2c->flags |= H2_CF_WAIT_INLIST;
6650 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01006651 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006652 }
Willy Tarreaud9464162020-01-10 18:25:07 +01006653 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02006654
Willy Tarreau7838a792019-08-12 18:42:03 +02006655 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
6656 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 +02006657 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006658 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02006659
Willy Tarreaucab22952019-10-31 15:48:18 +01006660 if (h2s->h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006661 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreaucab22952019-10-31 15:48:18 +01006662 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);
6663 return 0;
6664 }
6665
Christopher Faulet9b79a102019-07-15 11:22:56 +02006666 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006667
Willy Tarreau0bad0432018-06-14 16:54:01 +02006668 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01006669 h2s->flags |= H2_SF_OUTGOING_DATA;
6670
Christopher Faulet2e47e3a2023-01-13 11:40:24 +01006671 if (htx->extra && htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH)
Willy Tarreau48770452022-08-18 16:03:51 +02006672 h2s->flags |= H2_SF_MORE_HTX_DATA;
6673 else
6674 h2s->flags &= ~H2_SF_MORE_HTX_DATA;
6675
Willy Tarreau751f2d02018-10-05 09:35:00 +02006676 if (h2s->id == 0) {
6677 int32_t id = h2c_get_next_sid(h2s->h2c);
6678
6679 if (id < 0) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006680 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02006681 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 +02006682 return 0;
6683 }
6684
6685 eb32_delete(&h2s->by_id);
6686 h2s->by_id.key = h2s->id = id;
6687 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01006688 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02006689 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
6690 }
6691
Christopher Faulet9b79a102019-07-15 11:22:56 +02006692 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
6693 count && !htx_is_empty(htx)) {
6694 idx = htx_get_head(htx);
6695 blk = htx_get_blk(htx, idx);
6696 btype = htx_get_blk_type(blk);
6697 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006698
Christopher Faulet9b79a102019-07-15 11:22:56 +02006699 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02006700 case HTX_BLK_REQ_SL:
6701 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006702 ret = h2s_snd_bhdrs(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02006703 if (ret > 0) {
6704 total += ret;
6705 count -= ret;
6706 if (ret < bsize)
6707 goto done;
6708 }
6709 break;
6710
Willy Tarreau115e83b2018-12-01 19:17:53 +01006711 case HTX_BLK_RES_SL:
6712 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006713 ret = h2s_snd_fhdrs(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01006714 if (ret > 0) {
6715 total += ret;
6716 count -= ret;
6717 if (ret < bsize)
6718 goto done;
6719 }
6720 break;
6721
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006722 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006723 /* all these cause the emission of a DATA frame (possibly empty) */
Christopher Faulet991febd2020-12-02 15:17:31 +01006724 if (!(h2s->h2c->flags & H2_CF_IS_BACK) &&
6725 (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BODYLESS_RESP)) == H2_SF_BODYLESS_RESP)
6726 ret = h2s_skip_data(h2s, buf, count);
6727 else
6728 ret = h2s_make_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006729 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01006730 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006731 total += ret;
6732 count -= ret;
6733 if (ret < bsize)
6734 goto done;
6735 }
6736 break;
6737
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006738 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006739 case HTX_BLK_EOT:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006740 /* This is the first trailers block, all the subsequent ones */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006741 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006742 if (ret > 0) {
6743 total += ret;
6744 count -= ret;
6745 if (ret < bsize)
6746 goto done;
6747 }
6748 break;
6749
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006750 default:
6751 htx_remove_blk(htx, blk);
6752 total += bsize;
6753 count -= bsize;
6754 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006755 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006756 }
6757
Christopher Faulet9b79a102019-07-15 11:22:56 +02006758 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02006759 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02006760 /* trim any possibly pending data after we close (extra CR-LF,
6761 * unprocessed trailers, abnormal extra data, ...)
6762 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02006763 total += count;
6764 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02006765 }
6766
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006767 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01006768 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006769 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 +02006770 se_fl_set_error(h2s->sd);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01006771 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01006772 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006773 }
6774
Christopher Faulet9b79a102019-07-15 11:22:56 +02006775 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02006776
Olivier Houchard7505f942018-08-21 18:10:44 +02006777 if (total > 0) {
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006778 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006779 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 +02006780 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006781 }
Olivier Houchardd846c262018-10-19 17:24:29 +02006782
Olivier Houchard7505f942018-08-21 18:10:44 +02006783 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006784 /* If we're waiting for flow control, and we got a shutr on the
6785 * connection, we will never be unlocked, so add an error on
Willy Tarreau4596fe22022-05-17 19:07:51 +02006786 * the stream connector.
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006787 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02006788 if ((h2s->h2c->flags & H2_CF_RCVD_SHUT) &&
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006789 !b_data(&h2s->h2c->dbuf) &&
6790 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006791 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 +01006792 se_fl_set_error(h2s->sd);
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006793 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006794
Willy Tarreau5723f292020-01-10 15:16:57 +01006795 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
6796 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006797 /* Ok we managed to send something, leave the send_list if we were still there */
Willy Tarreaude4a5382023-10-17 08:25:19 +02006798 h2_remove_from_list(h2s);
6799 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 +01006800 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006801
Willy Tarreau7838a792019-08-12 18:42:03 +02006802 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006803 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02006804}
6805
Willy Tarreau90bffa22022-09-01 19:06:44 +02006806/* appends some info about stream <h2s> to buffer <msg>, or does nothing if
Willy Tarreau7051f732022-09-02 15:22:12 +02006807 * <h2s> is NULL. Returns non-zero if the stream is considered suspicious. May
6808 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
6809 * NULL, otherwise a single line is used.
Willy Tarreau90bffa22022-09-01 19:06:44 +02006810 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006811static int h2_dump_h2s_info(struct buffer *msg, const struct h2s *h2s, const char *pfx)
Willy Tarreau90bffa22022-09-01 19:06:44 +02006812{
6813 int ret = 0;
6814
6815 if (!h2s)
6816 return ret;
6817
Willy Tarreau7051f732022-09-02 15:22:12 +02006818 chunk_appendf(msg, " h2s.id=%d .st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u",
Willy Tarreau90bffa22022-09-01 19:06:44 +02006819 h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
6820 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02006821 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf));
6822
6823 if (pfx)
6824 chunk_appendf(msg, "\n%s", pfx);
6825
6826 chunk_appendf(msg, " .sc=%p", h2s_sc(h2s));
Willy Tarreau90bffa22022-09-01 19:06:44 +02006827 if (h2s_sc(h2s))
6828 chunk_appendf(msg, "(.flg=0x%08x .app=%p)",
6829 h2s_sc(h2s)->flags, h2s_sc(h2s)->app);
6830
Willy Tarreau7051f732022-09-02 15:22:12 +02006831 chunk_appendf(msg, " .sd=%p", h2s->sd);
Willy Tarreau90bffa22022-09-01 19:06:44 +02006832 chunk_appendf(msg, "(.flg=0x%08x)", se_fl_get(h2s->sd));
6833
Willy Tarreau7051f732022-09-02 15:22:12 +02006834 if (pfx)
6835 chunk_appendf(msg, "\n%s", pfx);
6836
Willy Tarreau90bffa22022-09-01 19:06:44 +02006837 chunk_appendf(msg, " .subs=%p", h2s->subs);
6838 if (h2s->subs) {
6839 chunk_appendf(msg, "(ev=%d tl=%p", h2s->subs->events, h2s->subs->tasklet);
6840 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
6841 h2s->subs->tasklet->calls,
6842 h2s->subs->tasklet->context);
6843 if (h2s->subs->tasklet->calls >= 1000000)
6844 ret = 1;
6845 resolve_sym_name(msg, NULL, h2s->subs->tasklet->process);
6846 chunk_appendf(msg, ")");
6847 }
6848 return ret;
6849}
6850
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006851/* appends some info about connection <h2c> to buffer <msg>, or does nothing if
6852 * <h2c> is NULL. Returns non-zero if the connection is considered suspicious.
Willy Tarreau7051f732022-09-02 15:22:12 +02006853 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
6854 * not NULL, otherwise a single line is used.
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006855 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006856static int h2_dump_h2c_info(struct buffer *msg, struct h2c *h2c, const char *pfx)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006857{
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006858 const struct buffer *hmbuf, *tmbuf;
6859 const struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006860 struct eb32_node *node;
6861 int fctl_cnt = 0;
6862 int send_cnt = 0;
6863 int tree_cnt = 0;
6864 int orph_cnt = 0;
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006865 int ret = 0;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006866
6867 if (!h2c)
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006868 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006869
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006870 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006871 fctl_cnt++;
6872
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006873 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006874 send_cnt++;
6875
6876 node = eb32_first(&h2c->streams_by_id);
6877 while (node) {
6878 h2s = container_of(node, struct h2s, by_id);
6879 tree_cnt++;
Willy Tarreau7be4ee02022-05-18 07:31:41 +02006880 if (!h2s_sc(h2s))
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006881 orph_cnt++;
6882 node = eb32_next(node);
6883 }
6884
Willy Tarreau60f62682019-05-26 11:32:27 +02006885 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02006886 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02006887 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau7051f732022-09-02 15:22:12 +02006888 " .nbst=%u .nbsc=%u",
Willy Tarreauab2ec452019-08-30 07:07:08 +02006889 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau7051f732022-09-02 15:22:12 +02006890 h2c->nb_streams, h2c->nb_sc);
6891
6892 if (pfx)
6893 chunk_appendf(msg, "\n%s", pfx);
6894
6895 chunk_appendf(msg, " .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
6896 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u",
6897 fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006898 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01006899 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02006900 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf));
6901
6902 if (pfx)
6903 chunk_appendf(msg, "\n%s", pfx);
6904
Christopher Faulet68ee7842022-10-12 10:21:33 +02006905 chunk_appendf(msg, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau60f62682019-05-26 11:32:27 +02006906 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
6907 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
6908 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02006909 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
6910 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01006911
Willy Tarreauf8c77092022-11-29 15:26:43 +01006912 chunk_appendf(msg, " .task=%p", h2c->task);
6913 if (h2c->task) {
6914 chunk_appendf(msg, " .exp=%s",
6915 h2c->task->expire ? tick_is_expired(h2c->task->expire, now_ms) ? "<PAST>" :
6916 human_time(TICKS_TO_MS(h2c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>");
6917 }
Willy Tarreau7051f732022-09-02 15:22:12 +02006918
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006919 return ret;
6920}
6921
6922/* for debugging with CLI's "show fd" command */
6923static int h2_show_fd(struct buffer *msg, struct connection *conn)
6924{
6925 struct h2c *h2c = conn->ctx;
6926 const struct h2s *h2s;
6927 struct eb32_node *node;
6928 int ret = 0;
6929
6930 if (!h2c)
6931 return ret;
6932
Willy Tarreau7051f732022-09-02 15:22:12 +02006933 ret |= h2_dump_h2c_info(msg, h2c, NULL);
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006934
6935 node = eb32_last(&h2c->streams_by_id);
6936 if (node) {
6937 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau90bffa22022-09-01 19:06:44 +02006938 chunk_appendf(msg, " last_h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02006939 ret |= h2_dump_h2s_info(msg, h2s, NULL);
Willy Tarreau987c0632018-12-18 10:32:05 +01006940 }
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006941
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006942 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006943}
Willy Tarreau62f52692017-10-08 23:01:42 +02006944
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02006945/* for debugging with CLI's "show sess" command. May emit multiple lines, each
6946 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
6947 * line is used. Each field starts with a space so it's safe to print it after
6948 * existing fields.
6949 */
6950static int h2_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
6951{
6952 struct h2s *h2s = sd->se;
6953 int ret = 0;
6954
6955 if (!h2s)
6956 return ret;
6957
6958 chunk_appendf(msg, " h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02006959 ret |= h2_dump_h2s_info(msg, h2s, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02006960 if (pfx)
6961 chunk_appendf(msg, "\n%s", pfx);
6962 chunk_appendf(msg, " h2c=%p", h2s->h2c);
Willy Tarreau7051f732022-09-02 15:22:12 +02006963 ret |= h2_dump_h2c_info(msg, h2s->h2c, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02006964 return ret;
6965}
6966
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006967/* Migrate the the connection to the current thread.
6968 * Return 0 if successful, non-zero otherwise.
6969 * Expected to be called with the old thread lock held.
6970 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02006971static int h2_takeover(struct connection *conn, int orig_tid)
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006972{
6973 struct h2c *h2c = conn->ctx;
Willy Tarreau617e80f2020-07-01 16:39:33 +02006974 struct task *task;
Willy Tarreau960f37c2023-11-17 10:56:33 +01006975 struct task *new_task;
6976 struct tasklet *new_tasklet;
6977
6978 /* Pre-allocate tasks so that we don't have to roll back after the xprt
6979 * has been migrated.
6980 */
6981 new_task = task_new_here();
6982 new_tasklet = tasklet_new();
6983 if (!new_task || !new_tasklet)
6984 goto fail;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006985
6986 if (fd_takeover(conn->handle.fd, conn) != 0)
Willy Tarreau960f37c2023-11-17 10:56:33 +01006987 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006988
6989 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
6990 /* We failed to takeover the xprt, even if the connection may
6991 * still be valid, flag it as error'd, as we have already
6992 * taken over the fd, and wake the tasklet, so that it will
6993 * destroy it.
6994 */
6995 conn->flags |= CO_FL_ERROR;
6996 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
Willy Tarreau960f37c2023-11-17 10:56:33 +01006997 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006998 }
6999
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007000 if (h2c->wait_event.events)
7001 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
7002 h2c->wait_event.events, &h2c->wait_event);
Willy Tarreau617e80f2020-07-01 16:39:33 +02007003
7004 task = h2c->task;
7005 if (task) {
Willy Tarreau960f37c2023-11-17 10:56:33 +01007006 /* only assign a task if there was already one, otherwise
7007 * the preallocated new task will be released.
7008 */
Willy Tarreau617e80f2020-07-01 16:39:33 +02007009 task->context = NULL;
7010 h2c->task = NULL;
7011 __ha_barrier_store();
7012 task_kill(task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007013
Willy Tarreau960f37c2023-11-17 10:56:33 +01007014 h2c->task = new_task;
7015 new_task = NULL;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007016 h2c->task->process = h2_timeout_task;
7017 h2c->task->context = h2c;
7018 }
Willy Tarreau960f37c2023-11-17 10:56:33 +01007019
7020 /* To let the tasklet know it should free itself, and do nothing else,
7021 * set its context to NULL.
7022 */
7023 h2c->wait_event.tasklet->context = NULL;
7024 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
7025
7026 h2c->wait_event.tasklet = new_tasklet;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007027 h2c->wait_event.tasklet->process = h2_io_cb;
7028 h2c->wait_event.tasklet->context = h2c;
7029 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
7030 SUB_RETRY_RECV, &h2c->wait_event);
7031
Willy Tarreau960f37c2023-11-17 10:56:33 +01007032 if (new_task)
7033 __task_free(new_task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007034 return 0;
Willy Tarreau960f37c2023-11-17 10:56:33 +01007035 fail:
7036 if (new_task)
7037 __task_free(new_task);
7038 tasklet_free(new_tasklet);
7039 return -1;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007040}
7041
Willy Tarreau62f52692017-10-08 23:01:42 +02007042/*******************************************************/
7043/* functions below are dedicated to the config parsers */
7044/*******************************************************/
7045
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007046/* config parser for global "tune.h2.header-table-size" */
7047static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007048 const struct proxy *defpx, const char *file, int line,
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007049 char **err)
7050{
7051 if (too_many_args(1, args, err, NULL))
7052 return -1;
7053
7054 h2_settings_header_table_size = atoi(args[1]);
7055 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
7056 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
7057 return -1;
7058 }
7059 return 0;
7060}
Willy Tarreau62f52692017-10-08 23:01:42 +02007061
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007062/* config parser for global "tune.h2.{be.,fe.,}initial-window-size" */
Willy Tarreaue6baec02017-07-27 11:45:11 +02007063static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007064 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue6baec02017-07-27 11:45:11 +02007065 char **err)
7066{
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007067 int *vptr;
7068
Willy Tarreaue6baec02017-07-27 11:45:11 +02007069 if (too_many_args(1, args, err, NULL))
7070 return -1;
7071
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007072 /* backend/frontend/default */
7073 vptr = (args[0][8] == 'b') ? &h2_be_settings_initial_window_size :
7074 (args[0][8] == 'f') ? &h2_fe_settings_initial_window_size :
7075 &h2_settings_initial_window_size;
7076
7077 *vptr = atoi(args[1]);
7078 if (*vptr < 0) {
Willy Tarreaue6baec02017-07-27 11:45:11 +02007079 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7080 return -1;
7081 }
7082 return 0;
7083}
7084
Willy Tarreauca1027c2023-04-18 15:57:03 +02007085/* config parser for global "tune.h2.{be.,fe.,}max-concurrent-streams" */
Willy Tarreau5242ef82017-07-27 11:47:28 +02007086static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007087 const struct proxy *defpx, const char *file, int line,
Willy Tarreau5242ef82017-07-27 11:47:28 +02007088 char **err)
7089{
Willy Tarreauca1027c2023-04-18 15:57:03 +02007090 uint *vptr;
7091
Willy Tarreau5242ef82017-07-27 11:47:28 +02007092 if (too_many_args(1, args, err, NULL))
7093 return -1;
7094
Willy Tarreauca1027c2023-04-18 15:57:03 +02007095 /* backend/frontend/default */
7096 vptr = (args[0][8] == 'b') ? &h2_be_settings_max_concurrent_streams :
7097 (args[0][8] == 'f') ? &h2_fe_settings_max_concurrent_streams :
7098 &h2_settings_max_concurrent_streams;
7099
7100 *vptr = atoi(args[1]);
7101 if ((int)*vptr < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02007102 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7103 return -1;
7104 }
7105 return 0;
7106}
7107
Willy Tarreau4869ed52023-10-13 18:11:59 +02007108/* config parser for global "tune.h2.fe.max-total-streams" */
7109static int h2_parse_max_total_streams(char **args, int section_type, struct proxy *curpx,
7110 const struct proxy *defpx, const char *file, int line,
7111 char **err)
7112{
7113 uint *vptr;
7114
7115 if (too_many_args(1, args, err, NULL))
7116 return -1;
7117
7118 /* frontend only for now */
7119 vptr = &h2_fe_max_total_streams;
7120
7121 *vptr = atoi(args[1]);
7122 if ((int)*vptr < 0) {
7123 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7124 return -1;
7125 }
7126 return 0;
7127}
7128
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007129/* config parser for global "tune.h2.max-frame-size" */
7130static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007131 const struct proxy *defpx, const char *file, int line,
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007132 char **err)
7133{
7134 if (too_many_args(1, args, err, NULL))
7135 return -1;
7136
7137 h2_settings_max_frame_size = atoi(args[1]);
7138 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
7139 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
7140 return -1;
7141 }
7142 return 0;
7143}
7144
Willy Tarreau62f52692017-10-08 23:01:42 +02007145
7146/****************************************/
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05007147/* MUX initialization and instantiation */
Willy Tarreau62f52692017-10-08 23:01:42 +02007148/***************************************/
7149
7150/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01007151static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02007152 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02007153 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02007154 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02007155 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02007156 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02007157 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02007158 .attach = h2_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02007159 .get_first_sc = h2_get_first_sc,
Willy Tarreau62f52692017-10-08 23:01:42 +02007160 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01007161 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01007162 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01007163 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02007164 .shutr = h2_shutr,
7165 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02007166 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007167 .show_fd = h2_show_fd,
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007168 .show_sd = h2_show_sd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007169 .takeover = h2_takeover,
Christopher Fauleta97cced2022-04-12 18:04:10 +02007170 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Willy Tarreau62f52692017-10-08 23:01:42 +02007171 .name = "H2",
7172};
7173
Christopher Faulet32f61c02018-04-10 14:33:41 +02007174static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02007175 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02007176
Willy Tarreau0108d902018-11-25 19:14:37 +01007177INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
7178
Willy Tarreau62f52692017-10-08 23:01:42 +02007179/* config keyword parsers */
7180static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007181 { CFG_GLOBAL, "tune.h2.be.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007182 { CFG_GLOBAL, "tune.h2.be.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007183 { CFG_GLOBAL, "tune.h2.fe.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007184 { CFG_GLOBAL, "tune.h2.fe.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau4869ed52023-10-13 18:11:59 +02007185 { CFG_GLOBAL, "tune.h2.fe.max-total-streams", h2_parse_max_total_streams },
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007186 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02007187 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02007188 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007189 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02007190 { 0, NULL, NULL }
7191}};
7192
Willy Tarreau0108d902018-11-25 19:14:37 +01007193INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007194
7195/* initialize internal structs after the config is parsed.
7196 * Returns zero on success, non-zero on error.
7197 */
7198static int init_h2()
7199{
7200 pool_head_hpack_tbl = create_pool("hpack_tbl",
7201 h2_settings_header_table_size,
7202 MEM_F_SHARED|MEM_F_EXACT);
Christopher Faulet52140992020-11-06 15:23:39 +01007203 if (!pool_head_hpack_tbl) {
7204 ha_alert("failed to allocate hpack_tbl memory pool\n");
7205 return (ERR_ALERT | ERR_FATAL);
7206 }
7207 return ERR_NONE;
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007208}
7209
7210REGISTER_POST_CHECK(init_h2);