blob: 220d1fe7d73308dbf37dd93511c5c96af47cde98 [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);
Christopher Fauletce7f4612024-08-01 16:22:41 +02002207 h2s_propagate_term_flags(h2c, h2s);
Willy Tarreau23482912019-05-07 15:23:14 +02002208
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002209 if (h2s->st < H2_SS_ERROR)
2210 h2s->st = H2_SS_ERROR;
2211 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01002212
2213 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002214 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002215}
2216
2217/* wake the streams attached to the connection, whose id is greater than <last>
2218 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002219 */
Willy Tarreau23482912019-05-07 15:23:14 +02002220static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002221{
2222 struct eb32_node *node;
2223 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002224
Willy Tarreau7838a792019-08-12 18:42:03 +02002225 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
2226
Christopher Fauletf02ca002019-03-07 16:21:34 +01002227 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002228 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
2229 while (node) {
2230 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002231 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002232 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002233 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002234
Christopher Fauletf02ca002019-03-07 16:21:34 +01002235 /* Wake all streams with unassigned ID (ID == 0) */
2236 node = eb32_lookup(&h2c->streams_by_id, 0);
2237 while (node) {
2238 h2s = container_of(node, struct h2s, by_id);
2239 if (h2s->id > 0)
2240 break;
2241 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002242 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002243 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002244
2245 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002246}
2247
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002248/* Wake up all blocked streams whose window size has become positive after the
2249 * mux's initial window was adjusted. This should be done after having processed
2250 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002251 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002252static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02002253{
2254 struct h2s *h2s;
2255 struct eb32_node *node;
2256
Willy Tarreau7838a792019-08-12 18:42:03 +02002257 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2258
Willy Tarreau3421aba2017-07-27 15:41:03 +02002259 node = eb32_first(&h2c->streams_by_id);
2260 while (node) {
2261 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002262 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002263 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002264 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002265 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2266 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002267 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002268 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002269 node = eb32_next(node);
2270 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002271
2272 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002273}
2274
2275/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
2276 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01002277 * return an error in h2c. The caller must have already verified frame length
2278 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002279 */
2280static int h2c_handle_settings(struct h2c *h2c)
2281{
2282 unsigned int offset;
2283 int error;
2284
Willy Tarreau7838a792019-08-12 18:42:03 +02002285 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
2286
Willy Tarreau3421aba2017-07-27 15:41:03 +02002287 if (h2c->dff & H2_F_SETTINGS_ACK) {
2288 if (h2c->dfl) {
2289 error = H2_ERR_FRAME_SIZE_ERROR;
2290 goto fail;
2291 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002292 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002293 }
2294
Willy Tarreau3421aba2017-07-27 15:41:03 +02002295 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002296 if (b_data(&h2c->dbuf) < h2c->dfl) {
2297 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002298 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002299 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002300
2301 /* parse the frame */
2302 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002303 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2304 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002305
2306 switch (type) {
2307 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2308 /* we need to update all existing streams with the
2309 * difference from the previous iws.
2310 */
2311 if (arg < 0) { // RFC7540#6.5.2
2312 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreauf0279f62024-03-11 07:35:19 +01002313 h2c_report_glitch(h2c, 1);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002314 goto fail;
2315 }
Willy Tarreaueec8dc12024-02-08 14:37:56 +01002316 /* Let's count a glitch here in case of a reduction
2317 * after H2_CS_SETTINGS1 because while it's not
Willy Tarreau22892322024-01-17 16:57:23 +01002318 * fundamentally invalid from a protocol's perspective,
2319 * it's often suspicious.
2320 */
Willy Tarreaueec8dc12024-02-08 14:37:56 +01002321 if (h2c->st0 != H2_CS_SETTINGS1 && arg < h2c->miw)
Willy Tarreau92f287b2024-03-11 07:33:44 +01002322 if (h2c_report_glitch(h2c, 1)) {
2323 error = H2_ERR_ENHANCE_YOUR_CALM;
2324 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);
2325 goto fail;
2326 }
Willy Tarreaueec8dc12024-02-08 14:37:56 +01002327
Willy Tarreau3421aba2017-07-27 15:41:03 +02002328 h2c->miw = arg;
2329 break;
2330 case H2_SETTINGS_MAX_FRAME_SIZE:
2331 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
Willy Tarreauf0279f62024-03-11 07:35:19 +01002332 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002333 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 +02002334 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002335 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002336 goto fail;
2337 }
2338 h2c->mfs = arg;
2339 break;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01002340 case H2_SETTINGS_HEADER_TABLE_SIZE:
2341 h2c->flags |= H2_CF_SHTS_UPDATED;
2342 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002343 case H2_SETTINGS_ENABLE_PUSH:
2344 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
Willy Tarreauf0279f62024-03-11 07:35:19 +01002345 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002346 TRACE_ERROR("ENABLE_PUSH out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002347 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002348 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002349 goto fail;
2350 }
2351 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002352 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2353 if (h2c->flags & H2_CF_IS_BACK) {
2354 /* the limit is only for the backend; for the frontend it is our limit */
Willy Tarreauca1027c2023-04-18 15:57:03 +02002355 if ((unsigned int)arg > h2c_max_concurrent_streams(h2c))
2356 arg = h2c_max_concurrent_streams(h2c);
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002357 h2c->streams_limit = arg;
2358 }
2359 break;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002360 case H2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
Amaury Denoyelle0df04362021-10-18 09:43:29 +02002361 if (arg == 1)
2362 h2c->flags |= H2_CF_RCVD_RFC8441;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002363 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002364 }
2365 }
2366
2367 /* need to ACK this frame now */
2368 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002369 done:
2370 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002371 return 1;
2372 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002373 if (!(h2c->flags & H2_CF_IS_BACK))
2374 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002375 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002376 out0:
2377 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 +02002378 return 0;
2379}
2380
2381/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2382 * success or one of the h2_status values.
2383 */
2384static int h2c_ack_settings(struct h2c *h2c)
2385{
2386 struct buffer *res;
2387 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002388 int ret = 0;
2389
2390 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002391
Willy Tarreau9c218e72019-05-26 10:08:28 +02002392 memcpy(str,
2393 "\x00\x00\x00" /* length : 0 (no data) */
2394 "\x04" "\x01" /* type : 4, flags : ACK */
2395 "\x00\x00\x00\x00" /* stream ID */, 9);
2396
Willy Tarreaubcc45952019-05-26 10:05:50 +02002397 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002398 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002399 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002400 h2c->flags |= H2_CF_MUX_MALLOC;
2401 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002402 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002403 }
2404
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002405 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002406 if (unlikely(ret <= 0)) {
2407 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002408 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2409 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002410 h2c->flags |= H2_CF_MUX_MFULL;
2411 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002412 }
2413 else {
2414 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002415 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002416 }
2417 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002418 out:
2419 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002420 return ret;
2421}
2422
Willy Tarreaucf68c782017-10-10 17:11:41 +02002423/* processes a PING frame and schedules an ACK if needed. The caller must pass
2424 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002425 * missing data. The caller must have already verified frame length
2426 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002427 */
2428static int h2c_handle_ping(struct h2c *h2c)
2429{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002430 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002431 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002432 h2c->st0 = H2_CS_FRAME_A;
2433 return 1;
2434}
2435
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002436/* Try to send a window update for stream id <sid> and value <increment>.
2437 * Returns > 0 on success or zero on missing room or failure. It may return an
2438 * error in h2c.
2439 */
2440static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2441{
2442 struct buffer *res;
2443 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002444 int ret = 0;
2445
2446 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002447
Willy Tarreau9c218e72019-05-26 10:08:28 +02002448 /* length: 4, type: 8, flags: none */
2449 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2450 write_n32(str + 5, sid);
2451 write_n32(str + 9, increment);
2452
Willy Tarreaubcc45952019-05-26 10:05:50 +02002453 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002454 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002455 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002456 h2c->flags |= H2_CF_MUX_MALLOC;
2457 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002458 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002459 }
2460
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002461 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002462 if (unlikely(ret <= 0)) {
2463 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002464 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2465 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002466 h2c->flags |= H2_CF_MUX_MFULL;
2467 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002468 }
2469 else {
2470 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002471 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002472 }
2473 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002474 out:
2475 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002476 return ret;
2477}
2478
2479/* try to send pending window update for the connection. It's safe to call it
2480 * with no pending updates. Returns > 0 on success or zero on missing room or
2481 * failure. It may return an error in h2c.
2482 */
2483static int h2c_send_conn_wu(struct h2c *h2c)
2484{
2485 int ret = 1;
2486
Willy Tarreau7838a792019-08-12 18:42:03 +02002487 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2488
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002489 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002490 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002491
Willy Tarreau97aaa672018-12-23 09:49:04 +01002492 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2493 /* increase the advertised connection window to 2G on
2494 * first update.
2495 */
2496 h2c->flags |= H2_CF_WINDOW_OPENED;
2497 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2498 }
2499
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002500 /* send WU for the connection */
2501 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2502 if (ret > 0)
2503 h2c->rcvd_c = 0;
2504
Willy Tarreau7838a792019-08-12 18:42:03 +02002505 out:
2506 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002507 return ret;
2508}
2509
2510/* try to send pending window update for the current dmux stream. It's safe to
2511 * call it with no pending updates. Returns > 0 on success or zero on missing
2512 * room or failure. It may return an error in h2c.
2513 */
2514static int h2c_send_strm_wu(struct h2c *h2c)
2515{
2516 int ret = 1;
2517
Willy Tarreau7838a792019-08-12 18:42:03 +02002518 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2519
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002520 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002521 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002522
2523 /* send WU for the stream */
2524 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2525 if (ret > 0)
2526 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002527 out:
2528 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002529 return ret;
2530}
2531
Willy Tarreaucf68c782017-10-10 17:11:41 +02002532/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2533 * success, 0 on missing data or one of the h2_status values.
2534 */
2535static int h2c_ack_ping(struct h2c *h2c)
2536{
2537 struct buffer *res;
2538 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002539 int ret = 0;
2540
2541 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002542
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002543 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002544 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002545
Willy Tarreaucf68c782017-10-10 17:11:41 +02002546 memcpy(str,
2547 "\x00\x00\x08" /* length : 8 (same payload) */
2548 "\x06" "\x01" /* type : 6, flags : ACK */
2549 "\x00\x00\x00\x00" /* stream ID */, 9);
2550
2551 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002552 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002553
Willy Tarreau9c218e72019-05-26 10:08:28 +02002554 res = br_tail(h2c->mbuf);
2555 retry:
2556 if (!h2_get_buf(h2c, res)) {
2557 h2c->flags |= H2_CF_MUX_MALLOC;
2558 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002559 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002560 }
2561
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002562 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002563 if (unlikely(ret <= 0)) {
2564 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002565 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2566 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002567 h2c->flags |= H2_CF_MUX_MFULL;
2568 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002569 }
2570 else {
2571 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002572 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002573 }
2574 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002575 out:
2576 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002577 return ret;
2578}
2579
Willy Tarreau26f95952017-07-27 17:18:30 +02002580/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2581 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002582 * h2c or h2s. The caller must have already verified frame length and stream ID
2583 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002584 */
2585static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2586{
2587 int32_t inc;
2588 int error;
2589
Willy Tarreau7838a792019-08-12 18:42:03 +02002590 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2591
Willy Tarreau26f95952017-07-27 17:18:30 +02002592 /* process full frame only */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002593 if (b_data(&h2c->dbuf) < h2c->dfl) {
2594 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002595 goto out0;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002596 }
Willy Tarreau26f95952017-07-27 17:18:30 +02002597
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002598 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002599
2600 if (h2c->dsi != 0) {
2601 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002602
2603 /* it's not an error to receive WU on a closed stream */
2604 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002605 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002606
2607 if (!inc) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002608 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002609 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 +02002610 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002611 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002612 goto strm_err;
2613 }
2614
Willy Tarreau22892322024-01-17 16:57:23 +01002615 /* WT: it would be tempting to count a glitch here for very small
2616 * increments (less than a few tens of bytes), but that might be
2617 * perfectly valid for many short streams, so better instead
2618 * count the number of WU per frame maybe. That would be better
2619 * dealt with using scores per frame.
2620 */
2621
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002622 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002623 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002624 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 +02002625 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002626 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002627 goto strm_err;
2628 }
2629
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002630 h2s->sws += inc;
2631 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002632 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002633 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002634 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2635 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002636 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002637 }
2638 }
2639 else {
2640 /* connection window update */
2641 if (!inc) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002642 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002643 TRACE_ERROR("conn WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002644 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002645 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002646 goto conn_err;
2647 }
2648
2649 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01002650 h2c_report_glitch(h2c, 1);
Willy Tarreau04256ce2024-01-17 16:56:18 +01002651 TRACE_ERROR("conn WINDOW_UPDATE inc<0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002652 error = H2_ERR_FLOW_CONTROL_ERROR;
2653 goto conn_err;
2654 }
2655
2656 h2c->mws += inc;
2657 }
2658
Willy Tarreau7838a792019-08-12 18:42:03 +02002659 done:
2660 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002661 return 1;
2662
2663 conn_err:
2664 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002665 out0:
2666 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 +02002667 return 0;
2668
2669 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002670 h2s_error(h2s, error);
2671 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002672 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002673 return 0;
2674}
2675
Willy Tarreaue96b0922017-10-30 00:28:29 +01002676/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002677 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2678 * have already verified frame length and stream ID validity. Described in
2679 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002680 */
2681static int h2c_handle_goaway(struct h2c *h2c)
2682{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002683 int last;
2684
Willy Tarreau7838a792019-08-12 18:42:03 +02002685 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002686 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002687 if (b_data(&h2c->dbuf) < h2c->dfl) {
2688 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002689 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002690 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002691 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002692
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002693 last = h2_get_n32(&h2c->dbuf, 0);
2694 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002695 if (h2c->last_sid < 0)
2696 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002697 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002698 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002699 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002700}
2701
Willy Tarreau92153fc2017-12-03 19:46:19 +01002702/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002703 * invalid. Returns > 0 on success or zero on missing data. It may return an
2704 * error in h2c. The caller must have already verified frame length and stream
2705 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002706 */
2707static int h2c_handle_priority(struct h2c *h2c)
2708{
Willy Tarreau7838a792019-08-12 18:42:03 +02002709 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2710
Willy Tarreau92153fc2017-12-03 19:46:19 +01002711 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002712 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002713 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002714 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002715 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002716 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002717
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002718 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002719 /* 7540#5.3 : can't depend on itself */
Willy Tarreauf0279f62024-03-11 07:35:19 +01002720 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002721 TRACE_ERROR("PRIORITY depends on itself", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002722 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02002723 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002724 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002725 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002726 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002727 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002728 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002729}
2730
Willy Tarreaucd234e92017-08-18 10:59:39 +02002731/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002732 * Returns > 0 on success or zero on missing data. The caller must have already
2733 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002734 */
2735static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2736{
Willy Tarreau7838a792019-08-12 18:42:03 +02002737 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2738
Willy Tarreaucd234e92017-08-18 10:59:39 +02002739 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002740 if (b_data(&h2c->dbuf) < h2c->dfl) {
2741 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 +02002742 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002743 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002744 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002745
2746 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002747 if (h2s->st == H2_SS_CLOSED) {
2748 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 +02002749 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002750 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002751
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002752 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002753 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002754
Willy Tarreau7be4ee02022-05-18 07:31:41 +02002755 if (h2s_sc(h2s)) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02002756 se_fl_set_error(h2s->sd);
Willy Tarreauf830f012018-12-19 17:44:55 +01002757 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002758 }
2759
2760 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002761 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002762 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002763}
2764
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002765/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2766 * It may return an error in h2c or h2s. The caller must consider that the
2767 * return value is the new h2s in case one was allocated (most common case).
2768 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002769 * errors here are reported as connection errors since it's impossible to
2770 * recover from such errors after the compression context has been altered.
2771 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002772static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002773{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002774 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002775 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002776 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 int error;
2778
Willy Tarreau7838a792019-08-12 18:42:03 +02002779 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2780
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002781 if (!b_size(&h2c->dbuf)) {
2782 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002783 goto out; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002784 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002785
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002786 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2787 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002788 goto out; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002789 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002790
2791 /* now either the frame is complete or the buffer is complete */
2792 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002793 /* The stream exists/existed, this must be a trailers frame */
2794 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002795 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &body_len, NULL);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002796 /* unrecoverable error ? */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002797 if (h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau17c630b2023-01-19 23:58:11 +01002798 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 +01002799 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002800 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002801 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002802
Christopher Faulet485da0b2021-10-08 08:56:00 +02002803 if (error == 0) {
2804 /* Demux not blocked because of the stream, it is an incomplete frame */
2805 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2806 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002807 goto out; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02002808 }
Willy Tarreauaab1a602019-05-06 11:12:18 +02002809
2810 if (error < 0) {
2811 /* Failed to decode this frame (e.g. too large request)
2812 * but the HPACK decompressor is still synchronized.
2813 */
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002814 sess_log(h2c->conn->owner);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002815 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau17c630b2023-01-19 23:58:11 +01002816 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 +02002817 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002818 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002819 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002820 goto done;
2821 }
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002822 /* the stream was already killed by an RST, let's consume
Willy Tarreau1f035502019-01-30 11:44:07 +01002823 * the data and send another RST.
2824 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002825 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002826 sess_log(h2c->conn->owner);
Willy Tarreau1f035502019-01-30 11:44:07 +01002827 h2s = (struct h2s*)h2_error_stream;
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002828 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 +01002829 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002830 }
2831 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2832 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2833 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreauf0279f62024-03-11 07:35:19 +01002834 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002835 TRACE_ERROR("HEADERS on invalid stream ID", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau4781b152021-04-06 13:53:36 +02002836 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002837 sess_log(h2c->conn->owner);
Willy Tarreaud6e5cde2023-10-20 18:38:34 +02002838 session_inc_http_req_ctr(h2c->conn->owner);
2839 session_inc_http_err_ctr(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002840 goto conn_err;
2841 }
Willy Tarreau4869ed52023-10-13 18:11:59 +02002842 else if (h2c->flags & H2_CF_DEM_TOOMANY) {
Willy Tarreau36c22322022-05-27 10:41:24 +02002843 goto out; // IDLE but too many sc still present
Willy Tarreau4869ed52023-10-13 18:11:59 +02002844 }
2845 else if (h2_fe_max_total_streams &&
2846 h2c->stream_cnt >= h2_fe_max_total_streams + h2c_max_concurrent_streams(h2c)) {
2847 /* We've already told this client we were going to close a
2848 * while ago and apparently it didn't care, so it's time to
2849 * stop processing its requests for real.
2850 */
2851 error = H2_ERR_ENHANCE_YOUR_CALM;
Willy Tarreauf0279f62024-03-11 07:35:19 +01002852 h2c_report_glitch(h2c, 1);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002853 TRACE_STATE("Stream limit violated", H2_EV_STRM_SHUT, h2c->conn);
2854 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
2855 sess_log(h2c->conn->owner);
2856 session_inc_http_req_ctr(h2c->conn->owner);
2857 session_inc_http_err_ctr(h2c->conn->owner);
2858 goto conn_err;
2859 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002860
Willy Tarreau7cfbb812023-01-26 16:02:01 +01002861 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002862
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002863 if (error == 0) {
2864 /* No error but missing data for demuxing, it is an incomplete frame */
2865 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
2866 h2c->flags |= H2_CF_DEM_SHORT_READ;
2867 goto out;
Willy Tarreauf43f36d2023-01-19 23:22:03 +01002868 }
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002869
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002870 /* Now we cannot roll back and we won't come back here anymore for this
2871 * stream, so this stream ID is open from a protocol perspective, even
2872 * if incomplete or broken, we want to count it as attempted.
2873 */
2874 if (h2c->dsi > h2c->max_id)
2875 h2c->max_id = h2c->dsi;
2876 h2c->stream_cnt++;
Willy Tarreau25919232019-01-03 14:48:18 +01002877
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002878 if (error < 0) {
2879 /* Failed to decode this stream. This might be due to a
2880 * recoverable error affecting only the stream (e.g. too large
2881 * request for buffer, that leaves the HPACK decompressor still
2882 * synchronized), or a non-recoverable error such as an invalid
2883 * frame type sequence (e.g. other frame type interleaved with
2884 * CONTINUATION), in which h2c_dec_hdrs() has already set the
2885 * error code in the connection and counted it in the relevant
2886 * stats. We still count a req error in both cases.
Willy Tarreau25919232019-01-03 14:48:18 +01002887 */
Willy Tarreaufb25b6e2024-01-18 17:01:45 +01002888 sess_log(h2c->conn->owner);
2889 session_inc_http_req_ctr(h2c->conn->owner);
2890 session_inc_http_err_ctr(h2c->conn->owner);
2891
2892 if (h2c->st0 >= H2_CS_ERROR) {
2893 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);
2894 goto out;
2895 }
2896
2897 /* recoverable stream error (e.g. too large request) */
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002898 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 +01002899 goto strm_err;
Willy Tarreau25919232019-01-03 14:48:18 +01002900 }
2901
Willy Tarreau29268e92021-06-17 08:29:14 +02002902 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn, 0, &rxbuf);
2903
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002904 /* Note: we don't emit any other logs below because if we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002905 * positively from h2c_frt_stream_new(), the stream will report the error,
2906 * and if we return in error, h2c_frt_stream_new() will emit the error.
Christopher Faulet7d013e72020-12-15 16:56:50 +01002907 *
2908 * Xfer the rxbuf to the stream. On success, the new stream owns the
2909 * rxbuf. On error, it is released here.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002910 */
Amaury Denoyelle90ac6052021-10-18 14:45:49 +02002911 h2s = h2c_frt_stream_new(h2c, h2c->dsi, &rxbuf, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002912 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002913 h2s = (struct h2s*)h2_refused_stream;
Willy Tarreaudf1cc5d2023-10-20 17:47:33 +02002914 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 +01002915 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002916 }
2917
2918 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002919 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002920 h2s->body_len = body_len;
Christopher Fauletc2f1d0e2023-05-24 11:34:45 +02002921 h2s_propagate_term_flags(h2c, h2s);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002922
Willy Tarreau88d138e2019-01-02 19:38:14 +01002923 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002924 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002925 if (h2s->st == H2_SS_OPEN)
2926 h2s->st = H2_SS_HREM;
2927 else
2928 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002929 }
Willy Tarreau0d6e5d22023-02-20 17:05:10 +01002930 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002931 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002932
2933 conn_err:
2934 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002935 out:
2936 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002937 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 +02002938 h2s = NULL;
2939 goto leave;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002940
Willy Tarreau54310dc2024-01-12 18:36:57 +01002941 strm_err:
Willy Tarreau54310dc2024-01-12 18:36:57 +01002942 h2s = (struct h2s*)h2_error_stream;
2943
Willy Tarreau96a10c22018-12-23 18:30:44 +01002944 send_rst:
2945 /* make the demux send an RST for the current stream. We may only
2946 * do this if we're certain that the HEADERS frame was properly
2947 * decompressed so that the HPACK decoder is still kept up to date.
2948 */
2949 h2_release_buf(h2c, &rxbuf);
2950 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002951
Willy Tarreau7838a792019-08-12 18:42:03 +02002952 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau4869ed52023-10-13 18:11:59 +02002953
2954 leave:
2955 if (h2_fe_max_total_streams && h2c->stream_cnt >= h2_fe_max_total_streams) {
2956 /* we've had enough streams on this connection, time to renew it.
2957 * In order to gracefully do this, we'll advertise a stream limit
2958 * of the current one plus the max concurrent streams value in the
2959 * GOAWAY frame, so that we're certain that the client is aware of
2960 * the limit before creating a new stream, but knows we won't harm
2961 * the streams in flight. Remember that client stream IDs are odd
2962 * so we apply twice the concurrent streams value to the current
2963 * ID.
2964 */
2965 if (h2c->last_sid <= 0 ||
2966 h2c->last_sid > h2c->max_id + 2 * h2c_max_concurrent_streams(h2c)) {
2967 /* not set yet or was too high */
2968 h2c->last_sid = h2c->max_id + 2 * h2c_max_concurrent_streams(h2c);
2969 h2c_send_goaway_error(h2c, NULL);
2970 }
2971 }
2972
Willy Tarreau96a10c22018-12-23 18:30:44 +01002973 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002974}
2975
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002976/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2977 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2978 * errors here are reported as connection errors since it's impossible to
2979 * recover from such errors after the compression context has been altered.
2980 */
2981static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2982{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002983 struct buffer rxbuf = BUF_NULL;
2984 unsigned long long body_len = 0;
2985 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002986 int error;
2987
Willy Tarreau7838a792019-08-12 18:42:03 +02002988 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2989
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002990 if (!b_size(&h2c->dbuf)) {
2991 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002992 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002993 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002994
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002995 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
2996 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02002997 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02002998 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002999
Christopher Faulet6884aa32019-09-23 15:28:20 +02003000 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7cfbb812023-01-26 16:02:01 +01003001 error = h2c_dec_hdrs(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len, h2s->upgrade_protocol);
Christopher Faulet6884aa32019-09-23 15:28:20 +02003002 }
3003 else {
3004 /* the connection was already killed by an RST, let's consume
3005 * the data and send another RST.
3006 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01003007 error = h2c_dec_hdrs(h2c, &rxbuf, &flags, &body_len, NULL);
Christopher Fauletea7a7782019-09-26 16:19:13 +02003008 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003009 h2c->st0 = H2_CS_FRAME_E;
3010 goto send_rst;
3011 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003012
Willy Tarreau25919232019-01-03 14:48:18 +01003013 /* unrecoverable error ? */
Willy Tarreau17c630b2023-01-19 23:58:11 +01003014 if (h2c->st0 >= H2_CS_ERROR) {
3015 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 +02003016 goto fail;
Willy Tarreau17c630b2023-01-19 23:58:11 +01003017 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003018
Willy Tarreau08bb1d62019-01-30 16:55:48 +01003019 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
3020 /* RFC7540#5.1 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003021 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003022 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 +01003023 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3024 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02003025 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003026 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01003027 }
3028
Willy Tarreau25919232019-01-03 14:48:18 +01003029 if (error <= 0) {
Christopher Faulet485da0b2021-10-08 08:56:00 +02003030 if (error == 0) {
3031 /* Demux not blocked because of the stream, it is an incomplete frame */
3032 if (!(h2c->flags &H2_CF_DEM_BLOCK_ANY))
3033 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003034 goto fail; // missing data
Christopher Faulet485da0b2021-10-08 08:56:00 +02003035 }
Willy Tarreau25919232019-01-03 14:48:18 +01003036
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003037 /* stream error : send RST_STREAM */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003038 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 +01003039 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003040 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02003041 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003042 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003043 }
3044
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003045 if (se_fl_test(h2s->sd, SE_FL_ERROR) && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003046 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02003047 else if (h2s->flags & H2_SF_ES_RCVD) {
3048 if (h2s->st == H2_SS_OPEN)
3049 h2s->st = H2_SS_HREM;
3050 else if (h2s->st == H2_SS_HLOC)
3051 h2s_close(h2s);
3052 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003053
Christopher Fauletf95f8762021-01-22 11:59:07 +01003054 /* Unblock busy server h2s waiting for the response headers to validate
3055 * the tunnel establishment or the end of the response of an oborted
3056 * tunnel
3057 */
3058 if ((h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY)) == (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY) ||
3059 (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)) {
3060 TRACE_STATE("Unblock h2s blocked on tunnel establishment/abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3061 h2s->flags &= ~H2_SF_BLK_MBUSY;
3062 }
3063
Willy Tarreau9abb3172021-06-16 18:32:42 +02003064 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 +02003065 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003066 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02003067 fail:
3068 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
3069 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003070
3071 send_rst:
3072 /* make the demux send an RST for the current stream. We may only
3073 * do this if we're certain that the HEADERS frame was properly
3074 * decompressed so that the HPACK decoder is still kept up to date.
3075 */
3076 h2_release_buf(h2c, &rxbuf);
3077 h2c->st0 = H2_CS_FRAME_E;
3078
Willy Tarreau022e5e52020-09-10 09:33:15 +02003079 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 +02003080 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
3081 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003082}
3083
Willy Tarreau454f9052017-10-26 19:40:35 +02003084/* processes a DATA frame. Returns > 0 on success or zero on missing data.
3085 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
3086 */
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003087static int h2c_handle_data(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003088{
3089 int error;
3090
Willy Tarreau7838a792019-08-12 18:42:03 +02003091 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3092
Willy Tarreau454f9052017-10-26 19:40:35 +02003093 /* note that empty DATA frames are perfectly valid and sometimes used
3094 * to signal an end of stream (with the ES flag).
3095 */
3096
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003097 if (!b_size(&h2c->dbuf) && h2c->dfl) {
3098 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003099 goto fail; // empty buffer
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003100 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003101
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003102 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) {
3103 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7838a792019-08-12 18:42:03 +02003104 goto fail; // incomplete frame
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003105 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003106
3107 /* now either the frame is complete or the buffer is complete */
3108
Willy Tarreau454f9052017-10-26 19:40:35 +02003109 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
3110 /* RFC7540#6.1 */
3111 error = H2_ERR_STREAM_CLOSED;
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003112 goto strm_err_wu;
Willy Tarreau454f9052017-10-26 19:40:35 +02003113 }
3114
Christopher Faulet35feba62023-09-13 16:21:58 +02003115 if (!(h2s->flags & H2_SF_HEADERS_RCVD)) {
3116 /* RFC9113#8.1: The header section must be received before the message content */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003117 h2c_report_glitch(h2c, 1);
Christopher Faulet35feba62023-09-13 16:21:58 +02003118 TRACE_ERROR("Unexpected DATA frame before the message headers", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3119 error = H2_ERR_PROTOCOL_ERROR;
3120 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003121 goto strm_err_wu;
Christopher Faulet35feba62023-09-13 16:21:58 +02003122 }
Christopher Faulet4f09ec82019-06-19 09:25:58 +02003123 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003124 /* RFC7540#8.1.2 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003125 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003126 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 +01003127 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003128 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003129 goto strm_err_wu;
Willy Tarreau1915ca22019-01-24 11:49:37 +01003130 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003131 if (!(h2c->flags & H2_CF_IS_BACK) &&
3132 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT) &&
3133 ((h2c->dfl - h2c->dpl) || !(h2c->dff & H2_F_DATA_END_STREAM))) {
3134 /* a tunnel attempt was aborted but the client still try to send some raw data.
3135 * Thus the stream is closed with the CANCEL error. Here we take care it is not
3136 * an empty DATA Frame with the ES flag. The error is only handled if ES was
3137 * already sent to the client because depending on the scheduling, these data may
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003138 * have been sent before the server response but not handle here.
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003139 */
3140 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3141 error = H2_ERR_CANCEL;
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003142 goto strm_err_wu;
Christopher Faulet91b21dc2021-01-22 12:13:15 +01003143 }
Willy Tarreau1915ca22019-01-24 11:49:37 +01003144
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003145 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02003146 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01003147
Willy Tarreau454f9052017-10-26 19:40:35 +02003148 /* call the upper layers to process the frame, then let the upper layer
3149 * notify the stream about any change.
3150 */
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003151 if (!h2s_sc(h2s)) {
Willy Tarreau082c4572019-08-06 10:11:02 +02003152 /* The upper layer has already closed, this may happen on
3153 * 4xx/redirects during POST, or when receiving a response
3154 * from an H2 server after the client has aborted.
3155 */
3156 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003157 goto strm_err;
3158 }
3159
Willy Tarreau8f650c32017-11-21 19:36:21 +01003160 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003161 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01003162
Willy Tarreau721c9742017-11-07 11:05:42 +01003163 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003164 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01003165 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02003166 }
3167
3168 /* check for completion : the callee will change this to FRAME_A or
3169 * FRAME_H once done.
3170 */
3171 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02003172 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02003173
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003174 /* last frame */
3175 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02003176 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01003177 if (h2s->st == H2_SS_OPEN)
3178 h2s->st = H2_SS_HREM;
3179 else
3180 h2s_close(h2s);
3181
Willy Tarreau1915ca22019-01-24 11:49:37 +01003182 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
3183 /* RFC7540#8.1.2 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003184 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003185 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 +01003186 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02003187 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01003188 goto strm_err;
3189 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01003190 }
3191
Christopher Fauletf95f8762021-01-22 11:59:07 +01003192 /* Unblock busy server h2s waiting for the end of the response for an
3193 * aborted tunnel
3194 */
3195 if ((h2c->flags & H2_CF_IS_BACK) &&
3196 (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)) {
3197 TRACE_STATE("Unblock h2s blocked on tunnel abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
3198 h2s->flags &= ~H2_SF_BLK_MBUSY;
3199 }
3200
Willy Tarreau7838a792019-08-12 18:42:03 +02003201 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003202 return 1;
3203
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003204 strm_err_wu:
3205 /* stream error before the frame was taken into account, we're
3206 * going to kill the stream but must still update the connection's
3207 * window.
3208 */
3209 h2c->rcvd_c += h2c->dfl - h2c->dpl;
Willy Tarreau454f9052017-10-26 19:40:35 +02003210 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01003211 h2s_error(h2s, error);
3212 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003213 fail:
3214 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 +02003215 return 0;
3216}
3217
Willy Tarreau63864812019-08-07 14:25:20 +02003218/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
3219 * valid for the current stream state. This is needed only after parsing the
3220 * frame header but in practice it can be performed at any time during
3221 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
3222 * or 0 in case of error, in which case either h2s or h2c will carry an error.
3223 */
3224static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
3225{
Willy Tarreau7838a792019-08-12 18:42:03 +02003226 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
3227
Willy Tarreau63864812019-08-07 14:25:20 +02003228 if (h2s->st == H2_SS_IDLE &&
3229 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
3230 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
3231 * this state MUST be treated as a connection error
3232 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003233 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003234 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 +02003235 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003236 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02003237 /* only log if no other stream can report the error */
3238 sess_log(h2c->conn->owner);
3239 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003240 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02003241 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 +02003242 return 0;
3243 }
3244
Willy Tarreau57a18162019-11-24 14:57:53 +01003245 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
3246 /* only PUSH_PROMISE would be permitted here */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003247 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003248 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 +01003249 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003250 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau57a18162019-11-24 14:57:53 +01003251 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
3252 return 0;
3253 }
3254
Willy Tarreau63864812019-08-07 14:25:20 +02003255 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
3256 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
3257 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
3258 * this state MUST be treated as a stream error.
3259 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
3260 * PUSH_PROMISE/CONTINUATION cause connection errors.
3261 */
Amaury Denoyellea8879232020-10-27 17:16:03 +01003262 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003263 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003264 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 +02003265 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003266 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003267 }
3268 else {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003269 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003270 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003271 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003272 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 +02003273 return 0;
3274 }
3275
3276 /* Below the management of frames received in closed state is a
3277 * bit hackish because the spec makes strong differences between
3278 * streams closed by receiving RST, sending RST, and seeing ES
3279 * in both directions. In addition to this, the creation of a
3280 * new stream reusing the identifier of a closed one will be
3281 * detected here. Given that we cannot keep track of all closed
3282 * streams forever, we consider that unknown closed streams were
3283 * closed on RST received, which allows us to respond with an
3284 * RST without breaking the connection (eg: to abort a transfer).
3285 * Some frames have to be silently ignored as well.
3286 */
3287 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
3288 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
3289 /* #5.1.1: The identifier of a newly
3290 * established stream MUST be numerically
3291 * greater than all streams that the initiating
3292 * endpoint has opened or reserved. This
3293 * governs streams that are opened using a
3294 * HEADERS frame and streams that are reserved
3295 * using PUSH_PROMISE. An endpoint that
3296 * receives an unexpected stream identifier
3297 * MUST respond with a connection error.
3298 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003299 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003300 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003301 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 +02003302 return 0;
3303 }
3304
Willy Tarreau4c08f122019-09-26 08:47:15 +02003305 if (h2s->flags & H2_SF_RST_RCVD &&
3306 !(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 +02003307 /* RFC7540#5.1:closed: an endpoint that
3308 * receives any frame other than PRIORITY after
3309 * receiving a RST_STREAM MUST treat that as a
3310 * stream error of type STREAM_CLOSED.
3311 *
3312 * Note that old streams fall into this category
3313 * and will lead to an RST being sent.
3314 *
3315 * However, we cannot generalize this to all frame types. Those
3316 * carrying compression state must still be processed before
3317 * being dropped or we'll desynchronize the decoder. This can
3318 * happen with request trailers received after sending an
3319 * RST_STREAM, or with header/trailers responses received after
3320 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02003321 *
3322 * In addition, since our CLOSED streams always carry the
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003323 * RST_RCVD bit, we don't want to accidentally catch valid
Willy Tarreau4c08f122019-09-26 08:47:15 +02003324 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02003325 */
Willy Tarreau3a36c9e2024-02-08 15:01:36 +01003326 if (h2c->dft == H2_FT_DATA) {
3327 /* even if we reject out-of-stream DATA, it must
3328 * still count against the connection's flow control.
3329 */
3330 h2c->rcvd_c += h2c->dfl - h2c->dpl;
3331 }
3332
Willy Tarreauf0279f62024-03-11 07:35:19 +01003333 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003334 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3335 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003336 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 +02003337 return 0;
3338 }
3339
3340 /* RFC7540#5.1:closed: if this state is reached as a
3341 * result of sending a RST_STREAM frame, the peer that
3342 * receives the RST_STREAM might have already sent
3343 * frames on the stream that cannot be withdrawn. An
3344 * endpoint MUST ignore frames that it receives on
3345 * closed streams after it has sent a RST_STREAM
3346 * frame. An endpoint MAY choose to limit the period
3347 * over which it ignores frames and treat frames that
3348 * arrive after this time as being in error.
3349 */
3350 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
3351 /* RFC7540#5.1:closed: any frame other than
3352 * PRIO/WU/RST in this state MUST be treated as
3353 * a connection error
3354 */
3355 if (h2c->dft != H2_FT_RST_STREAM &&
3356 h2c->dft != H2_FT_PRIORITY &&
3357 h2c->dft != H2_FT_WINDOW_UPDATE) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003358 h2c_report_glitch(h2c, 1);
Willy Tarreau63864812019-08-07 14:25:20 +02003359 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003360 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 +02003361 return 0;
3362 }
3363 }
3364 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003365 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003366 return 1;
3367}
3368
Willy Tarreaubc933932017-10-09 16:21:43 +02003369/* process Rx frames to be demultiplexed */
3370static void h2_process_demux(struct h2c *h2c)
3371{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003372 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003373 struct h2_fh hdr;
3374 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003375 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02003376
Willy Tarreau7838a792019-08-12 18:42:03 +02003377 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3378
Willy Tarreau081d4722017-05-16 21:51:05 +02003379 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003380 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02003381
3382 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3383 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003384 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02003385 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02003386 goto out;
3387
Willy Tarreau52eed752017-09-22 15:05:09 +02003388 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
3389 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003390 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau22892322024-01-17 16:57:23 +01003391 if (b_data(&h2c->dbuf) ||
3392 !(((const struct session *)h2c->conn->owner)->fe->options & (PR_O_NULLNOLOG|PR_O_IGNORE_PRB)))
Willy Tarreauf0279f62024-03-11 07:35:19 +01003393 h2c_report_glitch(h2c, 1);
Willy Tarreau22892322024-01-17 16:57:23 +01003394
Willy Tarreau7838a792019-08-12 18:42:03 +02003395 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003396 h2c->st0 = H2_CS_ERROR2;
Willy Tarreauee4684f2021-06-17 08:08:48 +02003397 if (b_data(&h2c->dbuf) ||
Christopher Faulet3f35da22021-07-26 10:18:35 +02003398 !(((const struct session *)h2c->conn->owner)->fe->options & (PR_O_NULLNOLOG|PR_O_IGNORE_PRB)))
Willy Tarreauee4684f2021-06-17 08:08:48 +02003399 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003400 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003401 goto done;
Willy Tarreau52eed752017-09-22 15:05:09 +02003402 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003403 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003404
3405 h2c->max_id = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02003406 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaued2b9d92022-08-18 15:30:41 +02003407 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau52eed752017-09-22 15:05:09 +02003408 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003409
3410 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003411 /* ensure that what is pending is a valid SETTINGS frame
3412 * without an ACK.
3413 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003414 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 +02003415 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003416 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003417 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau22de8d32018-09-05 19:55:58 +02003418 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003419 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003420 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 +02003421 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003422 if (!(h2c->flags & H2_CF_IS_BACK))
3423 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003424 }
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003425 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003426 }
3427
3428 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
3429 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003430 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003431 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 +02003432 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3433 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003434 if (!(h2c->flags & H2_CF_IS_BACK))
3435 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003436 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003437 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003438 }
3439
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003440 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003441 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003442 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003443 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 +02003444 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3445 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003446 if (!(h2c->flags & H2_CF_IS_BACK))
3447 sess_log(h2c->conn->owner);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003448 goto done;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003449 }
3450
Willy Tarreau3bf69182018-12-21 15:34:50 +01003451 /* that's OK, switch to FRAME_P to process it. This is
3452 * a SETTINGS frame whose header has already been
3453 * deleted above.
3454 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01003455 padlen = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +02003456 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003457 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003458 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003459 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003460
3461 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02003462 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02003463 int ret = 0;
3464
Willy Tarreau7838a792019-08-12 18:42:03 +02003465 if (!b_data(&h2c->dbuf)) {
3466 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003467 h2c->flags |= H2_CF_DEM_SHORT_READ;
3468 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003469 }
3470
3471 if (h2c->st0 >= H2_CS_ERROR) {
3472 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003473 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003474 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003475
3476 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003477 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003478 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) {
3479 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003480 break;
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003481 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003482
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003483 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003484 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003485 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 +02003486 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003487 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02003488 /* only log if no other stream can report the error */
3489 sess_log(h2c->conn->owner);
3490 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003491 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003492 break;
3493 }
3494
Willy Tarreau617592c2022-06-08 16:32:22 +02003495 if (h2c->rcvd_s && h2c->dsi != hdr.sid) {
3496 /* changed stream with a pending WU, need to
3497 * send it now.
3498 */
3499 TRACE_PROTO("sending stream WINDOW_UPDATE frame on stream switch", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
3500 ret = h2c_send_strm_wu(h2c);
3501 if (ret <= 0)
3502 break;
3503 }
3504
Christopher Fauletdd2a5622019-06-18 12:22:38 +02003505 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003506 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
3507 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
3508 * we read the pad length and drop it from the remaining
3509 * payload (one byte + the 9 remaining ones = 10 total
3510 * removed), so we have a frame payload starting after the
3511 * pad len. Flow controlled frames (DATA) also count the
3512 * padlen in the flow control, so it must be adjusted.
3513 */
3514 if (hdr.len < 1) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003515 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003516 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 +01003517 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003518 if (!(h2c->flags & H2_CF_IS_BACK))
3519 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003520 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003521 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003522 }
3523 hdr.len--;
3524
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003525 if (b_data(&h2c->dbuf) < 10) {
3526 h2c->flags |= H2_CF_DEM_SHORT_READ;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003527 break; // missing padlen
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003528 }
Willy Tarreau3bf69182018-12-21 15:34:50 +01003529
3530 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3531
3532 if (padlen > hdr.len) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003533 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003534 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 +01003535 /* RFC7540#6.1 : pad length = length of
3536 * frame payload or greater => error.
3537 */
3538 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003539 if (!(h2c->flags & H2_CF_IS_BACK))
3540 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003541 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003542 goto done;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003543 }
3544
3545 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3546 h2c->rcvd_c++;
3547 h2c->rcvd_s++;
3548 }
3549 b_del(&h2c->dbuf, 1);
3550 }
3551 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003552
3553 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003554 h2c->dfl = hdr.len;
3555 h2c->dsi = hdr.sid;
3556 h2c->dft = hdr.ft;
3557 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003558 h2c->dpl = padlen;
Willy Tarreau0f458712022-08-18 11:19:57 +02003559 h2c->flags |= H2_CF_DEM_IN_PROGRESS;
Willy Tarreau73db4342019-09-25 07:28:44 +02003560 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 +02003561 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003562
3563 /* check for minimum basic frame format validity */
3564 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3565 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01003566 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003567 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 +01003568 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003569 if (!(h2c->flags & H2_CF_IS_BACK))
3570 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003571 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003572 goto done;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003573 }
Willy Tarreau15a47332022-03-18 15:57:34 +01003574
3575 /* transition to HEADERS frame ends the keep-alive idle
Willy Tarreauf279a2f2023-05-30 15:42:35 +02003576 * timer and starts the http-request idle delay. It uses
3577 * the idle_start timer as well.
Willy Tarreau15a47332022-03-18 15:57:34 +01003578 */
3579 if (hdr.ft == H2_FT_HEADERS)
3580 h2c->idle_start = now_ms;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003581 }
3582
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003583 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3584 * H2_CS_FRAME_P indicates an incomplete previous operation
3585 * (most often the first attempt) and requires some validity
3586 * checks for the frame and the current state. The two other
3587 * ones are set after completion (or abortion) and must skip
3588 * validity checks.
3589 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003590 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3591
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003592 if (tmp_h2s != h2s && h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003593 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003594 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003595 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003596 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003597 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003598 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003599 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 +02003600 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003601 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003602 }
3603 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003604
Willy Tarreau63864812019-08-07 14:25:20 +02003605 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003606 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3607 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003608 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003609 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003610
Willy Tarreau7e98c052017-10-10 15:56:59 +02003611 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003612 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003613 if (h2c->st0 == H2_CS_FRAME_P) {
3614 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003615 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003616 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003617 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003618
Willy Tarreau7838a792019-08-12 18:42:03 +02003619 if (h2c->st0 == H2_CS_FRAME_A) {
3620 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 +02003621 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003622 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003623 break;
3624
Willy Tarreaucf68c782017-10-10 17:11:41 +02003625 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003626 if (h2c->st0 == H2_CS_FRAME_P) {
3627 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003628 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003629 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003630
Willy Tarreau7838a792019-08-12 18:42:03 +02003631 if (h2c->st0 == H2_CS_FRAME_A) {
3632 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 +02003633 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003634 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003635 break;
3636
Willy Tarreau26f95952017-07-27 17:18:30 +02003637 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003638 if (h2c->st0 == H2_CS_FRAME_P) {
3639 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 +02003640 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003641 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003642 break;
3643
Willy Tarreau61290ec2017-10-17 08:19:21 +02003644 case H2_FT_CONTINUATION:
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003645 /* RFC7540#6.10: CONTINUATION may only be preceded by
Willy Tarreauea18f862018-12-22 20:19:26 +01003646 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3647 * frames' parsers consume all following CONTINUATION
3648 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003649 */
Willy Tarreauf0279f62024-03-11 07:35:19 +01003650 h2c_report_glitch(h2c, 1);
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003651 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 +01003652 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003653 if (!(h2c->flags & H2_CF_IS_BACK))
3654 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003655 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003656 goto done;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003657
Willy Tarreau13278b42017-10-13 19:23:14 +02003658 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003659 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003660 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003661 if (h2c->flags & H2_CF_IS_BACK)
3662 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3663 else
3664 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003665 if (tmp_h2s) {
3666 h2s = tmp_h2s;
3667 ret = 1;
3668 }
3669 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003670 HA_ATOMIC_INC(&h2c->px_counters->headers_rcvd);
Willy Tarreau13278b42017-10-13 19:23:14 +02003671 break;
3672
Willy Tarreau454f9052017-10-26 19:40:35 +02003673 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003674 if (h2c->st0 == H2_CS_FRAME_P) {
3675 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003676 ret = h2c_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003677 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003678 HA_ATOMIC_INC(&h2c->px_counters->data_rcvd);
Willy Tarreau454f9052017-10-26 19:40:35 +02003679
Willy Tarreau7838a792019-08-12 18:42:03 +02003680 if (h2c->st0 == H2_CS_FRAME_A) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003681 /* rcvd_s will suffice to trigger the sending of a WU */
3682 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7838a792019-08-12 18:42:03 +02003683 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003684 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003685
Willy Tarreau92153fc2017-12-03 19:46:19 +01003686 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003687 if (h2c->st0 == H2_CS_FRAME_P) {
3688 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003689 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003690 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003691 break;
3692
Willy Tarreaucd234e92017-08-18 10:59:39 +02003693 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003694 if (h2c->st0 == H2_CS_FRAME_P) {
3695 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 +02003696 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003697 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003698 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_rcvd);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003699 break;
3700
Willy Tarreaue96b0922017-10-30 00:28:29 +01003701 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003702 if (h2c->st0 == H2_CS_FRAME_P) {
3703 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003704 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003705 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003706 HA_ATOMIC_INC(&h2c->px_counters->goaway_rcvd);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003707 break;
3708
Willy Tarreau1c661982017-10-30 13:52:01 +01003709 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003710 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003711 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003712 /* drop frames that we ignore. They may be larger than
3713 * the buffer so we drain all of their contents until
3714 * we reach the end.
3715 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003716 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3717 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003718 h2c->dfl -= ret;
3719 ret = h2c->dfl == 0;
3720 }
3721
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003722 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003723 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003724 if (h2s->st == H2_SS_ERROR) {
3725 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 +01003726 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003727 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003728
Willy Tarreau7838a792019-08-12 18:42:03 +02003729 if (h2c->st0 == H2_CS_FRAME_E) {
3730 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 +01003731 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003732 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003733
Willy Tarreau7e98c052017-10-10 15:56:59 +02003734 /* error or missing data condition met above ? */
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003735 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02003736 break;
3737
3738 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaubba7a4d2020-09-18 07:41:28 +02003739 if (h2c->dfl)
3740 TRACE_DEVEL("skipping remaining frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
Christopher Faulet5112a602019-09-26 16:38:28 +02003741 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3742 b_del(&h2c->dbuf, ret);
3743 h2c->dfl -= ret;
3744 if (!h2c->dfl) {
Willy Tarreau0f458712022-08-18 11:19:57 +02003745 h2c->flags &= ~H2_CF_DEM_IN_PROGRESS;
Christopher Faulet5112a602019-09-26 16:38:28 +02003746 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3747 h2c->st0 = H2_CS_FRAME_H;
Christopher Faulet5112a602019-09-26 16:38:28 +02003748 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003749 }
3750 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003751
Willy Tarreau617592c2022-06-08 16:32:22 +02003752 if (h2c->rcvd_s > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003753 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau617592c2022-06-08 16:32:22 +02003754 TRACE_PROTO("sending stream WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn, h2s);
3755 h2c_send_strm_wu(h2c);
3756 }
3757
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003758 if (h2c->rcvd_c > 0 &&
Christopher Faulet68ee7842022-10-12 10:21:33 +02003759 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003760 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003761 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003762 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003763
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003764 done:
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003765 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_DEM_SHORT_READ)) {
3766 if (h2c->flags & H2_CF_RCVD_SHUT)
3767 h2c->flags |= H2_CF_END_REACHED;
3768 }
3769
Willy Tarreau7be4ee02022-05-18 07:31:41 +02003770 if (h2s && h2s_sc(h2s) &&
Willy Tarreau567beb82018-12-18 16:52:44 +01003771 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003772 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003773 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003774 (h2s->flags & H2_SF_ES_RCVD) ||
Willy Tarreau95acc8b2022-05-27 16:14:10 +02003775 se_fl_test(h2s->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003776 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003777 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 +02003778 se_fl_set(h2s->sd, SE_FL_RCV_MORE);
Willy Tarreau7e094452018-12-19 18:08:52 +01003779 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003780 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003781
Willy Tarreau7838a792019-08-12 18:42:03 +02003782 if (old_iw != h2c->miw) {
3783 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003784 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003785 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003786
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003787 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003788 out:
3789 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003790 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02003791}
3792
Willy Tarreau989539b2020-01-10 17:01:29 +01003793/* resume each h2s eligible for sending in list head <head> */
3794static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3795{
3796 struct h2s *h2s, *h2s_back;
3797
3798 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3799
3800 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3801 if (h2c->mws <= 0 ||
3802 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3803 h2c->st0 >= H2_CS_ERROR)
3804 break;
3805
3806 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003807
Willy Tarreaud9464162020-01-10 18:25:07 +01003808 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003809 continue;
3810
Willy Tarreau5723f292020-01-10 15:16:57 +01003811 /* If the sender changed his mind and unsubscribed, let's just
3812 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003813 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003814 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3815 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003816 LIST_DEL_INIT(&h2s->list);
3817 continue;
3818 }
3819
Willy Tarreauf96508a2020-01-10 11:12:48 +01003820 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003821 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003822 tasklet_wakeup(h2s->subs->tasklet);
3823 h2s->subs->events &= ~SUB_RETRY_SEND;
3824 if (!h2s->subs->events)
3825 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003826 }
3827 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3828 tasklet_wakeup(h2s->shut_tl);
3829 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003830 }
3831
3832 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3833}
3834
Willy Tarreaude4a5382023-10-17 08:25:19 +02003835/* removes a stream from the list it may be in. If a stream has recently been
3836 * appended to the send_list, it might have been waiting on this one when
3837 * entering h2_snd_buf() and expecting it to complete before starting to send
3838 * in turn. For this reason we check (and clear) H2_CF_WAIT_INLIST to detect
3839 * this condition, and we try to resume sending streams if it happens. Note
3840 * that we don't need to do it for fctl_list as this list is relevant before
3841 * (only consulted after) a window update on the connection, and not because
3842 * of any competition with other streams.
3843 */
3844static inline void h2_remove_from_list(struct h2s *h2s)
3845{
3846 struct h2c *h2c = h2s->h2c;
3847
3848 if (!LIST_INLIST(&h2s->list))
3849 return;
3850
3851 LIST_DEL_INIT(&h2s->list);
3852 if (h2c->flags & H2_CF_WAIT_INLIST) {
3853 h2c->flags &= ~H2_CF_WAIT_INLIST;
3854 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
3855 }
3856}
3857
Willy Tarreaubc933932017-10-09 16:21:43 +02003858/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3859 * the end.
3860 */
3861static int h2_process_mux(struct h2c *h2c)
3862{
Willy Tarreau7838a792019-08-12 18:42:03 +02003863 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3864
Willy Tarreau01b44822018-10-03 14:26:37 +02003865 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3866 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3867 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3868 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003869 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003870 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003871 goto fail;
3872 }
3873 h2c->st0 = H2_CS_SETTINGS1;
3874 }
3875 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003876 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003877 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003878 }
3879
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003880 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003881 if (h2c->rcvd_s > 0 &&
3882 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3883 h2c_send_strm_wu(h2c) < 0)
3884 goto fail;
3885
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003886 if (h2c->rcvd_c > 0 &&
3887 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3888 h2c_send_conn_wu(h2c) < 0)
3889 goto fail;
3890
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003891 /* First we always process the flow control list because the streams
3892 * waiting there were already elected for immediate emission but were
3893 * blocked just on this.
3894 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02003895 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01003896 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3897 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003898
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003899 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003900 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003901 if (h2c->st0 == H2_CS_ERROR) {
3902 if (h2c->max_id >= 0) {
3903 h2c_send_goaway_error(h2c, NULL);
3904 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003905 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003906 }
3907
3908 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3909 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003910 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003911 done:
3912 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3913 return 1;
3914 out0:
3915 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3916 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003917}
3918
Willy Tarreau62f52692017-10-08 23:01:42 +02003919
Willy Tarreau479998a2018-11-18 06:30:59 +01003920/* Attempt to read data, and subscribe if none available.
3921 * The function returns 1 if data has been received, otherwise zero.
3922 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003923static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003924{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003925 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003926 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003927 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003928 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003929
Willy Tarreau7838a792019-08-12 18:42:03 +02003930 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3931
3932 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3933 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003934 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003935 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003936
Willy Tarreau7838a792019-08-12 18:42:03 +02003937 if (!h2_recv_allowed(h2c)) {
3938 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003939 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003940 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003941
Willy Tarreau44e973f2018-03-01 17:49:30 +01003942 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003943 if (!buf) {
3944 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003945 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003946 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003947 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003948
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003949 if (!b_data(buf)) {
3950 /* try to pre-align the buffer like the
3951 * rxbufs will be to optimize memory copies. We'll make
3952 * sure that the frame header lands at the end of the
3953 * HTX block to alias it upon recv. We cannot use the
3954 * head because rcv_buf() will realign the buffer if
3955 * it's empty. Thus we cheat and pretend we already
3956 * have a few bytes there.
3957 */
3958 max = buf_room_for_htx_data(buf) + 9;
3959 buf->head = sizeof(struct htx) - 9;
3960 }
3961 else
3962 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003963
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003964 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003965
Christopher Fauletde9d6052021-04-23 12:25:18 +02003966 if (max && !ret && h2_recv_allowed(h2c)) {
3967 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
3968 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003969 } else if (ret) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02003970 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn, 0, 0, (void*)(long)ret);
Christopher Fauletb5f7b522021-07-26 12:06:53 +02003971 h2c->flags &= ~H2_CF_DEM_SHORT_READ;
3972 }
Olivier Houchard81a15af2018-10-19 17:26:49 +02003973
Christopher Fauletde9d6052021-04-23 12:25:18 +02003974 if (conn_xprt_read0_pending(h2c->conn)) {
3975 TRACE_DATA("received read0", H2_EV_H2C_RECV, h2c->conn);
3976 h2c->flags |= H2_CF_RCVD_SHUT;
3977 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02003978 if (h2c->conn->flags & CO_FL_ERROR) {
3979 TRACE_DATA("connection error", H2_EV_H2C_RECV, h2c->conn);
3980 h2c->flags |= H2_CF_ERROR;
3981 }
Christopher Fauletde9d6052021-04-23 12:25:18 +02003982
Olivier Houcharda1411e62018-08-17 18:42:48 +02003983 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003984 h2_release_buf(h2c, &h2c->dbuf);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003985 goto end;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003986 }
3987
Willy Tarreau7838a792019-08-12 18:42:03 +02003988 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003989 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003990 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003991 }
3992
Christopher Fauletff7925d2022-10-11 19:12:40 +02003993 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02003994 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02003995 return !!ret || (h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERROR));
Willy Tarreau62f52692017-10-08 23:01:42 +02003996}
3997
Willy Tarreau479998a2018-11-18 06:30:59 +01003998/* Try to send data if possible.
3999 * The function returns 1 if data have been sent, otherwise zero.
4000 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02004001static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02004002{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004003 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02004004 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02004005 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02004006
Willy Tarreau7838a792019-08-12 18:42:03 +02004007 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02004008
Christopher Fauletff7925d2022-10-11 19:12:40 +02004009 if (h2c->flags & (H2_CF_ERROR|H2_CF_ERR_PENDING)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004010 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02004011 if (h2c->flags & H2_CF_RCVD_SHUT)
4012 h2c->flags |= H2_CF_ERROR;
4013 b_reset(br_tail(h2c->mbuf));
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004014 h2c->idle_start = now_ms;
Willy Tarreau7838a792019-08-12 18:42:03 +02004015 return 1;
4016 }
Olivier Houchard7505f942018-08-21 18:10:44 +02004017
Willy Tarreaubc933932017-10-09 16:21:43 +02004018 /* This loop is quite simple : it tries to fill as much as it can from
4019 * pending streams into the existing buffer until it's reportedly full
4020 * or the end of send requests is reached. Then it tries to send this
4021 * buffer's contents out, marks it not full if at least one byte could
4022 * be sent, and tries again.
4023 *
4024 * The snd_buf() function normally takes a "flags" argument which may
4025 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
4026 * data immediately comes and CO_SFL_STREAMER to indicate that the
4027 * connection is streaming lots of data (used to increase TLS record
4028 * size at the expense of latency). The former can be sent any time
4029 * there's a buffer full flag, as it indicates at least one stream
4030 * attempted to send and failed so there are pending data. An
4031 * alternative would be to set it as long as there's an active stream
4032 * but that would be problematic for ACKs until we have an absolute
4033 * guarantee that all waiters have at least one byte to send. The
4034 * latter should possibly not be set for now.
4035 */
4036
4037 done = 0;
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004038 while (!(conn->flags & CO_FL_WAIT_XPRT) && !done) {
Willy Tarreaubc933932017-10-09 16:21:43 +02004039 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004040 unsigned int released = 0;
4041 struct buffer *buf;
Willy Tarreau464fa062023-03-17 16:09:14 +01004042 uint to_send;
Willy Tarreaubc933932017-10-09 16:21:43 +02004043
4044 /* fill as much as we can into the current buffer */
4045 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
4046 done = h2_process_mux(h2c);
4047
Olivier Houchard2b094432019-01-29 18:28:36 +01004048 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004049 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01004050
Christopher Faulet9a3d3fc2020-10-22 16:24:58 +02004051 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
Willy Tarreaue6dc7a02021-10-21 17:30:06 +02004052 (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02004053 break;
4054
Christopher Faulet68ee7842022-10-12 10:21:33 +02004055 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))
Willy Tarreaubc933932017-10-09 16:21:43 +02004056 flags |= CO_SFL_MSG_MORE;
4057
Willy Tarreau464fa062023-03-17 16:09:14 +01004058 to_send = br_count(h2c->mbuf);
4059 if (to_send > 1) {
Willy Tarreau14ea98a2023-03-16 17:30:30 +01004060 /* usually we want to emit small TLS records to speed
4061 * up the decoding on the client. That's what is being
4062 * done by default. However if there is more than one
4063 * buffer being allocated, we're streaming large data
4064 * so we stich to large records.
4065 */
4066 flags |= CO_SFL_STREAMER;
4067 }
4068
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004069 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4070 if (b_data(buf)) {
Willy Tarreau464fa062023-03-17 16:09:14 +01004071 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf),
4072 flags | (to_send > 1 ? CO_SFL_MSG_MORE : 0));
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004073 if (!ret) {
4074 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004075 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004076 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004077 sent = 1;
Willy Tarreau464fa062023-03-17 16:09:14 +01004078 to_send--;
Willy Tarreau022e5e52020-09-10 09:33:15 +02004079 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn, 0, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004080 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004081 if (b_data(buf)) {
4082 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004083 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02004084 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004085 }
4086 b_free(buf);
4087 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02004088 }
Willy Tarreaubc933932017-10-09 16:21:43 +02004089
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004090 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004091 offer_buffers(NULL, released);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004092
Willy Tarreau93c55112023-03-16 16:47:44 +01004093 /* Normally if wrote at least one byte, the buffer is not full
4094 * anymore. However, if it was marked full because all of its
4095 * buffers were used, we don't want to instantly wake up many
4096 * streams because we'd create a thundering herd effect, notably
4097 * when data are flushed in small chunks. Instead we wait for
4098 * the buffer to be decongested again before allowing to send
4099 * again. It also has the added benefit of not pumping more
4100 * data from the other side when it's known that this one is
4101 * still congested.
4102 */
4103 if (sent && br_single(h2c->mbuf))
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02004104 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02004105 }
4106
Christopher Fauletff7925d2022-10-11 19:12:40 +02004107 if (conn->flags & CO_FL_ERROR) {
4108 h2c->flags |= H2_CF_ERR_PENDING;
4109 if (h2c->flags & H2_CF_RCVD_SHUT)
4110 h2c->flags |= H2_CF_ERROR;
Willy Tarreau51330962019-05-26 09:38:07 +02004111 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02004112 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02004113
Olivier Houchard6ff20392018-07-17 18:46:31 +02004114 /* We're not full anymore, so we can wake any task that are waiting
4115 * for us.
4116 */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004117 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
4118 h2c->flags &= ~H2_CF_WAIT_INLIST;
Willy Tarreau989539b2020-01-10 17:01:29 +01004119 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02004120 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01004121
Olivier Houchard910b2bc2018-07-17 18:49:38 +02004122 /* We're done, no more to send */
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004123 if (!(conn->flags & CO_FL_WAIT_XPRT) && !br_data(h2c->mbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004124 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Willy Tarreau936db562023-10-18 11:39:43 +02004125 if (h2c->flags & H2_CF_MBUF_HAS_DATA && !h2c->nb_sc) {
4126 h2c->flags &= ~H2_CF_MBUF_HAS_DATA;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004127 h2c->idle_start = now_ms;
Willy Tarreau936db562023-10-18 11:39:43 +02004128 }
Christopher Fauletff7925d2022-10-11 19:12:40 +02004129 goto end;
Willy Tarreau7838a792019-08-12 18:42:03 +02004130 }
Christopher Faulet21fb6bd2023-03-28 12:16:53 +02004131
Willy Tarreau7838a792019-08-12 18:42:03 +02004132 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
4133 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004134 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02004135 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004136 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Christopher Fauletff7925d2022-10-11 19:12:40 +02004137end:
4138 return sent || (h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR));
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004139}
4140
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004141/* this is the tasklet referenced in h2c->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004142struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004143{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004144 struct connection *conn;
4145 struct tasklet *tl = (struct tasklet *)t;
4146 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004147 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02004148 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02004149
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004150 if (state & TASK_F_USR1) {
4151 /* the tasklet was idling on an idle connection, it might have
4152 * been stolen, let's be careful!
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004153 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004154 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4155 if (t->context == NULL) {
4156 /* The connection has been taken over by another thread,
4157 * we're no longer responsible for it, so just free the
4158 * tasklet, and do nothing.
4159 */
4160 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4161 tasklet_free(tl);
Willy Tarreau74163142021-03-13 11:30:19 +01004162 t = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004163 goto leave;
4164 }
4165 conn = h2c->conn;
4166 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004167
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004168 /* Remove the connection from the list, to be sure nobody attempts
4169 * to use it while we handle the I/O events
4170 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004171 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004172 if (conn_in_list)
4173 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004174
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004175 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4176 } else {
4177 /* we're certain the connection was not in an idle list */
4178 conn = h2c->conn;
4179 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4180 conn_in_list = 0;
4181 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004182
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004183 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02004184 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004185 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02004186 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01004187 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004188 ret = h2_process(h2c);
4189
4190 /* If we were in an idle list, we want to add it back into it,
4191 * unless h2_process() returned -1, which mean it has destroyed
4192 * the connection (testing !ret is enough, if h2_process() wasn't
4193 * called then ret will be 0 anyway.
4194 */
Willy Tarreau74163142021-03-13 11:30:19 +01004195 if (ret < 0)
4196 t = NULL;
4197
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004198 if (!ret && conn_in_list) {
4199 struct server *srv = objt_server(conn->target);
4200
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004201 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004202 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02004203 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004204 else
Willy Tarreau85223482022-09-29 20:32:43 +02004205 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004206 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004207 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004208
Willy Tarreau38468772020-06-28 00:31:13 +02004209leave:
Willy Tarreau7838a792019-08-12 18:42:03 +02004210 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreau74163142021-03-13 11:30:19 +01004211 return t;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004212}
Willy Tarreaua2af5122017-10-09 11:56:46 +02004213
Willy Tarreau62f52692017-10-08 23:01:42 +02004214/* callback called on any event by the connection handler.
4215 * It applies changes and returns zero, or < 0 if it wants immediate
4216 * destruction of the connection (which normally doesn not happen in h2).
4217 */
Olivier Houchard7505f942018-08-21 18:10:44 +02004218static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02004219{
Olivier Houchard7505f942018-08-21 18:10:44 +02004220 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02004221
Willy Tarreau7838a792019-08-12 18:42:03 +02004222 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4223
Willy Tarreauf0961222021-02-05 11:41:46 +01004224 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) &&
4225 (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01004226 h2_process_demux(h2c);
4227
Christopher Fauletff7925d2022-10-11 19:12:40 +02004228 if (h2c->st0 >= H2_CS_ERROR || (h2c->flags & H2_CF_ERROR))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004229 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004230
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004231 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01004232 h2c->flags &= ~H2_CF_DEM_DFULL;
4233 }
Olivier Houchard7505f942018-08-21 18:10:44 +02004234 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01004235
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004236 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 +02004237 int send_goaway = 1;
4238 /* If a close-spread-time option is set, we want to avoid
4239 * closing all the active HTTP2 connections at once so we add a
4240 * random factor that will spread the closing.
4241 */
4242 if (tick_isset(global.close_spread_end)) {
4243 int remaining_window = tick_remain(now_ms, global.close_spread_end);
4244 if (remaining_window) {
4245 /* This should increase the closing rate the
4246 * further along the window we are. */
4247 send_goaway = (remaining_window <= statistical_prng_range(global.close_spread_time));
4248 }
4249 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02004250 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)
4251 send_goaway = 0; /* let the client close his connection himself */
Willy Tarreau8ec14062017-12-30 18:08:13 +01004252 /* frontend is stopping, reload likely in progress, let's try
4253 * to announce a graceful shutdown if not yet done. We don't
4254 * care if it fails, it will be tried again later.
4255 */
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02004256 if (send_goaway) {
4257 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
4258 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
4259 if (h2c->last_sid < 0)
4260 h2c->last_sid = (1U << 31) - 1;
4261 h2c_send_goaway_error(h2c, NULL);
4262 }
Willy Tarreau8ec14062017-12-30 18:08:13 +01004263 }
4264 }
4265
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004266 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004267 * If we received early data, and the handshake is done, wake
4268 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004269 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004270 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01004271 (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 +01004272 struct eb32_node *node;
4273 struct h2s *h2s;
4274
4275 h2c->flags |= H2_CF_WAIT_FOR_HS;
4276 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
4277
4278 while (node) {
4279 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004280 if (se_fl_test(h2s->sd, SE_FL_WAIT_FOR_HS))
Willy Tarreau7e094452018-12-19 18:08:52 +01004281 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004282 node = eb32_next(node);
4283 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01004284 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01004285
Christopher Fauletff7925d2022-10-11 19:12:40 +02004286 if ((h2c->flags & H2_CF_ERROR) || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01004287 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
4288 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
4289 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02004290 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004291
4292 if (eb_is_empty(&h2c->streams_by_id)) {
4293 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02004294 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004295 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004296 return -1;
4297 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004298
4299 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004300 if (conn->flags & CO_FL_LIST_MASK) {
4301 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004302 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004303 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4304 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004305 }
4306 else if (h2c->st0 == H2_CS_ERROR) {
4307 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004308 if (conn->flags & CO_FL_LIST_MASK) {
4309 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004310 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004311 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4312 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004313 }
4314
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004315 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01004316 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02004317
Christopher Fauletff7925d2022-10-11 19:12:40 +02004318 if (h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Olivier Houchard53216e72018-10-10 15:46:36 +02004319 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02004320 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02004321 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
4322 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02004323 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02004324
Willy Tarreau15a47332022-03-18 15:57:34 +01004325 h2c_update_timeout(h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02004326 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004327 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004328 return 0;
4329}
4330
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004331/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004332static int h2_wake(struct connection *conn)
4333{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004334 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02004335 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004336
Willy Tarreau7838a792019-08-12 18:42:03 +02004337 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
4338 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01004339 if (ret >= 0)
4340 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02004341 TRACE_LEAVE(H2_EV_H2C_WAKE);
4342 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004343}
4344
Willy Tarreauea392822017-10-31 10:02:25 +01004345/* Connection timeout management. The principle is that if there's no receipt
4346 * nor sending for a certain amount of time, the connection is closed. If the
4347 * MUX buffer still has lying data or is not allocatable, the connection is
4348 * immediately killed. If it's allocatable and empty, we attempt to send a
4349 * GOAWAY frame.
4350 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004351struct task *h2_timeout_task(struct task *t, void *context, unsigned int state)
Willy Tarreauea392822017-10-31 10:02:25 +01004352{
Olivier Houchard9f6af332018-05-25 14:04:04 +02004353 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01004354 int expired = tick_is_expired(t->expire, now_ms);
4355
Willy Tarreau7838a792019-08-12 18:42:03 +02004356 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
4357
Willy Tarreaubd42e922020-06-30 11:19:23 +02004358 if (h2c) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004359 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004360 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004361
4362 /* Somebody already stole the connection from us, so we should not
4363 * free it, we just have to free the task.
4364 */
4365 if (!t->context) {
4366 h2c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004367 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004368 goto do_leave;
4369 }
4370
4371
Willy Tarreaubd42e922020-06-30 11:19:23 +02004372 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004373 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004374 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
4375 return t;
4376 }
Willy Tarreauea392822017-10-31 10:02:25 +01004377
Willy Tarreaubd42e922020-06-30 11:19:23 +02004378 if (!h2c_may_expire(h2c)) {
4379 /* we do still have streams but all of them are idle, waiting
4380 * for the data layer, so we must not enforce the timeout here.
4381 */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004382 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004383 t->expire = TICK_ETERNITY;
4384 return t;
4385 }
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004386
Willy Tarreaubd42e922020-06-30 11:19:23 +02004387 /* We're about to destroy the connection, so make sure nobody attempts
4388 * to steal it from us.
4389 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01004390 if (h2c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004391 conn_delete_from_tree(&h2c->conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004392
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004393 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004394 }
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004395
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004396do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02004397 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02004398
4399 if (!h2c) {
4400 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02004401 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02004402 return NULL;
4403 }
4404
4405 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01004406 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02004407 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01004408
Willy Tarreau662fafc2019-05-26 09:43:07 +02004409 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01004410 /* don't even try to send a GOAWAY, the buffer is stuck */
4411 h2c->flags |= H2_CF_GOAWAY_FAILED;
4412 }
4413
4414 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01004415 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01004416 if (h2c_send_goaway_error(h2c, NULL) <= 0)
4417 h2c->flags |= H2_CF_GOAWAY_FAILED;
4418
Willy Tarreau662fafc2019-05-26 09:43:07 +02004419 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004420 unsigned int released = 0;
4421 struct buffer *buf;
4422
4423 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4424 if (b_data(buf)) {
4425 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
4426 if (!ret)
4427 break;
4428 b_del(buf, ret);
4429 if (b_data(buf))
4430 break;
4431 b_free(buf);
4432 released++;
4433 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02004434 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004435
4436 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004437 offer_buffers(NULL, released);
Willy Tarreau787db9a2018-06-14 18:31:46 +02004438 }
Willy Tarreauea392822017-10-31 10:02:25 +01004439
Willy Tarreau4481e262019-10-31 15:36:30 +01004440 /* in any case this connection must not be considered idle anymore */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004441 if (h2c->conn->flags & CO_FL_LIST_MASK) {
4442 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004443 conn_delete_from_tree(&h2c->conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004444 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4445 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004446
Willy Tarreau0975f112018-03-29 15:22:59 +02004447 /* either we can release everything now or it will be done later once
4448 * the last stream closes.
4449 */
4450 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02004451 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01004452
Willy Tarreau7838a792019-08-12 18:42:03 +02004453 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01004454 return NULL;
4455}
4456
4457
Willy Tarreau62f52692017-10-08 23:01:42 +02004458/*******************************************/
4459/* functions below are used by the streams */
4460/*******************************************/
4461
4462/*
4463 * Attach a new stream to a connection
4464 * (Used for outgoing connections)
4465 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004466static int h2_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02004467{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004468 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004469 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004470
Willy Tarreau7838a792019-08-12 18:42:03 +02004471 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004472 h2s = h2c_bck_stream_new(h2c, sd->sc, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004473 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004474 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01004475 return -1;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004476 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004477
4478 /* the connection is not idle anymore, let's mark this */
4479 HA_ATOMIC_AND(&h2c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004480 xprt_set_used(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004481
Willy Tarreau7838a792019-08-12 18:42:03 +02004482 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Christopher Faulete00ad352021-12-16 14:44:31 +01004483 return 0;
Willy Tarreau62f52692017-10-08 23:01:42 +02004484}
4485
Willy Tarreau4596fe22022-05-17 19:07:51 +02004486/* Retrieves the first valid stream connector from this connection, or returns
4487 * NULL. We have to scan because we may have some orphan streams. It might be
Willy Tarreaufafd3982018-11-18 21:29:20 +01004488 * beneficial to scan backwards from the end to reduce the likeliness to find
4489 * orphans.
4490 */
Willy Tarreaud1373532022-05-27 11:00:59 +02004491static struct stconn *h2_get_first_sc(const struct connection *conn)
Willy Tarreaufafd3982018-11-18 21:29:20 +01004492{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004493 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01004494 struct h2s *h2s;
4495 struct eb32_node *node;
4496
4497 node = eb32_first(&h2c->streams_by_id);
4498 while (node) {
4499 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004500 if (h2s_sc(h2s))
4501 return h2s_sc(h2s);
Willy Tarreaufafd3982018-11-18 21:29:20 +01004502 node = eb32_next(node);
4503 }
4504 return NULL;
4505}
4506
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004507static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4508{
4509 int ret = 0;
4510 struct h2c *h2c = conn->ctx;
4511
4512 switch (mux_ctl) {
4513 case MUX_STATUS:
4514 /* Only consider the mux to be ready if we're done with
4515 * the preface and settings, and we had no error.
4516 */
4517 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
4518 ret |= MUX_STATUS_READY;
4519 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02004520 case MUX_EXIT_STATUS:
4521 return MUX_ES_UNKNOWN;
Willy Tarreauc04a8ee2024-01-17 17:21:29 +01004522 case MUX_CTL_GET_GLITCHES:
4523 return h2c->glitches;
4524
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004525 default:
4526 return -1;
4527 }
4528}
4529
Willy Tarreau62f52692017-10-08 23:01:42 +02004530/*
Olivier Houchard060ed432018-11-06 16:32:42 +01004531 * Destroy the mux and the associated connection, if it is no longer used
4532 */
Christopher Faulet73c12072019-04-08 11:23:22 +02004533static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01004534{
Christopher Faulet73c12072019-04-08 11:23:22 +02004535 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01004536
Willy Tarreau7838a792019-08-12 18:42:03 +02004537 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02004538 if (eb_is_empty(&h2c->streams_by_id)) {
4539 BUG_ON(h2c->conn->ctx != h2c);
Christopher Faulet73c12072019-04-08 11:23:22 +02004540 h2_release(h2c);
Christopher Faulet4e610962022-04-14 11:23:50 +02004541 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004542 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01004543}
4544
4545/*
Willy Tarreau62f52692017-10-08 23:01:42 +02004546 * Detach the stream from the connection and possibly release the connection.
4547 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004548static void h2_detach(struct sedesc *sd)
Willy Tarreau62f52692017-10-08 23:01:42 +02004549{
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004550 struct h2s *h2s = sd->se;
Willy Tarreau60935142017-10-16 18:11:19 +02004551 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01004552 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004553
Willy Tarreau7838a792019-08-12 18:42:03 +02004554 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
4555
Willy Tarreau7838a792019-08-12 18:42:03 +02004556 if (!h2s) {
4557 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02004558 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004559 }
Willy Tarreau60935142017-10-16 18:11:19 +02004560
Willy Tarreaud9464162020-01-10 18:25:07 +01004561 /* there's no txbuf so we're certain not to be able to send anything */
4562 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02004563
Olivier Houchardf502aca2018-12-14 19:42:40 +01004564 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004565 h2c = h2s->h2c;
Willy Tarreau36c22322022-05-27 10:41:24 +02004566 h2c->nb_sc--;
Willy Tarreauf279a2f2023-05-30 15:42:35 +02004567 if (!h2c->nb_sc && !br_data(h2c->mbuf))
Willy Tarreau15a47332022-03-18 15:57:34 +01004568 h2c->idle_start = now_ms;
4569
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004570 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
Willy Tarreau36c22322022-05-27 10:41:24 +02004571 !h2_frt_has_too_many_sc(h2c)) {
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004572 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02004573 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004574 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02004575 }
Willy Tarreau60935142017-10-16 18:11:19 +02004576
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004577 /* this stream may be blocked waiting for some data to leave (possibly
4578 * an ES or RST frame), so orphan it in this case.
4579 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02004580 if (!(h2c->flags & (H2_CF_ERR_PENDING|H2_CF_ERROR)) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02004581 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01004582 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01004583 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004584 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau15a47332022-03-18 15:57:34 +01004585 /* refresh the timeout if none was active, so that the last
4586 * leaving stream may arm it.
4587 */
Willy Tarreau3fb2c6d2023-03-16 18:06:19 +01004588 if (h2c->task && !tick_isset(h2c->task->expire))
Willy Tarreau15a47332022-03-18 15:57:34 +01004589 h2c_update_timeout(h2c);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004590 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004591 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004592
Christopher Faulet68ee7842022-10-12 10:21:33 +02004593 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi)) {
Willy Tarreau45f752e2017-10-30 15:44:59 +01004594 /* unblock the connection if it was blocked on this
4595 * stream.
4596 */
4597 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
4598 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004599 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01004600 }
4601
Willy Tarreau71049cc2018-03-28 13:56:39 +02004602 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02004603
Christopher Faulet9b79a102019-07-15 11:22:56 +02004604 if (h2c->flags & H2_CF_IS_BACK) {
Christopher Fauletff7925d2022-10-11 19:12:40 +02004605 if (!(h2c->flags & (H2_CF_RCVD_SHUT|H2_CF_ERR_PENDING|H2_CF_ERROR))) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004606 if (h2c->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02004607 /* Add the connection in the session server list, if not already done */
4608 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
4609 h2c->conn->owner = NULL;
4610 if (eb_is_empty(&h2c->streams_by_id)) {
4611 h2c->conn->mux->destroy(h2c);
4612 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4613 return;
Christopher Fauletc5579d12020-07-01 15:45:41 +02004614 }
4615 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02004616 if (eb_is_empty(&h2c->streams_by_id)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004617 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
4618 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
4619 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houchard351411f2018-12-27 17:20:54 +01004620 return;
4621 }
4622 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004623 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004624 else {
4625 if (eb_is_empty(&h2c->streams_by_id)) {
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004626 /* If the connection is owned by the session, first remove it
4627 * from its list
4628 */
4629 if (h2c->conn->owner) {
4630 session_unown_conn(h2c->conn->owner, h2c->conn);
4631 h2c->conn->owner = NULL;
4632 }
4633
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004634 /* mark that the tasklet may lose its context to another thread and
4635 * that the handler needs to check it under the idle conns lock.
4636 */
4637 HA_ATOMIC_OR(&h2c->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004638 xprt_set_idle(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
4639
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004640 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01004641 /* The server doesn't want it, let's kill the connection right away */
4642 h2c->conn->mux->destroy(h2c);
4643 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4644 return;
4645 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01004646 /* At this point, the connection has been added to the
4647 * server idle list, so another thread may already have
4648 * hijacked it, so we can't do anything with it.
4649 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01004650 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
4651 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01004652
Olivier Houchard8a786902018-12-15 16:05:40 +01004653 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004654 else if (!h2c->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004655 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) &&
Amaury Denoyelle926a5612024-03-14 11:24:10 +01004656 !LIST_INLIST(&h2c->conn->sess_el)) {
Willy Tarreau85223482022-09-29 20:32:43 +02004657 eb64_insert(&__objt_server(h2c->conn->target)->per_thr[tid].avail_conns,
4658 &h2c->conn->hash_node->node);
Christopher Fauletc5579d12020-07-01 15:45:41 +02004659 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004660 }
4661 }
4662 }
4663
Willy Tarreaue323f342018-03-28 13:51:45 +02004664 /* We don't want to close right now unless we're removing the
4665 * last stream, and either the connection is in error, or it
4666 * reached the ID already specified in a GOAWAY frame received
4667 * or sent (as seen by last_sid >= 0).
4668 */
Olivier Houchard7a977432019-03-21 15:47:13 +01004669 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02004670 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02004671 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02004672 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02004673 }
4674 else if (h2c->task) {
Willy Tarreau15a47332022-03-18 15:57:34 +01004675 h2c_update_timeout(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004676 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02004677 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004678 else
4679 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004680}
4681
Willy Tarreau88bdba32019-05-13 18:17:53 +02004682/* Performs a synchronous or asynchronous shutr(). */
4683static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004684{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004685 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004686
Willy Tarreauf983d002019-05-14 10:40:21 +02004687 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004688 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004689
Willy Tarreau7838a792019-08-12 18:42:03 +02004690 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4691
Christopher Faulet24b85802023-10-16 19:30:02 +02004692 if (h2s->flags & H2_SF_WANT_SHUTW)
4693 goto add_to_list;
4694
Willy Tarreau18059042019-01-31 19:12:48 +01004695 /* a connstream may require us to immediately kill the whole connection
4696 * for example because of a "tcp-request content reject" rule that is
4697 * normally used to limit abuse. In this case we schedule a goaway to
4698 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004699 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004700 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004701 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004702 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004703 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4704 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4705 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004706 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4707 /* Nothing was never sent for this stream, so reset with
4708 * REFUSED_STREAM error to let the client retry the
4709 * request.
4710 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004711 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004712 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4713 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004714 else {
4715 /* a final response was already provided, we don't want this
4716 * stream anymore. This may happen when the server responds
4717 * before the end of an upload and closes quickly (redirect,
4718 * deny, ...)
4719 */
4720 h2s_error(h2s, H2_ERR_CANCEL);
4721 }
Willy Tarreau18059042019-01-31 19:12:48 +01004722
Willy Tarreau90c32322017-11-24 08:00:30 +01004723 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004724 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004725 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004726
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004727 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004728 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004729 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004730 done:
4731 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004732 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004733 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004734add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004735 /* Let the handler know we want to shutr, and add ourselves to the
4736 * most relevant list if not yet done. h2_deferred_shut() will be
4737 * automatically called via the shut_tl tasklet when there's room
4738 * again.
4739 */
4740 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau2b718102021-04-21 07:32:39 +02004741 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004742 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004743 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004744 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004745 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004746 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004747 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004748 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004749}
4750
Willy Tarreau88bdba32019-05-13 18:17:53 +02004751/* Performs a synchronous or asynchronous shutw(). */
4752static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004753{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004754 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004755
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004756 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004757 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004758
Willy Tarreau7838a792019-08-12 18:42:03 +02004759 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4760
Willy Tarreau473e0e52022-08-18 16:12:15 +02004761 if (h2s->st != H2_SS_ERROR &&
4762 (h2s->flags & (H2_SF_HEADERS_SENT | H2_SF_MORE_HTX_DATA)) == H2_SF_HEADERS_SENT) {
4763 /* we can cleanly close using an empty data frame only after headers
4764 * and if no more data is expected to be sent.
4765 */
Willy Tarreau58e32082017-11-07 14:41:09 +01004766 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4767 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004768 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004769
4770 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004771 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004772 else
4773 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004774 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004775 /* a connstream may require us to immediately kill the whole connection
4776 * for example because of a "tcp-request content reject" rule that is
4777 * normally used to limit abuse. In this case we schedule a goaway to
4778 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004779 */
Willy Tarreau95acc8b2022-05-27 16:14:10 +02004780 if (se_fl_test(h2s->sd, SE_FL_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004781 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004782 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004783 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4784 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4785 }
Willy Tarreau473e0e52022-08-18 16:12:15 +02004786 else if (h2s->flags & H2_SF_MORE_HTX_DATA) {
4787 /* some unsent data were pending (e.g. abort during an upload),
4788 * let's send a CANCEL.
4789 */
4790 TRACE_STATE("shutw before end of data, sending CANCEL", H2_EV_STRM_SHUT, h2c->conn, h2s);
4791 h2s_error(h2s, H2_ERR_CANCEL);
4792 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004793 else {
4794 /* Nothing was never sent for this stream, so reset with
4795 * REFUSED_STREAM error to let the client retry the
4796 * request.
4797 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004798 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004799 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4800 }
Willy Tarreau18059042019-01-31 19:12:48 +01004801
Willy Tarreau90c32322017-11-24 08:00:30 +01004802 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004803 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004804 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004805
Willy Tarreau00dd0782018-03-01 16:31:34 +01004806 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004807 }
4808
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004809 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004810 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004811
4812 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4813
Willy Tarreau88bdba32019-05-13 18:17:53 +02004814 done:
4815 h2s->flags &= ~H2_SF_WANT_SHUTW;
4816 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004817
4818 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004819 /* Let the handler know we want to shutw, and add ourselves to the
4820 * most relevant list if not yet done. h2_deferred_shut() will be
4821 * automatically called via the shut_tl tasklet when there's room
4822 * again.
4823 */
4824 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau2b718102021-04-21 07:32:39 +02004825 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004826 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004827 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004828 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004829 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004830 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004831 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004832 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004833}
4834
Willy Tarreau5723f292020-01-10 15:16:57 +01004835/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004836 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4837 * and prevented the last frame from being emitted.
4838 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004839struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004840{
4841 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004842 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004843
Willy Tarreau7838a792019-08-12 18:42:03 +02004844 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4845
Willy Tarreau5723f292020-01-10 15:16:57 +01004846 if (h2s->flags & H2_SF_NOTIFIED) {
4847 /* some data processing remains to be done first */
4848 goto end;
4849 }
4850
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004851 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004852 h2_do_shutw(h2s);
4853
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004854 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004855 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004856
Willy Tarreau88bdba32019-05-13 18:17:53 +02004857 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004858 /* We're done trying to send, remove ourself from the send_list */
Willy Tarreaude4a5382023-10-17 08:25:19 +02004859 h2_remove_from_list(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01004860
Willy Tarreau7be4ee02022-05-18 07:31:41 +02004861 if (!h2s_sc(h2s)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004862 h2s_destroy(h2s);
Willy Tarreau74163142021-03-13 11:30:19 +01004863 if (h2c_is_dead(h2c)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004864 h2_release(h2c);
Willy Tarreau74163142021-03-13 11:30:19 +01004865 t = NULL;
4866 }
Willy Tarreau88bdba32019-05-13 18:17:53 +02004867 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004868 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004869 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004870 TRACE_LEAVE(H2_EV_STRM_SHUT);
Willy Tarreau74163142021-03-13 11:30:19 +01004871 return t;
Willy Tarreau62f52692017-10-08 23:01:42 +02004872}
4873
Willy Tarreau4596fe22022-05-17 19:07:51 +02004874/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004875static void h2_shutr(struct stconn *sc, enum co_shr_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004876{
Willy Tarreau36c22322022-05-27 10:41:24 +02004877 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004878
Willy Tarreau7838a792019-08-12 18:42:03 +02004879 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004880 if (mode)
4881 h2_do_shutr(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004882 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004883}
4884
Willy Tarreau4596fe22022-05-17 19:07:51 +02004885/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreau36c22322022-05-27 10:41:24 +02004886static void h2_shutw(struct stconn *sc, enum co_shw_mode mode)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004887{
Willy Tarreau36c22322022-05-27 10:41:24 +02004888 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004889
Willy Tarreau7838a792019-08-12 18:42:03 +02004890 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004891 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004892 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004893}
4894
Christopher Faulet9b79a102019-07-15 11:22:56 +02004895/* Decode the payload of a HEADERS frame and produce the HTX request or response
4896 * depending on the connection's side. Returns a positive value on success, a
4897 * negative value on failure, or 0 if it couldn't proceed. May report connection
4898 * errors in h2c->errcode if the frame is non-decodable and the connection
4899 * unrecoverable. In absence of connection error when a failure is reported, the
4900 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004901 *
4902 * The function may fold CONTINUATION frames into the initial HEADERS frame
4903 * by removing padding and next frame header, then moving the CONTINUATION
4904 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4905 * leaving a hole between the main frame and the beginning of the next one.
4906 * The possibly remaining incomplete or next frame at the end may be moved
4907 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4908 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4909 *
4910 * A buffer at the beginning of processing may look like this :
4911 *
4912 * ,---.---------.-----.--------------.--------------.------.---.
4913 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4914 * `---^---------^-----^--------------^--------------^------^---'
4915 * | | <-----> | |
4916 * area | dpl | wrap
4917 * |<--------------> |
4918 * | dfl |
4919 * |<-------------------------------------------------->|
4920 * head data
4921 *
4922 * Padding is automatically overwritten when folding, participating to the
4923 * hole size after dfl :
4924 *
4925 * ,---.------------------------.-----.--------------.------.---.
4926 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4927 * `---^------------------------^-----^--------------^------^---'
4928 * | | <-----> | |
4929 * area | hole | wrap
4930 * |<-----------------------> |
4931 * | dfl |
4932 * |<-------------------------------------------------->|
4933 * head data
4934 *
4935 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4936 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4937 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004938 *
4939 * The <flags> field must point to either the stream's flags or to a copy of it
4940 * so that the function can update the following flags :
4941 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004942 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004943 *
4944 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4945 * decoding, in order to detect if we're dealing with a headers or a trailers
Willy Tarreau22892322024-01-17 16:57:23 +01004946 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). The
4947 * function takes care of counting glitches.
Willy Tarreau13278b42017-10-13 19:23:14 +02004948 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01004949static 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 +02004950{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004951 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004952 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004953 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004954 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004955 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004956 struct htx *htx = NULL;
Willy Tarreaud43a58a2024-01-19 18:20:21 +01004957 int flen = 0; // header frame len
4958 int fragments = 0;
Willy Tarreauea18f862018-12-22 20:19:26 +01004959 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004960 int ret = 0;
4961 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004962 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004963
Willy Tarreau7838a792019-08-12 18:42:03 +02004964 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4965
Willy Tarreauea18f862018-12-22 20:19:26 +01004966next_frame:
4967 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4968 goto leave; // incomplete input frame
4969
4970 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4971 * this case, we'll try to paste it immediately after the initial
4972 * HEADERS frame payload and kill any possible padding. The initial
4973 * frame's length will be increased to represent the concatenation
4974 * of the two frames. The next frame is read from position <tlen>
4975 * and written at position <flen> (minus padding if some is present).
4976 */
4977 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4978 struct h2_fh hdr;
4979 int clen; // CONTINUATION frame's payload length
4980
Willy Tarreau7838a792019-08-12 18:42:03 +02004981 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 +01004982 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4983 /* no more data, the buffer may be full, either due to
4984 * too large a frame or because of too large a hole that
4985 * we're going to compact at the end.
4986 */
4987 goto leave;
4988 }
4989
4990 if (hdr.ft != H2_FT_CONTINUATION) {
4991 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreauf0279f62024-03-11 07:35:19 +01004992 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02004993 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 +01004994 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004995 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004996 goto fail;
4997 }
4998
4999 if (hdr.sid != h2c->dsi) {
5000 /* RFC7540#6.10: frame of different stream */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005001 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005002 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 +01005003 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005004 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01005005 goto fail;
5006 }
5007
5008 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
5009 /* RFC7540#4.2: invalid frame length */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005010 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005011 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 +01005012 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
5013 goto fail;
5014 }
5015
5016 /* detect when we must stop aggragating frames */
5017 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
5018
5019 /* Take as much as we can of the CONTINUATION frame's payload */
5020 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
5021 if (clen > hdr.len)
5022 clen = hdr.len;
5023
5024 /* Move the frame's payload over the padding, hole and frame
5025 * header. At least one of hole or dpl is null (see diagrams
5026 * above). The hole moves after the new aggragated frame.
5027 */
5028 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 +02005029 h2c->dfl += hdr.len - h2c->dpl;
Willy Tarreauea18f862018-12-22 20:19:26 +01005030 hole += h2c->dpl + 9;
5031 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005032 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 +01005033 fragments++;
Willy Tarreauea18f862018-12-22 20:19:26 +01005034 goto next_frame;
5035 }
5036
5037 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01005038
Willy Tarreau13278b42017-10-13 19:23:14 +02005039 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005040 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02005041 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02005042 copy = alloc_trash_chunk();
5043 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005044 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 +02005045 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
5046 goto fail;
5047 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005048 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
5049 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
5050 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02005051 }
5052
Willy Tarreau13278b42017-10-13 19:23:14 +02005053 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
5054 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005055 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01005056 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005057 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005058 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 +01005059 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005060 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02005061 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01005062 }
5063
Willy Tarreaua01f45e2018-12-31 07:41:24 +01005064 if (flen < 5) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01005065 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005066 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 +01005067 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
5068 goto fail;
5069 }
5070
Willy Tarreau13278b42017-10-13 19:23:14 +02005071 hdrs += 5; // stream dep = 4, weight = 1
5072 flen -= 5;
5073 }
5074
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005075 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005076 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 +01005077 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01005078 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005079 }
Willy Tarreau13278b42017-10-13 19:23:14 +02005080
Willy Tarreau937f7602018-02-26 15:22:17 +01005081 /* we can't retry a failed decompression operation so we must be very
5082 * careful not to take any risks. In practice the output buffer is
5083 * always empty except maybe for trailers, in which case we simply have
5084 * to wait for the upper layer to finish consuming what is available.
5085 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005086 htx = htx_from_buf(rxbuf);
5087 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005088 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 +02005089 h2c->flags |= H2_CF_DEM_SFULL;
5090 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01005091 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005092
Willy Tarreau25919232019-01-03 14:48:18 +01005093 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005094 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
5095 sizeof(list)/sizeof(list[0]), tmp);
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005096
5097 if (outlen > 0 &&
5098 (TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED &&
5099 TRACE_ENABLED(TRACE_LEVEL_USER, H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, 0, 0, 0)) {
5100 struct ist n;
5101 int i;
5102
5103 for (i = 0; list[i].n.len; i++) {
5104 n = list[i].n;
5105
5106 if (!isttest(n)) {
5107 /* this is in fact a pseudo header whose number is in n.len */
5108 n = h2_phdr_to_ist(n.len);
5109 }
5110
5111 h2_trace_header(n, list[i].v, H2_EV_RX_FRAME|H2_EV_RX_HDR,
5112 ist(TRC_LOC), __FUNCTION__, h2c, NULL);
5113 }
5114 }
5115
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005116 if (outlen < 0) {
Willy Tarreauf0279f62024-03-11 07:35:19 +01005117 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005118 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 +01005119 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
5120 goto fail;
5121 }
5122
Willy Tarreau25919232019-01-03 14:48:18 +01005123 /* The PACK decompressor was updated, let's update the input buffer and
5124 * the parser's state to commit these changes and allow us to later
5125 * fail solely on the stream if needed.
5126 */
5127 b_del(&h2c->dbuf, h2c->dfl + hole);
5128 h2c->dfl = hole = 0;
5129 h2c->st0 = H2_CS_FRAME_H;
5130
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005131 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01005132 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005133 msgf |= (*flags & H2_SF_BODY_TUNNEL) ? H2_MSGF_BODY_TUNNEL: 0;
Amaury Denoyelle74162742020-12-11 17:53:05 +01005134 /* If an Extended CONNECT has been sent on this stream, set message flag
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005135 * to convert 200 response to 101 htx response */
Amaury Denoyelle74162742020-12-11 17:53:05 +01005136 msgf |= (*flags & H2_SF_EXT_CONNECT_SENT) ? H2_MSGF_EXT_CONNECT: 0;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01005137
Willy Tarreau88d138e2019-01-02 19:38:14 +01005138 if (*flags & H2_SF_HEADERS_RCVD)
5139 goto trailers;
5140
5141 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005142 if (h2c->flags & H2_CF_IS_BACK)
Amaury Denoyelle74162742020-12-11 17:53:05 +01005143 outlen = h2_make_htx_response(list, htx, &msgf, body_len, upgrade_protocol);
Christopher Faulet9b79a102019-07-15 11:22:56 +02005144 else
Willy Tarreaub6be1a42023-08-08 15:38:28 +02005145 outlen = h2_make_htx_request(list, htx, &msgf, body_len,
5146 !!(((const struct session *)h2c->conn->owner)->fe->options2 & PR_O2_REQBUG_OK));
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005147
Christopher Faulet3d875582021-04-26 17:46:13 +02005148 if (outlen < 0 || htx_free_space(htx) < global.tune.maxrewrite) {
Willy Tarreau25919232019-01-03 14:48:18 +01005149 /* too large headers? this is a stream error only */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005150 h2c_report_glitch(h2c, 1);
Willy Tarreau577fc272023-08-08 15:27:02 +02005151 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 +02005152 htx->flags |= HTX_FL_PARSING_ERROR;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01005153 goto fail;
5154 }
Willy Tarreau13278b42017-10-13 19:23:14 +02005155
Willy Tarreau174b06a2018-04-25 18:13:58 +02005156 if (msgf & H2_MSGF_BODY) {
5157 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005158 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005159 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005160 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005161 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02005162 }
Christopher Faulet7d247f02020-12-02 14:26:36 +01005163 if (msgf & H2_MSGF_BODYLESS_RSP)
5164 *flags |= H2_SF_BODYLESS_RESP;
Willy Tarreau174b06a2018-04-25 18:13:58 +02005165
Christopher Fauletd0db4232021-01-22 11:46:30 +01005166 if (msgf & H2_MSGF_BODY_TUNNEL)
5167 *flags |= H2_SF_BODY_TUNNEL;
5168 else {
5169 /* Abort the tunnel attempt, if any */
5170 if (*flags & H2_SF_BODY_TUNNEL)
5171 *flags |= H2_SF_TUNNEL_ABRT;
5172 *flags &= ~H2_SF_BODY_TUNNEL;
5173 }
5174
Willy Tarreau88d138e2019-01-02 19:38:14 +01005175 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01005176 /* indicate that a HEADERS frame was received for this stream, except
5177 * for 1xx responses. For 1xx responses, another HEADERS frame is
5178 * expected.
5179 */
5180 if (!(msgf & H2_MSGF_RSP_1XX))
5181 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01005182
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005183 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Christopher Faulet827a6292022-12-22 09:47:01 +01005184 if (msgf & H2_MSGF_RSP_1XX) {
5185 /* 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 +01005186 h2c_report_glitch(h2c, 1);
Christopher Faulet827a6292022-12-22 09:47:01 +01005187 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);
5188 goto fail;
5189 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005190 /* no more data are expected for this message */
5191 htx->flags |= HTX_FL_EOM;
Christopher Faulet1a60a662023-05-24 11:02:50 +02005192 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005193 }
Willy Tarreau937f7602018-02-26 15:22:17 +01005194
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005195 if (msgf & H2_MSGF_EXT_CONNECT)
5196 *flags |= H2_SF_EXT_CONNECT_RCVD;
5197
Willy Tarreau86277d42019-01-02 15:36:11 +01005198 /* success */
5199 ret = 1;
5200
Willy Tarreau68dd9852017-07-03 14:44:26 +02005201 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01005202 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01005203 * move the remaining data over it.
5204 */
5205 if (hole) {
5206 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
5207 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
5208 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
5209 b_sub(&h2c->dbuf, hole);
5210 }
5211
Christopher Faulete3ec2172023-12-13 15:36:52 +01005212 if (b_full(&h2c->dbuf) && h2c->dfl && (!htx || htx_is_empty(htx))) {
Willy Tarreauea18f862018-12-22 20:19:26 +01005213 /* too large frames */
5214 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01005215 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01005216 }
5217
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005218 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01005219 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02005220 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02005221 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreaud43a58a2024-01-19 18:20:21 +01005222
5223 /* Check for abuse of CONTINUATION: more than 4 fragments and less than
5224 * 1kB per fragment is clearly unusual and suspicious enough to count
5225 * one glitch per 1kB fragment in a 16kB buffer, which means that an
5226 * abuser sending 1600 1-byte frames in a 16kB buffer would increment
5227 * its counter by 100.
5228 */
Willy Tarreau92f287b2024-03-11 07:33:44 +01005229 if (unlikely(fragments > 4) && fragments > flen / 1024 && ret != 0) {
5230 if (h2c_report_glitch(h2c, (fragments + 15) / 16)) {
5231 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);
5232 ret = -1;
5233 }
5234 }
Willy Tarreaud43a58a2024-01-19 18:20:21 +01005235
Willy Tarreau86277d42019-01-02 15:36:11 +01005236 return ret;
5237
Willy Tarreau68dd9852017-07-03 14:44:26 +02005238 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01005239 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02005240 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005241
5242 trailers:
5243 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01005244 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
5245 /* It's a trailer but it's missing ES flag */
Willy Tarreauf0279f62024-03-11 07:35:19 +01005246 h2c_report_glitch(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02005247 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 +01005248 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02005249 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau88d138e2019-01-02 19:38:14 +01005250 goto fail;
5251 }
5252
Christopher Faulet9b79a102019-07-15 11:22:56 +02005253 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02005254 if (h2_make_htx_trailers(list, htx) <= 0) {
5255 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 +02005256 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005257 }
Christopher Faulet1a60a662023-05-24 11:02:50 +02005258 *flags |= H2_SF_ES_RCVD;
Willy Tarreau88d138e2019-01-02 19:38:14 +01005259 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02005260}
5261
Christopher Faulet9b79a102019-07-15 11:22:56 +02005262/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005263 * parser state is automatically updated. Returns > 0 if it could completely
5264 * send the current frame, 0 if it couldn't complete, in which case
Willy Tarreaub605c422022-05-17 17:04:55 +02005265 * SE_FL_RCV_MORE must be checked to know if some data remain pending (an empty
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005266 * DATA frame can return 0 as a valid result). Stream errors are reported in
5267 * h2s->errcode and connection errors in h2c->errcode. The caller must already
5268 * have checked the frame header and ensured that the frame was complete or the
5269 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02005270 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01005271static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02005272{
5273 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005274 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005275 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005276 struct htx *htx = NULL;
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005277 struct buffer *scbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02005278 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02005279
Willy Tarreau7838a792019-08-12 18:42:03 +02005280 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5281
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005282 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02005283
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005284 scbuf = h2_get_buf(h2c, &h2s->rxbuf);
5285 if (!scbuf) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005286 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02005287 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 +01005288 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005289 }
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005290 htx = htx_from_buf(scbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01005291
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005292try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005293 flen = h2c->dfl - h2c->dpl;
5294 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01005295 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005296
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005297 if (flen > b_data(&h2c->dbuf)) {
5298 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005299 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01005300 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01005301 }
5302
Christopher Faulet9b79a102019-07-15 11:22:56 +02005303 block = htx_free_data_space(htx);
5304 if (!block) {
5305 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005306 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 +02005307 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005308 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005309 if (flen > block)
5310 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005311
Christopher Faulet9b79a102019-07-15 11:22:56 +02005312 /* here, flen is the max we can copy into the output buffer */
5313 block = b_contig_data(&h2c->dbuf, 0);
5314 if (flen > block)
5315 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005316
Christopher Faulet9b79a102019-07-15 11:22:56 +02005317 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau022e5e52020-09-10 09:33:15 +02005318 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 +02005319
Christopher Faulet9b79a102019-07-15 11:22:56 +02005320 b_del(&h2c->dbuf, sent);
5321 h2c->dfl -= sent;
5322 h2c->rcvd_c += sent;
5323 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02005324
Christopher Faulet9b79a102019-07-15 11:22:56 +02005325 if (h2s->flags & H2_SF_DATA_CLEN) {
5326 h2s->body_len -= sent;
5327 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02005328 }
5329
Christopher Faulet9b79a102019-07-15 11:22:56 +02005330 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01005331 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005332 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 +01005333 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005334 }
5335
Christopher Faulet9b79a102019-07-15 11:22:56 +02005336 goto try_again;
5337
Willy Tarreau4a28da12018-01-04 14:41:00 +01005338 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01005339 /* here we're done with the frame, all the payload (except padding) was
5340 * transferred.
5341 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02005342
Christopher Faulet5be651d2021-01-22 15:28:03 +01005343 if (!(h2s->flags & H2_SF_BODY_TUNNEL) && (h2c->dff & H2_F_DATA_END_STREAM)) {
5344 /* no more data are expected for this message. This add the EOM
5345 * flag but only on the response path or if no tunnel attempt
5346 * was aborted. Otherwise (request path + tunnel abrted), the
5347 * EOM was already reported.
5348 */
Christopher Faulet33724322021-02-10 09:04:59 +01005349 if ((h2c->flags & H2_CF_IS_BACK) || !(h2s->flags & H2_SF_TUNNEL_ABRT)) {
Amaury Denoyelle25cf19d2023-05-11 16:50:04 +02005350 /* htx may be empty if receiving an empty DATA frame. */
5351 if (!htx_set_eom(htx))
5352 goto fail;
Christopher Faulet33724322021-02-10 09:04:59 +01005353 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02005354 }
5355
Willy Tarreaud1023bb2018-03-22 16:53:12 +01005356 h2c->rcvd_c += h2c->dpl;
5357 h2c->rcvd_s += h2c->dpl;
5358 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005359 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005360 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005361 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01005362 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01005363 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005364 if (htx)
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02005365 htx_to_buf(htx, scbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02005366 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01005367 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02005368}
5369
Willy Tarreau115e83b2018-12-01 19:17:53 +01005370/* Try to send a HEADERS frame matching HTX response present in HTX message
5371 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5372 * must check the stream's status to detect any error which might have happened
5373 * subsequently to a successful send. The htx blocks are automatically removed
5374 * from the message. The htx message is assumed to be valid since produced from
5375 * the internal code, hence it contains a start line, an optional series of
5376 * header blocks and an end of header, otherwise an invalid frame could be
5377 * emitted and the resulting htx message could be left in an inconsistent state.
5378 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005379static size_t h2s_snd_fhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005380{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005381 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01005382 struct h2c *h2c = h2s->h2c;
5383 struct htx_blk *blk;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005384 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005385 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005386 struct htx_sl *sl;
5387 enum htx_blk_type type;
5388 int es_now = 0;
5389 int ret = 0;
5390 int hdr;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005391
Willy Tarreau7838a792019-08-12 18:42:03 +02005392 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5393
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005394 /* get the start line (we do have one) and the rest of the headers,
5395 * that we dump starting at header 0 */
5396 sl = NULL;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005397 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005398 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005399 type = htx_get_blk_type(blk);
5400
5401 if (type == HTX_BLK_UNUSED)
5402 continue;
5403
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005404 if (type == HTX_BLK_EOH)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005405 break;
5406
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005407 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005408 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005409 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5410 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5411 goto fail;
5412 }
5413
5414 list[hdr].n = htx_get_blk_name(htx, blk);
5415 list[hdr].v = htx_get_blk_value(htx, blk);
5416 hdr++;
5417 }
5418 else if (type == HTX_BLK_RES_SL) {
Christopher Faulet56498132021-01-29 11:39:43 +01005419 BUG_ON(sl); /* Only one start-line expected */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005420 sl = htx_get_blk_ptr(htx, blk);
5421 h2s->status = sl->info.res.status;
Christopher Faulet7d247f02020-12-02 14:26:36 +01005422 if (h2s->status == 204 || h2s->status == 304)
5423 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005424 if (h2s->status < 100 || h2s->status > 999) {
5425 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5426 goto fail;
5427 }
5428 else if (h2s->status == 101) {
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005429 if (unlikely(h2s->flags & H2_SF_EXT_CONNECT_RCVD)) {
5430 /* If an Extended CONNECT has been received, we need to convert 101 to 200 */
5431 h2s->status = 200;
5432 h2s->flags &= ~H2_SF_EXT_CONNECT_RCVD;
5433 }
5434 else {
5435 /* Otherwise, 101 responses are not supported in H2, so return a error (RFC7540#8.1.1) */
5436 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5437 goto fail;
5438 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005439 }
5440 else if ((h2s->flags & H2_SF_BODY_TUNNEL) && h2s->status >= 300) {
5441 /* Abort the tunnel attempt */
5442 h2s->flags &= ~H2_SF_BODY_TUNNEL;
5443 h2s->flags |= H2_SF_TUNNEL_ABRT;
5444 }
5445 }
5446 else {
5447 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 +01005448 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005449 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005450 }
5451
Christopher Faulet56498132021-01-29 11:39:43 +01005452 /* The start-line me be defined */
5453 BUG_ON(!sl);
5454
Willy Tarreau115e83b2018-12-01 19:17:53 +01005455 /* marker for end of headers */
5456 list[hdr].n = ist("");
5457
Willy Tarreau9c218e72019-05-26 10:08:28 +02005458 mbuf = br_tail(h2c->mbuf);
5459 retry:
5460 if (!h2_get_buf(h2c, mbuf)) {
5461 h2c->flags |= H2_CF_MUX_MALLOC;
5462 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005463 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 +02005464 return 0;
5465 }
5466
Willy Tarreau115e83b2018-12-01 19:17:53 +01005467 chunk_reset(&outbuf);
5468
5469 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005470 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5471 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005472 break;
5473 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005474 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01005475 }
5476
5477 if (outbuf.size < 9)
5478 goto full;
5479
5480 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5481 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5482 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5483 outbuf.data = 9;
5484
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005485 if ((h2c->flags & (H2_CF_SHTS_UPDATED|H2_CF_DTSU_EMITTED)) == H2_CF_SHTS_UPDATED) {
5486 /* SETTINGS_HEADER_TABLE_SIZE changed, we must send an HPACK
5487 * dynamic table size update so that some clients are not
5488 * confused. In practice we only need to send the DTSU when the
5489 * advertised size is lower than the current one, and since we
5490 * don't use it and don't care about the default 4096 bytes,
5491 * we only ack it with a zero size thus we at most have to deal
5492 * with this once. See RFC7541#4.2 and #6.3 for the spec, and
5493 * below for the whole context and interoperability risks:
5494 * https://lists.w3.org/Archives/Public/ietf-http-wg/2021OctDec/0235.html
5495 */
5496 if (b_room(&outbuf) < 1)
5497 goto full;
5498 outbuf.area[outbuf.data++] = 0x20; // HPACK DTSU 0 bytes
5499
5500 /* let's not update the flags now but only once the buffer is
5501 * really committed.
5502 */
5503 }
5504
Willy Tarreau115e83b2018-12-01 19:17:53 +01005505 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01005506 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005507 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005508 goto realign_again;
5509 goto full;
5510 }
5511
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005512 if ((TRACE_SOURCE)->verbosity >= H2_VERB_ADVANCED) {
5513 char sts[4];
5514
5515 h2_trace_header(ist(":status"), ist(ultoa_r(h2s->status, sts, sizeof(sts))),
5516 H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__,
5517 h2c, h2s);
5518 }
5519
Willy Tarreau115e83b2018-12-01 19:17:53 +01005520 /* encode all headers, stop at empty name */
5521 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
5522 /* these ones do not exist in H2 and must be dropped. */
5523 if (isteq(list[hdr].n, ist("connection")) ||
5524 isteq(list[hdr].n, ist("proxy-connection")) ||
5525 isteq(list[hdr].n, ist("keep-alive")) ||
5526 isteq(list[hdr].n, ist("upgrade")) ||
5527 isteq(list[hdr].n, ist("transfer-encoding")))
5528 continue;
5529
Christopher Faulet86d144c2019-08-14 16:32:25 +02005530 /* Skip all pseudo-headers */
5531 if (*(list[hdr].n.ptr) == ':')
5532 continue;
5533
Willy Tarreau115e83b2018-12-01 19:17:53 +01005534 if (isteq(list[hdr].n, ist("")))
5535 break; // end
5536
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005537 if (!h2_encode_header(&outbuf, list[hdr].n, list[hdr].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5538 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005539 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005540 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005541 goto realign_again;
5542 goto full;
5543 }
5544 }
5545
Willy Tarreaucb985a42019-10-07 16:56:34 +02005546 /* update the frame's size */
5547 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5548
5549 if (outbuf.data > h2c->mfs + 9) {
5550 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5551 /* output full */
5552 if (b_space_wraps(mbuf))
5553 goto realign_again;
5554 goto full;
5555 }
5556 }
5557
Willy Tarreau3a537072021-06-17 08:40:04 +02005558 TRACE_USER("sent H2 response ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5559
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005560 /* remove all header blocks including the EOH and compute the
5561 * corresponding size.
Willy Tarreau115e83b2018-12-01 19:17:53 +01005562 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005563 ret = 0;
5564 blk = htx_get_head_blk(htx);
5565 while (blk) {
5566 type = htx_get_blk_type(blk);
5567 ret += htx_get_blksz(blk);
5568 blk = htx_remove_blk(htx, blk);
5569 /* The removed block is the EOH */
5570 if (type == HTX_BLK_EOH)
5571 break;
Christopher Faulet5be651d2021-01-22 15:28:03 +01005572 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005573
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005574 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005575 /* Response already closed: add END_STREAM */
5576 es_now = 1;
5577 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005578 else if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && h2s->status >= 200) {
5579 /* EOM+empty: we may need to add END_STREAM except for 1xx
Christopher Faulet991febd2020-12-02 15:17:31 +01005580 * responses and tunneled response.
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005581 */
Christopher Faulet991febd2020-12-02 15:17:31 +01005582 if (!(h2s->flags & H2_SF_BODY_TUNNEL) || h2s->status >= 300)
5583 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005584 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005585
Willy Tarreau115e83b2018-12-01 19:17:53 +01005586 if (es_now)
5587 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5588
5589 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005590 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02005591 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Faulet0b465482019-02-19 15:14:23 +01005592
5593 /* indicates the HEADERS frame was sent, except for 1xx responses. For
5594 * 1xx responses, another HEADERS frame is expected.
5595 */
Christopher Faulet89899422020-12-07 18:24:43 +01005596 if (h2s->status >= 200)
Christopher Faulet0b465482019-02-19 15:14:23 +01005597 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005598
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005599 if (h2c->flags & H2_CF_SHTS_UPDATED) {
5600 /* was sent above */
5601 h2c->flags |= H2_CF_DTSU_EMITTED;
Willy Tarreauc7d85482022-02-16 14:28:14 +01005602 h2c->flags &= ~H2_CF_SHTS_UPDATED;
Willy Tarreau39a0a1e2022-01-13 16:00:12 +01005603 }
5604
Willy Tarreau115e83b2018-12-01 19:17:53 +01005605 if (es_now) {
5606 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005607 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 +01005608 if (h2s->st == H2_SS_OPEN)
5609 h2s->st = H2_SS_HLOC;
5610 else
5611 h2s_close(h2s);
5612 }
5613
5614 /* OK we could properly deliver the response */
Willy Tarreau115e83b2018-12-01 19:17:53 +01005615 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005616 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005617 return ret;
5618 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005619 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5620 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005621 h2c->flags |= H2_CF_MUX_MFULL;
5622 h2s->flags |= H2_SF_BLK_MROOM;
5623 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005624 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 +01005625 goto end;
5626 fail:
5627 /* unparsable HTX messages, too large ones to be produced in the local
5628 * list etc go here (unrecoverable errors).
5629 */
5630 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5631 ret = 0;
5632 goto end;
5633}
5634
Willy Tarreau80739692018-10-05 11:35:57 +02005635/* Try to send a HEADERS frame matching HTX request present in HTX message
5636 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5637 * must check the stream's status to detect any error which might have happened
5638 * subsequently to a successful send. The htx blocks are automatically removed
5639 * from the message. The htx message is assumed to be valid since produced from
5640 * the internal code, hence it contains a start line, an optional series of
5641 * header blocks and an end of header, otherwise an invalid frame could be
5642 * emitted and the resulting htx message could be left in an inconsistent state.
5643 */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01005644static size_t h2s_snd_bhdrs(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02005645{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005646 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02005647 struct h2c *h2c = h2s->h2c;
5648 struct htx_blk *blk;
Willy Tarreau80739692018-10-05 11:35:57 +02005649 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005650 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02005651 struct htx_sl *sl;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005652 struct ist meth, uri, auth, host = IST_NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005653 enum htx_blk_type type;
5654 int es_now = 0;
5655 int ret = 0;
5656 int hdr;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005657 int extended_connect = 0;
Willy Tarreau80739692018-10-05 11:35:57 +02005658
Willy Tarreau7838a792019-08-12 18:42:03 +02005659 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5660
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005661 /* get the start line (we do have one) and the rest of the headers,
5662 * that we dump starting at header 0 */
5663 sl = NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005664 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005665 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005666 type = htx_get_blk_type(blk);
5667
5668 if (type == HTX_BLK_UNUSED)
5669 continue;
5670
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005671 if (type == HTX_BLK_EOH)
Willy Tarreau80739692018-10-05 11:35:57 +02005672 break;
5673
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005674 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005675 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005676 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5677 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5678 goto fail;
5679 }
Willy Tarreau80739692018-10-05 11:35:57 +02005680
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005681 list[hdr].n = htx_get_blk_name(htx, blk);
5682 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02005683
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005684 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005685 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name) &&
5686 isteq(list[hdr].n, h2c->proxy->server_id_hdr_name))
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005687 continue;
Christopher Faulet67d58092019-10-02 10:51:38 +02005688
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005689 /* Convert connection: upgrade to Extended connect from rfc 8441 */
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005690 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteqi(list[hdr].n, ist("connection"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005691 /* rfc 7230 #6.1 Connection = list of tokens */
5692 struct ist connection_ist = list[hdr].v;
5693 do {
5694 if (isteqi(iststop(connection_ist, ','),
5695 ist("upgrade"))) {
Amaury Denoyelle0df04362021-10-18 09:43:29 +02005696 if (!(h2c->flags & H2_CF_RCVD_RFC8441)) {
5697 TRACE_STATE("reject upgrade because of no RFC8441 support", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5698 goto fail;
5699 }
5700
Amaury Denoyellee0c258c2021-10-18 10:05:16 +02005701 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 +01005702 h2s->flags |= (H2_SF_BODY_TUNNEL|H2_SF_EXT_CONNECT_SENT);
5703 sl->info.req.meth = HTTP_METH_CONNECT;
5704 meth = ist("CONNECT");
5705
5706 extended_connect = 1;
5707 break;
5708 }
5709
5710 connection_ist = istadv(istfind(connection_ist, ','), 1);
5711 } while (istlen(connection_ist));
5712 }
5713
Christopher Faulet52a5ec22021-09-09 09:52:51 +02005714 if ((sl->flags & HTX_SL_F_CONN_UPG) && isteq(list[hdr].n, ist("upgrade"))) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005715 /* rfc 7230 #6.7 Upgrade = list of protocols
5716 * rfc 8441 #4 Extended connect = :protocol is single-valued
5717 *
5718 * only first HTTP/1 protocol is preserved
5719 */
5720 const struct ist protocol = iststop(list[hdr].v, ',');
5721 /* upgrade_protocol field is 16 bytes long in h2s */
5722 istpad(h2s->upgrade_protocol, isttrim(protocol, 15));
5723 }
5724
5725 if (isteq(list[hdr].n, ist("host")))
5726 host = list[hdr].v;
5727
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005728 hdr++;
5729 }
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005730 else if (type == HTX_BLK_REQ_SL) {
5731 BUG_ON(sl); /* Only one start-line expected */
5732 sl = htx_get_blk_ptr(htx, blk);
5733 meth = htx_sl_req_meth(sl);
5734 uri = htx_sl_req_uri(sl);
5735 if (sl->info.req.meth == HTTP_METH_HEAD)
5736 h2s->flags |= H2_SF_BODYLESS_RESP;
5737 if (unlikely(uri.len == 0)) {
5738 TRACE_ERROR("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5739 goto fail;
5740 }
5741 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005742 else {
5743 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5744 goto fail;
5745 }
Willy Tarreau80739692018-10-05 11:35:57 +02005746 }
5747
Christopher Faulet56498132021-01-29 11:39:43 +01005748 /* The start-line me be defined */
5749 BUG_ON(!sl);
5750
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005751 /* Now add the server name to a header (if requested) */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005752 if ((h2c->flags & H2_CF_IS_BACK) && isttest(h2c->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005753 struct server *srv = objt_server(h2c->conn->target);
5754
5755 if (srv) {
Tim Duesterhusb4b03772022-03-05 00:52:43 +01005756 list[hdr].n = h2c->proxy->server_id_hdr_name;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005757 list[hdr].v = ist(srv->id);
5758 hdr++;
5759 }
5760 }
5761
Willy Tarreau80739692018-10-05 11:35:57 +02005762 /* marker for end of headers */
5763 list[hdr].n = ist("");
5764
Willy Tarreau9c218e72019-05-26 10:08:28 +02005765 mbuf = br_tail(h2c->mbuf);
5766 retry:
5767 if (!h2_get_buf(h2c, mbuf)) {
5768 h2c->flags |= H2_CF_MUX_MALLOC;
5769 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005770 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 +02005771 return 0;
5772 }
5773
Willy Tarreau80739692018-10-05 11:35:57 +02005774 chunk_reset(&outbuf);
5775
5776 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005777 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5778 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005779 break;
5780 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005781 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02005782 }
5783
5784 if (outbuf.size < 9)
5785 goto full;
5786
5787 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5788 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5789 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5790 outbuf.data = 9;
5791
5792 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01005793 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005794 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005795 goto realign_again;
5796 goto full;
5797 }
5798
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005799 h2_trace_header(ist(":method"), meth, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5800
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005801 auth = ist(NULL);
5802
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005803 /* RFC7540 #8.3: the CONNECT method must have :
5804 * - :authority set to the URI part (host:port)
5805 * - :method set to CONNECT
5806 * - :scheme and :path omitted
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005807 *
5808 * Note that this is not applicable in case of the Extended CONNECT
5809 * protocol from rfc 8441.
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005810 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005811 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT) && !extended_connect) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005812 auth = uri;
5813
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005814 if (!h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5815 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005816 /* output full */
5817 if (b_space_wraps(mbuf))
5818 goto realign_again;
5819 goto full;
5820 }
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005821
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005822 h2s->flags |= H2_SF_BODY_TUNNEL;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005823 } else {
5824 /* other methods need a :scheme. If an authority is known from
5825 * the request line, it must be sent, otherwise only host is
5826 * sent. Host is never sent as the authority.
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005827 *
5828 * This code is also applicable for Extended CONNECT protocol
5829 * from rfc 8441.
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005830 */
5831 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02005832
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005833 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
5834 /* the URI seems to start with a scheme */
5835 int len = 1;
5836
5837 while (len < uri.len && uri.ptr[len] != ':')
5838 len++;
5839
5840 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5841 /* make the uri start at the authority now */
Tim Duesterhus9f75ed12021-03-02 18:57:26 +01005842 scheme = ist2(uri.ptr, len);
Tim Duesterhus154374c2021-03-02 18:57:27 +01005843 uri = istadv(uri, len + 3);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005844
5845 /* find the auth part of the URI */
Tim Duesterhus92c696e2021-02-28 16:11:36 +01005846 auth = ist2(uri.ptr, 0);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005847 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5848 auth.len++;
5849
Tim Duesterhus154374c2021-03-02 18:57:27 +01005850 uri = istadv(uri, auth.len);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005851 }
5852 }
5853
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005854 /* For Extended CONNECT, the :authority must be present.
5855 * Use host value for it.
5856 */
5857 if (unlikely(extended_connect) && isttest(host))
5858 auth = host;
5859
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005860 if (!scheme.len) {
5861 /* no explicit scheme, we're using an origin-form URI,
5862 * probably from an H1 request transcoded to H2 via an
5863 * external layer, then received as H2 without authority.
5864 * So we have to look up the scheme from the HTX flags.
5865 * In such a case only http and https are possible, and
5866 * https is the default (sent by browsers).
5867 */
5868 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5869 scheme = ist("http");
5870 else
5871 scheme = ist("https");
5872 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005873
5874 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005875 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005876 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005877 goto realign_again;
5878 goto full;
5879 }
Willy Tarreau80739692018-10-05 11:35:57 +02005880
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005881 if (auth.len &&
5882 !h2_encode_header(&outbuf, ist(":authority"), auth, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5883 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005884 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005885 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005886 goto realign_again;
5887 goto full;
5888 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005889
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005890 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5891 * be sent as '/' or '*'.
5892 */
5893 if (unlikely(!uri.len)) {
5894 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5895 uri = ist("*");
5896 else
5897 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005898 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005899
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005900 if (!hpack_encode_path(&outbuf, uri)) {
5901 /* output full */
5902 if (b_space_wraps(mbuf))
5903 goto realign_again;
5904 goto full;
5905 }
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005906
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005907 h2_trace_header(ist(":path"), uri, H2_EV_TX_FRAME|H2_EV_TX_HDR, ist(TRC_LOC), __FUNCTION__, h2c, h2s);
5908
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005909 /* encode the pseudo-header protocol from rfc8441 if using
5910 * Extended CONNECT method.
5911 */
5912 if (unlikely(extended_connect)) {
5913 const struct ist protocol = ist(h2s->upgrade_protocol);
5914 if (isttest(protocol)) {
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005915 if (!h2_encode_header(&outbuf, ist(":protocol"), protocol, H2_EV_TX_FRAME|H2_EV_TX_HDR,
5916 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005917 /* output full */
5918 if (b_space_wraps(mbuf))
5919 goto realign_again;
5920 goto full;
5921 }
5922 }
5923 }
Willy Tarreau80739692018-10-05 11:35:57 +02005924 }
5925
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005926 /* encode all headers, stop at empty name. Host is only sent if we
5927 * do not provide an authority.
5928 */
Willy Tarreau80739692018-10-05 11:35:57 +02005929 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005930 struct ist n = list[hdr].n;
5931 struct ist v = list[hdr].v;
5932
Willy Tarreau80739692018-10-05 11:35:57 +02005933 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005934 if (isteq(n, ist("connection")) ||
5935 (auth.len && isteq(n, ist("host"))) ||
5936 isteq(n, ist("proxy-connection")) ||
5937 isteq(n, ist("keep-alive")) ||
5938 isteq(n, ist("upgrade")) ||
5939 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005940 continue;
5941
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005942 if (isteq(n, ist("te"))) {
5943 /* "te" may only be sent with "trailers" if this value
5944 * is present, otherwise it must be deleted.
5945 */
5946 v = istist(v, ist("trailers"));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01005947 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005948 continue;
5949 v = ist("trailers");
5950 }
5951
Christopher Faulet86d144c2019-08-14 16:32:25 +02005952 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005953 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005954 continue;
5955
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005956 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005957 break; // end
5958
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01005959 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 +02005960 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005961 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005962 goto realign_again;
5963 goto full;
5964 }
5965 }
5966
Willy Tarreaucb985a42019-10-07 16:56:34 +02005967 /* update the frame's size */
5968 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5969
5970 if (outbuf.data > h2c->mfs + 9) {
5971 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5972 /* output full */
5973 if (b_space_wraps(mbuf))
5974 goto realign_again;
5975 goto full;
5976 }
5977 }
5978
Willy Tarreau3a537072021-06-17 08:40:04 +02005979 TRACE_USER("sent H2 request ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5980
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005981 /* remove all header blocks including the EOH and compute the
5982 * corresponding size.
Willy Tarreau80739692018-10-05 11:35:57 +02005983 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005984 ret = 0;
5985 blk = htx_get_head_blk(htx);
5986 while (blk) {
5987 type = htx_get_blk_type(blk);
5988 ret += htx_get_blksz(blk);
5989 blk = htx_remove_blk(htx, blk);
5990 /* The removed block is the EOH */
5991 if (type == HTX_BLK_EOH)
5992 break;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005993 }
Willy Tarreau80739692018-10-05 11:35:57 +02005994
Willy Tarreau95acc8b2022-05-27 16:14:10 +02005995 if (!h2s_sc(h2s) || se_fl_test(h2s->sd, SE_FL_SHW)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005996 /* Request already closed: add END_STREAM */
Willy Tarreau80739692018-10-05 11:35:57 +02005997 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005998 }
5999 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
6000 /* EOM+empty: we may need to add END_STREAM (except for CONNECT
6001 * request)
6002 */
6003 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6004 es_now = 1;
6005 }
Willy Tarreau80739692018-10-05 11:35:57 +02006006
Willy Tarreau80739692018-10-05 11:35:57 +02006007 if (es_now)
6008 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
6009
6010 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006011 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02006012 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau80739692018-10-05 11:35:57 +02006013 h2s->flags |= H2_SF_HEADERS_SENT;
6014 h2s->st = H2_SS_OPEN;
6015
Willy Tarreau80739692018-10-05 11:35:57 +02006016 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006017 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 +02006018 // trim any possibly pending data (eg: inconsistent content-length)
6019 h2s->flags |= H2_SF_ES_SENT;
6020 h2s->st = H2_SS_HLOC;
6021 }
6022
Willy Tarreau80739692018-10-05 11:35:57 +02006023 end:
6024 return ret;
6025 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006026 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6027 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02006028 h2c->flags |= H2_CF_MUX_MFULL;
6029 h2s->flags |= H2_SF_BLK_MROOM;
6030 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006031 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 +02006032 goto end;
6033 fail:
6034 /* unparsable HTX messages, too large ones to be produced in the local
6035 * list etc go here (unrecoverable errors).
6036 */
6037 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6038 ret = 0;
6039 goto end;
6040}
6041
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006042/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01006043 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
6044 * caller must check the stream's status to detect any error which might have
6045 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006046 * consumed, or zero if nothing done.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006047 */
Christopher Faulet142854b2020-12-02 15:12:40 +01006048static size_t h2s_make_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006049{
6050 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006051 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006052 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006053 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006054 size_t total = 0;
6055 int es_now = 0;
6056 int bsize; /* htx block size */
6057 int fsize; /* h2 frame size */
6058 struct htx_blk *blk;
6059 enum htx_blk_type type;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01006060 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006061
Willy Tarreau7838a792019-08-12 18:42:03 +02006062 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6063
Willy Tarreau98de12a2018-12-12 07:03:00 +01006064 htx = htx_from_buf(buf);
6065
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006066 /* We only come here with HTX_BLK_DATA blocks */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006067
6068 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01006069 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006070 goto end;
6071
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006072 if ((h2c->flags & H2_CF_IS_BACK) &&
Christopher Fauletf95f8762021-01-22 11:59:07 +01006073 (h2s->flags & (H2_SF_HEADERS_RCVD|H2_SF_BODY_TUNNEL)) == H2_SF_BODY_TUNNEL) {
6074 /* The response HEADERS frame not received yet. Thus the tunnel
6075 * is not fully established yet. In this situation, we block
6076 * data sending.
6077 */
6078 h2s->flags |= H2_SF_BLK_MBUSY;
6079 TRACE_STATE("Request DATA frame blocked waiting for tunnel establishment", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6080 goto end;
6081 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01006082 else if ((h2c->flags & H2_CF_IS_BACK) && (h2s->flags & H2_SF_TUNNEL_ABRT)) {
6083 /* a tunnel attempt was aborted but the is pending raw data to xfer to the server.
6084 * Thus the stream is closed with the CANCEL error. The error will be reported to
6085 * the upper layer as aserver abort. But at this stage there is nothing more we can
6086 * do. We just wait for the end of the response to be sure to not truncate it.
6087 */
6088 if (!(h2s->flags & H2_SF_ES_RCVD)) {
6089 TRACE_STATE("Request DATA frame blocked waiting end of aborted tunnel", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6090 h2s->flags |= H2_SF_BLK_MBUSY;
6091 }
6092 else {
6093 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
6094 h2s_error(h2s, H2_ERR_CANCEL);
6095 }
6096 goto end;
6097 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01006098
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006099 blk = htx_get_head_blk(htx);
6100 type = htx_get_blk_type(blk);
6101 bsize = htx_get_blksz(blk);
6102 fsize = bsize;
6103 trunc_out = 0;
6104 if (type != HTX_BLK_DATA)
6105 goto end;
6106
Willy Tarreau9c218e72019-05-26 10:08:28 +02006107 mbuf = br_tail(h2c->mbuf);
6108 retry:
6109 if (!h2_get_buf(h2c, mbuf)) {
6110 h2c->flags |= H2_CF_MUX_MALLOC;
6111 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006112 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 +02006113 goto end;
6114 }
6115
Willy Tarreau98de12a2018-12-12 07:03:00 +01006116 /* Perform some optimizations to reduce the number of buffer copies.
6117 * First, if the mux's buffer is empty and the htx area contains
6118 * exactly one data block of the same size as the requested count, and
6119 * this count fits within the frame size, the stream's window size, and
6120 * the connection's window size, then it's possible to simply swap the
6121 * caller's buffer with the mux's output buffer and adjust offsets and
6122 * length to match the entire DATA HTX block in the middle. In this
6123 * case we perform a true zero-copy operation from end-to-end. This is
6124 * the situation that happens all the time with large files. Second, if
6125 * this is not possible, but the mux's output buffer is empty, we still
6126 * have an opportunity to avoid the copy to the intermediary buffer, by
6127 * making the intermediary buffer's area point to the output buffer's
6128 * area. In this case we want to skip the HTX header to make sure that
6129 * copies remain aligned and that this operation remains possible all
6130 * the time. This goes for headers, data blocks and any data extracted
6131 * from the HTX blocks.
6132 */
6133 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02006134 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006135 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006136 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006137
Willy Tarreaubcc45952019-05-26 10:05:50 +02006138 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01006139 /* Too bad there are data left there. We're willing to memcpy/memmove
6140 * up to 1/4 of the buffer, which means that it's OK to copy a large
6141 * frame into a buffer containing few data if it needs to be realigned,
6142 * and that it's also OK to copy few data without realigning. Otherwise
6143 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01006144 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006145 if (fsize + 9 <= b_room(mbuf) &&
6146 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02006147 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
6148 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 +01006149 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02006150 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01006151
Willy Tarreau9c218e72019-05-26 10:08:28 +02006152 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6153 goto retry;
6154
Willy Tarreau98de12a2018-12-12 07:03:00 +01006155 h2c->flags |= H2_CF_MUX_MFULL;
6156 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006157 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 +01006158 goto end;
6159 }
6160
Christopher Faulet925abdf2021-04-27 22:51:07 +02006161 if (htx->flags & HTX_FL_EOM) {
6162 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6163 * message)
6164 */
6165 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6166 es_now = 1;
6167 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01006168 /* map an H2 frame to the HTX block so that we can put the
6169 * frame header there.
6170 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006171 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
6172 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01006173
6174 /* prepend an H2 DATA frame header just before the DATA block */
6175 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6176 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
Christopher Faulet925abdf2021-04-27 22:51:07 +02006177 if (es_now)
6178 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006179 h2_set_frame_size(outbuf.area, fsize);
6180
6181 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006182 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006183 h2c->mws -= fsize;
6184
6185 /* and exchange with our old area */
6186 buf->area = old_area;
6187 buf->data = buf->head = 0;
6188 total += fsize;
Christopher Faulet925abdf2021-04-27 22:51:07 +02006189 fsize = 0;
Willy Tarreau936db562023-10-18 11:39:43 +02006190 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau7838a792019-08-12 18:42:03 +02006191
6192 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 +02006193 goto out;
Willy Tarreau98de12a2018-12-12 07:03:00 +01006194 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01006195
Willy Tarreau98de12a2018-12-12 07:03:00 +01006196 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006197 /* for DATA and EOM we'll have to emit a frame, even if empty */
6198
6199 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006200 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6201 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006202 break;
6203 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006204 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006205 }
6206
6207 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02006208 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6209 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006210 h2c->flags |= H2_CF_MUX_MFULL;
6211 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006212 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006213 goto end;
6214 }
6215
6216 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
6217 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
6218 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6219 outbuf.data = 9;
6220
6221 /* we have in <fsize> the exact number of bytes we need to copy from
6222 * the HTX buffer. We need to check this against the connection's and
6223 * the stream's send windows, and to ensure that this fits in the max
6224 * frame size and in the buffer's available space minus 9 bytes (for
6225 * the frame header). The connection's flow control is applied last so
6226 * that we can use a separate list of streams which are immediately
6227 * unblocked on window opening. Note: we don't implement padding.
6228 */
6229
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006230 if (!fsize)
6231 goto send_empty;
6232
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006233 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006234 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreau2b718102021-04-21 07:32:39 +02006235 if (LIST_INLIST(&h2s->list))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006236 h2_remove_from_list(h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006237 LIST_APPEND(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02006238 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 +01006239 goto end;
6240 }
6241
Willy Tarreauee573762018-12-04 15:25:57 +01006242 if (fsize > count)
6243 fsize = count;
6244
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006245 if (fsize > h2s_mws(h2s))
6246 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006247
6248 if (h2c->mfs && fsize > h2c->mfs)
6249 fsize = h2c->mfs; // >0
6250
6251 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02006252 /* It doesn't fit at once. If it at least fits once split and
6253 * the amount of data to move is low, let's defragment the
6254 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006255 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006256 if (b_space_wraps(mbuf) &&
6257 (fsize + 9 <= b_room(mbuf)) &&
6258 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006259 goto realign_again;
6260 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01006261 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006262
6263 if (fsize <= 0) {
6264 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02006265 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6266 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006267 h2c->flags |= H2_CF_MUX_MFULL;
6268 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006269 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006270 goto end;
6271 }
6272 }
6273
6274 if (h2c->mws <= 0) {
6275 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02006276 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 +01006277 goto end;
6278 }
6279
6280 if (fsize > h2c->mws)
6281 fsize = h2c->mws;
6282
6283 /* now let's copy this this into the output buffer */
6284 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02006285 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01006286 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01006287 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006288
6289 send_empty:
6290 /* update the frame's size */
6291 h2_set_frame_size(outbuf.area, fsize);
6292
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006293 /* consume incoming HTX block */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006294 total += fsize;
6295 if (fsize == bsize) {
6296 htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006297 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
6298 /* EOM+empty: we may need to add END_STREAM (except for tunneled
6299 * message)
6300 */
6301 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
6302 es_now = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02006303 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006304 }
6305 else {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006306 /* we've truncated this block */
6307 htx_cut_data_blk(htx, blk, fsize);
6308 }
6309
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006310 if (es_now)
6311 outbuf.area[4] |= H2_F_DATA_END_STREAM;
6312
6313 /* commit the H2 response */
6314 b_add(mbuf, fsize + 9);
Willy Tarreau936db562023-10-18 11:39:43 +02006315 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006316
Christopher Faulet925abdf2021-04-27 22:51:07 +02006317 out:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006318 if (es_now) {
6319 if (h2s->st == H2_SS_OPEN)
6320 h2s->st = H2_SS_HLOC;
6321 else
6322 h2s_close(h2s);
6323
6324 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02006325 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 +01006326 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006327 else if (fsize) {
6328 if (fsize == bsize) {
6329 TRACE_DEVEL("more data may be available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6330 goto new_frame;
6331 }
6332 else if (trunc_out) {
6333 /* we've truncated this block */
6334 goto new_frame;
6335 }
6336 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006337
6338 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006339 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006340 return total;
6341}
6342
Christopher Faulet991febd2020-12-02 15:17:31 +01006343/* Skip the message payload (DATA blocks) and emit an empty DATA frame with the
6344 * ES flag set for stream <h2s>. This function is called for response known to
6345 * have no payload. Only DATA blocks are skipped. This means the trailers are
Ilya Shipitsinacf84592021-02-06 22:29:08 +05006346 * still emitted. The caller must check the stream's status to detect any error
Christopher Faulet991febd2020-12-02 15:17:31 +01006347 * which might have happened subsequently to a successful send. Returns the
6348 * number of data bytes consumed, or zero if nothing done.
6349 */
6350static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count)
6351{
6352 struct h2c *h2c = h2s->h2c;
6353 struct htx *htx;
6354 int bsize; /* htx block size */
6355 int fsize; /* h2 frame size */
6356 struct htx_blk *blk;
6357 enum htx_blk_type type;
6358 size_t total = 0;
6359
6360 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6361
Christopher Faulet991febd2020-12-02 15:17:31 +01006362 htx = htx_from_buf(buf);
6363
6364 next_data:
6365 if (!count || htx_is_empty(htx))
6366 goto end;
6367 blk = htx_get_head_blk(htx);
6368 type = htx_get_blk_type(blk);
6369 bsize = htx_get_blksz(blk);
6370 fsize = bsize;
6371 if (type != HTX_BLK_DATA)
6372 goto end;
6373
6374 if (fsize > count)
6375 fsize = count;
6376
6377 if (fsize != bsize)
6378 goto skip_data;
6379
6380 if (!(htx->flags & HTX_FL_EOM) || !htx_is_unique_blk(htx, blk))
6381 goto skip_data;
6382
6383 /* Here, it is the last block and it is also the end of the message. So
6384 * we can emit an empty DATA frame with the ES flag set
6385 */
6386 if (h2_send_empty_data_es(h2s) <= 0)
6387 goto end;
6388
6389 if (h2s->st == H2_SS_OPEN)
6390 h2s->st = H2_SS_HLOC;
6391 else
6392 h2s_close(h2s);
6393
6394 skip_data:
6395 /* consume incoming HTX block */
6396 total += fsize;
6397 if (fsize == bsize) {
6398 TRACE_DEVEL("more data may be available, trying to skip another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6399 htx_remove_blk(htx, blk);
6400 goto next_data;
6401 }
6402 else {
6403 /* we've truncated this block */
6404 htx_cut_data_blk(htx, blk, fsize);
6405 }
6406
6407 end:
6408 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
6409 return total;
6410}
6411
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006412/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
6413 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
6414 * processed. The caller must check the stream's status to detect any error
6415 * which might have happened subsequently to a successful send. The htx blocks
6416 * are automatically removed from the message. The htx message is assumed to be
6417 * valid since produced from the internal code. Processing stops when meeting
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006418 * the EOT, which *is* removed. All trailers are processed at once and sent as a
6419 * single frame. The ES flag is always set.
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006420 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006421static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006422{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02006423 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006424 struct h2c *h2c = h2s->h2c;
6425 struct htx_blk *blk;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006426 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006427 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006428 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006429 int ret = 0;
6430 int hdr;
6431 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006432
Willy Tarreau7838a792019-08-12 18:42:03 +02006433 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
6434
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006435 /* get trailers. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006436 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006437 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006438 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006439
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006440 if (type == HTX_BLK_UNUSED)
6441 continue;
6442
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006443 if (type == HTX_BLK_EOT)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006444 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006445 if (type == HTX_BLK_TLR) {
6446 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
6447 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
6448 goto fail;
6449 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006450
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006451 list[hdr].n = htx_get_blk_name(htx, blk);
6452 list[hdr].v = htx_get_blk_value(htx, blk);
6453 hdr++;
6454 }
6455 else {
6456 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 +01006457 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02006458 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006459 }
6460
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006461 /* marker for end of trailers */
6462 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006463
Willy Tarreau9c218e72019-05-26 10:08:28 +02006464 mbuf = br_tail(h2c->mbuf);
6465 retry:
6466 if (!h2_get_buf(h2c, mbuf)) {
6467 h2c->flags |= H2_CF_MUX_MALLOC;
6468 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006469 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 +02006470 goto end;
6471 }
6472
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006473 chunk_reset(&outbuf);
6474
6475 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006476 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6477 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006478 break;
6479 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006480 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006481 }
6482
6483 if (outbuf.size < 9)
6484 goto full;
6485
6486 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
6487 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
6488 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6489 outbuf.data = 9;
6490
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006491 /* encode all headers */
6492 for (idx = 0; idx < hdr; idx++) {
6493 /* these ones do not exist in H2 or must not appear in
6494 * trailers and must be dropped.
6495 */
6496 if (isteq(list[idx].n, ist("host")) ||
6497 isteq(list[idx].n, ist("content-length")) ||
6498 isteq(list[idx].n, ist("connection")) ||
6499 isteq(list[idx].n, ist("proxy-connection")) ||
6500 isteq(list[idx].n, ist("keep-alive")) ||
6501 isteq(list[idx].n, ist("upgrade")) ||
6502 isteq(list[idx].n, ist("te")) ||
6503 isteq(list[idx].n, ist("transfer-encoding")))
6504 continue;
6505
Christopher Faulet86d144c2019-08-14 16:32:25 +02006506 /* Skip all pseudo-headers */
6507 if (*(list[idx].n.ptr) == ':')
6508 continue;
6509
Willy Tarreau11e8a8c2023-01-24 19:43:11 +01006510 if (!h2_encode_header(&outbuf, list[idx].n, list[idx].v, H2_EV_TX_FRAME|H2_EV_TX_HDR,
6511 ist(TRC_LOC), __FUNCTION__, h2c, h2s)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006512 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006513 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006514 goto realign_again;
6515 goto full;
6516 }
6517 }
6518
Willy Tarreau5121e5d2019-05-06 15:13:41 +02006519 if (outbuf.data == 9) {
6520 /* here we have a problem, we have nothing to emit (either we
6521 * received an empty trailers block followed or we removed its
6522 * contents above). Because of this we can't send a HEADERS
6523 * frame, so we have to cheat and instead send an empty DATA
6524 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01006525 */
6526 outbuf.area[3] = H2_FT_DATA;
6527 outbuf.area[4] = H2_F_DATA_END_STREAM;
6528 }
6529
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006530 /* update the frame's size */
6531 h2_set_frame_size(outbuf.area, outbuf.data - 9);
6532
Willy Tarreau572d9f52019-10-11 16:58:37 +02006533 if (outbuf.data > h2c->mfs + 9) {
6534 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
6535 /* output full */
6536 if (b_space_wraps(mbuf))
6537 goto realign_again;
6538 goto full;
6539 }
6540 }
6541
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006542 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02006543 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 +02006544 b_add(mbuf, outbuf.data);
Willy Tarreau936db562023-10-18 11:39:43 +02006545 h2c->flags |= H2_CF_MBUF_HAS_DATA;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006546 h2s->flags |= H2_SF_ES_SENT;
6547
6548 if (h2s->st == H2_SS_OPEN)
6549 h2s->st = H2_SS_HLOC;
6550 else
6551 h2s_close(h2s);
6552
6553 /* OK we could properly deliver the response */
6554 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02006555 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006556 ret = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006557 blk = htx_get_head_blk(htx);
6558 while (blk) {
6559 type = htx_get_blk_type(blk);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006560 ret += htx_get_blksz(blk);
6561 blk = htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006562 /* The removed block is the EOT */
6563 if (type == HTX_BLK_EOT)
6564 break;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006565 }
6566
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006567 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006568 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006569 return ret;
6570 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006571 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6572 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006573 h2c->flags |= H2_CF_MUX_MFULL;
6574 h2s->flags |= H2_SF_BLK_MROOM;
6575 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006576 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 +01006577 goto end;
6578 fail:
6579 /* unparsable HTX messages, too large ones to be produced in the local
6580 * list etc go here (unrecoverable errors).
6581 */
6582 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6583 ret = 0;
6584 goto end;
6585}
6586
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006587/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6588 * event subscriber <es> is not allowed to change from a previous call as long
6589 * as at least one event is still subscribed. The <event_type> must only be a
6590 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006591 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006592static int h2_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02006593{
Willy Tarreau36c22322022-05-27 10:41:24 +02006594 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02006595 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006596
Willy Tarreau7838a792019-08-12 18:42:03 +02006597 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006598
6599 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006600 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006601
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006602 es->events |= event_type;
6603 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006604
6605 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006606 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006607
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006608 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006609 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02006610 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02006611 !LIST_INLIST(&h2s->list)) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006612 if (h2s->flags & H2_SF_BLK_MFCTL) {
6613 TRACE_DEVEL("Adding to fctl list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006614 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006615 }
6616 else {
6617 TRACE_DEVEL("Adding to send list", H2_EV_STRM_SEND, h2c->conn, h2s);
Willy Tarreau2b718102021-04-21 07:32:39 +02006618 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaude4a5382023-10-17 08:25:19 +02006619 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02006620 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02006621 }
Willy Tarreau7838a792019-08-12 18:42:03 +02006622 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006623 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006624}
6625
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006626/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6627 * The <es> pointer is not allowed to differ from the one passed to the
6628 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006629 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006630static int h2_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006631{
Willy Tarreau36c22322022-05-27 10:41:24 +02006632 struct h2s *h2s = __sc_mux_strm(sc);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006633
Willy Tarreau7838a792019-08-12 18:42:03 +02006634 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006635
6636 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006637 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006638
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006639 es->events &= ~event_type;
6640 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01006641 h2s->subs = NULL;
6642
6643 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006644 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006645
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006646 if (event_type & SUB_RETRY_SEND) {
Frédéric Lécaille67fda162022-06-30 12:01:54 +02006647 TRACE_DEVEL("unsubscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006648 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006649 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
Willy Tarreaude4a5382023-10-17 08:25:19 +02006650 h2_remove_from_list(h2s);
Olivier Houchardd846c262018-10-19 17:24:29 +02006651 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01006652
Willy Tarreau7838a792019-08-12 18:42:03 +02006653 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006654 return 0;
6655}
6656
6657
Christopher Faulet564e39c2021-09-21 15:50:55 +02006658/* Called from the upper layer, to receive data
6659 *
6660 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
6661 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
6662 * means the caller wants to flush input data (from the mux buffer and the
6663 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
6664 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
6665 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
6666 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
6667 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
6668 * copy as much data as possible.
6669 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006670static size_t h2_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Olivier Houchard511efea2018-08-16 15:30:32 +02006671{
Willy Tarreau36c22322022-05-27 10:41:24 +02006672 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau082f5592018-11-25 08:03:32 +01006673 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01006674 struct htx *h2s_htx = NULL;
6675 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02006676 size_t ret = 0;
6677
Willy Tarreau7838a792019-08-12 18:42:03 +02006678 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
6679
Olivier Houchard511efea2018-08-16 15:30:32 +02006680 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006681 h2s_htx = htx_from_buf(&h2s->rxbuf);
Christopher Fauletec361bb2022-02-21 15:12:54 +01006682 if (htx_is_empty(h2s_htx) && !(h2s_htx->flags & HTX_FL_PARSING_ERROR)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02006683 /* Here htx_to_buf() will set buffer data to 0 because
6684 * the HTX is empty.
6685 */
6686 htx_to_buf(h2s_htx, &h2s->rxbuf);
6687 goto end;
6688 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006689 ret = h2s_htx->data;
6690 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01006691
Christopher Faulet9b79a102019-07-15 11:22:56 +02006692 /* <buf> is empty and the message is small enough, swap the
6693 * buffers. */
6694 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01006695 htx_to_buf(buf_htx, buf);
6696 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006697 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
6698 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01006699 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006700
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006701 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_UNUSED);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006702
6703 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
6704 buf_htx->flags |= HTX_FL_PARSING_ERROR;
6705 if (htx_is_empty(buf_htx))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006706 se_fl_set(h2s->sd, SE_FL_EOI);
Willy Tarreau86724e22018-12-01 23:19:43 +01006707 }
Christopher Faulet72722c02023-02-23 14:26:34 +01006708 else if (htx_is_empty(h2s_htx)) {
Christopher Faulet42432f32020-11-20 17:43:16 +01006709 buf_htx->flags |= (h2s_htx->flags & HTX_FL_EOM);
Christopher Faulet72722c02023-02-23 14:26:34 +01006710 }
6711
Christopher Faulet9b79a102019-07-15 11:22:56 +02006712 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
6713 htx_to_buf(buf_htx, buf);
6714 htx_to_buf(h2s_htx, &h2s->rxbuf);
6715 ret -= h2s_htx->data;
6716
Christopher Faulet37070b22019-02-14 15:12:14 +01006717 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02006718 if (b_data(&h2s->rxbuf))
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006719 se_fl_set(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006720 else {
Christopher Faulet34f81d52023-05-04 16:41:37 +02006721 if (!(h2c->flags & H2_CF_IS_BACK) && (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_ES_RCVD))) {
Christopher Faulet4403cdf2023-05-04 15:49:12 +02006722 /* If request ES is reported to the upper layer, it means the
6723 * H2S now expects data from the opposite side.
6724 */
6725 se_expect_data(h2s->sd);
6726 }
6727
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006728 se_fl_clr(h2s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet531dd052023-05-24 11:14:38 +02006729 h2s_propagate_term_flags(h2c, h2s);
Olivier Houchard638b7992018-08-16 15:41:52 +02006730 if (b_size(&h2s->rxbuf)) {
6731 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01006732 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02006733 }
Olivier Houchard511efea2018-08-16 15:30:32 +02006734 }
6735
Willy Tarreau082f5592018-11-25 08:03:32 +01006736 if (ret && h2c->dsi == h2s->id) {
6737 /* demux is blocking on this stream's buffer */
6738 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02006739 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01006740 }
Christopher Faulet37070b22019-02-14 15:12:14 +01006741
Willy Tarreau7838a792019-08-12 18:42:03 +02006742 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02006743 return ret;
6744}
6745
Olivier Houchardd846c262018-10-19 17:24:29 +02006746
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006747/* Called from the upper layer, to send data from buffer <buf> for no more than
6748 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02006749 * flags may be updated on the stream connector.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006750 */
Willy Tarreau36c22322022-05-27 10:41:24 +02006751static size_t h2_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02006752{
Willy Tarreau36c22322022-05-27 10:41:24 +02006753 struct h2s *h2s = __sc_mux_strm(sc);
Willy Tarreau1dc41e72018-06-14 13:21:28 +02006754 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02006755 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006756 struct htx *htx;
6757 struct htx_blk *blk;
6758 enum htx_blk_type btype;
6759 uint32_t bsize;
6760 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006761
Willy Tarreau7838a792019-08-12 18:42:03 +02006762 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
6763
Olivier Houchardd360ac62019-03-22 17:37:16 +01006764 /* If we were not just woken because we wanted to send but couldn't,
6765 * and there's somebody else that is waiting to send, do nothing,
6766 * we will subscribe later and be put at the end of the list
6767 */
Willy Tarreaud9464162020-01-10 18:25:07 +01006768 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02006769 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
Willy Tarreaude4a5382023-10-17 08:25:19 +02006770 if (LIST_INLIST(&h2s->list))
6771 TRACE_DEVEL("stream already waiting, leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6772 else {
6773 TRACE_DEVEL("other streams already waiting, going to the queue and leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
6774 h2s->h2c->flags |= H2_CF_WAIT_INLIST;
6775 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01006776 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006777 }
Willy Tarreaud9464162020-01-10 18:25:07 +01006778 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02006779
Willy Tarreau7838a792019-08-12 18:42:03 +02006780 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
6781 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 +02006782 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006783 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02006784
Willy Tarreaucab22952019-10-31 15:48:18 +01006785 if (h2s->h2c->st0 >= H2_CS_ERROR) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006786 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreaucab22952019-10-31 15:48:18 +01006787 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);
6788 return 0;
6789 }
6790
Christopher Faulet9b79a102019-07-15 11:22:56 +02006791 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006792
Willy Tarreau0bad0432018-06-14 16:54:01 +02006793 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01006794 h2s->flags |= H2_SF_OUTGOING_DATA;
6795
Christopher Faulet2e47e3a2023-01-13 11:40:24 +01006796 if (htx->extra && htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH)
Willy Tarreau48770452022-08-18 16:03:51 +02006797 h2s->flags |= H2_SF_MORE_HTX_DATA;
6798 else
6799 h2s->flags &= ~H2_SF_MORE_HTX_DATA;
6800
Willy Tarreau751f2d02018-10-05 09:35:00 +02006801 if (h2s->id == 0) {
6802 int32_t id = h2c_get_next_sid(h2s->h2c);
6803
6804 if (id < 0) {
Willy Tarreau95acc8b2022-05-27 16:14:10 +02006805 se_fl_set(h2s->sd, SE_FL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02006806 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 +02006807 return 0;
6808 }
6809
6810 eb32_delete(&h2s->by_id);
6811 h2s->by_id.key = h2s->id = id;
6812 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01006813 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02006814 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
6815 }
6816
Christopher Faulet9b79a102019-07-15 11:22:56 +02006817 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
6818 count && !htx_is_empty(htx)) {
6819 idx = htx_get_head(htx);
6820 blk = htx_get_blk(htx, idx);
6821 btype = htx_get_blk_type(blk);
6822 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006823
Christopher Faulet9b79a102019-07-15 11:22:56 +02006824 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02006825 case HTX_BLK_REQ_SL:
6826 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006827 ret = h2s_snd_bhdrs(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02006828 if (ret > 0) {
6829 total += ret;
6830 count -= ret;
6831 if (ret < bsize)
6832 goto done;
6833 }
6834 break;
6835
Willy Tarreau115e83b2018-12-01 19:17:53 +01006836 case HTX_BLK_RES_SL:
6837 /* start-line before headers */
Willy Tarreau7cfbb812023-01-26 16:02:01 +01006838 ret = h2s_snd_fhdrs(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01006839 if (ret > 0) {
6840 total += ret;
6841 count -= ret;
6842 if (ret < bsize)
6843 goto done;
6844 }
6845 break;
6846
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006847 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006848 /* all these cause the emission of a DATA frame (possibly empty) */
Christopher Faulet991febd2020-12-02 15:17:31 +01006849 if (!(h2s->h2c->flags & H2_CF_IS_BACK) &&
6850 (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BODYLESS_RESP)) == H2_SF_BODYLESS_RESP)
6851 ret = h2s_skip_data(h2s, buf, count);
6852 else
6853 ret = h2s_make_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006854 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01006855 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006856 total += ret;
6857 count -= ret;
6858 if (ret < bsize)
6859 goto done;
6860 }
6861 break;
6862
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006863 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006864 case HTX_BLK_EOT:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006865 /* This is the first trailers block, all the subsequent ones */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006866 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006867 if (ret > 0) {
6868 total += ret;
6869 count -= ret;
6870 if (ret < bsize)
6871 goto done;
6872 }
6873 break;
6874
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006875 default:
6876 htx_remove_blk(htx, blk);
6877 total += bsize;
6878 count -= bsize;
6879 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006880 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006881 }
6882
Christopher Faulet9b79a102019-07-15 11:22:56 +02006883 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02006884 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02006885 /* trim any possibly pending data after we close (extra CR-LF,
6886 * unprocessed trailers, abnormal extra data, ...)
6887 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02006888 total += count;
6889 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02006890 }
6891
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006892 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01006893 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006894 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 +02006895 se_fl_set_error(h2s->sd);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01006896 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01006897 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006898 }
6899
Christopher Faulet9b79a102019-07-15 11:22:56 +02006900 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02006901
Olivier Houchard7505f942018-08-21 18:10:44 +02006902 if (total > 0) {
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006903 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006904 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 +02006905 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006906 }
Olivier Houchardd846c262018-10-19 17:24:29 +02006907
Olivier Houchard7505f942018-08-21 18:10:44 +02006908 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006909 /* If we're waiting for flow control, and we got a shutr on the
6910 * connection, we will never be unlocked, so add an error on
Willy Tarreau4596fe22022-05-17 19:07:51 +02006911 * the stream connector.
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006912 */
Christopher Fauletff7925d2022-10-11 19:12:40 +02006913 if ((h2s->h2c->flags & H2_CF_RCVD_SHUT) &&
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006914 !b_data(&h2s->h2c->dbuf) &&
6915 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006916 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 +01006917 se_fl_set_error(h2s->sd);
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006918 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006919
Willy Tarreau5723f292020-01-10 15:16:57 +01006920 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
6921 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006922 /* Ok we managed to send something, leave the send_list if we were still there */
Willy Tarreaude4a5382023-10-17 08:25:19 +02006923 h2_remove_from_list(h2s);
6924 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 +01006925 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006926
Willy Tarreau7838a792019-08-12 18:42:03 +02006927 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006928 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02006929}
6930
Willy Tarreau90bffa22022-09-01 19:06:44 +02006931/* appends some info about stream <h2s> to buffer <msg>, or does nothing if
Willy Tarreau7051f732022-09-02 15:22:12 +02006932 * <h2s> is NULL. Returns non-zero if the stream is considered suspicious. May
6933 * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not
6934 * NULL, otherwise a single line is used.
Willy Tarreau90bffa22022-09-01 19:06:44 +02006935 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006936static int h2_dump_h2s_info(struct buffer *msg, const struct h2s *h2s, const char *pfx)
Willy Tarreau90bffa22022-09-01 19:06:44 +02006937{
6938 int ret = 0;
6939
6940 if (!h2s)
6941 return ret;
6942
Willy Tarreau7051f732022-09-02 15:22:12 +02006943 chunk_appendf(msg, " h2s.id=%d .st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u",
Willy Tarreau90bffa22022-09-01 19:06:44 +02006944 h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
6945 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02006946 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf));
6947
6948 if (pfx)
6949 chunk_appendf(msg, "\n%s", pfx);
6950
6951 chunk_appendf(msg, " .sc=%p", h2s_sc(h2s));
Willy Tarreau90bffa22022-09-01 19:06:44 +02006952 if (h2s_sc(h2s))
6953 chunk_appendf(msg, "(.flg=0x%08x .app=%p)",
6954 h2s_sc(h2s)->flags, h2s_sc(h2s)->app);
6955
Willy Tarreau7051f732022-09-02 15:22:12 +02006956 chunk_appendf(msg, " .sd=%p", h2s->sd);
Willy Tarreau90bffa22022-09-01 19:06:44 +02006957 chunk_appendf(msg, "(.flg=0x%08x)", se_fl_get(h2s->sd));
6958
Willy Tarreau7051f732022-09-02 15:22:12 +02006959 if (pfx)
6960 chunk_appendf(msg, "\n%s", pfx);
6961
Willy Tarreau90bffa22022-09-01 19:06:44 +02006962 chunk_appendf(msg, " .subs=%p", h2s->subs);
6963 if (h2s->subs) {
6964 chunk_appendf(msg, "(ev=%d tl=%p", h2s->subs->events, h2s->subs->tasklet);
6965 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
6966 h2s->subs->tasklet->calls,
6967 h2s->subs->tasklet->context);
6968 if (h2s->subs->tasklet->calls >= 1000000)
6969 ret = 1;
6970 resolve_sym_name(msg, NULL, h2s->subs->tasklet->process);
6971 chunk_appendf(msg, ")");
6972 }
6973 return ret;
6974}
6975
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006976/* appends some info about connection <h2c> to buffer <msg>, or does nothing if
6977 * <h2c> is NULL. Returns non-zero if the connection is considered suspicious.
Willy Tarreau7051f732022-09-02 15:22:12 +02006978 * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is
6979 * not NULL, otherwise a single line is used.
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006980 */
Willy Tarreau7051f732022-09-02 15:22:12 +02006981static int h2_dump_h2c_info(struct buffer *msg, struct h2c *h2c, const char *pfx)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006982{
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02006983 const struct buffer *hmbuf, *tmbuf;
6984 const struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006985 struct eb32_node *node;
6986 int fctl_cnt = 0;
6987 int send_cnt = 0;
6988 int tree_cnt = 0;
6989 int orph_cnt = 0;
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006990 int ret = 0;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006991
6992 if (!h2c)
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006993 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006994
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006995 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006996 fctl_cnt++;
6997
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006998 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006999 send_cnt++;
7000
7001 node = eb32_first(&h2c->streams_by_id);
7002 while (node) {
7003 h2s = container_of(node, struct h2s, by_id);
7004 tree_cnt++;
Willy Tarreau7be4ee02022-05-18 07:31:41 +02007005 if (!h2s_sc(h2s))
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007006 orph_cnt++;
7007 node = eb32_next(node);
7008 }
7009
Willy Tarreau60f62682019-05-26 11:32:27 +02007010 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02007011 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02007012 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau22892322024-01-17 16:57:23 +01007013 " .nbst=%u .nbsc=%u, .glitches=%d",
Willy Tarreauab2ec452019-08-30 07:07:08 +02007014 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau22892322024-01-17 16:57:23 +01007015 h2c->nb_streams, h2c->nb_sc, h2c->glitches);
Willy Tarreau7051f732022-09-02 15:22:12 +02007016
7017 if (pfx)
7018 chunk_appendf(msg, "\n%s", pfx);
7019
7020 chunk_appendf(msg, " .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
7021 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u",
7022 fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01007023 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01007024 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
Willy Tarreau7051f732022-09-02 15:22:12 +02007025 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf));
7026
7027 if (pfx)
7028 chunk_appendf(msg, "\n%s", pfx);
7029
Christopher Faulet68ee7842022-10-12 10:21:33 +02007030 chunk_appendf(msg, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau60f62682019-05-26 11:32:27 +02007031 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
7032 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
7033 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02007034 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
7035 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01007036
Willy Tarreauf8c77092022-11-29 15:26:43 +01007037 chunk_appendf(msg, " .task=%p", h2c->task);
7038 if (h2c->task) {
7039 chunk_appendf(msg, " .exp=%s",
7040 h2c->task->expire ? tick_is_expired(h2c->task->expire, now_ms) ? "<PAST>" :
7041 human_time(TICKS_TO_MS(h2c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>");
7042 }
Willy Tarreau7051f732022-09-02 15:22:12 +02007043
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02007044 return ret;
7045}
7046
7047/* for debugging with CLI's "show fd" command */
7048static int h2_show_fd(struct buffer *msg, struct connection *conn)
7049{
7050 struct h2c *h2c = conn->ctx;
7051 const struct h2s *h2s;
7052 struct eb32_node *node;
7053 int ret = 0;
7054
7055 if (!h2c)
7056 return ret;
7057
Willy Tarreau7051f732022-09-02 15:22:12 +02007058 ret |= h2_dump_h2c_info(msg, h2c, NULL);
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02007059
7060 node = eb32_last(&h2c->streams_by_id);
7061 if (node) {
7062 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau90bffa22022-09-01 19:06:44 +02007063 chunk_appendf(msg, " last_h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02007064 ret |= h2_dump_h2s_info(msg, h2s, NULL);
Willy Tarreau987c0632018-12-18 10:32:05 +01007065 }
Willy Tarreau4e97bcc2022-09-01 19:25:57 +02007066
Willy Tarreau06bf83e2021-01-21 09:13:35 +01007067 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007068}
Willy Tarreau62f52692017-10-08 23:01:42 +02007069
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007070/* for debugging with CLI's "show sess" command. May emit multiple lines, each
7071 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
7072 * line is used. Each field starts with a space so it's safe to print it after
7073 * existing fields.
7074 */
7075static int h2_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
7076{
7077 struct h2s *h2s = sd->se;
7078 int ret = 0;
7079
7080 if (!h2s)
7081 return ret;
7082
7083 chunk_appendf(msg, " h2s=%p", h2s);
Willy Tarreau7051f732022-09-02 15:22:12 +02007084 ret |= h2_dump_h2s_info(msg, h2s, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007085 if (pfx)
7086 chunk_appendf(msg, "\n%s", pfx);
7087 chunk_appendf(msg, " h2c=%p", h2s->h2c);
Willy Tarreau7051f732022-09-02 15:22:12 +02007088 ret |= h2_dump_h2c_info(msg, h2s->h2c, pfx);
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007089 return ret;
7090}
7091
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007092/* Migrate the the connection to the current thread.
7093 * Return 0 if successful, non-zero otherwise.
7094 * Expected to be called with the old thread lock held.
7095 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02007096static int h2_takeover(struct connection *conn, int orig_tid)
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007097{
7098 struct h2c *h2c = conn->ctx;
Willy Tarreau617e80f2020-07-01 16:39:33 +02007099 struct task *task;
Willy Tarreau960f37c2023-11-17 10:56:33 +01007100 struct task *new_task;
7101 struct tasklet *new_tasklet;
7102
7103 /* Pre-allocate tasks so that we don't have to roll back after the xprt
7104 * has been migrated.
7105 */
7106 new_task = task_new_here();
7107 new_tasklet = tasklet_new();
7108 if (!new_task || !new_tasklet)
7109 goto fail;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007110
7111 if (fd_takeover(conn->handle.fd, conn) != 0)
Willy Tarreau960f37c2023-11-17 10:56:33 +01007112 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02007113
7114 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
7115 /* We failed to takeover the xprt, even if the connection may
7116 * still be valid, flag it as error'd, as we have already
7117 * taken over the fd, and wake the tasklet, so that it will
7118 * destroy it.
7119 */
7120 conn->flags |= CO_FL_ERROR;
7121 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
Willy Tarreau960f37c2023-11-17 10:56:33 +01007122 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02007123 }
7124
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007125 if (h2c->wait_event.events)
7126 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
7127 h2c->wait_event.events, &h2c->wait_event);
Willy Tarreau617e80f2020-07-01 16:39:33 +02007128
7129 task = h2c->task;
7130 if (task) {
Willy Tarreau960f37c2023-11-17 10:56:33 +01007131 /* only assign a task if there was already one, otherwise
7132 * the preallocated new task will be released.
7133 */
Willy Tarreau617e80f2020-07-01 16:39:33 +02007134 task->context = NULL;
7135 h2c->task = NULL;
7136 __ha_barrier_store();
7137 task_kill(task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007138
Willy Tarreau960f37c2023-11-17 10:56:33 +01007139 h2c->task = new_task;
7140 new_task = NULL;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007141 h2c->task->process = h2_timeout_task;
7142 h2c->task->context = h2c;
7143 }
Willy Tarreau960f37c2023-11-17 10:56:33 +01007144
7145 /* To let the tasklet know it should free itself, and do nothing else,
7146 * set its context to NULL.
7147 */
7148 h2c->wait_event.tasklet->context = NULL;
7149 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
7150
7151 h2c->wait_event.tasklet = new_tasklet;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007152 h2c->wait_event.tasklet->process = h2_io_cb;
7153 h2c->wait_event.tasklet->context = h2c;
7154 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
7155 SUB_RETRY_RECV, &h2c->wait_event);
7156
Willy Tarreau960f37c2023-11-17 10:56:33 +01007157 if (new_task)
7158 __task_free(new_task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007159 return 0;
Willy Tarreau960f37c2023-11-17 10:56:33 +01007160 fail:
7161 if (new_task)
7162 __task_free(new_task);
7163 tasklet_free(new_tasklet);
7164 return -1;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007165}
7166
Willy Tarreau62f52692017-10-08 23:01:42 +02007167/*******************************************************/
7168/* functions below are dedicated to the config parsers */
7169/*******************************************************/
7170
Willy Tarreau92f287b2024-03-11 07:33:44 +01007171/* config parser for global "tune.h2.{fe,be}.glitches-threshold" */
7172static int h2_parse_glitches_threshold(char **args, int section_type, struct proxy *curpx,
7173 const struct proxy *defpx, const char *file, int line,
7174 char **err)
7175{
7176 int *vptr;
7177
7178 if (too_many_args(1, args, err, NULL))
7179 return -1;
7180
7181 /* backend/frontend */
7182 vptr = (args[0][8] == 'b') ? &h2_be_glitches_threshold : &h2_fe_glitches_threshold;
7183
7184 *vptr = atoi(args[1]);
7185 if (*vptr < 0) {
7186 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7187 return -1;
7188 }
7189 return 0;
7190}
7191
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007192/* config parser for global "tune.h2.header-table-size" */
7193static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007194 const struct proxy *defpx, const char *file, int line,
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007195 char **err)
7196{
7197 if (too_many_args(1, args, err, NULL))
7198 return -1;
7199
7200 h2_settings_header_table_size = atoi(args[1]);
7201 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
7202 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
7203 return -1;
7204 }
7205 return 0;
7206}
Willy Tarreau62f52692017-10-08 23:01:42 +02007207
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007208/* config parser for global "tune.h2.{be.,fe.,}initial-window-size" */
Willy Tarreaue6baec02017-07-27 11:45:11 +02007209static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007210 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue6baec02017-07-27 11:45:11 +02007211 char **err)
7212{
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007213 int *vptr;
7214
Willy Tarreaue6baec02017-07-27 11:45:11 +02007215 if (too_many_args(1, args, err, NULL))
7216 return -1;
7217
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007218 /* backend/frontend/default */
7219 vptr = (args[0][8] == 'b') ? &h2_be_settings_initial_window_size :
7220 (args[0][8] == 'f') ? &h2_fe_settings_initial_window_size :
7221 &h2_settings_initial_window_size;
7222
7223 *vptr = atoi(args[1]);
7224 if (*vptr < 0) {
Willy Tarreaue6baec02017-07-27 11:45:11 +02007225 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7226 return -1;
7227 }
7228 return 0;
7229}
7230
Willy Tarreauca1027c2023-04-18 15:57:03 +02007231/* config parser for global "tune.h2.{be.,fe.,}max-concurrent-streams" */
Willy Tarreau5242ef82017-07-27 11:47:28 +02007232static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007233 const struct proxy *defpx, const char *file, int line,
Willy Tarreau5242ef82017-07-27 11:47:28 +02007234 char **err)
7235{
Willy Tarreauca1027c2023-04-18 15:57:03 +02007236 uint *vptr;
7237
Willy Tarreau5242ef82017-07-27 11:47:28 +02007238 if (too_many_args(1, args, err, NULL))
7239 return -1;
7240
Willy Tarreauca1027c2023-04-18 15:57:03 +02007241 /* backend/frontend/default */
7242 vptr = (args[0][8] == 'b') ? &h2_be_settings_max_concurrent_streams :
7243 (args[0][8] == 'f') ? &h2_fe_settings_max_concurrent_streams :
7244 &h2_settings_max_concurrent_streams;
7245
7246 *vptr = atoi(args[1]);
7247 if ((int)*vptr < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02007248 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7249 return -1;
7250 }
7251 return 0;
7252}
7253
Willy Tarreau4869ed52023-10-13 18:11:59 +02007254/* config parser for global "tune.h2.fe.max-total-streams" */
7255static int h2_parse_max_total_streams(char **args, int section_type, struct proxy *curpx,
7256 const struct proxy *defpx, const char *file, int line,
7257 char **err)
7258{
7259 uint *vptr;
7260
7261 if (too_many_args(1, args, err, NULL))
7262 return -1;
7263
7264 /* frontend only for now */
7265 vptr = &h2_fe_max_total_streams;
7266
7267 *vptr = atoi(args[1]);
7268 if ((int)*vptr < 0) {
7269 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7270 return -1;
7271 }
7272 return 0;
7273}
7274
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007275/* config parser for global "tune.h2.max-frame-size" */
7276static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01007277 const struct proxy *defpx, const char *file, int line,
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007278 char **err)
7279{
7280 if (too_many_args(1, args, err, NULL))
7281 return -1;
7282
7283 h2_settings_max_frame_size = atoi(args[1]);
7284 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
7285 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
7286 return -1;
7287 }
7288 return 0;
7289}
7290
Willy Tarreau62f52692017-10-08 23:01:42 +02007291
7292/****************************************/
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05007293/* MUX initialization and instantiation */
Willy Tarreau62f52692017-10-08 23:01:42 +02007294/***************************************/
7295
7296/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01007297static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02007298 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02007299 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02007300 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02007301 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02007302 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02007303 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02007304 .attach = h2_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02007305 .get_first_sc = h2_get_first_sc,
Willy Tarreau62f52692017-10-08 23:01:42 +02007306 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01007307 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01007308 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01007309 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02007310 .shutr = h2_shutr,
7311 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02007312 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02007313 .show_fd = h2_show_fd,
Willy Tarreaubf4ec6f2022-09-02 15:11:40 +02007314 .show_sd = h2_show_sd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01007315 .takeover = h2_takeover,
Christopher Fauleta97cced2022-04-12 18:04:10 +02007316 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Willy Tarreau62f52692017-10-08 23:01:42 +02007317 .name = "H2",
7318};
7319
Christopher Faulet32f61c02018-04-10 14:33:41 +02007320static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02007321 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02007322
Willy Tarreau0108d902018-11-25 19:14:37 +01007323INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
7324
Willy Tarreau62f52692017-10-08 23:01:42 +02007325/* config keyword parsers */
7326static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau92f287b2024-03-11 07:33:44 +01007327 { CFG_GLOBAL, "tune.h2.be.glitches-threshold", h2_parse_glitches_threshold },
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007328 { CFG_GLOBAL, "tune.h2.be.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007329 { CFG_GLOBAL, "tune.h2.be.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau92f287b2024-03-11 07:33:44 +01007330 { CFG_GLOBAL, "tune.h2.fe.glitches-threshold", h2_parse_glitches_threshold },
Willy Tarreau9d7abda2023-04-17 15:04:34 +02007331 { CFG_GLOBAL, "tune.h2.fe.initial-window-size", h2_parse_initial_window_size },
Willy Tarreauca1027c2023-04-18 15:57:03 +02007332 { CFG_GLOBAL, "tune.h2.fe.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau4869ed52023-10-13 18:11:59 +02007333 { CFG_GLOBAL, "tune.h2.fe.max-total-streams", h2_parse_max_total_streams },
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02007334 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02007335 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02007336 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01007337 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02007338 { 0, NULL, NULL }
7339}};
7340
Willy Tarreau0108d902018-11-25 19:14:37 +01007341INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007342
7343/* initialize internal structs after the config is parsed.
7344 * Returns zero on success, non-zero on error.
7345 */
7346static int init_h2()
7347{
7348 pool_head_hpack_tbl = create_pool("hpack_tbl",
7349 h2_settings_header_table_size,
7350 MEM_F_SHARED|MEM_F_EXACT);
Christopher Faulet52140992020-11-06 15:23:39 +01007351 if (!pool_head_hpack_tbl) {
7352 ha_alert("failed to allocate hpack_tbl memory pool\n");
7353 return (ERR_ALERT | ERR_FATAL);
7354 }
7355 return ERR_NONE;
Willy Tarreau2bdcc702020-05-19 11:31:11 +02007356}
7357
7358REGISTER_POST_CHECK(init_h2);