blob: fbc79634b6bfd85ec503e5d1cf2d85e0d0cad4ea [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudfd3de82020-06-04 23:46:14 +020013#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020017#include <haproxy/h2.h>
Willy Tarreaube327fa2020-06-03 09:09:57 +020018#include <haproxy/hpack-dec.h>
19#include <haproxy/hpack-enc.h>
20#include <haproxy/hpack-tbl.h>
Willy Tarreau87735332020-06-04 09:08:41 +020021#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020022#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/istbuf.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020025#include <haproxy/net_helper.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +010027#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020029#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020030#include <haproxy/trace.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020031
32
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010033/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020034static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010035static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010036static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020037static const struct h2s *h2_idle_stream;
38
Willy Tarreau5ab6b572017-09-22 08:05:00 +020039/* Connection flags (32 bit), in h2c->flags */
40#define H2_CF_NONE 0x00000000
41
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020042/* Flags indicating why writing to the mux is blocked. */
43#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
44#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
45#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
46
Willy Tarreau315d8072017-12-10 22:17:57 +010047/* Flags indicating why writing to the demux is blocked.
48 * The first two ones directly affect the ability for the mux to receive data
49 * from the connection. The other ones affect the mux's ability to demux
50 * received data.
51 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
53#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010054
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020055#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
56#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
57#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
58#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020059#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
60#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020061
Willy Tarreau081d4722017-05-16 21:51:05 +020062/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020063#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
64#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
65#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020066#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau3d4631f2021-01-20 10:53:13 +010067#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
68#define H2_CF_RCVD_SHUT 0x00020000 // a recv() attempt already failed on a shutdown
69#define H2_CF_END_REACHED 0x00040000 // pending data too short with RCVD_SHUT present
Willy Tarreau081d4722017-05-16 21:51:05 +020070
Willy Tarreau5ab6b572017-09-22 08:05:00 +020071/* H2 connection state, in h2c->st0 */
72enum h2_cs {
73 H2_CS_PREFACE, // init done, waiting for connection preface
74 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
75 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
76 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010077 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
78 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020079 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
80 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
81 H2_CS_ENTRIES // must be last
82} __attribute__((packed));
83
Willy Tarreau51330962019-05-26 09:38:07 +020084
Willy Tarreau9c218e72019-05-26 10:08:28 +020085/* 32 buffers: one for the ring's root, rest for the mbuf itself */
86#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020087
Willy Tarreau5ab6b572017-09-22 08:05:00 +020088/* H2 connection descriptor */
89struct h2c {
90 struct connection *conn;
91
92 enum h2_cs st0; /* mux state */
93 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
94
95 /* 16 bit hole here */
96 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010097 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098 int32_t max_id; /* highest ID known on this connection, <0 before preface */
99 uint32_t rcvd_c; /* newly received data to ACK for the connection */
100 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
101
102 /* states for the demux direction */
103 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105
106 int32_t dsi; /* demux stream ID (<0 = idle) */
107 int32_t dfl; /* demux frame length (if dsi >= 0) */
108 int8_t dft; /* demux frame type (if dsi >= 0) */
109 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100110 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
111 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200112 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
113
114 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200115 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200116 int32_t msi; /* mux stream ID (<0 = idle) */
117 int32_t mfl; /* mux frame length (if dsi >= 0) */
118 int8_t mft; /* mux frame type (if dsi >= 0) */
119 int8_t mff; /* mux frame flags (if dsi >= 0) */
120 /* 16 bit hole here */
121 int32_t miw; /* mux initial window size for all new streams */
122 int32_t mws; /* mux window size. Can be negative. */
123 int32_t mfs; /* mux's max frame size */
124
Willy Tarreauea392822017-10-31 10:02:25 +0100125 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100126 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100127 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200128 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100129 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100130 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200131 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100132 struct task *task; /* timeout management task */
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100133 struct h2_counters *px_counters; /* h2 counters attached to proxy */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200134 struct eb_root streams_by_id; /* all active streams by their ID */
135 struct list send_list; /* list of blocked streams requesting to send */
136 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau9edf6db2019-10-02 10:49:59 +0200137 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100138 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200139 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200140};
141
Willy Tarreau18312642017-10-11 07:57:07 +0200142/* H2 stream state, in h2s->st */
143enum h2_ss {
144 H2_SS_IDLE = 0, // idle
145 H2_SS_RLOC, // reserved(local)
146 H2_SS_RREM, // reserved(remote)
147 H2_SS_OPEN, // open
148 H2_SS_HREM, // half-closed(remote)
149 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200150 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200151 H2_SS_CLOSED, // closed
152 H2_SS_ENTRIES // must be last
153} __attribute__((packed));
154
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200155#define H2_SS_MASK(state) (1UL << (state))
156#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
157#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
158#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
159#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
160#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
161#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
162#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
163#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200164
Willy Tarreau18312642017-10-11 07:57:07 +0200165/* HTTP/2 stream flags (32 bit), in h2s->flags */
166#define H2_SF_NONE 0x00000000
167#define H2_SF_ES_RCVD 0x00000001
168#define H2_SF_ES_SENT 0x00000002
169
170#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
171#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
172
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200173/* stream flags indicating the reason the stream is blocked */
174#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
Willy Tarreau9edf6db2019-10-02 10:49:59 +0200175#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list)
176#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list)
177#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl (must be in blocked list)
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200178#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
179
Willy Tarreau454f9052017-10-26 19:40:35 +0200180/* stream flags indicating how data is supposed to be sent */
181#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
Christopher Faulet7d247f02020-12-02 14:26:36 +0100182#define H2_SF_BODYLESS_RESP 0x00000200 /* Bodyless response message */
Christopher Fauletd0db4232021-01-22 11:46:30 +0100183#define H2_SF_BODY_TUNNEL 0x00000400 // Attempt to establish a Tunnelled stream (the result depends on the status code)
184
Willy Tarreau454f9052017-10-26 19:40:35 +0200185
Willy Tarreaud9464162020-01-10 18:25:07 +0100186#define H2_SF_NOTIFIED 0x00000800 // a paused stream was notified to try to send again
Willy Tarreau67434202017-11-06 20:20:51 +0100187#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100188#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100189
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100190#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
191
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200192#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
193#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200194#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200195
Amaury Denoyelle5fb48ea2020-12-11 17:53:04 +0100196#define H2_SF_EXT_CONNECT_SENT 0x00040000 // rfc 8441 an Extended CONNECT has been sent
Amaury Denoyelleefe22762020-12-11 17:53:08 +0100197#define H2_SF_EXT_CONNECT_RCVD 0x00080000 // rfc 8441 an Extended CONNECT has been received and parsed
Christopher Fauletd0db4232021-01-22 11:46:30 +0100198
Amaury Denoyelle5fb48ea2020-12-11 17:53:04 +0100199#define H2_SF_TUNNEL_ABRT 0x00100000 // A tunnel attempt was aborted
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200200
Willy Tarreau18312642017-10-11 07:57:07 +0200201/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
Christopher Fauletfafd1b02020-11-03 18:25:52 +0100202 * it is being processed in the internal HTTP representation (HTX).
Willy Tarreau18312642017-10-11 07:57:07 +0200203 */
204struct h2s {
205 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100206 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200207 struct h2c *h2c;
Willy Tarreau18312642017-10-11 07:57:07 +0200208 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200209 int32_t id; /* stream ID */
210 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200211 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200212 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
213 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200214 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100215 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200216 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreauf96508a2020-01-10 11:12:48 +0100217 struct wait_event *subs; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200218 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau5723f292020-01-10 15:16:57 +0100219 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to send an RST after we failed to,
220 * in case there's no other subscription to do it */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100221
222 char upgrade_protocol[16]; /* rfc 8441: requested protocol on Extended CONNECT */
Willy Tarreau18312642017-10-11 07:57:07 +0200223};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200224
Willy Tarreauc6405142017-09-21 20:23:50 +0200225/* descriptor for an h2 frame header */
226struct h2_fh {
227 uint32_t len; /* length, host order, 24 bits */
228 uint32_t sid; /* stream id, host order, 31 bits */
229 uint8_t ft; /* frame type */
230 uint8_t ff; /* frame flags */
231};
232
Willy Tarreau12ae2122019-08-08 18:23:12 +0200233/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200234static void h2_trace(enum trace_level level, uint64_t mask, \
235 const struct trace_source *src,
236 const struct ist where, const struct ist func,
237 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200238
239/* The event representation is split like this :
240 * strm - application layer
241 * h2s - internal H2 stream
242 * h2c - internal H2 connection
243 * conn - external connection
244 *
245 */
246static const struct trace_event h2_trace_events[] = {
247#define H2_EV_H2C_NEW (1ULL << 0)
Willy Tarreau87951942019-08-30 07:34:36 +0200248 { .mask = H2_EV_H2C_NEW, .name = "h2c_new", .desc = "new H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200249#define H2_EV_H2C_RECV (1ULL << 1)
Willy Tarreau87951942019-08-30 07:34:36 +0200250 { .mask = H2_EV_H2C_RECV, .name = "h2c_recv", .desc = "Rx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200251#define H2_EV_H2C_SEND (1ULL << 2)
Willy Tarreau87951942019-08-30 07:34:36 +0200252 { .mask = H2_EV_H2C_SEND, .name = "h2c_send", .desc = "Tx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200253#define H2_EV_H2C_FCTL (1ULL << 3)
Willy Tarreau87951942019-08-30 07:34:36 +0200254 { .mask = H2_EV_H2C_FCTL, .name = "h2c_fctl", .desc = "H2 connection flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200255#define H2_EV_H2C_BLK (1ULL << 4)
Willy Tarreau87951942019-08-30 07:34:36 +0200256 { .mask = H2_EV_H2C_BLK, .name = "h2c_blk", .desc = "H2 connection blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200257#define H2_EV_H2C_WAKE (1ULL << 5)
Willy Tarreau87951942019-08-30 07:34:36 +0200258 { .mask = H2_EV_H2C_WAKE, .name = "h2c_wake", .desc = "H2 connection woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200259#define H2_EV_H2C_END (1ULL << 6)
Willy Tarreau87951942019-08-30 07:34:36 +0200260 { .mask = H2_EV_H2C_END, .name = "h2c_end", .desc = "H2 connection terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200261#define H2_EV_H2C_ERR (1ULL << 7)
Willy Tarreau87951942019-08-30 07:34:36 +0200262 { .mask = H2_EV_H2C_ERR, .name = "h2c_err", .desc = "error on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200263#define H2_EV_RX_FHDR (1ULL << 8)
Willy Tarreau87951942019-08-30 07:34:36 +0200264 { .mask = H2_EV_RX_FHDR, .name = "rx_fhdr", .desc = "H2 frame header received" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200265#define H2_EV_RX_FRAME (1ULL << 9)
Willy Tarreau87951942019-08-30 07:34:36 +0200266 { .mask = H2_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200267#define H2_EV_RX_EOI (1ULL << 10)
Willy Tarreau87951942019-08-30 07:34:36 +0200268 { .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 +0200269#define H2_EV_RX_PREFACE (1ULL << 11)
Willy Tarreau87951942019-08-30 07:34:36 +0200270 { .mask = H2_EV_RX_PREFACE, .name = "rx_preface", .desc = "receipt of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200271#define H2_EV_RX_DATA (1ULL << 12)
Willy Tarreau87951942019-08-30 07:34:36 +0200272 { .mask = H2_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200273#define H2_EV_RX_HDR (1ULL << 13)
Willy Tarreau87951942019-08-30 07:34:36 +0200274 { .mask = H2_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200275#define H2_EV_RX_PRIO (1ULL << 14)
Willy Tarreau87951942019-08-30 07:34:36 +0200276 { .mask = H2_EV_RX_PRIO, .name = "rx_prio", .desc = "receipt of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200277#define H2_EV_RX_RST (1ULL << 15)
Willy Tarreau87951942019-08-30 07:34:36 +0200278 { .mask = H2_EV_RX_RST, .name = "rx_rst", .desc = "receipt of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200279#define H2_EV_RX_SETTINGS (1ULL << 16)
Willy Tarreau87951942019-08-30 07:34:36 +0200280 { .mask = H2_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200281#define H2_EV_RX_PUSH (1ULL << 17)
Willy Tarreau87951942019-08-30 07:34:36 +0200282 { .mask = H2_EV_RX_PUSH, .name = "rx_push", .desc = "receipt of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200283#define H2_EV_RX_PING (1ULL << 18)
Willy Tarreau87951942019-08-30 07:34:36 +0200284 { .mask = H2_EV_RX_PING, .name = "rx_ping", .desc = "receipt of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200285#define H2_EV_RX_GOAWAY (1ULL << 19)
Willy Tarreau87951942019-08-30 07:34:36 +0200286 { .mask = H2_EV_RX_GOAWAY, .name = "rx_goaway", .desc = "receipt of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200287#define H2_EV_RX_WU (1ULL << 20)
Willy Tarreau87951942019-08-30 07:34:36 +0200288 { .mask = H2_EV_RX_WU, .name = "rx_wu", .desc = "receipt of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200289#define H2_EV_RX_CONT (1ULL << 21)
Willy Tarreau87951942019-08-30 07:34:36 +0200290 { .mask = H2_EV_RX_CONT, .name = "rx_cont", .desc = "receipt of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200291#define H2_EV_TX_FRAME (1ULL << 22)
Willy Tarreau87951942019-08-30 07:34:36 +0200292 { .mask = H2_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200293#define H2_EV_TX_EOI (1ULL << 23)
Willy Tarreau87951942019-08-30 07:34:36 +0200294 { .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 +0200295#define H2_EV_TX_PREFACE (1ULL << 24)
Willy Tarreau87951942019-08-30 07:34:36 +0200296 { .mask = H2_EV_TX_PREFACE, .name = "tx_preface", .desc = "transmission of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200297#define H2_EV_TX_DATA (1ULL << 25)
Willy Tarreau87951942019-08-30 07:34:36 +0200298 { .mask = H2_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200299#define H2_EV_TX_HDR (1ULL << 26)
Willy Tarreau87951942019-08-30 07:34:36 +0200300 { .mask = H2_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200301#define H2_EV_TX_PRIO (1ULL << 27)
Willy Tarreau87951942019-08-30 07:34:36 +0200302 { .mask = H2_EV_TX_PRIO, .name = "tx_prio", .desc = "transmission of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200303#define H2_EV_TX_RST (1ULL << 28)
Willy Tarreau87951942019-08-30 07:34:36 +0200304 { .mask = H2_EV_TX_RST, .name = "tx_rst", .desc = "transmission of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200305#define H2_EV_TX_SETTINGS (1ULL << 29)
Willy Tarreau87951942019-08-30 07:34:36 +0200306 { .mask = H2_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200307#define H2_EV_TX_PUSH (1ULL << 30)
Willy Tarreau87951942019-08-30 07:34:36 +0200308 { .mask = H2_EV_TX_PUSH, .name = "tx_push", .desc = "transmission of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200309#define H2_EV_TX_PING (1ULL << 31)
Willy Tarreau87951942019-08-30 07:34:36 +0200310 { .mask = H2_EV_TX_PING, .name = "tx_ping", .desc = "transmission of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200311#define H2_EV_TX_GOAWAY (1ULL << 32)
Willy Tarreau87951942019-08-30 07:34:36 +0200312 { .mask = H2_EV_TX_GOAWAY, .name = "tx_goaway", .desc = "transmission of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200313#define H2_EV_TX_WU (1ULL << 33)
Willy Tarreau87951942019-08-30 07:34:36 +0200314 { .mask = H2_EV_TX_WU, .name = "tx_wu", .desc = "transmission of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200315#define H2_EV_TX_CONT (1ULL << 34)
Willy Tarreau87951942019-08-30 07:34:36 +0200316 { .mask = H2_EV_TX_CONT, .name = "tx_cont", .desc = "transmission of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200317#define H2_EV_H2S_NEW (1ULL << 35)
Willy Tarreau87951942019-08-30 07:34:36 +0200318 { .mask = H2_EV_H2S_NEW, .name = "h2s_new", .desc = "new H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200319#define H2_EV_H2S_RECV (1ULL << 36)
Willy Tarreau87951942019-08-30 07:34:36 +0200320 { .mask = H2_EV_H2S_RECV, .name = "h2s_recv", .desc = "Rx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200321#define H2_EV_H2S_SEND (1ULL << 37)
Willy Tarreau87951942019-08-30 07:34:36 +0200322 { .mask = H2_EV_H2S_SEND, .name = "h2s_send", .desc = "Tx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200323#define H2_EV_H2S_FCTL (1ULL << 38)
Willy Tarreau87951942019-08-30 07:34:36 +0200324 { .mask = H2_EV_H2S_FCTL, .name = "h2s_fctl", .desc = "H2 stream flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200325#define H2_EV_H2S_BLK (1ULL << 39)
Willy Tarreau87951942019-08-30 07:34:36 +0200326 { .mask = H2_EV_H2S_BLK, .name = "h2s_blk", .desc = "H2 stream blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200327#define H2_EV_H2S_WAKE (1ULL << 40)
Willy Tarreau87951942019-08-30 07:34:36 +0200328 { .mask = H2_EV_H2S_WAKE, .name = "h2s_wake", .desc = "H2 stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200329#define H2_EV_H2S_END (1ULL << 41)
Willy Tarreau87951942019-08-30 07:34:36 +0200330 { .mask = H2_EV_H2S_END, .name = "h2s_end", .desc = "H2 stream terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200331#define H2_EV_H2S_ERR (1ULL << 42)
Willy Tarreau87951942019-08-30 07:34:36 +0200332 { .mask = H2_EV_H2S_ERR, .name = "h2s_err", .desc = "error on H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200333#define H2_EV_STRM_NEW (1ULL << 43)
Willy Tarreau87951942019-08-30 07:34:36 +0200334 { .mask = H2_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200335#define H2_EV_STRM_RECV (1ULL << 44)
Willy Tarreau87951942019-08-30 07:34:36 +0200336 { .mask = H2_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200337#define H2_EV_STRM_SEND (1ULL << 45)
Willy Tarreau87951942019-08-30 07:34:36 +0200338 { .mask = H2_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200339#define H2_EV_STRM_FULL (1ULL << 46)
Willy Tarreau87951942019-08-30 07:34:36 +0200340 { .mask = H2_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200341#define H2_EV_STRM_WAKE (1ULL << 47)
Willy Tarreau87951942019-08-30 07:34:36 +0200342 { .mask = H2_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200343#define H2_EV_STRM_SHUT (1ULL << 48)
Willy Tarreau87951942019-08-30 07:34:36 +0200344 { .mask = H2_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200345#define H2_EV_STRM_END (1ULL << 49)
Willy Tarreau87951942019-08-30 07:34:36 +0200346 { .mask = H2_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200347#define H2_EV_STRM_ERR (1ULL << 50)
Willy Tarreau87951942019-08-30 07:34:36 +0200348 { .mask = H2_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200349#define H2_EV_PROTO_ERR (1ULL << 51)
Willy Tarreau87951942019-08-30 07:34:36 +0200350 { .mask = H2_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200351 { }
352};
353
354static const struct name_desc h2_trace_lockon_args[4] = {
355 /* arg1 */ { /* already used by the connection */ },
356 /* arg2 */ { .name="h2s", .desc="H2 stream" },
357 /* arg3 */ { },
358 /* arg4 */ { }
359};
360
361static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200362#define H2_VERB_CLEAN 1
363 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
364#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200365 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200366#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200367 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200368#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200369 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200370#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200371 { .name="complete", .desc="add full data dump when available" },
372 { /* end */ }
373};
374
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200375static struct trace_source trace_h2 __read_mostly = {
Willy Tarreau12ae2122019-08-08 18:23:12 +0200376 .name = IST("h2"),
377 .desc = "HTTP/2 multiplexer",
378 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200379 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200380 .known_events = h2_trace_events,
381 .lockon_args = h2_trace_lockon_args,
382 .decoding = h2_trace_decoding,
383 .report_events = ~0, // report everything by default
384};
385
386#define TRACE_SOURCE &trace_h2
387INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
388
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100389/* h2 stats module */
390enum {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100391 H2_ST_HEADERS_RCVD,
392 H2_ST_DATA_RCVD,
393 H2_ST_SETTINGS_RCVD,
394 H2_ST_RST_STREAM_RCVD,
395 H2_ST_GOAWAY_RCVD,
396
Amaury Denoyellea8879232020-10-27 17:16:03 +0100397 H2_ST_CONN_PROTO_ERR,
398 H2_ST_STRM_PROTO_ERR,
399 H2_ST_RST_STREAM_RESP,
400 H2_ST_GOAWAY_RESP,
401
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100402 H2_ST_OPEN_CONN,
403 H2_ST_OPEN_STREAM,
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100404 H2_ST_TOTAL_CONN,
405 H2_ST_TOTAL_STREAM,
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100406
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100407 H2_STATS_COUNT /* must be the last member of the enum */
408};
409
410static struct name_desc h2_stats[] = {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100411 [H2_ST_HEADERS_RCVD] = { .name = "h2_headers_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100412 .desc = "Total number of received HEADERS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100413 [H2_ST_DATA_RCVD] = { .name = "h2_data_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100414 .desc = "Total number of received DATA frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100415 [H2_ST_SETTINGS_RCVD] = { .name = "h2_settings_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100416 .desc = "Total number of received SETTINGS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100417 [H2_ST_RST_STREAM_RCVD] = { .name = "h2_rst_stream_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100418 .desc = "Total number of received RST_STREAM frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100419 [H2_ST_GOAWAY_RCVD] = { .name = "h2_goaway_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100420 .desc = "Total number of received GOAWAY frames" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100421
422 [H2_ST_CONN_PROTO_ERR] = { .name = "h2_detected_conn_protocol_errors",
423 .desc = "Total number of connection protocol errors" },
424 [H2_ST_STRM_PROTO_ERR] = { .name = "h2_detected_strm_protocol_errors",
425 .desc = "Total number of stream protocol errors" },
426 [H2_ST_RST_STREAM_RESP] = { .name = "h2_rst_stream_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100427 .desc = "Total number of RST_STREAM sent on detected error" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100428 [H2_ST_GOAWAY_RESP] = { .name = "h2_goaway_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100429 .desc = "Total number of GOAWAY sent on detected error" },
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100430
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100431 [H2_ST_OPEN_CONN] = { .name = "h2_open_connections",
432 .desc = "Count of currently open connections" },
433 [H2_ST_OPEN_STREAM] = { .name = "h2_backend_open_streams",
434 .desc = "Count of currently open streams" },
Amaury Denoyelle377d8782021-02-03 16:27:22 +0100435 [H2_ST_TOTAL_CONN] = { .name = "h2_total_connections",
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100436 .desc = "Total number of connections" },
Amaury Denoyelle377d8782021-02-03 16:27:22 +0100437 [H2_ST_TOTAL_STREAM] = { .name = "h2_backend_total_streams",
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100438 .desc = "Total number of streams" },
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100439};
440
441static struct h2_counters {
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100442 long long headers_rcvd; /* total number of HEADERS frame received */
443 long long data_rcvd; /* total number of DATA frame received */
444 long long settings_rcvd; /* total number of SETTINGS frame received */
445 long long rst_stream_rcvd; /* total number of RST_STREAM frame received */
446 long long goaway_rcvd; /* total number of GOAWAY frame received */
Amaury Denoyellea8879232020-10-27 17:16:03 +0100447
448 long long conn_proto_err; /* total number of protocol errors detected */
449 long long strm_proto_err; /* total number of protocol errors detected */
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100450 long long rst_stream_resp; /* total number of RST_STREAM frame sent on error */
451 long long goaway_resp; /* total number of GOAWAY frame sent on error */
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100452
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100453 long long open_conns; /* count of currently open connections */
454 long long open_streams; /* count of currently open streams */
455 long long total_conns; /* total number of connections */
456 long long total_streams; /* total number of streams */
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100457} h2_counters;
458
459static void h2_fill_stats(void *data, struct field *stats)
460{
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100461 struct h2_counters *counters = data;
462
463 stats[H2_ST_HEADERS_RCVD] = mkf_u64(FN_COUNTER, counters->headers_rcvd);
464 stats[H2_ST_DATA_RCVD] = mkf_u64(FN_COUNTER, counters->data_rcvd);
465 stats[H2_ST_SETTINGS_RCVD] = mkf_u64(FN_COUNTER, counters->settings_rcvd);
466 stats[H2_ST_RST_STREAM_RCVD] = mkf_u64(FN_COUNTER, counters->rst_stream_rcvd);
467 stats[H2_ST_GOAWAY_RCVD] = mkf_u64(FN_COUNTER, counters->goaway_rcvd);
Amaury Denoyellea8879232020-10-27 17:16:03 +0100468
469 stats[H2_ST_CONN_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->conn_proto_err);
470 stats[H2_ST_STRM_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->strm_proto_err);
471 stats[H2_ST_RST_STREAM_RESP] = mkf_u64(FN_COUNTER, counters->rst_stream_resp);
472 stats[H2_ST_GOAWAY_RESP] = mkf_u64(FN_COUNTER, counters->goaway_resp);
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100473
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100474 stats[H2_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
475 stats[H2_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
476 stats[H2_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
477 stats[H2_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100478}
479
480static struct stats_module h2_stats_module = {
481 .name = "h2",
482 .fill_stats = h2_fill_stats,
483 .stats = h2_stats,
484 .stats_count = H2_STATS_COUNT,
485 .counters = &h2_counters,
486 .counters_size = sizeof(h2_counters),
487 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
488 .clearable = 1,
489};
490
491INITCALL1(STG_REGISTER, stats_register_module, &h2_stats_module);
492
Willy Tarreau8ceae722018-11-26 11:58:30 +0100493/* the h2c connection pool */
494DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
495
496/* the h2s stream pool */
497DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
498
Willy Tarreaudc572362018-12-12 08:08:05 +0100499/* The default connection window size is 65535, it may only be enlarged using
500 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
501 * we'll pretend we already received the difference between the two to send
502 * an equivalent window update to enlarge it to 2G-1.
503 */
504#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
505
Willy Tarreau455d5682019-05-24 19:42:18 +0200506/* maximum amount of data we're OK with re-aligning for buffer optimizations */
507#define MAX_DATA_REALIGN 1024
508
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200509/* a few settings from the global section */
510static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200511static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100512static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100513static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200514
Willy Tarreau2a856182017-05-16 15:20:39 +0200515/* a dmumy closed stream */
516static const struct h2s *h2_closed_stream = &(const struct h2s){
517 .cs = NULL,
518 .h2c = NULL,
519 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100520 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100521 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200522 .id = 0,
523};
524
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100525/* a dmumy closed stream returning a PROTOCOL_ERROR error */
526static const struct h2s *h2_error_stream = &(const struct h2s){
527 .cs = NULL,
528 .h2c = NULL,
529 .st = H2_SS_CLOSED,
530 .errcode = H2_ERR_PROTOCOL_ERROR,
531 .flags = 0,
532 .id = 0,
533};
534
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100535/* a dmumy closed stream returning a REFUSED_STREAM error */
536static const struct h2s *h2_refused_stream = &(const struct h2s){
537 .cs = NULL,
538 .h2c = NULL,
539 .st = H2_SS_CLOSED,
540 .errcode = H2_ERR_REFUSED_STREAM,
541 .flags = 0,
542 .id = 0,
543};
544
Willy Tarreau2a856182017-05-16 15:20:39 +0200545/* and a dummy idle stream for use with any unannounced stream */
546static const struct h2s *h2_idle_stream = &(const struct h2s){
547 .cs = NULL,
548 .h2c = NULL,
549 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100550 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200551 .id = 0,
552};
553
Willy Tarreau144f84a2021-03-02 16:09:26 +0100554struct task *h2_timeout_task(struct task *t, void *context, unsigned int state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200555static int h2_send(struct h2c *h2c);
556static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200557static int h2_process(struct h2c *h2c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100558/* h2_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100559struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100560static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Amaury Denoyelle74162742020-12-11 17:53:05 +0100561static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len, char *upgrade_protocol);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100562static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100563struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100564static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100565static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200566
Willy Tarreauab2ec452019-08-30 07:07:08 +0200567/* returns a h2c state as an abbreviated 3-letter string, or "???" if unknown */
568static inline const char *h2c_st_to_str(enum h2_cs st)
569{
570 switch (st) {
571 case H2_CS_PREFACE: return "PRF";
572 case H2_CS_SETTINGS1: return "STG";
573 case H2_CS_FRAME_H: return "FRH";
574 case H2_CS_FRAME_P: return "FRP";
575 case H2_CS_FRAME_A: return "FRA";
576 case H2_CS_FRAME_E: return "FRE";
577 case H2_CS_ERROR: return "ERR";
578 case H2_CS_ERROR2: return "ER2";
579 default: return "???";
580 }
581}
582
583/* returns a h2s state as an abbreviated 3-letter string, or "???" if unknown */
584static inline const char *h2s_st_to_str(enum h2_ss st)
585{
586 switch (st) {
587 case H2_SS_IDLE: return "IDL"; // idle
588 case H2_SS_RLOC: return "RSL"; // reserved local
589 case H2_SS_RREM: return "RSR"; // reserved remote
590 case H2_SS_OPEN: return "OPN"; // open
591 case H2_SS_HREM: return "HCR"; // half-closed remote
592 case H2_SS_HLOC: return "HCL"; // half-closed local
593 case H2_SS_ERROR : return "ERR"; // error
594 case H2_SS_CLOSED: return "CLO"; // closed
595 default: return "???";
596 }
597}
598
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200599/* the H2 traces always expect that arg1, if non-null, is of type connection
600 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
601 * that arg3, if non-null, is either of type htx for tx headers, or of type
602 * buffer for everything else.
603 */
604static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
605 const struct ist where, const struct ist func,
606 const void *a1, const void *a2, const void *a3, const void *a4)
607{
608 const struct connection *conn = a1;
609 const struct h2c *h2c = conn ? conn->ctx : NULL;
610 const struct h2s *h2s = a2;
611 const struct buffer *buf = a3;
612 const struct htx *htx;
613 int pos;
614
615 if (!h2c) // nothing to add
616 return;
617
Willy Tarreau17104d42019-08-30 07:12:55 +0200618 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau73db4342019-09-25 07:28:44 +0200619 chunk_appendf(&trace_buf, " : h2c=%p(%c,%s)", h2c, conn_is_back(conn) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
620
Willy Tarreau8e6f7492021-06-16 17:47:24 +0200621 if (mask & H2_EV_H2C_NEW) // inside h2_init, otherwise it's hard to match conn & h2c
622 conn_append_debug_info(&trace_buf, conn, " : ");
623
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100624 if (h2c->errcode)
625 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2c->errcode), h2c->errcode);
626
Willy Tarreau73db4342019-09-25 07:28:44 +0200627 if (h2c->dsi >= 0 &&
628 (mask & (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) == (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) {
Willy Tarreau8520d872020-09-18 07:39:29 +0200629 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 +0200630 }
631
632 if (h2s) {
633 if (h2s->id <= 0)
634 chunk_appendf(&trace_buf, " dsi=%d", h2c->dsi);
635 chunk_appendf(&trace_buf, " h2s=%p(%d,%s)", h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100636 if (h2s->id && h2s->errcode)
637 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2s->errcode), h2s->errcode);
Willy Tarreau73db4342019-09-25 07:28:44 +0200638 }
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200639 }
640
641 /* Let's dump decoded requests and responses right after parsing. They
642 * are traced at level USER with a few recognizable flags.
643 */
644 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
645 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
646 htx = htxbuf(buf); // recv req/res
647 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
648 htx = a3; // send req/res
649 else
650 htx = NULL;
651
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200652 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200653 const struct htx_blk *blk = htx_get_blk(htx, pos);
654 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
655 enum htx_blk_type type = htx_get_blk_type(blk);
656
657 if (type == HTX_BLK_REQ_SL)
658 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200659 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200660 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
661 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
662 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
663 else if (type == HTX_BLK_RES_SL)
664 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200665 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200666 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
667 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
668 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
669 }
670}
671
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200672
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100673/* Detect a pending read0 for a H2 connection. It happens if a read0 was
674 * already reported on a previous xprt->rcvbuf() AND a frame parser failed
675 * to parse pending data, confirming no more progress is possible because
676 * we're facing a truncated frame. The function returns 1 to report a read0
677 * or 0 otherwise.
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200678 */
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100679static inline int h2c_read0_pending(struct h2c *h2c)
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200680{
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100681 return !!(h2c->flags & H2_CF_END_REACHED);
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200682}
683
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200684/* returns true if the connection is allowed to expire, false otherwise. A
685 * connection may expire when:
686 * - it has no stream
687 * - it has data in the mux buffer
688 * - it has streams in the blocked list
689 * - it has streams in the fctl list
690 * - it has streams in the send list
691 * Otherwise it means some streams are waiting in the data layer and it should
692 * not expire.
693 */
694static inline int h2c_may_expire(const struct h2c *h2c)
695{
696 return eb_is_empty(&h2c->streams_by_id) ||
697 br_data(h2c->mbuf) ||
698 !LIST_ISEMPTY(&h2c->blocked_list) ||
699 !LIST_ISEMPTY(&h2c->fctl_list) ||
Willy Tarreaud9464162020-01-10 18:25:07 +0100700 !LIST_ISEMPTY(&h2c->send_list);
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200701}
702
Olivier Houchard7a977432019-03-21 15:47:13 +0100703static __inline int
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200704h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100705{
706 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
707 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
708 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
709 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200710 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100711 (conn_xprt_read0_pending(h2c->conn) ||
712 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
713 return 1;
714
715 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100716}
717
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200718/*****************************************************/
719/* functions below are for dynamic buffer management */
720/*****************************************************/
721
Willy Tarreau315d8072017-12-10 22:17:57 +0100722/* indicates whether or not the we may call the h2_recv() function to attempt
723 * to receive data into the buffer and/or demux pending data. The condition is
724 * a bit complex due to some API limits for now. The rules are the following :
725 * - if an error or a shutdown was detected on the connection and the buffer
726 * is empty, we must not attempt to receive
727 * - if the demux buf failed to be allocated, we must not try to receive and
728 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100729 * - if no flag indicates a blocking condition, we may attempt to receive,
730 * regardless of whether the demux buffer is full or not, so that only
731 * de demux part decides whether or not to block. This is needed because
732 * the connection API indeed prevents us from re-enabling receipt that is
733 * already enabled in a polled state, so we must always immediately stop
734 * as soon as the demux can't proceed so as never to hit an end of read
735 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100736 * - otherwise must may not attempt
737 */
738static inline int h2_recv_allowed(const struct h2c *h2c)
739{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200740 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100741 (h2c->st0 >= H2_CS_ERROR ||
742 h2c->conn->flags & CO_FL_ERROR ||
743 conn_xprt_read0_pending(h2c->conn)))
744 return 0;
745
746 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100747 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100748 return 1;
749
750 return 0;
751}
752
Willy Tarreau47b515a2018-12-21 16:09:41 +0100753/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200754static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100755{
756 if (!h2_recv_allowed(h2c))
757 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200758 if ((!consider_buffer || !b_data(&h2c->dbuf))
759 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100760 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200761 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100762}
763
764
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100765/* returns true if the front connection has too many conn_streams attached */
766static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200767{
Willy Tarreaua8754662018-12-23 20:43:58 +0100768 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200769}
770
Willy Tarreau44e973f2018-03-01 17:49:30 +0100771/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
772 * flags are used to figure what buffer was requested. It returns 1 if the
773 * allocation succeeds, in which case the connection is woken up, or 0 if it's
774 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200775 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100776static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200777{
778 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100779 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200780
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100781 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc(&h2c->dbuf)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200782 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200783 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200784 return 1;
785 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200786
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100787 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc(br_tail(h2c->mbuf))) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100788 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200789
790 if (h2c->flags & H2_CF_DEM_MROOM) {
791 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200792 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200793 }
Willy Tarreau14398122017-09-22 14:26:04 +0200794 return 1;
795 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100796
797 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
798 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100799 b_alloc(&h2s->rxbuf)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100800 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200801 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100802 return 1;
803 }
804
Willy Tarreau14398122017-09-22 14:26:04 +0200805 return 0;
806}
807
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200808static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200809{
810 struct buffer *buf = NULL;
811
Willy Tarreau2b718102021-04-21 07:32:39 +0200812 if (likely(!LIST_INLIST(&h2c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100813 unlikely((buf = b_alloc(bptr)) == NULL)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100814 h2c->buf_wait.target = h2c;
815 h2c->buf_wait.wakeup_cb = h2_buf_available;
Willy Tarreau2b718102021-04-21 07:32:39 +0200816 LIST_APPEND(&ti->buffer_wq, &h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200817 }
818 return buf;
819}
820
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200821static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200822{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200823 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100824 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100825 offer_buffers(NULL, 1);
Willy Tarreau14398122017-09-22 14:26:04 +0200826 }
827}
828
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200829static inline void h2_release_mbuf(struct h2c *h2c)
830{
831 struct buffer *buf;
832 unsigned int count = 0;
833
834 while (b_size(buf = br_head_pick(h2c->mbuf))) {
835 b_free(buf);
836 count++;
837 }
838 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100839 offer_buffers(NULL, count);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200840}
841
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100842/* returns the number of allocatable outgoing streams for the connection taking
843 * the last_sid and the reserved ones into account.
844 */
845static inline int h2_streams_left(const struct h2c *h2c)
846{
847 int ret;
848
849 /* consider the number of outgoing streams we're allowed to create before
850 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
851 * nb_reserved is the number of streams which don't yet have an ID.
852 */
853 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
854 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
855 if (ret < 0)
856 ret = 0;
857 return ret;
858}
859
Willy Tarreau00f18a32019-01-26 12:19:01 +0100860/* returns the number of streams in use on a connection to figure if it's
861 * idle or not. We check nb_cs and not nb_streams as the caller will want
862 * to know if it was the last one after a detach().
863 */
864static int h2_used_streams(struct connection *conn)
865{
866 struct h2c *h2c = conn->ctx;
867
868 return h2c->nb_cs;
869}
870
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100871/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100872static int h2_avail_streams(struct connection *conn)
873{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100874 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100875 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100876 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100877
Willy Tarreau6afec462019-01-28 06:40:19 +0100878 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
879 * streams on the connection.
880 */
881 if (h2c->last_sid >= 0)
882 return 0;
883
Willy Tarreauc61966f2019-10-31 15:10:03 +0100884 if (h2c->st0 >= H2_CS_ERROR)
885 return 0;
886
Willy Tarreau86949782019-01-31 10:42:05 +0100887 /* note: may be negative if a SETTINGS frame changes the limit */
888 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100889
890 /* we must also consider the limit imposed by stream IDs */
891 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100892 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100893 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100894 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
895 ret1 = MIN(ret1, ret2);
896 }
897 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100898}
899
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200900
Willy Tarreau62f52692017-10-08 23:01:42 +0200901/*****************************************************************/
902/* functions below are dedicated to the mux setup and management */
903/*****************************************************************/
904
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200905/* Initialize the mux once it's attached. For outgoing connections, the context
906 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200907 * connections from the fact that the context is still NULL (even during mux
908 * upgrades). <input> is always used as Input buffer and may contain data. It is
909 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200910 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200911static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
912 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200913{
914 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100915 struct task *t = NULL;
Christopher Fauletf81ef032019-10-04 15:19:43 +0200916 void *conn_ctx = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200917
Christopher Fauletf81ef032019-10-04 15:19:43 +0200918 TRACE_ENTER(H2_EV_H2C_NEW);
Willy Tarreau7838a792019-08-12 18:42:03 +0200919
Willy Tarreaubafbe012017-11-24 17:34:44 +0100920 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200921 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200922 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200923
Christopher Faulete9b70722019-04-08 10:46:02 +0200924 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200925 h2c->flags = H2_CF_IS_BACK;
926 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
927 if (tick_isset(prx->timeout.serverfin))
928 h2c->shut_timeout = prx->timeout.serverfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100929
930 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
931 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200932 } else {
933 h2c->flags = H2_CF_NONE;
934 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
935 if (tick_isset(prx->timeout.clientfin))
936 h2c->shut_timeout = prx->timeout.clientfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100937
938 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
939 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200940 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100941
Willy Tarreau0b37d652018-10-03 10:33:02 +0200942 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100943 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100944 if (tick_isset(h2c->timeout)) {
945 t = task_new(tid_bit);
946 if (!t)
947 goto fail;
948
949 h2c->task = t;
950 t->process = h2_timeout_task;
951 t->context = h2c;
952 t->expire = tick_add(now_ms, h2c->timeout);
953 }
Willy Tarreauea392822017-10-31 10:02:25 +0100954
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200955 h2c->wait_event.tasklet = tasklet_new();
956 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200957 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200958 h2c->wait_event.tasklet->process = h2_io_cb;
959 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100960 h2c->wait_event.events = 0;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200961 if (!conn_is_back(conn)) {
962 /* Connection might already be in the stopping_list if subject
963 * to h1->h2 upgrade.
964 */
965 if (!LIST_INLIST(&conn->stopping_list)) {
966 LIST_APPEND(&mux_stopping_data[tid].list,
967 &conn->stopping_list);
968 }
969 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200970
Willy Tarreau2bdcc702020-05-19 11:31:11 +0200971 h2c->ddht = hpack_dht_alloc();
Willy Tarreau32218eb2017-09-22 08:07:25 +0200972 if (!h2c->ddht)
973 goto fail;
974
975 /* Initialise the context. */
976 h2c->st0 = H2_CS_PREFACE;
977 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100978 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200979 h2c->max_id = -1;
980 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100981 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200982 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100983 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200984 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100985 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100986 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200987
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200988 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200989 h2c->dsi = -1;
990 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100991
Willy Tarreau32218eb2017-09-22 08:07:25 +0200992 h2c->last_sid = -1;
993
Willy Tarreau51330962019-05-26 09:38:07 +0200994 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200995 h2c->miw = 65535; /* mux initial window size */
996 h2c->mws = 65535; /* mux window size */
997 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200998 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200999 LIST_INIT(&h2c->send_list);
1000 LIST_INIT(&h2c->fctl_list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +02001001 LIST_INIT(&h2c->blocked_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +01001002 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001003
Christopher Fauletf81ef032019-10-04 15:19:43 +02001004 conn->ctx = h2c;
1005
Willy Tarreau8e6f7492021-06-16 17:47:24 +02001006 TRACE_USER("new H2 connection", H2_EV_H2C_NEW, conn);
1007
Willy Tarreau3f133572017-10-31 19:21:06 +01001008 if (t)
1009 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +01001010
Willy Tarreau01b44822018-10-03 14:26:37 +02001011 if (h2c->flags & H2_CF_IS_BACK) {
1012 /* FIXME: this is temporary, for outgoing connections we need
1013 * to immediately allocate a stream until the code is modified
1014 * so that the caller calls ->attach(). For now the outgoing cs
Christopher Fauletf81ef032019-10-04 15:19:43 +02001015 * is stored as conn->ctx by the caller and saved in conn_ctx.
Willy Tarreau01b44822018-10-03 14:26:37 +02001016 */
1017 struct h2s *h2s;
1018
Christopher Fauletf81ef032019-10-04 15:19:43 +02001019 h2s = h2c_bck_stream_new(h2c, conn_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +02001020 if (!h2s)
1021 goto fail_stream;
1022 }
1023
Willy Tarreau4781b152021-04-06 13:53:36 +02001024 HA_ATOMIC_INC(&h2c->px_counters->open_conns);
1025 HA_ATOMIC_INC(&h2c->px_counters->total_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001026
Willy Tarreau0f383582018-10-03 14:22:21 +02001027 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02001028 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001029 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001030 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +02001031 fail_stream:
1032 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +02001033 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001034 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001035 if (h2c->wait_event.tasklet)
1036 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001037 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +02001038 fail_no_h2c:
Christopher Fauletf81ef032019-10-04 15:19:43 +02001039 conn->ctx = conn_ctx; /* restore saved ctx */
1040 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001041 return -1;
1042}
1043
Willy Tarreau751f2d02018-10-05 09:35:00 +02001044/* returns the next allocatable outgoing stream ID for the H2 connection, or
1045 * -1 if no more is allocatable.
1046 */
1047static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
1048{
1049 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +01001050
1051 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +02001052 id = -1;
1053 return id;
1054}
1055
Willy Tarreau2373acc2017-10-12 17:35:14 +02001056/* returns the stream associated with id <id> or NULL if not found */
1057static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
1058{
1059 struct eb32_node *node;
1060
Willy Tarreau751f2d02018-10-05 09:35:00 +02001061 if (id == 0)
1062 return (struct h2s *)h2_closed_stream;
1063
Willy Tarreau2a856182017-05-16 15:20:39 +02001064 if (id > h2c->max_id)
1065 return (struct h2s *)h2_idle_stream;
1066
Willy Tarreau2373acc2017-10-12 17:35:14 +02001067 node = eb32_lookup(&h2c->streams_by_id, id);
1068 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +02001069 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +02001070
1071 return container_of(node, struct h2s, by_id);
1072}
1073
Christopher Faulet73c12072019-04-08 11:23:22 +02001074/* release function. This one should be called to free all resources allocated
1075 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +02001076 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001077static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02001078{
William Dauchy477757c2020-08-07 22:19:23 +02001079 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001080
Willy Tarreau7838a792019-08-12 18:42:03 +02001081 TRACE_ENTER(H2_EV_H2C_END);
1082
Willy Tarreau32218eb2017-09-22 08:07:25 +02001083 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +02001084 /* The connection must be aattached to this mux to be released */
1085 if (h2c->conn && h2c->conn->ctx == h2c)
1086 conn = h2c->conn;
1087
Willy Tarreau7838a792019-08-12 18:42:03 +02001088 TRACE_DEVEL("freeing h2c", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001089 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +02001090
Willy Tarreau2b718102021-04-21 07:32:39 +02001091 if (LIST_INLIST(&h2c->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01001092 LIST_DEL_INIT(&h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +02001093
Willy Tarreau44e973f2018-03-01 17:49:30 +01001094 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +02001095 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +01001096
Willy Tarreauea392822017-10-31 10:02:25 +01001097 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +02001098 h2c->task->context = NULL;
1099 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +01001100 h2c->task = NULL;
1101 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001102 if (h2c->wait_event.tasklet)
1103 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet21d849f2019-09-18 11:07:20 +02001104 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001105 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet21d849f2019-09-18 11:07:20 +02001106 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +01001107
Willy Tarreau4781b152021-04-06 13:53:36 +02001108 HA_ATOMIC_DEC(&h2c->px_counters->open_conns);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001109
Willy Tarreaubafbe012017-11-24 17:34:44 +01001110 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001111 }
1112
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001113 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001114 if (!conn_is_back(conn))
1115 LIST_DEL_INIT(&conn->stopping_list);
1116
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001117 conn->mux = NULL;
1118 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02001119 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001120
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001121 conn_stop_tracking(conn);
1122 conn_full_close(conn);
1123 if (conn->destroy_cb)
1124 conn->destroy_cb(conn);
1125 conn_free(conn);
1126 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001127
1128 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +02001129}
1130
1131
Willy Tarreau71681172017-10-23 14:39:06 +02001132/******************************************************/
1133/* functions below are for the H2 protocol processing */
1134/******************************************************/
1135
1136/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +01001137static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +02001138{
1139 return h2s ? h2s->id : 0;
1140}
1141
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001142/* returns the sum of the stream's own window size and the mux's initial
1143 * window, which together form the stream's effective window size.
1144 */
1145static inline int h2s_mws(const struct h2s *h2s)
1146{
1147 return h2s->sws + h2s->h2c->miw;
1148}
1149
Willy Tarreau5b5e6872017-09-25 16:17:25 +02001150/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001151static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +02001152{
1153 if (h2c->msi < 0)
1154 return 0;
1155
1156 if (h2c->msi == h2s_id(h2s))
1157 return 0;
1158
1159 return 1;
1160}
1161
Willy Tarreau741d6df2017-10-17 08:00:59 +02001162/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +01001163static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +02001164{
Willy Tarreau022e5e52020-09-10 09:33:15 +02001165 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn, 0, 0, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +02001166 h2c->errcode = err;
1167 h2c->st0 = H2_CS_ERROR;
1168}
1169
Willy Tarreau175cebb2019-01-24 10:02:24 +01001170/* marks an error on the stream. It may also update an already closed stream
1171 * (e.g. to report an error after an RST was received).
1172 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001173static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +02001174{
Willy Tarreau175cebb2019-01-24 10:02:24 +01001175 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001176 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s, 0, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +02001177 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +01001178 if (h2s->st < H2_SS_ERROR)
1179 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +01001180 if (h2s->cs)
1181 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001182 }
1183}
1184
Willy Tarreau7e094452018-12-19 18:08:52 +01001185/* attempt to notify the data layer of recv availability */
1186static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1187{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001188 if (h2s->subs && h2s->subs->events & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001189 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001190 tasklet_wakeup(h2s->subs->tasklet);
1191 h2s->subs->events &= ~SUB_RETRY_RECV;
1192 if (!h2s->subs->events)
1193 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001194 }
1195}
1196
1197/* attempt to notify the data layer of send availability */
1198static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1199{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001200 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001201 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01001202 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01001203 tasklet_wakeup(h2s->subs->tasklet);
1204 h2s->subs->events &= ~SUB_RETRY_SEND;
1205 if (!h2s->subs->events)
1206 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001207 }
Willy Tarreau5723f292020-01-10 15:16:57 +01001208 else if (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) {
1209 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
1210 tasklet_wakeup(h2s->shut_tl);
1211 }
Willy Tarreau7e094452018-12-19 18:08:52 +01001212}
1213
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001214/* alerts the data layer, trying to wake it up by all means, following
1215 * this sequence :
1216 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1217 * - if its subscribed to send, then it's woken up for send
1218 * - if it was subscribed to neither, its ->wake() callback is called
1219 * It is safe to call this function with a closed stream which doesn't have a
1220 * conn_stream anymore.
1221 */
1222static void __maybe_unused h2s_alert(struct h2s *h2s)
1223{
Willy Tarreau7838a792019-08-12 18:42:03 +02001224 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1225
Willy Tarreauf96508a2020-01-10 11:12:48 +01001226 if (h2s->subs ||
Willy Tarreau5723f292020-01-10 15:16:57 +01001227 (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW))) {
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001228 h2s_notify_recv(h2s);
1229 h2s_notify_send(h2s);
1230 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001231 else if (h2s->cs && h2s->cs->data_cb->wake != NULL) {
1232 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001233 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau7838a792019-08-12 18:42:03 +02001234 }
1235
1236 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001237}
1238
Willy Tarreaue4820742017-07-27 13:37:23 +02001239/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001240static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001241{
1242 uint8_t *out = frame;
1243
1244 *out = len >> 16;
1245 write_n16(out + 1, len);
1246}
1247
Willy Tarreau54c15062017-10-10 17:10:03 +02001248/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1249 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1250 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001251 * available in the buffer's input prior to calling this function. The buffer
1252 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001253 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001254static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001255 const struct buffer *b, int o)
1256{
Willy Tarreau591d4452018-06-15 17:21:00 +02001257 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 +02001258}
1259
Willy Tarreau1f094672017-11-20 21:27:45 +01001260static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001261{
Willy Tarreau591d4452018-06-15 17:21:00 +02001262 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001263}
1264
Willy Tarreau1f094672017-11-20 21:27:45 +01001265static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001266{
Willy Tarreau591d4452018-06-15 17:21:00 +02001267 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001268}
1269
Willy Tarreau1f094672017-11-20 21:27:45 +01001270static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001271{
Willy Tarreau591d4452018-06-15 17:21:00 +02001272 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001273}
1274
1275
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001276/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1277 * The algorithm is not obvious. It turns out that H2 headers are neither
1278 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1279 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001280 *
1281 * b0 b1 b2 b3 b4 b5..b8
1282 * +----------+---------+--------+----+----+----------------------+
1283 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1284 * +----------+---------+--------+----+----+----------------------+
1285 *
1286 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1287 * we get the sid properly aligned and ordered, and 16 bits of len properly
1288 * ordered as well. The type and flags can be extracted using bit shifts from
1289 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001290 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1291 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001292 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001293static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001294{
1295 uint64_t w;
1296
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001297 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001298 return 0;
1299
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001300 w = h2_get_n64(b, o + 1);
1301 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001302 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1303 h->ff = w >> 32;
1304 h->ft = w >> 40;
1305 h->len += w >> 48;
1306 return 1;
1307}
1308
1309/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1310 * h2_peek_frame_hdr() above.
1311 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001312static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001313{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001314 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001315}
1316
1317/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001318static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001319{
1320 int ret;
1321
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001322 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001323 if (ret > 0)
1324 h2_skip_frame_hdr(b);
1325 return ret;
1326}
1327
Willy Tarreaucb985a42019-10-07 16:56:34 +02001328
1329/* try to fragment the headers frame present at the beginning of buffer <b>,
1330 * enforcing a limit of <mfs> bytes per frame. Returns 0 on failure, 1 on
1331 * success. Typical causes of failure include a buffer not large enough to
1332 * add extra frame headers. The existing frame size is read in the current
1333 * frame. Its EH flag will be cleared if CONTINUATION frames need to be added,
1334 * and its length will be adjusted. The stream ID for continuation frames will
1335 * be copied from the initial frame's.
1336 */
1337static int h2_fragment_headers(struct buffer *b, uint32_t mfs)
1338{
1339 size_t remain = b->data - 9;
1340 int extra_frames = (remain - 1) / mfs;
1341 size_t fsize;
1342 char *fptr;
1343 int frame;
1344
1345 if (b->data <= mfs + 9)
1346 return 1;
1347
1348 /* Too large a frame, we need to fragment it using CONTINUATION
1349 * frames. We start from the end and move tails as needed.
1350 */
1351 if (b->data + extra_frames * 9 > b->size)
1352 return 0;
1353
1354 for (frame = extra_frames; frame; frame--) {
1355 fsize = ((remain - 1) % mfs) + 1;
1356 remain -= fsize;
1357
1358 /* move data */
1359 fptr = b->area + 9 + remain + (frame - 1) * 9;
1360 memmove(fptr + 9, b->area + 9 + remain, fsize);
1361 b->data += 9;
1362
1363 /* write new frame header */
1364 h2_set_frame_size(fptr, fsize);
1365 fptr[3] = H2_FT_CONTINUATION;
1366 fptr[4] = (frame == extra_frames) ? H2_F_HEADERS_END_HEADERS : 0;
1367 write_n32(fptr + 5, read_n32(b->area + 5));
1368 }
1369
1370 b->area[4] &= ~H2_F_HEADERS_END_HEADERS;
1371 h2_set_frame_size(b->area, remain);
1372 return 1;
1373}
1374
1375
Willy Tarreau00dd0782018-03-01 16:31:34 +01001376/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1377 * its connection if the stream was not yet closed. Please use this exclusively
1378 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001379 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001380static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001381{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001382 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001383 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001384 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001385 if (!h2s->id)
1386 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +01001387 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +01001388 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
1389 h2s_notify_recv(h2s);
1390 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001391 HA_ATOMIC_DEC(&h2s->h2c->px_counters->open_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001392
Willy Tarreau7838a792019-08-12 18:42:03 +02001393 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001394 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001395 h2s->st = H2_SS_CLOSED;
1396}
1397
Willy Tarreau71049cc2018-03-28 13:56:39 +02001398/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001399/* h2s_destroy should only ever be called by the thread that owns the stream,
1400 * that means that a tasklet should be used if we want to destroy the h2s
1401 * from another thread
1402 */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001403static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001404{
Willy Tarreau7838a792019-08-12 18:42:03 +02001405 struct connection *conn = h2s->h2c->conn;
1406
1407 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1408
Willy Tarreau0a10de62018-03-01 16:27:53 +01001409 h2s_close(h2s);
1410 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001411 if (b_size(&h2s->rxbuf)) {
1412 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001413 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02001414 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001415
1416 if (h2s->subs)
1417 h2s->subs->events = 0;
1418
Joseph Herlantd77575d2018-11-25 10:54:45 -08001419 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001420 * reference left would be in the h2c send_list/fctl_list, and if
1421 * we're in it, we're getting out anyway
1422 */
Olivier Houchardd360ac62019-03-22 17:37:16 +01001423 LIST_DEL_INIT(&h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01001424
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001425 /* ditto, calling tasklet_free() here should be ok */
Willy Tarreau5723f292020-01-10 15:16:57 +01001426 tasklet_free(h2s->shut_tl);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001427 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001428
1429 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001430}
1431
Willy Tarreaua8e49542018-10-03 18:53:55 +02001432/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1433 * stream tree. In case of error, nothing is added and NULL is returned. The
1434 * causes of errors can be any failed memory allocation. The caller is
1435 * responsible for checking if the connection may support an extra stream
1436 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001437 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001438static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001439{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001440 struct h2s *h2s;
1441
Willy Tarreau7838a792019-08-12 18:42:03 +02001442 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1443
Willy Tarreaubafbe012017-11-24 17:34:44 +01001444 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001445 if (!h2s)
1446 goto out;
1447
Willy Tarreau5723f292020-01-10 15:16:57 +01001448 h2s->shut_tl = tasklet_new();
1449 if (!h2s->shut_tl) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001450 pool_free(pool_head_h2s, h2s);
1451 goto out;
1452 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001453 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01001454 h2s->shut_tl->process = h2_deferred_shut;
1455 h2s->shut_tl->context = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001456 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001457 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001458 h2s->cs = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001459 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001460 h2s->flags = H2_SF_NONE;
1461 h2s->errcode = H2_ERR_NO_ERROR;
1462 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001463 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001464 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001465 h2s->rxbuf = BUF_NULL;
Amaury Denoyelle74162742020-12-11 17:53:05 +01001466 memset(h2s->upgrade_protocol, 0, sizeof(h2s->upgrade_protocol));
Willy Tarreau751f2d02018-10-05 09:35:00 +02001467
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001468 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001469 if (id > 0)
1470 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001471 else
1472 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001473
1474 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001475 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001476 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001477
Willy Tarreau4781b152021-04-06 13:53:36 +02001478 HA_ATOMIC_INC(&h2c->px_counters->open_streams);
1479 HA_ATOMIC_INC(&h2c->px_counters->total_streams);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001480
Willy Tarreau7838a792019-08-12 18:42:03 +02001481 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001482 return h2s;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001483 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001484 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001485 return NULL;
1486}
1487
1488/* creates a new stream <id> on the h2c connection and returns it, or NULL in
Christopher Faulet7d013e72020-12-15 16:56:50 +01001489 * case of memory allocation error. <input> is used as input buffer for the new
1490 * stream. On success, it is transferred to the stream and the mux is no longer
1491 * responsible of it. On error, <input> is unchanged, thus the mux must still
1492 * take care of it.
Willy Tarreaua8e49542018-10-03 18:53:55 +02001493 */
Christopher Faulet7d013e72020-12-15 16:56:50 +01001494static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id, struct buffer *input)
Willy Tarreaua8e49542018-10-03 18:53:55 +02001495{
1496 struct session *sess = h2c->conn->owner;
1497 struct conn_stream *cs;
1498 struct h2s *h2s;
1499
Willy Tarreau7838a792019-08-12 18:42:03 +02001500 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1501
Willy Tarreaua8e49542018-10-03 18:53:55 +02001502 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1503 goto out;
1504
1505 h2s = h2s_new(h2c, id);
1506 if (!h2s)
1507 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001508
Christopher Faulet236c93b2020-07-02 09:19:54 +02001509 cs = cs_new(h2c->conn, h2c->conn->target);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001510 if (!cs)
1511 goto out_close;
1512
Olivier Houchard746fb772018-12-15 19:42:00 +01001513 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001514 h2s->cs = cs;
1515 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001516 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001517
Christopher Faulet7d013e72020-12-15 16:56:50 +01001518 if (stream_create_from_cs(cs, input) < 0)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001519 goto out_free_cs;
1520
Willy Tarreau590a0512018-09-05 11:56:48 +02001521 /* We want the accept date presented to the next stream to be the one
1522 * we have now, the handshake time to be null (since the next stream
1523 * is not delayed by a handshake), and the idle time to count since
1524 * right now.
1525 */
1526 sess->accept_date = date;
1527 sess->tv_accept = now;
1528 sess->t_handshake = 0;
1529
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001530 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001531 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001532 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001533 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001534 return h2s;
1535
1536 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001537 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001538 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001539 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001540 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001541 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001542 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001543 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001544 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001545 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001546}
1547
Willy Tarreau751f2d02018-10-05 09:35:00 +02001548/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1549 * and returns it, or NULL in case of memory allocation error or if the highest
1550 * possible stream ID was reached.
1551 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001552static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001553{
1554 struct h2s *h2s = NULL;
1555
Willy Tarreau7838a792019-08-12 18:42:03 +02001556 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1557
Willy Tarreau86949782019-01-31 10:42:05 +01001558 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001559 goto out;
1560
Willy Tarreaua80dca82019-01-24 17:08:28 +01001561 if (h2_streams_left(h2c) < 1)
1562 goto out;
1563
Willy Tarreau751f2d02018-10-05 09:35:00 +02001564 /* Defer choosing the ID until we send the first message to create the stream */
1565 h2s = h2s_new(h2c, 0);
1566 if (!h2s)
1567 goto out;
1568
1569 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001570 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001571 cs->ctx = h2s;
1572 h2c->nb_cs++;
1573
Willy Tarreau751f2d02018-10-05 09:35:00 +02001574 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001575 if (likely(h2s))
1576 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1577 else
1578 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001579 return h2s;
1580}
1581
Willy Tarreaube5b7152017-09-25 16:25:39 +02001582/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1583 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1584 * the various settings codes.
1585 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001586static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001587{
1588 struct buffer *res;
1589 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001590 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001591 int mfs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001592 int ret = 0;
1593
1594 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001595
1596 if (h2c_mux_busy(h2c, NULL)) {
1597 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001598 goto out;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001599 }
1600
Willy Tarreaube5b7152017-09-25 16:25:39 +02001601 chunk_init(&buf, buf_data, sizeof(buf_data));
1602 chunk_memcpy(&buf,
1603 "\x00\x00\x00" /* length : 0 for now */
1604 "\x04\x00" /* type : 4 (settings), flags : 0 */
1605 "\x00\x00\x00\x00", /* stream ID : 0 */
1606 9);
1607
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001608 if (h2c->flags & H2_CF_IS_BACK) {
1609 /* send settings_enable_push=0 */
1610 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1611 }
1612
Amaury Denoyellebefeae82021-07-09 17:14:30 +02001613 /* rfc 8441 #3 SETTINGS_ENABLE_CONNECT_PROTOCOL=1,
1614 * sent automatically unless disabled in the global config */
1615 if (!(global.tune.options & GTUNE_DISABLE_H2_WEBSOCKET))
1616 chunk_memcat(&buf, "\x00\x08\x00\x00\x00\x01", 6);
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01001617
Willy Tarreaube5b7152017-09-25 16:25:39 +02001618 if (h2_settings_header_table_size != 4096) {
1619 char str[6] = "\x00\x01"; /* header_table_size */
1620
1621 write_n32(str + 2, h2_settings_header_table_size);
1622 chunk_memcat(&buf, str, 6);
1623 }
1624
1625 if (h2_settings_initial_window_size != 65535) {
1626 char str[6] = "\x00\x04"; /* initial_window_size */
1627
1628 write_n32(str + 2, h2_settings_initial_window_size);
1629 chunk_memcat(&buf, str, 6);
1630 }
1631
1632 if (h2_settings_max_concurrent_streams != 0) {
1633 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1634
1635 /* Note: 0 means "unlimited" for haproxy's config but not for
1636 * the protocol, so never send this value!
1637 */
1638 write_n32(str + 2, h2_settings_max_concurrent_streams);
1639 chunk_memcat(&buf, str, 6);
1640 }
1641
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001642 mfs = h2_settings_max_frame_size;
1643 if (mfs > global.tune.bufsize)
1644 mfs = global.tune.bufsize;
1645
1646 if (!mfs)
1647 mfs = global.tune.bufsize;
1648
1649 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001650 char str[6] = "\x00\x05"; /* max_frame_size */
1651
1652 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1653 * match bufsize - rewrite size, but at the moment it seems
1654 * that clients don't take care of it.
1655 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001656 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001657 chunk_memcat(&buf, str, 6);
1658 }
1659
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001660 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001661
1662 res = br_tail(h2c->mbuf);
1663 retry:
1664 if (!h2_get_buf(h2c, res)) {
1665 h2c->flags |= H2_CF_MUX_MALLOC;
1666 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001667 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001668 }
1669
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001670 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001671 if (unlikely(ret <= 0)) {
1672 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001673 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1674 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001675 h2c->flags |= H2_CF_MUX_MFULL;
1676 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001677 }
1678 else {
1679 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001680 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001681 }
1682 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001683 out:
1684 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001685 return ret;
1686}
1687
Willy Tarreau52eed752017-09-22 15:05:09 +02001688/* Try to receive a connection preface, then upon success try to send our
1689 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1690 * missing data. It may return an error in h2c.
1691 */
1692static int h2c_frt_recv_preface(struct h2c *h2c)
1693{
1694 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001695 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001696
Willy Tarreau7838a792019-08-12 18:42:03 +02001697 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1698
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001699 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001700
1701 if (unlikely(ret1 <= 0)) {
Amaury Denoyellea8879232020-10-27 17:16:03 +01001702 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01001703 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 +02001704 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauee4684f2021-06-17 08:08:48 +02001705 if (b_data(&h2c->dbuf) ||
1706 !(((const struct session *)h2c->conn->owner)->fe->options & PR_O_IGNORE_PRB))
1707 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01001708 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001709 ret2 = 0;
1710 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001711 }
1712
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001713 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001714 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001715 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001716 out:
1717 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001718 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001719}
1720
Willy Tarreau01b44822018-10-03 14:26:37 +02001721/* Try to send a connection preface, then upon success try to send our
1722 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1723 * missing data. It may return an error in h2c.
1724 */
1725static int h2c_bck_send_preface(struct h2c *h2c)
1726{
1727 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001728 int ret = 0;
1729
1730 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001731
1732 if (h2c_mux_busy(h2c, NULL)) {
1733 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001734 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001735 }
1736
Willy Tarreaubcc45952019-05-26 10:05:50 +02001737 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001738 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001739 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001740 h2c->flags |= H2_CF_MUX_MALLOC;
1741 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001742 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001743 }
1744
1745 if (!b_data(res)) {
1746 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001747 ret = b_istput(res, ist(H2_CONN_PREFACE));
1748 if (unlikely(ret <= 0)) {
1749 if (!ret) {
1750 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1751 goto retry;
1752 h2c->flags |= H2_CF_MUX_MFULL;
1753 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001754 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001755 }
1756 else {
1757 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001758 ret = 0;
1759 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001760 }
1761 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001762 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001763 ret = h2c_send_settings(h2c);
1764 out:
1765 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1766 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001767}
1768
Willy Tarreau081d4722017-05-16 21:51:05 +02001769/* try to send a GOAWAY frame on the connection to report an error or a graceful
1770 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1771 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1772 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1773 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1774 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1775 * on unrecoverable failure. It will not attempt to send one again in this last
1776 * case so that it is safe to use h2c_error() to report such errors.
1777 */
1778static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1779{
1780 struct buffer *res;
1781 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001782 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001783
Willy Tarreau7838a792019-08-12 18:42:03 +02001784 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1785
1786 if (h2c->flags & H2_CF_GOAWAY_FAILED) {
1787 ret = 1; // claim that it worked
1788 goto out;
1789 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001790
1791 if (h2c_mux_busy(h2c, h2s)) {
1792 if (h2s)
1793 h2s->flags |= H2_SF_BLK_MBUSY;
1794 else
1795 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001796 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001797 }
1798
Willy Tarreau9c218e72019-05-26 10:08:28 +02001799 /* len: 8, type: 7, flags: none, sid: 0 */
1800 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1801
1802 if (h2c->last_sid < 0)
1803 h2c->last_sid = h2c->max_id;
1804
1805 write_n32(str + 9, h2c->last_sid);
1806 write_n32(str + 13, h2c->errcode);
1807
Willy Tarreaubcc45952019-05-26 10:05:50 +02001808 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001809 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001810 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001811 h2c->flags |= H2_CF_MUX_MALLOC;
1812 if (h2s)
1813 h2s->flags |= H2_SF_BLK_MROOM;
1814 else
1815 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001816 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001817 }
1818
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001819 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001820 if (unlikely(ret <= 0)) {
1821 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001822 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1823 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001824 h2c->flags |= H2_CF_MUX_MFULL;
1825 if (h2s)
1826 h2s->flags |= H2_SF_BLK_MROOM;
1827 else
1828 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001829 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001830 }
1831 else {
1832 /* we cannot report this error using GOAWAY, so we mark
1833 * it and claim a success.
1834 */
1835 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1836 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001837 ret = 1;
1838 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001839 }
1840 }
1841 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001842
1843 /* some codes are not for real errors, just attempts to close cleanly */
1844 switch (h2c->errcode) {
1845 case H2_ERR_NO_ERROR:
1846 case H2_ERR_ENHANCE_YOUR_CALM:
1847 case H2_ERR_REFUSED_STREAM:
1848 case H2_ERR_CANCEL:
1849 break;
1850 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001851 HA_ATOMIC_INC(&h2c->px_counters->goaway_resp);
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001852 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001853 out:
1854 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001855 return ret;
1856}
1857
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001858/* Try to send an RST_STREAM frame on the connection for the indicated stream
1859 * during mux operations. This stream must be valid and cannot be closed
1860 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1861 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1862 * not yet.
1863 *
1864 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1865 * to write the message, it subscribes the stream to future notifications.
1866 */
1867static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1868{
1869 struct buffer *res;
1870 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001871 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001872
Willy Tarreau7838a792019-08-12 18:42:03 +02001873 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1874
1875 if (!h2s || h2s->st == H2_SS_CLOSED) {
1876 ret = 1;
1877 goto out;
1878 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001879
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001880 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1881 * RST_STREAM in response to a RST_STREAM frame.
1882 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001883 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001884 ret = 1;
1885 goto ignore;
1886 }
1887
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001888 if (h2c_mux_busy(h2c, h2s)) {
1889 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001890 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001891 }
1892
Willy Tarreau9c218e72019-05-26 10:08:28 +02001893 /* len: 4, type: 3, flags: none */
1894 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1895 write_n32(str + 5, h2s->id);
1896 write_n32(str + 9, h2s->errcode);
1897
Willy Tarreaubcc45952019-05-26 10:05:50 +02001898 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001899 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001900 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001901 h2c->flags |= H2_CF_MUX_MALLOC;
1902 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001903 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001904 }
1905
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001906 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001907 if (unlikely(ret <= 0)) {
1908 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001909 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1910 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001911 h2c->flags |= H2_CF_MUX_MFULL;
1912 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001913 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001914 }
1915 else {
1916 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001917 ret = 0;
1918 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001919 }
1920 }
1921
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001922 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001923 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001924 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001925 out:
1926 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001927 return ret;
1928}
1929
1930/* Try to send an RST_STREAM frame on the connection for the stream being
1931 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001932 * error code, even if the stream is one of the dummy ones, and will update
1933 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001934 *
1935 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1936 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001937 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001938 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001939 */
1940static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1941{
1942 struct buffer *res;
1943 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001944 int ret = 0;
1945
1946 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001947
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001948 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1949 * RST_STREAM in response to a RST_STREAM frame.
1950 */
1951 if (h2c->dft == H2_FT_RST_STREAM) {
1952 ret = 1;
1953 goto ignore;
1954 }
1955
Willy Tarreau27a84c92017-10-17 08:10:17 +02001956 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001957 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001958 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001959 }
1960
Willy Tarreau9c218e72019-05-26 10:08:28 +02001961 /* len: 4, type: 3, flags: none */
1962 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1963
1964 write_n32(str + 5, h2c->dsi);
1965 write_n32(str + 9, h2s->errcode);
1966
Willy Tarreaubcc45952019-05-26 10:05:50 +02001967 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001968 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001969 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001970 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001971 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001972 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001973 }
1974
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001975 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001976 if (unlikely(ret <= 0)) {
1977 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001978 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1979 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001980 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001981 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001982 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001983 }
1984 else {
1985 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001986 ret = 0;
1987 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001988 }
1989 }
1990
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001991 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001992 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001993 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001994 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001995 }
1996
Willy Tarreau7838a792019-08-12 18:42:03 +02001997 out:
Willy Tarreau4781b152021-04-06 13:53:36 +02001998 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_resp);
Willy Tarreau7838a792019-08-12 18:42:03 +02001999 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002000 return ret;
2001}
2002
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002003/* try to send an empty DATA frame with the ES flag set to notify about the
2004 * end of stream and match a shutdown(write). If an ES was already sent as
2005 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
2006 * on success or zero if nothing was done. In case of lack of room to write the
2007 * message, it subscribes the requesting stream to future notifications.
2008 */
2009static int h2_send_empty_data_es(struct h2s *h2s)
2010{
2011 struct h2c *h2c = h2s->h2c;
2012 struct buffer *res;
2013 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002014 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002015
Willy Tarreau7838a792019-08-12 18:42:03 +02002016 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
2017
2018 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
2019 ret = 1;
2020 goto out;
2021 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002022
2023 if (h2c_mux_busy(h2c, h2s)) {
2024 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002025 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002026 }
2027
Willy Tarreau9c218e72019-05-26 10:08:28 +02002028 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
2029 memcpy(str, "\x00\x00\x00\x00\x01", 5);
2030 write_n32(str + 5, h2s->id);
2031
Willy Tarreaubcc45952019-05-26 10:05:50 +02002032 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002033 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002034 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002035 h2c->flags |= H2_CF_MUX_MALLOC;
2036 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002037 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002038 }
2039
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002040 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002041 if (likely(ret > 0)) {
2042 h2s->flags |= H2_SF_ES_SENT;
2043 }
2044 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002045 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2046 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002047 h2c->flags |= H2_CF_MUX_MFULL;
2048 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002049 }
2050 else {
2051 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002052 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002053 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002054 out:
2055 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002056 return ret;
2057}
2058
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002059/* wake a specific stream and assign its conn_stream some CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02002060 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002061 * is automatically updated accordingly. If the stream is orphaned, it is
2062 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01002063 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002064static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01002065{
Willy Tarreau7838a792019-08-12 18:42:03 +02002066 struct h2c *h2c = h2s->h2c;
2067
2068 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
2069
Christopher Fauletf02ca002019-03-07 16:21:34 +01002070 if (!h2s->cs) {
2071 /* this stream was already orphaned */
2072 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002073 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002074 return;
2075 }
2076
Christopher Fauletaade4ed2020-10-08 15:38:41 +02002077 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002078 if (h2s->st == H2_SS_OPEN)
2079 h2s->st = H2_SS_HREM;
2080 else if (h2s->st == H2_SS_HLOC)
2081 h2s_close(h2s);
2082 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002083
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002084 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
2085 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
2086 h2s->cs->flags |= CS_FL_ERR_PENDING;
2087 if (h2s->cs->flags & CS_FL_EOS)
2088 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02002089
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002090 if (h2s->st < H2_SS_ERROR)
2091 h2s->st = H2_SS_ERROR;
2092 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01002093
2094 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002095 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002096}
2097
2098/* wake the streams attached to the connection, whose id is greater than <last>
2099 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002100 */
Willy Tarreau23482912019-05-07 15:23:14 +02002101static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002102{
2103 struct eb32_node *node;
2104 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002105
Willy Tarreau7838a792019-08-12 18:42:03 +02002106 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
2107
Christopher Fauletf02ca002019-03-07 16:21:34 +01002108 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002109 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
2110 while (node) {
2111 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002112 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002113 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002114 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002115
Christopher Fauletf02ca002019-03-07 16:21:34 +01002116 /* Wake all streams with unassigned ID (ID == 0) */
2117 node = eb32_lookup(&h2c->streams_by_id, 0);
2118 while (node) {
2119 h2s = container_of(node, struct h2s, by_id);
2120 if (h2s->id > 0)
2121 break;
2122 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002123 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002124 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002125
2126 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002127}
2128
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002129/* Wake up all blocked streams whose window size has become positive after the
2130 * mux's initial window was adjusted. This should be done after having processed
2131 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002132 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002133static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02002134{
2135 struct h2s *h2s;
2136 struct eb32_node *node;
2137
Willy Tarreau7838a792019-08-12 18:42:03 +02002138 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2139
Willy Tarreau3421aba2017-07-27 15:41:03 +02002140 node = eb32_first(&h2c->streams_by_id);
2141 while (node) {
2142 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002143 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002144 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002145 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002146 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2147 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002148 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002149 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002150 node = eb32_next(node);
2151 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002152
2153 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002154}
2155
2156/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
2157 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01002158 * return an error in h2c. The caller must have already verified frame length
2159 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002160 */
2161static int h2c_handle_settings(struct h2c *h2c)
2162{
2163 unsigned int offset;
2164 int error;
2165
Willy Tarreau7838a792019-08-12 18:42:03 +02002166 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
2167
Willy Tarreau3421aba2017-07-27 15:41:03 +02002168 if (h2c->dff & H2_F_SETTINGS_ACK) {
2169 if (h2c->dfl) {
2170 error = H2_ERR_FRAME_SIZE_ERROR;
2171 goto fail;
2172 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002173 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002174 }
2175
Willy Tarreau3421aba2017-07-27 15:41:03 +02002176 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002177 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002178 goto out0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002179
2180 /* parse the frame */
2181 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002182 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2183 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002184
2185 switch (type) {
2186 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2187 /* we need to update all existing streams with the
2188 * difference from the previous iws.
2189 */
2190 if (arg < 0) { // RFC7540#6.5.2
2191 error = H2_ERR_FLOW_CONTROL_ERROR;
2192 goto fail;
2193 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002194 h2c->miw = arg;
2195 break;
2196 case H2_SETTINGS_MAX_FRAME_SIZE:
2197 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002198 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 +02002199 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002200 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002201 goto fail;
2202 }
2203 h2c->mfs = arg;
2204 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002205 case H2_SETTINGS_ENABLE_PUSH:
2206 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002207 TRACE_ERROR("ENABLE_PUSH out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002208 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002209 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002210 goto fail;
2211 }
2212 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002213 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2214 if (h2c->flags & H2_CF_IS_BACK) {
2215 /* the limit is only for the backend; for the frontend it is our limit */
2216 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
2217 arg = h2_settings_max_concurrent_streams;
2218 h2c->streams_limit = arg;
2219 }
2220 break;
Amaury Denoyellef9dcbee2020-12-11 17:53:10 +01002221 case H2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
2222 /* nothing to do here as this settings is automatically
2223 * transmits to the client */
2224 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002225 }
2226 }
2227
2228 /* need to ACK this frame now */
2229 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002230 done:
2231 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002232 return 1;
2233 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002234 if (!(h2c->flags & H2_CF_IS_BACK))
2235 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002236 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002237 out0:
2238 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 +02002239 return 0;
2240}
2241
2242/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2243 * success or one of the h2_status values.
2244 */
2245static int h2c_ack_settings(struct h2c *h2c)
2246{
2247 struct buffer *res;
2248 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002249 int ret = 0;
2250
2251 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002252
2253 if (h2c_mux_busy(h2c, NULL)) {
2254 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002255 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002256 }
2257
Willy Tarreau9c218e72019-05-26 10:08:28 +02002258 memcpy(str,
2259 "\x00\x00\x00" /* length : 0 (no data) */
2260 "\x04" "\x01" /* type : 4, flags : ACK */
2261 "\x00\x00\x00\x00" /* stream ID */, 9);
2262
Willy Tarreaubcc45952019-05-26 10:05:50 +02002263 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002264 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002265 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002266 h2c->flags |= H2_CF_MUX_MALLOC;
2267 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002268 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002269 }
2270
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002271 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002272 if (unlikely(ret <= 0)) {
2273 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002274 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2275 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002276 h2c->flags |= H2_CF_MUX_MFULL;
2277 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002278 }
2279 else {
2280 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002281 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002282 }
2283 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002284 out:
2285 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002286 return ret;
2287}
2288
Willy Tarreaucf68c782017-10-10 17:11:41 +02002289/* processes a PING frame and schedules an ACK if needed. The caller must pass
2290 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002291 * missing data. The caller must have already verified frame length
2292 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002293 */
2294static int h2c_handle_ping(struct h2c *h2c)
2295{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002296 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002297 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002298 h2c->st0 = H2_CS_FRAME_A;
2299 return 1;
2300}
2301
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002302/* Try to send a window update for stream id <sid> and value <increment>.
2303 * Returns > 0 on success or zero on missing room or failure. It may return an
2304 * error in h2c.
2305 */
2306static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2307{
2308 struct buffer *res;
2309 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002310 int ret = 0;
2311
2312 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002313
2314 if (h2c_mux_busy(h2c, NULL)) {
2315 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002316 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002317 }
2318
Willy Tarreau9c218e72019-05-26 10:08:28 +02002319 /* length: 4, type: 8, flags: none */
2320 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2321 write_n32(str + 5, sid);
2322 write_n32(str + 9, increment);
2323
Willy Tarreaubcc45952019-05-26 10:05:50 +02002324 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002325 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002326 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002327 h2c->flags |= H2_CF_MUX_MALLOC;
2328 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002329 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002330 }
2331
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002332 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002333 if (unlikely(ret <= 0)) {
2334 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002335 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2336 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002337 h2c->flags |= H2_CF_MUX_MFULL;
2338 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002339 }
2340 else {
2341 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002342 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002343 }
2344 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002345 out:
2346 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002347 return ret;
2348}
2349
2350/* try to send pending window update for the connection. It's safe to call it
2351 * with no pending updates. Returns > 0 on success or zero on missing room or
2352 * failure. It may return an error in h2c.
2353 */
2354static int h2c_send_conn_wu(struct h2c *h2c)
2355{
2356 int ret = 1;
2357
Willy Tarreau7838a792019-08-12 18:42:03 +02002358 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2359
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002360 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002361 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002362
Willy Tarreau97aaa672018-12-23 09:49:04 +01002363 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2364 /* increase the advertised connection window to 2G on
2365 * first update.
2366 */
2367 h2c->flags |= H2_CF_WINDOW_OPENED;
2368 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2369 }
2370
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002371 /* send WU for the connection */
2372 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2373 if (ret > 0)
2374 h2c->rcvd_c = 0;
2375
Willy Tarreau7838a792019-08-12 18:42:03 +02002376 out:
2377 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002378 return ret;
2379}
2380
2381/* try to send pending window update for the current dmux stream. It's safe to
2382 * call it with no pending updates. Returns > 0 on success or zero on missing
2383 * room or failure. It may return an error in h2c.
2384 */
2385static int h2c_send_strm_wu(struct h2c *h2c)
2386{
2387 int ret = 1;
2388
Willy Tarreau7838a792019-08-12 18:42:03 +02002389 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2390
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002391 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002392 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002393
2394 /* send WU for the stream */
2395 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2396 if (ret > 0)
2397 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002398 out:
2399 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002400 return ret;
2401}
2402
Willy Tarreaucf68c782017-10-10 17:11:41 +02002403/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2404 * success, 0 on missing data or one of the h2_status values.
2405 */
2406static int h2c_ack_ping(struct h2c *h2c)
2407{
2408 struct buffer *res;
2409 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002410 int ret = 0;
2411
2412 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002413
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002414 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002415 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002416
2417 if (h2c_mux_busy(h2c, NULL)) {
2418 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002419 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002420 }
2421
Willy Tarreaucf68c782017-10-10 17:11:41 +02002422 memcpy(str,
2423 "\x00\x00\x08" /* length : 8 (same payload) */
2424 "\x06" "\x01" /* type : 6, flags : ACK */
2425 "\x00\x00\x00\x00" /* stream ID */, 9);
2426
2427 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002428 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002429
Willy Tarreau9c218e72019-05-26 10:08:28 +02002430 res = br_tail(h2c->mbuf);
2431 retry:
2432 if (!h2_get_buf(h2c, res)) {
2433 h2c->flags |= H2_CF_MUX_MALLOC;
2434 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002435 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002436 }
2437
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002438 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002439 if (unlikely(ret <= 0)) {
2440 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002441 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2442 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002443 h2c->flags |= H2_CF_MUX_MFULL;
2444 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002445 }
2446 else {
2447 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002448 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002449 }
2450 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002451 out:
2452 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002453 return ret;
2454}
2455
Willy Tarreau26f95952017-07-27 17:18:30 +02002456/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2457 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002458 * h2c or h2s. The caller must have already verified frame length and stream ID
2459 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002460 */
2461static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2462{
2463 int32_t inc;
2464 int error;
2465
Willy Tarreau7838a792019-08-12 18:42:03 +02002466 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2467
Willy Tarreau26f95952017-07-27 17:18:30 +02002468 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002469 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002470 goto out0;
Willy Tarreau26f95952017-07-27 17:18:30 +02002471
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002472 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002473
2474 if (h2c->dsi != 0) {
2475 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002476
2477 /* it's not an error to receive WU on a closed stream */
2478 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002479 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002480
2481 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002482 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 +02002483 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002484 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002485 goto strm_err;
2486 }
2487
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002488 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002489 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 +02002490 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002491 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002492 goto strm_err;
2493 }
2494
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002495 h2s->sws += inc;
2496 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002497 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002498 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002499 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2500 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreau2b718102021-04-21 07:32:39 +02002501 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002502 }
2503 }
2504 else {
2505 /* connection window update */
2506 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002507 TRACE_ERROR("conn WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002508 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002509 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau26f95952017-07-27 17:18:30 +02002510 goto conn_err;
2511 }
2512
2513 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2514 error = H2_ERR_FLOW_CONTROL_ERROR;
2515 goto conn_err;
2516 }
2517
2518 h2c->mws += inc;
2519 }
2520
Willy Tarreau7838a792019-08-12 18:42:03 +02002521 done:
2522 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002523 return 1;
2524
2525 conn_err:
2526 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002527 out0:
2528 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 +02002529 return 0;
2530
2531 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002532 h2s_error(h2s, error);
2533 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002534 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002535 return 0;
2536}
2537
Willy Tarreaue96b0922017-10-30 00:28:29 +01002538/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002539 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2540 * have already verified frame length and stream ID validity. Described in
2541 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002542 */
2543static int h2c_handle_goaway(struct h2c *h2c)
2544{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002545 int last;
2546
Willy Tarreau7838a792019-08-12 18:42:03 +02002547 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002548 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002549 if (b_data(&h2c->dbuf) < h2c->dfl) {
2550 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002551 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002552 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002553
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002554 last = h2_get_n32(&h2c->dbuf, 0);
2555 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002556 if (h2c->last_sid < 0)
2557 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002558 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002559 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002560 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002561}
2562
Willy Tarreau92153fc2017-12-03 19:46:19 +01002563/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002564 * invalid. Returns > 0 on success or zero on missing data. It may return an
2565 * error in h2c. The caller must have already verified frame length and stream
2566 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002567 */
2568static int h2c_handle_priority(struct h2c *h2c)
2569{
Willy Tarreau7838a792019-08-12 18:42:03 +02002570 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2571
Willy Tarreau92153fc2017-12-03 19:46:19 +01002572 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002573 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002574 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002575 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002576 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002577
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002578 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002579 /* 7540#5.3 : can't depend on itself */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002580 TRACE_ERROR("PRIORITY depends on itself", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002581 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02002582 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002583 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002584 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002585 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002586 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002587 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002588}
2589
Willy Tarreaucd234e92017-08-18 10:59:39 +02002590/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002591 * Returns > 0 on success or zero on missing data. The caller must have already
2592 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002593 */
2594static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2595{
Willy Tarreau7838a792019-08-12 18:42:03 +02002596 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2597
Willy Tarreaucd234e92017-08-18 10:59:39 +02002598 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002599 if (b_data(&h2c->dbuf) < h2c->dfl) {
2600 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002601 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002602 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002603
2604 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002605 if (h2s->st == H2_SS_CLOSED) {
2606 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 +02002607 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002608 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002609
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002610 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002611 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002612
2613 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002614 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002615 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002616 }
2617
2618 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002619 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002620 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002621}
2622
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002623/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2624 * It may return an error in h2c or h2s. The caller must consider that the
2625 * return value is the new h2s in case one was allocated (most common case).
2626 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002627 * errors here are reported as connection errors since it's impossible to
2628 * recover from such errors after the compression context has been altered.
2629 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002630static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002631{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002632 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002633 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002634 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002635 int error;
2636
Willy Tarreau7838a792019-08-12 18:42:03 +02002637 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2638
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002639 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002640 goto out; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002641
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002642 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002643 goto out; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002644
2645 /* now either the frame is complete or the buffer is complete */
2646 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002647 /* The stream exists/existed, this must be a trailers frame */
2648 if (h2s->st != H2_SS_CLOSED) {
Amaury Denoyelle74162742020-12-11 17:53:05 +01002649 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len, NULL);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002650 /* unrecoverable error ? */
2651 if (h2c->st0 >= H2_CS_ERROR)
2652 goto out;
2653
2654 if (error == 0)
2655 goto out; // missing data
2656
2657 if (error < 0) {
2658 /* Failed to decode this frame (e.g. too large request)
2659 * but the HPACK decompressor is still synchronized.
2660 */
2661 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2662 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002663 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002664 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002665 goto done;
2666 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002667 /* the connection was already killed by an RST, let's consume
2668 * the data and send another RST.
2669 */
Amaury Denoyelle74162742020-12-11 17:53:05 +01002670 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau1f035502019-01-30 11:44:07 +01002671 h2s = (struct h2s*)h2_error_stream;
2672 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002673 }
2674 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2675 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2676 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002677 TRACE_ERROR("HEADERS on invalid stream ID", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau4781b152021-04-06 13:53:36 +02002678 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002679 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002680 goto conn_err;
2681 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002682 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2683 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002684
Amaury Denoyelle74162742020-12-11 17:53:05 +01002685 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002686
Willy Tarreau25919232019-01-03 14:48:18 +01002687 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002688 if (h2c->st0 >= H2_CS_ERROR)
2689 goto out;
2690
Willy Tarreau25919232019-01-03 14:48:18 +01002691 if (error <= 0) {
2692 if (error == 0)
2693 goto out; // missing data
2694
2695 /* Failed to decode this stream (e.g. too large request)
2696 * but the HPACK decompressor is still synchronized.
2697 */
2698 h2s = (struct h2s*)h2_error_stream;
2699 goto send_rst;
2700 }
2701
Willy Tarreau29268e92021-06-17 08:29:14 +02002702 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn, 0, &rxbuf);
2703
Willy Tarreau22de8d32018-09-05 19:55:58 +02002704 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002705 * positively from h2c_frt_stream_new(), the stream will report the error,
2706 * and if we return in error, h2c_frt_stream_new() will emit the error.
Christopher Faulet7d013e72020-12-15 16:56:50 +01002707 *
2708 * Xfer the rxbuf to the stream. On success, the new stream owns the
2709 * rxbuf. On error, it is released here.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002710 */
Christopher Faulet7d013e72020-12-15 16:56:50 +01002711 h2s = h2c_frt_stream_new(h2c, h2c->dsi, &rxbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002712 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002713 h2s = (struct h2s*)h2_refused_stream;
2714 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002715 }
2716
2717 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002718 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002719 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002720
Willy Tarreau88d138e2019-01-02 19:38:14 +01002721 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002722 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002723 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002724
2725 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002726 if (h2s->st == H2_SS_OPEN)
2727 h2s->st = H2_SS_HREM;
2728 else
2729 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002730 }
2731
Willy Tarreau3a429f02019-01-03 11:41:50 +01002732 /* update the max stream ID if the request is being processed */
2733 if (h2s->id > h2c->max_id)
2734 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002735
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002736 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002737
2738 conn_err:
2739 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002740 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002741
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002742 out:
2743 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002744 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002745 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002746
2747 send_rst:
2748 /* make the demux send an RST for the current stream. We may only
2749 * do this if we're certain that the HEADERS frame was properly
2750 * decompressed so that the HPACK decoder is still kept up to date.
2751 */
2752 h2_release_buf(h2c, &rxbuf);
2753 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002754
Willy Tarreau022e5e52020-09-10 09:33:15 +02002755 TRACE_USER("rejected H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn, 0, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002756 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002757 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002758}
2759
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002760/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2761 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2762 * errors here are reported as connection errors since it's impossible to
2763 * recover from such errors after the compression context has been altered.
2764 */
2765static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2766{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002767 struct buffer rxbuf = BUF_NULL;
2768 unsigned long long body_len = 0;
2769 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002770 int error;
2771
Willy Tarreau7838a792019-08-12 18:42:03 +02002772 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2773
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002774 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002775 goto fail; // empty buffer
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002776
2777 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002778 goto fail; // incomplete frame
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002779
Christopher Faulet6884aa32019-09-23 15:28:20 +02002780 if (h2s->st != H2_SS_CLOSED) {
Amaury Denoyelle74162742020-12-11 17:53:05 +01002781 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len, h2s->upgrade_protocol);
Christopher Faulet6884aa32019-09-23 15:28:20 +02002782 }
2783 else {
2784 /* the connection was already killed by an RST, let's consume
2785 * the data and send another RST.
2786 */
Amaury Denoyelle74162742020-12-11 17:53:05 +01002787 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len, NULL);
Christopher Fauletea7a7782019-09-26 16:19:13 +02002788 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002789 h2c->st0 = H2_CS_FRAME_E;
2790 goto send_rst;
2791 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002792
Willy Tarreau25919232019-01-03 14:48:18 +01002793 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002794 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002795 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002796
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002797 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2798 /* RFC7540#5.1 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002799 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 +01002800 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2801 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02002802 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002803 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002804 }
2805
Willy Tarreau25919232019-01-03 14:48:18 +01002806 if (error <= 0) {
2807 if (error == 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002808 goto fail; // missing data
Willy Tarreau25919232019-01-03 14:48:18 +01002809
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002810 /* stream error : send RST_STREAM */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002811 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 +01002812 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002813 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau4781b152021-04-06 13:53:36 +02002814 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002815 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002816 }
2817
Christopher Fauletfa922f02019-05-07 10:55:17 +02002818 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002819 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002820
Willy Tarreau927b88b2019-03-04 08:03:25 +01002821 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002822 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002823 else if (h2s->flags & H2_SF_ES_RCVD) {
2824 if (h2s->st == H2_SS_OPEN)
2825 h2s->st = H2_SS_HREM;
2826 else if (h2s->st == H2_SS_HLOC)
2827 h2s_close(h2s);
2828 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002829
Christopher Fauletf95f8762021-01-22 11:59:07 +01002830 /* Unblock busy server h2s waiting for the response headers to validate
2831 * the tunnel establishment or the end of the response of an oborted
2832 * tunnel
2833 */
2834 if ((h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY)) == (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY) ||
2835 (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)) {
2836 TRACE_STATE("Unblock h2s blocked on tunnel establishment/abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2837 h2s->flags &= ~H2_SF_BLK_MBUSY;
2838 }
2839
Willy Tarreau9abb3172021-06-16 18:32:42 +02002840 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 +02002841 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002842 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02002843 fail:
2844 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2845 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002846
2847 send_rst:
2848 /* make the demux send an RST for the current stream. We may only
2849 * do this if we're certain that the HEADERS frame was properly
2850 * decompressed so that the HPACK decoder is still kept up to date.
2851 */
2852 h2_release_buf(h2c, &rxbuf);
2853 h2c->st0 = H2_CS_FRAME_E;
2854
Willy Tarreau022e5e52020-09-10 09:33:15 +02002855 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 +02002856 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2857 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002858}
2859
Willy Tarreau454f9052017-10-26 19:40:35 +02002860/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2861 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2862 */
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01002863static int h2c_handle_data(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002864{
2865 int error;
2866
Willy Tarreau7838a792019-08-12 18:42:03 +02002867 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2868
Willy Tarreau454f9052017-10-26 19:40:35 +02002869 /* note that empty DATA frames are perfectly valid and sometimes used
2870 * to signal an end of stream (with the ES flag).
2871 */
2872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002873 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002874 goto fail; // empty buffer
Willy Tarreau454f9052017-10-26 19:40:35 +02002875
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002876 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002877 goto fail; // incomplete frame
Willy Tarreau454f9052017-10-26 19:40:35 +02002878
2879 /* now either the frame is complete or the buffer is complete */
2880
Willy Tarreau454f9052017-10-26 19:40:35 +02002881 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2882 /* RFC7540#6.1 */
2883 error = H2_ERR_STREAM_CLOSED;
2884 goto strm_err;
2885 }
2886
Christopher Faulet4f09ec82019-06-19 09:25:58 +02002887 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002888 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002889 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 +01002890 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002891 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01002892 goto strm_err;
2893 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01002894 if (!(h2c->flags & H2_CF_IS_BACK) &&
2895 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT) &&
2896 ((h2c->dfl - h2c->dpl) || !(h2c->dff & H2_F_DATA_END_STREAM))) {
2897 /* a tunnel attempt was aborted but the client still try to send some raw data.
2898 * Thus the stream is closed with the CANCEL error. Here we take care it is not
2899 * an empty DATA Frame with the ES flag. The error is only handled if ES was
2900 * already sent to the client because depending on the scheduling, these data may
Ilya Shipitsinacf84592021-02-06 22:29:08 +05002901 * have been sent before the server response but not handle here.
Christopher Faulet91b21dc2021-01-22 12:13:15 +01002902 */
2903 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2904 error = H2_ERR_CANCEL;
2905 goto strm_err;
2906 }
Willy Tarreau1915ca22019-01-24 11:49:37 +01002907
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002908 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02002909 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002910
Willy Tarreau454f9052017-10-26 19:40:35 +02002911 /* call the upper layers to process the frame, then let the upper layer
2912 * notify the stream about any change.
2913 */
2914 if (!h2s->cs) {
Willy Tarreau082c4572019-08-06 10:11:02 +02002915 /* The upper layer has already closed, this may happen on
2916 * 4xx/redirects during POST, or when receiving a response
2917 * from an H2 server after the client has aborted.
2918 */
2919 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002920 goto strm_err;
2921 }
2922
Willy Tarreau8f650c32017-11-21 19:36:21 +01002923 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002924 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01002925
Willy Tarreau721c9742017-11-07 11:05:42 +01002926 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002927 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002928 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002929 }
2930
2931 /* check for completion : the callee will change this to FRAME_A or
2932 * FRAME_H once done.
2933 */
2934 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02002935 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02002936
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002937 /* last frame */
2938 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002939 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002940 if (h2s->st == H2_SS_OPEN)
2941 h2s->st = H2_SS_HREM;
2942 else
2943 h2s_close(h2s);
2944
Willy Tarreau1915ca22019-01-24 11:49:37 +01002945 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2946 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002947 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 +01002948 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02002949 HA_ATOMIC_INC(&h2c->px_counters->strm_proto_err);
Willy Tarreau1915ca22019-01-24 11:49:37 +01002950 goto strm_err;
2951 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002952 }
2953
Christopher Fauletf95f8762021-01-22 11:59:07 +01002954 /* Unblock busy server h2s waiting for the end of the response for an
2955 * aborted tunnel
2956 */
2957 if ((h2c->flags & H2_CF_IS_BACK) &&
2958 (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)) {
2959 TRACE_STATE("Unblock h2s blocked on tunnel abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2960 h2s->flags &= ~H2_SF_BLK_MBUSY;
2961 }
2962
Willy Tarreau7838a792019-08-12 18:42:03 +02002963 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02002964 return 1;
2965
Willy Tarreau454f9052017-10-26 19:40:35 +02002966 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002967 h2s_error(h2s, error);
2968 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002969 fail:
2970 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 +02002971 return 0;
2972}
2973
Willy Tarreau63864812019-08-07 14:25:20 +02002974/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
2975 * valid for the current stream state. This is needed only after parsing the
2976 * frame header but in practice it can be performed at any time during
2977 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
2978 * or 0 in case of error, in which case either h2s or h2c will carry an error.
2979 */
2980static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
2981{
Willy Tarreau7838a792019-08-12 18:42:03 +02002982 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
2983
Willy Tarreau63864812019-08-07 14:25:20 +02002984 if (h2s->st == H2_SS_IDLE &&
2985 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2986 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2987 * this state MUST be treated as a connection error
2988 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002989 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 +02002990 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002991 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02002992 /* only log if no other stream can report the error */
2993 sess_log(h2c->conn->owner);
2994 }
Willy Tarreau4781b152021-04-06 13:53:36 +02002995 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7838a792019-08-12 18:42:03 +02002996 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 +02002997 return 0;
2998 }
2999
Willy Tarreau57a18162019-11-24 14:57:53 +01003000 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
3001 /* only PUSH_PROMISE would be permitted here */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003002 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 +01003003 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003004 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau57a18162019-11-24 14:57:53 +01003005 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
3006 return 0;
3007 }
3008
Willy Tarreau63864812019-08-07 14:25:20 +02003009 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
3010 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
3011 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
3012 * this state MUST be treated as a stream error.
3013 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
3014 * PUSH_PROMISE/CONTINUATION cause connection errors.
3015 */
Amaury Denoyellea8879232020-10-27 17:16:03 +01003016 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003017 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 +02003018 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02003019 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003020 }
3021 else {
Willy Tarreau63864812019-08-07 14:25:20 +02003022 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003023 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003024 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 +02003025 return 0;
3026 }
3027
3028 /* Below the management of frames received in closed state is a
3029 * bit hackish because the spec makes strong differences between
3030 * streams closed by receiving RST, sending RST, and seeing ES
3031 * in both directions. In addition to this, the creation of a
3032 * new stream reusing the identifier of a closed one will be
3033 * detected here. Given that we cannot keep track of all closed
3034 * streams forever, we consider that unknown closed streams were
3035 * closed on RST received, which allows us to respond with an
3036 * RST without breaking the connection (eg: to abort a transfer).
3037 * Some frames have to be silently ignored as well.
3038 */
3039 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
3040 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
3041 /* #5.1.1: The identifier of a newly
3042 * established stream MUST be numerically
3043 * greater than all streams that the initiating
3044 * endpoint has opened or reserved. This
3045 * governs streams that are opened using a
3046 * HEADERS frame and streams that are reserved
3047 * using PUSH_PROMISE. An endpoint that
3048 * receives an unexpected stream identifier
3049 * MUST respond with a connection error.
3050 */
3051 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003052 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 +02003053 return 0;
3054 }
3055
Willy Tarreau4c08f122019-09-26 08:47:15 +02003056 if (h2s->flags & H2_SF_RST_RCVD &&
3057 !(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 +02003058 /* RFC7540#5.1:closed: an endpoint that
3059 * receives any frame other than PRIORITY after
3060 * receiving a RST_STREAM MUST treat that as a
3061 * stream error of type STREAM_CLOSED.
3062 *
3063 * Note that old streams fall into this category
3064 * and will lead to an RST being sent.
3065 *
3066 * However, we cannot generalize this to all frame types. Those
3067 * carrying compression state must still be processed before
3068 * being dropped or we'll desynchronize the decoder. This can
3069 * happen with request trailers received after sending an
3070 * RST_STREAM, or with header/trailers responses received after
3071 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02003072 *
3073 * In addition, since our CLOSED streams always carry the
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003074 * RST_RCVD bit, we don't want to accidentally catch valid
Willy Tarreau4c08f122019-09-26 08:47:15 +02003075 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02003076 */
3077 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3078 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003079 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 +02003080 return 0;
3081 }
3082
3083 /* RFC7540#5.1:closed: if this state is reached as a
3084 * result of sending a RST_STREAM frame, the peer that
3085 * receives the RST_STREAM might have already sent
3086 * frames on the stream that cannot be withdrawn. An
3087 * endpoint MUST ignore frames that it receives on
3088 * closed streams after it has sent a RST_STREAM
3089 * frame. An endpoint MAY choose to limit the period
3090 * over which it ignores frames and treat frames that
3091 * arrive after this time as being in error.
3092 */
3093 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
3094 /* RFC7540#5.1:closed: any frame other than
3095 * PRIO/WU/RST in this state MUST be treated as
3096 * a connection error
3097 */
3098 if (h2c->dft != H2_FT_RST_STREAM &&
3099 h2c->dft != H2_FT_PRIORITY &&
3100 h2c->dft != H2_FT_WINDOW_UPDATE) {
3101 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003102 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 +02003103 return 0;
3104 }
3105 }
3106 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003107 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003108 return 1;
3109}
3110
Willy Tarreaubc933932017-10-09 16:21:43 +02003111/* process Rx frames to be demultiplexed */
3112static void h2_process_demux(struct h2c *h2c)
3113{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003114 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003115 struct h2_fh hdr;
3116 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003117 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02003118
Willy Tarreau7838a792019-08-12 18:42:03 +02003119 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3120
Willy Tarreau081d4722017-05-16 21:51:05 +02003121 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003122 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02003123
3124 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3125 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003126 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02003127 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02003128 goto out;
3129
Willy Tarreau52eed752017-09-22 15:05:09 +02003130 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
3131 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003132 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003133 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003134 h2c->st0 = H2_CS_ERROR2;
Willy Tarreauee4684f2021-06-17 08:08:48 +02003135 if (b_data(&h2c->dbuf) ||
3136 !(((const struct session *)h2c->conn->owner)->fe->options & PR_O_IGNORE_PRB))
3137 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003138 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003139 goto fail;
3140 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003141 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003142
3143 h2c->max_id = 0;
3144 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003145 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003146 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003147
3148 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003149 /* ensure that what is pending is a valid SETTINGS frame
3150 * without an ACK.
3151 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003152 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 +02003153 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003154 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003155 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003156 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 +02003157 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003158 if (!(h2c->flags & H2_CF_IS_BACK))
3159 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003160 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003161 goto fail;
3162 }
3163
3164 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
3165 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003166 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 +02003167 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3168 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003169 if (!(h2c->flags & H2_CF_IS_BACK))
3170 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003171 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003172 goto fail;
3173 }
3174
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003175 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003176 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003177 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 +02003178 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3179 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003180 if (!(h2c->flags & H2_CF_IS_BACK))
3181 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003182 goto fail;
3183 }
3184
Willy Tarreau3bf69182018-12-21 15:34:50 +01003185 /* that's OK, switch to FRAME_P to process it. This is
3186 * a SETTINGS frame whose header has already been
3187 * deleted above.
3188 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01003189 padlen = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +02003190 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003191 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003192 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003193 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003194
3195 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02003196 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02003197 int ret = 0;
3198
Willy Tarreau7838a792019-08-12 18:42:03 +02003199 if (!b_data(&h2c->dbuf)) {
3200 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
Willy Tarreau133aaa92021-02-05 12:16:01 +01003201 goto dbuf_empty;
Willy Tarreau7838a792019-08-12 18:42:03 +02003202 }
3203
3204 if (h2c->st0 >= H2_CS_ERROR) {
3205 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003206 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003207 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003208
3209 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau30d05f32019-08-06 15:49:51 +02003210 h2c->rcvd_s = 0;
3211
Willy Tarreau7838a792019-08-12 18:42:03 +02003212 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreaua4428bd2018-12-22 18:11:41 +01003213 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02003214 break;
3215
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003216 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003217 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 +02003218 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003219 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02003220 /* only log if no other stream can report the error */
3221 sess_log(h2c->conn->owner);
3222 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003223 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003224 break;
3225 }
3226
Christopher Fauletdd2a5622019-06-18 12:22:38 +02003227 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003228 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
3229 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
3230 * we read the pad length and drop it from the remaining
3231 * payload (one byte + the 9 remaining ones = 10 total
3232 * removed), so we have a frame payload starting after the
3233 * pad len. Flow controlled frames (DATA) also count the
3234 * padlen in the flow control, so it must be adjusted.
3235 */
3236 if (hdr.len < 1) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003237 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 +01003238 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003239 if (!(h2c->flags & H2_CF_IS_BACK))
3240 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003241 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3bf69182018-12-21 15:34:50 +01003242 goto fail;
3243 }
3244 hdr.len--;
3245
3246 if (b_data(&h2c->dbuf) < 10)
3247 break; // missing padlen
3248
3249 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3250
3251 if (padlen > hdr.len) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003252 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 +01003253 /* RFC7540#6.1 : pad length = length of
3254 * frame payload or greater => error.
3255 */
3256 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003257 if (!(h2c->flags & H2_CF_IS_BACK))
3258 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003259 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau3bf69182018-12-21 15:34:50 +01003260 goto fail;
3261 }
3262
3263 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3264 h2c->rcvd_c++;
3265 h2c->rcvd_s++;
3266 }
3267 b_del(&h2c->dbuf, 1);
3268 }
3269 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003270
3271 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003272 h2c->dfl = hdr.len;
3273 h2c->dsi = hdr.sid;
3274 h2c->dft = hdr.ft;
3275 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003276 h2c->dpl = padlen;
Willy Tarreau73db4342019-09-25 07:28:44 +02003277 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 +02003278 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003279
3280 /* check for minimum basic frame format validity */
3281 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3282 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003283 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 +01003284 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003285 if (!(h2c->flags & H2_CF_IS_BACK))
3286 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003287 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003288 goto fail;
3289 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003290 }
3291
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003292 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3293 * H2_CS_FRAME_P indicates an incomplete previous operation
3294 * (most often the first attempt) and requires some validity
3295 * checks for the frame and the current state. The two other
3296 * ones are set after completion (or abortion) and must skip
3297 * validity checks.
3298 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003299 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3300
Willy Tarreau567beb82018-12-18 16:52:44 +01003301 if (tmp_h2s != h2s && h2s && h2s->cs &&
3302 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003303 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003304 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003305 (h2s->flags & H2_SF_ES_RCVD) ||
3306 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003307 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003308 TRACE_DEVEL("notifying stream before switching SID", H2_EV_RX_FRAME|H2_EV_STRM_WAKE, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003309 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003310 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003311 }
3312 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003313
Willy Tarreau63864812019-08-07 14:25:20 +02003314 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003315 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3316 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003317 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003318 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003319
Willy Tarreau7e98c052017-10-10 15:56:59 +02003320 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003321 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003322 if (h2c->st0 == H2_CS_FRAME_P) {
3323 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003324 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003325 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003326 HA_ATOMIC_INC(&h2c->px_counters->settings_rcvd);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003327
Willy Tarreau7838a792019-08-12 18:42:03 +02003328 if (h2c->st0 == H2_CS_FRAME_A) {
3329 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 +02003330 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003331 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003332 break;
3333
Willy Tarreaucf68c782017-10-10 17:11:41 +02003334 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003335 if (h2c->st0 == H2_CS_FRAME_P) {
3336 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003337 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003338 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003339
Willy Tarreau7838a792019-08-12 18:42:03 +02003340 if (h2c->st0 == H2_CS_FRAME_A) {
3341 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 +02003342 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003343 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003344 break;
3345
Willy Tarreau26f95952017-07-27 17:18:30 +02003346 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003347 if (h2c->st0 == H2_CS_FRAME_P) {
3348 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 +02003349 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003350 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003351 break;
3352
Willy Tarreau61290ec2017-10-17 08:19:21 +02003353 case H2_FT_CONTINUATION:
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003354 /* RFC7540#6.10: CONTINUATION may only be preceded by
Willy Tarreauea18f862018-12-22 20:19:26 +01003355 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3356 * frames' parsers consume all following CONTINUATION
3357 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003358 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003359 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 +01003360 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003361 if (!(h2c->flags & H2_CF_IS_BACK))
3362 sess_log(h2c->conn->owner);
Willy Tarreau4781b152021-04-06 13:53:36 +02003363 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01003364 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003365
Willy Tarreau13278b42017-10-13 19:23:14 +02003366 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003367 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003368 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003369 if (h2c->flags & H2_CF_IS_BACK)
3370 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3371 else
3372 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003373 if (tmp_h2s) {
3374 h2s = tmp_h2s;
3375 ret = 1;
3376 }
3377 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003378 HA_ATOMIC_INC(&h2c->px_counters->headers_rcvd);
Willy Tarreau13278b42017-10-13 19:23:14 +02003379 break;
3380
Willy Tarreau454f9052017-10-26 19:40:35 +02003381 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003382 if (h2c->st0 == H2_CS_FRAME_P) {
3383 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003384 ret = h2c_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003385 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003386 HA_ATOMIC_INC(&h2c->px_counters->data_rcvd);
Willy Tarreau454f9052017-10-26 19:40:35 +02003387
Willy Tarreau7838a792019-08-12 18:42:03 +02003388 if (h2c->st0 == H2_CS_FRAME_A) {
3389 TRACE_PROTO("sending stream WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003390 ret = h2c_send_strm_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003391 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003392 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003393
Willy Tarreau92153fc2017-12-03 19:46:19 +01003394 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003395 if (h2c->st0 == H2_CS_FRAME_P) {
3396 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003397 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003398 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003399 break;
3400
Willy Tarreaucd234e92017-08-18 10:59:39 +02003401 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003402 if (h2c->st0 == H2_CS_FRAME_P) {
3403 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 +02003404 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003405 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003406 HA_ATOMIC_INC(&h2c->px_counters->rst_stream_rcvd);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003407 break;
3408
Willy Tarreaue96b0922017-10-30 00:28:29 +01003409 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003410 if (h2c->st0 == H2_CS_FRAME_P) {
3411 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003412 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003413 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003414 HA_ATOMIC_INC(&h2c->px_counters->goaway_rcvd);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003415 break;
3416
Willy Tarreau1c661982017-10-30 13:52:01 +01003417 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003418 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003419 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003420 /* drop frames that we ignore. They may be larger than
3421 * the buffer so we drain all of their contents until
3422 * we reach the end.
3423 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003424 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3425 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003426 h2c->dfl -= ret;
3427 ret = h2c->dfl == 0;
3428 }
3429
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003430 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003431 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003432 if (h2s->st == H2_SS_ERROR) {
3433 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 +01003434 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003435 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003436
Willy Tarreau7838a792019-08-12 18:42:03 +02003437 if (h2c->st0 == H2_CS_FRAME_E) {
3438 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 +01003439 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003440 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003441
Willy Tarreau133aaa92021-02-05 12:16:01 +01003442 dbuf_empty:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003443 /* error or missing data condition met above ? */
Willy Tarreau7838a792019-08-12 18:42:03 +02003444 if (ret <= 0) {
3445 TRACE_DEVEL("insufficient data to proceed", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003446 if (h2c->flags & H2_CF_RCVD_SHUT)
3447 h2c->flags |= H2_CF_END_REACHED;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003448 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003449 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003450
3451 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaubba7a4d2020-09-18 07:41:28 +02003452 if (h2c->dfl)
3453 TRACE_DEVEL("skipping remaining frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
Christopher Faulet5112a602019-09-26 16:38:28 +02003454 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3455 b_del(&h2c->dbuf, ret);
3456 h2c->dfl -= ret;
3457 if (!h2c->dfl) {
3458 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3459 h2c->st0 = H2_CS_FRAME_H;
3460 h2c->dsi = -1;
3461 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003462 }
3463 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003464
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003465 if (h2c->rcvd_c > 0 &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003466 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) {
3467 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003468 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003469 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003470
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003471 done:
Willy Tarreau567beb82018-12-18 16:52:44 +01003472 if (h2s && h2s->cs &&
3473 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003474 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003475 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003476 (h2s->flags & H2_SF_ES_RCVD) ||
3477 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003478 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003479 TRACE_DEVEL("notifying stream before switching SID", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003480 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003481 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003482 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003483
Willy Tarreau7838a792019-08-12 18:42:03 +02003484 if (old_iw != h2c->miw) {
3485 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003486 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003487 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003488
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003489 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003490 out:
3491 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003492 return;
3493
3494 fail:
3495 /* we can go here on missing data, blocked response or error, but we
3496 * need to check if we've met a short read condition.
3497 */
3498 if (h2c->flags & H2_CF_RCVD_SHUT)
3499 h2c->flags |= H2_CF_END_REACHED;
3500 goto done;
Willy Tarreaubc933932017-10-09 16:21:43 +02003501}
3502
Willy Tarreau989539b2020-01-10 17:01:29 +01003503/* resume each h2s eligible for sending in list head <head> */
3504static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3505{
3506 struct h2s *h2s, *h2s_back;
3507
3508 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3509
3510 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3511 if (h2c->mws <= 0 ||
3512 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3513 h2c->st0 >= H2_CS_ERROR)
3514 break;
3515
3516 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003517
Willy Tarreaud9464162020-01-10 18:25:07 +01003518 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003519 continue;
3520
Willy Tarreau5723f292020-01-10 15:16:57 +01003521 /* If the sender changed his mind and unsubscribed, let's just
3522 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003523 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003524 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3525 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003526 LIST_DEL_INIT(&h2s->list);
3527 continue;
3528 }
3529
Willy Tarreauf96508a2020-01-10 11:12:48 +01003530 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003531 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003532 tasklet_wakeup(h2s->subs->tasklet);
3533 h2s->subs->events &= ~SUB_RETRY_SEND;
3534 if (!h2s->subs->events)
3535 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003536 }
3537 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3538 tasklet_wakeup(h2s->shut_tl);
3539 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003540 }
3541
3542 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3543}
3544
Willy Tarreaubc933932017-10-09 16:21:43 +02003545/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3546 * the end.
3547 */
3548static int h2_process_mux(struct h2c *h2c)
3549{
Willy Tarreau7838a792019-08-12 18:42:03 +02003550 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3551
Willy Tarreau01b44822018-10-03 14:26:37 +02003552 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3553 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3554 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3555 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003556 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003557 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003558 goto fail;
3559 }
3560 h2c->st0 = H2_CS_SETTINGS1;
3561 }
3562 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003563 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003564 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003565 }
3566
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003567 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003568 if (h2c->rcvd_s > 0 &&
3569 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3570 h2c_send_strm_wu(h2c) < 0)
3571 goto fail;
3572
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003573 if (h2c->rcvd_c > 0 &&
3574 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3575 h2c_send_conn_wu(h2c) < 0)
3576 goto fail;
3577
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003578 /* First we always process the flow control list because the streams
3579 * waiting there were already elected for immediate emission but were
3580 * blocked just on this.
3581 */
Willy Tarreau989539b2020-01-10 17:01:29 +01003582 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3583 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003584
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003585 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003586 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003587 if (h2c->st0 == H2_CS_ERROR) {
3588 if (h2c->max_id >= 0) {
3589 h2c_send_goaway_error(h2c, NULL);
3590 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003591 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003592 }
3593
3594 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3595 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003596 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003597 done:
3598 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3599 return 1;
3600 out0:
3601 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3602 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003603}
3604
Willy Tarreau62f52692017-10-08 23:01:42 +02003605
Willy Tarreau479998a2018-11-18 06:30:59 +01003606/* Attempt to read data, and subscribe if none available.
3607 * The function returns 1 if data has been received, otherwise zero.
3608 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003609static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003610{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003611 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003612 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003613 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003614 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003615
Willy Tarreau7838a792019-08-12 18:42:03 +02003616 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3617
3618 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3619 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003620 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003621 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003622
Willy Tarreau7838a792019-08-12 18:42:03 +02003623 if (!h2_recv_allowed(h2c)) {
3624 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003625 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003626 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003627
Willy Tarreau44e973f2018-03-01 17:49:30 +01003628 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003629 if (!buf) {
3630 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003631 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003632 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003633 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003634
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003635 if (h2c->flags & H2_CF_RCVD_SHUT) {
3636 TRACE_DEVEL("leaving on rcvd_shut", H2_EV_H2C_RECV, h2c->conn);
3637 return 0;
3638 }
3639
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003640 if (!b_data(buf)) {
3641 /* try to pre-align the buffer like the
3642 * rxbufs will be to optimize memory copies. We'll make
3643 * sure that the frame header lands at the end of the
3644 * HTX block to alias it upon recv. We cannot use the
3645 * head because rcv_buf() will realign the buffer if
3646 * it's empty. Thus we cheat and pretend we already
3647 * have a few bytes there.
3648 */
3649 max = buf_room_for_htx_data(buf) + 9;
3650 buf->head = sizeof(struct htx) - 9;
3651 }
3652 else
3653 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003654
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003655 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003656
Christopher Fauletde9d6052021-04-23 12:25:18 +02003657 if (max && !ret && h2_recv_allowed(h2c)) {
3658 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
3659 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003660 } else if (ret)
Willy Tarreau022e5e52020-09-10 09:33:15 +02003661 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn, 0, 0, (void*)(long)ret);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003662
Christopher Fauletde9d6052021-04-23 12:25:18 +02003663 if (conn_xprt_read0_pending(h2c->conn)) {
3664 TRACE_DATA("received read0", H2_EV_H2C_RECV, h2c->conn);
3665 h2c->flags |= H2_CF_RCVD_SHUT;
3666 }
3667
Olivier Houcharda1411e62018-08-17 18:42:48 +02003668 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003669 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02003670 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard46677732018-11-29 17:06:17 +01003671 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003672 }
3673
Willy Tarreau7838a792019-08-12 18:42:03 +02003674 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003675 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003676 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003677 }
3678
3679 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003680 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003681}
3682
Willy Tarreau479998a2018-11-18 06:30:59 +01003683/* Try to send data if possible.
3684 * The function returns 1 if data have been sent, otherwise zero.
3685 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003686static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003687{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003688 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003689 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003690 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003691
Willy Tarreau7838a792019-08-12 18:42:03 +02003692 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003693
Willy Tarreau7838a792019-08-12 18:42:03 +02003694 if (conn->flags & CO_FL_ERROR) {
3695 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
3696 return 1;
3697 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003698
Willy Tarreau911db9b2020-01-23 16:27:54 +01003699 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreaua2af5122017-10-09 11:56:46 +02003700 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003701 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003702 }
3703
Willy Tarreaubc933932017-10-09 16:21:43 +02003704 /* This loop is quite simple : it tries to fill as much as it can from
3705 * pending streams into the existing buffer until it's reportedly full
3706 * or the end of send requests is reached. Then it tries to send this
3707 * buffer's contents out, marks it not full if at least one byte could
3708 * be sent, and tries again.
3709 *
3710 * The snd_buf() function normally takes a "flags" argument which may
3711 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3712 * data immediately comes and CO_SFL_STREAMER to indicate that the
3713 * connection is streaming lots of data (used to increase TLS record
3714 * size at the expense of latency). The former can be sent any time
3715 * there's a buffer full flag, as it indicates at least one stream
3716 * attempted to send and failed so there are pending data. An
3717 * alternative would be to set it as long as there's an active stream
3718 * but that would be problematic for ACKs until we have an absolute
3719 * guarantee that all waiters have at least one byte to send. The
3720 * latter should possibly not be set for now.
3721 */
3722
3723 done = 0;
3724 while (!done) {
3725 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003726 unsigned int released = 0;
3727 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02003728
3729 /* fill as much as we can into the current buffer */
3730 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3731 done = h2_process_mux(h2c);
3732
Olivier Houchard2b094432019-01-29 18:28:36 +01003733 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003734 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003735
Christopher Faulet9a3d3fc2020-10-22 16:24:58 +02003736 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
3737 (h2c->st0 == H2_CS_ERROR2) || (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02003738 break;
3739
3740 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
3741 flags |= CO_SFL_MSG_MORE;
3742
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003743 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3744 if (b_data(buf)) {
3745 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003746 if (!ret) {
3747 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003748 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003749 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003750 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02003751 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn, 0, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003752 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003753 if (b_data(buf)) {
3754 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003755 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003756 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003757 }
3758 b_free(buf);
3759 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003760 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003761
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003762 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003763 offer_buffers(NULL, released);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003764
Willy Tarreaubc933932017-10-09 16:21:43 +02003765 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02003766 if (sent)
3767 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02003768 }
3769
Willy Tarreaua2af5122017-10-09 11:56:46 +02003770 if (conn->flags & CO_FL_SOCK_WR_SH) {
3771 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02003772 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003773 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003774 /* We're not full anymore, so we can wake any task that are waiting
3775 * for us.
3776 */
Willy Tarreau989539b2020-01-10 17:01:29 +01003777 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H)
3778 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01003779
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003780 /* We're done, no more to send */
Willy Tarreau7838a792019-08-12 18:42:03 +02003781 if (!br_data(h2c->mbuf)) {
3782 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003783 return sent;
Willy Tarreau7838a792019-08-12 18:42:03 +02003784 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003785schedule:
Willy Tarreau7838a792019-08-12 18:42:03 +02003786 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
3787 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003788 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003789 }
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003790
Willy Tarreau7838a792019-08-12 18:42:03 +02003791 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003792 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003793}
3794
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003795/* this is the tasklet referenced in h2c->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003796struct task *h2_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003797{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003798 struct connection *conn;
3799 struct tasklet *tl = (struct tasklet *)t;
3800 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003801 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02003802 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003803
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003804 if (state & TASK_F_USR1) {
3805 /* the tasklet was idling on an idle connection, it might have
3806 * been stolen, let's be careful!
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003807 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003808 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3809 if (t->context == NULL) {
3810 /* The connection has been taken over by another thread,
3811 * we're no longer responsible for it, so just free the
3812 * tasklet, and do nothing.
3813 */
3814 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3815 tasklet_free(tl);
Willy Tarreau74163142021-03-13 11:30:19 +01003816 t = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003817 goto leave;
3818 }
3819 conn = h2c->conn;
3820 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003821
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003822 conn_in_list = conn->flags & CO_FL_LIST_MASK;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003823
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003824 /* Remove the connection from the list, to be sure nobody attempts
3825 * to use it while we handle the I/O events
3826 */
3827 if (conn_in_list)
3828 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003829
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003830 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3831 } else {
3832 /* we're certain the connection was not in an idle list */
3833 conn = h2c->conn;
3834 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3835 conn_in_list = 0;
3836 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003837
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003838 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003839 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003840 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003841 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003842 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003843 ret = h2_process(h2c);
3844
3845 /* If we were in an idle list, we want to add it back into it,
3846 * unless h2_process() returned -1, which mean it has destroyed
3847 * the connection (testing !ret is enough, if h2_process() wasn't
3848 * called then ret will be 0 anyway.
3849 */
Willy Tarreau74163142021-03-13 11:30:19 +01003850 if (ret < 0)
3851 t = NULL;
3852
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003853 if (!ret && conn_in_list) {
3854 struct server *srv = objt_server(conn->target);
3855
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003856 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003857 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003858 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003859 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003860 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003861 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003862 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003863
Willy Tarreau38468772020-06-28 00:31:13 +02003864leave:
Willy Tarreau7838a792019-08-12 18:42:03 +02003865 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreau74163142021-03-13 11:30:19 +01003866 return t;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003867}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003868
Willy Tarreau62f52692017-10-08 23:01:42 +02003869/* callback called on any event by the connection handler.
3870 * It applies changes and returns zero, or < 0 if it wants immediate
3871 * destruction of the connection (which normally doesn not happen in h2).
3872 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003873static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003874{
Olivier Houchard7505f942018-08-21 18:10:44 +02003875 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003876
Willy Tarreau7838a792019-08-12 18:42:03 +02003877 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3878
Willy Tarreauf0961222021-02-05 11:41:46 +01003879 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) &&
3880 (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003881 h2_process_demux(h2c);
3882
3883 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003884 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003885
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003886 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003887 h2c->flags &= ~H2_CF_DEM_DFULL;
3888 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003889 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003890
Willy Tarreaub1e600c2020-10-13 18:09:15 +02003891 if (unlikely(h2c->proxy->disabled) && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003892 /* frontend is stopping, reload likely in progress, let's try
3893 * to announce a graceful shutdown if not yet done. We don't
3894 * care if it fails, it will be tried again later.
3895 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003896 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
Willy Tarreau8ec14062017-12-30 18:08:13 +01003897 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3898 if (h2c->last_sid < 0)
3899 h2c->last_sid = (1U << 31) - 1;
3900 h2c_send_goaway_error(h2c, NULL);
3901 }
3902 }
3903
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003904 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003905 * If we received early data, and the handshake is done, wake
3906 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003907 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003908 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003909 (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 +01003910 struct eb32_node *node;
3911 struct h2s *h2s;
3912
3913 h2c->flags |= H2_CF_WAIT_FOR_HS;
3914 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3915
3916 while (node) {
3917 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003918 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003919 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003920 node = eb32_next(node);
3921 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003922 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003923
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003924 if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003925 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3926 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3927 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003928 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003929
3930 if (eb_is_empty(&h2c->streams_by_id)) {
3931 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003932 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003933 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003934 return -1;
3935 }
Willy Tarreau4481e262019-10-31 15:36:30 +01003936
3937 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01003938 if (conn->flags & CO_FL_LIST_MASK) {
3939 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003940 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01003941 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3942 }
Willy Tarreau4481e262019-10-31 15:36:30 +01003943 }
3944 else if (h2c->st0 == H2_CS_ERROR) {
3945 /* connections in error must be removed from the idle lists */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01003946 if (conn->flags & CO_FL_LIST_MASK) {
3947 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003948 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01003949 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3950 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003951 }
3952
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003953 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003954 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003955
Olivier Houchard53216e72018-10-10 15:46:36 +02003956 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3957 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3958 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003959 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003960 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3961 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003962 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003963
Willy Tarreau3f133572017-10-31 19:21:06 +01003964 if (h2c->task) {
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02003965 if (h2c_may_expire(h2c))
3966 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3967 else
3968 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003969 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003970 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003971
Olivier Houchard7505f942018-08-21 18:10:44 +02003972 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003973 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003974 return 0;
3975}
3976
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003977/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003978static int h2_wake(struct connection *conn)
3979{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003980 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02003981 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003982
Willy Tarreau7838a792019-08-12 18:42:03 +02003983 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3984 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01003985 if (ret >= 0)
3986 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003987 TRACE_LEAVE(H2_EV_H2C_WAKE);
3988 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003989}
3990
Willy Tarreauea392822017-10-31 10:02:25 +01003991/* Connection timeout management. The principle is that if there's no receipt
3992 * nor sending for a certain amount of time, the connection is closed. If the
3993 * MUX buffer still has lying data or is not allocatable, the connection is
3994 * immediately killed. If it's allocatable and empty, we attempt to send a
3995 * GOAWAY frame.
3996 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003997struct task *h2_timeout_task(struct task *t, void *context, unsigned int state)
Willy Tarreauea392822017-10-31 10:02:25 +01003998{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003999 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01004000 int expired = tick_is_expired(t->expire, now_ms);
4001
Willy Tarreau7838a792019-08-12 18:42:03 +02004002 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
4003
Willy Tarreaubd42e922020-06-30 11:19:23 +02004004 if (h2c) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004005 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004006 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004007
4008 /* Somebody already stole the connection from us, so we should not
4009 * free it, we just have to free the task.
4010 */
4011 if (!t->context) {
4012 h2c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004013 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004014 goto do_leave;
4015 }
4016
4017
Willy Tarreaubd42e922020-06-30 11:19:23 +02004018 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004019 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004020 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
4021 return t;
4022 }
Willy Tarreauea392822017-10-31 10:02:25 +01004023
Willy Tarreaubd42e922020-06-30 11:19:23 +02004024 if (!h2c_may_expire(h2c)) {
4025 /* we do still have streams but all of them are idle, waiting
4026 * for the data layer, so we must not enforce the timeout here.
4027 */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004028 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004029 t->expire = TICK_ETERNITY;
4030 return t;
4031 }
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004032
Willy Tarreaubd42e922020-06-30 11:19:23 +02004033 /* We're about to destroy the connection, so make sure nobody attempts
4034 * to steal it from us.
4035 */
Willy Tarreaubd42e922020-06-30 11:19:23 +02004036 if (h2c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004037 conn_delete_from_tree(&h2c->conn->hash_node->node);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004038
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01004039 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02004040 }
Olivier Houchardcd4159f2020-03-10 18:39:42 +01004041
Olivier Houchard48ce6a32020-07-02 11:58:05 +02004042do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02004043 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02004044
4045 if (!h2c) {
4046 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02004047 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02004048 return NULL;
4049 }
4050
4051 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01004052 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02004053 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01004054
Willy Tarreau662fafc2019-05-26 09:43:07 +02004055 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01004056 /* don't even try to send a GOAWAY, the buffer is stuck */
4057 h2c->flags |= H2_CF_GOAWAY_FAILED;
4058 }
4059
4060 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01004061 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01004062 if (h2c_send_goaway_error(h2c, NULL) <= 0)
4063 h2c->flags |= H2_CF_GOAWAY_FAILED;
4064
Willy Tarreau662fafc2019-05-26 09:43:07 +02004065 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004066 unsigned int released = 0;
4067 struct buffer *buf;
4068
4069 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4070 if (b_data(buf)) {
4071 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
4072 if (!ret)
4073 break;
4074 b_del(buf, ret);
4075 if (b_data(buf))
4076 break;
4077 b_free(buf);
4078 released++;
4079 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02004080 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004081
4082 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01004083 offer_buffers(NULL, released);
Willy Tarreau787db9a2018-06-14 18:31:46 +02004084 }
Willy Tarreauea392822017-10-31 10:02:25 +01004085
Willy Tarreau4481e262019-10-31 15:36:30 +01004086 /* in any case this connection must not be considered idle anymore */
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004087 if (h2c->conn->flags & CO_FL_LIST_MASK) {
4088 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004089 conn_delete_from_tree(&h2c->conn->hash_node->node);
Amaury Denoyelle3d752a82021-02-19 15:37:38 +01004090 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
4091 }
Willy Tarreau4481e262019-10-31 15:36:30 +01004092
Willy Tarreau0975f112018-03-29 15:22:59 +02004093 /* either we can release everything now or it will be done later once
4094 * the last stream closes.
4095 */
4096 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02004097 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01004098
Willy Tarreau7838a792019-08-12 18:42:03 +02004099 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01004100 return NULL;
4101}
4102
4103
Willy Tarreau62f52692017-10-08 23:01:42 +02004104/*******************************************/
4105/* functions below are used by the streams */
4106/*******************************************/
4107
4108/*
4109 * Attach a new stream to a connection
4110 * (Used for outgoing connections)
4111 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01004112static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02004113{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004114 struct conn_stream *cs;
4115 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004116 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004117
Willy Tarreau7838a792019-08-12 18:42:03 +02004118 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02004119 cs = cs_new(conn, conn->target);
Willy Tarreau7838a792019-08-12 18:42:03 +02004120 if (!cs) {
4121 TRACE_DEVEL("leaving on CS allocation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004122 return NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004123 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01004124 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004125 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004126 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004127 cs_free(cs);
4128 return NULL;
4129 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004130
4131 /* the connection is not idle anymore, let's mark this */
4132 HA_ATOMIC_AND(&h2c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004133 xprt_set_used(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004134
Willy Tarreau7838a792019-08-12 18:42:03 +02004135 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004136 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02004137}
4138
Willy Tarreaufafd3982018-11-18 21:29:20 +01004139/* Retrieves the first valid conn_stream from this connection, or returns NULL.
4140 * We have to scan because we may have some orphan streams. It might be
4141 * beneficial to scan backwards from the end to reduce the likeliness to find
4142 * orphans.
4143 */
4144static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
4145{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004146 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01004147 struct h2s *h2s;
4148 struct eb32_node *node;
4149
4150 node = eb32_first(&h2c->streams_by_id);
4151 while (node) {
4152 h2s = container_of(node, struct h2s, by_id);
4153 if (h2s->cs)
4154 return h2s->cs;
4155 node = eb32_next(node);
4156 }
4157 return NULL;
4158}
4159
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004160static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4161{
4162 int ret = 0;
4163 struct h2c *h2c = conn->ctx;
4164
4165 switch (mux_ctl) {
4166 case MUX_STATUS:
4167 /* Only consider the mux to be ready if we're done with
4168 * the preface and settings, and we had no error.
4169 */
4170 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
4171 ret |= MUX_STATUS_READY;
4172 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02004173 case MUX_EXIT_STATUS:
4174 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004175 default:
4176 return -1;
4177 }
4178}
4179
Willy Tarreau62f52692017-10-08 23:01:42 +02004180/*
Olivier Houchard060ed432018-11-06 16:32:42 +01004181 * Destroy the mux and the associated connection, if it is no longer used
4182 */
Christopher Faulet73c12072019-04-08 11:23:22 +02004183static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01004184{
Christopher Faulet73c12072019-04-08 11:23:22 +02004185 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01004186
Willy Tarreau7838a792019-08-12 18:42:03 +02004187 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02004188 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02004189 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004190 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01004191}
4192
4193/*
Willy Tarreau62f52692017-10-08 23:01:42 +02004194 * Detach the stream from the connection and possibly release the connection.
4195 */
4196static void h2_detach(struct conn_stream *cs)
4197{
Willy Tarreau60935142017-10-16 18:11:19 +02004198 struct h2s *h2s = cs->ctx;
4199 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01004200 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004201
Willy Tarreau7838a792019-08-12 18:42:03 +02004202 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
4203
Willy Tarreau60935142017-10-16 18:11:19 +02004204 cs->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004205 if (!h2s) {
4206 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02004207 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004208 }
Willy Tarreau60935142017-10-16 18:11:19 +02004209
Willy Tarreaud9464162020-01-10 18:25:07 +01004210 /* there's no txbuf so we're certain not to be able to send anything */
4211 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02004212
Olivier Houchardf502aca2018-12-14 19:42:40 +01004213 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004214 h2c = h2s->h2c;
4215 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02004216 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004217 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
4218 !h2_frt_has_too_many_cs(h2c)) {
4219 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02004220 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004221 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02004222 }
Willy Tarreau60935142017-10-16 18:11:19 +02004223
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004224 /* this stream may be blocked waiting for some data to leave (possibly
4225 * an ES or RST frame), so orphan it in this case.
4226 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02004227 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02004228 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01004229 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01004230 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004231 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004232 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004233 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004234
Willy Tarreau45f752e2017-10-30 15:44:59 +01004235 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
4236 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
4237 /* unblock the connection if it was blocked on this
4238 * stream.
4239 */
4240 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
4241 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004242 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01004243 }
4244
Willy Tarreau71049cc2018-03-28 13:56:39 +02004245 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02004246
Christopher Faulet9b79a102019-07-15 11:22:56 +02004247 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01004248 if (!(h2c->conn->flags &
4249 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004250 if (h2c->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02004251 /* Add the connection in the session server list, if not already done */
4252 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
4253 h2c->conn->owner = NULL;
4254 if (eb_is_empty(&h2c->streams_by_id)) {
4255 h2c->conn->mux->destroy(h2c);
4256 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4257 return;
Christopher Fauletc5579d12020-07-01 15:45:41 +02004258 }
4259 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02004260 if (eb_is_empty(&h2c->streams_by_id)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004261 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
4262 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
4263 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houchard351411f2018-12-27 17:20:54 +01004264 return;
4265 }
4266 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004267 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004268 else {
4269 if (eb_is_empty(&h2c->streams_by_id)) {
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004270 /* If the connection is owned by the session, first remove it
4271 * from its list
4272 */
4273 if (h2c->conn->owner) {
4274 session_unown_conn(h2c->conn->owner, h2c->conn);
4275 h2c->conn->owner = NULL;
4276 }
4277
Willy Tarreaue388f2f2021-03-02 16:51:09 +01004278 /* mark that the tasklet may lose its context to another thread and
4279 * that the handler needs to check it under the idle conns lock.
4280 */
4281 HA_ATOMIC_OR(&h2c->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01004282 xprt_set_idle(h2c->conn, h2c->conn->xprt, h2c->conn->xprt_ctx);
4283
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004284 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01004285 /* The server doesn't want it, let's kill the connection right away */
4286 h2c->conn->mux->destroy(h2c);
4287 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4288 return;
4289 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01004290 /* At this point, the connection has been added to the
4291 * server idle list, so another thread may already have
4292 * hijacked it, so we can't do anything with it.
4293 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01004294 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
4295 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01004296
Olivier Houchard8a786902018-12-15 16:05:40 +01004297 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004298 else if (!h2c->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004299 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02004300 !LIST_INLIST(&h2c->conn->session_list)) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01004301 ebmb_insert(&__objt_server(h2c->conn->target)->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01004302 &h2c->conn->hash_node->node,
4303 sizeof(h2c->conn->hash_node->hash));
Christopher Fauletc5579d12020-07-01 15:45:41 +02004304 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004305 }
4306 }
4307 }
4308
Willy Tarreaue323f342018-03-28 13:51:45 +02004309 /* We don't want to close right now unless we're removing the
4310 * last stream, and either the connection is in error, or it
4311 * reached the ID already specified in a GOAWAY frame received
4312 * or sent (as seen by last_sid >= 0).
4313 */
Olivier Houchard7a977432019-03-21 15:47:13 +01004314 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02004315 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02004316 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02004317 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02004318 }
4319 else if (h2c->task) {
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004320 if (h2c_may_expire(h2c))
4321 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
4322 else
4323 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02004324 task_queue(h2c->task);
Willy Tarreau7838a792019-08-12 18:42:03 +02004325 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02004326 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004327 else
4328 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004329}
4330
Willy Tarreau88bdba32019-05-13 18:17:53 +02004331/* Performs a synchronous or asynchronous shutr(). */
4332static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004333{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004334 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004335
Willy Tarreauf983d002019-05-14 10:40:21 +02004336 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004337 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004338
Willy Tarreau7838a792019-08-12 18:42:03 +02004339 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4340
Willy Tarreau18059042019-01-31 19:12:48 +01004341 /* a connstream may require us to immediately kill the whole connection
4342 * for example because of a "tcp-request content reject" rule that is
4343 * normally used to limit abuse. In this case we schedule a goaway to
4344 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004345 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004346 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004347 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004348 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004349 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4350 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4351 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004352 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4353 /* Nothing was never sent for this stream, so reset with
4354 * REFUSED_STREAM error to let the client retry the
4355 * request.
4356 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004357 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004358 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4359 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004360 else {
4361 /* a final response was already provided, we don't want this
4362 * stream anymore. This may happen when the server responds
4363 * before the end of an upload and closes quickly (redirect,
4364 * deny, ...)
4365 */
4366 h2s_error(h2s, H2_ERR_CANCEL);
4367 }
Willy Tarreau18059042019-01-31 19:12:48 +01004368
Willy Tarreau90c32322017-11-24 08:00:30 +01004369 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004370 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004371 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004372
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004373 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004374 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004375 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004376 done:
4377 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004378 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004379 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004380add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004381 /* Let the handler know we want to shutr, and add ourselves to the
4382 * most relevant list if not yet done. h2_deferred_shut() will be
4383 * automatically called via the shut_tl tasklet when there's room
4384 * again.
4385 */
4386 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau2b718102021-04-21 07:32:39 +02004387 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004388 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004389 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004390 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004391 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004392 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004393 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004394 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004395}
4396
Willy Tarreau88bdba32019-05-13 18:17:53 +02004397/* Performs a synchronous or asynchronous shutw(). */
4398static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004399{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004400 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004401
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004402 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004403 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004404
Willy Tarreau7838a792019-08-12 18:42:03 +02004405 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4406
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004407 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01004408 /* we can cleanly close using an empty data frame only after headers */
4409
4410 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4411 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004412 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004413
4414 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004415 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004416 else
4417 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004418 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004419 /* a connstream may require us to immediately kill the whole connection
4420 * for example because of a "tcp-request content reject" rule that is
4421 * normally used to limit abuse. In this case we schedule a goaway to
4422 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004423 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004424 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004425 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004426 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004427 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4428 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4429 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004430 else {
4431 /* Nothing was never sent for this stream, so reset with
4432 * REFUSED_STREAM error to let the client retry the
4433 * request.
4434 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004435 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004436 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4437 }
Willy Tarreau18059042019-01-31 19:12:48 +01004438
Willy Tarreau90c32322017-11-24 08:00:30 +01004439 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004440 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004441 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004442
Willy Tarreau00dd0782018-03-01 16:31:34 +01004443 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004444 }
4445
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004446 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004447 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004448
4449 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4450
Willy Tarreau88bdba32019-05-13 18:17:53 +02004451 done:
4452 h2s->flags &= ~H2_SF_WANT_SHUTW;
4453 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004454
4455 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004456 /* Let the handler know we want to shutw, and add ourselves to the
4457 * most relevant list if not yet done. h2_deferred_shut() will be
4458 * automatically called via the shut_tl tasklet when there's room
4459 * again.
4460 */
4461 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau2b718102021-04-21 07:32:39 +02004462 if (!LIST_INLIST(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004463 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02004464 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004465 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Willy Tarreau2b718102021-04-21 07:32:39 +02004466 LIST_APPEND(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004467 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004468 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004469 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004470}
4471
Willy Tarreau5723f292020-01-10 15:16:57 +01004472/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004473 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4474 * and prevented the last frame from being emitted.
4475 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004476struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned int state)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004477{
4478 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004479 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004480
Willy Tarreau7838a792019-08-12 18:42:03 +02004481 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4482
Willy Tarreau5723f292020-01-10 15:16:57 +01004483 if (h2s->flags & H2_SF_NOTIFIED) {
4484 /* some data processing remains to be done first */
4485 goto end;
4486 }
4487
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004488 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004489 h2_do_shutw(h2s);
4490
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004491 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004492 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004493
Willy Tarreau88bdba32019-05-13 18:17:53 +02004494 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004495 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004496 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01004497
Willy Tarreau88bdba32019-05-13 18:17:53 +02004498 if (!h2s->cs) {
4499 h2s_destroy(h2s);
Willy Tarreau74163142021-03-13 11:30:19 +01004500 if (h2c_is_dead(h2c)) {
Willy Tarreau88bdba32019-05-13 18:17:53 +02004501 h2_release(h2c);
Willy Tarreau74163142021-03-13 11:30:19 +01004502 t = NULL;
4503 }
Willy Tarreau88bdba32019-05-13 18:17:53 +02004504 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004505 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004506 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004507 TRACE_LEAVE(H2_EV_STRM_SHUT);
Willy Tarreau74163142021-03-13 11:30:19 +01004508 return t;
Willy Tarreau62f52692017-10-08 23:01:42 +02004509}
4510
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004511/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004512static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
4513{
4514 struct h2s *h2s = cs->ctx;
4515
Willy Tarreau7838a792019-08-12 18:42:03 +02004516 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004517 if (cs->flags & CS_FL_KILL_CONN)
4518 h2s->flags |= H2_SF_KILL_CONN;
4519
Willy Tarreau7838a792019-08-12 18:42:03 +02004520 if (mode)
4521 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004522
Willy Tarreau7838a792019-08-12 18:42:03 +02004523 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004524}
4525
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004526/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004527static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
4528{
4529 struct h2s *h2s = cs->ctx;
4530
Willy Tarreau7838a792019-08-12 18:42:03 +02004531 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004532 if (cs->flags & CS_FL_KILL_CONN)
4533 h2s->flags |= H2_SF_KILL_CONN;
4534
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004535 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004536 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004537}
4538
Christopher Faulet9b79a102019-07-15 11:22:56 +02004539/* Decode the payload of a HEADERS frame and produce the HTX request or response
4540 * depending on the connection's side. Returns a positive value on success, a
4541 * negative value on failure, or 0 if it couldn't proceed. May report connection
4542 * errors in h2c->errcode if the frame is non-decodable and the connection
4543 * unrecoverable. In absence of connection error when a failure is reported, the
4544 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004545 *
4546 * The function may fold CONTINUATION frames into the initial HEADERS frame
4547 * by removing padding and next frame header, then moving the CONTINUATION
4548 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4549 * leaving a hole between the main frame and the beginning of the next one.
4550 * The possibly remaining incomplete or next frame at the end may be moved
4551 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4552 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4553 *
4554 * A buffer at the beginning of processing may look like this :
4555 *
4556 * ,---.---------.-----.--------------.--------------.------.---.
4557 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4558 * `---^---------^-----^--------------^--------------^------^---'
4559 * | | <-----> | |
4560 * area | dpl | wrap
4561 * |<--------------> |
4562 * | dfl |
4563 * |<-------------------------------------------------->|
4564 * head data
4565 *
4566 * Padding is automatically overwritten when folding, participating to the
4567 * hole size after dfl :
4568 *
4569 * ,---.------------------------.-----.--------------.------.---.
4570 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4571 * `---^------------------------^-----^--------------^------^---'
4572 * | | <-----> | |
4573 * area | hole | wrap
4574 * |<-----------------------> |
4575 * | dfl |
4576 * |<-------------------------------------------------->|
4577 * head data
4578 *
4579 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4580 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4581 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004582 *
4583 * The <flags> field must point to either the stream's flags or to a copy of it
4584 * so that the function can update the following flags :
4585 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004586 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004587 *
4588 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4589 * decoding, in order to detect if we're dealing with a headers or a trailers
4590 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004591 */
Amaury Denoyelle74162742020-12-11 17:53:05 +01004592static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len, char *upgrade_protocol)
Willy Tarreau13278b42017-10-13 19:23:14 +02004593{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004594 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004595 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004596 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004597 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004598 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004599 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004600 int flen; // header frame len
4601 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004602 int ret = 0;
4603 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004604 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004605
Willy Tarreau7838a792019-08-12 18:42:03 +02004606 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4607
Willy Tarreauea18f862018-12-22 20:19:26 +01004608next_frame:
4609 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4610 goto leave; // incomplete input frame
4611
4612 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4613 * this case, we'll try to paste it immediately after the initial
4614 * HEADERS frame payload and kill any possible padding. The initial
4615 * frame's length will be increased to represent the concatenation
4616 * of the two frames. The next frame is read from position <tlen>
4617 * and written at position <flen> (minus padding if some is present).
4618 */
4619 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4620 struct h2_fh hdr;
4621 int clen; // CONTINUATION frame's payload length
4622
Willy Tarreau7838a792019-08-12 18:42:03 +02004623 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 +01004624 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4625 /* no more data, the buffer may be full, either due to
4626 * too large a frame or because of too large a hole that
4627 * we're going to compact at the end.
4628 */
4629 goto leave;
4630 }
4631
4632 if (hdr.ft != H2_FT_CONTINUATION) {
4633 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004634 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 +01004635 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004636 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004637 goto fail;
4638 }
4639
4640 if (hdr.sid != h2c->dsi) {
4641 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004642 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 +01004643 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004644 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreauea18f862018-12-22 20:19:26 +01004645 goto fail;
4646 }
4647
4648 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4649 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004650 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 +01004651 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4652 goto fail;
4653 }
4654
4655 /* detect when we must stop aggragating frames */
4656 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4657
4658 /* Take as much as we can of the CONTINUATION frame's payload */
4659 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4660 if (clen > hdr.len)
4661 clen = hdr.len;
4662
4663 /* Move the frame's payload over the padding, hole and frame
4664 * header. At least one of hole or dpl is null (see diagrams
4665 * above). The hole moves after the new aggragated frame.
4666 */
4667 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 +02004668 h2c->dfl += hdr.len - h2c->dpl;
Willy Tarreauea18f862018-12-22 20:19:26 +01004669 hole += h2c->dpl + 9;
4670 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004671 TRACE_STATE("waiting for next continuation frame", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_CONT|H2_EV_RX_HDR, h2c->conn);
Willy Tarreauea18f862018-12-22 20:19:26 +01004672 goto next_frame;
4673 }
4674
4675 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004676
Willy Tarreau13278b42017-10-13 19:23:14 +02004677 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004678 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004679 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004680 copy = alloc_trash_chunk();
4681 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004682 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 +02004683 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4684 goto fail;
4685 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004686 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4687 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4688 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004689 }
4690
Willy Tarreau13278b42017-10-13 19:23:14 +02004691 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4692 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004693 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004694 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004695 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 +01004696 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004697 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004698 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004699 }
4700
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004701 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004702 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 +01004703 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4704 goto fail;
4705 }
4706
Willy Tarreau13278b42017-10-13 19:23:14 +02004707 hdrs += 5; // stream dep = 4, weight = 1
4708 flen -= 5;
4709 }
4710
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004711 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004712 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 +01004713 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004714 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004715 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004716
Willy Tarreau937f7602018-02-26 15:22:17 +01004717 /* we can't retry a failed decompression operation so we must be very
4718 * careful not to take any risks. In practice the output buffer is
4719 * always empty except maybe for trailers, in which case we simply have
4720 * to wait for the upper layer to finish consuming what is available.
4721 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004722 htx = htx_from_buf(rxbuf);
4723 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004724 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 +02004725 h2c->flags |= H2_CF_DEM_SFULL;
4726 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004727 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004728
Willy Tarreau25919232019-01-03 14:48:18 +01004729 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004730 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4731 sizeof(list)/sizeof(list[0]), tmp);
4732 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004733 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 +01004734 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
4735 goto fail;
4736 }
4737
Willy Tarreau25919232019-01-03 14:48:18 +01004738 /* The PACK decompressor was updated, let's update the input buffer and
4739 * the parser's state to commit these changes and allow us to later
4740 * fail solely on the stream if needed.
4741 */
4742 b_del(&h2c->dbuf, h2c->dfl + hole);
4743 h2c->dfl = hole = 0;
4744 h2c->st0 = H2_CS_FRAME_H;
4745
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004746 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01004747 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Christopher Fauletd0db4232021-01-22 11:46:30 +01004748 msgf |= (*flags & H2_SF_BODY_TUNNEL) ? H2_MSGF_BODY_TUNNEL: 0;
Amaury Denoyelle74162742020-12-11 17:53:05 +01004749 /* If an Extended CONNECT has been sent on this stream, set message flag
Ilya Shipitsinacf84592021-02-06 22:29:08 +05004750 * to convert 200 response to 101 htx response */
Amaury Denoyelle74162742020-12-11 17:53:05 +01004751 msgf |= (*flags & H2_SF_EXT_CONNECT_SENT) ? H2_MSGF_EXT_CONNECT: 0;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004752
Willy Tarreau88d138e2019-01-02 19:38:14 +01004753 if (*flags & H2_SF_HEADERS_RCVD)
4754 goto trailers;
4755
4756 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004757 if (h2c->flags & H2_CF_IS_BACK)
Amaury Denoyelle74162742020-12-11 17:53:05 +01004758 outlen = h2_make_htx_response(list, htx, &msgf, body_len, upgrade_protocol);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004759 else
4760 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004761
Christopher Faulet3d875582021-04-26 17:46:13 +02004762 if (outlen < 0 || htx_free_space(htx) < global.tune.maxrewrite) {
Willy Tarreau25919232019-01-03 14:48:18 +01004763 /* too large headers? this is a stream error only */
Christopher Faulet3d875582021-04-26 17:46:13 +02004764 TRACE_STATE("message headers too large", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR|H2_EV_PROTO_ERR, h2c->conn);
4765 htx->flags |= HTX_FL_PARSING_ERROR;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004766 goto fail;
4767 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004768
Willy Tarreau174b06a2018-04-25 18:13:58 +02004769 if (msgf & H2_MSGF_BODY) {
4770 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004771 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004772 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004773 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004774 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02004775 }
Christopher Faulet7d247f02020-12-02 14:26:36 +01004776 if (msgf & H2_MSGF_BODYLESS_RSP)
4777 *flags |= H2_SF_BODYLESS_RESP;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004778
Christopher Fauletd0db4232021-01-22 11:46:30 +01004779 if (msgf & H2_MSGF_BODY_TUNNEL)
4780 *flags |= H2_SF_BODY_TUNNEL;
4781 else {
4782 /* Abort the tunnel attempt, if any */
4783 if (*flags & H2_SF_BODY_TUNNEL)
4784 *flags |= H2_SF_TUNNEL_ABRT;
4785 *flags &= ~H2_SF_BODY_TUNNEL;
4786 }
4787
Willy Tarreau88d138e2019-01-02 19:38:14 +01004788 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01004789 /* indicate that a HEADERS frame was received for this stream, except
4790 * for 1xx responses. For 1xx responses, another HEADERS frame is
4791 * expected.
4792 */
4793 if (!(msgf & H2_MSGF_RSP_1XX))
4794 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004795
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004796 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
4797 /* no more data are expected for this message */
4798 htx->flags |= HTX_FL_EOM;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004799 }
Willy Tarreau937f7602018-02-26 15:22:17 +01004800
Amaury Denoyelleefe22762020-12-11 17:53:08 +01004801 if (msgf & H2_MSGF_EXT_CONNECT)
4802 *flags |= H2_SF_EXT_CONNECT_RCVD;
4803
Willy Tarreau86277d42019-01-02 15:36:11 +01004804 /* success */
4805 ret = 1;
4806
Willy Tarreau68dd9852017-07-03 14:44:26 +02004807 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01004808 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01004809 * move the remaining data over it.
4810 */
4811 if (hole) {
4812 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
4813 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
4814 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
4815 b_sub(&h2c->dbuf, hole);
4816 }
4817
Christopher Faulet07f88d72021-04-21 10:39:53 +02004818 if (b_full(&h2c->dbuf) && h2c->dfl) {
Willy Tarreauea18f862018-12-22 20:19:26 +01004819 /* too large frames */
4820 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01004821 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01004822 }
4823
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004824 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004825 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02004826 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02004827 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01004828 return ret;
4829
Willy Tarreau68dd9852017-07-03 14:44:26 +02004830 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01004831 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02004832 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004833
4834 trailers:
4835 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01004836 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
4837 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02004838 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 +01004839 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau4781b152021-04-06 13:53:36 +02004840 HA_ATOMIC_INC(&h2c->px_counters->conn_proto_err);
Willy Tarreau88d138e2019-01-02 19:38:14 +01004841 goto fail;
4842 }
4843
Christopher Faulet9b79a102019-07-15 11:22:56 +02004844 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02004845 if (h2_make_htx_trailers(list, htx) <= 0) {
4846 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 +02004847 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004848 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004849 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02004850}
4851
Christopher Faulet9b79a102019-07-15 11:22:56 +02004852/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004853 * parser state is automatically updated. Returns > 0 if it could completely
4854 * send the current frame, 0 if it couldn't complete, in which case
4855 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
4856 * DATA frame can return 0 as a valid result). Stream errors are reported in
4857 * h2s->errcode and connection errors in h2c->errcode. The caller must already
4858 * have checked the frame header and ensured that the frame was complete or the
4859 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02004860 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01004861static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02004862{
4863 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004864 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004865 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004866 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004867 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004868 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02004869
Willy Tarreau7838a792019-08-12 18:42:03 +02004870 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
4871
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004872 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02004873
Olivier Houchard638b7992018-08-16 15:41:52 +02004874 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004875 if (!csbuf) {
4876 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02004877 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 +01004878 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004879 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004880 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004881
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004882try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004883 flen = h2c->dfl - h2c->dpl;
4884 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01004885 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004886
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004887 if (flen > b_data(&h2c->dbuf)) {
4888 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004889 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01004890 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004891 }
4892
Christopher Faulet9b79a102019-07-15 11:22:56 +02004893 block = htx_free_data_space(htx);
4894 if (!block) {
4895 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004896 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 +02004897 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004898 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004899 if (flen > block)
4900 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004901
Christopher Faulet9b79a102019-07-15 11:22:56 +02004902 /* here, flen is the max we can copy into the output buffer */
4903 block = b_contig_data(&h2c->dbuf, 0);
4904 if (flen > block)
4905 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004906
Christopher Faulet9b79a102019-07-15 11:22:56 +02004907 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau022e5e52020-09-10 09:33:15 +02004908 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 +02004909
Christopher Faulet9b79a102019-07-15 11:22:56 +02004910 b_del(&h2c->dbuf, sent);
4911 h2c->dfl -= sent;
4912 h2c->rcvd_c += sent;
4913 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02004914
Christopher Faulet9b79a102019-07-15 11:22:56 +02004915 if (h2s->flags & H2_SF_DATA_CLEN) {
4916 h2s->body_len -= sent;
4917 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004918 }
4919
Christopher Faulet9b79a102019-07-15 11:22:56 +02004920 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01004921 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004922 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 +01004923 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004924 }
4925
Christopher Faulet9b79a102019-07-15 11:22:56 +02004926 goto try_again;
4927
Willy Tarreau4a28da12018-01-04 14:41:00 +01004928 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004929 /* here we're done with the frame, all the payload (except padding) was
4930 * transferred.
4931 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004932
Christopher Faulet5be651d2021-01-22 15:28:03 +01004933 if (!(h2s->flags & H2_SF_BODY_TUNNEL) && (h2c->dff & H2_F_DATA_END_STREAM)) {
4934 /* no more data are expected for this message. This add the EOM
4935 * flag but only on the response path or if no tunnel attempt
4936 * was aborted. Otherwise (request path + tunnel abrted), the
4937 * EOM was already reported.
4938 */
Christopher Faulet33724322021-02-10 09:04:59 +01004939 if ((h2c->flags & H2_CF_IS_BACK) || !(h2s->flags & H2_SF_TUNNEL_ABRT)) {
4940 /* If we receive an empty DATA frame with ES flag while the HTX
4941 * message is empty, we must be sure to push a block to be sure
4942 * the HTX EOM flag will be handled on the other side. It is a
4943 * workaround because for now it is not possible to push empty
4944 * HTX DATA block. And without this block, there is no way to
4945 * "commit" the end of the message.
4946 */
4947 if (htx_is_empty(htx)) {
4948 if (!htx_add_endof(htx, HTX_BLK_EOT))
4949 goto fail;
4950 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004951 htx->flags |= HTX_FL_EOM;
Christopher Faulet33724322021-02-10 09:04:59 +01004952 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004953 }
4954
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004955 h2c->rcvd_c += h2c->dpl;
4956 h2c->rcvd_s += h2c->dpl;
4957 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004958 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02004959 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004960 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004961 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004962 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004963 if (htx)
4964 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004965 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004966 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004967}
4968
Willy Tarreau115e83b2018-12-01 19:17:53 +01004969/* Try to send a HEADERS frame matching HTX response present in HTX message
4970 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4971 * must check the stream's status to detect any error which might have happened
4972 * subsequently to a successful send. The htx blocks are automatically removed
4973 * from the message. The htx message is assumed to be valid since produced from
4974 * the internal code, hence it contains a start line, an optional series of
4975 * header blocks and an end of header, otherwise an invalid frame could be
4976 * emitted and the resulting htx message could be left in an inconsistent state.
4977 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004978static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004979{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004980 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004981 struct h2c *h2c = h2s->h2c;
4982 struct htx_blk *blk;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004983 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004984 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004985 struct htx_sl *sl;
4986 enum htx_blk_type type;
4987 int es_now = 0;
4988 int ret = 0;
4989 int hdr;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004990
Willy Tarreau7838a792019-08-12 18:42:03 +02004991 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4992
Willy Tarreau115e83b2018-12-01 19:17:53 +01004993 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004994 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004995 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004996 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004997 return 0;
4998 }
4999
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005000 /* get the start line (we do have one) and the rest of the headers,
5001 * that we dump starting at header 0 */
5002 sl = NULL;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005003 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005004 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01005005 type = htx_get_blk_type(blk);
5006
5007 if (type == HTX_BLK_UNUSED)
5008 continue;
5009
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005010 if (type == HTX_BLK_EOH)
Willy Tarreau115e83b2018-12-01 19:17:53 +01005011 break;
5012
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005013 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005014 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005015 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5016 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5017 goto fail;
5018 }
5019
5020 list[hdr].n = htx_get_blk_name(htx, blk);
5021 list[hdr].v = htx_get_blk_value(htx, blk);
5022 hdr++;
5023 }
5024 else if (type == HTX_BLK_RES_SL) {
Christopher Faulet56498132021-01-29 11:39:43 +01005025 BUG_ON(sl); /* Only one start-line expected */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005026 sl = htx_get_blk_ptr(htx, blk);
5027 h2s->status = sl->info.res.status;
Christopher Faulet7d247f02020-12-02 14:26:36 +01005028 if (h2s->status == 204 || h2s->status == 304)
5029 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005030 if (h2s->status < 100 || h2s->status > 999) {
5031 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5032 goto fail;
5033 }
5034 else if (h2s->status == 101) {
Amaury Denoyelleefe22762020-12-11 17:53:08 +01005035 if (unlikely(h2s->flags & H2_SF_EXT_CONNECT_RCVD)) {
5036 /* If an Extended CONNECT has been received, we need to convert 101 to 200 */
5037 h2s->status = 200;
5038 h2s->flags &= ~H2_SF_EXT_CONNECT_RCVD;
5039 }
5040 else {
5041 /* Otherwise, 101 responses are not supported in H2, so return a error (RFC7540#8.1.1) */
5042 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5043 goto fail;
5044 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005045 }
5046 else if ((h2s->flags & H2_SF_BODY_TUNNEL) && h2s->status >= 300) {
5047 /* Abort the tunnel attempt */
5048 h2s->flags &= ~H2_SF_BODY_TUNNEL;
5049 h2s->flags |= H2_SF_TUNNEL_ABRT;
5050 }
5051 }
5052 else {
5053 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 +01005054 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005055 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005056 }
5057
Christopher Faulet56498132021-01-29 11:39:43 +01005058 /* The start-line me be defined */
5059 BUG_ON(!sl);
5060
Willy Tarreau115e83b2018-12-01 19:17:53 +01005061 /* marker for end of headers */
5062 list[hdr].n = ist("");
5063
Willy Tarreau9c218e72019-05-26 10:08:28 +02005064 mbuf = br_tail(h2c->mbuf);
5065 retry:
5066 if (!h2_get_buf(h2c, mbuf)) {
5067 h2c->flags |= H2_CF_MUX_MALLOC;
5068 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005069 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 +02005070 return 0;
5071 }
5072
Willy Tarreau115e83b2018-12-01 19:17:53 +01005073 chunk_reset(&outbuf);
5074
5075 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005076 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5077 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005078 break;
5079 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005080 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01005081 }
5082
5083 if (outbuf.size < 9)
5084 goto full;
5085
5086 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5087 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5088 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5089 outbuf.data = 9;
5090
5091 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01005092 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005093 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005094 goto realign_again;
5095 goto full;
5096 }
5097
5098 /* encode all headers, stop at empty name */
5099 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
5100 /* these ones do not exist in H2 and must be dropped. */
5101 if (isteq(list[hdr].n, ist("connection")) ||
5102 isteq(list[hdr].n, ist("proxy-connection")) ||
5103 isteq(list[hdr].n, ist("keep-alive")) ||
5104 isteq(list[hdr].n, ist("upgrade")) ||
5105 isteq(list[hdr].n, ist("transfer-encoding")))
5106 continue;
5107
Christopher Faulet86d144c2019-08-14 16:32:25 +02005108 /* Skip all pseudo-headers */
5109 if (*(list[hdr].n.ptr) == ':')
5110 continue;
5111
Willy Tarreau115e83b2018-12-01 19:17:53 +01005112 if (isteq(list[hdr].n, ist("")))
5113 break; // end
5114
5115 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
5116 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005117 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005118 goto realign_again;
5119 goto full;
5120 }
5121 }
5122
Willy Tarreaucb985a42019-10-07 16:56:34 +02005123 /* update the frame's size */
5124 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5125
5126 if (outbuf.data > h2c->mfs + 9) {
5127 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5128 /* output full */
5129 if (b_space_wraps(mbuf))
5130 goto realign_again;
5131 goto full;
5132 }
5133 }
5134
Willy Tarreau3a537072021-06-17 08:40:04 +02005135 TRACE_USER("sent H2 response ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5136
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005137 /* remove all header blocks including the EOH and compute the
5138 * corresponding size.
Willy Tarreau115e83b2018-12-01 19:17:53 +01005139 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005140 ret = 0;
5141 blk = htx_get_head_blk(htx);
5142 while (blk) {
5143 type = htx_get_blk_type(blk);
5144 ret += htx_get_blksz(blk);
5145 blk = htx_remove_blk(htx, blk);
5146 /* The removed block is the EOH */
5147 if (type == HTX_BLK_EOH)
5148 break;
Christopher Faulet5be651d2021-01-22 15:28:03 +01005149 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005150
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005151 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) {
5152 /* Response already closed: add END_STREAM */
5153 es_now = 1;
5154 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005155 else if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && h2s->status >= 200) {
5156 /* EOM+empty: we may need to add END_STREAM except for 1xx
Christopher Faulet991febd2020-12-02 15:17:31 +01005157 * responses and tunneled response.
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005158 */
Christopher Faulet991febd2020-12-02 15:17:31 +01005159 if (!(h2s->flags & H2_SF_BODY_TUNNEL) || h2s->status >= 300)
5160 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005161 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005162
Willy Tarreau115e83b2018-12-01 19:17:53 +01005163 if (es_now)
5164 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5165
5166 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005167 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01005168
5169 /* indicates the HEADERS frame was sent, except for 1xx responses. For
5170 * 1xx responses, another HEADERS frame is expected.
5171 */
Christopher Faulet89899422020-12-07 18:24:43 +01005172 if (h2s->status >= 200)
Christopher Faulet0b465482019-02-19 15:14:23 +01005173 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005174
Willy Tarreau115e83b2018-12-01 19:17:53 +01005175 if (es_now) {
5176 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005177 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 +01005178 if (h2s->st == H2_SS_OPEN)
5179 h2s->st = H2_SS_HLOC;
5180 else
5181 h2s_close(h2s);
5182 }
5183
5184 /* OK we could properly deliver the response */
Willy Tarreau115e83b2018-12-01 19:17:53 +01005185 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005186 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005187 return ret;
5188 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005189 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5190 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005191 h2c->flags |= H2_CF_MUX_MFULL;
5192 h2s->flags |= H2_SF_BLK_MROOM;
5193 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005194 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 +01005195 goto end;
5196 fail:
5197 /* unparsable HTX messages, too large ones to be produced in the local
5198 * list etc go here (unrecoverable errors).
5199 */
5200 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5201 ret = 0;
5202 goto end;
5203}
5204
Willy Tarreau80739692018-10-05 11:35:57 +02005205/* Try to send a HEADERS frame matching HTX request present in HTX message
5206 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5207 * must check the stream's status to detect any error which might have happened
5208 * subsequently to a successful send. The htx blocks are automatically removed
5209 * from the message. The htx message is assumed to be valid since produced from
5210 * the internal code, hence it contains a start line, an optional series of
5211 * header blocks and an end of header, otherwise an invalid frame could be
5212 * emitted and the resulting htx message could be left in an inconsistent state.
5213 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005214static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02005215{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005216 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02005217 struct h2c *h2c = h2s->h2c;
5218 struct htx_blk *blk;
Willy Tarreau80739692018-10-05 11:35:57 +02005219 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005220 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02005221 struct htx_sl *sl;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005222 struct ist meth, uri, auth, host = IST_NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005223 enum htx_blk_type type;
5224 int es_now = 0;
5225 int ret = 0;
5226 int hdr;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005227 int extended_connect = 0;
Willy Tarreau80739692018-10-05 11:35:57 +02005228
Willy Tarreau7838a792019-08-12 18:42:03 +02005229 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5230
Willy Tarreau80739692018-10-05 11:35:57 +02005231 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005232 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02005233 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005234 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02005235 return 0;
5236 }
5237
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005238 /* get the start line (we do have one) and the rest of the headers,
5239 * that we dump starting at header 0 */
5240 sl = NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005241 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005242 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005243 type = htx_get_blk_type(blk);
5244
5245 if (type == HTX_BLK_UNUSED)
5246 continue;
5247
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005248 if (type == HTX_BLK_EOH)
Willy Tarreau80739692018-10-05 11:35:57 +02005249 break;
5250
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005251 if (type == HTX_BLK_HDR) {
Christopher Faulet56498132021-01-29 11:39:43 +01005252 BUG_ON(!sl); /* The start-line mut be defined before any headers */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005253 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5254 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5255 goto fail;
5256 }
Willy Tarreau80739692018-10-05 11:35:57 +02005257
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005258 list[hdr].n = htx_get_blk_name(htx, blk);
5259 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02005260
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005261 /* Skip header if same name is used to add the server name */
5262 if ((h2c->flags & H2_CF_IS_BACK) && h2c->proxy->server_id_hdr_name &&
5263 isteq(list[hdr].n, ist2(h2c->proxy->server_id_hdr_name, h2c->proxy->server_id_hdr_len)))
5264 continue;
Christopher Faulet67d58092019-10-02 10:51:38 +02005265
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005266 /* Convert connection: upgrade to Extended connect from rfc 8441 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005267 if (isteqi(list[hdr].n, ist("connection"))) {
5268 /* rfc 7230 #6.1 Connection = list of tokens */
5269 struct ist connection_ist = list[hdr].v;
5270 do {
5271 if (isteqi(iststop(connection_ist, ','),
5272 ist("upgrade"))) {
5273 h2s->flags |= (H2_SF_BODY_TUNNEL|H2_SF_EXT_CONNECT_SENT);
5274 sl->info.req.meth = HTTP_METH_CONNECT;
5275 meth = ist("CONNECT");
5276
5277 extended_connect = 1;
5278 break;
5279 }
5280
5281 connection_ist = istadv(istfind(connection_ist, ','), 1);
5282 } while (istlen(connection_ist));
5283 }
5284
5285 if (isteq(list[hdr].n, ist("upgrade"))) {
5286 /* rfc 7230 #6.7 Upgrade = list of protocols
5287 * rfc 8441 #4 Extended connect = :protocol is single-valued
5288 *
5289 * only first HTTP/1 protocol is preserved
5290 */
5291 const struct ist protocol = iststop(list[hdr].v, ',');
5292 /* upgrade_protocol field is 16 bytes long in h2s */
5293 istpad(h2s->upgrade_protocol, isttrim(protocol, 15));
5294 }
5295
5296 if (isteq(list[hdr].n, ist("host")))
5297 host = list[hdr].v;
5298
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005299 hdr++;
5300 }
Christopher Fauletc29b4bf2021-01-29 11:49:16 +01005301 else if (type == HTX_BLK_REQ_SL) {
5302 BUG_ON(sl); /* Only one start-line expected */
5303 sl = htx_get_blk_ptr(htx, blk);
5304 meth = htx_sl_req_meth(sl);
5305 uri = htx_sl_req_uri(sl);
5306 if (sl->info.req.meth == HTTP_METH_HEAD)
5307 h2s->flags |= H2_SF_BODYLESS_RESP;
5308 if (unlikely(uri.len == 0)) {
5309 TRACE_ERROR("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5310 goto fail;
5311 }
5312 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005313 else {
5314 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5315 goto fail;
5316 }
Willy Tarreau80739692018-10-05 11:35:57 +02005317 }
5318
Christopher Faulet56498132021-01-29 11:39:43 +01005319 /* The start-line me be defined */
5320 BUG_ON(!sl);
5321
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005322 /* Now add the server name to a header (if requested) */
5323 if ((h2c->flags & H2_CF_IS_BACK) && h2c->proxy->server_id_hdr_name) {
5324 struct server *srv = objt_server(h2c->conn->target);
5325
5326 if (srv) {
5327 list[hdr].n = ist2(h2c->proxy->server_id_hdr_name, h2c->proxy->server_id_hdr_len);
5328 list[hdr].v = ist(srv->id);
5329 hdr++;
5330 }
5331 }
5332
Willy Tarreau80739692018-10-05 11:35:57 +02005333 /* marker for end of headers */
5334 list[hdr].n = ist("");
5335
Willy Tarreau9c218e72019-05-26 10:08:28 +02005336 mbuf = br_tail(h2c->mbuf);
5337 retry:
5338 if (!h2_get_buf(h2c, mbuf)) {
5339 h2c->flags |= H2_CF_MUX_MALLOC;
5340 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005341 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 +02005342 return 0;
5343 }
5344
Willy Tarreau80739692018-10-05 11:35:57 +02005345 chunk_reset(&outbuf);
5346
5347 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005348 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5349 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005350 break;
5351 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005352 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02005353 }
5354
5355 if (outbuf.size < 9)
5356 goto full;
5357
5358 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5359 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5360 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5361 outbuf.data = 9;
5362
5363 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01005364 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005365 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005366 goto realign_again;
5367 goto full;
5368 }
5369
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005370 auth = ist(NULL);
5371
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005372 /* RFC7540 #8.3: the CONNECT method must have :
5373 * - :authority set to the URI part (host:port)
5374 * - :method set to CONNECT
5375 * - :scheme and :path omitted
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005376 *
5377 * Note that this is not applicable in case of the Extended CONNECT
5378 * protocol from rfc 8441.
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005379 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005380 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT) && !extended_connect) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005381 auth = uri;
5382
5383 if (!hpack_encode_header(&outbuf, ist(":authority"), auth)) {
5384 /* output full */
5385 if (b_space_wraps(mbuf))
5386 goto realign_again;
5387 goto full;
5388 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005389 h2s->flags |= H2_SF_BODY_TUNNEL;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005390 } else {
5391 /* other methods need a :scheme. If an authority is known from
5392 * the request line, it must be sent, otherwise only host is
5393 * sent. Host is never sent as the authority.
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005394 *
5395 * This code is also applicable for Extended CONNECT protocol
5396 * from rfc 8441.
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005397 */
5398 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02005399
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005400 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
5401 /* the URI seems to start with a scheme */
5402 int len = 1;
5403
5404 while (len < uri.len && uri.ptr[len] != ':')
5405 len++;
5406
5407 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5408 /* make the uri start at the authority now */
Tim Duesterhus9f75ed12021-03-02 18:57:26 +01005409 scheme = ist2(uri.ptr, len);
Tim Duesterhus154374c2021-03-02 18:57:27 +01005410 uri = istadv(uri, len + 3);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005411
5412 /* find the auth part of the URI */
Tim Duesterhus92c696e2021-02-28 16:11:36 +01005413 auth = ist2(uri.ptr, 0);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005414 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5415 auth.len++;
5416
Tim Duesterhus154374c2021-03-02 18:57:27 +01005417 uri = istadv(uri, auth.len);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005418 }
5419 }
5420
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005421 /* For Extended CONNECT, the :authority must be present.
5422 * Use host value for it.
5423 */
5424 if (unlikely(extended_connect) && isttest(host))
5425 auth = host;
5426
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005427 if (!scheme.len) {
5428 /* no explicit scheme, we're using an origin-form URI,
5429 * probably from an H1 request transcoded to H2 via an
5430 * external layer, then received as H2 without authority.
5431 * So we have to look up the scheme from the HTX flags.
5432 * In such a case only http and https are possible, and
5433 * https is the default (sent by browsers).
5434 */
5435 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5436 scheme = ist("http");
5437 else
5438 scheme = ist("https");
5439 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005440
5441 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005442 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005443 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005444 goto realign_again;
5445 goto full;
5446 }
Willy Tarreau80739692018-10-05 11:35:57 +02005447
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005448 if (auth.len && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005449 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005450 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005451 goto realign_again;
5452 goto full;
5453 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005454
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005455 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5456 * be sent as '/' or '*'.
5457 */
5458 if (unlikely(!uri.len)) {
5459 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5460 uri = ist("*");
5461 else
5462 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005463 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005464
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005465 if (!hpack_encode_path(&outbuf, uri)) {
5466 /* output full */
5467 if (b_space_wraps(mbuf))
5468 goto realign_again;
5469 goto full;
5470 }
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005471
5472 /* encode the pseudo-header protocol from rfc8441 if using
5473 * Extended CONNECT method.
5474 */
5475 if (unlikely(extended_connect)) {
5476 const struct ist protocol = ist(h2s->upgrade_protocol);
5477 if (isttest(protocol)) {
5478 if (!hpack_encode_header(&outbuf,
5479 ist(":protocol"),
5480 protocol)) {
5481 /* output full */
5482 if (b_space_wraps(mbuf))
5483 goto realign_again;
5484 goto full;
5485 }
5486 }
5487 }
Willy Tarreau80739692018-10-05 11:35:57 +02005488 }
5489
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005490 /* encode all headers, stop at empty name. Host is only sent if we
5491 * do not provide an authority.
5492 */
Willy Tarreau80739692018-10-05 11:35:57 +02005493 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005494 struct ist n = list[hdr].n;
5495 struct ist v = list[hdr].v;
5496
Willy Tarreau80739692018-10-05 11:35:57 +02005497 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005498 if (isteq(n, ist("connection")) ||
5499 (auth.len && isteq(n, ist("host"))) ||
5500 isteq(n, ist("proxy-connection")) ||
5501 isteq(n, ist("keep-alive")) ||
5502 isteq(n, ist("upgrade")) ||
5503 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005504 continue;
5505
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005506 if (isteq(n, ist("te"))) {
5507 /* "te" may only be sent with "trailers" if this value
5508 * is present, otherwise it must be deleted.
5509 */
5510 v = istist(v, ist("trailers"));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01005511 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005512 continue;
5513 v = ist("trailers");
5514 }
5515
Christopher Faulet86d144c2019-08-14 16:32:25 +02005516 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005517 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005518 continue;
5519
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005520 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005521 break; // end
5522
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005523 if (!hpack_encode_header(&outbuf, n, v)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005524 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005525 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005526 goto realign_again;
5527 goto full;
5528 }
5529 }
5530
Willy Tarreaucb985a42019-10-07 16:56:34 +02005531 /* update the frame's size */
5532 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5533
5534 if (outbuf.data > h2c->mfs + 9) {
5535 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5536 /* output full */
5537 if (b_space_wraps(mbuf))
5538 goto realign_again;
5539 goto full;
5540 }
5541 }
5542
Willy Tarreau3a537072021-06-17 08:40:04 +02005543 TRACE_USER("sent H2 request ", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
5544
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005545 /* remove all header blocks including the EOH and compute the
5546 * corresponding size.
Willy Tarreau80739692018-10-05 11:35:57 +02005547 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005548 ret = 0;
5549 blk = htx_get_head_blk(htx);
5550 while (blk) {
5551 type = htx_get_blk_type(blk);
5552 ret += htx_get_blksz(blk);
5553 blk = htx_remove_blk(htx, blk);
5554 /* The removed block is the EOH */
5555 if (type == HTX_BLK_EOH)
5556 break;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005557 }
Willy Tarreau80739692018-10-05 11:35:57 +02005558
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005559 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) {
5560 /* Request already closed: add END_STREAM */
Willy Tarreau80739692018-10-05 11:35:57 +02005561 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005562 }
5563 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5564 /* EOM+empty: we may need to add END_STREAM (except for CONNECT
5565 * request)
5566 */
5567 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5568 es_now = 1;
5569 }
Willy Tarreau80739692018-10-05 11:35:57 +02005570
Willy Tarreau80739692018-10-05 11:35:57 +02005571 if (es_now)
5572 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5573
5574 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005575 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02005576 h2s->flags |= H2_SF_HEADERS_SENT;
5577 h2s->st = H2_SS_OPEN;
5578
Willy Tarreau80739692018-10-05 11:35:57 +02005579 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005580 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 +02005581 // trim any possibly pending data (eg: inconsistent content-length)
5582 h2s->flags |= H2_SF_ES_SENT;
5583 h2s->st = H2_SS_HLOC;
5584 }
5585
Willy Tarreau80739692018-10-05 11:35:57 +02005586 end:
5587 return ret;
5588 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005589 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5590 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02005591 h2c->flags |= H2_CF_MUX_MFULL;
5592 h2s->flags |= H2_SF_BLK_MROOM;
5593 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005594 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 +02005595 goto end;
5596 fail:
5597 /* unparsable HTX messages, too large ones to be produced in the local
5598 * list etc go here (unrecoverable errors).
5599 */
5600 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5601 ret = 0;
5602 goto end;
5603}
5604
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005605/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005606 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5607 * caller must check the stream's status to detect any error which might have
5608 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005609 * consumed, or zero if nothing done.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005610 */
Christopher Faulet142854b2020-12-02 15:12:40 +01005611static size_t h2s_make_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005612{
5613 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005614 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005615 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005616 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005617 size_t total = 0;
5618 int es_now = 0;
5619 int bsize; /* htx block size */
5620 int fsize; /* h2 frame size */
5621 struct htx_blk *blk;
5622 enum htx_blk_type type;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005623 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005624
Willy Tarreau7838a792019-08-12 18:42:03 +02005625 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5626
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005627 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005628 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005629 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005630 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005631 goto end;
5632 }
5633
Willy Tarreau98de12a2018-12-12 07:03:00 +01005634 htx = htx_from_buf(buf);
5635
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005636 /* We only come here with HTX_BLK_DATA blocks */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005637
5638 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005639 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005640 goto end;
5641
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005642 if ((h2c->flags & H2_CF_IS_BACK) &&
Christopher Fauletf95f8762021-01-22 11:59:07 +01005643 (h2s->flags & (H2_SF_HEADERS_RCVD|H2_SF_BODY_TUNNEL)) == H2_SF_BODY_TUNNEL) {
5644 /* The response HEADERS frame not received yet. Thus the tunnel
5645 * is not fully established yet. In this situation, we block
5646 * data sending.
5647 */
5648 h2s->flags |= H2_SF_BLK_MBUSY;
5649 TRACE_STATE("Request DATA frame blocked waiting for tunnel establishment", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5650 goto end;
5651 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01005652 else if ((h2c->flags & H2_CF_IS_BACK) && (h2s->flags & H2_SF_TUNNEL_ABRT)) {
5653 /* a tunnel attempt was aborted but the is pending raw data to xfer to the server.
5654 * Thus the stream is closed with the CANCEL error. The error will be reported to
5655 * the upper layer as aserver abort. But at this stage there is nothing more we can
5656 * do. We just wait for the end of the response to be sure to not truncate it.
5657 */
5658 if (!(h2s->flags & H2_SF_ES_RCVD)) {
5659 TRACE_STATE("Request DATA frame blocked waiting end of aborted tunnel", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5660 h2s->flags |= H2_SF_BLK_MBUSY;
5661 }
5662 else {
5663 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5664 h2s_error(h2s, H2_ERR_CANCEL);
5665 }
5666 goto end;
5667 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01005668
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005669 blk = htx_get_head_blk(htx);
5670 type = htx_get_blk_type(blk);
5671 bsize = htx_get_blksz(blk);
5672 fsize = bsize;
5673 trunc_out = 0;
5674 if (type != HTX_BLK_DATA)
5675 goto end;
5676
Willy Tarreau9c218e72019-05-26 10:08:28 +02005677 mbuf = br_tail(h2c->mbuf);
5678 retry:
5679 if (!h2_get_buf(h2c, mbuf)) {
5680 h2c->flags |= H2_CF_MUX_MALLOC;
5681 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005682 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 +02005683 goto end;
5684 }
5685
Willy Tarreau98de12a2018-12-12 07:03:00 +01005686 /* Perform some optimizations to reduce the number of buffer copies.
5687 * First, if the mux's buffer is empty and the htx area contains
5688 * exactly one data block of the same size as the requested count, and
5689 * this count fits within the frame size, the stream's window size, and
5690 * the connection's window size, then it's possible to simply swap the
5691 * caller's buffer with the mux's output buffer and adjust offsets and
5692 * length to match the entire DATA HTX block in the middle. In this
5693 * case we perform a true zero-copy operation from end-to-end. This is
5694 * the situation that happens all the time with large files. Second, if
5695 * this is not possible, but the mux's output buffer is empty, we still
5696 * have an opportunity to avoid the copy to the intermediary buffer, by
5697 * making the intermediary buffer's area point to the output buffer's
5698 * area. In this case we want to skip the HTX header to make sure that
5699 * copies remain aligned and that this operation remains possible all
5700 * the time. This goes for headers, data blocks and any data extracted
5701 * from the HTX blocks.
5702 */
5703 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02005704 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005705 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005706 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005707
Willy Tarreaubcc45952019-05-26 10:05:50 +02005708 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005709 /* Too bad there are data left there. We're willing to memcpy/memmove
5710 * up to 1/4 of the buffer, which means that it's OK to copy a large
5711 * frame into a buffer containing few data if it needs to be realigned,
5712 * and that it's also OK to copy few data without realigning. Otherwise
5713 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005714 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005715 if (fsize + 9 <= b_room(mbuf) &&
5716 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005717 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
5718 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 +01005719 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02005720 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005721
Willy Tarreau9c218e72019-05-26 10:08:28 +02005722 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5723 goto retry;
5724
Willy Tarreau98de12a2018-12-12 07:03:00 +01005725 h2c->flags |= H2_CF_MUX_MFULL;
5726 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005727 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 +01005728 goto end;
5729 }
5730
Christopher Faulet925abdf2021-04-27 22:51:07 +02005731 if (htx->flags & HTX_FL_EOM) {
5732 /* EOM+empty: we may need to add END_STREAM (except for tunneled
5733 * message)
5734 */
5735 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5736 es_now = 1;
5737 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01005738 /* map an H2 frame to the HTX block so that we can put the
5739 * frame header there.
5740 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005741 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5742 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005743
5744 /* prepend an H2 DATA frame header just before the DATA block */
5745 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5746 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
Christopher Faulet925abdf2021-04-27 22:51:07 +02005747 if (es_now)
5748 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005749 h2_set_frame_size(outbuf.area, fsize);
5750
5751 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005752 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005753 h2c->mws -= fsize;
5754
5755 /* and exchange with our old area */
5756 buf->area = old_area;
5757 buf->data = buf->head = 0;
5758 total += fsize;
Christopher Faulet925abdf2021-04-27 22:51:07 +02005759 fsize = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005760
5761 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 +02005762 goto out;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005763 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005764
Willy Tarreau98de12a2018-12-12 07:03:00 +01005765 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005766 /* for DATA and EOM we'll have to emit a frame, even if empty */
5767
5768 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005769 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5770 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005771 break;
5772 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005773 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005774 }
5775
5776 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005777 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5778 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005779 h2c->flags |= H2_CF_MUX_MFULL;
5780 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005781 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005782 goto end;
5783 }
5784
5785 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5786 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5787 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5788 outbuf.data = 9;
5789
5790 /* we have in <fsize> the exact number of bytes we need to copy from
5791 * the HTX buffer. We need to check this against the connection's and
5792 * the stream's send windows, and to ensure that this fits in the max
5793 * frame size and in the buffer's available space minus 9 bytes (for
5794 * the frame header). The connection's flow control is applied last so
5795 * that we can use a separate list of streams which are immediately
5796 * unblocked on window opening. Note: we don't implement padding.
5797 */
5798
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005799 if (!fsize)
5800 goto send_empty;
5801
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005802 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005803 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreau2b718102021-04-21 07:32:39 +02005804 if (LIST_INLIST(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005805 LIST_DEL_INIT(&h2s->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02005806 LIST_APPEND(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02005807 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 +01005808 goto end;
5809 }
5810
Willy Tarreauee573762018-12-04 15:25:57 +01005811 if (fsize > count)
5812 fsize = count;
5813
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005814 if (fsize > h2s_mws(h2s))
5815 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005816
5817 if (h2c->mfs && fsize > h2c->mfs)
5818 fsize = h2c->mfs; // >0
5819
5820 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005821 /* It doesn't fit at once. If it at least fits once split and
5822 * the amount of data to move is low, let's defragment the
5823 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005824 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005825 if (b_space_wraps(mbuf) &&
5826 (fsize + 9 <= b_room(mbuf)) &&
5827 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005828 goto realign_again;
5829 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005830 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005831
5832 if (fsize <= 0) {
5833 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005834 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5835 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005836 h2c->flags |= H2_CF_MUX_MFULL;
5837 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005838 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005839 goto end;
5840 }
5841 }
5842
5843 if (h2c->mws <= 0) {
5844 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005845 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 +01005846 goto end;
5847 }
5848
5849 if (fsize > h2c->mws)
5850 fsize = h2c->mws;
5851
5852 /* now let's copy this this into the output buffer */
5853 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005854 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005855 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005856 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005857
5858 send_empty:
5859 /* update the frame's size */
5860 h2_set_frame_size(outbuf.area, fsize);
5861
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005862 /* consume incoming HTX block */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005863 total += fsize;
5864 if (fsize == bsize) {
5865 htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005866 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5867 /* EOM+empty: we may need to add END_STREAM (except for tunneled
5868 * message)
5869 */
5870 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5871 es_now = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02005872 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005873 }
5874 else {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005875 /* we've truncated this block */
5876 htx_cut_data_blk(htx, blk, fsize);
5877 }
5878
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005879 if (es_now)
5880 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5881
5882 /* commit the H2 response */
5883 b_add(mbuf, fsize + 9);
5884
Christopher Faulet925abdf2021-04-27 22:51:07 +02005885 out:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005886 if (es_now) {
5887 if (h2s->st == H2_SS_OPEN)
5888 h2s->st = H2_SS_HLOC;
5889 else
5890 h2s_close(h2s);
5891
5892 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005893 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 +01005894 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005895 else if (fsize) {
5896 if (fsize == bsize) {
5897 TRACE_DEVEL("more data may be available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5898 goto new_frame;
5899 }
5900 else if (trunc_out) {
5901 /* we've truncated this block */
5902 goto new_frame;
5903 }
5904 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005905
5906 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005907 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005908 return total;
5909}
5910
Christopher Faulet991febd2020-12-02 15:17:31 +01005911/* Skip the message payload (DATA blocks) and emit an empty DATA frame with the
5912 * ES flag set for stream <h2s>. This function is called for response known to
5913 * have no payload. Only DATA blocks are skipped. This means the trailers are
Ilya Shipitsinacf84592021-02-06 22:29:08 +05005914 * still emitted. The caller must check the stream's status to detect any error
Christopher Faulet991febd2020-12-02 15:17:31 +01005915 * which might have happened subsequently to a successful send. Returns the
5916 * number of data bytes consumed, or zero if nothing done.
5917 */
5918static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count)
5919{
5920 struct h2c *h2c = h2s->h2c;
5921 struct htx *htx;
5922 int bsize; /* htx block size */
5923 int fsize; /* h2 frame size */
5924 struct htx_blk *blk;
5925 enum htx_blk_type type;
5926 size_t total = 0;
5927
5928 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5929
5930 if (h2c_mux_busy(h2c, h2s)) {
5931 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5932 h2s->flags |= H2_SF_BLK_MBUSY;
5933 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5934 goto end;
5935 }
5936
5937 htx = htx_from_buf(buf);
5938
5939 next_data:
5940 if (!count || htx_is_empty(htx))
5941 goto end;
5942 blk = htx_get_head_blk(htx);
5943 type = htx_get_blk_type(blk);
5944 bsize = htx_get_blksz(blk);
5945 fsize = bsize;
5946 if (type != HTX_BLK_DATA)
5947 goto end;
5948
5949 if (fsize > count)
5950 fsize = count;
5951
5952 if (fsize != bsize)
5953 goto skip_data;
5954
5955 if (!(htx->flags & HTX_FL_EOM) || !htx_is_unique_blk(htx, blk))
5956 goto skip_data;
5957
5958 /* Here, it is the last block and it is also the end of the message. So
5959 * we can emit an empty DATA frame with the ES flag set
5960 */
5961 if (h2_send_empty_data_es(h2s) <= 0)
5962 goto end;
5963
5964 if (h2s->st == H2_SS_OPEN)
5965 h2s->st = H2_SS_HLOC;
5966 else
5967 h2s_close(h2s);
5968
5969 skip_data:
5970 /* consume incoming HTX block */
5971 total += fsize;
5972 if (fsize == bsize) {
5973 TRACE_DEVEL("more data may be available, trying to skip another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5974 htx_remove_blk(htx, blk);
5975 goto next_data;
5976 }
5977 else {
5978 /* we've truncated this block */
5979 htx_cut_data_blk(htx, blk, fsize);
5980 }
5981
5982 end:
5983 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5984 return total;
5985}
5986
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005987/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5988 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5989 * processed. The caller must check the stream's status to detect any error
5990 * which might have happened subsequently to a successful send. The htx blocks
5991 * are automatically removed from the message. The htx message is assumed to be
5992 * valid since produced from the internal code. Processing stops when meeting
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005993 * the EOT, which *is* removed. All trailers are processed at once and sent as a
5994 * single frame. The ES flag is always set.
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005995 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005996static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005997{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005998 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005999 struct h2c *h2c = h2s->h2c;
6000 struct htx_blk *blk;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006001 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02006002 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006003 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006004 int ret = 0;
6005 int hdr;
6006 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006007
Willy Tarreau7838a792019-08-12 18:42:03 +02006008 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
6009
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006010 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006011 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006012 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02006013 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006014 goto end;
6015 }
6016
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006017 /* get trailers. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006018 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006019 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006020 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006021
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006022 if (type == HTX_BLK_UNUSED)
6023 continue;
6024
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006025 if (type == HTX_BLK_EOT)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006026 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006027 if (type == HTX_BLK_TLR) {
6028 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
6029 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
6030 goto fail;
6031 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006032
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006033 list[hdr].n = htx_get_blk_name(htx, blk);
6034 list[hdr].v = htx_get_blk_value(htx, blk);
6035 hdr++;
6036 }
6037 else {
6038 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 +01006039 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02006040 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006041 }
6042
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006043 /* marker for end of trailers */
6044 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006045
Willy Tarreau9c218e72019-05-26 10:08:28 +02006046 mbuf = br_tail(h2c->mbuf);
6047 retry:
6048 if (!h2_get_buf(h2c, mbuf)) {
6049 h2c->flags |= H2_CF_MUX_MALLOC;
6050 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02006051 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 +02006052 goto end;
6053 }
6054
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006055 chunk_reset(&outbuf);
6056
6057 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02006058 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
6059 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006060 break;
6061 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02006062 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006063 }
6064
6065 if (outbuf.size < 9)
6066 goto full;
6067
6068 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
6069 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
6070 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
6071 outbuf.data = 9;
6072
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006073 /* encode all headers */
6074 for (idx = 0; idx < hdr; idx++) {
6075 /* these ones do not exist in H2 or must not appear in
6076 * trailers and must be dropped.
6077 */
6078 if (isteq(list[idx].n, ist("host")) ||
6079 isteq(list[idx].n, ist("content-length")) ||
6080 isteq(list[idx].n, ist("connection")) ||
6081 isteq(list[idx].n, ist("proxy-connection")) ||
6082 isteq(list[idx].n, ist("keep-alive")) ||
6083 isteq(list[idx].n, ist("upgrade")) ||
6084 isteq(list[idx].n, ist("te")) ||
6085 isteq(list[idx].n, ist("transfer-encoding")))
6086 continue;
6087
Christopher Faulet86d144c2019-08-14 16:32:25 +02006088 /* Skip all pseudo-headers */
6089 if (*(list[idx].n.ptr) == ':')
6090 continue;
6091
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006092 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
6093 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006094 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006095 goto realign_again;
6096 goto full;
6097 }
6098 }
6099
Willy Tarreau5121e5d2019-05-06 15:13:41 +02006100 if (outbuf.data == 9) {
6101 /* here we have a problem, we have nothing to emit (either we
6102 * received an empty trailers block followed or we removed its
6103 * contents above). Because of this we can't send a HEADERS
6104 * frame, so we have to cheat and instead send an empty DATA
6105 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01006106 */
6107 outbuf.area[3] = H2_FT_DATA;
6108 outbuf.area[4] = H2_F_DATA_END_STREAM;
6109 }
6110
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006111 /* update the frame's size */
6112 h2_set_frame_size(outbuf.area, outbuf.data - 9);
6113
Willy Tarreau572d9f52019-10-11 16:58:37 +02006114 if (outbuf.data > h2c->mfs + 9) {
6115 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
6116 /* output full */
6117 if (b_space_wraps(mbuf))
6118 goto realign_again;
6119 goto full;
6120 }
6121 }
6122
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006123 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02006124 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 +02006125 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006126 h2s->flags |= H2_SF_ES_SENT;
6127
6128 if (h2s->st == H2_SS_OPEN)
6129 h2s->st = H2_SS_HLOC;
6130 else
6131 h2s_close(h2s);
6132
6133 /* OK we could properly deliver the response */
6134 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02006135 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006136 ret = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006137 blk = htx_get_head_blk(htx);
6138 while (blk) {
6139 type = htx_get_blk_type(blk);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006140 ret += htx_get_blksz(blk);
6141 blk = htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006142 /* The removed block is the EOT */
6143 if (type == HTX_BLK_EOT)
6144 break;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006145 }
6146
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006147 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006148 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006149 return ret;
6150 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006151 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6152 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006153 h2c->flags |= H2_CF_MUX_MFULL;
6154 h2s->flags |= H2_SF_BLK_MROOM;
6155 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006156 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 +01006157 goto end;
6158 fail:
6159 /* unparsable HTX messages, too large ones to be produced in the local
6160 * list etc go here (unrecoverable errors).
6161 */
6162 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6163 ret = 0;
6164 goto end;
6165}
6166
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006167/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6168 * event subscriber <es> is not allowed to change from a previous call as long
6169 * as at least one event is still subscribed. The <event_type> must only be a
6170 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006171 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006172static int h2_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02006173{
Olivier Houchard6ff20392018-07-17 18:46:31 +02006174 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02006175 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006176
Willy Tarreau7838a792019-08-12 18:42:03 +02006177 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006178
6179 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006180 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006181
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006182 es->events |= event_type;
6183 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006184
6185 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006186 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006187
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006188 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006189 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02006190 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02006191 !LIST_INLIST(&h2s->list)) {
Olivier Houchardf8338152019-05-14 17:50:32 +02006192 if (h2s->flags & H2_SF_BLK_MFCTL)
Willy Tarreau2b718102021-04-21 07:32:39 +02006193 LIST_APPEND(&h2c->fctl_list, &h2s->list);
Olivier Houchardf8338152019-05-14 17:50:32 +02006194 else
Willy Tarreau2b718102021-04-21 07:32:39 +02006195 LIST_APPEND(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02006196 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02006197 }
Willy Tarreau7838a792019-08-12 18:42:03 +02006198 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006199 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006200}
6201
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006202/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6203 * The <es> pointer is not allowed to differ from the one passed to the
6204 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006205 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006206static int h2_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006207{
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006208 struct h2s *h2s = cs->ctx;
6209
Willy Tarreau7838a792019-08-12 18:42:03 +02006210 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006211
6212 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006213 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006214
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006215 es->events &= ~event_type;
6216 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01006217 h2s->subs = NULL;
6218
6219 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006220 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006221
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006222 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006223 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006224 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006225 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
6226 LIST_DEL_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02006227 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01006228
Willy Tarreau7838a792019-08-12 18:42:03 +02006229 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006230 return 0;
6231}
6232
6233
Olivier Houchard511efea2018-08-16 15:30:32 +02006234/* Called from the upper layer, to receive data */
6235static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
6236{
Olivier Houchard638b7992018-08-16 15:41:52 +02006237 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01006238 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01006239 struct htx *h2s_htx = NULL;
6240 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02006241 size_t ret = 0;
6242
Willy Tarreau7838a792019-08-12 18:42:03 +02006243 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
6244
Olivier Houchard511efea2018-08-16 15:30:32 +02006245 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006246 h2s_htx = htx_from_buf(&h2s->rxbuf);
6247 if (htx_is_empty(h2s_htx)) {
6248 /* Here htx_to_buf() will set buffer data to 0 because
6249 * the HTX is empty.
6250 */
6251 htx_to_buf(h2s_htx, &h2s->rxbuf);
6252 goto end;
6253 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01006254
Christopher Faulet9b79a102019-07-15 11:22:56 +02006255 ret = h2s_htx->data;
6256 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01006257
Christopher Faulet9b79a102019-07-15 11:22:56 +02006258 /* <buf> is empty and the message is small enough, swap the
6259 * buffers. */
6260 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01006261 htx_to_buf(buf_htx, buf);
6262 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006263 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
6264 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01006265 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006266
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006267 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_UNUSED);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006268
6269 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
6270 buf_htx->flags |= HTX_FL_PARSING_ERROR;
6271 if (htx_is_empty(buf_htx))
6272 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01006273 }
Christopher Faulet810df062020-07-22 16:20:34 +02006274 else if (htx_is_empty(h2s_htx))
Christopher Faulet42432f32020-11-20 17:43:16 +01006275 buf_htx->flags |= (h2s_htx->flags & HTX_FL_EOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006276
Christopher Faulet9b79a102019-07-15 11:22:56 +02006277 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
6278 htx_to_buf(buf_htx, buf);
6279 htx_to_buf(h2s_htx, &h2s->rxbuf);
6280 ret -= h2s_htx->data;
6281
Christopher Faulet37070b22019-02-14 15:12:14 +01006282 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02006283 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01006284 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006285 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01006286 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd0db4232021-01-22 11:46:30 +01006287 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02006288 cs->flags |= CS_FL_EOI;
Christopher Fauletd0db4232021-01-22 11:46:30 +01006289 /* Add EOS flag for tunnel */
6290 if (h2s->flags & H2_SF_BODY_TUNNEL)
6291 cs->flags |= CS_FL_EOS;
6292 }
Christopher Fauletaade4ed2020-10-08 15:38:41 +02006293 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02006294 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01006295 if (cs->flags & CS_FL_ERR_PENDING)
6296 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02006297 if (b_size(&h2s->rxbuf)) {
6298 b_free(&h2s->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01006299 offer_buffers(NULL, 1);
Olivier Houchard638b7992018-08-16 15:41:52 +02006300 }
Olivier Houchard511efea2018-08-16 15:30:32 +02006301 }
6302
Willy Tarreau082f5592018-11-25 08:03:32 +01006303 if (ret && h2c->dsi == h2s->id) {
6304 /* demux is blocking on this stream's buffer */
6305 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02006306 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01006307 }
Christopher Faulet37070b22019-02-14 15:12:14 +01006308
Willy Tarreau7838a792019-08-12 18:42:03 +02006309 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02006310 return ret;
6311}
6312
Olivier Houchardd846c262018-10-19 17:24:29 +02006313
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006314/* Called from the upper layer, to send data from buffer <buf> for no more than
6315 * <count> bytes. Returns the number of bytes effectively sent. Some status
6316 * flags may be updated on the conn_stream.
6317 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02006318static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02006319{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006320 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02006321 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02006322 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006323 struct htx *htx;
6324 struct htx_blk *blk;
6325 enum htx_blk_type btype;
6326 uint32_t bsize;
6327 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006328
Willy Tarreau7838a792019-08-12 18:42:03 +02006329 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
6330
Olivier Houchardd360ac62019-03-22 17:37:16 +01006331 /* If we were not just woken because we wanted to send but couldn't,
6332 * and there's somebody else that is waiting to send, do nothing,
6333 * we will subscribe later and be put at the end of the list
6334 */
Willy Tarreaud9464162020-01-10 18:25:07 +01006335 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02006336 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
6337 TRACE_DEVEL("other streams already waiting, going to the queue and leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
Olivier Houchardd360ac62019-03-22 17:37:16 +01006338 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006339 }
Willy Tarreaud9464162020-01-10 18:25:07 +01006340 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02006341
Willy Tarreau7838a792019-08-12 18:42:03 +02006342 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
6343 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 +02006344 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006345 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02006346
Willy Tarreaucab22952019-10-31 15:48:18 +01006347 if (h2s->h2c->st0 >= H2_CS_ERROR) {
6348 cs->flags |= CS_FL_ERROR;
6349 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);
6350 return 0;
6351 }
6352
Christopher Faulet9b79a102019-07-15 11:22:56 +02006353 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006354
Willy Tarreau0bad0432018-06-14 16:54:01 +02006355 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01006356 h2s->flags |= H2_SF_OUTGOING_DATA;
6357
Willy Tarreau751f2d02018-10-05 09:35:00 +02006358 if (h2s->id == 0) {
6359 int32_t id = h2c_get_next_sid(h2s->h2c);
6360
6361 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02006362 cs->flags |= CS_FL_ERROR;
Willy Tarreau7838a792019-08-12 18:42:03 +02006363 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 +02006364 return 0;
6365 }
6366
6367 eb32_delete(&h2s->by_id);
6368 h2s->by_id.key = h2s->id = id;
6369 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01006370 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02006371 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
6372 }
6373
Christopher Faulet9b79a102019-07-15 11:22:56 +02006374 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
6375 count && !htx_is_empty(htx)) {
6376 idx = htx_get_head(htx);
6377 blk = htx_get_blk(htx, idx);
6378 btype = htx_get_blk_type(blk);
6379 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006380
Christopher Faulet9b79a102019-07-15 11:22:56 +02006381 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02006382 case HTX_BLK_REQ_SL:
6383 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006384 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02006385 if (ret > 0) {
6386 total += ret;
6387 count -= ret;
6388 if (ret < bsize)
6389 goto done;
6390 }
6391 break;
6392
Willy Tarreau115e83b2018-12-01 19:17:53 +01006393 case HTX_BLK_RES_SL:
6394 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006395 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01006396 if (ret > 0) {
6397 total += ret;
6398 count -= ret;
6399 if (ret < bsize)
6400 goto done;
6401 }
6402 break;
6403
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006404 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006405 /* all these cause the emission of a DATA frame (possibly empty) */
Christopher Faulet991febd2020-12-02 15:17:31 +01006406 if (!(h2s->h2c->flags & H2_CF_IS_BACK) &&
6407 (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BODYLESS_RESP)) == H2_SF_BODYLESS_RESP)
6408 ret = h2s_skip_data(h2s, buf, count);
6409 else
6410 ret = h2s_make_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006411 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01006412 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006413 total += ret;
6414 count -= ret;
6415 if (ret < bsize)
6416 goto done;
6417 }
6418 break;
6419
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006420 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006421 case HTX_BLK_EOT:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006422 /* This is the first trailers block, all the subsequent ones */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006423 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006424 if (ret > 0) {
6425 total += ret;
6426 count -= ret;
6427 if (ret < bsize)
6428 goto done;
6429 }
6430 break;
6431
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006432 default:
6433 htx_remove_blk(htx, blk);
6434 total += bsize;
6435 count -= bsize;
6436 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006437 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006438 }
6439
Christopher Faulet9b79a102019-07-15 11:22:56 +02006440 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02006441 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02006442 /* trim any possibly pending data after we close (extra CR-LF,
6443 * unprocessed trailers, abnormal extra data, ...)
6444 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02006445 total += count;
6446 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02006447 }
6448
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006449 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01006450 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006451 TRACE_DEVEL("reporting RST/error to the app-layer stream", H2_EV_H2S_SEND|H2_EV_H2S_ERR|H2_EV_STRM_ERR, h2s->h2c->conn, h2s);
Willy Tarreauec988c72018-12-19 18:00:29 +01006452 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01006453 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01006454 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006455 }
6456
Christopher Faulet9b79a102019-07-15 11:22:56 +02006457 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02006458
Olivier Houchard7505f942018-08-21 18:10:44 +02006459 if (total > 0) {
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006460 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006461 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 +02006462 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006463 }
Olivier Houchardd846c262018-10-19 17:24:29 +02006464
Olivier Houchard7505f942018-08-21 18:10:44 +02006465 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006466 /* If we're waiting for flow control, and we got a shutr on the
6467 * connection, we will never be unlocked, so add an error on
6468 * the conn_stream.
6469 */
6470 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
6471 !b_data(&h2s->h2c->dbuf) &&
6472 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006473 TRACE_DEVEL("fctl with shutr, reporting error to app-layer", H2_EV_H2S_SEND|H2_EV_STRM_SEND|H2_EV_STRM_ERR, h2s->h2c->conn, h2s);
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006474 if (cs->flags & CS_FL_EOS)
6475 cs->flags |= CS_FL_ERROR;
6476 else
6477 cs->flags |= CS_FL_ERR_PENDING;
6478 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006479
Willy Tarreau5723f292020-01-10 15:16:57 +01006480 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
6481 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006482 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01006483 LIST_DEL_INIT(&h2s->list);
6484 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006485
Willy Tarreau7838a792019-08-12 18:42:03 +02006486 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006487 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02006488}
6489
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006490/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01006491static int h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006492{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01006493 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01006494 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006495 struct eb32_node *node;
6496 int fctl_cnt = 0;
6497 int send_cnt = 0;
6498 int tree_cnt = 0;
6499 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02006500 struct buffer *hmbuf, *tmbuf;
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006501 int ret = 0;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006502
6503 if (!h2c)
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006504 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006505
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006506 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006507 fctl_cnt++;
6508
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006509 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006510 send_cnt++;
6511
Willy Tarreau3af37712018-12-18 14:34:41 +01006512 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006513 node = eb32_first(&h2c->streams_by_id);
6514 while (node) {
6515 h2s = container_of(node, struct h2s, by_id);
6516 tree_cnt++;
6517 if (!h2s->cs)
6518 orph_cnt++;
6519 node = eb32_next(node);
6520 }
6521
Willy Tarreau60f62682019-05-26 11:32:27 +02006522 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02006523 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02006524 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau987c0632018-12-18 10:32:05 +01006525 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02006526 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
6527 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreauab2ec452019-08-30 07:07:08 +02006528 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau616ac812018-07-24 14:12:42 +02006529 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006530 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01006531 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
6532 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
6533 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02006534 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
6535 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
6536 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02006537 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
6538 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01006539
6540 if (h2s) {
Willy Tarreaued4464e2021-01-20 15:50:03 +01006541 chunk_appendf(msg, " last_h2s=%p .id=%d .st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
Willy Tarreauab2ec452019-08-30 07:07:08 +02006542 h2s, h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
Willy Tarreau987c0632018-12-18 10:32:05 +01006543 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
6544 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
6545 h2s->cs);
6546 if (h2s->cs)
Willy Tarreau98e40b92021-01-20 16:27:01 +01006547 chunk_appendf(msg, "(.flg=0x%08x .data=%p)",
Willy Tarreau987c0632018-12-18 10:32:05 +01006548 h2s->cs->flags, h2s->cs->data);
Willy Tarreau98e40b92021-01-20 16:27:01 +01006549
6550 chunk_appendf(&trash, " .subs=%p", h2s->subs);
6551 if (h2s->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01006552 chunk_appendf(&trash, "(ev=%d tl=%p", h2s->subs->events, h2s->subs->tasklet);
6553 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
6554 h2s->subs->tasklet->calls,
6555 h2s->subs->tasklet->context);
6556 if (h2s->subs->tasklet->calls >= 1000000)
6557 ret = 1;
6558 resolve_sym_name(&trash, NULL, h2s->subs->tasklet->process);
6559 chunk_appendf(&trash, ")");
Willy Tarreau98e40b92021-01-20 16:27:01 +01006560 }
Willy Tarreau987c0632018-12-18 10:32:05 +01006561 }
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006562 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006563}
Willy Tarreau62f52692017-10-08 23:01:42 +02006564
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006565/* Migrate the the connection to the current thread.
6566 * Return 0 if successful, non-zero otherwise.
6567 * Expected to be called with the old thread lock held.
6568 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02006569static int h2_takeover(struct connection *conn, int orig_tid)
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006570{
6571 struct h2c *h2c = conn->ctx;
Willy Tarreau617e80f2020-07-01 16:39:33 +02006572 struct task *task;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006573
6574 if (fd_takeover(conn->handle.fd, conn) != 0)
6575 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006576
6577 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
6578 /* We failed to takeover the xprt, even if the connection may
6579 * still be valid, flag it as error'd, as we have already
6580 * taken over the fd, and wake the tasklet, so that it will
6581 * destroy it.
6582 */
6583 conn->flags |= CO_FL_ERROR;
6584 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
6585 return -1;
6586 }
6587
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006588 if (h2c->wait_event.events)
6589 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
6590 h2c->wait_event.events, &h2c->wait_event);
6591 /* To let the tasklet know it should free itself, and do nothing else,
6592 * set its context to NULL.
6593 */
6594 h2c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02006595 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
Willy Tarreau617e80f2020-07-01 16:39:33 +02006596
6597 task = h2c->task;
6598 if (task) {
6599 task->context = NULL;
6600 h2c->task = NULL;
6601 __ha_barrier_store();
6602 task_kill(task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006603
6604 h2c->task = task_new(tid_bit);
6605 if (!h2c->task) {
6606 h2_release(h2c);
6607 return -1;
6608 }
6609 h2c->task->process = h2_timeout_task;
6610 h2c->task->context = h2c;
6611 }
6612 h2c->wait_event.tasklet = tasklet_new();
6613 if (!h2c->wait_event.tasklet) {
6614 h2_release(h2c);
6615 return -1;
6616 }
6617 h2c->wait_event.tasklet->process = h2_io_cb;
6618 h2c->wait_event.tasklet->context = h2c;
6619 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
6620 SUB_RETRY_RECV, &h2c->wait_event);
6621
6622 return 0;
6623}
6624
Willy Tarreau62f52692017-10-08 23:01:42 +02006625/*******************************************************/
6626/* functions below are dedicated to the config parsers */
6627/*******************************************************/
6628
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006629/* config parser for global "tune.h2.header-table-size" */
6630static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006631 const struct proxy *defpx, const char *file, int line,
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006632 char **err)
6633{
6634 if (too_many_args(1, args, err, NULL))
6635 return -1;
6636
6637 h2_settings_header_table_size = atoi(args[1]);
6638 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
6639 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
6640 return -1;
6641 }
6642 return 0;
6643}
Willy Tarreau62f52692017-10-08 23:01:42 +02006644
Willy Tarreaue6baec02017-07-27 11:45:11 +02006645/* config parser for global "tune.h2.initial-window-size" */
6646static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006647 const struct proxy *defpx, const char *file, int line,
Willy Tarreaue6baec02017-07-27 11:45:11 +02006648 char **err)
6649{
6650 if (too_many_args(1, args, err, NULL))
6651 return -1;
6652
6653 h2_settings_initial_window_size = atoi(args[1]);
6654 if (h2_settings_initial_window_size < 0) {
6655 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
6656 return -1;
6657 }
6658 return 0;
6659}
6660
Willy Tarreau5242ef82017-07-27 11:47:28 +02006661/* config parser for global "tune.h2.max-concurrent-streams" */
6662static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006663 const struct proxy *defpx, const char *file, int line,
Willy Tarreau5242ef82017-07-27 11:47:28 +02006664 char **err)
6665{
6666 if (too_many_args(1, args, err, NULL))
6667 return -1;
6668
6669 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01006670 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02006671 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
6672 return -1;
6673 }
6674 return 0;
6675}
6676
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006677/* config parser for global "tune.h2.max-frame-size" */
6678static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006679 const struct proxy *defpx, const char *file, int line,
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006680 char **err)
6681{
6682 if (too_many_args(1, args, err, NULL))
6683 return -1;
6684
6685 h2_settings_max_frame_size = atoi(args[1]);
6686 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
6687 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
6688 return -1;
6689 }
6690 return 0;
6691}
6692
Willy Tarreau62f52692017-10-08 23:01:42 +02006693
6694/****************************************/
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05006695/* MUX initialization and instantiation */
Willy Tarreau62f52692017-10-08 23:01:42 +02006696/***************************************/
6697
6698/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01006699static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02006700 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02006701 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02006702 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02006703 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02006704 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006705 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02006706 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01006707 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02006708 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01006709 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01006710 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01006711 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02006712 .shutr = h2_shutr,
6713 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02006714 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006715 .show_fd = h2_show_fd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006716 .takeover = h2_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01006717 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Willy Tarreau62f52692017-10-08 23:01:42 +02006718 .name = "H2",
6719};
6720
Christopher Faulet32f61c02018-04-10 14:33:41 +02006721static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02006722 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02006723
Willy Tarreau0108d902018-11-25 19:14:37 +01006724INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
6725
Willy Tarreau62f52692017-10-08 23:01:42 +02006726/* config keyword parsers */
6727static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006728 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02006729 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006730 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006731 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006732 { 0, NULL, NULL }
6733}};
6734
Willy Tarreau0108d902018-11-25 19:14:37 +01006735INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02006736
6737/* initialize internal structs after the config is parsed.
6738 * Returns zero on success, non-zero on error.
6739 */
6740static int init_h2()
6741{
6742 pool_head_hpack_tbl = create_pool("hpack_tbl",
6743 h2_settings_header_table_size,
6744 MEM_F_SHARED|MEM_F_EXACT);
Christopher Faulet52140992020-11-06 15:23:39 +01006745 if (!pool_head_hpack_tbl) {
6746 ha_alert("failed to allocate hpack_tbl memory pool\n");
6747 return (ERR_ALERT | ERR_FATAL);
6748 }
6749 return ERR_NONE;
Willy Tarreau2bdcc702020-05-19 11:31:11 +02006750}
6751
6752REGISTER_POST_CHECK(init_h2);