blob: 891242e3780b75068f98956d16cd081eee9972c9 [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 Tarreau22892322024-01-17 16:57:23 +010079
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 Tarreau22892322024-01-17 16:57:23 +010084 int glitches; /* total number of glitches on this connection */
85
Willy Tarreau0b37d652018-10-03 10:33:02 +020086 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +010087 struct task *task; /* timeout management task */
Amaury Denoyellec92697d2020-10-27 17:16:01 +010088 struct h2_counters *px_counters; /* h2 counters attached to proxy */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020089 struct eb_root streams_by_id; /* all active streams by their ID */
90 struct list send_list; /* list of blocked streams requesting to send */
91 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau9edf6db2019-10-02 10:49:59 +020092 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Willy Tarreau44e973f2018-03-01 17:49:30 +010093 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +020094 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095};
96
Willy Tarreau2c249eb2019-05-13 18:06:17 +020097
Willy Tarreau18312642017-10-11 07:57:07 +020098/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
Christopher Fauletfafd1b02020-11-03 18:25:52 +010099 * it is being processed in the internal HTTP representation (HTX).
Willy Tarreau18312642017-10-11 07:57:07 +0200100 */
101struct h2s {
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200102 struct sedesc *sd;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100103 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200104 struct h2c *h2c;
Willy Tarreau18312642017-10-11 07:57:07 +0200105 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200106 int32_t id; /* stream ID */
107 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200108 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200109 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
110 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200111 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100112 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200113 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200114 struct wait_event *subs; /* recv wait_event the stream connector associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200115 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau5723f292020-01-10 15:16:57 +0100116 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to send an RST after we failed to,
117 * in case there's no other subscription to do it */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100118
119 char upgrade_protocol[16]; /* rfc 8441: requested protocol on Extended CONNECT */
Willy Tarreau18312642017-10-11 07:57:07 +0200120};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200121
Willy Tarreauc6405142017-09-21 20:23:50 +0200122/* descriptor for an h2 frame header */
123struct h2_fh {
124 uint32_t len; /* length, host order, 24 bits */
125 uint32_t sid; /* stream id, host order, 31 bits */
126 uint8_t ft; /* frame type */
127 uint8_t ff; /* frame flags */
128};
129
Willy Tarreau12ae2122019-08-08 18:23:12 +0200130/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200131static void h2_trace(enum trace_level level, uint64_t mask, \
132 const struct trace_source *src,
133 const struct ist where, const struct ist func,
134 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200135
136/* The event representation is split like this :
137 * strm - application layer
138 * h2s - internal H2 stream
139 * h2c - internal H2 connection
140 * conn - external connection
141 *
142 */
143static const struct trace_event h2_trace_events[] = {
144#define H2_EV_H2C_NEW (1ULL << 0)
Willy Tarreau87951942019-08-30 07:34:36 +0200145 { .mask = H2_EV_H2C_NEW, .name = "h2c_new", .desc = "new H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200146#define H2_EV_H2C_RECV (1ULL << 1)
Willy Tarreau87951942019-08-30 07:34:36 +0200147 { .mask = H2_EV_H2C_RECV, .name = "h2c_recv", .desc = "Rx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200148#define H2_EV_H2C_SEND (1ULL << 2)
Willy Tarreau87951942019-08-30 07:34:36 +0200149 { .mask = H2_EV_H2C_SEND, .name = "h2c_send", .desc = "Tx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200150#define H2_EV_H2C_FCTL (1ULL << 3)
Willy Tarreau87951942019-08-30 07:34:36 +0200151 { .mask = H2_EV_H2C_FCTL, .name = "h2c_fctl", .desc = "H2 connection flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200152#define H2_EV_H2C_BLK (1ULL << 4)
Willy Tarreau87951942019-08-30 07:34:36 +0200153 { .mask = H2_EV_H2C_BLK, .name = "h2c_blk", .desc = "H2 connection blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200154#define H2_EV_H2C_WAKE (1ULL << 5)
Willy Tarreau87951942019-08-30 07:34:36 +0200155 { .mask = H2_EV_H2C_WAKE, .name = "h2c_wake", .desc = "H2 connection woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200156#define H2_EV_H2C_END (1ULL << 6)
Willy Tarreau87951942019-08-30 07:34:36 +0200157 { .mask = H2_EV_H2C_END, .name = "h2c_end", .desc = "H2 connection terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200158#define H2_EV_H2C_ERR (1ULL << 7)
Willy Tarreau87951942019-08-30 07:34:36 +0200159 { .mask = H2_EV_H2C_ERR, .name = "h2c_err", .desc = "error on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200160#define H2_EV_RX_FHDR (1ULL << 8)
Willy Tarreau87951942019-08-30 07:34:36 +0200161 { .mask = H2_EV_RX_FHDR, .name = "rx_fhdr", .desc = "H2 frame header received" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200162#define H2_EV_RX_FRAME (1ULL << 9)
Willy Tarreau87951942019-08-30 07:34:36 +0200163 { .mask = H2_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200164#define H2_EV_RX_EOI (1ULL << 10)
Willy Tarreau87951942019-08-30 07:34:36 +0200165 { .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 +0200166#define H2_EV_RX_PREFACE (1ULL << 11)
Willy Tarreau87951942019-08-30 07:34:36 +0200167 { .mask = H2_EV_RX_PREFACE, .name = "rx_preface", .desc = "receipt of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200168#define H2_EV_RX_DATA (1ULL << 12)
Willy Tarreau87951942019-08-30 07:34:36 +0200169 { .mask = H2_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200170#define H2_EV_RX_HDR (1ULL << 13)
Willy Tarreau87951942019-08-30 07:34:36 +0200171 { .mask = H2_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200172#define H2_EV_RX_PRIO (1ULL << 14)
Willy Tarreau87951942019-08-30 07:34:36 +0200173 { .mask = H2_EV_RX_PRIO, .name = "rx_prio", .desc = "receipt of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200174#define H2_EV_RX_RST (1ULL << 15)
Willy Tarreau87951942019-08-30 07:34:36 +0200175 { .mask = H2_EV_RX_RST, .name = "rx_rst", .desc = "receipt of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200176#define H2_EV_RX_SETTINGS (1ULL << 16)
Willy Tarreau87951942019-08-30 07:34:36 +0200177 { .mask = H2_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200178#define H2_EV_RX_PUSH (1ULL << 17)
Willy Tarreau87951942019-08-30 07:34:36 +0200179 { .mask = H2_EV_RX_PUSH, .name = "rx_push", .desc = "receipt of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200180#define H2_EV_RX_PING (1ULL << 18)
Willy Tarreau87951942019-08-30 07:34:36 +0200181 { .mask = H2_EV_RX_PING, .name = "rx_ping", .desc = "receipt of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200182#define H2_EV_RX_GOAWAY (1ULL << 19)
Willy Tarreau87951942019-08-30 07:34:36 +0200183 { .mask = H2_EV_RX_GOAWAY, .name = "rx_goaway", .desc = "receipt of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200184#define H2_EV_RX_WU (1ULL << 20)
Willy Tarreau87951942019-08-30 07:34:36 +0200185 { .mask = H2_EV_RX_WU, .name = "rx_wu", .desc = "receipt of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200186#define H2_EV_RX_CONT (1ULL << 21)
Willy Tarreau87951942019-08-30 07:34:36 +0200187 { .mask = H2_EV_RX_CONT, .name = "rx_cont", .desc = "receipt of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200188#define H2_EV_TX_FRAME (1ULL << 22)
Willy Tarreau87951942019-08-30 07:34:36 +0200189 { .mask = H2_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200190#define H2_EV_TX_EOI (1ULL << 23)
Willy Tarreau87951942019-08-30 07:34:36 +0200191 { .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 +0200192#define H2_EV_TX_PREFACE (1ULL << 24)
Willy Tarreau87951942019-08-30 07:34:36 +0200193 { .mask = H2_EV_TX_PREFACE, .name = "tx_preface", .desc = "transmission of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200194#define H2_EV_TX_DATA (1ULL << 25)
Willy Tarreau87951942019-08-30 07:34:36 +0200195 { .mask = H2_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200196#define H2_EV_TX_HDR (1ULL << 26)
Willy Tarreau87951942019-08-30 07:34:36 +0200197 { .mask = H2_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200198#define H2_EV_TX_PRIO (1ULL << 27)
Willy Tarreau87951942019-08-30 07:34:36 +0200199 { .mask = H2_EV_TX_PRIO, .name = "tx_prio", .desc = "transmission of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200200#define H2_EV_TX_RST (1ULL << 28)
Willy Tarreau87951942019-08-30 07:34:36 +0200201 { .mask = H2_EV_TX_RST, .name = "tx_rst", .desc = "transmission of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200202#define H2_EV_TX_SETTINGS (1ULL << 29)
Willy Tarreau87951942019-08-30 07:34:36 +0200203 { .mask = H2_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200204#define H2_EV_TX_PUSH (1ULL << 30)
Willy Tarreau87951942019-08-30 07:34:36 +0200205 { .mask = H2_EV_TX_PUSH, .name = "tx_push", .desc = "transmission of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200206#define H2_EV_TX_PING (1ULL << 31)
Willy Tarreau87951942019-08-30 07:34:36 +0200207 { .mask = H2_EV_TX_PING, .name = "tx_ping", .desc = "transmission of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200208#define H2_EV_TX_GOAWAY (1ULL << 32)
Willy Tarreau87951942019-08-30 07:34:36 +0200209 { .mask = H2_EV_TX_GOAWAY, .name = "tx_goaway", .desc = "transmission of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200210#define H2_EV_TX_WU (1ULL << 33)
Willy Tarreau87951942019-08-30 07:34:36 +0200211 { .mask = H2_EV_TX_WU, .name = "tx_wu", .desc = "transmission of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200212#define H2_EV_TX_CONT (1ULL << 34)
Willy Tarreau87951942019-08-30 07:34:36 +0200213 { .mask = H2_EV_TX_CONT, .name = "tx_cont", .desc = "transmission of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200214#define H2_EV_H2S_NEW (1ULL << 35)
Willy Tarreau87951942019-08-30 07:34:36 +0200215 { .mask = H2_EV_H2S_NEW, .name = "h2s_new", .desc = "new H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200216#define H2_EV_H2S_RECV (1ULL << 36)
Willy Tarreau87951942019-08-30 07:34:36 +0200217 { .mask = H2_EV_H2S_RECV, .name = "h2s_recv", .desc = "Rx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200218#define H2_EV_H2S_SEND (1ULL << 37)
Willy Tarreau87951942019-08-30 07:34:36 +0200219 { .mask = H2_EV_H2S_SEND, .name = "h2s_send", .desc = "Tx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200220#define H2_EV_H2S_FCTL (1ULL << 38)
Willy Tarreau87951942019-08-30 07:34:36 +0200221 { .mask = H2_EV_H2S_FCTL, .name = "h2s_fctl", .desc = "H2 stream flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200222#define H2_EV_H2S_BLK (1ULL << 39)
Willy Tarreau87951942019-08-30 07:34:36 +0200223 { .mask = H2_EV_H2S_BLK, .name = "h2s_blk", .desc = "H2 stream blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200224#define H2_EV_H2S_WAKE (1ULL << 40)
Willy Tarreau87951942019-08-30 07:34:36 +0200225 { .mask = H2_EV_H2S_WAKE, .name = "h2s_wake", .desc = "H2 stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200226#define H2_EV_H2S_END (1ULL << 41)
Willy Tarreau87951942019-08-30 07:34:36 +0200227 { .mask = H2_EV_H2S_END, .name = "h2s_end", .desc = "H2 stream terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200228#define H2_EV_H2S_ERR (1ULL << 42)
Willy Tarreau87951942019-08-30 07:34:36 +0200229 { .mask = H2_EV_H2S_ERR, .name = "h2s_err", .desc = "error on H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200230#define H2_EV_STRM_NEW (1ULL << 43)
Willy Tarreau87951942019-08-30 07:34:36 +0200231 { .mask = H2_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200232#define H2_EV_STRM_RECV (1ULL << 44)
Willy Tarreau87951942019-08-30 07:34:36 +0200233 { .mask = H2_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200234#define H2_EV_STRM_SEND (1ULL << 45)
Willy Tarreau87951942019-08-30 07:34:36 +0200235 { .mask = H2_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200236#define H2_EV_STRM_FULL (1ULL << 46)
Willy Tarreau87951942019-08-30 07:34:36 +0200237 { .mask = H2_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200238#define H2_EV_STRM_WAKE (1ULL << 47)
Willy Tarreau87951942019-08-30 07:34:36 +0200239 { .mask = H2_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200240#define H2_EV_STRM_SHUT (1ULL << 48)
Willy Tarreau87951942019-08-30 07:34:36 +0200241 { .mask = H2_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200242#define H2_EV_STRM_END (1ULL << 49)
Willy Tarreau87951942019-08-30 07:34:36 +0200243 { .mask = H2_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200244#define H2_EV_STRM_ERR (1ULL << 50)
Willy Tarreau87951942019-08-30 07:34:36 +0200245 { .mask = H2_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200246#define H2_EV_PROTO_ERR (1ULL << 51)
Willy Tarreau87951942019-08-30 07:34:36 +0200247 { .mask = H2_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200248 { }
249};
250
251static const struct name_desc h2_trace_lockon_args[4] = {
252 /* arg1 */ { /* already used by the connection */ },
253 /* arg2 */ { .name="h2s", .desc="H2 stream" },
254 /* arg3 */ { },
255 /* arg4 */ { }
256};
257
258static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200259#define H2_VERB_CLEAN 1
260 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
261#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200262 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200263#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200264 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200265#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200266 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200267#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200268 { .name="complete", .desc="add full data dump when available" },
269 { /* end */ }
270};
271
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200272static struct trace_source trace_h2 __read_mostly = {
Willy Tarreau12ae2122019-08-08 18:23:12 +0200273 .name = IST("h2"),
274 .desc = "HTTP/2 multiplexer",
275 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200276 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200277 .known_events = h2_trace_events,
278 .lockon_args = h2_trace_lockon_args,
279 .decoding = h2_trace_decoding,
280 .report_events = ~0, // report everything by default
281};
282
283#define TRACE_SOURCE &trace_h2
284INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
285
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100286/* h2 stats module */
287enum {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100288 H2_ST_HEADERS_RCVD,
289 H2_ST_DATA_RCVD,
290 H2_ST_SETTINGS_RCVD,
291 H2_ST_RST_STREAM_RCVD,
292 H2_ST_GOAWAY_RCVD,
293
Amaury Denoyellea8879232020-10-27 17:16:03 +0100294 H2_ST_CONN_PROTO_ERR,
295 H2_ST_STRM_PROTO_ERR,
296 H2_ST_RST_STREAM_RESP,
297 H2_ST_GOAWAY_RESP,
298
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100299 H2_ST_OPEN_CONN,
300 H2_ST_OPEN_STREAM,
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100301 H2_ST_TOTAL_CONN,
302 H2_ST_TOTAL_STREAM,
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100303
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100304 H2_STATS_COUNT /* must be the last member of the enum */
305};
306
307static struct name_desc h2_stats[] = {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100308 [H2_ST_HEADERS_RCVD] = { .name = "h2_headers_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100309 .desc = "Total number of received HEADERS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100310 [H2_ST_DATA_RCVD] = { .name = "h2_data_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100311 .desc = "Total number of received DATA frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100312 [H2_ST_SETTINGS_RCVD] = { .name = "h2_settings_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100313 .desc = "Total number of received SETTINGS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100314 [H2_ST_RST_STREAM_RCVD] = { .name = "h2_rst_stream_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100315 .desc = "Total number of received RST_STREAM frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100316 [H2_ST_GOAWAY_RCVD] = { .name = "h2_goaway_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100317 .desc = "Total number of received GOAWAY frames" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100318
319 [H2_ST_CONN_PROTO_ERR] = { .name = "h2_detected_conn_protocol_errors",
320 .desc = "Total number of connection protocol errors" },
321 [H2_ST_STRM_PROTO_ERR] = { .name = "h2_detected_strm_protocol_errors",
322 .desc = "Total number of stream protocol errors" },
323 [H2_ST_RST_STREAM_RESP] = { .name = "h2_rst_stream_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100324 .desc = "Total number of RST_STREAM sent on detected error" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100325 [H2_ST_GOAWAY_RESP] = { .name = "h2_goaway_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100326 .desc = "Total number of GOAWAY sent on detected error" },
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100327
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100328 [H2_ST_OPEN_CONN] = { .name = "h2_open_connections",
329 .desc = "Count of currently open connections" },
330 [H2_ST_OPEN_STREAM] = { .name = "h2_backend_open_streams",
331 .desc = "Count of currently open streams" },
Amaury Denoyelle377d8782021-02-03 16:27:22 +0100332 [H2_ST_TOTAL_CONN] = { .name = "h2_total_connections",
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100333 .desc = "Total number of connections" },
Amaury Denoyelle377d8782021-02-03 16:27:22 +0100334 [H2_ST_TOTAL_STREAM] = { .name = "h2_backend_total_streams",
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100335 .desc = "Total number of streams" },
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100336};
337
338static struct h2_counters {
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100339 long long headers_rcvd; /* total number of HEADERS frame received */
340 long long data_rcvd; /* total number of DATA frame received */
341 long long settings_rcvd; /* total number of SETTINGS frame received */
342 long long rst_stream_rcvd; /* total number of RST_STREAM frame received */
343 long long goaway_rcvd; /* total number of GOAWAY frame received */
Amaury Denoyellea8879232020-10-27 17:16:03 +0100344
345 long long conn_proto_err; /* total number of protocol errors detected */
346 long long strm_proto_err; /* total number of protocol errors detected */
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100347 long long rst_stream_resp; /* total number of RST_STREAM frame sent on error */
348 long long goaway_resp; /* total number of GOAWAY frame sent on error */
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100349
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100350 long long open_conns; /* count of currently open connections */
351 long long open_streams; /* count of currently open streams */
352 long long total_conns; /* total number of connections */
353 long long total_streams; /* total number of streams */
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100354} h2_counters;
355
356static void h2_fill_stats(void *data, struct field *stats)
357{
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100358 struct h2_counters *counters = data;
359
360 stats[H2_ST_HEADERS_RCVD] = mkf_u64(FN_COUNTER, counters->headers_rcvd);
361 stats[H2_ST_DATA_RCVD] = mkf_u64(FN_COUNTER, counters->data_rcvd);
362 stats[H2_ST_SETTINGS_RCVD] = mkf_u64(FN_COUNTER, counters->settings_rcvd);
363 stats[H2_ST_RST_STREAM_RCVD] = mkf_u64(FN_COUNTER, counters->rst_stream_rcvd);
364 stats[H2_ST_GOAWAY_RCVD] = mkf_u64(FN_COUNTER, counters->goaway_rcvd);
Amaury Denoyellea8879232020-10-27 17:16:03 +0100365
366 stats[H2_ST_CONN_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->conn_proto_err);
367 stats[H2_ST_STRM_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->strm_proto_err);
368 stats[H2_ST_RST_STREAM_RESP] = mkf_u64(FN_COUNTER, counters->rst_stream_resp);
369 stats[H2_ST_GOAWAY_RESP] = mkf_u64(FN_COUNTER, counters->goaway_resp);
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100370
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100371 stats[H2_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
372 stats[H2_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
373 stats[H2_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
374 stats[H2_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100375}
376
377static struct stats_module h2_stats_module = {
378 .name = "h2",
379 .fill_stats = h2_fill_stats,
380 .stats = h2_stats,
381 .stats_count = H2_STATS_COUNT,
382 .counters = &h2_counters,
383 .counters_size = sizeof(h2_counters),
384 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
385 .clearable = 1,
386};
387
388INITCALL1(STG_REGISTER, stats_register_module, &h2_stats_module);
389
Willy Tarreau8ceae722018-11-26 11:58:30 +0100390/* the h2c connection pool */
391DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
392
393/* the h2s stream pool */
394DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
395
Willy Tarreaudc572362018-12-12 08:08:05 +0100396/* The default connection window size is 65535, it may only be enlarged using
397 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
398 * we'll pretend we already received the difference between the two to send
399 * an equivalent window update to enlarge it to 2G-1.
400 */
401#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
402
Willy Tarreau455d5682019-05-24 19:42:18 +0200403/* maximum amount of data we're OK with re-aligning for buffer optimizations */
404#define MAX_DATA_REALIGN 1024
405
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200406/* a few settings from the global section */
407static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreau9d7abda2023-04-17 15:04:34 +0200408static int h2_settings_initial_window_size = 65536; /* default initial value */
409static int h2_be_settings_initial_window_size = 0; /* backend's default initial value */
410static int h2_fe_settings_initial_window_size = 0; /* frontend's default initial value */
Willy Tarreau92f287b2024-03-11 07:33:44 +0100411static int h2_be_glitches_threshold = 0; /* backend's max glitches: unlimited */
412static int h2_fe_glitches_threshold = 0; /* frontend's max glitches: unlimited */
Willy Tarreauca1027c2023-04-18 15:57:03 +0200413static unsigned int h2_settings_max_concurrent_streams = 100; /* default value */
414static unsigned int h2_be_settings_max_concurrent_streams = 0; /* backend value */
415static unsigned int h2_fe_settings_max_concurrent_streams = 0; /* frontend value */
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100416static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200417
Willy Tarreau4869ed52023-10-13 18:11:59 +0200418/* other non-protocol settings */
419static unsigned int h2_fe_max_total_streams = 0; /* frontend value */
420
Willy Tarreaub22b5f02022-05-10 14:57:16 +0200421/* a dummy closed endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +0200422static const struct sedesc closed_ep = {
Willy Tarreauc1054922022-05-18 07:43:52 +0200423 .sc = NULL,
Willy Tarreaub605c422022-05-17 17:04:55 +0200424 .flags = SE_FL_DETACHED,
Willy Tarreaub22b5f02022-05-10 14:57:16 +0200425};
426
Willy Tarreau2a856182017-05-16 15:20:39 +0200427/* a dmumy closed stream */
428static const struct h2s *h2_closed_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200429 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau2a856182017-05-16 15:20:39 +0200430 .h2c = NULL,
431 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100432 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100433 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200434 .id = 0,
435};
436
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100437/* a dmumy closed stream returning a PROTOCOL_ERROR error */
438static const struct h2s *h2_error_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200439 .sd = (struct sedesc *)&closed_ep,
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100440 .h2c = NULL,
441 .st = H2_SS_CLOSED,
442 .errcode = H2_ERR_PROTOCOL_ERROR,
443 .flags = 0,
444 .id = 0,
445};
446
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100447/* a dmumy closed stream returning a REFUSED_STREAM error */
448static const struct h2s *h2_refused_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200449 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100450 .h2c = NULL,
451 .st = H2_SS_CLOSED,
452 .errcode = H2_ERR_REFUSED_STREAM,
453 .flags = 0,
454 .id = 0,
455};
456
Willy Tarreau2a856182017-05-16 15:20:39 +0200457/* and a dummy idle stream for use with any unannounced stream */
458static const struct h2s *h2_idle_stream = &(const struct h2s){
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200459 .sd = (struct sedesc *)&closed_ep,
Willy Tarreau2a856182017-05-16 15:20:39 +0200460 .h2c = NULL,
461 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100462 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200463 .id = 0,
464};
465
Willy Tarreaude4a5382023-10-17 08:25:19 +0200466
Willy Tarreau144f84a2021-03-02 16:09:26 +0100467struct task *h2_timeout_task(struct task *t, void *context, unsigned int state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200468static int h2_send(struct h2c *h2c);
469static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200470static int h2_process(struct h2c *h2c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100471/* h2_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100472struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100473static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau7cfbb812023-01-26 16:02:01 +0100474static 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 +0100475static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100476struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state);
Willy Tarreau36c22322022-05-27 10:41:24 +0200477static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct stconn *sc, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100478static void h2s_alert(struct h2s *h2s);
Willy Tarreaude4a5382023-10-17 08:25:19 +0200479static inline void h2_remove_from_list(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200480
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200481/* returns the stconn associated to the H2 stream */
482static forceinline struct stconn *h2s_sc(const struct h2s *h2s)
483{
Willy Tarreau95acc8b2022-05-27 16:14:10 +0200484 return h2s->sd->sc;
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200485}
486
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200487/* the H2 traces always expect that arg1, if non-null, is of type connection
488 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
489 * that arg3, if non-null, is either of type htx for tx headers, or of type
490 * buffer for everything else.
491 */
492static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
493 const struct ist where, const struct ist func,
494 const void *a1, const void *a2, const void *a3, const void *a4)
495{
496 const struct connection *conn = a1;
497 const struct h2c *h2c = conn ? conn->ctx : NULL;
498 const struct h2s *h2s = a2;
499 const struct buffer *buf = a3;
500 const struct htx *htx;
501 int pos;
502
503 if (!h2c) // nothing to add
504 return;
505
Willy Tarreau17104d42019-08-30 07:12:55 +0200506 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau73db4342019-09-25 07:28:44 +0200507 chunk_appendf(&trace_buf, " : h2c=%p(%c,%s)", h2c, conn_is_back(conn) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
508
Willy Tarreau8e6f7492021-06-16 17:47:24 +0200509 if (mask & H2_EV_H2C_NEW) // inside h2_init, otherwise it's hard to match conn & h2c
510 conn_append_debug_info(&trace_buf, conn, " : ");
511
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100512 if (h2c->errcode)
513 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2c->errcode), h2c->errcode);
514
Willy Tarreau22892322024-01-17 16:57:23 +0100515 if (h2c->glitches)
516 chunk_appendf(&trace_buf, " glitches=%d", h2c->glitches);
517
Willy Tarreau0f458712022-08-18 11:19:57 +0200518 if (h2c->flags & H2_CF_DEM_IN_PROGRESS && // frame processing has started, type and length are valid
Willy Tarreau73db4342019-09-25 07:28:44 +0200519 (mask & (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) == (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) {
Willy Tarreau8520d872020-09-18 07:39:29 +0200520 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 +0200521 }
522
523 if (h2s) {
524 if (h2s->id <= 0)
525 chunk_appendf(&trace_buf, " dsi=%d", h2c->dsi);
Willy Tarreauf9f44992023-02-20 16:57:47 +0100526 if (h2s == h2_idle_stream)
527 chunk_appendf(&trace_buf, " h2s=IDL");
Willy Tarreau4ba6f9e2023-10-20 17:32:13 +0200528 else if (h2s != h2_closed_stream && h2s != h2_refused_stream && h2s != h2_error_stream)
Willy Tarreauf9f44992023-02-20 16:57:47 +0100529 chunk_appendf(&trace_buf, " h2s=%p(%d,%s)", h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreau4ba6f9e2023-10-20 17:32:13 +0200530 else if (h2c->dsi > 0) // don't show that before sid is known
531 chunk_appendf(&trace_buf, " h2s=CLO");
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100532 if (h2s->id && h2s->errcode)
533 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2s->errcode), h2s->errcode);
Willy Tarreau73db4342019-09-25 07:28:44 +0200534 }
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200535 }
536
537 /* Let's dump decoded requests and responses right after parsing. They
538 * are traced at level USER with a few recognizable flags.
539 */
540 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
541 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
542 htx = htxbuf(buf); // recv req/res
543 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
544 htx = a3; // send req/res
545 else
546 htx = NULL;
547
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200548 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200549 const struct htx_blk *blk = htx_get_blk(htx, pos);
550 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
551 enum htx_blk_type type = htx_get_blk_type(blk);
552
553 if (type == HTX_BLK_REQ_SL)
554 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200555 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200556 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
557 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
558 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
559 else if (type == HTX_BLK_RES_SL)
560 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200561 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200562 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
563 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
564 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
565 }
566}
567
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200568
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100569/* Detect a pending read0 for a H2 connection. It happens if a read0 was
570 * already reported on a previous xprt->rcvbuf() AND a frame parser failed
571 * to parse pending data, confirming no more progress is possible because
572 * we're facing a truncated frame. The function returns 1 to report a read0
573 * or 0 otherwise.
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200574 */
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100575static inline int h2c_read0_pending(struct h2c *h2c)
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200576{
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100577 return !!(h2c->flags & H2_CF_END_REACHED);
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200578}
579
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200580/* returns true if the connection is allowed to expire, false otherwise. A
Willy Tarreau34395832022-03-18 14:59:54 +0100581 * connection may expire when it has no attached streams. As long as streams
582 * are attached, the application layer is responsible for timeout management,
583 * and each layer will detach when it doesn't want to wait anymore. When the
584 * last one leaves, the connection must take over timeout management.
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200585 */
586static inline int h2c_may_expire(const struct h2c *h2c)
587{
Willy Tarreau36c22322022-05-27 10:41:24 +0200588 return !h2c->nb_sc;
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200589}
590
Willy Tarreauca1027c2023-04-18 15:57:03 +0200591/* returns the number of max concurrent streams permitted on a connection,
592 * depending on its side (frontend or backend), falling back to the default
593 * h2_settings_max_concurrent_streams. It may even be zero.
594 */
595static inline int h2c_max_concurrent_streams(const struct h2c *h2c)
596{
597 int ret;
598
599 ret = (h2c->flags & H2_CF_IS_BACK) ?
600 h2_be_settings_max_concurrent_streams :
601 h2_fe_settings_max_concurrent_streams;
602
603 ret = ret ? ret : h2_settings_max_concurrent_streams;
604 return ret;
605}
606
Willy Tarreau15a47332022-03-18 15:57:34 +0100607/* update h2c timeout if needed */
608static void h2c_update_timeout(struct h2c *h2c)
609{
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200610 int is_idle_conn = 0;
611
Willy Tarreau15a47332022-03-18 15:57:34 +0100612 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
613
614 if (!h2c->task)
615 goto leave;
616
617 if (h2c_may_expire(h2c)) {
618 /* no more streams attached */
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200619 if (br_data(h2c->mbuf)) {
Willy Tarreau15a47332022-03-18 15:57:34 +0100620 /* pending output data: always the regular data timeout */
621 h2c->task->expire = tick_add_ifset(now_ms, h2c->timeout);
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200622 } else {
623 /* no stream, no output data */
624 if (!(h2c->flags & H2_CF_IS_BACK)) {
625 int to;
Willy Tarreau86b08a32022-04-13 17:40:28 +0200626
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200627 if (h2c->max_id > 0 && !b_data(&h2c->dbuf) &&
628 tick_isset(h2c->proxy->timeout.httpka)) {
629 /* idle after having seen one stream => keep-alive */
630 to = h2c->proxy->timeout.httpka;
631 } else {
632 /* before first request, or started to deserialize a
633 * new req => http-request.
634 */
635 to = h2c->proxy->timeout.httpreq;
636 }
Willy Tarreau86b08a32022-04-13 17:40:28 +0200637
Willy Tarreaud38d8c62023-05-15 11:28:48 +0200638 h2c->task->expire = tick_add_ifset(h2c->idle_start, to);
639 is_idle_conn = 1;
640 }
641
642 if (h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) {
643 /* GOAWAY sent (or failed), closing in progress */
644 int exp = tick_add_ifset(now_ms, h2c->shut_timeout);
645
646 h2c->task->expire = tick_first(h2c->task->expire, exp);
647 is_idle_conn = 1;
648 }
649
650 /* if a timeout above was not set, fall back to the default one */
651 if (!tick_isset(h2c->task->expire))
652 h2c->task->expire = tick_add_ifset(now_ms, h2c->timeout);
Willy Tarreau15a47332022-03-18 15:57:34 +0100653 }
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +0200654
655 if ((h2c->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
656 is_idle_conn && tick_isset(global.close_spread_end)) {
657 /* If a soft-stop is in progress and a close-spread-time
658 * is set, we want to spread idle connection closing roughly
659 * evenly across the defined window. This should only
660 * act on idle frontend connections.
661 * If the window end is already in the past, we wake the
662 * timeout task up immediately so that it can be closed.
663 */
664 int remaining_window = tick_remain(now_ms, global.close_spread_end);
665 if (remaining_window) {
666 /* We don't need to reset the expire if it would
667 * already happen before the close window end.
668 */
669 if (tick_isset(h2c->task->expire) &&
670 tick_is_le(global.close_spread_end, h2c->task->expire)) {
671 /* Set an expire value shorter than the current value
672 * because the close spread window end comes earlier.
673 */
674 h2c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window));
675 }
676 }
677 else {
678 /* We are past the soft close window end, wake the timeout
679 * task up immediately.
680 */
681 task_wakeup(h2c->task, TASK_WOKEN_TIMER);
682 }
683 }
684
Willy Tarreau15a47332022-03-18 15:57:34 +0100685 } else {
686 h2c->task->expire = TICK_ETERNITY;
687 }
688 task_queue(h2c->task);
689 leave:
690 TRACE_LEAVE(H2_EV_H2C_WAKE);
691}
692
Olivier Houchard7a977432019-03-21 15:47:13 +0100693static __inline int
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200694h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100695{
696 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
Christopher Fauletff7925d2022-10-11 19:12:40 +0200697 ((h2c->flags & H2_CF_ERROR) || /* errors close immediately */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +0200698 (h2c->flags & H2_CF_ERR_PENDING && h2c->st0 < H2_CS_FRAME_H) || /* early error during connect */
Olivier Houchard7a977432019-03-21 15:47:13 +0100699 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
700 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200701 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Christopher Fauletff7925d2022-10-11 19:12:40 +0200702 ((h2c->flags & H2_CF_RCVD_SHUT) ||
Olivier Houchard7a977432019-03-21 15:47:13 +0100703 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
704 return 1;
705
706 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100707}
708
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200709/*****************************************************/
710/* functions below are for dynamic buffer management */
711/*****************************************************/
712
Willy Tarreau315d8072017-12-10 22:17:57 +0100713/* indicates whether or not the we may call the h2_recv() function to attempt
714 * to receive data into the buffer and/or demux pending data. The condition is
715 * a bit complex due to some API limits for now. The rules are the following :
716 * - if an error or a shutdown was detected on the connection and the buffer
717 * is empty, we must not attempt to receive
718 * - if the demux buf failed to be allocated, we must not try to receive and
719 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100720 * - if no flag indicates a blocking condition, we may attempt to receive,
721 * regardless of whether the demux buffer is full or not, so that only
722 * de demux part decides whether or not to block. This is needed because
723 * the connection API indeed prevents us from re-enabling receipt that is
724 * already enabled in a polled state, so we must always immediately stop
725 * as soon as the demux can't proceed so as never to hit an end of read
726 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100727 * - otherwise must may not attempt
728 */
729static inline int h2_recv_allowed(const struct h2c *h2c)
730{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200731 if (b_data(&h2c->dbuf) == 0 &&
Christopher Fauletff7925d2022-10-11 19:12:40 +0200732 ((h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR)) || h2c->st0 >= H2_CS_ERROR))
Willy Tarreau315d8072017-12-10 22:17:57 +0100733 return 0;
734
735 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100736 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100737 return 1;
738
739 return 0;
740}
741
Willy Tarreau47b515a2018-12-21 16:09:41 +0100742/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200743static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100744{
745 if (!h2_recv_allowed(h2c))
746 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200747 if ((!consider_buffer || !b_data(&h2c->dbuf))
748 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100749 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200750 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100751}
752
753
Willy Tarreau4596fe22022-05-17 19:07:51 +0200754/* returns true if the front connection has too many stream connectors attached */
Willy Tarreau36c22322022-05-27 10:41:24 +0200755static inline int h2_frt_has_too_many_sc(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200756{
Willy Tarreauca1027c2023-04-18 15:57:03 +0200757 return h2c->nb_sc > h2c_max_concurrent_streams(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +0200758}
759
Willy Tarreau44e973f2018-03-01 17:49:30 +0100760/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
761 * flags are used to figure what buffer was requested. It returns 1 if the
762 * allocation succeeds, in which case the connection is woken up, or 0 if it's
763 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200764 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100765static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200766{
767 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100768 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200769
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100770 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc(&h2c->dbuf)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200771 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200772 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200773 return 1;
774 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200775
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100776 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc(br_tail(h2c->mbuf))) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100777 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200778
779 if (h2c->flags & H2_CF_DEM_MROOM) {
780 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200781 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200782 }
Willy Tarreau14398122017-09-22 14:26:04 +0200783 return 1;
784 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100785
786 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
Willy Tarreau7be4ee02022-05-18 07:31:41 +0200787 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s_sc(h2s) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100788 b_alloc(&h2s->rxbuf)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100789 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200790 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100791 return 1;
792 }
793
Willy Tarreau14398122017-09-22 14:26:04 +0200794 return 0;
795}
796
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200797static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200798{
799 struct buffer *buf = NULL;
800
Willy Tarreau2b718102021-04-21 07:32:39 +0200801 if (likely(!LIST_INLIST(&h2c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100802 unlikely((buf = b_alloc(bptr)) == NULL)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100803 h2c->buf_wait.target = h2c;
804 h2c->buf_wait.wakeup_cb = h2_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200805 LIST_APPEND(&th_ctx->buffer_wq, &h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200806 }
807 return buf;
808}
809
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200810static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200811{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200812 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100813 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100814 offer_buffers(NULL, 1);
Willy Tarreau14398122017-09-22 14:26:04 +0200815 }
816}
817
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200818static inline void h2_release_mbuf(struct h2c *h2c)
819{
820 struct buffer *buf;
821 unsigned int count = 0;
822
823 while (b_size(buf = br_head_pick(h2c->mbuf))) {
824 b_free(buf);
825 count++;
826 }
827 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100828 offer_buffers(NULL, count);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200829}
830
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100831/* returns the number of allocatable outgoing streams for the connection taking
832 * the last_sid and the reserved ones into account.
833 */
834static inline int h2_streams_left(const struct h2c *h2c)
835{
836 int ret;
837
838 /* consider the number of outgoing streams we're allowed to create before
839 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
840 * nb_reserved is the number of streams which don't yet have an ID.
841 */
842 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
843 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
844 if (ret < 0)
845 ret = 0;
846 return ret;
847}
848
Willy Tarreau00f18a32019-01-26 12:19:01 +0100849/* returns the number of streams in use on a connection to figure if it's
Willy Tarreau36c22322022-05-27 10:41:24 +0200850 * idle or not. We check nb_sc and not nb_streams as the caller will want
Willy Tarreau00f18a32019-01-26 12:19:01 +0100851 * to know if it was the last one after a detach().
852 */
853static int h2_used_streams(struct connection *conn)
854{
855 struct h2c *h2c = conn->ctx;
856
Willy Tarreau36c22322022-05-27 10:41:24 +0200857 return h2c->nb_sc;
Willy Tarreau00f18a32019-01-26 12:19:01 +0100858}
859
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100860/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100861static int h2_avail_streams(struct connection *conn)
862{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100863 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100864 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100865 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100866
Willy Tarreau6afec462019-01-28 06:40:19 +0100867 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
868 * streams on the connection.
869 */
870 if (h2c->last_sid >= 0)
871 return 0;
872
Willy Tarreauc61966f2019-10-31 15:10:03 +0100873 if (h2c->st0 >= H2_CS_ERROR)
874 return 0;
875
Willy Tarreau86949782019-01-31 10:42:05 +0100876 /* note: may be negative if a SETTINGS frame changes the limit */
877 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100878
879 /* we must also consider the limit imposed by stream IDs */
880 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100881 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100882 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100883 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
884 ret1 = MIN(ret1, ret2);
885 }
886 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100887}
888
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200889/* Unconditionally produce a trace of the header. Please do not call this one
Willy Tarreau11e8a8c2023-01-24 19:43:11 +0100890 * and use h2_trace_header() instead which first checks if traces are enabled.
891 */
892void _h2_trace_header(const struct ist hn, const struct ist hv,
893 uint64_t mask, const struct ist trc_loc, const char *func,
894 const struct h2c *h2c, const struct h2s *h2s)
895{
896 struct ist n_ist, v_ist;
897 const char *c_str, *s_str;
898
899 chunk_reset(&trash);
900 c_str = chunk_newstr(&trash);
901 if (h2c) {
902 chunk_appendf(&trash, "h2c=%p(%c,%s) ",
903 h2c, (h2c->flags & H2_CF_IS_BACK) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
904 }
905
906 s_str = chunk_newstr(&trash);
907 if (h2s) {
908 if (h2s->id <= 0)
909 chunk_appendf(&trash, "dsi=%d ", h2s->h2c->dsi);
910 chunk_appendf(&trash, "h2s=%p(%d,%s) ", h2s, h2s->id, h2s_st_to_str(h2s->st));
911 }
912 else if (h2c)
913 chunk_appendf(&trash, "dsi=%d ", h2c->dsi);
914
915 n_ist = ist2(chunk_newstr(&trash), 0);
916 istscpy(&n_ist, hn, 256);
917 trash.data += n_ist.len;
918 if (n_ist.len != hn.len)
919 chunk_appendf(&trash, " (... +%ld)", (long)(hn.len - n_ist.len));
920
921 v_ist = ist2(chunk_newstr(&trash), 0);
922 istscpy(&v_ist, hv, 1024);
923 trash.data += v_ist.len;
924 if (v_ist.len != hv.len)
925 chunk_appendf(&trash, " (... +%ld)", (long)(hv.len - v_ist.len));
926
927 TRACE_PRINTF_LOC(TRACE_LEVEL_USER, mask, trc_loc, func,
Christopher Fauletc2545162023-01-30 08:26:09 +0100928 (h2c ? h2c->conn : 0), 0, 0, 0,
Willy Tarreau11e8a8c2023-01-24 19:43:11 +0100929 "%s%s%s %s: %s", c_str, s_str,
930 (mask & H2_EV_TX_HDR) ? "sndh" : "rcvh",
931 n_ist.ptr, v_ist.ptr);
932}
933
934/* produce a trace of the header after checking that tracing is enabled */
935static inline void h2_trace_header(const struct ist hn, const struct ist hv,
936 uint64_t mask, const struct ist trc_loc, const char *func,
937 const struct h2c *h2c, const struct h2s *h2s)
938{
939 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
940 TRACE_ENABLED(TRACE_LEVEL_USER, mask, h2c ? h2c->conn : 0, h2s, 0, 0))
941 _h2_trace_header(hn, hv, mask, trc_loc, func, h2c, h2s);
942}
943
944/* hpack-encode header name <hn> and value <hv>, possibly emitting a trace if
945 * currently enabled. This is done on behalf of function <func> at <trc_loc>
946 * passed as ist(TRC_LOC), h2c <h2c>, and h2s <h2s>, all of which may be NULL.
947 * The trace is only emitted if the header is emitted (in which case non-zero
948 * is returned). The trash is modified. In the traces, the header's name will
949 * be truncated to 256 chars and the header's value to 1024 chars.
950 */
951static inline int h2_encode_header(struct buffer *buf, const struct ist hn, const struct ist hv,
952 uint64_t mask, const struct ist trc_loc, const char *func,
953 const struct h2c *h2c, const struct h2s *h2s)
954{
955 int ret;
956
957 ret = hpack_encode_header(buf, hn, hv);
958 if (ret)
959 h2_trace_header(hn, hv, mask, trc_loc, func, h2c, h2s);
960
961 return ret;
962}
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200963
Willy Tarreau62f52692017-10-08 23:01:42 +0200964/*****************************************************************/
965/* functions below are dedicated to the mux setup and management */
966/*****************************************************************/
967
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200968/* Initialize the mux once it's attached. For outgoing connections, the context
969 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200970 * connections from the fact that the context is still NULL (even during mux
971 * upgrades). <input> is always used as Input buffer and may contain data. It is
972 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200973 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200974static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
975 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200976{
977 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100978 struct task *t = NULL;
Christopher Fauletf81ef032019-10-04 15:19:43 +0200979 void *conn_ctx = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200980
Christopher Fauletf81ef032019-10-04 15:19:43 +0200981 TRACE_ENTER(H2_EV_H2C_NEW);
Willy Tarreau7838a792019-08-12 18:42:03 +0200982
Willy Tarreaubafbe012017-11-24 17:34:44 +0100983 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200984 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200985 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200986
Christopher Faulete9b70722019-04-08 10:46:02 +0200987 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200988 h2c->flags = H2_CF_IS_BACK;
989 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
990 if (tick_isset(prx->timeout.serverfin))
991 h2c->shut_timeout = prx->timeout.serverfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100992
993 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
994 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200995 } else {
996 h2c->flags = H2_CF_NONE;
997 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
998 if (tick_isset(prx->timeout.clientfin))
999 h2c->shut_timeout = prx->timeout.clientfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +01001000
1001 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
1002 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +02001003 }
Willy Tarreau3f133572017-10-31 19:21:06 +01001004
Willy Tarreau0b37d652018-10-03 10:33:02 +02001005 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +01001006 h2c->task = NULL;
Willy Tarreau389ab0d2023-03-20 19:16:04 +01001007 h2c->wait_event.tasklet = NULL;
Willy Tarreau15a47332022-03-18 15:57:34 +01001008 h2c->idle_start = now_ms;
Willy Tarreau3f133572017-10-31 19:21:06 +01001009 if (tick_isset(h2c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001010 t = task_new_here();
Willy Tarreau3f133572017-10-31 19:21:06 +01001011 if (!t)
1012 goto fail;
1013
1014 h2c->task = t;
1015 t->process = h2_timeout_task;
1016 t->context = h2c;
1017 t->expire = tick_add(now_ms, h2c->timeout);
1018 }
Willy Tarreauea392822017-10-31 10:02:25 +01001019
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001020 h2c->wait_event.tasklet = tasklet_new();
1021 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001022 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001023 h2c->wait_event.tasklet->process = h2_io_cb;
1024 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001025 h2c->wait_event.events = 0;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001026 if (!conn_is_back(conn)) {
1027 /* Connection might already be in the stopping_list if subject
1028 * to h1->h2 upgrade.
1029 */
1030 if (!LIST_INLIST(&conn->stopping_list)) {
1031 LIST_APPEND(&mux_stopping_data[tid].list,
1032 &conn->stopping_list);
1033 }
1034 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001035
Willy Tarreau2bdcc702020-05-19 11:31:11 +02001036 h2c->ddht = hpack_dht_alloc();
Willy Tarreau32218eb2017-09-22 08:07:25 +02001037 if (!h2c->ddht)
1038 goto fail;
1039
1040 /* Initialise the context. */
1041 h2c->st0 = H2_CS_PREFACE;
1042 h2c->conn = conn;
Willy Tarreauca1027c2023-04-18 15:57:03 +02001043 h2c->streams_limit = h2c_max_concurrent_streams(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001044 h2c->max_id = -1;
1045 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +01001046 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001047 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +01001048 h2c->nb_streams = 0;
Willy Tarreau36c22322022-05-27 10:41:24 +02001049 h2c->nb_sc = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001050 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001051 h2c->stream_cnt = 0;
Willy Tarreau22892322024-01-17 16:57:23 +01001052 h2c->glitches = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001053
Christopher Faulet51f73eb2019-04-08 11:22:47 +02001054 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001055 h2c->dsi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001056
Willy Tarreau32218eb2017-09-22 08:07:25 +02001057 h2c->last_sid = -1;
1058
Willy Tarreau51330962019-05-26 09:38:07 +02001059 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +02001060 h2c->miw = 65535; /* mux initial window size */
1061 h2c->mws = 65535; /* mux window size */
1062 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +02001063 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +02001064 LIST_INIT(&h2c->send_list);
1065 LIST_INIT(&h2c->fctl_list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +02001066 LIST_INIT(&h2c->blocked_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +01001067 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001068
Christopher Fauletf81ef032019-10-04 15:19:43 +02001069 conn->ctx = h2c;
1070
Willy Tarreau8e6f7492021-06-16 17:47:24 +02001071 TRACE_USER("new H2 connection", H2_EV_H2C_NEW, conn);
1072
Willy Tarreau3f133572017-10-31 19:21:06 +01001073 if (t)
1074 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +01001075
Willy Tarreau01b44822018-10-03 14:26:37 +02001076 if (h2c->flags & H2_CF_IS_BACK) {
1077 /* FIXME: this is temporary, for outgoing connections we need
1078 * to immediately allocate a stream until the code is modified
Willy Tarreau36c22322022-05-27 10:41:24 +02001079 * so that the caller calls ->attach(). For now the outgoing sc
Christopher Fauletf81ef032019-10-04 15:19:43 +02001080 * is stored as conn->ctx by the caller and saved in conn_ctx.
Willy Tarreau01b44822018-10-03 14:26:37 +02001081 */
1082 struct h2s *h2s;
1083
Christopher Fauletf81ef032019-10-04 15:19:43 +02001084 h2s = h2c_bck_stream_new(h2c, conn_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +02001085 if (!h2s)
1086 goto fail_stream;
1087 }
1088
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001089 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 2);
Willy Tarreau4781b152021-04-06 13:53:36 +02001090 HA_ATOMIC_INC(&h2c->px_counters->open_conns);
1091 HA_ATOMIC_INC(&h2c->px_counters->total_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001092
Willy Tarreau0f383582018-10-03 14:22:21 +02001093 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02001094 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001095 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001096 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +02001097 fail_stream:
1098 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +02001099 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001100 task_destroy(t);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02001101 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001102 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +02001103 fail_no_h2c:
Willy Tarreau3b990fe2022-01-12 17:24:26 +01001104 if (!conn_is_back(conn))
1105 LIST_DEL_INIT(&conn->stopping_list);
Christopher Fauletf81ef032019-10-04 15:19:43 +02001106 conn->ctx = conn_ctx; /* restore saved ctx */
1107 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001108 return -1;
1109}
1110
Willy Tarreau751f2d02018-10-05 09:35:00 +02001111/* returns the next allocatable outgoing stream ID for the H2 connection, or
1112 * -1 if no more is allocatable.
1113 */
1114static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
1115{
1116 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +01001117
1118 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +02001119 id = -1;
1120 return id;
1121}
1122
Willy Tarreau2373acc2017-10-12 17:35:14 +02001123/* returns the stream associated with id <id> or NULL if not found */
1124static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
1125{
1126 struct eb32_node *node;
1127
Willy Tarreau751f2d02018-10-05 09:35:00 +02001128 if (id == 0)
1129 return (struct h2s *)h2_closed_stream;
1130
Willy Tarreau2a856182017-05-16 15:20:39 +02001131 if (id > h2c->max_id)
1132 return (struct h2s *)h2_idle_stream;
1133
Willy Tarreau2373acc2017-10-12 17:35:14 +02001134 node = eb32_lookup(&h2c->streams_by_id, id);
1135 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +02001136 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +02001137
1138 return container_of(node, struct h2s, by_id);
1139}
1140
Christopher Faulet73c12072019-04-08 11:23:22 +02001141/* release function. This one should be called to free all resources allocated
1142 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +02001143 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001144static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02001145{
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001146 struct connection *conn = h2c->conn;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001147
Willy Tarreau7838a792019-08-12 18:42:03 +02001148 TRACE_ENTER(H2_EV_H2C_END);
1149
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001150 hpack_dht_free(h2c->ddht);
Christopher Faulet61840e72019-04-15 09:33:32 +02001151
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001152 if (LIST_INLIST(&h2c->buf_wait.list))
1153 LIST_DEL_INIT(&h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +02001154
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001155 h2_release_buf(h2c, &h2c->dbuf);
1156 h2_release_mbuf(h2c);
Willy Tarreau14398122017-09-22 14:26:04 +02001157
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001158 if (h2c->task) {
1159 h2c->task->context = NULL;
1160 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
1161 h2c->task = NULL;
1162 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02001163 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001164 if (conn && h2c->wait_event.events != 0)
1165 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
1166 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +01001167
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001168 HA_ATOMIC_DEC(&h2c->px_counters->open_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001169
Christopher Faulet4de1bff2022-04-14 11:36:41 +02001170 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001171
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001172 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001173 if (!conn_is_back(conn))
1174 LIST_DEL_INIT(&conn->stopping_list);
1175
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001176 conn->mux = NULL;
1177 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02001178 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001179
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001180 conn_stop_tracking(conn);
Willy Tarreau0b222472021-10-21 22:24:31 +02001181
1182 /* there might be a GOAWAY frame still pending in the TCP
1183 * stack, and if the peer continues to send (i.e. window
1184 * updates etc), this can result in losing the GOAWAY. For
1185 * this reason we try to drain anything received in between.
1186 */
1187 conn->flags |= CO_FL_WANT_DRAIN;
1188
1189 conn_xprt_shutw(conn);
1190 conn_xprt_close(conn);
1191 conn_sock_shutw(conn, !conn_is_back(conn));
1192 conn_ctrl_close(conn);
1193
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001194 if (conn->destroy_cb)
1195 conn->destroy_cb(conn);
1196 conn_free(conn);
1197 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001198
1199 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +02001200}
1201
1202
Willy Tarreau71681172017-10-23 14:39:06 +02001203/******************************************************/
1204/* functions below are for the H2 protocol processing */
1205/******************************************************/
1206
1207/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +01001208static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +02001209{
1210 return h2s ? h2s->id : 0;
1211}
1212
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001213/* returns the sum of the stream's own window size and the mux's initial
1214 * window, which together form the stream's effective window size.
1215 */
1216static inline int h2s_mws(const struct h2s *h2s)
1217{
1218 return h2s->sws + h2s->h2c->miw;
1219}
1220
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001221/* marks an error on the connection. Before settings are sent, we must not send
1222 * a GOAWAY frame, and the error state will prevent h2c_send_goaway_error()
1223 * from verifying this so we set H2_CF_GOAWAY_FAILED to make sure it will not
1224 * even try.
1225 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001226static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +02001227{
Willy Tarreau022e5e52020-09-10 09:33:15 +02001228 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn, 0, 0, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +02001229 h2c->errcode = err;
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001230 if (h2c->st0 < H2_CS_SETTINGS1)
1231 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau741d6df2017-10-17 08:00:59 +02001232 h2c->st0 = H2_CS_ERROR;
1233}
1234
Willy Tarreau175cebb2019-01-24 10:02:24 +01001235/* marks an error on the stream. It may also update an already closed stream
1236 * (e.g. to report an error after an RST was received).
1237 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001238static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +02001239{
Willy Tarreau175cebb2019-01-24 10:02:24 +01001240 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001241 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s, 0, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +02001242 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +01001243 if (h2s->st < H2_SS_ERROR)
1244 h2s->st = H2_SS_ERROR;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001245 se_fl_set_error(h2s->sd);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001246 }
1247}
1248
Willy Tarreau7e094452018-12-19 18:08:52 +01001249/* attempt to notify the data layer of recv availability */
1250static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1251{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001252 if (h2s->subs && h2s->subs->events & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001253 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001254 tasklet_wakeup(h2s->subs->tasklet);
1255 h2s->subs->events &= ~SUB_RETRY_RECV;
1256 if (!h2s->subs->events)
1257 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001258 }
1259}
1260
1261/* attempt to notify the data layer of send availability */
1262static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1263{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001264 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001265 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01001266 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01001267 tasklet_wakeup(h2s->subs->tasklet);
1268 h2s->subs->events &= ~SUB_RETRY_SEND;
1269 if (!h2s->subs->events)
1270 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001271 }
Willy Tarreau5723f292020-01-10 15:16:57 +01001272 else if (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) {
1273 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
1274 tasklet_wakeup(h2s->shut_tl);
1275 }
Willy Tarreau7e094452018-12-19 18:08:52 +01001276}
1277
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001278/* alerts the data layer, trying to wake it up by all means, following
1279 * this sequence :
1280 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1281 * - if its subscribed to send, then it's woken up for send
1282 * - if it was subscribed to neither, its ->wake() callback is called
1283 * It is safe to call this function with a closed stream which doesn't have a
Willy Tarreau4596fe22022-05-17 19:07:51 +02001284 * stream connector anymore.
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001285 */
1286static void __maybe_unused h2s_alert(struct h2s *h2s)
1287{
Willy Tarreau7838a792019-08-12 18:42:03 +02001288 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1289
Willy Tarreauf96508a2020-01-10 11:12:48 +01001290 if (h2s->subs ||
Willy Tarreau5723f292020-01-10 15:16:57 +01001291 (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW))) {
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001292 h2s_notify_recv(h2s);
1293 h2s_notify_send(h2s);
1294 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +02001295 else if (h2s_sc(h2s) && h2s_sc(h2s)->app_ops->wake != NULL) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001296 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau2f2318d2022-05-18 10:17:16 +02001297 h2s_sc(h2s)->app_ops->wake(h2s_sc(h2s));
Willy Tarreau7838a792019-08-12 18:42:03 +02001298 }
1299
1300 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001301}
1302
Willy Tarreau92f287b2024-03-11 07:33:44 +01001303/* report one or more glitches on the connection. That is any unexpected event
1304 * that may occasionally happen but if repeated a bit too much, might indicate
1305 * a misbehaving or completely bogus peer. It normally returns zero, unless the
1306 * glitch limit was reached, in which case an error is also reported on the
1307 * connection.
1308 */
1309static inline int h2c_report_glitch(struct h2c *h2c, int increment)
1310{
1311 int thres = (h2c->flags & H2_CF_IS_BACK) ?
1312 h2_be_glitches_threshold : h2_fe_glitches_threshold;
1313
1314 h2c->glitches += increment;
1315 if (thres && h2c->glitches >= thres) {
1316 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
1317 return 1;
1318 }
1319 return 0;
1320}
1321
Willy Tarreaue4820742017-07-27 13:37:23 +02001322/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001323static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001324{
1325 uint8_t *out = frame;
1326
1327 *out = len >> 16;
1328 write_n16(out + 1, len);
1329}
1330
Willy Tarreau54c15062017-10-10 17:10:03 +02001331/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1332 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1333 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001334 * available in the buffer's input prior to calling this function. The buffer
1335 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001336 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001337static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001338 const struct buffer *b, int o)
1339{
Willy Tarreau591d4452018-06-15 17:21:00 +02001340 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 +02001341}
1342
Willy Tarreau1f094672017-11-20 21:27:45 +01001343static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001344{
Willy Tarreau591d4452018-06-15 17:21:00 +02001345 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001346}
1347
Willy Tarreau1f094672017-11-20 21:27:45 +01001348static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001349{
Willy Tarreau591d4452018-06-15 17:21:00 +02001350 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001351}
1352
Willy Tarreau1f094672017-11-20 21:27:45 +01001353static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001354{
Willy Tarreau591d4452018-06-15 17:21:00 +02001355 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001356}
1357
1358
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001359/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1360 * The algorithm is not obvious. It turns out that H2 headers are neither
1361 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1362 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001363 *
1364 * b0 b1 b2 b3 b4 b5..b8
1365 * +----------+---------+--------+----+----+----------------------+
1366 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1367 * +----------+---------+--------+----+----+----------------------+
1368 *
1369 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1370 * we get the sid properly aligned and ordered, and 16 bits of len properly
1371 * ordered as well. The type and flags can be extracted using bit shifts from
1372 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001373 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1374 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001375 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001376static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001377{
1378 uint64_t w;
1379
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001380 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001381 return 0;
1382
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001383 w = h2_get_n64(b, o + 1);
1384 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001385 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1386 h->ff = w >> 32;
1387 h->ft = w >> 40;
1388 h->len += w >> 48;
1389 return 1;
1390}
1391
1392/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1393 * h2_peek_frame_hdr() above.
1394 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001395static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001396{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001397 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001398}
1399
1400/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001401static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001402{
1403 int ret;
1404
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001405 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001406 if (ret > 0)
1407 h2_skip_frame_hdr(b);
1408 return ret;
1409}
1410
Willy Tarreaucb985a42019-10-07 16:56:34 +02001411
1412/* try to fragment the headers frame present at the beginning of buffer <b>,
1413 * enforcing a limit of <mfs> bytes per frame. Returns 0 on failure, 1 on
1414 * success. Typical causes of failure include a buffer not large enough to
1415 * add extra frame headers. The existing frame size is read in the current
1416 * frame. Its EH flag will be cleared if CONTINUATION frames need to be added,
1417 * and its length will be adjusted. The stream ID for continuation frames will
1418 * be copied from the initial frame's.
1419 */
1420static int h2_fragment_headers(struct buffer *b, uint32_t mfs)
1421{
1422 size_t remain = b->data - 9;
1423 int extra_frames = (remain - 1) / mfs;
1424 size_t fsize;
1425 char *fptr;
1426 int frame;
1427
1428 if (b->data <= mfs + 9)
1429 return 1;
1430
1431 /* Too large a frame, we need to fragment it using CONTINUATION
1432 * frames. We start from the end and move tails as needed.
1433 */
1434 if (b->data + extra_frames * 9 > b->size)
1435 return 0;
1436
1437 for (frame = extra_frames; frame; frame--) {
1438 fsize = ((remain - 1) % mfs) + 1;
1439 remain -= fsize;
1440
1441 /* move data */
1442 fptr = b->area + 9 + remain + (frame - 1) * 9;
1443 memmove(fptr + 9, b->area + 9 + remain, fsize);
1444 b->data += 9;
1445
1446 /* write new frame header */
1447 h2_set_frame_size(fptr, fsize);
1448 fptr[3] = H2_FT_CONTINUATION;
1449 fptr[4] = (frame == extra_frames) ? H2_F_HEADERS_END_HEADERS : 0;
1450 write_n32(fptr + 5, read_n32(b->area + 5));
1451 }
1452
1453 b->area[4] &= ~H2_F_HEADERS_END_HEADERS;
1454 h2_set_frame_size(b->area, remain);
1455 return 1;
1456}
1457
1458
Willy Tarreau00dd0782018-03-01 16:31:34 +01001459/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1460 * its connection if the stream was not yet closed. Please use this exclusively
Willy Tarreaubcdc6cc2023-03-20 19:14:47 +01001461 * before closing a stream to ensure stream count is well maintained. Note that
1462 * it does explicitly support being called with a partially initialized h2s
1463 * (e.g. sd==NULL).
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001464 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001465static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001466{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001467 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001468 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001469 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001470 if (!h2s->id)
1471 h2s->h2c->nb_reserved--;
Willy Tarreaubcdc6cc2023-03-20 19:14:47 +01001472 if (h2s->sd && h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001473 if (!se_fl_test(h2s->sd, SE_FL_EOS) && !b_data(&h2s->rxbuf))
Willy Tarreaua27db382019-03-25 18:13:16 +01001474 h2s_notify_recv(h2s);
1475 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001476 HA_ATOMIC_DEC(&h2s->h2c->px_counters->open_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001477
Willy Tarreau7838a792019-08-12 18:42:03 +02001478 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001479 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001480 h2s->st = H2_SS_CLOSED;
1481}
1482
Christopher Faulet531dd052023-05-24 11:14:38 +02001483/* Check h2c and h2s flags to evaluate if EOI/EOS/ERR_PENDING/ERROR flags must
1484 * be set on the SE.
1485 */
1486static inline void h2s_propagate_term_flags(struct h2c *h2c, struct h2s *h2s)
1487{
1488 if (h2s->flags & H2_SF_ES_RCVD) {
1489 se_fl_set(h2s->sd, SE_FL_EOI);
1490 /* Add EOS flag for tunnel */
1491 if (h2s->flags & H2_SF_BODY_TUNNEL)
1492 se_fl_set(h2s->sd, SE_FL_EOS);
1493 }
1494 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED) {
1495 se_fl_set(h2s->sd, SE_FL_EOS);
1496 if (!se_fl_test(h2s->sd, SE_FL_EOI))
1497 se_fl_set(h2s->sd, SE_FL_ERROR);
1498 }
1499 if (se_fl_test(h2s->sd, SE_FL_ERR_PENDING))
1500 se_fl_set(h2s->sd, SE_FL_ERROR);
1501}
1502
Willy Tarreau71049cc2018-03-28 13:56:39 +02001503/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001504/* h2s_destroy should only ever be called by the thread that owns the stream,
1505 * that means that a tasklet should be used if we want to destroy the h2s
1506 * from another thread
1507 */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001508static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001509{
Willy Tarreau7838a792019-08-12 18:42:03 +02001510 struct connection *conn = h2s->h2c->conn;
1511
1512 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1513
Willy Tarreau0a10de62018-03-01 16:27:53 +01001514 h2s_close(h2s);
1515 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001516 if (b_size(&h2s->rxbuf)) {
1517 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001518 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02001519 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001520
1521 if (h2s->subs)
1522 h2s->subs->events = 0;
1523
Joseph Herlantd77575d2018-11-25 10:54:45 -08001524 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001525 * reference left would be in the h2c send_list/fctl_list, and if
1526 * we're in it, we're getting out anyway
1527 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02001528 h2_remove_from_list(h2s);
Willy Tarreau5723f292020-01-10 15:16:57 +01001529
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001530 /* ditto, calling tasklet_free() here should be ok */
Willy Tarreau5723f292020-01-10 15:16:57 +01001531 tasklet_free(h2s->shut_tl);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001532 BUG_ON(h2s->sd && !se_fl_test(h2s->sd, SE_FL_ORPHAN));
1533 sedesc_free(h2s->sd);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001534 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001535
1536 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001537}
1538
Willy Tarreaua8e49542018-10-03 18:53:55 +02001539/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1540 * stream tree. In case of error, nothing is added and NULL is returned. The
1541 * causes of errors can be any failed memory allocation. The caller is
1542 * responsible for checking if the connection may support an extra stream
1543 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001544 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001545static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001546{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001547 struct h2s *h2s;
1548
Willy Tarreau7838a792019-08-12 18:42:03 +02001549 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1550
Willy Tarreaubafbe012017-11-24 17:34:44 +01001551 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001552 if (!h2s)
1553 goto out;
1554
Willy Tarreau5723f292020-01-10 15:16:57 +01001555 h2s->shut_tl = tasklet_new();
1556 if (!h2s->shut_tl) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001557 pool_free(pool_head_h2s, h2s);
1558 goto out;
1559 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001560 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01001561 h2s->shut_tl->process = h2_deferred_shut;
1562 h2s->shut_tl->context = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001563 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001564 h2s->h2c = h2c;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001565 h2s->sd = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001566 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001567 h2s->flags = H2_SF_NONE;
1568 h2s->errcode = H2_ERR_NO_ERROR;
1569 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001570 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001571 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001572 h2s->rxbuf = BUF_NULL;
Amaury Denoyelle74162742020-12-11 17:53:05 +01001573 memset(h2s->upgrade_protocol, 0, sizeof(h2s->upgrade_protocol));
Willy Tarreau751f2d02018-10-05 09:35:00 +02001574
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001575 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001576 if (id > 0)
1577 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001578 else
1579 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001580
1581 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001582 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001583
Willy Tarreau4781b152021-04-06 13:53:36 +02001584 HA_ATOMIC_INC(&h2c->px_counters->open_streams);
1585 HA_ATOMIC_INC(&h2c->px_counters->total_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001586
Willy Tarreau7838a792019-08-12 18:42:03 +02001587 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001588 return h2s;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001589 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001590 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001591 return NULL;
1592}
1593
1594/* creates a new stream <id> on the h2c connection and returns it, or NULL in
Christopher Faulet7d013e72020-12-15 16:56:50 +01001595 * case of memory allocation error. <input> is used as input buffer for the new
1596 * stream. On success, it is transferred to the stream and the mux is no longer
1597 * responsible of it. On error, <input> is unchanged, thus the mux must still
1598 * take care of it.
Willy Tarreaua8e49542018-10-03 18:53:55 +02001599 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02001600static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id, struct buffer *input, uint32_t flags)
Willy Tarreaua8e49542018-10-03 18:53:55 +02001601{
1602 struct session *sess = h2c->conn->owner;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001603 struct h2s *h2s;
1604
Willy Tarreau7838a792019-08-12 18:42:03 +02001605 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1606
Willy Tarreauca1027c2023-04-18 15:57:03 +02001607 if (h2c->nb_streams >= h2c_max_concurrent_streams(h2c)) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01001608 h2c_report_glitch(h2c, 1);
Willy Tarreaue872f752022-05-12 09:24:41 +02001609 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 +02001610 session_inc_http_req_ctr(sess);
1611 session_inc_http_err_ctr(sess);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001612 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001613 }
Willy Tarreaua8e49542018-10-03 18:53:55 +02001614
1615 h2s = h2s_new(h2c, id);
1616 if (!h2s)
Willy Tarreaue872f752022-05-12 09:24:41 +02001617 goto out_alloc;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001618
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001619 h2s->sd = sedesc_new();
1620 if (!h2s->sd)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001621 goto out_close;
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001622 h2s->sd->se = h2s;
1623 h2s->sd->conn = h2c->conn;
1624 se_fl_set(h2s->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
Christopher Faulet4403cdf2023-05-04 15:49:12 +02001625 /* The request is not finished, don't expect data from the opposite side
1626 * yet
1627 */
Christopher Faulet78b1eb22023-05-24 11:44:53 +02001628 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 +02001629 se_expect_no_data(h2s->sd);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001630
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02001631 /* FIXME wrong analogy between ext-connect and websocket, this need to
1632 * be refine.
1633 */
1634 if (flags & H2_SF_EXT_CONNECT_RCVD)
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001635 se_fl_set(h2s->sd, SE_FL_WEBSOCKET);
Christopher Fauletb669d682022-03-22 18:37:19 +01001636
Willy Tarreaud0de6772022-02-04 09:05:37 +01001637 /* The stream will record the request's accept date (which is either the
1638 * end of the connection's or the date immediately after the previous
1639 * request) and the idle time, which is the delay since the previous
1640 * request. We can set the value now, it will be copied by stream_new().
1641 */
Willy Tarreau69530f52023-04-28 09:16:15 +02001642 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001643
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001644 if (!sc_new_from_endp(h2s->sd, sess, input))
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001645 goto out_close;
Willy Tarreaucd6bb1a2022-05-10 15:00:03 +02001646
Willy Tarreau36c22322022-05-27 10:41:24 +02001647 h2c->nb_sc++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001648
Willy Tarreau590a0512018-09-05 11:56:48 +02001649 /* We want the accept date presented to the next stream to be the one
1650 * we have now, the handshake time to be null (since the next stream
1651 * is not delayed by a handshake), and the idle time to count since
1652 * right now.
1653 */
1654 sess->accept_date = date;
Willy Tarreau69530f52023-04-28 09:16:15 +02001655 sess->accept_ts = now_ns;
Willy Tarreau590a0512018-09-05 11:56:48 +02001656 sess->t_handshake = 0;
Willy Tarreaud0de6772022-02-04 09:05:37 +01001657 sess->t_idle = 0;
Willy Tarreau590a0512018-09-05 11:56:48 +02001658
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001659 /* OK done, the stream lives its own life now */
Willy Tarreau36c22322022-05-27 10:41:24 +02001660 if (h2_frt_has_too_many_sc(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001661 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001662 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001663 return h2s;
1664
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001665 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001666 h2s_destroy(h2s);
Willy Tarreaue872f752022-05-12 09:24:41 +02001667 out_alloc:
1668 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 +02001669 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001670 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001671 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001672 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001673}
1674
Willy Tarreau36c22322022-05-27 10:41:24 +02001675/* allocates a new stream associated to stream connector <sc> on the h2c
Willy Tarreau4596fe22022-05-17 19:07:51 +02001676 * connection and returns it, or NULL in case of memory allocation error or if
1677 * the highest possible stream ID was reached.
Willy Tarreau751f2d02018-10-05 09:35:00 +02001678 */
Willy Tarreau36c22322022-05-27 10:41:24 +02001679static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct stconn *sc, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001680{
1681 struct h2s *h2s = NULL;
1682
Willy Tarreau7838a792019-08-12 18:42:03 +02001683 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1684
Willy Tarreaue872f752022-05-12 09:24:41 +02001685 if (h2c->nb_streams >= h2c->streams_limit) {
1686 TRACE_ERROR("Aborting stream since negotiated limit is too low", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001687 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001688 }
Willy Tarreau751f2d02018-10-05 09:35:00 +02001689
Willy Tarreaue872f752022-05-12 09:24:41 +02001690 if (h2_streams_left(h2c) < 1) {
1691 TRACE_ERROR("Aborting stream since no more streams left", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreaua80dca82019-01-24 17:08:28 +01001692 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001693 }
Willy Tarreaua80dca82019-01-24 17:08:28 +01001694
Willy Tarreau751f2d02018-10-05 09:35:00 +02001695 /* Defer choosing the ID until we send the first message to create the stream */
1696 h2s = h2s_new(h2c, 0);
Willy Tarreaue872f752022-05-12 09:24:41 +02001697 if (!h2s) {
1698 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001699 goto out;
Willy Tarreaue872f752022-05-12 09:24:41 +02001700 }
Willy Tarreau751f2d02018-10-05 09:35:00 +02001701
Willy Tarreau36c22322022-05-27 10:41:24 +02001702 if (sc_attach_mux(sc, h2s, h2c->conn) < 0) {
Willy Tarreaue872f752022-05-12 09:24:41 +02001703 TRACE_ERROR("Failed to allocate a new stream", H2_EV_H2S_NEW, h2c->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +02001704 h2s_destroy(h2s);
1705 h2s = NULL;
1706 goto out;
1707 }
Willy Tarreau95acc8b2022-05-27 16:14:10 +02001708 h2s->sd = sc->sedesc;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001709 h2s->sess = sess;
Willy Tarreau36c22322022-05-27 10:41:24 +02001710 h2c->nb_sc++;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001711
Willy Tarreau54310dc2024-01-12 18:36:57 +01001712 /* on the backend we can afford to only count total streams upon success */
1713 h2c->stream_cnt++;
1714
Willy Tarreau751f2d02018-10-05 09:35:00 +02001715 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001716 if (likely(h2s))
1717 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1718 else
1719 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001720 return h2s;
1721}
1722
Willy Tarreaube5b7152017-09-25 16:25:39 +02001723/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1724 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1725 * the various settings codes.
1726 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001727static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001728{
1729 struct buffer *res;
1730 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001731 struct buffer buf;
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001732 int iws;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001733 int mfs;
Willy Tarreauca1027c2023-04-18 15:57:03 +02001734 int mcs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001735 int ret = 0;
1736
1737 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001738
Willy Tarreaube5b7152017-09-25 16:25:39 +02001739 chunk_init(&buf, buf_data, sizeof(buf_data));
1740 chunk_memcpy(&buf,
1741 "\x00\x00\x00" /* length : 0 for now */
1742 "\x04\x00" /* type : 4 (settings), flags : 0 */
1743 "\x00\x00\x00\x00", /* stream ID : 0 */
1744 9);
1745
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001746 if (h2c->flags & H2_CF_IS_BACK) {
1747 /* send settings_enable_push=0 */
1748 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1749 }
1750
Amaury Denoyellebefeae82021-07-09 17:14:30 +02001751 /* rfc 8441 #3 SETTINGS_ENABLE_CONNECT_PROTOCOL=1,
1752 * sent automatically unless disabled in the global config */
1753 if (!(global.tune.options & GTUNE_DISABLE_H2_WEBSOCKET))
1754 chunk_memcat(&buf, "\x00\x08\x00\x00\x00\x01", 6);
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01001755
Willy Tarreaube5b7152017-09-25 16:25:39 +02001756 if (h2_settings_header_table_size != 4096) {
1757 char str[6] = "\x00\x01"; /* header_table_size */
1758
1759 write_n32(str + 2, h2_settings_header_table_size);
1760 chunk_memcat(&buf, str, 6);
1761 }
1762
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001763 iws = (h2c->flags & H2_CF_IS_BACK) ?
1764 h2_be_settings_initial_window_size:
1765 h2_fe_settings_initial_window_size;
1766 iws = iws ? iws : h2_settings_initial_window_size;
1767
1768 if (iws != 65535) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001769 char str[6] = "\x00\x04"; /* initial_window_size */
1770
Willy Tarreau9d7abda2023-04-17 15:04:34 +02001771 write_n32(str + 2, iws);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001772 chunk_memcat(&buf, str, 6);
1773 }
1774
Willy Tarreauca1027c2023-04-18 15:57:03 +02001775 mcs = h2c_max_concurrent_streams(h2c);
1776 if (mcs != 0) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001777 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1778
1779 /* Note: 0 means "unlimited" for haproxy's config but not for
1780 * the protocol, so never send this value!
1781 */
Willy Tarreauca1027c2023-04-18 15:57:03 +02001782 write_n32(str + 2, mcs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001783 chunk_memcat(&buf, str, 6);
1784 }
1785
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001786 mfs = h2_settings_max_frame_size;
1787 if (mfs > global.tune.bufsize)
1788 mfs = global.tune.bufsize;
1789
1790 if (!mfs)
1791 mfs = global.tune.bufsize;
1792
1793 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001794 char str[6] = "\x00\x05"; /* max_frame_size */
1795
1796 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1797 * match bufsize - rewrite size, but at the moment it seems
1798 * that clients don't take care of it.
1799 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001800 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001801 chunk_memcat(&buf, str, 6);
1802 }
1803
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001804 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001805
1806 res = br_tail(h2c->mbuf);
1807 retry:
1808 if (!h2_get_buf(h2c, res)) {
1809 h2c->flags |= H2_CF_MUX_MALLOC;
1810 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001811 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001812 }
1813
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001814 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001815 if (unlikely(ret <= 0)) {
1816 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001817 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1818 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001819 h2c->flags |= H2_CF_MUX_MFULL;
1820 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001821 }
1822 else {
1823 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001824 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001825 }
1826 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001827 out:
1828 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001829 return ret;
1830}
1831
Willy Tarreau52eed752017-09-22 15:05:09 +02001832/* Try to receive a connection preface, then upon success try to send our
1833 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1834 * missing data. It may return an error in h2c.
1835 */
1836static int h2c_frt_recv_preface(struct h2c *h2c)
1837{
1838 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001839 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001840
Willy Tarreau7838a792019-08-12 18:42:03 +02001841 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1842
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001843 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001844
1845 if (unlikely(ret1 <= 0)) {
Christopher Fauletb5f7b522021-07-26 12:06:53 +02001846 if (!ret1)
1847 h2c->flags |= H2_CF_DEM_SHORT_READ;
Christopher Fauletff7925d2022-10-11 19:12:40 +02001848 if (ret1 < 0 || (h2c->flags & H2_CF_RCVD_SHUT)) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01001849 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01001850 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 +02001851 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauee4684f2021-06-17 08:08:48 +02001852 if (b_data(&h2c->dbuf) ||
1853 !(((const struct session *)h2c->conn->owner)->fe->options & PR_O_IGNORE_PRB))
1854 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01001855 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001856 ret2 = 0;
1857 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001858 }
1859
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001860 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001861 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001862 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001863 out:
1864 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001865 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001866}
1867
Willy Tarreau01b44822018-10-03 14:26:37 +02001868/* Try to send a connection preface, then upon success try to send our
1869 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1870 * missing data. It may return an error in h2c.
1871 */
1872static int h2c_bck_send_preface(struct h2c *h2c)
1873{
1874 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001875 int ret = 0;
1876
1877 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001878
Willy Tarreaubcc45952019-05-26 10:05:50 +02001879 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001880 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001881 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001882 h2c->flags |= H2_CF_MUX_MALLOC;
1883 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001884 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001885 }
1886
1887 if (!b_data(res)) {
1888 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001889 ret = b_istput(res, ist(H2_CONN_PREFACE));
1890 if (unlikely(ret <= 0)) {
1891 if (!ret) {
1892 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1893 goto retry;
1894 h2c->flags |= H2_CF_MUX_MFULL;
1895 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001896 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001897 }
1898 else {
1899 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001900 ret = 0;
1901 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001902 }
1903 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001904 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001905 ret = h2c_send_settings(h2c);
1906 out:
1907 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1908 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001909}
1910
Willy Tarreau081d4722017-05-16 21:51:05 +02001911/* try to send a GOAWAY frame on the connection to report an error or a graceful
1912 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1913 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1914 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1915 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1916 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1917 * on unrecoverable failure. It will not attempt to send one again in this last
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001918 * case, nor will it send one if settings were not sent (e.g. still waiting for
1919 * a preface) so that it is safe to use h2c_error() to report such errors.
Willy Tarreau081d4722017-05-16 21:51:05 +02001920 */
1921static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1922{
1923 struct buffer *res;
1924 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001925 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001926
Willy Tarreau7838a792019-08-12 18:42:03 +02001927 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1928
Willy Tarreau15dbedd2022-04-13 09:40:52 +02001929 if ((h2c->flags & H2_CF_GOAWAY_FAILED) || h2c->st0 < H2_CS_SETTINGS1) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001930 ret = 1; // claim that it worked
1931 goto out;
1932 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001933
Willy Tarreau9c218e72019-05-26 10:08:28 +02001934 /* len: 8, type: 7, flags: none, sid: 0 */
1935 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1936
1937 if (h2c->last_sid < 0)
1938 h2c->last_sid = h2c->max_id;
1939
1940 write_n32(str + 9, h2c->last_sid);
1941 write_n32(str + 13, h2c->errcode);
1942
Willy Tarreaubcc45952019-05-26 10:05:50 +02001943 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001944 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001945 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001946 h2c->flags |= H2_CF_MUX_MALLOC;
1947 if (h2s)
1948 h2s->flags |= H2_SF_BLK_MROOM;
1949 else
1950 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001951 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001952 }
1953
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001954 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001955 if (unlikely(ret <= 0)) {
1956 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001957 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1958 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001959 h2c->flags |= H2_CF_MUX_MFULL;
1960 if (h2s)
1961 h2s->flags |= H2_SF_BLK_MROOM;
1962 else
1963 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001964 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001965 }
1966 else {
1967 /* we cannot report this error using GOAWAY, so we mark
1968 * it and claim a success.
1969 */
1970 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1971 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001972 ret = 1;
1973 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001974 }
1975 }
1976 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001977
1978 /* some codes are not for real errors, just attempts to close cleanly */
1979 switch (h2c->errcode) {
1980 case H2_ERR_NO_ERROR:
1981 case H2_ERR_ENHANCE_YOUR_CALM:
1982 case H2_ERR_REFUSED_STREAM:
1983 case H2_ERR_CANCEL:
1984 break;
1985 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001986 HA_ATOMIC_INC(&h2c->px_counters->goaway_resp);
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001987 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001988 out:
1989 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001990 return ret;
1991}
1992
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001993/* Try to send an RST_STREAM frame on the connection for the indicated stream
1994 * during mux operations. This stream must be valid and cannot be closed
1995 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1996 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1997 * not yet.
1998 *
1999 * Returns > 0 on success or zero if nothing was done. In case of lack of room
2000 * to write the message, it subscribes the stream to future notifications.
2001 */
2002static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
2003{
2004 struct buffer *res;
2005 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002006 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002007
Willy Tarreau7838a792019-08-12 18:42:03 +02002008 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
2009
2010 if (!h2s || h2s->st == H2_SS_CLOSED) {
2011 ret = 1;
2012 goto out;
2013 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002014
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002015 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
2016 * RST_STREAM in response to a RST_STREAM frame.
2017 */
Willy Tarreau231f6162019-08-06 10:01:40 +02002018 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002019 ret = 1;
2020 goto ignore;
2021 }
2022
Willy Tarreau9c218e72019-05-26 10:08:28 +02002023 /* len: 4, type: 3, flags: none */
2024 memcpy(str, "\x00\x00\x04\x03\x00", 5);
2025 write_n32(str + 5, h2s->id);
2026 write_n32(str + 9, h2s->errcode);
2027
Willy Tarreaubcc45952019-05-26 10:05:50 +02002028 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002029 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002030 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002031 h2c->flags |= H2_CF_MUX_MALLOC;
2032 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002033 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002034 }
2035
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002036 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002037 if (unlikely(ret <= 0)) {
2038 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002039 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2040 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002041 h2c->flags |= H2_CF_MUX_MFULL;
2042 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002043 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002044 }
2045 else {
2046 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002047 ret = 0;
2048 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002049 }
2050 }
2051
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002052 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002053 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01002054 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002055 out:
2056 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002057 return ret;
2058}
2059
2060/* Try to send an RST_STREAM frame on the connection for the stream being
2061 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01002062 * error code, even if the stream is one of the dummy ones, and will update
2063 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002064 *
2065 * Returns > 0 on success or zero if nothing was done. In case of lack of room
2066 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08002067 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002068 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02002069 */
2070static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
2071{
2072 struct buffer *res;
2073 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002074 int ret = 0;
2075
2076 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002077
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002078 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
2079 * RST_STREAM in response to a RST_STREAM frame.
2080 */
2081 if (h2c->dft == H2_FT_RST_STREAM) {
2082 ret = 1;
2083 goto ignore;
2084 }
2085
Willy Tarreau9c218e72019-05-26 10:08:28 +02002086 /* len: 4, type: 3, flags: none */
2087 memcpy(str, "\x00\x00\x04\x03\x00", 5);
2088
2089 write_n32(str + 5, h2c->dsi);
2090 write_n32(str + 9, h2s->errcode);
2091
Willy Tarreaubcc45952019-05-26 10:05:50 +02002092 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002093 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002094 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02002095 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002096 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002097 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002098 }
2099
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002100 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02002101 if (unlikely(ret <= 0)) {
2102 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002103 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2104 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002105 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002106 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002107 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002108 }
2109 else {
2110 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002111 ret = 0;
2112 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002113 }
2114 }
2115
Willy Tarreau8adae7c2018-03-22 17:37:05 +01002116 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02002117 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02002118 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01002119 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002120 }
2121
Willy Tarreau7838a792019-08-12 18:42:03 +02002122 out:
Willy Tarreau4781b152021-04-06 13:53:36 +02002123 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_resp);
Willy Tarreau7838a792019-08-12 18:42:03 +02002124 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002125 return ret;
2126}
2127
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002128/* try to send an empty DATA frame with the ES flag set to notify about the
2129 * end of stream and match a shutdown(write). If an ES was already sent as
2130 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
2131 * on success or zero if nothing was done. In case of lack of room to write the
2132 * message, it subscribes the requesting stream to future notifications.
2133 */
2134static int h2_send_empty_data_es(struct h2s *h2s)
2135{
2136 struct h2c *h2c = h2s->h2c;
2137 struct buffer *res;
2138 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002139 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002140
Willy Tarreau7838a792019-08-12 18:42:03 +02002141 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
2142
2143 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
2144 ret = 1;
2145 goto out;
2146 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002147
Willy Tarreau9c218e72019-05-26 10:08:28 +02002148 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
2149 memcpy(str, "\x00\x00\x00\x00\x01", 5);
2150 write_n32(str + 5, h2s->id);
2151
Willy Tarreaubcc45952019-05-26 10:05:50 +02002152 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002153 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002154 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002155 h2c->flags |= H2_CF_MUX_MALLOC;
2156 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002157 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002158 }
2159
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002160 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002161 if (likely(ret > 0)) {
2162 h2s->flags |= H2_SF_ES_SENT;
2163 }
2164 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002165 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2166 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002167 h2c->flags |= H2_CF_MUX_MFULL;
2168 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002169 }
2170 else {
2171 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002172 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002173 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002174 out:
2175 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002176 return ret;
2177}
2178
Willy Tarreau4596fe22022-05-17 19:07:51 +02002179/* wake a specific stream and assign its stream connector some SE_FL_* flags
2180 * among SE_FL_ERR_PENDING and SE_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002181 * is automatically updated accordingly. If the stream is orphaned, it is
2182 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01002183 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002184static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01002185{
Willy Tarreau7838a792019-08-12 18:42:03 +02002186 struct h2c *h2c = h2s->h2c;
2187
2188 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
2189
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002190 if (!h2s_sc(h2s)) {
Christopher Fauletf02ca002019-03-07 16:21:34 +01002191 /* this stream was already orphaned */
2192 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002193 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002194 return;
2195 }
2196
Christopher Fauletaade4ed2020-10-08 15:38:41 +02002197 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002198 if (h2s->st == H2_SS_OPEN)
2199 h2s->st = H2_SS_HREM;
2200 else if (h2s->st == H2_SS_HLOC)
2201 h2s_close(h2s);
2202 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002203
Christopher Fauletff7925d2022-10-11 19:12:40 +02002204 if (h2s->h2c->st0 >= H2_CS_ERROR || (h2s->h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) ||
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002205 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02002206 se_fl_set_error(h2s->sd);
Willy Tarreau23482912019-05-07 15:23:14 +02002207
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002208 if (h2s->st < H2_SS_ERROR)
2209 h2s->st = H2_SS_ERROR;
2210 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01002211
2212 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002213 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002214}
2215
2216/* wake the streams attached to the connection, whose id is greater than <last>
2217 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002218 */
Willy Tarreau23482912019-05-07 15:23:14 +02002219static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002220{
2221 struct eb32_node *node;
2222 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002223
Willy Tarreau7838a792019-08-12 18:42:03 +02002224 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
2225
Christopher Fauletf02ca002019-03-07 16:21:34 +01002226 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002227 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
2228 while (node) {
2229 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002230 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002231 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002232 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002233
Christopher Fauletf02ca002019-03-07 16:21:34 +01002234 /* Wake all streams with unassigned ID (ID == 0) */
2235 node = eb32_lookup(&h2c->streams_by_id, 0);
2236 while (node) {
2237 h2s = container_of(node, struct h2s, by_id);
2238 if (h2s->id > 0)
2239 break;
2240 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002241 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002242 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002243
2244 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002245}
2246
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002247/* Wake up all blocked streams whose window size has become positive after the
2248 * mux's initial window was adjusted. This should be done after having processed
2249 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002250 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002251static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02002252{
2253 struct h2s *h2s;
2254 struct eb32_node *node;
2255
Willy Tarreau7838a792019-08-12 18:42:03 +02002256 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2257
Willy Tarreau3421aba2017-07-27 15:41:03 +02002258 node = eb32_first(&h2c->streams_by_id);
2259 while (node) {
2260 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002261 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002262 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002263 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002264 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2265 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002266 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002267 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002268 node = eb32_next(node);
2269 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002270
2271 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002272}
2273
2274/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
2275 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01002276 * return an error in h2c. The caller must have already verified frame length
2277 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002278 */
2279static int h2c_handle_settings(struct h2c *h2c)
2280{
2281 unsigned int offset;
2282 int error;
2283
Willy Tarreau7838a792019-08-12 18:42:03 +02002284 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
2285
Willy Tarreau3421aba2017-07-27 15:41:03 +02002286 if (h2c->dff & H2_F_SETTINGS_ACK) {
2287 if (h2c->dfl) {
2288 error = H2_ERR_FRAME_SIZE_ERROR;
2289 goto fail;
2290 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002291 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002292 }
2293
Willy Tarreau3421aba2017-07-27 15:41:03 +02002294 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002295 if (b_data(&h2c->dbuf) < h2c->dfl) {
2296 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002297 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002298 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002299
2300 /* parse the frame */
2301 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002302 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2303 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002304
2305 switch (type) {
2306 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2307 /* we need to update all existing streams with the
2308 * difference from the previous iws.
2309 */
2310 if (arg < 0) { // RFC7540#6.5.2
2311 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreauf0279f62024-03-11 07:35:19 +01002312 h2c_report_glitch(h2c, 1);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002313 goto fail;
2314 }
Willy Tarreaueec8dc12024-02-08 14:37:56 +01002315 /* Let's count a glitch here in case of a reduction
2316 * after H2_CS_SETTINGS1 because while it's not
Willy Tarreau22892322024-01-17 16:57:23 +01002317 * fundamentally invalid from a protocol's perspective,
2318 * it's often suspicious.
2319 */
Willy Tarreaueec8dc12024-02-08 14:37:56 +01002320 if (h2c->st0 != H2_CS_SETTINGS1 && arg < h2c->miw)
Willy Tarreau92f287b2024-03-11 07:33:44 +01002321 if (h2c_report_glitch(h2c, 1)) {
2322 error = H2_ERR_ENHANCE_YOUR_CALM;
2323 TRACE_STATE("glitch limit reached on SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
2324 goto fail;
2325 }
Willy Tarreaueec8dc12024-02-08 14:37:56 +01002326
Willy Tarreau3421aba2017-07-27 15:41:03 +02002327 h2c->miw = arg;
2328 break;
2329 case H2_SETTINGS_MAX_FRAME_SIZE:
2330 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
Willy Tarreauf0279f62024-03-11 07:35:19 +01002331 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002332 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 +02002333 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002334 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002335 goto fail;
2336 }
2337 h2c->mfs = arg;
2338 break;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01002339 case H2_SETTINGS_HEADER_TABLE_SIZE:
2340 h2c->flags |= H2_CF_SHTS_UPDATED;
2341 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002342 case H2_SETTINGS_ENABLE_PUSH:
2343 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
Willy Tarreauf0279f62024-03-11 07:35:19 +01002344 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002345 TRACE_ERROR("ENABLE_PUSH out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002346 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002347 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002348 goto fail;
2349 }
2350 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002351 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2352 if (h2c->flags & H2_CF_IS_BACK) {
2353 /* the limit is only for the backend; for the frontend it is our limit */
Willy Tarreauca1027c2023-04-18 15:57:03 +02002354 if ((unsigned int)arg > h2c_max_concurrent_streams(h2c))
2355 arg = h2c_max_concurrent_streams(h2c);
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002356 h2c->streams_limit = arg;
2357 }
2358 break;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002359 case H2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
Amaury Denoyelle0df04362021-10-18 09:43:29 +02002360 if (arg == 1)
2361 h2c->flags |= H2_CF_RCVD_RFC8441;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002362 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002363 }
2364 }
2365
2366 /* need to ACK this frame now */
2367 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002368 done:
2369 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002370 return 1;
2371 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002372 if (!(h2c->flags & H2_CF_IS_BACK))
2373 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002374 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002375 out0:
2376 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 +02002377 return 0;
2378}
2379
2380/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2381 * success or one of the h2_status values.
2382 */
2383static int h2c_ack_settings(struct h2c *h2c)
2384{
2385 struct buffer *res;
2386 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002387 int ret = 0;
2388
2389 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002390
Willy Tarreau9c218e72019-05-26 10:08:28 +02002391 memcpy(str,
2392 "\x00\x00\x00" /* length : 0 (no data) */
2393 "\x04" "\x01" /* type : 4, flags : ACK */
2394 "\x00\x00\x00\x00" /* stream ID */, 9);
2395
Willy Tarreaubcc45952019-05-26 10:05:50 +02002396 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002397 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002398 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002399 h2c->flags |= H2_CF_MUX_MALLOC;
2400 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002401 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002402 }
2403
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002404 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002405 if (unlikely(ret <= 0)) {
2406 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002407 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2408 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002409 h2c->flags |= H2_CF_MUX_MFULL;
2410 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002411 }
2412 else {
2413 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002414 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002415 }
2416 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002417 out:
2418 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002419 return ret;
2420}
2421
Willy Tarreaucf68c782017-10-10 17:11:41 +02002422/* processes a PING frame and schedules an ACK if needed. The caller must pass
2423 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002424 * missing data. The caller must have already verified frame length
2425 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002426 */
2427static int h2c_handle_ping(struct h2c *h2c)
2428{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002429 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002430 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002431 h2c->st0 = H2_CS_FRAME_A;
2432 return 1;
2433}
2434
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002435/* Try to send a window update for stream id <sid> and value <increment>.
2436 * Returns > 0 on success or zero on missing room or failure. It may return an
2437 * error in h2c.
2438 */
2439static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2440{
2441 struct buffer *res;
2442 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002443 int ret = 0;
2444
2445 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002446
Willy Tarreau9c218e72019-05-26 10:08:28 +02002447 /* length: 4, type: 8, flags: none */
2448 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2449 write_n32(str + 5, sid);
2450 write_n32(str + 9, increment);
2451
Willy Tarreaubcc45952019-05-26 10:05:50 +02002452 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002453 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002454 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002455 h2c->flags |= H2_CF_MUX_MALLOC;
2456 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002457 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002458 }
2459
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002460 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002461 if (unlikely(ret <= 0)) {
2462 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002463 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2464 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002465 h2c->flags |= H2_CF_MUX_MFULL;
2466 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002467 }
2468 else {
2469 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002470 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002471 }
2472 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002473 out:
2474 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002475 return ret;
2476}
2477
2478/* try to send pending window update for the connection. It's safe to call it
2479 * with no pending updates. Returns > 0 on success or zero on missing room or
2480 * failure. It may return an error in h2c.
2481 */
2482static int h2c_send_conn_wu(struct h2c *h2c)
2483{
2484 int ret = 1;
2485
Willy Tarreau7838a792019-08-12 18:42:03 +02002486 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2487
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002488 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002489 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002490
Willy Tarreau97aaa672018-12-23 09:49:04 +01002491 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2492 /* increase the advertised connection window to 2G on
2493 * first update.
2494 */
2495 h2c->flags |= H2_CF_WINDOW_OPENED;
2496 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2497 }
2498
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002499 /* send WU for the connection */
2500 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2501 if (ret > 0)
2502 h2c->rcvd_c = 0;
2503
Willy Tarreau7838a792019-08-12 18:42:03 +02002504 out:
2505 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002506 return ret;
2507}
2508
2509/* try to send pending window update for the current dmux stream. It's safe to
2510 * call it with no pending updates. Returns > 0 on success or zero on missing
2511 * room or failure. It may return an error in h2c.
2512 */
2513static int h2c_send_strm_wu(struct h2c *h2c)
2514{
2515 int ret = 1;
2516
Willy Tarreau7838a792019-08-12 18:42:03 +02002517 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2518
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002519 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002520 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002521
2522 /* send WU for the stream */
2523 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2524 if (ret > 0)
2525 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002526 out:
2527 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002528 return ret;
2529}
2530
Willy Tarreaucf68c782017-10-10 17:11:41 +02002531/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2532 * success, 0 on missing data or one of the h2_status values.
2533 */
2534static int h2c_ack_ping(struct h2c *h2c)
2535{
2536 struct buffer *res;
2537 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002538 int ret = 0;
2539
2540 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002541
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002542 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002543 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002544
Willy Tarreaucf68c782017-10-10 17:11:41 +02002545 memcpy(str,
2546 "\x00\x00\x08" /* length : 8 (same payload) */
2547 "\x06" "\x01" /* type : 6, flags : ACK */
2548 "\x00\x00\x00\x00" /* stream ID */, 9);
2549
2550 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002551 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002552
Willy Tarreau9c218e72019-05-26 10:08:28 +02002553 res = br_tail(h2c->mbuf);
2554 retry:
2555 if (!h2_get_buf(h2c, res)) {
2556 h2c->flags |= H2_CF_MUX_MALLOC;
2557 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002558 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002559 }
2560
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002561 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002562 if (unlikely(ret <= 0)) {
2563 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002564 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2565 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002566 h2c->flags |= H2_CF_MUX_MFULL;
2567 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002568 }
2569 else {
2570 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002571 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002572 }
2573 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002574 out:
2575 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002576 return ret;
2577}
2578
Willy Tarreau26f95952017-07-27 17:18:30 +02002579/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2580 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002581 * h2c or h2s. The caller must have already verified frame length and stream ID
2582 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002583 */
2584static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2585{
2586 int32_t inc;
2587 int error;
2588
Willy Tarreau7838a792019-08-12 18:42:03 +02002589 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2590
Willy Tarreau26f95952017-07-27 17:18:30 +02002591 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002592 if (b_data(&h2c->dbuf) < h2c->dfl) {
2593 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002594 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002595 }
Willy Tarreau26f95952017-07-27 17:18:30 +02002596
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002597 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002598
2599 if (h2c->dsi != 0) {
2600 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002601
2602 /* it's not an error to receive WU on a closed stream */
2603 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002604 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002605
2606 if (!inc) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002607 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002608 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 +02002609 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002610 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002611 goto strm_err;
2612 }
2613
Willy Tarreau22892322024-01-17 16:57:23 +01002614 /* WT: it would be tempting to count a glitch here for very small
2615 * increments (less than a few tens of bytes), but that might be
2616 * perfectly valid for many short streams, so better instead
2617 * count the number of WU per frame maybe. That would be better
2618 * dealt with using scores per frame.
2619 */
2620
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002621 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002622 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002623 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 +02002624 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002625 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002626 goto strm_err;
2627 }
2628
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002629 h2s->sws += inc;
2630 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002631 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002632 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002633 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2634 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002635 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002636 }
2637 }
2638 else {
2639 /* connection window update */
2640 if (!inc) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002641 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002642 TRACE_ERROR("conn WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002643 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002644 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002645 goto conn_err;
2646 }
2647
2648 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002649 h2c_report_glitch(h2c, 1);
Willy Tarreau04256ce2024-01-17 16:56:18 +01002650 TRACE_ERROR("conn WINDOW_UPDATE inc<0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002651 error = H2_ERR_FLOW_CONTROL_ERROR;
2652 goto conn_err;
2653 }
2654
2655 h2c->mws += inc;
2656 }
2657
Willy Tarreau7838a792019-08-12 18:42:03 +02002658 done:
2659 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002660 return 1;
2661
2662 conn_err:
2663 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002664 out0:
2665 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 +02002666 return 0;
2667
2668 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002669 h2s_error(h2s, error);
2670 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002671 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002672 return 0;
2673}
2674
Willy Tarreaue96b0922017-10-30 00:28:29 +01002675/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002676 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2677 * have already verified frame length and stream ID validity. Described in
2678 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002679 */
2680static int h2c_handle_goaway(struct h2c *h2c)
2681{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002682 int last;
2683
Willy Tarreau7838a792019-08-12 18:42:03 +02002684 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002685 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002686 if (b_data(&h2c->dbuf) < h2c->dfl) {
2687 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002688 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002689 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002690 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002691
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002692 last = h2_get_n32(&h2c->dbuf, 0);
2693 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002694 if (h2c->last_sid < 0)
2695 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002696 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002697 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002698 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002699}
2700
Willy Tarreau92153fc2017-12-03 19:46:19 +01002701/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002702 * invalid. Returns > 0 on success or zero on missing data. It may return an
2703 * error in h2c. The caller must have already verified frame length and stream
2704 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002705 */
2706static int h2c_handle_priority(struct h2c *h2c)
2707{
Willy Tarreau7838a792019-08-12 18:42:03 +02002708 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2709
Willy Tarreau92153fc2017-12-03 19:46:19 +01002710 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002711 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002712 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002713 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002714 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002715 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002716
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002717 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002718 /* 7540#5.3 : can't depend on itself */
Willy Tarreauf0279f62024-03-11 07:35:19 +01002719 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002720 TRACE_ERROR("PRIORITY depends on itself", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002721 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02002722 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002723 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002724 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002725 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002726 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002727 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002728}
2729
Willy Tarreaucd234e92017-08-18 10:59:39 +02002730/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002731 * Returns > 0 on success or zero on missing data. The caller must have already
2732 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002733 */
2734static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2735{
Willy Tarreau7838a792019-08-12 18:42:03 +02002736 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2737
Willy Tarreaucd234e92017-08-18 10:59:39 +02002738 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002739 if (b_data(&h2c->dbuf) < h2c->dfl) {
2740 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 +02002741 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002742 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002743 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002744
2745 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002746 if (h2s->st == H2_SS_CLOSED) {
2747 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 +02002748 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002749 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002750
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002751 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002752 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002753
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002754 if (h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02002755 se_fl_set_error(h2s->sd);
Willy Tarreauf830f012018-12-19 17:44:55 +01002756 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002757 }
2758
2759 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002760 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002761 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002762}
2763
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002764/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2765 * It may return an error in h2c or h2s. The caller must consider that the
2766 * return value is the new h2s in case one was allocated (most common case).
2767 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002768 * errors here are reported as connection errors since it's impossible to
2769 * recover from such errors after the compression context has been altered.
2770 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002771static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002772{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002773 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002774 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002775 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002776 int error;
2777
Willy Tarreau7838a792019-08-12 18:42:03 +02002778 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2779
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002780 if (!b_size(&h2c->dbuf)) {
2781 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002782 goto out; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002783 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002784
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002785 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2786 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002787 goto out; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002788 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002789
2790 /* now either the frame is complete or the buffer is complete */
2791 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002792 /* The stream exists/existed, this must be a trailers frame */
2793 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002794 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &body_len, NULL);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002795 /* unrecoverable error ? */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002796 if (h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau17c630b2023-01-19 23:58:11 +01002797 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 +01002798 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002799 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002800 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002801
Christopher Faulet485da0b2021-10-08 08:56:00 +02002802 if (error == 0) {
2803 /* Demux not blocked because of the stream, it is an incomplete frame */
2804 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2805 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002806 goto out; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002807 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002808
2809 if (error < 0) {
2810 /* Failed to decode this frame (e.g. too large request)
2811 * but the HPACK decompressor is still synchronized.
2812 */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002813 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002814 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau17c630b2023-01-19 23:58:11 +01002815 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 +02002816 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002817 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002818 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002819 goto done;
2820 }
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002821 /* the stream was already killed by an RST, let's consume
Willy Tarreau1f035502019-01-30 11:44:07 +01002822 * the data and send another RST.
2823 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002824 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002825 sess_log(h2c->conn->owner);
Willy Tarreau1f035502019-01-30 11:44:07 +01002826 h2s = (struct h2s*)h2_error_stream;
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002827 TRACE_USER("rcvd H2 trailers on closed stream", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, h2s, &rxbuf);
Willy Tarreau1f035502019-01-30 11:44:07 +01002828 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002829 }
2830 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2831 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2832 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreauf0279f62024-03-11 07:35:19 +01002833 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002834 TRACE_ERROR("HEADERS on invalid stream ID", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau4781b152021-04-06 13:53:36 +02002835 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002836 sess_log(h2c->conn->owner);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02002837 session_inc_http_req_ctr(h2c->conn->owner);
2838 session_inc_http_err_ctr(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002839 goto conn_err;
2840 }
Willy Tarreau4869ed52023-10-13 18:11:59 +02002841 else if (h2c->flags & H2_CF_DEM_TOOMANY) {
Willy Tarreau36c22322022-05-27 10:41:24 +02002842 goto out; // IDLE but too many sc still present
Willy Tarreau4869ed52023-10-13 18:11:59 +02002843 }
2844 else if (h2_fe_max_total_streams &&
2845 h2c->stream_cnt >= h2_fe_max_total_streams + h2c_max_concurrent_streams(h2c)) {
2846 /* We've already told this client we were going to close a
2847 * while ago and apparently it didn't care, so it's time to
2848 * stop processing its requests for real.
2849 */
2850 error = H2_ERR_ENHANCE_YOUR_CALM;
Willy Tarreauf0279f62024-03-11 07:35:19 +01002851 h2c_report_glitch(h2c, 1);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002852 TRACE_STATE("Stream limit violated", H2_EV_STRM_SHUT, h2c->conn);
2853 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
2854 sess_log(h2c->conn->owner);
2855 session_inc_http_req_ctr(h2c->conn->owner);
2856 session_inc_http_err_ctr(h2c->conn->owner);
2857 goto conn_err;
2858 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002859
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002860 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002861
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002862 if (error == 0) {
2863 /* No error but missing data for demuxing, it is an incomplete frame */
2864 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2865 h2c->flags |= H2_CF_DEM_SHORT_READ;
2866 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002867 }
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002868
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002869 /* Now we cannot roll back and we won't come back here anymore for this
2870 * stream, so this stream ID is open from a protocol perspective, even
2871 * if incomplete or broken, we want to count it as attempted.
2872 */
2873 if (h2c->dsi > h2c->max_id)
2874 h2c->max_id = h2c->dsi;
2875 h2c->stream_cnt++;
Willy Tarreau25919232019-01-03 14:48:18 +01002876
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002877 if (error < 0) {
2878 /* Failed to decode this stream. This might be due to a
2879 * recoverable error affecting only the stream (e.g. too large
2880 * request for buffer, that leaves the HPACK decompressor still
2881 * synchronized), or a non-recoverable error such as an invalid
2882 * frame type sequence (e.g. other frame type interleaved with
2883 * CONTINUATION), in which h2c_dec_hdrs() has already set the
2884 * error code in the connection and counted it in the relevant
2885 * stats. We still count a req error in both cases.
Willy Tarreau25919232019-01-03 14:48:18 +01002886 */
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002887 sess_log(h2c->conn->owner);
2888 session_inc_http_req_ctr(h2c->conn->owner);
2889 session_inc_http_err_ctr(h2c->conn->owner);
2890
2891 if (h2c->st0 >= H2_CS_ERROR) {
2892 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);
2893 goto out;
2894 }
2895
2896 /* recoverable stream error (e.g. too large request) */
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002897 TRACE_USER("rcvd unparsable H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, h2s, &rxbuf);
Willy Tarreau54310dc2024-01-12 18:36:57 +01002898 goto strm_err;
Willy Tarreau25919232019-01-03 14:48:18 +01002899 }
2900
Willy Tarreau29268e92021-06-17 08:29:14 +02002901 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn, 0, &rxbuf);
2902
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002903 /* Note: we don't emit any other logs below because if we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002904 * positively from h2c_frt_stream_new(), the stream will report the error,
2905 * and if we return in error, h2c_frt_stream_new() will emit the error.
Christopher Faulet7d013e72020-12-15 16:56:50 +01002906 *
2907 * Xfer the rxbuf to the stream. On success, the new stream owns the
2908 * rxbuf. On error, it is released here.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002909 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02002910 h2s = h2c_frt_stream_new(h2c, h2c->dsi, &rxbuf, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002911 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002912 h2s = (struct h2s*)h2_refused_stream;
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002913 TRACE_USER("refused H2 req. ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, h2s, &rxbuf);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002914 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002915 }
2916
2917 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002918 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002919 h2s->body_len = body_len;
Christopher Fauletc2f1d0e2023-05-24 11:34:45 +02002920 h2s_propagate_term_flags(h2c, h2s);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002921
Willy Tarreau88d138e2019-01-02 19:38:14 +01002922 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002923 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002924 if (h2s->st == H2_SS_OPEN)
2925 h2s->st = H2_SS_HREM;
2926 else
2927 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002928 }
Willy Tarreau0d6e5d22023-02-20 17:05:10 +01002929 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002930 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002931
2932 conn_err:
2933 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002934 out:
2935 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002936 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 +02002937 h2s = NULL;
2938 goto leave;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002939
Willy Tarreau54310dc2024-01-12 18:36:57 +01002940 strm_err:
Willy Tarreau54310dc2024-01-12 18:36:57 +01002941 h2s = (struct h2s*)h2_error_stream;
2942
Willy Tarreau96a10c22018-12-23 18:30:44 +01002943 send_rst:
2944 /* make the demux send an RST for the current stream. We may only
2945 * do this if we're certain that the HEADERS frame was properly
2946 * decompressed so that the HPACK decoder is still kept up to date.
2947 */
2948 h2_release_buf(h2c, &rxbuf);
2949 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002950
Willy Tarreau7838a792019-08-12 18:42:03 +02002951 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002952
2953 leave:
2954 if (h2_fe_max_total_streams && h2c->stream_cnt >= h2_fe_max_total_streams) {
2955 /* we've had enough streams on this connection, time to renew it.
2956 * In order to gracefully do this, we'll advertise a stream limit
2957 * of the current one plus the max concurrent streams value in the
2958 * GOAWAY frame, so that we're certain that the client is aware of
2959 * the limit before creating a new stream, but knows we won't harm
2960 * the streams in flight. Remember that client stream IDs are odd
2961 * so we apply twice the concurrent streams value to the current
2962 * ID.
2963 */
2964 if (h2c->last_sid <= 0 ||
2965 h2c->last_sid > h2c->max_id + 2 * h2c_max_concurrent_streams(h2c)) {
2966 /* not set yet or was too high */
2967 h2c->last_sid = h2c->max_id + 2 * h2c_max_concurrent_streams(h2c);
2968 h2c_send_goaway_error(h2c, NULL);
2969 }
2970 }
2971
Willy Tarreau96a10c22018-12-23 18:30:44 +01002972 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002973}
2974
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002975/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2976 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2977 * errors here are reported as connection errors since it's impossible to
2978 * recover from such errors after the compression context has been altered.
2979 */
2980static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2981{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002982 struct buffer rxbuf = BUF_NULL;
2983 unsigned long long body_len = 0;
2984 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002985 int error;
2986
Willy Tarreau7838a792019-08-12 18:42:03 +02002987 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2988
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002989 if (!b_size(&h2c->dbuf)) {
2990 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002991 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002992 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002993
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002994 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2995 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002996 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002997 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002998
Christopher Faulet6884aa32019-09-23 15:28:20 +02002999 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01003000 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len, h2s->upgrade_protocol);
Christopher Faulet6884aa32019-09-23 15:28:20 +02003001 }
3002 else {
3003 /* the connection was already killed by an RST, let's consume
3004 * the data and send another RST.
3005 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01003006 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Christopher Fauletea7a7782019-09-26 16:19:13 +02003007 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003008 h2c->st0 = H2_CS_FRAME_E;
3009 goto send_rst;
3010 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003011
Willy Tarreau25919232019-01-03 14:48:18 +01003012 /* unrecoverable error ? */
Willy Tarreau17c630b2023-01-19 23:58:11 +01003013 if (h2c->st0 >= H2_CS_ERROR) {
3014 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 +02003015 goto fail;
Willy Tarreau17c630b2023-01-19 23:58:11 +01003016 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003017
Willy Tarreau08bb1d62019-01-30 16:55:48 +01003018 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
3019 /* RFC7540#5.1 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003020 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003021 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 +01003022 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3023 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02003024 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003025 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01003026 }
3027
Willy Tarreau25919232019-01-03 14:48:18 +01003028 if (error <= 0) {
Christopher Faulet485da0b2021-10-08 08:56:00 +02003029 if (error == 0) {
3030 /* Demux not blocked because of the stream, it is an incomplete frame */
3031 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
3032 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003033 goto fail; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02003034 }
Willy Tarreau25919232019-01-03 14:48:18 +01003035
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003036 /* stream error : send RST_STREAM */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003037 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 +01003038 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003039 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02003040 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003041 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003042 }
3043
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003044 if (se_fl_test(h2s->sd, SE_FL_ERROR) && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003045 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02003046 else if (h2s->flags & H2_SF_ES_RCVD) {
3047 if (h2s->st == H2_SS_OPEN)
3048 h2s->st = H2_SS_HREM;
3049 else if (h2s->st == H2_SS_HLOC)
3050 h2s_close(h2s);
3051 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003052
Christopher Fauletf95f8762021-01-22 11:59:07 +01003053 /* Unblock busy server h2s waiting for the response headers to validate
3054 * the tunnel establishment or the end of the response of an oborted
3055 * tunnel
3056 */
3057 if ((h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY)) == (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY) ||
3058 (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)) {
3059 TRACE_STATE("Unblock h2s blocked on tunnel establishment/abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3060 h2s->flags &= ~H2_SF_BLK_MBUSY;
3061 }
3062
Willy Tarreau9abb3172021-06-16 18:32:42 +02003063 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 +02003064 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003065 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02003066 fail:
3067 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
3068 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003069
3070 send_rst:
3071 /* make the demux send an RST for the current stream. We may only
3072 * do this if we're certain that the HEADERS frame was properly
3073 * decompressed so that the HPACK decoder is still kept up to date.
3074 */
3075 h2_release_buf(h2c, &rxbuf);
3076 h2c->st0 = H2_CS_FRAME_E;
3077
Willy Tarreau022e5e52020-09-10 09:33:15 +02003078 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 +02003079 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
3080 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003081}
3082
Willy Tarreau454f9052017-10-26 19:40:35 +02003083/* processes a DATA frame. Returns > 0 on success or zero on missing data.
3084 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
3085 */
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003086static int h2c_handle_data(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003087{
3088 int error;
3089
Willy Tarreau7838a792019-08-12 18:42:03 +02003090 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3091
Willy Tarreau454f9052017-10-26 19:40:35 +02003092 /* note that empty DATA frames are perfectly valid and sometimes used
3093 * to signal an end of stream (with the ES flag).
3094 */
3095
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003096 if (!b_size(&h2c->dbuf) && h2c->dfl) {
3097 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003098 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003099 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003100
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003101 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
3102 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003103 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003104 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003105
3106 /* now either the frame is complete or the buffer is complete */
3107
Willy Tarreau454f9052017-10-26 19:40:35 +02003108 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
3109 /* RFC7540#6.1 */
3110 error = H2_ERR_STREAM_CLOSED;
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003111 goto strm_err_wu;
Willy Tarreau454f9052017-10-26 19:40:35 +02003112 }
3113
Christopher Faulet35feba62023-09-13 16:21:58 +02003114 if (!(h2s->flags & H2_SF_HEADERS_RCVD)) {
3115 /* RFC9113#8.1: The header section must be received before the message content */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003116 h2c_report_glitch(h2c, 1);
Christopher Faulet35feba62023-09-13 16:21:58 +02003117 TRACE_ERROR("Unexpected DATA frame before the message headers", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3118 error = H2_ERR_PROTOCOL_ERROR;
3119 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003120 goto strm_err_wu;
Christopher Faulet35feba62023-09-13 16:21:58 +02003121 }
Christopher Faulet4f09ec82019-06-19 09:25:58 +02003122 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003123 /* RFC7540#8.1.2 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003124 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003125 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 +01003126 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003127 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003128 goto strm_err_wu;
Willy Tarreau1915ca22019-01-24 11:49:37 +01003129 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003130 if (!(h2c->flags & H2_CF_IS_BACK) &&
3131 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT) &&
3132 ((h2c->dfl - h2c->dpl) || !(h2c->dff & H2_F_DATA_END_STREAM))) {
3133 /* a tunnel attempt was aborted but the client still try to send some raw data.
3134 * Thus the stream is closed with the CANCEL error. Here we take care it is not
3135 * an empty DATA Frame with the ES flag. The error is only handled if ES was
3136 * already sent to the client because depending on the scheduling, these data may
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003137 * have been sent before the server response but not handle here.
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003138 */
3139 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3140 error = H2_ERR_CANCEL;
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003141 goto strm_err_wu;
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003142 }
Willy Tarreau1915ca22019-01-24 11:49:37 +01003143
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003144 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02003145 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003146
Willy Tarreau454f9052017-10-26 19:40:35 +02003147 /* call the upper layers to process the frame, then let the upper layer
3148 * notify the stream about any change.
3149 */
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003150 if (!h2s_sc(h2s)) {
Willy Tarreau082c4572019-08-06 10:11:02 +02003151 /* The upper layer has already closed, this may happen on
3152 * 4xx/redirects during POST, or when receiving a response
3153 * from an H2 server after the client has aborted.
3154 */
3155 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003156 goto strm_err;
3157 }
3158
Willy Tarreau8f650c32017-11-21 19:36:21 +01003159 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003160 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01003161
Willy Tarreau721c9742017-11-07 11:05:42 +01003162 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003163 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01003164 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02003165 }
3166
3167 /* check for completion : the callee will change this to FRAME_A or
3168 * FRAME_H once done.
3169 */
3170 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02003171 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02003172
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003173 /* last frame */
3174 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02003175 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01003176 if (h2s->st == H2_SS_OPEN)
3177 h2s->st = H2_SS_HREM;
3178 else
3179 h2s_close(h2s);
3180
Willy Tarreau1915ca22019-01-24 11:49:37 +01003181 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
3182 /* RFC7540#8.1.2 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003183 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003184 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 +01003185 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003186 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003187 goto strm_err;
3188 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003189 }
3190
Christopher Fauletf95f8762021-01-22 11:59:07 +01003191 /* Unblock busy server h2s waiting for the end of the response for an
3192 * aborted tunnel
3193 */
3194 if ((h2c->flags & H2_CF_IS_BACK) &&
3195 (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)) {
3196 TRACE_STATE("Unblock h2s blocked on tunnel abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3197 h2s->flags &= ~H2_SF_BLK_MBUSY;
3198 }
3199
Willy Tarreau7838a792019-08-12 18:42:03 +02003200 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003201 return 1;
3202
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003203 strm_err_wu:
3204 /* stream error before the frame was taken into account, we're
3205 * going to kill the stream but must still update the connection's
3206 * window.
3207 */
3208 h2c->rcvd_c += h2c->dfl - h2c->dpl;
Willy Tarreau454f9052017-10-26 19:40:35 +02003209 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01003210 h2s_error(h2s, error);
3211 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003212 fail:
3213 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 +02003214 return 0;
3215}
3216
Willy Tarreau63864812019-08-07 14:25:20 +02003217/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
3218 * valid for the current stream state. This is needed only after parsing the
3219 * frame header but in practice it can be performed at any time during
3220 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
3221 * or 0 in case of error, in which case either h2s or h2c will carry an error.
3222 */
3223static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
3224{
Willy Tarreau7838a792019-08-12 18:42:03 +02003225 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
3226
Willy Tarreau63864812019-08-07 14:25:20 +02003227 if (h2s->st == H2_SS_IDLE &&
3228 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
3229 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
3230 * this state MUST be treated as a connection error
3231 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003232 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003233 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 +02003234 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003235 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02003236 /* only log if no other stream can report the error */
3237 sess_log(h2c->conn->owner);
3238 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003239 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003240 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 +02003241 return 0;
3242 }
3243
Willy Tarreau57a18162019-11-24 14:57:53 +01003244 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
3245 /* only PUSH_PROMISE would be permitted here */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003246 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003247 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 +01003248 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003249 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau57a18162019-11-24 14:57:53 +01003250 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
3251 return 0;
3252 }
3253
Willy Tarreau63864812019-08-07 14:25:20 +02003254 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
3255 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
3256 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
3257 * this state MUST be treated as a stream error.
3258 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
3259 * PUSH_PROMISE/CONTINUATION cause connection errors.
3260 */
Amaury Denoyellea8879232020-10-27 17:16:03 +01003261 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003262 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003263 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 +02003264 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003265 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003266 }
3267 else {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003268 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003269 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003270 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003271 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 +02003272 return 0;
3273 }
3274
3275 /* Below the management of frames received in closed state is a
3276 * bit hackish because the spec makes strong differences between
3277 * streams closed by receiving RST, sending RST, and seeing ES
3278 * in both directions. In addition to this, the creation of a
3279 * new stream reusing the identifier of a closed one will be
3280 * detected here. Given that we cannot keep track of all closed
3281 * streams forever, we consider that unknown closed streams were
3282 * closed on RST received, which allows us to respond with an
3283 * RST without breaking the connection (eg: to abort a transfer).
3284 * Some frames have to be silently ignored as well.
3285 */
3286 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
3287 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
3288 /* #5.1.1: The identifier of a newly
3289 * established stream MUST be numerically
3290 * greater than all streams that the initiating
3291 * endpoint has opened or reserved. This
3292 * governs streams that are opened using a
3293 * HEADERS frame and streams that are reserved
3294 * using PUSH_PROMISE. An endpoint that
3295 * receives an unexpected stream identifier
3296 * MUST respond with a connection error.
3297 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003298 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003299 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003300 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 +02003301 return 0;
3302 }
3303
Willy Tarreau4c08f122019-09-26 08:47:15 +02003304 if (h2s->flags & H2_SF_RST_RCVD &&
3305 !(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 +02003306 /* RFC7540#5.1:closed: an endpoint that
3307 * receives any frame other than PRIORITY after
3308 * receiving a RST_STREAM MUST treat that as a
3309 * stream error of type STREAM_CLOSED.
3310 *
3311 * Note that old streams fall into this category
3312 * and will lead to an RST being sent.
3313 *
3314 * However, we cannot generalize this to all frame types. Those
3315 * carrying compression state must still be processed before
3316 * being dropped or we'll desynchronize the decoder. This can
3317 * happen with request trailers received after sending an
3318 * RST_STREAM, or with header/trailers responses received after
3319 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02003320 *
3321 * In addition, since our CLOSED streams always carry the
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003322 * RST_RCVD bit, we don't want to accidentally catch valid
Willy Tarreau4c08f122019-09-26 08:47:15 +02003323 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02003324 */
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003325 if (h2c->dft == H2_FT_DATA) {
3326 /* even if we reject out-of-stream DATA, it must
3327 * still count against the connection's flow control.
3328 */
3329 h2c->rcvd_c += h2c->dfl - h2c->dpl;
3330 }
3331
Willy Tarreauf0279f62024-03-11 07:35:19 +01003332 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003333 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3334 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003335 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 +02003336 return 0;
3337 }
3338
3339 /* RFC7540#5.1:closed: if this state is reached as a
3340 * result of sending a RST_STREAM frame, the peer that
3341 * receives the RST_STREAM might have already sent
3342 * frames on the stream that cannot be withdrawn. An
3343 * endpoint MUST ignore frames that it receives on
3344 * closed streams after it has sent a RST_STREAM
3345 * frame. An endpoint MAY choose to limit the period
3346 * over which it ignores frames and treat frames that
3347 * arrive after this time as being in error.
3348 */
3349 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
3350 /* RFC7540#5.1:closed: any frame other than
3351 * PRIO/WU/RST in this state MUST be treated as
3352 * a connection error
3353 */
3354 if (h2c->dft != H2_FT_RST_STREAM &&
3355 h2c->dft != H2_FT_PRIORITY &&
3356 h2c->dft != H2_FT_WINDOW_UPDATE) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003357 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003358 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003359 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 +02003360 return 0;
3361 }
3362 }
3363 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003364 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003365 return 1;
3366}
3367
Willy Tarreaubc933932017-10-09 16:21:43 +02003368/* process Rx frames to be demultiplexed */
3369static void h2_process_demux(struct h2c *h2c)
3370{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003371 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003372 struct h2_fh hdr;
3373 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003374 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02003375
Willy Tarreau7838a792019-08-12 18:42:03 +02003376 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3377
Willy Tarreau081d4722017-05-16 21:51:05 +02003378 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003379 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02003380
3381 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3382 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003383 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02003384 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02003385 goto out;
3386
Willy Tarreau52eed752017-09-22 15:05:09 +02003387 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
3388 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003389 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau22892322024-01-17 16:57:23 +01003390 if (b_data(&h2c->dbuf) ||
3391 !(((const struct session *)h2c->conn->owner)->fe->options & (PR_O_NULLNOLOG|PR_O_IGNORE_PRB)))
Willy Tarreauf0279f62024-03-11 07:35:19 +01003392 h2c_report_glitch(h2c, 1);
Willy Tarreau22892322024-01-17 16:57:23 +01003393
Willy Tarreau7838a792019-08-12 18:42:03 +02003394 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003395 h2c->st0 = H2_CS_ERROR2;
Willy Tarreauee4684f2021-06-17 08:08:48 +02003396 if (b_data(&h2c->dbuf) ||
Christopher Faulet3f35da22021-07-26 10:18:35 +02003397 !(((const struct session *)h2c->conn->owner)->fe->options & (PR_O_NULLNOLOG|PR_O_IGNORE_PRB)))
Willy Tarreauee4684f2021-06-17 08:08:48 +02003398 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003399 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003400 goto done;
Willy Tarreau52eed752017-09-22 15:05:09 +02003401 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003402 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003403
3404 h2c->max_id = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02003405 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaued2b9d92022-08-18 15:30:41 +02003406 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau52eed752017-09-22 15:05:09 +02003407 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003408
3409 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003410 /* ensure that what is pending is a valid SETTINGS frame
3411 * without an ACK.
3412 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003413 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 +02003414 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003415 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003416 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau22de8d32018-09-05 19:55:58 +02003417 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003418 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003419 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 +02003420 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003421 if (!(h2c->flags & H2_CF_IS_BACK))
3422 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003423 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003424 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003425 }
3426
3427 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
3428 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003429 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003430 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 +02003431 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3432 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003433 if (!(h2c->flags & H2_CF_IS_BACK))
3434 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003435 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003436 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003437 }
3438
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003439 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003440 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003441 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003442 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 +02003443 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3444 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003445 if (!(h2c->flags & H2_CF_IS_BACK))
3446 sess_log(h2c->conn->owner);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003447 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003448 }
3449
Willy Tarreau3bf69182018-12-21 15:34:50 +01003450 /* that's OK, switch to FRAME_P to process it. This is
3451 * a SETTINGS frame whose header has already been
3452 * deleted above.
3453 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01003454 padlen = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +02003455 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003456 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003457 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003458 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003459
3460 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02003461 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02003462 int ret = 0;
3463
Willy Tarreau7838a792019-08-12 18:42:03 +02003464 if (!b_data(&h2c->dbuf)) {
3465 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003466 h2c->flags |= H2_CF_DEM_SHORT_READ;
3467 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003468 }
3469
3470 if (h2c->st0 >= H2_CS_ERROR) {
3471 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003472 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003473 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003474
3475 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003476 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003477 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) {
3478 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003479 break;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003480 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003481
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003482 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003483 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003484 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 +02003485 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003486 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02003487 /* only log if no other stream can report the error */
3488 sess_log(h2c->conn->owner);
3489 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003490 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003491 break;
3492 }
3493
Willy Tarreau617592c2022-06-08 16:32:22 +02003494 if (h2c->rcvd_s && h2c->dsi != hdr.sid) {
3495 /* changed stream with a pending WU, need to
3496 * send it now.
3497 */
3498 TRACE_PROTO("sending stream WINDOW_UPDATE frame on stream switch", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
3499 ret = h2c_send_strm_wu(h2c);
3500 if (ret <= 0)
3501 break;
3502 }
3503
Christopher Fauletdd2a5622019-06-18 12:22:38 +02003504 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003505 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
3506 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
3507 * we read the pad length and drop it from the remaining
3508 * payload (one byte + the 9 remaining ones = 10 total
3509 * removed), so we have a frame payload starting after the
3510 * pad len. Flow controlled frames (DATA) also count the
3511 * padlen in the flow control, so it must be adjusted.
3512 */
3513 if (hdr.len < 1) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003514 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003515 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 +01003516 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003517 if (!(h2c->flags & H2_CF_IS_BACK))
3518 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003519 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003520 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003521 }
3522 hdr.len--;
3523
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003524 if (b_data(&h2c->dbuf) < 10) {
3525 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003526 break; // missing padlen
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003527 }
Willy Tarreau3bf69182018-12-21 15:34:50 +01003528
3529 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3530
3531 if (padlen > hdr.len) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003532 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003533 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 +01003534 /* RFC7540#6.1 : pad length = length of
3535 * frame payload or greater => error.
3536 */
3537 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003538 if (!(h2c->flags & H2_CF_IS_BACK))
3539 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003540 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003541 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003542 }
3543
3544 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3545 h2c->rcvd_c++;
3546 h2c->rcvd_s++;
3547 }
3548 b_del(&h2c->dbuf, 1);
3549 }
3550 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003551
3552 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003553 h2c->dfl = hdr.len;
3554 h2c->dsi = hdr.sid;
3555 h2c->dft = hdr.ft;
3556 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003557 h2c->dpl = padlen;
Willy Tarreau0f458712022-08-18 11:19:57 +02003558 h2c->flags |= H2_CF_DEM_IN_PROGRESS;
Willy Tarreau73db4342019-09-25 07:28:44 +02003559 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 +02003560 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003561
3562 /* check for minimum basic frame format validity */
3563 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3564 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003565 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003566 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 +01003567 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003568 if (!(h2c->flags & H2_CF_IS_BACK))
3569 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003570 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003571 goto done;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003572 }
Willy Tarreau15a47332022-03-18 15:57:34 +01003573
3574 /* transition to HEADERS frame ends the keep-alive idle
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003575 * timer and starts the http-request idle delay. It uses
3576 * the idle_start timer as well.
Willy Tarreau15a47332022-03-18 15:57:34 +01003577 */
3578 if (hdr.ft == H2_FT_HEADERS)
3579 h2c->idle_start = now_ms;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003580 }
3581
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003582 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3583 * H2_CS_FRAME_P indicates an incomplete previous operation
3584 * (most often the first attempt) and requires some validity
3585 * checks for the frame and the current state. The two other
3586 * ones are set after completion (or abortion) and must skip
3587 * validity checks.
3588 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003589 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3590
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003591 if (tmp_h2s != h2s && h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003592 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003593 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003594 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003595 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003596 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003597 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003598 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 +02003599 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003600 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003601 }
3602 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003603
Willy Tarreau63864812019-08-07 14:25:20 +02003604 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003605 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3606 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003607 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003608 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003609
Willy Tarreau7e98c052017-10-10 15:56:59 +02003610 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003611 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003612 if (h2c->st0 == H2_CS_FRAME_P) {
3613 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003614 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003615 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003616 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003617
Willy Tarreau7838a792019-08-12 18:42:03 +02003618 if (h2c->st0 == H2_CS_FRAME_A) {
3619 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 +02003620 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003621 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003622 break;
3623
Willy Tarreaucf68c782017-10-10 17:11:41 +02003624 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003625 if (h2c->st0 == H2_CS_FRAME_P) {
3626 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003627 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003628 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003629
Willy Tarreau7838a792019-08-12 18:42:03 +02003630 if (h2c->st0 == H2_CS_FRAME_A) {
3631 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 +02003632 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003633 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003634 break;
3635
Willy Tarreau26f95952017-07-27 17:18:30 +02003636 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003637 if (h2c->st0 == H2_CS_FRAME_P) {
3638 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 +02003639 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003640 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003641 break;
3642
Willy Tarreau61290ec2017-10-17 08:19:21 +02003643 case H2_FT_CONTINUATION:
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003644 /* RFC7540#6.10: CONTINUATION may only be preceded by
Willy Tarreauea18f862018-12-22 20:19:26 +01003645 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3646 * frames' parsers consume all following CONTINUATION
3647 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003648 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003649 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003650 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 +01003651 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003652 if (!(h2c->flags & H2_CF_IS_BACK))
3653 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003654 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003655 goto done;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003656
Willy Tarreau13278b42017-10-13 19:23:14 +02003657 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003658 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003659 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003660 if (h2c->flags & H2_CF_IS_BACK)
3661 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3662 else
3663 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003664 if (tmp_h2s) {
3665 h2s = tmp_h2s;
3666 ret = 1;
3667 }
3668 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003669 HA_ATOMIC_INC(&h2c->px_counters->headers_rcvd);
Willy Tarreau13278b42017-10-13 19:23:14 +02003670 break;
3671
Willy Tarreau454f9052017-10-26 19:40:35 +02003672 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003673 if (h2c->st0 == H2_CS_FRAME_P) {
3674 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003675 ret = h2c_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003676 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003677 HA_ATOMIC_INC(&h2c->px_counters->data_rcvd);
Willy Tarreau454f9052017-10-26 19:40:35 +02003678
Willy Tarreau7838a792019-08-12 18:42:03 +02003679 if (h2c->st0 == H2_CS_FRAME_A) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003680 /* rcvd_s will suffice to trigger the sending of a WU */
3681 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7838a792019-08-12 18:42:03 +02003682 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003683 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003684
Willy Tarreau92153fc2017-12-03 19:46:19 +01003685 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003686 if (h2c->st0 == H2_CS_FRAME_P) {
3687 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003688 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003689 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003690 break;
3691
Willy Tarreaucd234e92017-08-18 10:59:39 +02003692 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003693 if (h2c->st0 == H2_CS_FRAME_P) {
3694 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 +02003695 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003696 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003697 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_rcvd);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003698 break;
3699
Willy Tarreaue96b0922017-10-30 00:28:29 +01003700 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003701 if (h2c->st0 == H2_CS_FRAME_P) {
3702 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003703 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003704 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003705 HA_ATOMIC_INC(&h2c->px_counters->goaway_rcvd);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003706 break;
3707
Willy Tarreau1c661982017-10-30 13:52:01 +01003708 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003709 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003710 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003711 /* drop frames that we ignore. They may be larger than
3712 * the buffer so we drain all of their contents until
3713 * we reach the end.
3714 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003715 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3716 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003717 h2c->dfl -= ret;
3718 ret = h2c->dfl == 0;
3719 }
3720
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003721 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003722 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003723 if (h2s->st == H2_SS_ERROR) {
3724 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 +01003725 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003726 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003727
Willy Tarreau7838a792019-08-12 18:42:03 +02003728 if (h2c->st0 == H2_CS_FRAME_E) {
3729 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 +01003730 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003731 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003732
Willy Tarreau7e98c052017-10-10 15:56:59 +02003733 /* error or missing data condition met above ? */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003734 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02003735 break;
3736
3737 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaubba7a4d2020-09-18 07:41:28 +02003738 if (h2c->dfl)
3739 TRACE_DEVEL("skipping remaining frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
Christopher Faulet5112a602019-09-26 16:38:28 +02003740 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3741 b_del(&h2c->dbuf, ret);
3742 h2c->dfl -= ret;
3743 if (!h2c->dfl) {
Willy Tarreau0f458712022-08-18 11:19:57 +02003744 h2c->flags &= ~H2_CF_DEM_IN_PROGRESS;
Christopher Faulet5112a602019-09-26 16:38:28 +02003745 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3746 h2c->st0 = H2_CS_FRAME_H;
Christopher Faulet5112a602019-09-26 16:38:28 +02003747 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003748 }
3749 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003750
Willy Tarreau617592c2022-06-08 16:32:22 +02003751 if (h2c->rcvd_s > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003752 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003753 TRACE_PROTO("sending stream WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn, h2s);
3754 h2c_send_strm_wu(h2c);
3755 }
3756
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003757 if (h2c->rcvd_c > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003758 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003759 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003760 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003761 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003762
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003763 done:
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003764 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_DEM_SHORT_READ)) {
3765 if (h2c->flags & H2_CF_RCVD_SHUT)
3766 h2c->flags |= H2_CF_END_REACHED;
3767 }
3768
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003769 if (h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003770 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003771 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003772 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003773 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003774 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003775 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003776 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 +02003777 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003778 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003779 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003780
Willy Tarreau7838a792019-08-12 18:42:03 +02003781 if (old_iw != h2c->miw) {
3782 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003783 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003784 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003785
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003786 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003787 out:
3788 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003789 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02003790}
3791
Willy Tarreau989539b2020-01-10 17:01:29 +01003792/* resume each h2s eligible for sending in list head <head> */
3793static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3794{
3795 struct h2s *h2s, *h2s_back;
3796
3797 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3798
3799 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3800 if (h2c->mws <= 0 ||
3801 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3802 h2c->st0 >= H2_CS_ERROR)
3803 break;
3804
3805 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003806
Willy Tarreaud9464162020-01-10 18:25:07 +01003807 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003808 continue;
3809
Willy Tarreau5723f292020-01-10 15:16:57 +01003810 /* If the sender changed his mind and unsubscribed, let's just
3811 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003812 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003813 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3814 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003815 LIST_DEL_INIT(&h2s->list);
3816 continue;
3817 }
3818
Willy Tarreauf96508a2020-01-10 11:12:48 +01003819 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003820 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003821 tasklet_wakeup(h2s->subs->tasklet);
3822 h2s->subs->events &= ~SUB_RETRY_SEND;
3823 if (!h2s->subs->events)
3824 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003825 }
3826 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3827 tasklet_wakeup(h2s->shut_tl);
3828 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003829 }
3830
3831 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3832}
3833
Willy Tarreaude4a5382023-10-17 08:25:19 +02003834/* removes a stream from the list it may be in. If a stream has recently been
3835 * appended to the send_list, it might have been waiting on this one when
3836 * entering h2_snd_buf() and expecting it to complete before starting to send
3837 * in turn. For this reason we check (and clear) H2_CF_WAIT_INLIST to detect
3838 * this condition, and we try to resume sending streams if it happens. Note
3839 * that we don't need to do it for fctl_list as this list is relevant before
3840 * (only consulted after) a window update on the connection, and not because
3841 * of any competition with other streams.
3842 */
3843static inline void h2_remove_from_list(struct h2s *h2s)
3844{
3845 struct h2c *h2c = h2s->h2c;
3846
3847 if (!LIST_INLIST(&h2s->list))
3848 return;
3849
3850 LIST_DEL_INIT(&h2s->list);
3851 if (h2c->flags & H2_CF_WAIT_INLIST) {
3852 h2c->flags &= ~H2_CF_WAIT_INLIST;
3853 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
3854 }
3855}
3856
Willy Tarreaubc933932017-10-09 16:21:43 +02003857/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3858 * the end.
3859 */
3860static int h2_process_mux(struct h2c *h2c)
3861{
Willy Tarreau7838a792019-08-12 18:42:03 +02003862 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3863
Willy Tarreau01b44822018-10-03 14:26:37 +02003864 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3865 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3866 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3867 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003868 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003869 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003870 goto fail;
3871 }
3872 h2c->st0 = H2_CS_SETTINGS1;
3873 }
3874 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003875 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003876 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003877 }
3878
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003879 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003880 if (h2c->rcvd_s > 0 &&
3881 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3882 h2c_send_strm_wu(h2c) < 0)
3883 goto fail;
3884
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003885 if (h2c->rcvd_c > 0 &&
3886 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3887 h2c_send_conn_wu(h2c) < 0)
3888 goto fail;
3889
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003890 /* First we always process the flow control list because the streams
3891 * waiting there were already elected for immediate emission but were
3892 * blocked just on this.
3893 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02003894 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01003895 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3896 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003897
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003898 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003899 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003900 if (h2c->st0 == H2_CS_ERROR) {
3901 if (h2c->max_id >= 0) {
3902 h2c_send_goaway_error(h2c, NULL);
3903 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003904 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003905 }
3906
3907 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3908 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003909 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003910 done:
3911 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3912 return 1;
3913 out0:
3914 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3915 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003916}
3917
Willy Tarreau62f52692017-10-08 23:01:42 +02003918
Willy Tarreau479998a2018-11-18 06:30:59 +01003919/* Attempt to read data, and subscribe if none available.
3920 * The function returns 1 if data has been received, otherwise zero.
3921 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003922static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003923{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003924 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003925 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003926 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003927 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003928
Willy Tarreau7838a792019-08-12 18:42:03 +02003929 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3930
3931 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3932 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003933 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003934 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003935
Willy Tarreau7838a792019-08-12 18:42:03 +02003936 if (!h2_recv_allowed(h2c)) {
3937 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003938 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003939 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003940
Willy Tarreau44e973f2018-03-01 17:49:30 +01003941 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003942 if (!buf) {
3943 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003944 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003945 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003946 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003947
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003948 if (!b_data(buf)) {
3949 /* try to pre-align the buffer like the
3950 * rxbufs will be to optimize memory copies. We'll make
3951 * sure that the frame header lands at the end of the
3952 * HTX block to alias it upon recv. We cannot use the
3953 * head because rcv_buf() will realign the buffer if
3954 * it's empty. Thus we cheat and pretend we already
3955 * have a few bytes there.
3956 */
3957 max = buf_room_for_htx_data(buf) + 9;
3958 buf->head = sizeof(struct htx) - 9;
3959 }
3960 else
3961 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003962
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003963 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003964
Christopher Fauletde9d6052021-04-23 12:25:18 +02003965 if (max && !ret && h2_recv_allowed(h2c)) {
3966 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
3967 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003968 } else if (ret) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02003969 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn, 0, 0, (void*)(long)ret);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003970 h2c->flags &= ~H2_CF_DEM_SHORT_READ;
3971 }
Olivier Houchard81a15af2018-10-19 17:26:49 +02003972
Christopher Fauletde9d6052021-04-23 12:25:18 +02003973 if (conn_xprt_read0_pending(h2c->conn)) {
3974 TRACE_DATA("received read0", H2_EV_H2C_RECV, h2c->conn);
3975 h2c->flags |= H2_CF_RCVD_SHUT;
3976 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02003977 if (h2c->conn->flags & CO_FL_ERROR) {
3978 TRACE_DATA("connection error", H2_EV_H2C_RECV, h2c->conn);
3979 h2c->flags |= H2_CF_ERROR;
3980 }
Christopher Fauletde9d6052021-04-23 12:25:18 +02003981
Olivier Houcharda1411e62018-08-17 18:42:48 +02003982 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003983 h2_release_buf(h2c, &h2c->dbuf);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003984 goto end;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003985 }
3986
Willy Tarreau7838a792019-08-12 18:42:03 +02003987 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003988 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003989 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003990 }
3991
Christopher Fauletff7925d2022-10-11 19:12:40 +02003992 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02003993 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003994 return !!ret || (h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR));
Willy Tarreau62f52692017-10-08 23:01:42 +02003995}
3996
Willy Tarreau479998a2018-11-18 06:30:59 +01003997/* Try to send data if possible.
3998 * The function returns 1 if data have been sent, otherwise zero.
3999 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02004000static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02004001{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004002 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02004003 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02004004 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02004005
Willy Tarreau7838a792019-08-12 18:42:03 +02004006 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02004007
Christopher Fauletff7925d2022-10-11 19:12:40 +02004008 if (h2c->flags & (H2_CF_ERROR|H2_CF_ERR_PENDING)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004009 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02004010 if (h2c->flags & H2_CF_RCVD_SHUT)
4011 h2c->flags |= H2_CF_ERROR;
4012 b_reset(br_tail(h2c->mbuf));
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004013 h2c->idle_start = now_ms;
Willy Tarreau7838a792019-08-12 18:42:03 +02004014 return 1;
4015 }
Olivier Houchard7505f942018-08-21 18:10:44 +02004016
Willy Tarreaubc933932017-10-09 16:21:43 +02004017 /* This loop is quite simple : it tries to fill as much as it can from
4018 * pending streams into the existing buffer until it's reportedly full
4019 * or the end of send requests is reached. Then it tries to send this
4020 * buffer's contents out, marks it not full if at least one byte could
4021 * be sent, and tries again.
4022 *
4023 * The snd_buf() function normally takes a "flags" argument which may
4024 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
4025 * data immediately comes and CO_SFL_STREAMER to indicate that the
4026 * connection is streaming lots of data (used to increase TLS record
4027 * size at the expense of latency). The former can be sent any time
4028 * there's a buffer full flag, as it indicates at least one stream
4029 * attempted to send and failed so there are pending data. An
4030 * alternative would be to set it as long as there's an active stream
4031 * but that would be problematic for ACKs until we have an absolute
4032 * guarantee that all waiters have at least one byte to send. The
4033 * latter should possibly not be set for now.
4034 */
4035
4036 done = 0;
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004037 while (!(conn->flags & CO_FL_WAIT_XPRT) && !done) {
Willy Tarreaubc933932017-10-09 16:21:43 +02004038 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004039 unsigned int released = 0;
4040 struct buffer *buf;
Willy Tarreau464fa062023-03-17 16:09:14 +01004041 uint to_send;
Willy Tarreaubc933932017-10-09 16:21:43 +02004042
4043 /* fill as much as we can into the current buffer */
4044 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
4045 done = h2_process_mux(h2c);
4046
Olivier Houchard2b094432019-01-29 18:28:36 +01004047 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004048 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01004049
Christopher Faulet9a3d3fc2020-10-22 16:24:58 +02004050 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
Willy Tarreaue6dc7a02021-10-21 17:30:06 +02004051 (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02004052 break;
4053
Christopher Faulet68ee7842022-10-12 10:21:33 +02004054 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))
Willy Tarreaubc933932017-10-09 16:21:43 +02004055 flags |= CO_SFL_MSG_MORE;
4056
Willy Tarreau464fa062023-03-17 16:09:14 +01004057 to_send = br_count(h2c->mbuf);
4058 if (to_send > 1) {
Willy Tarreau14ea98a2023-03-16 17:30:30 +01004059 /* usually we want to emit small TLS records to speed
4060 * up the decoding on the client. That's what is being
4061 * done by default. However if there is more than one
4062 * buffer being allocated, we're streaming large data
4063 * so we stich to large records.
4064 */
4065 flags |= CO_SFL_STREAMER;
4066 }
4067
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004068 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4069 if (b_data(buf)) {
Willy Tarreau464fa062023-03-17 16:09:14 +01004070 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf),
4071 flags | (to_send > 1 ? CO_SFL_MSG_MORE : 0));
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004072 if (!ret) {
4073 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004074 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004075 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004076 sent = 1;
Willy Tarreau464fa062023-03-17 16:09:14 +01004077 to_send--;
Willy Tarreau022e5e52020-09-10 09:33:15 +02004078 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn, 0, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004079 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004080 if (b_data(buf)) {
4081 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004082 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004083 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004084 }
4085 b_free(buf);
4086 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02004087 }
Willy Tarreaubc933932017-10-09 16:21:43 +02004088
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004089 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004090 offer_buffers(NULL, released);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004091
Willy Tarreau93c55112023-03-16 16:47:44 +01004092 /* Normally if wrote at least one byte, the buffer is not full
4093 * anymore. However, if it was marked full because all of its
4094 * buffers were used, we don't want to instantly wake up many
4095 * streams because we'd create a thundering herd effect, notably
4096 * when data are flushed in small chunks. Instead we wait for
4097 * the buffer to be decongested again before allowing to send
4098 * again. It also has the added benefit of not pumping more
4099 * data from the other side when it's known that this one is
4100 * still congested.
4101 */
4102 if (sent && br_single(h2c->mbuf))
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02004103 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02004104 }
4105
Christopher Fauletff7925d2022-10-11 19:12:40 +02004106 if (conn->flags & CO_FL_ERROR) {
4107 h2c->flags |= H2_CF_ERR_PENDING;
4108 if (h2c->flags & H2_CF_RCVD_SHUT)
4109 h2c->flags |= H2_CF_ERROR;
Willy Tarreau51330962019-05-26 09:38:07 +02004110 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02004111 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02004112
Olivier Houchard6ff20392018-07-17 18:46:31 +02004113 /* We're not full anymore, so we can wake any task that are waiting
4114 * for us.
4115 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004116 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
4117 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01004118 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02004119 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01004120
Olivier Houchard910b2bc2018-07-17 18:49:38 +02004121 /* We're done, no more to send */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004122 if (!(conn->flags & CO_FL_WAIT_XPRT) && !br_data(h2c->mbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004123 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Willy Tarreau936db562023-10-18 11:39:43 +02004124 if (h2c->flags & H2_CF_MBUF_HAS_DATA && !h2c->nb_sc) {
4125 h2c->flags &= ~H2_CF_MBUF_HAS_DATA;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004126 h2c->idle_start = now_ms;
Willy Tarreau936db562023-10-18 11:39:43 +02004127 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02004128 goto end;
Willy Tarreau7838a792019-08-12 18:42:03 +02004129 }
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004130
Willy Tarreau7838a792019-08-12 18:42:03 +02004131 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
4132 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004133 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02004134 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004135 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02004136end:
4137 return sent || (h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR));
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004138}
4139
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004140/* this is the tasklet referenced in h2c->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004141struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004142{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004143 struct connection *conn;
4144 struct tasklet *tl = (struct tasklet *)t;
4145 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004146 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02004147 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004148
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004149 if (state & TASK_F_USR1) {
4150 /* the tasklet was idling on an idle connection, it might have
4151 * been stolen, let's be careful!
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004152 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004153 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4154 if (t->context == NULL) {
4155 /* The connection has been taken over by another thread,
4156 * we're no longer responsible for it, so just free the
4157 * tasklet, and do nothing.
4158 */
4159 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4160 tasklet_free(tl);
Willy Tarreau74163142021-03-13 11:30:19 +01004161 t = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004162 goto leave;
4163 }
4164 conn = h2c->conn;
4165 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004166
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004167 /* Remove the connection from the list, to be sure nobody attempts
4168 * to use it while we handle the I/O events
4169 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004170 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004171 if (conn_in_list)
4172 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004173
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004174 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4175 } else {
4176 /* we're certain the connection was not in an idle list */
4177 conn = h2c->conn;
4178 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4179 conn_in_list = 0;
4180 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004181
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004182 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02004183 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004184 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02004185 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01004186 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004187 ret = h2_process(h2c);
4188
4189 /* If we were in an idle list, we want to add it back into it,
4190 * unless h2_process() returned -1, which mean it has destroyed
4191 * the connection (testing !ret is enough, if h2_process() wasn't
4192 * called then ret will be 0 anyway.
4193 */
Willy Tarreau74163142021-03-13 11:30:19 +01004194 if (ret < 0)
4195 t = NULL;
4196
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004197 if (!ret && conn_in_list) {
4198 struct server *srv = objt_server(conn->target);
4199
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004200 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004201 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02004202 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004203 else
Willy Tarreau85223482022-09-29 20:32:43 +02004204 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004205 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004206 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004207
Willy Tarreau38468772020-06-28 00:31:13 +02004208leave:
Willy Tarreau7838a792019-08-12 18:42:03 +02004209 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreau74163142021-03-13 11:30:19 +01004210 return t;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004211}
Willy Tarreaua2af5122017-10-09 11:56:46 +02004212
Willy Tarreau62f52692017-10-08 23:01:42 +02004213/* callback called on any event by the connection handler.
4214 * It applies changes and returns zero, or < 0 if it wants immediate
4215 * destruction of the connection (which normally doesn not happen in h2).
4216 */
Olivier Houchard7505f942018-08-21 18:10:44 +02004217static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02004218{
Olivier Houchard7505f942018-08-21 18:10:44 +02004219 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02004220
Willy Tarreau7838a792019-08-12 18:42:03 +02004221 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4222
Willy Tarreauf0961222021-02-05 11:41:46 +01004223 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) &&
4224 (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01004225 h2_process_demux(h2c);
4226
Christopher Fauletff7925d2022-10-11 19:12:40 +02004227 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_ERROR))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004228 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004229
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004230 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01004231 h2c->flags &= ~H2_CF_DEM_DFULL;
4232 }
Olivier Houchard7505f942018-08-21 18:10:44 +02004233 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004234
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004235 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 +02004236 int send_goaway = 1;
4237 /* If a close-spread-time option is set, we want to avoid
4238 * closing all the active HTTP2 connections at once so we add a
4239 * random factor that will spread the closing.
4240 */
4241 if (tick_isset(global.close_spread_end)) {
4242 int remaining_window = tick_remain(now_ms, global.close_spread_end);
4243 if (remaining_window) {
4244 /* This should increase the closing rate the
4245 * further along the window we are. */
4246 send_goaway = (remaining_window <= statistical_prng_range(global.close_spread_time));
4247 }
4248 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02004249 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
4250 send_goaway = 0; /* let the client close his connection himself */
Willy Tarreau8ec14062017-12-30 18:08:13 +01004251 /* frontend is stopping, reload likely in progress, let's try
4252 * to announce a graceful shutdown if not yet done. We don't
4253 * care if it fails, it will be tried again later.
4254 */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02004255 if (send_goaway) {
4256 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
4257 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
4258 if (h2c->last_sid < 0)
4259 h2c->last_sid = (1U << 31) - 1;
4260 h2c_send_goaway_error(h2c, NULL);
4261 }
Willy Tarreau8ec14062017-12-30 18:08:13 +01004262 }
4263 }
4264
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004265 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004266 * If we received early data, and the handshake is done, wake
4267 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004268 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004269 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01004270 (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 +01004271 struct eb32_node *node;
4272 struct h2s *h2s;
4273
4274 h2c->flags |= H2_CF_WAIT_FOR_HS;
4275 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
4276
4277 while (node) {
4278 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004279 if (se_fl_test(h2s->sd, SE_FL_WAIT_FOR_HS))
Willy Tarreau7e094452018-12-19 18:08:52 +01004280 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004281 node = eb32_next(node);
4282 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004283 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004284
Christopher Fauletff7925d2022-10-11 19:12:40 +02004285 if ((h2c->flags & H2_CF_ERROR) || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01004286 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
4287 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
4288 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02004289 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004290
4291 if (eb_is_empty(&h2c->streams_by_id)) {
4292 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02004293 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004294 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004295 return -1;
4296 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004297
4298 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004299 if (conn->flags & CO_FL_LIST_MASK) {
4300 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004301 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004302 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4303 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004304 }
4305 else if (h2c->st0 == H2_CS_ERROR) {
4306 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004307 if (conn->flags & CO_FL_LIST_MASK) {
4308 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004309 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004310 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4311 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004312 }
4313
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004314 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01004315 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004316
Christopher Fauletff7925d2022-10-11 19:12:40 +02004317 if (h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Olivier Houchard53216e72018-10-10 15:46:36 +02004318 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02004319 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02004320 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
4321 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02004322 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02004323
Willy Tarreau15a47332022-03-18 15:57:34 +01004324 h2c_update_timeout(h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02004325 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004326 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004327 return 0;
4328}
4329
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004330/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004331static int h2_wake(struct connection *conn)
4332{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004333 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02004334 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004335
Willy Tarreau7838a792019-08-12 18:42:03 +02004336 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4337 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01004338 if (ret >= 0)
4339 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02004340 TRACE_LEAVE(H2_EV_H2C_WAKE);
4341 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004342}
4343
Willy Tarreauea392822017-10-31 10:02:25 +01004344/* Connection timeout management. The principle is that if there's no receipt
4345 * nor sending for a certain amount of time, the connection is closed. If the
4346 * MUX buffer still has lying data or is not allocatable, the connection is
4347 * immediately killed. If it's allocatable and empty, we attempt to send a
4348 * GOAWAY frame.
4349 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004350struct task *h2_timeout_task(struct task *t, void *context, unsigned int state)
Willy Tarreauea392822017-10-31 10:02:25 +01004351{
Olivier Houchard9f6af332018-05-25 14:04:04 +02004352 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01004353 int expired = tick_is_expired(t->expire, now_ms);
4354
Willy Tarreau7838a792019-08-12 18:42:03 +02004355 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
4356
Willy Tarreaubd42e922020-06-30 11:19:23 +02004357 if (h2c) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004358 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004359 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004360
4361 /* Somebody already stole the connection from us, so we should not
4362 * free it, we just have to free the task.
4363 */
4364 if (!t->context) {
4365 h2c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004366 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004367 goto do_leave;
4368 }
4369
4370
Willy Tarreaubd42e922020-06-30 11:19:23 +02004371 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004372 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004373 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
4374 return t;
4375 }
Willy Tarreauea392822017-10-31 10:02:25 +01004376
Willy Tarreaubd42e922020-06-30 11:19:23 +02004377 if (!h2c_may_expire(h2c)) {
4378 /* we do still have streams but all of them are idle, waiting
4379 * for the data layer, so we must not enforce the timeout here.
4380 */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004381 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004382 t->expire = TICK_ETERNITY;
4383 return t;
4384 }
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004385
Willy Tarreaubd42e922020-06-30 11:19:23 +02004386 /* We're about to destroy the connection, so make sure nobody attempts
4387 * to steal it from us.
4388 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004389 if (h2c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004390 conn_delete_from_tree(&h2c->conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004391
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004392 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004393 }
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004394
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004395do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02004396 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02004397
4398 if (!h2c) {
4399 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02004400 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02004401 return NULL;
4402 }
4403
4404 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01004405 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02004406 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01004407
Willy Tarreau662fafc2019-05-26 09:43:07 +02004408 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01004409 /* don't even try to send a GOAWAY, the buffer is stuck */
4410 h2c->flags |= H2_CF_GOAWAY_FAILED;
4411 }
4412
4413 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01004414 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01004415 if (h2c_send_goaway_error(h2c, NULL) <= 0)
4416 h2c->flags |= H2_CF_GOAWAY_FAILED;
4417
Willy Tarreau662fafc2019-05-26 09:43:07 +02004418 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004419 unsigned int released = 0;
4420 struct buffer *buf;
4421
4422 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4423 if (b_data(buf)) {
4424 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
4425 if (!ret)
4426 break;
4427 b_del(buf, ret);
4428 if (b_data(buf))
4429 break;
4430 b_free(buf);
4431 released++;
4432 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02004433 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004434
4435 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004436 offer_buffers(NULL, released);
Willy Tarreau787db9a2018-06-14 18:31:46 +02004437 }
Willy Tarreauea392822017-10-31 10:02:25 +01004438
Willy Tarreau4481e262019-10-31 15:36:30 +01004439 /* in any case this connection must not be considered idle anymore */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004440 if (h2c->conn->flags & CO_FL_LIST_MASK) {
4441 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004442 conn_delete_from_tree(&h2c->conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004443 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4444 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004445
Willy Tarreau0975f112018-03-29 15:22:59 +02004446 /* either we can release everything now or it will be done later once
4447 * the last stream closes.
4448 */
4449 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02004450 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01004451
Willy Tarreau7838a792019-08-12 18:42:03 +02004452 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01004453 return NULL;
4454}
4455
4456
Willy Tarreau62f52692017-10-08 23:01:42 +02004457/*******************************************/
4458/* functions below are used by the streams */
4459/*******************************************/
4460
4461/*
4462 * Attach a new stream to a connection
4463 * (Used for outgoing connections)
4464 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004465static int h2_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02004466{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004467 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004468 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004469
Willy Tarreau7838a792019-08-12 18:42:03 +02004470 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004471 h2s = h2c_bck_stream_new(h2c, sd->sc, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004472 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004473 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01004474 return -1;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004475 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004476
4477 /* the connection is not idle anymore, let's mark this */
4478 HA_ATOMIC_AND(&h2c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004479 xprt_set_used(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004480
Willy Tarreau7838a792019-08-12 18:42:03 +02004481 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Christopher Faulete00ad352021-12-16 14:44:31 +01004482 return 0;
Willy Tarreau62f52692017-10-08 23:01:42 +02004483}
4484
Willy Tarreau4596fe22022-05-17 19:07:51 +02004485/* Retrieves the first valid stream connector from this connection, or returns
4486 * NULL. We have to scan because we may have some orphan streams. It might be
Willy Tarreaufafd3982018-11-18 21:29:20 +01004487 * beneficial to scan backwards from the end to reduce the likeliness to find
4488 * orphans.
4489 */
Willy Tarreaud1373532022-05-27 11:00:59 +02004490static struct stconn *h2_get_first_sc(const struct connection *conn)
Willy Tarreaufafd3982018-11-18 21:29:20 +01004491{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004492 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01004493 struct h2s *h2s;
4494 struct eb32_node *node;
4495
4496 node = eb32_first(&h2c->streams_by_id);
4497 while (node) {
4498 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004499 if (h2s_sc(h2s))
4500 return h2s_sc(h2s);
Willy Tarreaufafd3982018-11-18 21:29:20 +01004501 node = eb32_next(node);
4502 }
4503 return NULL;
4504}
4505
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004506static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4507{
4508 int ret = 0;
4509 struct h2c *h2c = conn->ctx;
4510
4511 switch (mux_ctl) {
4512 case MUX_STATUS:
4513 /* Only consider the mux to be ready if we're done with
4514 * the preface and settings, and we had no error.
4515 */
4516 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
4517 ret |= MUX_STATUS_READY;
4518 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02004519 case MUX_EXIT_STATUS:
4520 return MUX_ES_UNKNOWN;
Willy Tarreauc04a8ee2024-01-17 17:21:29 +01004521 case MUX_CTL_GET_GLITCHES:
4522 return h2c->glitches;
4523
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004524 default:
4525 return -1;
4526 }
4527}
4528
Willy Tarreau62f52692017-10-08 23:01:42 +02004529/*
Olivier Houchard060ed432018-11-06 16:32:42 +01004530 * Destroy the mux and the associated connection, if it is no longer used
4531 */
Christopher Faulet73c12072019-04-08 11:23:22 +02004532static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01004533{
Christopher Faulet73c12072019-04-08 11:23:22 +02004534 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01004535
Willy Tarreau7838a792019-08-12 18:42:03 +02004536 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02004537 if (eb_is_empty(&h2c->streams_by_id)) {
4538 BUG_ON(h2c->conn->ctx != h2c);
Christopher Faulet73c12072019-04-08 11:23:22 +02004539 h2_release(h2c);
Christopher Faulet4e610962022-04-14 11:23:50 +02004540 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004541 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01004542}
4543
4544/*
Willy Tarreau62f52692017-10-08 23:01:42 +02004545 * Detach the stream from the connection and possibly release the connection.
4546 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004547static void h2_detach(struct sedesc *sd)
Willy Tarreau62f52692017-10-08 23:01:42 +02004548{
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004549 struct h2s *h2s = sd->se;
Willy Tarreau60935142017-10-16 18:11:19 +02004550 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01004551 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004552
Willy Tarreau7838a792019-08-12 18:42:03 +02004553 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
4554
Willy Tarreau7838a792019-08-12 18:42:03 +02004555 if (!h2s) {
4556 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02004557 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004558 }
Willy Tarreau60935142017-10-16 18:11:19 +02004559
Willy Tarreaud9464162020-01-10 18:25:07 +01004560 /* there's no txbuf so we're certain not to be able to send anything */
4561 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02004562
Olivier Houchardf502aca2018-12-14 19:42:40 +01004563 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004564 h2c = h2s->h2c;
Willy Tarreau36c22322022-05-27 10:41:24 +02004565 h2c->nb_sc--;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004566 if (!h2c->nb_sc && !br_data(h2c->mbuf))
Willy Tarreau15a47332022-03-18 15:57:34 +01004567 h2c->idle_start = now_ms;
4568
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004569 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
Willy Tarreau36c22322022-05-27 10:41:24 +02004570 !h2_frt_has_too_many_sc(h2c)) {
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004571 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02004572 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004573 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02004574 }
Willy Tarreau60935142017-10-16 18:11:19 +02004575
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004576 /* this stream may be blocked waiting for some data to leave (possibly
4577 * an ES or RST frame), so orphan it in this case.
4578 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02004579 if (!(h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02004580 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01004581 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01004582 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004583 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau15a47332022-03-18 15:57:34 +01004584 /* refresh the timeout if none was active, so that the last
4585 * leaving stream may arm it.
4586 */
Willy Tarreau3fb2c6d2023-03-16 18:06:19 +01004587 if (h2c->task && !tick_isset(h2c->task->expire))
Willy Tarreau15a47332022-03-18 15:57:34 +01004588 h2c_update_timeout(h2c);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004589 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004590 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004591
Christopher Faulet68ee7842022-10-12 10:21:33 +02004592 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi)) {
Willy Tarreau45f752e2017-10-30 15:44:59 +01004593 /* unblock the connection if it was blocked on this
4594 * stream.
4595 */
4596 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
4597 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004598 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01004599 }
4600
Willy Tarreau71049cc2018-03-28 13:56:39 +02004601 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02004602
Christopher Faulet9b79a102019-07-15 11:22:56 +02004603 if (h2c->flags & H2_CF_IS_BACK) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02004604 if (!(h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERR_PENDING|H2_CF_ERROR))) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004605 if (h2c->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02004606 /* Add the connection in the session server list, if not already done */
4607 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
4608 h2c->conn->owner = NULL;
4609 if (eb_is_empty(&h2c->streams_by_id)) {
4610 h2c->conn->mux->destroy(h2c);
4611 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4612 return;
Christopher Fauletc5579d12020-07-01 15:45:41 +02004613 }
4614 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02004615 if (eb_is_empty(&h2c->streams_by_id)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004616 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
4617 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
4618 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houchard351411f2018-12-27 17:20:54 +01004619 return;
4620 }
4621 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004622 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004623 else {
4624 if (eb_is_empty(&h2c->streams_by_id)) {
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004625 /* If the connection is owned by the session, first remove it
4626 * from its list
4627 */
4628 if (h2c->conn->owner) {
4629 session_unown_conn(h2c->conn->owner, h2c->conn);
4630 h2c->conn->owner = NULL;
4631 }
4632
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004633 /* mark that the tasklet may lose its context to another thread and
4634 * that the handler needs to check it under the idle conns lock.
4635 */
4636 HA_ATOMIC_OR(&h2c->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004637 xprt_set_idle(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
4638
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004639 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01004640 /* The server doesn't want it, let's kill the connection right away */
4641 h2c->conn->mux->destroy(h2c);
4642 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4643 return;
4644 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01004645 /* At this point, the connection has been added to the
4646 * server idle list, so another thread may already have
4647 * hijacked it, so we can't do anything with it.
4648 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01004649 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
4650 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01004651
Olivier Houchard8a786902018-12-15 16:05:40 +01004652 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004653 else if (!h2c->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004654 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) &&
Amaury Denoyelle926a5612024-03-14 11:24:10 +01004655 !LIST_INLIST(&h2c->conn->sess_el)) {
Willy Tarreau85223482022-09-29 20:32:43 +02004656 eb64_insert(&__objt_server(h2c->conn->target)->per_thr[tid].avail_conns,
4657 &h2c->conn->hash_node->node);
Christopher Fauletc5579d12020-07-01 15:45:41 +02004658 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004659 }
4660 }
4661 }
4662
Willy Tarreaue323f342018-03-28 13:51:45 +02004663 /* We don't want to close right now unless we're removing the
4664 * last stream, and either the connection is in error, or it
4665 * reached the ID already specified in a GOAWAY frame received
4666 * or sent (as seen by last_sid >= 0).
4667 */
Olivier Houchard7a977432019-03-21 15:47:13 +01004668 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02004669 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02004670 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02004671 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02004672 }
4673 else if (h2c->task) {
Willy Tarreau15a47332022-03-18 15:57:34 +01004674 h2c_update_timeout(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004675 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02004676 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004677 else
4678 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004679}
4680
Willy Tarreau88bdba32019-05-13 18:17:53 +02004681/* Performs a synchronous or asynchronous shutr(). */
4682static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004683{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004684 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004685
Willy Tarreauf983d002019-05-14 10:40:21 +02004686 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004687 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004688
Willy Tarreau7838a792019-08-12 18:42:03 +02004689 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4690
Christopher Faulet24b85802023-10-16 19:30:02 +02004691 if (h2s->flags & H2_SF_WANT_SHUTW)
4692 goto add_to_list;
4693
Willy Tarreau18059042019-01-31 19:12:48 +01004694 /* a connstream may require us to immediately kill the whole connection
4695 * for example because of a "tcp-request content reject" rule that is
4696 * normally used to limit abuse. In this case we schedule a goaway to
4697 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004698 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004699 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004700 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004701 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004702 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4703 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4704 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004705 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4706 /* Nothing was never sent for this stream, so reset with
4707 * REFUSED_STREAM error to let the client retry the
4708 * request.
4709 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004710 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004711 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4712 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004713 else {
4714 /* a final response was already provided, we don't want this
4715 * stream anymore. This may happen when the server responds
4716 * before the end of an upload and closes quickly (redirect,
4717 * deny, ...)
4718 */
4719 h2s_error(h2s, H2_ERR_CANCEL);
4720 }
Willy Tarreau18059042019-01-31 19:12:48 +01004721
Willy Tarreau90c32322017-11-24 08:00:30 +01004722 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004723 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004724 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004725
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004726 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004727 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004728 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004729 done:
4730 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004731 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004732 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004733add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004734 /* Let the handler know we want to shutr, and add ourselves to the
4735 * most relevant list if not yet done. h2_deferred_shut() will be
4736 * automatically called via the shut_tl tasklet when there's room
4737 * again.
4738 */
4739 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau2b718102021-04-21 07:32:39 +02004740 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004741 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004742 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004743 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004744 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004745 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004746 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004747 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004748}
4749
Willy Tarreau88bdba32019-05-13 18:17:53 +02004750/* Performs a synchronous or asynchronous shutw(). */
4751static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004752{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004753 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004754
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004755 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004756 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004757
Willy Tarreau7838a792019-08-12 18:42:03 +02004758 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4759
Willy Tarreau473e0e52022-08-18 16:12:15 +02004760 if (h2s->st != H2_SS_ERROR &&
4761 (h2s->flags & (H2_SF_HEADERS_SENT | H2_SF_MORE_HTX_DATA)) == H2_SF_HEADERS_SENT) {
4762 /* we can cleanly close using an empty data frame only after headers
4763 * and if no more data is expected to be sent.
4764 */
Willy Tarreau58e32082017-11-07 14:41:09 +01004765 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4766 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004767 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004768
4769 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004770 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004771 else
4772 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004773 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004774 /* a connstream may require us to immediately kill the whole connection
4775 * for example because of a "tcp-request content reject" rule that is
4776 * normally used to limit abuse. In this case we schedule a goaway to
4777 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004778 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004779 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004780 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004781 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004782 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4783 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4784 }
Willy Tarreau473e0e52022-08-18 16:12:15 +02004785 else if (h2s->flags & H2_SF_MORE_HTX_DATA) {
4786 /* some unsent data were pending (e.g. abort during an upload),
4787 * let's send a CANCEL.
4788 */
4789 TRACE_STATE("shutw before end of data, sending CANCEL", H2_EV_STRM_SHUT, h2c->conn, h2s);
4790 h2s_error(h2s, H2_ERR_CANCEL);
4791 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004792 else {
4793 /* Nothing was never sent for this stream, so reset with
4794 * REFUSED_STREAM error to let the client retry the
4795 * request.
4796 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004797 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004798 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4799 }
Willy Tarreau18059042019-01-31 19:12:48 +01004800
Willy Tarreau90c32322017-11-24 08:00:30 +01004801 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004802 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004803 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004804
Willy Tarreau00dd0782018-03-01 16:31:34 +01004805 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004806 }
4807
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004808 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004809 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004810
4811 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4812
Willy Tarreau88bdba32019-05-13 18:17:53 +02004813 done:
4814 h2s->flags &= ~H2_SF_WANT_SHUTW;
4815 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004816
4817 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004818 /* Let the handler know we want to shutw, and add ourselves to the
4819 * most relevant list if not yet done. h2_deferred_shut() will be
4820 * automatically called via the shut_tl tasklet when there's room
4821 * again.
4822 */
4823 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau2b718102021-04-21 07:32:39 +02004824 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004825 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004826 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004827 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004828 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004829 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004830 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004831 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004832}
4833
Willy Tarreau5723f292020-01-10 15:16:57 +01004834/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004835 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4836 * and prevented the last frame from being emitted.
4837 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004838struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004839{
4840 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004841 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004842
Willy Tarreau7838a792019-08-12 18:42:03 +02004843 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4844
Willy Tarreau5723f292020-01-10 15:16:57 +01004845 if (h2s->flags & H2_SF_NOTIFIED) {
4846 /* some data processing remains to be done first */
4847 goto end;
4848 }
4849
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004850 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004851 h2_do_shutw(h2s);
4852
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004853 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004854 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004855
Willy Tarreau88bdba32019-05-13 18:17:53 +02004856 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004857 /* We're done trying to send, remove ourself from the send_list */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004858 h2_remove_from_list(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01004859
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004860 if (!h2s_sc(h2s)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004861 h2s_destroy(h2s);
Willy Tarreau74163142021-03-13 11:30:19 +01004862 if (h2c_is_dead(h2c)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004863 h2_release(h2c);
Willy Tarreau74163142021-03-13 11:30:19 +01004864 t = NULL;
4865 }
Willy Tarreau88bdba32019-05-13 18:17:53 +02004866 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004867 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004868 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004869 TRACE_LEAVE(H2_EV_STRM_SHUT);
Willy Tarreau74163142021-03-13 11:30:19 +01004870 return t;
Willy Tarreau62f52692017-10-08 23:01:42 +02004871}
4872
Willy Tarreau4596fe22022-05-17 19:07:51 +02004873/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004874static void h2_shutr(struct stconn *sc, enum co_shr_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004875{
Willy Tarreau36c22322022-05-27 10:41:24 +02004876 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004877
Willy Tarreau7838a792019-08-12 18:42:03 +02004878 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004879 if (mode)
4880 h2_do_shutr(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004881 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004882}
4883
Willy Tarreau4596fe22022-05-17 19:07:51 +02004884/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004885static void h2_shutw(struct stconn *sc, enum co_shw_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004886{
Willy Tarreau36c22322022-05-27 10:41:24 +02004887 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004888
Willy Tarreau7838a792019-08-12 18:42:03 +02004889 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004890 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004891 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004892}
4893
Christopher Faulet9b79a102019-07-15 11:22:56 +02004894/* Decode the payload of a HEADERS frame and produce the HTX request or response
4895 * depending on the connection's side. Returns a positive value on success, a
4896 * negative value on failure, or 0 if it couldn't proceed. May report connection
4897 * errors in h2c->errcode if the frame is non-decodable and the connection
4898 * unrecoverable. In absence of connection error when a failure is reported, the
4899 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004900 *
4901 * The function may fold CONTINUATION frames into the initial HEADERS frame
4902 * by removing padding and next frame header, then moving the CONTINUATION
4903 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4904 * leaving a hole between the main frame and the beginning of the next one.
4905 * The possibly remaining incomplete or next frame at the end may be moved
4906 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4907 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4908 *
4909 * A buffer at the beginning of processing may look like this :
4910 *
4911 * ,---.---------.-----.--------------.--------------.------.---.
4912 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4913 * `---^---------^-----^--------------^--------------^------^---'
4914 * | | <-----> | |
4915 * area | dpl | wrap
4916 * |<--------------> |
4917 * | dfl |
4918 * |<-------------------------------------------------->|
4919 * head data
4920 *
4921 * Padding is automatically overwritten when folding, participating to the
4922 * hole size after dfl :
4923 *
4924 * ,---.------------------------.-----.--------------.------.---.
4925 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4926 * `---^------------------------^-----^--------------^------^---'
4927 * | | <-----> | |
4928 * area | hole | wrap
4929 * |<-----------------------> |
4930 * | dfl |
4931 * |<-------------------------------------------------->|
4932 * head data
4933 *
4934 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4935 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4936 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004937 *
4938 * The <flags> field must point to either the stream's flags or to a copy of it
4939 * so that the function can update the following flags :
4940 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004941 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004942 *
4943 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4944 * decoding, in order to detect if we're dealing with a headers or a trailers
Willy Tarreau22892322024-01-17 16:57:23 +01004945 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). The
4946 * function takes care of counting glitches.
Willy Tarreau13278b42017-10-13 19:23:14 +02004947 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01004948static 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 +02004949{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004950 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004951 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004952 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004953 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004954 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004955 struct htx *htx = NULL;
Willy Tarreaud43a58a2024-01-19 18:20:21 +01004956 int flen = 0; // header frame len
4957 int fragments = 0;
Willy Tarreauea18f862018-12-22 20:19:26 +01004958 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004959 int ret = 0;
4960 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004961 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004962
Willy Tarreau7838a792019-08-12 18:42:03 +02004963 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4964
Willy Tarreauea18f862018-12-22 20:19:26 +01004965next_frame:
4966 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4967 goto leave; // incomplete input frame
4968
4969 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4970 * this case, we'll try to paste it immediately after the initial
4971 * HEADERS frame payload and kill any possible padding. The initial
4972 * frame's length will be increased to represent the concatenation
4973 * of the two frames. The next frame is read from position <tlen>
4974 * and written at position <flen> (minus padding if some is present).
4975 */
4976 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4977 struct h2_fh hdr;
4978 int clen; // CONTINUATION frame's payload length
4979
Willy Tarreau7838a792019-08-12 18:42:03 +02004980 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 +01004981 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4982 /* no more data, the buffer may be full, either due to
4983 * too large a frame or because of too large a hole that
4984 * we're going to compact at the end.
4985 */
4986 goto leave;
4987 }
4988
4989 if (hdr.ft != H2_FT_CONTINUATION) {
4990 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreauf0279f62024-03-11 07:35:19 +01004991 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02004992 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 +01004993 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004994 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004995 goto fail;
4996 }
4997
4998 if (hdr.sid != h2c->dsi) {
4999 /* RFC7540#6.10: frame of different stream */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005000 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005001 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 +01005002 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005003 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01005004 goto fail;
5005 }
5006
5007 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
5008 /* RFC7540#4.2: invalid frame length */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005009 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005010 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 +01005011 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
5012 goto fail;
5013 }
5014
5015 /* detect when we must stop aggragating frames */
5016 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
5017
5018 /* Take as much as we can of the CONTINUATION frame's payload */
5019 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
5020 if (clen > hdr.len)
5021 clen = hdr.len;
5022
5023 /* Move the frame's payload over the padding, hole and frame
5024 * header. At least one of hole or dpl is null (see diagrams
5025 * above). The hole moves after the new aggragated frame.
5026 */
5027 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 +02005028 h2c->dfl += hdr.len - h2c->dpl;
Willy Tarreauea18f862018-12-22 20:19:26 +01005029 hole += h2c->dpl + 9;
5030 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005031 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 Tarreaud43a58a2024-01-19 18:20:21 +01005032 fragments++;
Willy Tarreauea18f862018-12-22 20:19:26 +01005033 goto next_frame;
5034 }
5035
5036 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01005037
Willy Tarreau13278b42017-10-13 19:23:14 +02005038 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005039 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02005040 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02005041 copy = alloc_trash_chunk();
5042 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005043 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 +02005044 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
5045 goto fail;
5046 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005047 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
5048 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
5049 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02005050 }
5051
Willy Tarreau13278b42017-10-13 19:23:14 +02005052 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
5053 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005054 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01005055 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005056 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005057 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 +01005058 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005059 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02005060 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01005061 }
5062
Willy Tarreaua01f45e2018-12-31 07:41:24 +01005063 if (flen < 5) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01005064 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005065 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 +01005066 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
5067 goto fail;
5068 }
5069
Willy Tarreau13278b42017-10-13 19:23:14 +02005070 hdrs += 5; // stream dep = 4, weight = 1
5071 flen -= 5;
5072 }
5073
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005074 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005075 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 +01005076 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01005077 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005078 }
Willy Tarreau13278b42017-10-13 19:23:14 +02005079
Willy Tarreau937f7602018-02-26 15:22:17 +01005080 /* we can't retry a failed decompression operation so we must be very
5081 * careful not to take any risks. In practice the output buffer is
5082 * always empty except maybe for trailers, in which case we simply have
5083 * to wait for the upper layer to finish consuming what is available.
5084 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005085 htx = htx_from_buf(rxbuf);
5086 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005087 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 +02005088 h2c->flags |= H2_CF_DEM_SFULL;
5089 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01005090 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005091
Willy Tarreau25919232019-01-03 14:48:18 +01005092 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005093 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
5094 sizeof(list)/sizeof(list[0]), tmp);
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005095
5096 if (outlen > 0 &&
5097 (TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
5098 TRACE_ENABLED(TRACE_LEVEL_USER, H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, 0, 0, 0)) {
5099 struct ist n;
5100 int i;
5101
5102 for (i = 0; list[i].n.len; i++) {
5103 n = list[i].n;
5104
5105 if (!isttest(n)) {
5106 /* this is in fact a pseudo header whose number is in n.len */
5107 n = h2_phdr_to_ist(n.len);
5108 }
5109
5110 h2_trace_header(n, list[i].v, H2_EV_RX_FRAME|H2_EV_RX_HDR,
5111 ist(TRC_LOC), __FUNCTION__, h2c, NULL);
5112 }
5113 }
5114
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005115 if (outlen < 0) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01005116 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005117 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 +01005118 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
5119 goto fail;
5120 }
5121
Willy Tarreau25919232019-01-03 14:48:18 +01005122 /* The PACK decompressor was updated, let's update the input buffer and
5123 * the parser's state to commit these changes and allow us to later
5124 * fail solely on the stream if needed.
5125 */
5126 b_del(&h2c->dbuf, h2c->dfl + hole);
5127 h2c->dfl = hole = 0;
5128 h2c->st0 = H2_CS_FRAME_H;
5129
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005130 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01005131 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005132 msgf |= (*flags & H2_SF_BODY_TUNNEL) ? H2_MSGF_BODY_TUNNEL: 0;
Amaury Denoyelle74162742020-12-11 17:53:05 +01005133 /* If an Extended CONNECT has been sent on this stream, set message flag
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005134 * to convert 200 response to 101 htx response */
Amaury Denoyelle74162742020-12-11 17:53:05 +01005135 msgf |= (*flags & H2_SF_EXT_CONNECT_SENT) ? H2_MSGF_EXT_CONNECT: 0;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01005136
Willy Tarreau88d138e2019-01-02 19:38:14 +01005137 if (*flags & H2_SF_HEADERS_RCVD)
5138 goto trailers;
5139
5140 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005141 if (h2c->flags & H2_CF_IS_BACK)
Amaury Denoyelle74162742020-12-11 17:53:05 +01005142 outlen = h2_make_htx_response(list, htx, &msgf, body_len, upgrade_protocol);
Christopher Faulet9b79a102019-07-15 11:22:56 +02005143 else
Willy Tarreaub6be1a42023-08-08 15:38:28 +02005144 outlen = h2_make_htx_request(list, htx, &msgf, body_len,
5145 !!(((const struct session *)h2c->conn->owner)->fe->options2 & PR_O2_REQBUG_OK));
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005146
Christopher Faulet3d875582021-04-26 17:46:13 +02005147 if (outlen < 0 || htx_free_space(htx) < global.tune.maxrewrite) {
Willy Tarreau25919232019-01-03 14:48:18 +01005148 /* too large headers? this is a stream error only */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005149 h2c_report_glitch(h2c, 1);
Willy Tarreau577fc272023-08-08 15:27:02 +02005150 TRACE_STATE("message headers too large or invalid", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR|H2_EV_PROTO_ERR, h2c->conn);
Christopher Faulet3d875582021-04-26 17:46:13 +02005151 htx->flags |= HTX_FL_PARSING_ERROR;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005152 goto fail;
5153 }
Willy Tarreau13278b42017-10-13 19:23:14 +02005154
Willy Tarreau174b06a2018-04-25 18:13:58 +02005155 if (msgf & H2_MSGF_BODY) {
5156 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005157 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005158 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005159 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005160 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02005161 }
Christopher Faulet7d247f02020-12-02 14:26:36 +01005162 if (msgf & H2_MSGF_BODYLESS_RSP)
5163 *flags |= H2_SF_BODYLESS_RESP;
Willy Tarreau174b06a2018-04-25 18:13:58 +02005164
Christopher Fauletd0db4232021-01-22 11:46:30 +01005165 if (msgf & H2_MSGF_BODY_TUNNEL)
5166 *flags |= H2_SF_BODY_TUNNEL;
5167 else {
5168 /* Abort the tunnel attempt, if any */
5169 if (*flags & H2_SF_BODY_TUNNEL)
5170 *flags |= H2_SF_TUNNEL_ABRT;
5171 *flags &= ~H2_SF_BODY_TUNNEL;
5172 }
5173
Willy Tarreau88d138e2019-01-02 19:38:14 +01005174 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01005175 /* indicate that a HEADERS frame was received for this stream, except
5176 * for 1xx responses. For 1xx responses, another HEADERS frame is
5177 * expected.
5178 */
5179 if (!(msgf & H2_MSGF_RSP_1XX))
5180 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01005181
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005182 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Christopher Faulet827a6292022-12-22 09:47:01 +01005183 if (msgf & H2_MSGF_RSP_1XX) {
5184 /* RFC9113#8.1 : HEADERS frame with the ES flag set that carries an informational status code is malformed */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005185 h2c_report_glitch(h2c, 1);
Christopher Faulet827a6292022-12-22 09:47:01 +01005186 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);
5187 goto fail;
5188 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005189 /* no more data are expected for this message */
5190 htx->flags |= HTX_FL_EOM;
Christopher Faulet1a60a662023-05-24 11:02:50 +02005191 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005192 }
Willy Tarreau937f7602018-02-26 15:22:17 +01005193
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005194 if (msgf & H2_MSGF_EXT_CONNECT)
5195 *flags |= H2_SF_EXT_CONNECT_RCVD;
5196
Willy Tarreau86277d42019-01-02 15:36:11 +01005197 /* success */
5198 ret = 1;
5199
Willy Tarreau68dd9852017-07-03 14:44:26 +02005200 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01005201 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01005202 * move the remaining data over it.
5203 */
5204 if (hole) {
5205 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
5206 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
5207 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
5208 b_sub(&h2c->dbuf, hole);
5209 }
5210
Christopher Faulete3ec2172023-12-13 15:36:52 +01005211 if (b_full(&h2c->dbuf) && h2c->dfl && (!htx || htx_is_empty(htx))) {
Willy Tarreauea18f862018-12-22 20:19:26 +01005212 /* too large frames */
5213 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01005214 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01005215 }
5216
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005217 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005218 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02005219 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02005220 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreaud43a58a2024-01-19 18:20:21 +01005221
5222 /* Check for abuse of CONTINUATION: more than 4 fragments and less than
5223 * 1kB per fragment is clearly unusual and suspicious enough to count
5224 * one glitch per 1kB fragment in a 16kB buffer, which means that an
5225 * abuser sending 1600 1-byte frames in a 16kB buffer would increment
5226 * its counter by 100.
5227 */
Willy Tarreau92f287b2024-03-11 07:33:44 +01005228 if (unlikely(fragments > 4) && fragments > flen / 1024 && ret != 0) {
5229 if (h2c_report_glitch(h2c, (fragments + 15) / 16)) {
5230 TRACE_STATE("glitch limit reached on CONTINUATION frame", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
5231 ret = -1;
5232 }
5233 }
Willy Tarreaud43a58a2024-01-19 18:20:21 +01005234
Willy Tarreau86277d42019-01-02 15:36:11 +01005235 return ret;
5236
Willy Tarreau68dd9852017-07-03 14:44:26 +02005237 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01005238 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02005239 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005240
5241 trailers:
5242 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01005243 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
5244 /* It's a trailer but it's missing ES flag */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005245 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005246 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 +01005247 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005248 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau88d138e2019-01-02 19:38:14 +01005249 goto fail;
5250 }
5251
Christopher Faulet9b79a102019-07-15 11:22:56 +02005252 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02005253 if (h2_make_htx_trailers(list, htx) <= 0) {
5254 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 +02005255 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005256 }
Christopher Faulet1a60a662023-05-24 11:02:50 +02005257 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005258 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02005259}
5260
Christopher Faulet9b79a102019-07-15 11:22:56 +02005261/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005262 * parser state is automatically updated. Returns > 0 if it could completely
5263 * send the current frame, 0 if it couldn't complete, in which case
Willy Tarreaub605c422022-05-17 17:04:55 +02005264 * SE_FL_RCV_MORE must be checked to know if some data remain pending (an empty
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005265 * DATA frame can return 0 as a valid result). Stream errors are reported in
5266 * h2s->errcode and connection errors in h2c->errcode. The caller must already
5267 * have checked the frame header and ensured that the frame was complete or the
5268 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02005269 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01005270static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02005271{
5272 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005273 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005274 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005275 struct htx *htx = NULL;
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005276 struct buffer *scbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005277 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02005278
Willy Tarreau7838a792019-08-12 18:42:03 +02005279 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5280
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005281 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02005282
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005283 scbuf = h2_get_buf(h2c, &h2s->rxbuf);
5284 if (!scbuf) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005285 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02005286 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 +01005287 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005288 }
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005289 htx = htx_from_buf(scbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01005290
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005291try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005292 flen = h2c->dfl - h2c->dpl;
5293 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01005294 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005295
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005296 if (flen > b_data(&h2c->dbuf)) {
5297 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005298 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01005299 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005300 }
5301
Christopher Faulet9b79a102019-07-15 11:22:56 +02005302 block = htx_free_data_space(htx);
5303 if (!block) {
5304 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005305 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 +02005306 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005307 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005308 if (flen > block)
5309 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005310
Christopher Faulet9b79a102019-07-15 11:22:56 +02005311 /* here, flen is the max we can copy into the output buffer */
5312 block = b_contig_data(&h2c->dbuf, 0);
5313 if (flen > block)
5314 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005315
Christopher Faulet9b79a102019-07-15 11:22:56 +02005316 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau022e5e52020-09-10 09:33:15 +02005317 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 +02005318
Christopher Faulet9b79a102019-07-15 11:22:56 +02005319 b_del(&h2c->dbuf, sent);
5320 h2c->dfl -= sent;
5321 h2c->rcvd_c += sent;
5322 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02005323
Christopher Faulet9b79a102019-07-15 11:22:56 +02005324 if (h2s->flags & H2_SF_DATA_CLEN) {
5325 h2s->body_len -= sent;
5326 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005327 }
5328
Christopher Faulet9b79a102019-07-15 11:22:56 +02005329 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005330 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005331 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 +01005332 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005333 }
5334
Christopher Faulet9b79a102019-07-15 11:22:56 +02005335 goto try_again;
5336
Willy Tarreau4a28da12018-01-04 14:41:00 +01005337 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005338 /* here we're done with the frame, all the payload (except padding) was
5339 * transferred.
5340 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02005341
Christopher Faulet5be651d2021-01-22 15:28:03 +01005342 if (!(h2s->flags & H2_SF_BODY_TUNNEL) && (h2c->dff & H2_F_DATA_END_STREAM)) {
5343 /* no more data are expected for this message. This add the EOM
5344 * flag but only on the response path or if no tunnel attempt
5345 * was aborted. Otherwise (request path + tunnel abrted), the
5346 * EOM was already reported.
5347 */
Christopher Faulet33724322021-02-10 09:04:59 +01005348 if ((h2c->flags & H2_CF_IS_BACK) || !(h2s->flags & H2_SF_TUNNEL_ABRT)) {
Amaury Denoyelle25cf19d2023-05-11 16:50:04 +02005349 /* htx may be empty if receiving an empty DATA frame. */
5350 if (!htx_set_eom(htx))
5351 goto fail;
Christopher Faulet33724322021-02-10 09:04:59 +01005352 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02005353 }
5354
Willy Tarreaud1023bb2018-03-22 16:53:12 +01005355 h2c->rcvd_c += h2c->dpl;
5356 h2c->rcvd_s += h2c->dpl;
5357 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005358 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005359 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005360 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005361 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01005362 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005363 if (htx)
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005364 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005365 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01005366 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005367}
5368
Willy Tarreau115e83b2018-12-01 19:17:53 +01005369/* Try to send a HEADERS frame matching HTX response present in HTX message
5370 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5371 * must check the stream's status to detect any error which might have happened
5372 * subsequently to a successful send. The htx blocks are automatically removed
5373 * from the message. The htx message is assumed to be valid since produced from
5374 * the internal code, hence it contains a start line, an optional series of
5375 * header blocks and an end of header, otherwise an invalid frame could be
5376 * emitted and the resulting htx message could be left in an inconsistent state.
5377 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005378static size_t h2s_snd_fhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005379{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005380 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01005381 struct h2c *h2c = h2s->h2c;
5382 struct htx_blk *blk;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005383 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005384 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005385 struct htx_sl *sl;
5386 enum htx_blk_type type;
5387 int es_now = 0;
5388 int ret = 0;
5389 int hdr;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005390
Willy Tarreau7838a792019-08-12 18:42:03 +02005391 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5392
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005393 /* get the start line (we do have one) and the rest of the headers,
5394 * that we dump starting at header 0 */
5395 sl = NULL;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005396 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005397 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005398 type = htx_get_blk_type(blk);
5399
5400 if (type == HTX_BLK_UNUSED)
5401 continue;
5402
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005403 if (type == HTX_BLK_EOH)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005404 break;
5405
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005406 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005407 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005408 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5409 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5410 goto fail;
5411 }
5412
5413 list[hdr].n = htx_get_blk_name(htx, blk);
5414 list[hdr].v = htx_get_blk_value(htx, blk);
5415 hdr++;
5416 }
5417 else if (type == HTX_BLK_RES_SL) {
Christopher Faulet56498132021-01-29 11:39:43 +01005418 BUG_ON(sl); /* Only one start-line expected */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005419 sl = htx_get_blk_ptr(htx, blk);
5420 h2s->status = sl->info.res.status;
Christopher Faulet7d247f02020-12-02 14:26:36 +01005421 if (h2s->status == 204 || h2s->status == 304)
5422 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005423 if (h2s->status < 100 || h2s->status > 999) {
5424 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5425 goto fail;
5426 }
5427 else if (h2s->status == 101) {
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005428 if (unlikely(h2s->flags & H2_SF_EXT_CONNECT_RCVD)) {
5429 /* If an Extended CONNECT has been received, we need to convert 101 to 200 */
5430 h2s->status = 200;
5431 h2s->flags &= ~H2_SF_EXT_CONNECT_RCVD;
5432 }
5433 else {
5434 /* Otherwise, 101 responses are not supported in H2, so return a error (RFC7540#8.1.1) */
5435 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5436 goto fail;
5437 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005438 }
5439 else if ((h2s->flags & H2_SF_BODY_TUNNEL) && h2s->status >= 300) {
5440 /* Abort the tunnel attempt */
5441 h2s->flags &= ~H2_SF_BODY_TUNNEL;
5442 h2s->flags |= H2_SF_TUNNEL_ABRT;
5443 }
5444 }
5445 else {
5446 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 +01005447 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005448 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005449 }
5450
Christopher Faulet56498132021-01-29 11:39:43 +01005451 /* The start-line me be defined */
5452 BUG_ON(!sl);
5453
Willy Tarreau115e83b2018-12-01 19:17:53 +01005454 /* marker for end of headers */
5455 list[hdr].n = ist("");
5456
Willy Tarreau9c218e72019-05-26 10:08:28 +02005457 mbuf = br_tail(h2c->mbuf);
5458 retry:
5459 if (!h2_get_buf(h2c, mbuf)) {
5460 h2c->flags |= H2_CF_MUX_MALLOC;
5461 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005462 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 +02005463 return 0;
5464 }
5465
Willy Tarreau115e83b2018-12-01 19:17:53 +01005466 chunk_reset(&outbuf);
5467
5468 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005469 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5470 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005471 break;
5472 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005473 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01005474 }
5475
5476 if (outbuf.size < 9)
5477 goto full;
5478
5479 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5480 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5481 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5482 outbuf.data = 9;
5483
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005484 if ((h2c->flags & (H2_CF_SHTS_UPDATED|H2_CF_DTSU_EMITTED)) == H2_CF_SHTS_UPDATED) {
5485 /* SETTINGS_HEADER_TABLE_SIZE changed, we must send an HPACK
5486 * dynamic table size update so that some clients are not
5487 * confused. In practice we only need to send the DTSU when the
5488 * advertised size is lower than the current one, and since we
5489 * don't use it and don't care about the default 4096 bytes,
5490 * we only ack it with a zero size thus we at most have to deal
5491 * with this once. See RFC7541#4.2 and #6.3 for the spec, and
5492 * below for the whole context and interoperability risks:
5493 * https://lists.w3.org/Archives/Public/ietf-http-wg/2021OctDec/0235.html
5494 */
5495 if (b_room(&outbuf) < 1)
5496 goto full;
5497 outbuf.area[outbuf.data++] = 0x20; // HPACK DTSU 0 bytes
5498
5499 /* let's not update the flags now but only once the buffer is
5500 * really committed.
5501 */
5502 }
5503
Willy Tarreau115e83b2018-12-01 19:17:53 +01005504 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01005505 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005506 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005507 goto realign_again;
5508 goto full;
5509 }
5510
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005511 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED) {
5512 char sts[4];
5513
5514 h2_trace_header(ist(":status"), ist(ultoa_r(h2s->status, sts, sizeof(sts))),
5515 H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__,
5516 h2c, h2s);
5517 }
5518
Willy Tarreau115e83b2018-12-01 19:17:53 +01005519 /* encode all headers, stop at empty name */
5520 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
5521 /* these ones do not exist in H2 and must be dropped. */
5522 if (isteq(list[hdr].n, ist("connection")) ||
5523 isteq(list[hdr].n, ist("proxy-connection")) ||
5524 isteq(list[hdr].n, ist("keep-alive")) ||
5525 isteq(list[hdr].n, ist("upgrade")) ||
5526 isteq(list[hdr].n, ist("transfer-encoding")))
5527 continue;
5528
Christopher Faulet86d144c2019-08-14 16:32:25 +02005529 /* Skip all pseudo-headers */
5530 if (*(list[hdr].n.ptr) == ':')
5531 continue;
5532
Willy Tarreau115e83b2018-12-01 19:17:53 +01005533 if (isteq(list[hdr].n, ist("")))
5534 break; // end
5535
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005536 if (!h2_encode_header(&outbuf, list[hdr].n, list[hdr].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5537 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005538 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005539 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005540 goto realign_again;
5541 goto full;
5542 }
5543 }
5544
Willy Tarreaucb985a42019-10-07 16:56:34 +02005545 /* update the frame's size */
5546 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5547
5548 if (outbuf.data > h2c->mfs + 9) {
5549 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5550 /* output full */
5551 if (b_space_wraps(mbuf))
5552 goto realign_again;
5553 goto full;
5554 }
5555 }
5556
Willy Tarreau3a537072021-06-17 08:40:04 +02005557 TRACE_USER("sent H2 response ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5558
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005559 /* remove all header blocks including the EOH and compute the
5560 * corresponding size.
Willy Tarreau115e83b2018-12-01 19:17:53 +01005561 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005562 ret = 0;
5563 blk = htx_get_head_blk(htx);
5564 while (blk) {
5565 type = htx_get_blk_type(blk);
5566 ret += htx_get_blksz(blk);
5567 blk = htx_remove_blk(htx, blk);
5568 /* The removed block is the EOH */
5569 if (type == HTX_BLK_EOH)
5570 break;
Christopher Faulet5be651d2021-01-22 15:28:03 +01005571 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005572
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005573 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005574 /* Response already closed: add END_STREAM */
5575 es_now = 1;
5576 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005577 else if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && h2s->status >= 200) {
5578 /* EOM+empty: we may need to add END_STREAM except for 1xx
Christopher Faulet991febd2020-12-02 15:17:31 +01005579 * responses and tunneled response.
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005580 */
Christopher Faulet991febd2020-12-02 15:17:31 +01005581 if (!(h2s->flags & H2_SF_BODY_TUNNEL) || h2s->status >= 300)
5582 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005583 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005584
Willy Tarreau115e83b2018-12-01 19:17:53 +01005585 if (es_now)
5586 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5587
5588 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005589 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02005590 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Faulet0b465482019-02-19 15:14:23 +01005591
5592 /* indicates the HEADERS frame was sent, except for 1xx responses. For
5593 * 1xx responses, another HEADERS frame is expected.
5594 */
Christopher Faulet89899422020-12-07 18:24:43 +01005595 if (h2s->status >= 200)
Christopher Faulet0b465482019-02-19 15:14:23 +01005596 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005597
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005598 if (h2c->flags & H2_CF_SHTS_UPDATED) {
5599 /* was sent above */
5600 h2c->flags |= H2_CF_DTSU_EMITTED;
Willy Tarreauc7d85482022-02-16 14:28:14 +01005601 h2c->flags &= ~H2_CF_SHTS_UPDATED;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005602 }
5603
Willy Tarreau115e83b2018-12-01 19:17:53 +01005604 if (es_now) {
5605 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005606 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 +01005607 if (h2s->st == H2_SS_OPEN)
5608 h2s->st = H2_SS_HLOC;
5609 else
5610 h2s_close(h2s);
5611 }
5612
5613 /* OK we could properly deliver the response */
Willy Tarreau115e83b2018-12-01 19:17:53 +01005614 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005615 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005616 return ret;
5617 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005618 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5619 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005620 h2c->flags |= H2_CF_MUX_MFULL;
5621 h2s->flags |= H2_SF_BLK_MROOM;
5622 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005623 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 +01005624 goto end;
5625 fail:
5626 /* unparsable HTX messages, too large ones to be produced in the local
5627 * list etc go here (unrecoverable errors).
5628 */
5629 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5630 ret = 0;
5631 goto end;
5632}
5633
Willy Tarreau80739692018-10-05 11:35:57 +02005634/* Try to send a HEADERS frame matching HTX request present in HTX message
5635 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5636 * must check the stream's status to detect any error which might have happened
5637 * subsequently to a successful send. The htx blocks are automatically removed
5638 * from the message. The htx message is assumed to be valid since produced from
5639 * the internal code, hence it contains a start line, an optional series of
5640 * header blocks and an end of header, otherwise an invalid frame could be
5641 * emitted and the resulting htx message could be left in an inconsistent state.
5642 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005643static size_t h2s_snd_bhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02005644{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005645 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02005646 struct h2c *h2c = h2s->h2c;
5647 struct htx_blk *blk;
Willy Tarreau80739692018-10-05 11:35:57 +02005648 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005649 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02005650 struct htx_sl *sl;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005651 struct ist meth, uri, auth, host = IST_NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005652 enum htx_blk_type type;
5653 int es_now = 0;
5654 int ret = 0;
5655 int hdr;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005656 int extended_connect = 0;
Willy Tarreau80739692018-10-05 11:35:57 +02005657
Willy Tarreau7838a792019-08-12 18:42:03 +02005658 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5659
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005660 /* get the start line (we do have one) and the rest of the headers,
5661 * that we dump starting at header 0 */
5662 sl = NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005663 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005664 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005665 type = htx_get_blk_type(blk);
5666
5667 if (type == HTX_BLK_UNUSED)
5668 continue;
5669
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005670 if (type == HTX_BLK_EOH)
Willy Tarreau80739692018-10-05 11:35:57 +02005671 break;
5672
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005673 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005674 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005675 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5676 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5677 goto fail;
5678 }
Willy Tarreau80739692018-10-05 11:35:57 +02005679
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005680 list[hdr].n = htx_get_blk_name(htx, blk);
5681 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02005682
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005683 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005684 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name) &&
5685 isteq(list[hdr].n, h2c->proxy->server_id_hdr_name))
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005686 continue;
Christopher Faulet67d58092019-10-02 10:51:38 +02005687
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005688 /* Convert connection: upgrade to Extended connect from rfc 8441 */
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005689 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteqi(list[hdr].n, ist("connection"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005690 /* rfc 7230 #6.1 Connection = list of tokens */
5691 struct ist connection_ist = list[hdr].v;
5692 do {
5693 if (isteqi(iststop(connection_ist, ','),
5694 ist("upgrade"))) {
Amaury Denoyelle0df04362021-10-18 09:43:29 +02005695 if (!(h2c->flags & H2_CF_RCVD_RFC8441)) {
5696 TRACE_STATE("reject upgrade because of no RFC8441 support", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5697 goto fail;
5698 }
5699
Amaury Denoyellee0c258c2021-10-18 10:05:16 +02005700 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 +01005701 h2s->flags |= (H2_SF_BODY_TUNNEL|H2_SF_EXT_CONNECT_SENT);
5702 sl->info.req.meth = HTTP_METH_CONNECT;
5703 meth = ist("CONNECT");
5704
5705 extended_connect = 1;
5706 break;
5707 }
5708
5709 connection_ist = istadv(istfind(connection_ist, ','), 1);
5710 } while (istlen(connection_ist));
5711 }
5712
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005713 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteq(list[hdr].n, ist("upgrade"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005714 /* rfc 7230 #6.7 Upgrade = list of protocols
5715 * rfc 8441 #4 Extended connect = :protocol is single-valued
5716 *
5717 * only first HTTP/1 protocol is preserved
5718 */
5719 const struct ist protocol = iststop(list[hdr].v, ',');
5720 /* upgrade_protocol field is 16 bytes long in h2s */
5721 istpad(h2s->upgrade_protocol, isttrim(protocol, 15));
5722 }
5723
5724 if (isteq(list[hdr].n, ist("host")))
5725 host = list[hdr].v;
5726
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005727 hdr++;
5728 }
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005729 else if (type == HTX_BLK_REQ_SL) {
5730 BUG_ON(sl); /* Only one start-line expected */
5731 sl = htx_get_blk_ptr(htx, blk);
5732 meth = htx_sl_req_meth(sl);
5733 uri = htx_sl_req_uri(sl);
5734 if (sl->info.req.meth == HTTP_METH_HEAD)
5735 h2s->flags |= H2_SF_BODYLESS_RESP;
5736 if (unlikely(uri.len == 0)) {
5737 TRACE_ERROR("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5738 goto fail;
5739 }
5740 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005741 else {
5742 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5743 goto fail;
5744 }
Willy Tarreau80739692018-10-05 11:35:57 +02005745 }
5746
Christopher Faulet56498132021-01-29 11:39:43 +01005747 /* The start-line me be defined */
5748 BUG_ON(!sl);
5749
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005750 /* Now add the server name to a header (if requested) */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005751 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005752 struct server *srv = objt_server(h2c->conn->target);
5753
5754 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005755 list[hdr].n = h2c->proxy->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005756 list[hdr].v = ist(srv->id);
5757 hdr++;
5758 }
5759 }
5760
Willy Tarreau80739692018-10-05 11:35:57 +02005761 /* marker for end of headers */
5762 list[hdr].n = ist("");
5763
Willy Tarreau9c218e72019-05-26 10:08:28 +02005764 mbuf = br_tail(h2c->mbuf);
5765 retry:
5766 if (!h2_get_buf(h2c, mbuf)) {
5767 h2c->flags |= H2_CF_MUX_MALLOC;
5768 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005769 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 +02005770 return 0;
5771 }
5772
Willy Tarreau80739692018-10-05 11:35:57 +02005773 chunk_reset(&outbuf);
5774
5775 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005776 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5777 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005778 break;
5779 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005780 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02005781 }
5782
5783 if (outbuf.size < 9)
5784 goto full;
5785
5786 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5787 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5788 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5789 outbuf.data = 9;
5790
5791 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01005792 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005793 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005794 goto realign_again;
5795 goto full;
5796 }
5797
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005798 h2_trace_header(ist(":method"), meth, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5799
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005800 auth = ist(NULL);
5801
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005802 /* RFC7540 #8.3: the CONNECT method must have :
5803 * - :authority set to the URI part (host:port)
5804 * - :method set to CONNECT
5805 * - :scheme and :path omitted
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005806 *
5807 * Note that this is not applicable in case of the Extended CONNECT
5808 * protocol from rfc 8441.
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005809 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005810 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT) && !extended_connect) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005811 auth = uri;
5812
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005813 if (!h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5814 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005815 /* output full */
5816 if (b_space_wraps(mbuf))
5817 goto realign_again;
5818 goto full;
5819 }
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005820
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005821 h2s->flags |= H2_SF_BODY_TUNNEL;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005822 } else {
5823 /* other methods need a :scheme. If an authority is known from
5824 * the request line, it must be sent, otherwise only host is
5825 * sent. Host is never sent as the authority.
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005826 *
5827 * This code is also applicable for Extended CONNECT protocol
5828 * from rfc 8441.
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005829 */
5830 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02005831
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005832 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
5833 /* the URI seems to start with a scheme */
5834 int len = 1;
5835
5836 while (len < uri.len && uri.ptr[len] != ':')
5837 len++;
5838
5839 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5840 /* make the uri start at the authority now */
Tim Duesterhus9f75ed12021-03-02 18:57:26 +01005841 scheme = ist2(uri.ptr, len);
Tim Duesterhus154374c2021-03-02 18:57:27 +01005842 uri = istadv(uri, len + 3);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005843
5844 /* find the auth part of the URI */
Tim Duesterhus92c696e2021-02-28 16:11:36 +01005845 auth = ist2(uri.ptr, 0);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005846 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5847 auth.len++;
5848
Tim Duesterhus154374c2021-03-02 18:57:27 +01005849 uri = istadv(uri, auth.len);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005850 }
5851 }
5852
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005853 /* For Extended CONNECT, the :authority must be present.
5854 * Use host value for it.
5855 */
5856 if (unlikely(extended_connect) && isttest(host))
5857 auth = host;
5858
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005859 if (!scheme.len) {
5860 /* no explicit scheme, we're using an origin-form URI,
5861 * probably from an H1 request transcoded to H2 via an
5862 * external layer, then received as H2 without authority.
5863 * So we have to look up the scheme from the HTX flags.
5864 * In such a case only http and https are possible, and
5865 * https is the default (sent by browsers).
5866 */
5867 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5868 scheme = ist("http");
5869 else
5870 scheme = ist("https");
5871 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005872
5873 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005874 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005875 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005876 goto realign_again;
5877 goto full;
5878 }
Willy Tarreau80739692018-10-05 11:35:57 +02005879
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005880 if (auth.len &&
5881 !h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5882 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005883 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005884 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005885 goto realign_again;
5886 goto full;
5887 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005888
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005889 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5890 * be sent as '/' or '*'.
5891 */
5892 if (unlikely(!uri.len)) {
5893 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5894 uri = ist("*");
5895 else
5896 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005897 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005898
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005899 if (!hpack_encode_path(&outbuf, uri)) {
5900 /* output full */
5901 if (b_space_wraps(mbuf))
5902 goto realign_again;
5903 goto full;
5904 }
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005905
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005906 h2_trace_header(ist(":path"), uri, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5907
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005908 /* encode the pseudo-header protocol from rfc8441 if using
5909 * Extended CONNECT method.
5910 */
5911 if (unlikely(extended_connect)) {
5912 const struct ist protocol = ist(h2s->upgrade_protocol);
5913 if (isttest(protocol)) {
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005914 if (!h2_encode_header(&outbuf, ist(":protocol"), protocol, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5915 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005916 /* output full */
5917 if (b_space_wraps(mbuf))
5918 goto realign_again;
5919 goto full;
5920 }
5921 }
5922 }
Willy Tarreau80739692018-10-05 11:35:57 +02005923 }
5924
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005925 /* encode all headers, stop at empty name. Host is only sent if we
5926 * do not provide an authority.
5927 */
Willy Tarreau80739692018-10-05 11:35:57 +02005928 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005929 struct ist n = list[hdr].n;
5930 struct ist v = list[hdr].v;
5931
Willy Tarreau80739692018-10-05 11:35:57 +02005932 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005933 if (isteq(n, ist("connection")) ||
5934 (auth.len && isteq(n, ist("host"))) ||
5935 isteq(n, ist("proxy-connection")) ||
5936 isteq(n, ist("keep-alive")) ||
5937 isteq(n, ist("upgrade")) ||
5938 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005939 continue;
5940
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005941 if (isteq(n, ist("te"))) {
5942 /* "te" may only be sent with "trailers" if this value
5943 * is present, otherwise it must be deleted.
5944 */
5945 v = istist(v, ist("trailers"));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01005946 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005947 continue;
5948 v = ist("trailers");
5949 }
5950
Christopher Faulet86d144c2019-08-14 16:32:25 +02005951 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005952 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005953 continue;
5954
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005955 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005956 break; // end
5957
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005958 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 +02005959 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005960 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005961 goto realign_again;
5962 goto full;
5963 }
5964 }
5965
Willy Tarreaucb985a42019-10-07 16:56:34 +02005966 /* update the frame's size */
5967 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5968
5969 if (outbuf.data > h2c->mfs + 9) {
5970 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5971 /* output full */
5972 if (b_space_wraps(mbuf))
5973 goto realign_again;
5974 goto full;
5975 }
5976 }
5977
Willy Tarreau3a537072021-06-17 08:40:04 +02005978 TRACE_USER("sent H2 request ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5979
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005980 /* remove all header blocks including the EOH and compute the
5981 * corresponding size.
Willy Tarreau80739692018-10-05 11:35:57 +02005982 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005983 ret = 0;
5984 blk = htx_get_head_blk(htx);
5985 while (blk) {
5986 type = htx_get_blk_type(blk);
5987 ret += htx_get_blksz(blk);
5988 blk = htx_remove_blk(htx, blk);
5989 /* The removed block is the EOH */
5990 if (type == HTX_BLK_EOH)
5991 break;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005992 }
Willy Tarreau80739692018-10-05 11:35:57 +02005993
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005994 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005995 /* Request already closed: add END_STREAM */
Willy Tarreau80739692018-10-05 11:35:57 +02005996 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005997 }
5998 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5999 /* EOM+empty: we may need to add END_STREAM (except for CONNECT
6000 * request)
6001 */
6002 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6003 es_now = 1;
6004 }
Willy Tarreau80739692018-10-05 11:35:57 +02006005
Willy Tarreau80739692018-10-05 11:35:57 +02006006 if (es_now)
6007 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
6008
6009 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006010 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02006011 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau80739692018-10-05 11:35:57 +02006012 h2s->flags |= H2_SF_HEADERS_SENT;
6013 h2s->st = H2_SS_OPEN;
6014
Willy Tarreau80739692018-10-05 11:35:57 +02006015 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006016 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 +02006017 // trim any possibly pending data (eg: inconsistent content-length)
6018 h2s->flags |= H2_SF_ES_SENT;
6019 h2s->st = H2_SS_HLOC;
6020 }
6021
Willy Tarreau80739692018-10-05 11:35:57 +02006022 end:
6023 return ret;
6024 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006025 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6026 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02006027 h2c->flags |= H2_CF_MUX_MFULL;
6028 h2s->flags |= H2_SF_BLK_MROOM;
6029 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006030 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 +02006031 goto end;
6032 fail:
6033 /* unparsable HTX messages, too large ones to be produced in the local
6034 * list etc go here (unrecoverable errors).
6035 */
6036 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6037 ret = 0;
6038 goto end;
6039}
6040
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006041/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01006042 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
6043 * caller must check the stream's status to detect any error which might have
6044 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006045 * consumed, or zero if nothing done.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006046 */
Christopher Faulet142854b2020-12-02 15:12:40 +01006047static size_t h2s_make_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006048{
6049 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006050 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006051 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006052 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006053 size_t total = 0;
6054 int es_now = 0;
6055 int bsize; /* htx block size */
6056 int fsize; /* h2 frame size */
6057 struct htx_blk *blk;
6058 enum htx_blk_type type;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01006059 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006060
Willy Tarreau7838a792019-08-12 18:42:03 +02006061 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6062
Willy Tarreau98de12a2018-12-12 07:03:00 +01006063 htx = htx_from_buf(buf);
6064
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006065 /* We only come here with HTX_BLK_DATA blocks */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006066
6067 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01006068 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006069 goto end;
6070
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006071 if ((h2c->flags & H2_CF_IS_BACK) &&
Christopher Fauletf95f8762021-01-22 11:59:07 +01006072 (h2s->flags & (H2_SF_HEADERS_RCVD|H2_SF_BODY_TUNNEL)) == H2_SF_BODY_TUNNEL) {
6073 /* The response HEADERS frame not received yet. Thus the tunnel
6074 * is not fully established yet. In this situation, we block
6075 * data sending.
6076 */
6077 h2s->flags |= H2_SF_BLK_MBUSY;
6078 TRACE_STATE("Request DATA frame blocked waiting for tunnel establishment", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6079 goto end;
6080 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01006081 else if ((h2c->flags & H2_CF_IS_BACK) && (h2s->flags & H2_SF_TUNNEL_ABRT)) {
6082 /* a tunnel attempt was aborted but the is pending raw data to xfer to the server.
6083 * Thus the stream is closed with the CANCEL error. The error will be reported to
6084 * the upper layer as aserver abort. But at this stage there is nothing more we can
6085 * do. We just wait for the end of the response to be sure to not truncate it.
6086 */
6087 if (!(h2s->flags & H2_SF_ES_RCVD)) {
6088 TRACE_STATE("Request DATA frame blocked waiting end of aborted tunnel", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6089 h2s->flags |= H2_SF_BLK_MBUSY;
6090 }
6091 else {
6092 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
6093 h2s_error(h2s, H2_ERR_CANCEL);
6094 }
6095 goto end;
6096 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01006097
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006098 blk = htx_get_head_blk(htx);
6099 type = htx_get_blk_type(blk);
6100 bsize = htx_get_blksz(blk);
6101 fsize = bsize;
6102 trunc_out = 0;
6103 if (type != HTX_BLK_DATA)
6104 goto end;
6105
Willy Tarreau9c218e72019-05-26 10:08:28 +02006106 mbuf = br_tail(h2c->mbuf);
6107 retry:
6108 if (!h2_get_buf(h2c, mbuf)) {
6109 h2c->flags |= H2_CF_MUX_MALLOC;
6110 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006111 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 +02006112 goto end;
6113 }
6114
Willy Tarreau98de12a2018-12-12 07:03:00 +01006115 /* Perform some optimizations to reduce the number of buffer copies.
6116 * First, if the mux's buffer is empty and the htx area contains
6117 * exactly one data block of the same size as the requested count, and
6118 * this count fits within the frame size, the stream's window size, and
6119 * the connection's window size, then it's possible to simply swap the
6120 * caller's buffer with the mux's output buffer and adjust offsets and
6121 * length to match the entire DATA HTX block in the middle. In this
6122 * case we perform a true zero-copy operation from end-to-end. This is
6123 * the situation that happens all the time with large files. Second, if
6124 * this is not possible, but the mux's output buffer is empty, we still
6125 * have an opportunity to avoid the copy to the intermediary buffer, by
6126 * making the intermediary buffer's area point to the output buffer's
6127 * area. In this case we want to skip the HTX header to make sure that
6128 * copies remain aligned and that this operation remains possible all
6129 * the time. This goes for headers, data blocks and any data extracted
6130 * from the HTX blocks.
6131 */
6132 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02006133 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006134 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006135 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006136
Willy Tarreaubcc45952019-05-26 10:05:50 +02006137 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01006138 /* Too bad there are data left there. We're willing to memcpy/memmove
6139 * up to 1/4 of the buffer, which means that it's OK to copy a large
6140 * frame into a buffer containing few data if it needs to be realigned,
6141 * and that it's also OK to copy few data without realigning. Otherwise
6142 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01006143 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006144 if (fsize + 9 <= b_room(mbuf) &&
6145 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02006146 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
6147 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 +01006148 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02006149 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01006150
Willy Tarreau9c218e72019-05-26 10:08:28 +02006151 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6152 goto retry;
6153
Willy Tarreau98de12a2018-12-12 07:03:00 +01006154 h2c->flags |= H2_CF_MUX_MFULL;
6155 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006156 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 +01006157 goto end;
6158 }
6159
Christopher Faulet925abdf2021-04-27 22:51:07 +02006160 if (htx->flags & HTX_FL_EOM) {
6161 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6162 * message)
6163 */
6164 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6165 es_now = 1;
6166 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01006167 /* map an H2 frame to the HTX block so that we can put the
6168 * frame header there.
6169 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006170 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
6171 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01006172
6173 /* prepend an H2 DATA frame header just before the DATA block */
6174 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6175 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
Christopher Faulet925abdf2021-04-27 22:51:07 +02006176 if (es_now)
6177 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006178 h2_set_frame_size(outbuf.area, fsize);
6179
6180 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006181 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006182 h2c->mws -= fsize;
6183
6184 /* and exchange with our old area */
6185 buf->area = old_area;
6186 buf->data = buf->head = 0;
6187 total += fsize;
Christopher Faulet925abdf2021-04-27 22:51:07 +02006188 fsize = 0;
Willy Tarreau936db562023-10-18 11:39:43 +02006189 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau7838a792019-08-12 18:42:03 +02006190
6191 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 +02006192 goto out;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006193 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01006194
Willy Tarreau98de12a2018-12-12 07:03:00 +01006195 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006196 /* for DATA and EOM we'll have to emit a frame, even if empty */
6197
6198 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006199 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6200 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006201 break;
6202 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006203 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006204 }
6205
6206 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02006207 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6208 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006209 h2c->flags |= H2_CF_MUX_MFULL;
6210 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006211 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006212 goto end;
6213 }
6214
6215 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
6216 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6217 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6218 outbuf.data = 9;
6219
6220 /* we have in <fsize> the exact number of bytes we need to copy from
6221 * the HTX buffer. We need to check this against the connection's and
6222 * the stream's send windows, and to ensure that this fits in the max
6223 * frame size and in the buffer's available space minus 9 bytes (for
6224 * the frame header). The connection's flow control is applied last so
6225 * that we can use a separate list of streams which are immediately
6226 * unblocked on window opening. Note: we don't implement padding.
6227 */
6228
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006229 if (!fsize)
6230 goto send_empty;
6231
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006232 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006233 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreau2b718102021-04-21 07:32:39 +02006234 if (LIST_INLIST(&h2s->list))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006235 h2_remove_from_list(h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006236 LIST_APPEND(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02006237 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 +01006238 goto end;
6239 }
6240
Willy Tarreauee573762018-12-04 15:25:57 +01006241 if (fsize > count)
6242 fsize = count;
6243
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006244 if (fsize > h2s_mws(h2s))
6245 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006246
6247 if (h2c->mfs && fsize > h2c->mfs)
6248 fsize = h2c->mfs; // >0
6249
6250 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02006251 /* It doesn't fit at once. If it at least fits once split and
6252 * the amount of data to move is low, let's defragment the
6253 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006254 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006255 if (b_space_wraps(mbuf) &&
6256 (fsize + 9 <= b_room(mbuf)) &&
6257 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006258 goto realign_again;
6259 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01006260 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006261
6262 if (fsize <= 0) {
6263 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02006264 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6265 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006266 h2c->flags |= H2_CF_MUX_MFULL;
6267 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006268 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006269 goto end;
6270 }
6271 }
6272
6273 if (h2c->mws <= 0) {
6274 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02006275 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 +01006276 goto end;
6277 }
6278
6279 if (fsize > h2c->mws)
6280 fsize = h2c->mws;
6281
6282 /* now let's copy this this into the output buffer */
6283 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006284 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01006285 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01006286 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006287
6288 send_empty:
6289 /* update the frame's size */
6290 h2_set_frame_size(outbuf.area, fsize);
6291
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006292 /* consume incoming HTX block */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006293 total += fsize;
6294 if (fsize == bsize) {
6295 htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006296 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
6297 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6298 * message)
6299 */
6300 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6301 es_now = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02006302 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006303 }
6304 else {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006305 /* we've truncated this block */
6306 htx_cut_data_blk(htx, blk, fsize);
6307 }
6308
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006309 if (es_now)
6310 outbuf.area[4] |= H2_F_DATA_END_STREAM;
6311
6312 /* commit the H2 response */
6313 b_add(mbuf, fsize + 9);
Willy Tarreau936db562023-10-18 11:39:43 +02006314 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006315
Christopher Faulet925abdf2021-04-27 22:51:07 +02006316 out:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006317 if (es_now) {
6318 if (h2s->st == H2_SS_OPEN)
6319 h2s->st = H2_SS_HLOC;
6320 else
6321 h2s_close(h2s);
6322
6323 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02006324 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 +01006325 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006326 else if (fsize) {
6327 if (fsize == bsize) {
6328 TRACE_DEVEL("more data may be available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6329 goto new_frame;
6330 }
6331 else if (trunc_out) {
6332 /* we've truncated this block */
6333 goto new_frame;
6334 }
6335 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006336
6337 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006338 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006339 return total;
6340}
6341
Christopher Faulet991febd2020-12-02 15:17:31 +01006342/* Skip the message payload (DATA blocks) and emit an empty DATA frame with the
6343 * ES flag set for stream <h2s>. This function is called for response known to
6344 * have no payload. Only DATA blocks are skipped. This means the trailers are
Ilya Shipitsinacf84592021-02-06 22:29:08 +05006345 * still emitted. The caller must check the stream's status to detect any error
Christopher Faulet991febd2020-12-02 15:17:31 +01006346 * which might have happened subsequently to a successful send. Returns the
6347 * number of data bytes consumed, or zero if nothing done.
6348 */
6349static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count)
6350{
6351 struct h2c *h2c = h2s->h2c;
6352 struct htx *htx;
6353 int bsize; /* htx block size */
6354 int fsize; /* h2 frame size */
6355 struct htx_blk *blk;
6356 enum htx_blk_type type;
6357 size_t total = 0;
6358
6359 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6360
Christopher Faulet991febd2020-12-02 15:17:31 +01006361 htx = htx_from_buf(buf);
6362
6363 next_data:
6364 if (!count || htx_is_empty(htx))
6365 goto end;
6366 blk = htx_get_head_blk(htx);
6367 type = htx_get_blk_type(blk);
6368 bsize = htx_get_blksz(blk);
6369 fsize = bsize;
6370 if (type != HTX_BLK_DATA)
6371 goto end;
6372
6373 if (fsize > count)
6374 fsize = count;
6375
6376 if (fsize != bsize)
6377 goto skip_data;
6378
6379 if (!(htx->flags & HTX_FL_EOM) || !htx_is_unique_blk(htx, blk))
6380 goto skip_data;
6381
6382 /* Here, it is the last block and it is also the end of the message. So
6383 * we can emit an empty DATA frame with the ES flag set
6384 */
6385 if (h2_send_empty_data_es(h2s) <= 0)
6386 goto end;
6387
6388 if (h2s->st == H2_SS_OPEN)
6389 h2s->st = H2_SS_HLOC;
6390 else
6391 h2s_close(h2s);
6392
6393 skip_data:
6394 /* consume incoming HTX block */
6395 total += fsize;
6396 if (fsize == bsize) {
6397 TRACE_DEVEL("more data may be available, trying to skip another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6398 htx_remove_blk(htx, blk);
6399 goto next_data;
6400 }
6401 else {
6402 /* we've truncated this block */
6403 htx_cut_data_blk(htx, blk, fsize);
6404 }
6405
6406 end:
6407 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6408 return total;
6409}
6410
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006411/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
6412 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
6413 * processed. The caller must check the stream's status to detect any error
6414 * which might have happened subsequently to a successful send. The htx blocks
6415 * are automatically removed from the message. The htx message is assumed to be
6416 * valid since produced from the internal code. Processing stops when meeting
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006417 * the EOT, which *is* removed. All trailers are processed at once and sent as a
6418 * single frame. The ES flag is always set.
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006419 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006420static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006421{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02006422 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006423 struct h2c *h2c = h2s->h2c;
6424 struct htx_blk *blk;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006425 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006426 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006427 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006428 int ret = 0;
6429 int hdr;
6430 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006431
Willy Tarreau7838a792019-08-12 18:42:03 +02006432 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
6433
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006434 /* get trailers. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006435 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006436 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006437 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006438
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006439 if (type == HTX_BLK_UNUSED)
6440 continue;
6441
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006442 if (type == HTX_BLK_EOT)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006443 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006444 if (type == HTX_BLK_TLR) {
6445 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
6446 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
6447 goto fail;
6448 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006449
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006450 list[hdr].n = htx_get_blk_name(htx, blk);
6451 list[hdr].v = htx_get_blk_value(htx, blk);
6452 hdr++;
6453 }
6454 else {
6455 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 +01006456 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02006457 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006458 }
6459
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006460 /* marker for end of trailers */
6461 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006462
Willy Tarreau9c218e72019-05-26 10:08:28 +02006463 mbuf = br_tail(h2c->mbuf);
6464 retry:
6465 if (!h2_get_buf(h2c, mbuf)) {
6466 h2c->flags |= H2_CF_MUX_MALLOC;
6467 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006468 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 +02006469 goto end;
6470 }
6471
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006472 chunk_reset(&outbuf);
6473
6474 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006475 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6476 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006477 break;
6478 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006479 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006480 }
6481
6482 if (outbuf.size < 9)
6483 goto full;
6484
6485 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
6486 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
6487 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6488 outbuf.data = 9;
6489
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006490 /* encode all headers */
6491 for (idx = 0; idx < hdr; idx++) {
6492 /* these ones do not exist in H2 or must not appear in
6493 * trailers and must be dropped.
6494 */
6495 if (isteq(list[idx].n, ist("host")) ||
6496 isteq(list[idx].n, ist("content-length")) ||
6497 isteq(list[idx].n, ist("connection")) ||
6498 isteq(list[idx].n, ist("proxy-connection")) ||
6499 isteq(list[idx].n, ist("keep-alive")) ||
6500 isteq(list[idx].n, ist("upgrade")) ||
6501 isteq(list[idx].n, ist("te")) ||
6502 isteq(list[idx].n, ist("transfer-encoding")))
6503 continue;
6504
Christopher Faulet86d144c2019-08-14 16:32:25 +02006505 /* Skip all pseudo-headers */
6506 if (*(list[idx].n.ptr) == ':')
6507 continue;
6508
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01006509 if (!h2_encode_header(&outbuf, list[idx].n, list[idx].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
6510 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006511 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006512 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006513 goto realign_again;
6514 goto full;
6515 }
6516 }
6517
Willy Tarreau5121e5d2019-05-06 15:13:41 +02006518 if (outbuf.data == 9) {
6519 /* here we have a problem, we have nothing to emit (either we
6520 * received an empty trailers block followed or we removed its
6521 * contents above). Because of this we can't send a HEADERS
6522 * frame, so we have to cheat and instead send an empty DATA
6523 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01006524 */
6525 outbuf.area[3] = H2_FT_DATA;
6526 outbuf.area[4] = H2_F_DATA_END_STREAM;
6527 }
6528
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006529 /* update the frame's size */
6530 h2_set_frame_size(outbuf.area, outbuf.data - 9);
6531
Willy Tarreau572d9f52019-10-11 16:58:37 +02006532 if (outbuf.data > h2c->mfs + 9) {
6533 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
6534 /* output full */
6535 if (b_space_wraps(mbuf))
6536 goto realign_again;
6537 goto full;
6538 }
6539 }
6540
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006541 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02006542 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 +02006543 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02006544 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006545 h2s->flags |= H2_SF_ES_SENT;
6546
6547 if (h2s->st == H2_SS_OPEN)
6548 h2s->st = H2_SS_HLOC;
6549 else
6550 h2s_close(h2s);
6551
6552 /* OK we could properly deliver the response */
6553 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02006554 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006555 ret = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006556 blk = htx_get_head_blk(htx);
6557 while (blk) {
6558 type = htx_get_blk_type(blk);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006559 ret += htx_get_blksz(blk);
6560 blk = htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006561 /* The removed block is the EOT */
6562 if (type == HTX_BLK_EOT)
6563 break;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006564 }
6565
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006566 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006567 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006568 return ret;
6569 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006570 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6571 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006572 h2c->flags |= H2_CF_MUX_MFULL;
6573 h2s->flags |= H2_SF_BLK_MROOM;
6574 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006575 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 +01006576 goto end;
6577 fail:
6578 /* unparsable HTX messages, too large ones to be produced in the local
6579 * list etc go here (unrecoverable errors).
6580 */
6581 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6582 ret = 0;
6583 goto end;
6584}
6585
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006586/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6587 * event subscriber <es> is not allowed to change from a previous call as long
6588 * as at least one event is still subscribed. The <event_type> must only be a
6589 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006590 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006591static int h2_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02006592{
Willy Tarreau36c22322022-05-27 10:41:24 +02006593 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02006594 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006595
Willy Tarreau7838a792019-08-12 18:42:03 +02006596 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006597
6598 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006599 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006600
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006601 es->events |= event_type;
6602 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006603
6604 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006605 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006606
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006607 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006608 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02006609 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02006610 !LIST_INLIST(&h2s->list)) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006611 if (h2s->flags & H2_SF_BLK_MFCTL) {
6612 TRACE_DEVEL("Adding to fctl list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006613 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006614 }
6615 else {
6616 TRACE_DEVEL("Adding to send list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006617 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006618 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02006619 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02006620 }
Willy Tarreau7838a792019-08-12 18:42:03 +02006621 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006622 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006623}
6624
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006625/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6626 * The <es> pointer is not allowed to differ from the one passed to the
6627 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006628 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006629static int h2_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006630{
Willy Tarreau36c22322022-05-27 10:41:24 +02006631 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006632
Willy Tarreau7838a792019-08-12 18:42:03 +02006633 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006634
6635 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006636 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006637
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006638 es->events &= ~event_type;
6639 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01006640 h2s->subs = NULL;
6641
6642 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006643 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006644
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006645 if (event_type & SUB_RETRY_SEND) {
Frédéric Lécaille67fda162022-06-30 12:01:54 +02006646 TRACE_DEVEL("unsubscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006647 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006648 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006649 h2_remove_from_list(h2s);
Olivier Houchardd846c262018-10-19 17:24:29 +02006650 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01006651
Willy Tarreau7838a792019-08-12 18:42:03 +02006652 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006653 return 0;
6654}
6655
6656
Christopher Faulet564e39c2021-09-21 15:50:55 +02006657/* Called from the upper layer, to receive data
6658 *
6659 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
6660 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
6661 * means the caller wants to flush input data (from the mux buffer and the
6662 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
6663 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
6664 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
6665 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
6666 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
6667 * copy as much data as possible.
6668 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006669static size_t h2_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Olivier Houchard511efea2018-08-16 15:30:32 +02006670{
Willy Tarreau36c22322022-05-27 10:41:24 +02006671 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau082f5592018-11-25 08:03:32 +01006672 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01006673 struct htx *h2s_htx = NULL;
6674 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02006675 size_t ret = 0;
6676
Willy Tarreau7838a792019-08-12 18:42:03 +02006677 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
6678
Olivier Houchard511efea2018-08-16 15:30:32 +02006679 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006680 h2s_htx = htx_from_buf(&h2s->rxbuf);
Christopher Fauletec361bb2022-02-21 15:12:54 +01006681 if (htx_is_empty(h2s_htx) && !(h2s_htx->flags & HTX_FL_PARSING_ERROR)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02006682 /* Here htx_to_buf() will set buffer data to 0 because
6683 * the HTX is empty.
6684 */
6685 htx_to_buf(h2s_htx, &h2s->rxbuf);
6686 goto end;
6687 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006688 ret = h2s_htx->data;
6689 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01006690
Christopher Faulet9b79a102019-07-15 11:22:56 +02006691 /* <buf> is empty and the message is small enough, swap the
6692 * buffers. */
6693 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01006694 htx_to_buf(buf_htx, buf);
6695 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006696 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
6697 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01006698 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006699
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006700 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_UNUSED);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006701
6702 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
6703 buf_htx->flags |= HTX_FL_PARSING_ERROR;
6704 if (htx_is_empty(buf_htx))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006705 se_fl_set(h2s->sd, SE_FL_EOI);
Willy Tarreau86724e22018-12-01 23:19:43 +01006706 }
Christopher Faulet72722c02023-02-23 14:26:34 +01006707 else if (htx_is_empty(h2s_htx)) {
Christopher Faulet42432f32020-11-20 17:43:16 +01006708 buf_htx->flags |= (h2s_htx->flags & HTX_FL_EOM);
Christopher Faulet72722c02023-02-23 14:26:34 +01006709 }
6710
Christopher Faulet9b79a102019-07-15 11:22:56 +02006711 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
6712 htx_to_buf(buf_htx, buf);
6713 htx_to_buf(h2s_htx, &h2s->rxbuf);
6714 ret -= h2s_htx->data;
6715
Christopher Faulet37070b22019-02-14 15:12:14 +01006716 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02006717 if (b_data(&h2s->rxbuf))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006718 se_fl_set(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006719 else {
Christopher Faulet34f81d52023-05-04 16:41:37 +02006720 if (!(h2c->flags & H2_CF_IS_BACK) && (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_ES_RCVD))) {
Christopher Faulet4403cdf2023-05-04 15:49:12 +02006721 /* If request ES is reported to the upper layer, it means the
6722 * H2S now expects data from the opposite side.
6723 */
6724 se_expect_data(h2s->sd);
6725 }
6726
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006727 se_fl_clr(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet531dd052023-05-24 11:14:38 +02006728 h2s_propagate_term_flags(h2c, h2s);
Olivier Houchard638b7992018-08-16 15:41:52 +02006729 if (b_size(&h2s->rxbuf)) {
6730 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01006731 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02006732 }
Olivier Houchard511efea2018-08-16 15:30:32 +02006733 }
6734
Willy Tarreau082f5592018-11-25 08:03:32 +01006735 if (ret && h2c->dsi == h2s->id) {
6736 /* demux is blocking on this stream's buffer */
6737 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02006738 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01006739 }
Christopher Faulet37070b22019-02-14 15:12:14 +01006740
Willy Tarreau7838a792019-08-12 18:42:03 +02006741 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02006742 return ret;
6743}
6744
Olivier Houchardd846c262018-10-19 17:24:29 +02006745
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006746/* Called from the upper layer, to send data from buffer <buf> for no more than
6747 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02006748 * flags may be updated on the stream connector.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006749 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006750static size_t h2_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02006751{
Willy Tarreau36c22322022-05-27 10:41:24 +02006752 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau1dc41e72018-06-14 13:21:28 +02006753 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02006754 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006755 struct htx *htx;
6756 struct htx_blk *blk;
6757 enum htx_blk_type btype;
6758 uint32_t bsize;
6759 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006760
Willy Tarreau7838a792019-08-12 18:42:03 +02006761 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
6762
Olivier Houchardd360ac62019-03-22 17:37:16 +01006763 /* If we were not just woken because we wanted to send but couldn't,
6764 * and there's somebody else that is waiting to send, do nothing,
6765 * we will subscribe later and be put at the end of the list
6766 */
Willy Tarreaud9464162020-01-10 18:25:07 +01006767 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02006768 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006769 if (LIST_INLIST(&h2s->list))
6770 TRACE_DEVEL("stream already waiting, leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6771 else {
6772 TRACE_DEVEL("other streams already waiting, going to the queue and leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6773 h2s->h2c->flags |= H2_CF_WAIT_INLIST;
6774 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01006775 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006776 }
Willy Tarreaud9464162020-01-10 18:25:07 +01006777 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02006778
Willy Tarreau7838a792019-08-12 18:42:03 +02006779 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
6780 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 +02006781 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006782 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02006783
Willy Tarreaucab22952019-10-31 15:48:18 +01006784 if (h2s->h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006785 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreaucab22952019-10-31 15:48:18 +01006786 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);
6787 return 0;
6788 }
6789
Christopher Faulet9b79a102019-07-15 11:22:56 +02006790 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006791
Willy Tarreau0bad0432018-06-14 16:54:01 +02006792 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01006793 h2s->flags |= H2_SF_OUTGOING_DATA;
6794
Christopher Faulet2e47e3a2023-01-13 11:40:24 +01006795 if (htx->extra && htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH)
Willy Tarreau48770452022-08-18 16:03:51 +02006796 h2s->flags |= H2_SF_MORE_HTX_DATA;
6797 else
6798 h2s->flags &= ~H2_SF_MORE_HTX_DATA;
6799
Willy Tarreau751f2d02018-10-05 09:35:00 +02006800 if (h2s->id == 0) {
6801 int32_t id = h2c_get_next_sid(h2s->h2c);
6802
6803 if (id < 0) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006804 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02006805 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 +02006806 return 0;
6807 }
6808
6809 eb32_delete(&h2s->by_id);
6810 h2s->by_id.key = h2s->id = id;
6811 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01006812 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02006813 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
6814 }
6815
Christopher Faulet9b79a102019-07-15 11:22:56 +02006816 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
6817 count && !htx_is_empty(htx)) {
6818 idx = htx_get_head(htx);
6819 blk = htx_get_blk(htx, idx);
6820 btype = htx_get_blk_type(blk);
6821 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006822
Christopher Faulet9b79a102019-07-15 11:22:56 +02006823 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02006824 case HTX_BLK_REQ_SL:
6825 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006826 ret = h2s_snd_bhdrs(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02006827 if (ret > 0) {
6828 total += ret;
6829 count -= ret;
6830 if (ret < bsize)
6831 goto done;
6832 }
6833 break;
6834
Willy Tarreau115e83b2018-12-01 19:17:53 +01006835 case HTX_BLK_RES_SL:
6836 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006837 ret = h2s_snd_fhdrs(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01006838 if (ret > 0) {
6839 total += ret;
6840 count -= ret;
6841 if (ret < bsize)
6842 goto done;
6843 }
6844 break;
6845
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006846 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006847 /* all these cause the emission of a DATA frame (possibly empty) */
Christopher Faulet991febd2020-12-02 15:17:31 +01006848 if (!(h2s->h2c->flags & H2_CF_IS_BACK) &&
6849 (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BODYLESS_RESP)) == H2_SF_BODYLESS_RESP)
6850 ret = h2s_skip_data(h2s, buf, count);
6851 else
6852 ret = h2s_make_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006853 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01006854 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006855 total += ret;
6856 count -= ret;
6857 if (ret < bsize)
6858 goto done;
6859 }
6860 break;
6861
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006862 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006863 case HTX_BLK_EOT:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006864 /* This is the first trailers block, all the subsequent ones */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006865 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006866 if (ret > 0) {
6867 total += ret;
6868 count -= ret;
6869 if (ret < bsize)
6870 goto done;
6871 }
6872 break;
6873
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006874 default:
6875 htx_remove_blk(htx, blk);
6876 total += bsize;
6877 count -= bsize;
6878 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006879 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006880 }
6881
Christopher Faulet9b79a102019-07-15 11:22:56 +02006882 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02006883 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02006884 /* trim any possibly pending data after we close (extra CR-LF,
6885 * unprocessed trailers, abnormal extra data, ...)
6886 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02006887 total += count;
6888 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02006889 }
6890
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006891 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01006892 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006893 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 +02006894 se_fl_set_error(h2s->sd);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01006895 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01006896 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006897 }
6898
Christopher Faulet9b79a102019-07-15 11:22:56 +02006899 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02006900
Olivier Houchard7505f942018-08-21 18:10:44 +02006901 if (total > 0) {
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006902 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006903 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 +02006904 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006905 }
Olivier Houchardd846c262018-10-19 17:24:29 +02006906
Olivier Houchard7505f942018-08-21 18:10:44 +02006907 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006908 /* If we're waiting for flow control, and we got a shutr on the
6909 * connection, we will never be unlocked, so add an error on
Willy Tarreau4596fe22022-05-17 19:07:51 +02006910 * the stream connector.
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006911 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02006912 if ((h2s->h2c->flags & H2_CF_RCVD_SHUT) &&
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006913 !b_data(&h2s->h2c->dbuf) &&
6914 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006915 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 +01006916 se_fl_set_error(h2s->sd);
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006917 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006918
Willy Tarreau5723f292020-01-10 15:16:57 +01006919 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
6920 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006921 /* Ok we managed to send something, leave the send_list if we were still there */
Willy Tarreaude4a5382023-10-17 08:25:19 +02006922 h2_remove_from_list(h2s);
6923 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 +01006924 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006925
Willy Tarreau7838a792019-08-12 18:42:03 +02006926 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006927 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02006928}
6929
Willy Tarreau90bffa22022-09-01 19:06:44 +02006930/* appends some info about stream <h2s> to buffer <msg>, or does nothing if
Willy Tarreau7051f732022-09-02 15:22:12 +02006931 * <h2s> is NULL. Returns non-zero if the stream is considered suspicious. May
6932 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
6933 * NULL, otherwise a single line is used.
Willy Tarreau90bffa22022-09-01 19:06:44 +02006934 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006935static int h2_dump_h2s_info(struct buffer *msg, const struct h2s *h2s, const char *pfx)
Willy Tarreau90bffa22022-09-01 19:06:44 +02006936{
6937 int ret = 0;
6938
6939 if (!h2s)
6940 return ret;
6941
Willy Tarreau7051f732022-09-02 15:22:12 +02006942 chunk_appendf(msg, " h2s.id=%d .st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u",
Willy Tarreau90bffa22022-09-01 19:06:44 +02006943 h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
6944 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02006945 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf));
6946
6947 if (pfx)
6948 chunk_appendf(msg, "\n%s", pfx);
6949
6950 chunk_appendf(msg, " .sc=%p", h2s_sc(h2s));
Willy Tarreau90bffa22022-09-01 19:06:44 +02006951 if (h2s_sc(h2s))
6952 chunk_appendf(msg, "(.flg=0x%08x .app=%p)",
6953 h2s_sc(h2s)->flags, h2s_sc(h2s)->app);
6954
Willy Tarreau7051f732022-09-02 15:22:12 +02006955 chunk_appendf(msg, " .sd=%p", h2s->sd);
Willy Tarreau90bffa22022-09-01 19:06:44 +02006956 chunk_appendf(msg, "(.flg=0x%08x)", se_fl_get(h2s->sd));
6957
Willy Tarreau7051f732022-09-02 15:22:12 +02006958 if (pfx)
6959 chunk_appendf(msg, "\n%s", pfx);
6960
Willy Tarreau90bffa22022-09-01 19:06:44 +02006961 chunk_appendf(msg, " .subs=%p", h2s->subs);
6962 if (h2s->subs) {
6963 chunk_appendf(msg, "(ev=%d tl=%p", h2s->subs->events, h2s->subs->tasklet);
6964 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
6965 h2s->subs->tasklet->calls,
6966 h2s->subs->tasklet->context);
6967 if (h2s->subs->tasklet->calls >= 1000000)
6968 ret = 1;
6969 resolve_sym_name(msg, NULL, h2s->subs->tasklet->process);
6970 chunk_appendf(msg, ")");
6971 }
6972 return ret;
6973}
6974
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006975/* appends some info about connection <h2c> to buffer <msg>, or does nothing if
6976 * <h2c> is NULL. Returns non-zero if the connection is considered suspicious.
Willy Tarreau7051f732022-09-02 15:22:12 +02006977 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
6978 * not NULL, otherwise a single line is used.
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006979 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006980static int h2_dump_h2c_info(struct buffer *msg, struct h2c *h2c, const char *pfx)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006981{
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006982 const struct buffer *hmbuf, *tmbuf;
6983 const struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006984 struct eb32_node *node;
6985 int fctl_cnt = 0;
6986 int send_cnt = 0;
6987 int tree_cnt = 0;
6988 int orph_cnt = 0;
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006989 int ret = 0;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006990
6991 if (!h2c)
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006992 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006993
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006994 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006995 fctl_cnt++;
6996
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006997 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006998 send_cnt++;
6999
7000 node = eb32_first(&h2c->streams_by_id);
7001 while (node) {
7002 h2s = container_of(node, struct h2s, by_id);
7003 tree_cnt++;
Willy Tarreau7be4ee02022-05-18 07:31:41 +02007004 if (!h2s_sc(h2s))
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007005 orph_cnt++;
7006 node = eb32_next(node);
7007 }
7008
Willy Tarreau60f62682019-05-26 11:32:27 +02007009 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02007010 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02007011 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau22892322024-01-17 16:57:23 +01007012 " .nbst=%u .nbsc=%u, .glitches=%d",
Willy Tarreauab2ec452019-08-30 07:07:08 +02007013 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau22892322024-01-17 16:57:23 +01007014 h2c->nb_streams, h2c->nb_sc, h2c->glitches);
Willy Tarreau7051f732022-09-02 15:22:12 +02007015
7016 if (pfx)
7017 chunk_appendf(msg, "\n%s", pfx);
7018
7019 chunk_appendf(msg, " .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
7020 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u",
7021 fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01007022 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01007023 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02007024 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf));
7025
7026 if (pfx)
7027 chunk_appendf(msg, "\n%s", pfx);
7028
Christopher Faulet68ee7842022-10-12 10:21:33 +02007029 chunk_appendf(msg, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau60f62682019-05-26 11:32:27 +02007030 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
7031 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
7032 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02007033 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
7034 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01007035
Willy Tarreauf8c77092022-11-29 15:26:43 +01007036 chunk_appendf(msg, " .task=%p", h2c->task);
7037 if (h2c->task) {
7038 chunk_appendf(msg, " .exp=%s",
7039 h2c->task->expire ? tick_is_expired(h2c->task->expire, now_ms) ? "<PAST>" :
7040 human_time(TICKS_TO_MS(h2c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>");
7041 }
Willy Tarreau7051f732022-09-02 15:22:12 +02007042
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02007043 return ret;
7044}
7045
7046/* for debugging with CLI's "show fd" command */
7047static int h2_show_fd(struct buffer *msg, struct connection *conn)
7048{
7049 struct h2c *h2c = conn->ctx;
7050 const struct h2s *h2s;
7051 struct eb32_node *node;
7052 int ret = 0;
7053
7054 if (!h2c)
7055 return ret;
7056
Willy Tarreau7051f732022-09-02 15:22:12 +02007057 ret |= h2_dump_h2c_info(msg, h2c, NULL);
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02007058
7059 node = eb32_last(&h2c->streams_by_id);
7060 if (node) {
7061 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau90bffa22022-09-01 19:06:44 +02007062 chunk_appendf(msg, " last_h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02007063 ret |= h2_dump_h2s_info(msg, h2s, NULL);
Willy Tarreau987c0632018-12-18 10:32:05 +01007064 }
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02007065
Willy Tarreau06bf83e2021-01-21 09:13:35 +01007066 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007067}
Willy Tarreau62f52692017-10-08 23:01:42 +02007068
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007069/* for debugging with CLI's "show sess" command. May emit multiple lines, each
7070 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
7071 * line is used. Each field starts with a space so it's safe to print it after
7072 * existing fields.
7073 */
7074static int h2_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
7075{
7076 struct h2s *h2s = sd->se;
7077 int ret = 0;
7078
7079 if (!h2s)
7080 return ret;
7081
7082 chunk_appendf(msg, " h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02007083 ret |= h2_dump_h2s_info(msg, h2s, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007084 if (pfx)
7085 chunk_appendf(msg, "\n%s", pfx);
7086 chunk_appendf(msg, " h2c=%p", h2s->h2c);
Willy Tarreau7051f732022-09-02 15:22:12 +02007087 ret |= h2_dump_h2c_info(msg, h2s->h2c, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007088 return ret;
7089}
7090
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007091/* Migrate the the connection to the current thread.
7092 * Return 0 if successful, non-zero otherwise.
7093 * Expected to be called with the old thread lock held.
7094 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02007095static int h2_takeover(struct connection *conn, int orig_tid)
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007096{
7097 struct h2c *h2c = conn->ctx;
Willy Tarreau617e80f2020-07-01 16:39:33 +02007098 struct task *task;
Willy Tarreau960f37c2023-11-17 10:56:33 +01007099 struct task *new_task;
7100 struct tasklet *new_tasklet;
7101
7102 /* Pre-allocate tasks so that we don't have to roll back after the xprt
7103 * has been migrated.
7104 */
7105 new_task = task_new_here();
7106 new_tasklet = tasklet_new();
7107 if (!new_task || !new_tasklet)
7108 goto fail;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007109
7110 if (fd_takeover(conn->handle.fd, conn) != 0)
Willy Tarreau960f37c2023-11-17 10:56:33 +01007111 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02007112
7113 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
7114 /* We failed to takeover the xprt, even if the connection may
7115 * still be valid, flag it as error'd, as we have already
7116 * taken over the fd, and wake the tasklet, so that it will
7117 * destroy it.
7118 */
7119 conn->flags |= CO_FL_ERROR;
7120 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
Willy Tarreau960f37c2023-11-17 10:56:33 +01007121 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02007122 }
7123
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007124 if (h2c->wait_event.events)
7125 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
7126 h2c->wait_event.events, &h2c->wait_event);
Willy Tarreau617e80f2020-07-01 16:39:33 +02007127
7128 task = h2c->task;
7129 if (task) {
Willy Tarreau960f37c2023-11-17 10:56:33 +01007130 /* only assign a task if there was already one, otherwise
7131 * the preallocated new task will be released.
7132 */
Willy Tarreau617e80f2020-07-01 16:39:33 +02007133 task->context = NULL;
7134 h2c->task = NULL;
7135 __ha_barrier_store();
7136 task_kill(task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007137
Willy Tarreau960f37c2023-11-17 10:56:33 +01007138 h2c->task = new_task;
7139 new_task = NULL;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007140 h2c->task->process = h2_timeout_task;
7141 h2c->task->context = h2c;
7142 }
Willy Tarreau960f37c2023-11-17 10:56:33 +01007143
7144 /* To let the tasklet know it should free itself, and do nothing else,
7145 * set its context to NULL.
7146 */
7147 h2c->wait_event.tasklet->context = NULL;
7148 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
7149
7150 h2c->wait_event.tasklet = new_tasklet;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007151 h2c->wait_event.tasklet->process = h2_io_cb;
7152 h2c->wait_event.tasklet->context = h2c;
7153 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
7154 SUB_RETRY_RECV, &h2c->wait_event);
7155
Willy Tarreau960f37c2023-11-17 10:56:33 +01007156 if (new_task)
7157 __task_free(new_task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007158 return 0;
Willy Tarreau960f37c2023-11-17 10:56:33 +01007159 fail:
7160 if (new_task)
7161 __task_free(new_task);
7162 tasklet_free(new_tasklet);
7163 return -1;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007164}
7165
Willy Tarreau62f52692017-10-08 23:01:42 +02007166/*******************************************************/
7167/* functions below are dedicated to the config parsers */
7168/*******************************************************/
7169
Willy Tarreau92f287b2024-03-11 07:33:44 +01007170/* config parser for global "tune.h2.{fe,be}.glitches-threshold" */
7171static int h2_parse_glitches_threshold(char **args, int section_type, struct proxy *curpx,
7172 const struct proxy *defpx, const char *file, int line,
7173 char **err)
7174{
7175 int *vptr;
7176
7177 if (too_many_args(1, args, err, NULL))
7178 return -1;
7179
7180 /* backend/frontend */
7181 vptr = (args[0][8] == 'b') ? &h2_be_glitches_threshold : &h2_fe_glitches_threshold;
7182
7183 *vptr = atoi(args[1]);
7184 if (*vptr < 0) {
7185 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7186 return -1;
7187 }
7188 return 0;
7189}
7190
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007191/* config parser for global "tune.h2.header-table-size" */
7192static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007193 const struct proxy *defpx, const char *file, int line,
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007194 char **err)
7195{
7196 if (too_many_args(1, args, err, NULL))
7197 return -1;
7198
7199 h2_settings_header_table_size = atoi(args[1]);
7200 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
7201 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
7202 return -1;
7203 }
7204 return 0;
7205}
Willy Tarreau62f52692017-10-08 23:01:42 +02007206
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007207/* config parser for global "tune.h2.{be.,fe.,}initial-window-size" */
Willy Tarreaue6baec02017-07-27 11:45:11 +02007208static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007209 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue6baec02017-07-27 11:45:11 +02007210 char **err)
7211{
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007212 int *vptr;
7213
Willy Tarreaue6baec02017-07-27 11:45:11 +02007214 if (too_many_args(1, args, err, NULL))
7215 return -1;
7216
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007217 /* backend/frontend/default */
7218 vptr = (args[0][8] == 'b') ? &h2_be_settings_initial_window_size :
7219 (args[0][8] == 'f') ? &h2_fe_settings_initial_window_size :
7220 &h2_settings_initial_window_size;
7221
7222 *vptr = atoi(args[1]);
7223 if (*vptr < 0) {
Willy Tarreaue6baec02017-07-27 11:45:11 +02007224 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7225 return -1;
7226 }
7227 return 0;
7228}
7229
Willy Tarreauca1027c2023-04-18 15:57:03 +02007230/* config parser for global "tune.h2.{be.,fe.,}max-concurrent-streams" */
Willy Tarreau5242ef82017-07-27 11:47:28 +02007231static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007232 const struct proxy *defpx, const char *file, int line,
Willy Tarreau5242ef82017-07-27 11:47:28 +02007233 char **err)
7234{
Willy Tarreauca1027c2023-04-18 15:57:03 +02007235 uint *vptr;
7236
Willy Tarreau5242ef82017-07-27 11:47:28 +02007237 if (too_many_args(1, args, err, NULL))
7238 return -1;
7239
Willy Tarreauca1027c2023-04-18 15:57:03 +02007240 /* backend/frontend/default */
7241 vptr = (args[0][8] == 'b') ? &h2_be_settings_max_concurrent_streams :
7242 (args[0][8] == 'f') ? &h2_fe_settings_max_concurrent_streams :
7243 &h2_settings_max_concurrent_streams;
7244
7245 *vptr = atoi(args[1]);
7246 if ((int)*vptr < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02007247 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7248 return -1;
7249 }
7250 return 0;
7251}
7252
Willy Tarreau4869ed52023-10-13 18:11:59 +02007253/* config parser for global "tune.h2.fe.max-total-streams" */
7254static int h2_parse_max_total_streams(char **args, int section_type, struct proxy *curpx,
7255 const struct proxy *defpx, const char *file, int line,
7256 char **err)
7257{
7258 uint *vptr;
7259
7260 if (too_many_args(1, args, err, NULL))
7261 return -1;
7262
7263 /* frontend only for now */
7264 vptr = &h2_fe_max_total_streams;
7265
7266 *vptr = atoi(args[1]);
7267 if ((int)*vptr < 0) {
7268 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7269 return -1;
7270 }
7271 return 0;
7272}
7273
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007274/* config parser for global "tune.h2.max-frame-size" */
7275static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007276 const struct proxy *defpx, const char *file, int line,
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007277 char **err)
7278{
7279 if (too_many_args(1, args, err, NULL))
7280 return -1;
7281
7282 h2_settings_max_frame_size = atoi(args[1]);
7283 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
7284 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
7285 return -1;
7286 }
7287 return 0;
7288}
7289
Willy Tarreau62f52692017-10-08 23:01:42 +02007290
7291/****************************************/
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05007292/* MUX initialization and instantiation */
Willy Tarreau62f52692017-10-08 23:01:42 +02007293/***************************************/
7294
7295/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01007296static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02007297 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02007298 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02007299 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02007300 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02007301 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02007302 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02007303 .attach = h2_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02007304 .get_first_sc = h2_get_first_sc,
Willy Tarreau62f52692017-10-08 23:01:42 +02007305 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01007306 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01007307 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01007308 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02007309 .shutr = h2_shutr,
7310 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02007311 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007312 .show_fd = h2_show_fd,
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007313 .show_sd = h2_show_sd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007314 .takeover = h2_takeover,
Christopher Fauleta97cced2022-04-12 18:04:10 +02007315 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Willy Tarreau62f52692017-10-08 23:01:42 +02007316 .name = "H2",
7317};
7318
Christopher Faulet32f61c02018-04-10 14:33:41 +02007319static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02007320 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02007321
Willy Tarreau0108d902018-11-25 19:14:37 +01007322INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
7323
Willy Tarreau62f52692017-10-08 23:01:42 +02007324/* config keyword parsers */
7325static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau92f287b2024-03-11 07:33:44 +01007326 { CFG_GLOBAL, "tune.h2.be.glitches-threshold", h2_parse_glitches_threshold },
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007327 { CFG_GLOBAL, "tune.h2.be.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007328 { CFG_GLOBAL, "tune.h2.be.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau92f287b2024-03-11 07:33:44 +01007329 { CFG_GLOBAL, "tune.h2.fe.glitches-threshold", h2_parse_glitches_threshold },
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007330 { CFG_GLOBAL, "tune.h2.fe.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007331 { CFG_GLOBAL, "tune.h2.fe.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau4869ed52023-10-13 18:11:59 +02007332 { CFG_GLOBAL, "tune.h2.fe.max-total-streams", h2_parse_max_total_streams },
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007333 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02007334 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02007335 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007336 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02007337 { 0, NULL, NULL }
7338}};
7339
Willy Tarreau0108d902018-11-25 19:14:37 +01007340INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007341
7342/* initialize internal structs after the config is parsed.
7343 * Returns zero on success, non-zero on error.
7344 */
7345static int init_h2()
7346{
7347 pool_head_hpack_tbl = create_pool("hpack_tbl",
7348 h2_settings_header_table_size,
7349 MEM_F_SHARED|MEM_F_EXACT);
Christopher Faulet52140992020-11-06 15:23:39 +01007350 if (!pool_head_hpack_tbl) {
7351 ha_alert("failed to allocate hpack_tbl memory pool\n");
7352 return (ERR_ALERT | ERR_FATAL);
7353 }
7354 return ERR_NONE;
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007355}
7356
7357REGISTER_POST_CHECK(init_h2);