blob: 25cf98ebcb78013c575d6417f82ae373009ba641 [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
Christopher Fauletd0db4232021-01-22 11:46:30 +0100197
Amaury Denoyelle5fb48ea2020-12-11 17:53:04 +0100198#define H2_SF_TUNNEL_ABRT 0x00100000 // A tunnel attempt was aborted
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200199
Willy Tarreau18312642017-10-11 07:57:07 +0200200/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
Christopher Fauletfafd1b02020-11-03 18:25:52 +0100201 * it is being processed in the internal HTTP representation (HTX).
Willy Tarreau18312642017-10-11 07:57:07 +0200202 */
203struct h2s {
204 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100205 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200206 struct h2c *h2c;
Willy Tarreau18312642017-10-11 07:57:07 +0200207 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200208 int32_t id; /* stream ID */
209 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200210 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200211 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
212 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200213 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100214 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200215 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreauf96508a2020-01-10 11:12:48 +0100216 struct wait_event *subs; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200217 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau5723f292020-01-10 15:16:57 +0100218 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to send an RST after we failed to,
219 * in case there's no other subscription to do it */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100220
221 char upgrade_protocol[16]; /* rfc 8441: requested protocol on Extended CONNECT */
Willy Tarreau18312642017-10-11 07:57:07 +0200222};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200223
Willy Tarreauc6405142017-09-21 20:23:50 +0200224/* descriptor for an h2 frame header */
225struct h2_fh {
226 uint32_t len; /* length, host order, 24 bits */
227 uint32_t sid; /* stream id, host order, 31 bits */
228 uint8_t ft; /* frame type */
229 uint8_t ff; /* frame flags */
230};
231
Willy Tarreau12ae2122019-08-08 18:23:12 +0200232/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200233static void h2_trace(enum trace_level level, uint64_t mask, \
234 const struct trace_source *src,
235 const struct ist where, const struct ist func,
236 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200237
238/* The event representation is split like this :
239 * strm - application layer
240 * h2s - internal H2 stream
241 * h2c - internal H2 connection
242 * conn - external connection
243 *
244 */
245static const struct trace_event h2_trace_events[] = {
246#define H2_EV_H2C_NEW (1ULL << 0)
Willy Tarreau87951942019-08-30 07:34:36 +0200247 { .mask = H2_EV_H2C_NEW, .name = "h2c_new", .desc = "new H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200248#define H2_EV_H2C_RECV (1ULL << 1)
Willy Tarreau87951942019-08-30 07:34:36 +0200249 { .mask = H2_EV_H2C_RECV, .name = "h2c_recv", .desc = "Rx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200250#define H2_EV_H2C_SEND (1ULL << 2)
Willy Tarreau87951942019-08-30 07:34:36 +0200251 { .mask = H2_EV_H2C_SEND, .name = "h2c_send", .desc = "Tx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200252#define H2_EV_H2C_FCTL (1ULL << 3)
Willy Tarreau87951942019-08-30 07:34:36 +0200253 { .mask = H2_EV_H2C_FCTL, .name = "h2c_fctl", .desc = "H2 connection flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200254#define H2_EV_H2C_BLK (1ULL << 4)
Willy Tarreau87951942019-08-30 07:34:36 +0200255 { .mask = H2_EV_H2C_BLK, .name = "h2c_blk", .desc = "H2 connection blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200256#define H2_EV_H2C_WAKE (1ULL << 5)
Willy Tarreau87951942019-08-30 07:34:36 +0200257 { .mask = H2_EV_H2C_WAKE, .name = "h2c_wake", .desc = "H2 connection woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200258#define H2_EV_H2C_END (1ULL << 6)
Willy Tarreau87951942019-08-30 07:34:36 +0200259 { .mask = H2_EV_H2C_END, .name = "h2c_end", .desc = "H2 connection terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200260#define H2_EV_H2C_ERR (1ULL << 7)
Willy Tarreau87951942019-08-30 07:34:36 +0200261 { .mask = H2_EV_H2C_ERR, .name = "h2c_err", .desc = "error on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200262#define H2_EV_RX_FHDR (1ULL << 8)
Willy Tarreau87951942019-08-30 07:34:36 +0200263 { .mask = H2_EV_RX_FHDR, .name = "rx_fhdr", .desc = "H2 frame header received" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200264#define H2_EV_RX_FRAME (1ULL << 9)
Willy Tarreau87951942019-08-30 07:34:36 +0200265 { .mask = H2_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200266#define H2_EV_RX_EOI (1ULL << 10)
Willy Tarreau87951942019-08-30 07:34:36 +0200267 { .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 +0200268#define H2_EV_RX_PREFACE (1ULL << 11)
Willy Tarreau87951942019-08-30 07:34:36 +0200269 { .mask = H2_EV_RX_PREFACE, .name = "rx_preface", .desc = "receipt of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200270#define H2_EV_RX_DATA (1ULL << 12)
Willy Tarreau87951942019-08-30 07:34:36 +0200271 { .mask = H2_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200272#define H2_EV_RX_HDR (1ULL << 13)
Willy Tarreau87951942019-08-30 07:34:36 +0200273 { .mask = H2_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200274#define H2_EV_RX_PRIO (1ULL << 14)
Willy Tarreau87951942019-08-30 07:34:36 +0200275 { .mask = H2_EV_RX_PRIO, .name = "rx_prio", .desc = "receipt of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200276#define H2_EV_RX_RST (1ULL << 15)
Willy Tarreau87951942019-08-30 07:34:36 +0200277 { .mask = H2_EV_RX_RST, .name = "rx_rst", .desc = "receipt of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200278#define H2_EV_RX_SETTINGS (1ULL << 16)
Willy Tarreau87951942019-08-30 07:34:36 +0200279 { .mask = H2_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200280#define H2_EV_RX_PUSH (1ULL << 17)
Willy Tarreau87951942019-08-30 07:34:36 +0200281 { .mask = H2_EV_RX_PUSH, .name = "rx_push", .desc = "receipt of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200282#define H2_EV_RX_PING (1ULL << 18)
Willy Tarreau87951942019-08-30 07:34:36 +0200283 { .mask = H2_EV_RX_PING, .name = "rx_ping", .desc = "receipt of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200284#define H2_EV_RX_GOAWAY (1ULL << 19)
Willy Tarreau87951942019-08-30 07:34:36 +0200285 { .mask = H2_EV_RX_GOAWAY, .name = "rx_goaway", .desc = "receipt of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200286#define H2_EV_RX_WU (1ULL << 20)
Willy Tarreau87951942019-08-30 07:34:36 +0200287 { .mask = H2_EV_RX_WU, .name = "rx_wu", .desc = "receipt of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200288#define H2_EV_RX_CONT (1ULL << 21)
Willy Tarreau87951942019-08-30 07:34:36 +0200289 { .mask = H2_EV_RX_CONT, .name = "rx_cont", .desc = "receipt of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200290#define H2_EV_TX_FRAME (1ULL << 22)
Willy Tarreau87951942019-08-30 07:34:36 +0200291 { .mask = H2_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200292#define H2_EV_TX_EOI (1ULL << 23)
Willy Tarreau87951942019-08-30 07:34:36 +0200293 { .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 +0200294#define H2_EV_TX_PREFACE (1ULL << 24)
Willy Tarreau87951942019-08-30 07:34:36 +0200295 { .mask = H2_EV_TX_PREFACE, .name = "tx_preface", .desc = "transmission of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200296#define H2_EV_TX_DATA (1ULL << 25)
Willy Tarreau87951942019-08-30 07:34:36 +0200297 { .mask = H2_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200298#define H2_EV_TX_HDR (1ULL << 26)
Willy Tarreau87951942019-08-30 07:34:36 +0200299 { .mask = H2_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200300#define H2_EV_TX_PRIO (1ULL << 27)
Willy Tarreau87951942019-08-30 07:34:36 +0200301 { .mask = H2_EV_TX_PRIO, .name = "tx_prio", .desc = "transmission of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200302#define H2_EV_TX_RST (1ULL << 28)
Willy Tarreau87951942019-08-30 07:34:36 +0200303 { .mask = H2_EV_TX_RST, .name = "tx_rst", .desc = "transmission of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200304#define H2_EV_TX_SETTINGS (1ULL << 29)
Willy Tarreau87951942019-08-30 07:34:36 +0200305 { .mask = H2_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200306#define H2_EV_TX_PUSH (1ULL << 30)
Willy Tarreau87951942019-08-30 07:34:36 +0200307 { .mask = H2_EV_TX_PUSH, .name = "tx_push", .desc = "transmission of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200308#define H2_EV_TX_PING (1ULL << 31)
Willy Tarreau87951942019-08-30 07:34:36 +0200309 { .mask = H2_EV_TX_PING, .name = "tx_ping", .desc = "transmission of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200310#define H2_EV_TX_GOAWAY (1ULL << 32)
Willy Tarreau87951942019-08-30 07:34:36 +0200311 { .mask = H2_EV_TX_GOAWAY, .name = "tx_goaway", .desc = "transmission of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200312#define H2_EV_TX_WU (1ULL << 33)
Willy Tarreau87951942019-08-30 07:34:36 +0200313 { .mask = H2_EV_TX_WU, .name = "tx_wu", .desc = "transmission of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200314#define H2_EV_TX_CONT (1ULL << 34)
Willy Tarreau87951942019-08-30 07:34:36 +0200315 { .mask = H2_EV_TX_CONT, .name = "tx_cont", .desc = "transmission of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200316#define H2_EV_H2S_NEW (1ULL << 35)
Willy Tarreau87951942019-08-30 07:34:36 +0200317 { .mask = H2_EV_H2S_NEW, .name = "h2s_new", .desc = "new H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200318#define H2_EV_H2S_RECV (1ULL << 36)
Willy Tarreau87951942019-08-30 07:34:36 +0200319 { .mask = H2_EV_H2S_RECV, .name = "h2s_recv", .desc = "Rx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200320#define H2_EV_H2S_SEND (1ULL << 37)
Willy Tarreau87951942019-08-30 07:34:36 +0200321 { .mask = H2_EV_H2S_SEND, .name = "h2s_send", .desc = "Tx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200322#define H2_EV_H2S_FCTL (1ULL << 38)
Willy Tarreau87951942019-08-30 07:34:36 +0200323 { .mask = H2_EV_H2S_FCTL, .name = "h2s_fctl", .desc = "H2 stream flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200324#define H2_EV_H2S_BLK (1ULL << 39)
Willy Tarreau87951942019-08-30 07:34:36 +0200325 { .mask = H2_EV_H2S_BLK, .name = "h2s_blk", .desc = "H2 stream blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200326#define H2_EV_H2S_WAKE (1ULL << 40)
Willy Tarreau87951942019-08-30 07:34:36 +0200327 { .mask = H2_EV_H2S_WAKE, .name = "h2s_wake", .desc = "H2 stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200328#define H2_EV_H2S_END (1ULL << 41)
Willy Tarreau87951942019-08-30 07:34:36 +0200329 { .mask = H2_EV_H2S_END, .name = "h2s_end", .desc = "H2 stream terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200330#define H2_EV_H2S_ERR (1ULL << 42)
Willy Tarreau87951942019-08-30 07:34:36 +0200331 { .mask = H2_EV_H2S_ERR, .name = "h2s_err", .desc = "error on H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200332#define H2_EV_STRM_NEW (1ULL << 43)
Willy Tarreau87951942019-08-30 07:34:36 +0200333 { .mask = H2_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200334#define H2_EV_STRM_RECV (1ULL << 44)
Willy Tarreau87951942019-08-30 07:34:36 +0200335 { .mask = H2_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200336#define H2_EV_STRM_SEND (1ULL << 45)
Willy Tarreau87951942019-08-30 07:34:36 +0200337 { .mask = H2_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200338#define H2_EV_STRM_FULL (1ULL << 46)
Willy Tarreau87951942019-08-30 07:34:36 +0200339 { .mask = H2_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200340#define H2_EV_STRM_WAKE (1ULL << 47)
Willy Tarreau87951942019-08-30 07:34:36 +0200341 { .mask = H2_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200342#define H2_EV_STRM_SHUT (1ULL << 48)
Willy Tarreau87951942019-08-30 07:34:36 +0200343 { .mask = H2_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200344#define H2_EV_STRM_END (1ULL << 49)
Willy Tarreau87951942019-08-30 07:34:36 +0200345 { .mask = H2_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200346#define H2_EV_STRM_ERR (1ULL << 50)
Willy Tarreau87951942019-08-30 07:34:36 +0200347 { .mask = H2_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200348#define H2_EV_PROTO_ERR (1ULL << 51)
Willy Tarreau87951942019-08-30 07:34:36 +0200349 { .mask = H2_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200350 { }
351};
352
353static const struct name_desc h2_trace_lockon_args[4] = {
354 /* arg1 */ { /* already used by the connection */ },
355 /* arg2 */ { .name="h2s", .desc="H2 stream" },
356 /* arg3 */ { },
357 /* arg4 */ { }
358};
359
360static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200361#define H2_VERB_CLEAN 1
362 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
363#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200364 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200365#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200366 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200367#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200368 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200369#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200370 { .name="complete", .desc="add full data dump when available" },
371 { /* end */ }
372};
373
374static struct trace_source trace_h2 = {
375 .name = IST("h2"),
376 .desc = "HTTP/2 multiplexer",
377 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200378 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200379 .known_events = h2_trace_events,
380 .lockon_args = h2_trace_lockon_args,
381 .decoding = h2_trace_decoding,
382 .report_events = ~0, // report everything by default
383};
384
385#define TRACE_SOURCE &trace_h2
386INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
387
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100388/* h2 stats module */
389enum {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100390 H2_ST_HEADERS_RCVD,
391 H2_ST_DATA_RCVD,
392 H2_ST_SETTINGS_RCVD,
393 H2_ST_RST_STREAM_RCVD,
394 H2_ST_GOAWAY_RCVD,
395
Amaury Denoyellea8879232020-10-27 17:16:03 +0100396 H2_ST_CONN_PROTO_ERR,
397 H2_ST_STRM_PROTO_ERR,
398 H2_ST_RST_STREAM_RESP,
399 H2_ST_GOAWAY_RESP,
400
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100401 H2_ST_OPEN_CONN,
402 H2_ST_OPEN_STREAM,
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100403 H2_ST_TOTAL_CONN,
404 H2_ST_TOTAL_STREAM,
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100405
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100406 H2_STATS_COUNT /* must be the last member of the enum */
407};
408
409static struct name_desc h2_stats[] = {
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100410 [H2_ST_HEADERS_RCVD] = { .name = "h2_headers_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100411 .desc = "Total number of received HEADERS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100412 [H2_ST_DATA_RCVD] = { .name = "h2_data_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100413 .desc = "Total number of received DATA frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100414 [H2_ST_SETTINGS_RCVD] = { .name = "h2_settings_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100415 .desc = "Total number of received SETTINGS frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100416 [H2_ST_RST_STREAM_RCVD] = { .name = "h2_rst_stream_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100417 .desc = "Total number of received RST_STREAM frames" },
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100418 [H2_ST_GOAWAY_RCVD] = { .name = "h2_goaway_rcvd",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100419 .desc = "Total number of received GOAWAY frames" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100420
421 [H2_ST_CONN_PROTO_ERR] = { .name = "h2_detected_conn_protocol_errors",
422 .desc = "Total number of connection protocol errors" },
423 [H2_ST_STRM_PROTO_ERR] = { .name = "h2_detected_strm_protocol_errors",
424 .desc = "Total number of stream protocol errors" },
425 [H2_ST_RST_STREAM_RESP] = { .name = "h2_rst_stream_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100426 .desc = "Total number of RST_STREAM sent on detected error" },
Amaury Denoyellea8879232020-10-27 17:16:03 +0100427 [H2_ST_GOAWAY_RESP] = { .name = "h2_goaway_resp",
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100428 .desc = "Total number of GOAWAY sent on detected error" },
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100429
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100430 [H2_ST_OPEN_CONN] = { .name = "h2_open_connections",
431 .desc = "Count of currently open connections" },
432 [H2_ST_OPEN_STREAM] = { .name = "h2_backend_open_streams",
433 .desc = "Count of currently open streams" },
434 [H2_ST_TOTAL_CONN] = { .name = "h2_open_connections",
435 .desc = "Total number of connections" },
436 [H2_ST_TOTAL_STREAM] = { .name = "h2_backend_open_streams",
437 .desc = "Total number of streams" },
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100438};
439
440static struct h2_counters {
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100441 long long headers_rcvd; /* total number of HEADERS frame received */
442 long long data_rcvd; /* total number of DATA frame received */
443 long long settings_rcvd; /* total number of SETTINGS frame received */
444 long long rst_stream_rcvd; /* total number of RST_STREAM frame received */
445 long long goaway_rcvd; /* total number of GOAWAY frame received */
Amaury Denoyellea8879232020-10-27 17:16:03 +0100446
447 long long conn_proto_err; /* total number of protocol errors detected */
448 long long strm_proto_err; /* total number of protocol errors detected */
Amaury Denoyelle2ac34d92020-11-03 15:04:44 +0100449 long long rst_stream_resp; /* total number of RST_STREAM frame sent on error */
450 long long goaway_resp; /* total number of GOAWAY frame sent on error */
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100451
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100452 long long open_conns; /* count of currently open connections */
453 long long open_streams; /* count of currently open streams */
454 long long total_conns; /* total number of connections */
455 long long total_streams; /* total number of streams */
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100456} h2_counters;
457
458static void h2_fill_stats(void *data, struct field *stats)
459{
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +0100460 struct h2_counters *counters = data;
461
462 stats[H2_ST_HEADERS_RCVD] = mkf_u64(FN_COUNTER, counters->headers_rcvd);
463 stats[H2_ST_DATA_RCVD] = mkf_u64(FN_COUNTER, counters->data_rcvd);
464 stats[H2_ST_SETTINGS_RCVD] = mkf_u64(FN_COUNTER, counters->settings_rcvd);
465 stats[H2_ST_RST_STREAM_RCVD] = mkf_u64(FN_COUNTER, counters->rst_stream_rcvd);
466 stats[H2_ST_GOAWAY_RCVD] = mkf_u64(FN_COUNTER, counters->goaway_rcvd);
Amaury Denoyellea8879232020-10-27 17:16:03 +0100467
468 stats[H2_ST_CONN_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->conn_proto_err);
469 stats[H2_ST_STRM_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->strm_proto_err);
470 stats[H2_ST_RST_STREAM_RESP] = mkf_u64(FN_COUNTER, counters->rst_stream_resp);
471 stats[H2_ST_GOAWAY_RESP] = mkf_u64(FN_COUNTER, counters->goaway_resp);
Amaury Denoyelle66942c12020-10-27 17:16:04 +0100472
Amaury Denoyellee7b891f2020-11-03 15:04:45 +0100473 stats[H2_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
474 stats[H2_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
475 stats[H2_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
476 stats[H2_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Amaury Denoyelle3238b3f2020-10-27 17:16:00 +0100477}
478
479static struct stats_module h2_stats_module = {
480 .name = "h2",
481 .fill_stats = h2_fill_stats,
482 .stats = h2_stats,
483 .stats_count = H2_STATS_COUNT,
484 .counters = &h2_counters,
485 .counters_size = sizeof(h2_counters),
486 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
487 .clearable = 1,
488};
489
490INITCALL1(STG_REGISTER, stats_register_module, &h2_stats_module);
491
Willy Tarreau8ceae722018-11-26 11:58:30 +0100492/* the h2c connection pool */
493DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
494
495/* the h2s stream pool */
496DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
497
Willy Tarreaudc572362018-12-12 08:08:05 +0100498/* The default connection window size is 65535, it may only be enlarged using
499 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
500 * we'll pretend we already received the difference between the two to send
501 * an equivalent window update to enlarge it to 2G-1.
502 */
503#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
504
Willy Tarreau455d5682019-05-24 19:42:18 +0200505/* maximum amount of data we're OK with re-aligning for buffer optimizations */
506#define MAX_DATA_REALIGN 1024
507
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200508/* a few settings from the global section */
509static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200510static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100511static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100512static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200513
Willy Tarreau2a856182017-05-16 15:20:39 +0200514/* a dmumy closed stream */
515static const struct h2s *h2_closed_stream = &(const struct h2s){
516 .cs = NULL,
517 .h2c = NULL,
518 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100519 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100520 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200521 .id = 0,
522};
523
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100524/* a dmumy closed stream returning a PROTOCOL_ERROR error */
525static const struct h2s *h2_error_stream = &(const struct h2s){
526 .cs = NULL,
527 .h2c = NULL,
528 .st = H2_SS_CLOSED,
529 .errcode = H2_ERR_PROTOCOL_ERROR,
530 .flags = 0,
531 .id = 0,
532};
533
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100534/* a dmumy closed stream returning a REFUSED_STREAM error */
535static const struct h2s *h2_refused_stream = &(const struct h2s){
536 .cs = NULL,
537 .h2c = NULL,
538 .st = H2_SS_CLOSED,
539 .errcode = H2_ERR_REFUSED_STREAM,
540 .flags = 0,
541 .id = 0,
542};
543
Willy Tarreau2a856182017-05-16 15:20:39 +0200544/* and a dummy idle stream for use with any unannounced stream */
545static const struct h2s *h2_idle_stream = &(const struct h2s){
546 .cs = NULL,
547 .h2c = NULL,
548 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100549 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200550 .id = 0,
551};
552
Olivier Houchard9f6af332018-05-25 14:04:04 +0200553static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200554static int h2_send(struct h2c *h2c);
555static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200556static int h2_process(struct h2c *h2c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100557/* h2_io_cb is exported to see it resolved in "show fd" */
558struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100559static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Amaury Denoyelle74162742020-12-11 17:53:05 +0100560static 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 +0100561static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200562static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100563static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100564static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200565
Willy Tarreauab2ec452019-08-30 07:07:08 +0200566/* returns a h2c state as an abbreviated 3-letter string, or "???" if unknown */
567static inline const char *h2c_st_to_str(enum h2_cs st)
568{
569 switch (st) {
570 case H2_CS_PREFACE: return "PRF";
571 case H2_CS_SETTINGS1: return "STG";
572 case H2_CS_FRAME_H: return "FRH";
573 case H2_CS_FRAME_P: return "FRP";
574 case H2_CS_FRAME_A: return "FRA";
575 case H2_CS_FRAME_E: return "FRE";
576 case H2_CS_ERROR: return "ERR";
577 case H2_CS_ERROR2: return "ER2";
578 default: return "???";
579 }
580}
581
582/* returns a h2s state as an abbreviated 3-letter string, or "???" if unknown */
583static inline const char *h2s_st_to_str(enum h2_ss st)
584{
585 switch (st) {
586 case H2_SS_IDLE: return "IDL"; // idle
587 case H2_SS_RLOC: return "RSL"; // reserved local
588 case H2_SS_RREM: return "RSR"; // reserved remote
589 case H2_SS_OPEN: return "OPN"; // open
590 case H2_SS_HREM: return "HCR"; // half-closed remote
591 case H2_SS_HLOC: return "HCL"; // half-closed local
592 case H2_SS_ERROR : return "ERR"; // error
593 case H2_SS_CLOSED: return "CLO"; // closed
594 default: return "???";
595 }
596}
597
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200598/* the H2 traces always expect that arg1, if non-null, is of type connection
599 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
600 * that arg3, if non-null, is either of type htx for tx headers, or of type
601 * buffer for everything else.
602 */
603static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
604 const struct ist where, const struct ist func,
605 const void *a1, const void *a2, const void *a3, const void *a4)
606{
607 const struct connection *conn = a1;
608 const struct h2c *h2c = conn ? conn->ctx : NULL;
609 const struct h2s *h2s = a2;
610 const struct buffer *buf = a3;
611 const struct htx *htx;
612 int pos;
613
614 if (!h2c) // nothing to add
615 return;
616
Willy Tarreau17104d42019-08-30 07:12:55 +0200617 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau73db4342019-09-25 07:28:44 +0200618 chunk_appendf(&trace_buf, " : h2c=%p(%c,%s)", h2c, conn_is_back(conn) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
619
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100620 if (h2c->errcode)
621 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2c->errcode), h2c->errcode);
622
Willy Tarreau73db4342019-09-25 07:28:44 +0200623 if (h2c->dsi >= 0 &&
624 (mask & (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) == (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) {
Willy Tarreau8520d872020-09-18 07:39:29 +0200625 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 +0200626 }
627
628 if (h2s) {
629 if (h2s->id <= 0)
630 chunk_appendf(&trace_buf, " dsi=%d", h2c->dsi);
631 chunk_appendf(&trace_buf, " h2s=%p(%d,%s)", h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100632 if (h2s->id && h2s->errcode)
633 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2s->errcode), h2s->errcode);
Willy Tarreau73db4342019-09-25 07:28:44 +0200634 }
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200635 }
636
637 /* Let's dump decoded requests and responses right after parsing. They
638 * are traced at level USER with a few recognizable flags.
639 */
640 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
641 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
642 htx = htxbuf(buf); // recv req/res
643 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
644 htx = a3; // send req/res
645 else
646 htx = NULL;
647
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200648 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200649 const struct htx_blk *blk = htx_get_blk(htx, pos);
650 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
651 enum htx_blk_type type = htx_get_blk_type(blk);
652
653 if (type == HTX_BLK_REQ_SL)
654 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200655 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200656 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
657 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
658 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
659 else if (type == HTX_BLK_RES_SL)
660 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200661 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200662 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
663 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
664 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
665 }
666}
667
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200668
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100669/* Detect a pending read0 for a H2 connection. It happens if a read0 was
670 * already reported on a previous xprt->rcvbuf() AND a frame parser failed
671 * to parse pending data, confirming no more progress is possible because
672 * we're facing a truncated frame. The function returns 1 to report a read0
673 * or 0 otherwise.
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200674 */
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100675static inline int h2c_read0_pending(struct h2c *h2c)
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200676{
Willy Tarreau3d4631f2021-01-20 10:53:13 +0100677 return !!(h2c->flags & H2_CF_END_REACHED);
Christopher Fauletaade4ed2020-10-08 15:38:41 +0200678}
679
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200680/* returns true if the connection is allowed to expire, false otherwise. A
681 * connection may expire when:
682 * - it has no stream
683 * - it has data in the mux buffer
684 * - it has streams in the blocked list
685 * - it has streams in the fctl list
686 * - it has streams in the send list
687 * Otherwise it means some streams are waiting in the data layer and it should
688 * not expire.
689 */
690static inline int h2c_may_expire(const struct h2c *h2c)
691{
692 return eb_is_empty(&h2c->streams_by_id) ||
693 br_data(h2c->mbuf) ||
694 !LIST_ISEMPTY(&h2c->blocked_list) ||
695 !LIST_ISEMPTY(&h2c->fctl_list) ||
Willy Tarreaud9464162020-01-10 18:25:07 +0100696 !LIST_ISEMPTY(&h2c->send_list);
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200697}
698
Olivier Houchard7a977432019-03-21 15:47:13 +0100699static __inline int
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200700h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100701{
702 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
703 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
704 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
705 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200706 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100707 (conn_xprt_read0_pending(h2c->conn) ||
708 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
709 return 1;
710
711 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100712}
713
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200714/*****************************************************/
715/* functions below are for dynamic buffer management */
716/*****************************************************/
717
Willy Tarreau315d8072017-12-10 22:17:57 +0100718/* indicates whether or not the we may call the h2_recv() function to attempt
719 * to receive data into the buffer and/or demux pending data. The condition is
720 * a bit complex due to some API limits for now. The rules are the following :
721 * - if an error or a shutdown was detected on the connection and the buffer
722 * is empty, we must not attempt to receive
723 * - if the demux buf failed to be allocated, we must not try to receive and
724 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100725 * - if no flag indicates a blocking condition, we may attempt to receive,
726 * regardless of whether the demux buffer is full or not, so that only
727 * de demux part decides whether or not to block. This is needed because
728 * the connection API indeed prevents us from re-enabling receipt that is
729 * already enabled in a polled state, so we must always immediately stop
730 * as soon as the demux can't proceed so as never to hit an end of read
731 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100732 * - otherwise must may not attempt
733 */
734static inline int h2_recv_allowed(const struct h2c *h2c)
735{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200736 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100737 (h2c->st0 >= H2_CS_ERROR ||
738 h2c->conn->flags & CO_FL_ERROR ||
739 conn_xprt_read0_pending(h2c->conn)))
740 return 0;
741
742 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100743 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100744 return 1;
745
746 return 0;
747}
748
Willy Tarreau47b515a2018-12-21 16:09:41 +0100749/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200750static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100751{
752 if (!h2_recv_allowed(h2c))
753 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200754 if ((!consider_buffer || !b_data(&h2c->dbuf))
755 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100756 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200757 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100758}
759
760
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100761/* returns true if the front connection has too many conn_streams attached */
762static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200763{
Willy Tarreaua8754662018-12-23 20:43:58 +0100764 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200765}
766
Willy Tarreau44e973f2018-03-01 17:49:30 +0100767/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
768 * flags are used to figure what buffer was requested. It returns 1 if the
769 * allocation succeeds, in which case the connection is woken up, or 0 if it's
770 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200771 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100772static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200773{
774 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100775 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200776
Willy Tarreau44e973f2018-03-01 17:49:30 +0100777 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200778 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200779 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200780 return 1;
781 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200782
Willy Tarreau51330962019-05-26 09:38:07 +0200783 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100784 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200785
786 if (h2c->flags & H2_CF_DEM_MROOM) {
787 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200788 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200789 }
Willy Tarreau14398122017-09-22 14:26:04 +0200790 return 1;
791 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100792
793 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
794 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200795 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100796 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200797 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100798 return 1;
799 }
800
Willy Tarreau14398122017-09-22 14:26:04 +0200801 return 0;
802}
803
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200804static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200805{
806 struct buffer *buf = NULL;
807
Willy Tarreau21046592020-02-26 10:39:36 +0100808 if (likely(!MT_LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100809 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
810 h2c->buf_wait.target = h2c;
811 h2c->buf_wait.wakeup_cb = h2_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200812 MT_LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200813 }
814 return buf;
815}
816
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200817static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200818{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200819 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100820 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200821 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200822 }
823}
824
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200825static inline void h2_release_mbuf(struct h2c *h2c)
826{
827 struct buffer *buf;
828 unsigned int count = 0;
829
830 while (b_size(buf = br_head_pick(h2c->mbuf))) {
831 b_free(buf);
832 count++;
833 }
834 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200835 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200836}
837
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100838/* returns the number of allocatable outgoing streams for the connection taking
839 * the last_sid and the reserved ones into account.
840 */
841static inline int h2_streams_left(const struct h2c *h2c)
842{
843 int ret;
844
845 /* consider the number of outgoing streams we're allowed to create before
846 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
847 * nb_reserved is the number of streams which don't yet have an ID.
848 */
849 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
850 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
851 if (ret < 0)
852 ret = 0;
853 return ret;
854}
855
Willy Tarreau00f18a32019-01-26 12:19:01 +0100856/* returns the number of streams in use on a connection to figure if it's
857 * idle or not. We check nb_cs and not nb_streams as the caller will want
858 * to know if it was the last one after a detach().
859 */
860static int h2_used_streams(struct connection *conn)
861{
862 struct h2c *h2c = conn->ctx;
863
864 return h2c->nb_cs;
865}
866
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100867/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100868static int h2_avail_streams(struct connection *conn)
869{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100870 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100871 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100872 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100873
Willy Tarreau6afec462019-01-28 06:40:19 +0100874 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
875 * streams on the connection.
876 */
877 if (h2c->last_sid >= 0)
878 return 0;
879
Willy Tarreauc61966f2019-10-31 15:10:03 +0100880 if (h2c->st0 >= H2_CS_ERROR)
881 return 0;
882
Willy Tarreau86949782019-01-31 10:42:05 +0100883 /* note: may be negative if a SETTINGS frame changes the limit */
884 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100885
886 /* we must also consider the limit imposed by stream IDs */
887 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100888 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100889 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100890 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
891 ret1 = MIN(ret1, ret2);
892 }
893 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100894}
895
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200896
Willy Tarreau62f52692017-10-08 23:01:42 +0200897/*****************************************************************/
898/* functions below are dedicated to the mux setup and management */
899/*****************************************************************/
900
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200901/* Initialize the mux once it's attached. For outgoing connections, the context
902 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200903 * connections from the fact that the context is still NULL (even during mux
904 * upgrades). <input> is always used as Input buffer and may contain data. It is
905 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200906 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200907static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
908 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200909{
910 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100911 struct task *t = NULL;
Christopher Fauletf81ef032019-10-04 15:19:43 +0200912 void *conn_ctx = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200913
Christopher Fauletf81ef032019-10-04 15:19:43 +0200914 TRACE_ENTER(H2_EV_H2C_NEW);
Willy Tarreau7838a792019-08-12 18:42:03 +0200915
Willy Tarreaubafbe012017-11-24 17:34:44 +0100916 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200917 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200918 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200919
Christopher Faulete9b70722019-04-08 10:46:02 +0200920 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200921 h2c->flags = H2_CF_IS_BACK;
922 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
923 if (tick_isset(prx->timeout.serverfin))
924 h2c->shut_timeout = prx->timeout.serverfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100925
926 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
927 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200928 } else {
929 h2c->flags = H2_CF_NONE;
930 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
931 if (tick_isset(prx->timeout.clientfin))
932 h2c->shut_timeout = prx->timeout.clientfin;
Amaury Denoyellec92697d2020-10-27 17:16:01 +0100933
934 h2c->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
935 &h2_stats_module);
Willy Tarreau01b44822018-10-03 14:26:37 +0200936 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100937
Willy Tarreau0b37d652018-10-03 10:33:02 +0200938 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100939 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100940 if (tick_isset(h2c->timeout)) {
941 t = task_new(tid_bit);
942 if (!t)
943 goto fail;
944
945 h2c->task = t;
946 t->process = h2_timeout_task;
947 t->context = h2c;
948 t->expire = tick_add(now_ms, h2c->timeout);
949 }
Willy Tarreauea392822017-10-31 10:02:25 +0100950
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200951 h2c->wait_event.tasklet = tasklet_new();
952 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200953 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200954 h2c->wait_event.tasklet->process = h2_io_cb;
955 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100956 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200957
Willy Tarreau2bdcc702020-05-19 11:31:11 +0200958 h2c->ddht = hpack_dht_alloc();
Willy Tarreau32218eb2017-09-22 08:07:25 +0200959 if (!h2c->ddht)
960 goto fail;
961
962 /* Initialise the context. */
963 h2c->st0 = H2_CS_PREFACE;
964 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100965 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200966 h2c->max_id = -1;
967 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100968 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200969 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100970 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200971 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100972 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100973 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200974
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200975 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200976 h2c->dsi = -1;
977 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100978
Willy Tarreau32218eb2017-09-22 08:07:25 +0200979 h2c->last_sid = -1;
980
Willy Tarreau51330962019-05-26 09:38:07 +0200981 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200982 h2c->miw = 65535; /* mux initial window size */
983 h2c->mws = 65535; /* mux window size */
984 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200985 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200986 LIST_INIT(&h2c->send_list);
987 LIST_INIT(&h2c->fctl_list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +0200988 LIST_INIT(&h2c->blocked_list);
Willy Tarreau21046592020-02-26 10:39:36 +0100989 MT_LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200990
Christopher Fauletf81ef032019-10-04 15:19:43 +0200991 conn->ctx = h2c;
992
Willy Tarreau3f133572017-10-31 19:21:06 +0100993 if (t)
994 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100995
Willy Tarreau01b44822018-10-03 14:26:37 +0200996 if (h2c->flags & H2_CF_IS_BACK) {
997 /* FIXME: this is temporary, for outgoing connections we need
998 * to immediately allocate a stream until the code is modified
999 * so that the caller calls ->attach(). For now the outgoing cs
Christopher Fauletf81ef032019-10-04 15:19:43 +02001000 * is stored as conn->ctx by the caller and saved in conn_ctx.
Willy Tarreau01b44822018-10-03 14:26:37 +02001001 */
1002 struct h2s *h2s;
1003
Christopher Fauletf81ef032019-10-04 15:19:43 +02001004 h2s = h2c_bck_stream_new(h2c, conn_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +02001005 if (!h2s)
1006 goto fail_stream;
1007 }
1008
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001009 HA_ATOMIC_ADD(&h2c->px_counters->open_conns, 1);
Amaury Denoyellee7b891f2020-11-03 15:04:45 +01001010 HA_ATOMIC_ADD(&h2c->px_counters->total_conns, 1);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001011
Willy Tarreau0f383582018-10-03 14:22:21 +02001012 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02001013 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001014 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001015 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +02001016 fail_stream:
1017 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +02001018 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +02001019 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001020 if (h2c->wait_event.tasklet)
1021 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001022 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +02001023 fail_no_h2c:
Christopher Fauletf81ef032019-10-04 15:19:43 +02001024 conn->ctx = conn_ctx; /* restore saved ctx */
1025 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001026 return -1;
1027}
1028
Willy Tarreau751f2d02018-10-05 09:35:00 +02001029/* returns the next allocatable outgoing stream ID for the H2 connection, or
1030 * -1 if no more is allocatable.
1031 */
1032static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
1033{
1034 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +01001035
1036 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +02001037 id = -1;
1038 return id;
1039}
1040
Willy Tarreau2373acc2017-10-12 17:35:14 +02001041/* returns the stream associated with id <id> or NULL if not found */
1042static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
1043{
1044 struct eb32_node *node;
1045
Willy Tarreau751f2d02018-10-05 09:35:00 +02001046 if (id == 0)
1047 return (struct h2s *)h2_closed_stream;
1048
Willy Tarreau2a856182017-05-16 15:20:39 +02001049 if (id > h2c->max_id)
1050 return (struct h2s *)h2_idle_stream;
1051
Willy Tarreau2373acc2017-10-12 17:35:14 +02001052 node = eb32_lookup(&h2c->streams_by_id, id);
1053 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +02001054 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +02001055
1056 return container_of(node, struct h2s, by_id);
1057}
1058
Christopher Faulet73c12072019-04-08 11:23:22 +02001059/* release function. This one should be called to free all resources allocated
1060 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +02001061 */
Christopher Faulet73c12072019-04-08 11:23:22 +02001062static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02001063{
William Dauchy477757c2020-08-07 22:19:23 +02001064 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001065
Willy Tarreau7838a792019-08-12 18:42:03 +02001066 TRACE_ENTER(H2_EV_H2C_END);
1067
Willy Tarreau32218eb2017-09-22 08:07:25 +02001068 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +02001069 /* The connection must be aattached to this mux to be released */
1070 if (h2c->conn && h2c->conn->ctx == h2c)
1071 conn = h2c->conn;
1072
Willy Tarreau7838a792019-08-12 18:42:03 +02001073 TRACE_DEVEL("freeing h2c", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001074 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +02001075
Willy Tarreau21046592020-02-26 10:39:36 +01001076 if (MT_LIST_ADDED(&h2c->buf_wait.list))
1077 MT_LIST_DEL(&h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +02001078
Willy Tarreau44e973f2018-03-01 17:49:30 +01001079 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +02001080 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +01001081
Willy Tarreauea392822017-10-31 10:02:25 +01001082 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +02001083 h2c->task->context = NULL;
1084 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +01001085 h2c->task = NULL;
1086 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001087 if (h2c->wait_event.tasklet)
1088 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet21d849f2019-09-18 11:07:20 +02001089 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001090 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet21d849f2019-09-18 11:07:20 +02001091 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +01001092
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001093 HA_ATOMIC_SUB(&h2c->px_counters->open_conns, 1);
1094
Willy Tarreaubafbe012017-11-24 17:34:44 +01001095 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001096 }
1097
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001098 if (conn) {
1099 conn->mux = NULL;
1100 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02001101 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +02001102
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001103 conn_stop_tracking(conn);
1104 conn_full_close(conn);
1105 if (conn->destroy_cb)
1106 conn->destroy_cb(conn);
1107 conn_free(conn);
1108 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001109
1110 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +02001111}
1112
1113
Willy Tarreau71681172017-10-23 14:39:06 +02001114/******************************************************/
1115/* functions below are for the H2 protocol processing */
1116/******************************************************/
1117
1118/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +01001119static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +02001120{
1121 return h2s ? h2s->id : 0;
1122}
1123
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001124/* returns the sum of the stream's own window size and the mux's initial
1125 * window, which together form the stream's effective window size.
1126 */
1127static inline int h2s_mws(const struct h2s *h2s)
1128{
1129 return h2s->sws + h2s->h2c->miw;
1130}
1131
Willy Tarreau5b5e6872017-09-25 16:17:25 +02001132/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001133static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +02001134{
1135 if (h2c->msi < 0)
1136 return 0;
1137
1138 if (h2c->msi == h2s_id(h2s))
1139 return 0;
1140
1141 return 1;
1142}
1143
Willy Tarreau741d6df2017-10-17 08:00:59 +02001144/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +01001145static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +02001146{
Willy Tarreau022e5e52020-09-10 09:33:15 +02001147 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn, 0, 0, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +02001148 h2c->errcode = err;
1149 h2c->st0 = H2_CS_ERROR;
1150}
1151
Willy Tarreau175cebb2019-01-24 10:02:24 +01001152/* marks an error on the stream. It may also update an already closed stream
1153 * (e.g. to report an error after an RST was received).
1154 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001155static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +02001156{
Willy Tarreau175cebb2019-01-24 10:02:24 +01001157 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001158 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s, 0, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +02001159 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +01001160 if (h2s->st < H2_SS_ERROR)
1161 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +01001162 if (h2s->cs)
1163 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001164 }
1165}
1166
Willy Tarreau7e094452018-12-19 18:08:52 +01001167/* attempt to notify the data layer of recv availability */
1168static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1169{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001170 if (h2s->subs && h2s->subs->events & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001171 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001172 tasklet_wakeup(h2s->subs->tasklet);
1173 h2s->subs->events &= ~SUB_RETRY_RECV;
1174 if (!h2s->subs->events)
1175 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001176 }
1177}
1178
1179/* attempt to notify the data layer of send availability */
1180static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1181{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001182 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001183 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01001184 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01001185 tasklet_wakeup(h2s->subs->tasklet);
1186 h2s->subs->events &= ~SUB_RETRY_SEND;
1187 if (!h2s->subs->events)
1188 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001189 }
Willy Tarreau5723f292020-01-10 15:16:57 +01001190 else if (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) {
1191 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
1192 tasklet_wakeup(h2s->shut_tl);
1193 }
Willy Tarreau7e094452018-12-19 18:08:52 +01001194}
1195
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001196/* alerts the data layer, trying to wake it up by all means, following
1197 * this sequence :
1198 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1199 * - if its subscribed to send, then it's woken up for send
1200 * - if it was subscribed to neither, its ->wake() callback is called
1201 * It is safe to call this function with a closed stream which doesn't have a
1202 * conn_stream anymore.
1203 */
1204static void __maybe_unused h2s_alert(struct h2s *h2s)
1205{
Willy Tarreau7838a792019-08-12 18:42:03 +02001206 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1207
Willy Tarreauf96508a2020-01-10 11:12:48 +01001208 if (h2s->subs ||
Willy Tarreau5723f292020-01-10 15:16:57 +01001209 (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW))) {
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001210 h2s_notify_recv(h2s);
1211 h2s_notify_send(h2s);
1212 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001213 else if (h2s->cs && h2s->cs->data_cb->wake != NULL) {
1214 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001215 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau7838a792019-08-12 18:42:03 +02001216 }
1217
1218 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001219}
1220
Willy Tarreaue4820742017-07-27 13:37:23 +02001221/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001222static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001223{
1224 uint8_t *out = frame;
1225
1226 *out = len >> 16;
1227 write_n16(out + 1, len);
1228}
1229
Willy Tarreau54c15062017-10-10 17:10:03 +02001230/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1231 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1232 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001233 * available in the buffer's input prior to calling this function. The buffer
1234 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001235 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001236static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001237 const struct buffer *b, int o)
1238{
Willy Tarreau591d4452018-06-15 17:21:00 +02001239 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 +02001240}
1241
Willy Tarreau1f094672017-11-20 21:27:45 +01001242static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001243{
Willy Tarreau591d4452018-06-15 17:21:00 +02001244 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001245}
1246
Willy Tarreau1f094672017-11-20 21:27:45 +01001247static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001248{
Willy Tarreau591d4452018-06-15 17:21:00 +02001249 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001250}
1251
Willy Tarreau1f094672017-11-20 21:27:45 +01001252static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001253{
Willy Tarreau591d4452018-06-15 17:21:00 +02001254 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001255}
1256
1257
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001258/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1259 * The algorithm is not obvious. It turns out that H2 headers are neither
1260 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1261 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001262 *
1263 * b0 b1 b2 b3 b4 b5..b8
1264 * +----------+---------+--------+----+----+----------------------+
1265 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1266 * +----------+---------+--------+----+----+----------------------+
1267 *
1268 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1269 * we get the sid properly aligned and ordered, and 16 bits of len properly
1270 * ordered as well. The type and flags can be extracted using bit shifts from
1271 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001272 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1273 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001274 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001275static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001276{
1277 uint64_t w;
1278
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001279 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001280 return 0;
1281
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001282 w = h2_get_n64(b, o + 1);
1283 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001284 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1285 h->ff = w >> 32;
1286 h->ft = w >> 40;
1287 h->len += w >> 48;
1288 return 1;
1289}
1290
1291/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1292 * h2_peek_frame_hdr() above.
1293 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001294static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001295{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001296 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001297}
1298
1299/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001300static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001301{
1302 int ret;
1303
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001304 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001305 if (ret > 0)
1306 h2_skip_frame_hdr(b);
1307 return ret;
1308}
1309
Willy Tarreaucb985a42019-10-07 16:56:34 +02001310
1311/* try to fragment the headers frame present at the beginning of buffer <b>,
1312 * enforcing a limit of <mfs> bytes per frame. Returns 0 on failure, 1 on
1313 * success. Typical causes of failure include a buffer not large enough to
1314 * add extra frame headers. The existing frame size is read in the current
1315 * frame. Its EH flag will be cleared if CONTINUATION frames need to be added,
1316 * and its length will be adjusted. The stream ID for continuation frames will
1317 * be copied from the initial frame's.
1318 */
1319static int h2_fragment_headers(struct buffer *b, uint32_t mfs)
1320{
1321 size_t remain = b->data - 9;
1322 int extra_frames = (remain - 1) / mfs;
1323 size_t fsize;
1324 char *fptr;
1325 int frame;
1326
1327 if (b->data <= mfs + 9)
1328 return 1;
1329
1330 /* Too large a frame, we need to fragment it using CONTINUATION
1331 * frames. We start from the end and move tails as needed.
1332 */
1333 if (b->data + extra_frames * 9 > b->size)
1334 return 0;
1335
1336 for (frame = extra_frames; frame; frame--) {
1337 fsize = ((remain - 1) % mfs) + 1;
1338 remain -= fsize;
1339
1340 /* move data */
1341 fptr = b->area + 9 + remain + (frame - 1) * 9;
1342 memmove(fptr + 9, b->area + 9 + remain, fsize);
1343 b->data += 9;
1344
1345 /* write new frame header */
1346 h2_set_frame_size(fptr, fsize);
1347 fptr[3] = H2_FT_CONTINUATION;
1348 fptr[4] = (frame == extra_frames) ? H2_F_HEADERS_END_HEADERS : 0;
1349 write_n32(fptr + 5, read_n32(b->area + 5));
1350 }
1351
1352 b->area[4] &= ~H2_F_HEADERS_END_HEADERS;
1353 h2_set_frame_size(b->area, remain);
1354 return 1;
1355}
1356
1357
Willy Tarreau00dd0782018-03-01 16:31:34 +01001358/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1359 * its connection if the stream was not yet closed. Please use this exclusively
1360 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001361 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001362static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001363{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001364 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001365 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001366 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001367 if (!h2s->id)
1368 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +01001369 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +01001370 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
1371 h2s_notify_recv(h2s);
1372 }
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001373 HA_ATOMIC_SUB(&h2s->h2c->px_counters->open_streams, 1);
1374
Willy Tarreau7838a792019-08-12 18:42:03 +02001375 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001376 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001377 h2s->st = H2_SS_CLOSED;
1378}
1379
Willy Tarreau71049cc2018-03-28 13:56:39 +02001380/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001381/* h2s_destroy should only ever be called by the thread that owns the stream,
1382 * that means that a tasklet should be used if we want to destroy the h2s
1383 * from another thread
1384 */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001385static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001386{
Willy Tarreau7838a792019-08-12 18:42:03 +02001387 struct connection *conn = h2s->h2c->conn;
1388
1389 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1390
Willy Tarreau0a10de62018-03-01 16:27:53 +01001391 h2s_close(h2s);
1392 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001393 if (b_size(&h2s->rxbuf)) {
1394 b_free(&h2s->rxbuf);
1395 offer_buffers(NULL, tasks_run_queue);
1396 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001397
1398 if (h2s->subs)
1399 h2s->subs->events = 0;
1400
Joseph Herlantd77575d2018-11-25 10:54:45 -08001401 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001402 * reference left would be in the h2c send_list/fctl_list, and if
1403 * we're in it, we're getting out anyway
1404 */
Olivier Houchardd360ac62019-03-22 17:37:16 +01001405 LIST_DEL_INIT(&h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01001406
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001407 /* ditto, calling tasklet_free() here should be ok */
Willy Tarreau5723f292020-01-10 15:16:57 +01001408 tasklet_free(h2s->shut_tl);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001409 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001410
1411 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001412}
1413
Willy Tarreaua8e49542018-10-03 18:53:55 +02001414/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1415 * stream tree. In case of error, nothing is added and NULL is returned. The
1416 * causes of errors can be any failed memory allocation. The caller is
1417 * responsible for checking if the connection may support an extra stream
1418 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001419 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001420static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001421{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001422 struct h2s *h2s;
1423
Willy Tarreau7838a792019-08-12 18:42:03 +02001424 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1425
Willy Tarreaubafbe012017-11-24 17:34:44 +01001426 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001427 if (!h2s)
1428 goto out;
1429
Willy Tarreau5723f292020-01-10 15:16:57 +01001430 h2s->shut_tl = tasklet_new();
1431 if (!h2s->shut_tl) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001432 pool_free(pool_head_h2s, h2s);
1433 goto out;
1434 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001435 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01001436 h2s->shut_tl->process = h2_deferred_shut;
1437 h2s->shut_tl->context = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001438 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001439 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001440 h2s->cs = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001441 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001442 h2s->flags = H2_SF_NONE;
1443 h2s->errcode = H2_ERR_NO_ERROR;
1444 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001445 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001446 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001447 h2s->rxbuf = BUF_NULL;
Amaury Denoyelle74162742020-12-11 17:53:05 +01001448 memset(h2s->upgrade_protocol, 0, sizeof(h2s->upgrade_protocol));
Willy Tarreau751f2d02018-10-05 09:35:00 +02001449
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001450 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001451 if (id > 0)
1452 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001453 else
1454 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001455
1456 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001457 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001458 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001459
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001460 HA_ATOMIC_ADD(&h2c->px_counters->open_streams, 1);
Amaury Denoyellee7b891f2020-11-03 15:04:45 +01001461 HA_ATOMIC_ADD(&h2c->px_counters->total_streams, 1);
Amaury Denoyelle66942c12020-10-27 17:16:04 +01001462
Willy Tarreau7838a792019-08-12 18:42:03 +02001463 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001464 return h2s;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001465 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001466 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001467 return NULL;
1468}
1469
1470/* creates a new stream <id> on the h2c connection and returns it, or NULL in
Christopher Faulet7d013e72020-12-15 16:56:50 +01001471 * case of memory allocation error. <input> is used as input buffer for the new
1472 * stream. On success, it is transferred to the stream and the mux is no longer
1473 * responsible of it. On error, <input> is unchanged, thus the mux must still
1474 * take care of it.
Willy Tarreaua8e49542018-10-03 18:53:55 +02001475 */
Christopher Faulet7d013e72020-12-15 16:56:50 +01001476static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id, struct buffer *input)
Willy Tarreaua8e49542018-10-03 18:53:55 +02001477{
1478 struct session *sess = h2c->conn->owner;
1479 struct conn_stream *cs;
1480 struct h2s *h2s;
1481
Willy Tarreau7838a792019-08-12 18:42:03 +02001482 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1483
Willy Tarreaua8e49542018-10-03 18:53:55 +02001484 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1485 goto out;
1486
1487 h2s = h2s_new(h2c, id);
1488 if (!h2s)
1489 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001490
Christopher Faulet236c93b2020-07-02 09:19:54 +02001491 cs = cs_new(h2c->conn, h2c->conn->target);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001492 if (!cs)
1493 goto out_close;
1494
Olivier Houchard746fb772018-12-15 19:42:00 +01001495 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001496 h2s->cs = cs;
1497 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001498 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001499
Christopher Faulet7d013e72020-12-15 16:56:50 +01001500 if (stream_create_from_cs(cs, input) < 0)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001501 goto out_free_cs;
1502
Willy Tarreau590a0512018-09-05 11:56:48 +02001503 /* We want the accept date presented to the next stream to be the one
1504 * we have now, the handshake time to be null (since the next stream
1505 * is not delayed by a handshake), and the idle time to count since
1506 * right now.
1507 */
1508 sess->accept_date = date;
1509 sess->tv_accept = now;
1510 sess->t_handshake = 0;
1511
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001512 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001513 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001514 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001515 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001516 return h2s;
1517
1518 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001519 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001520 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001521 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001522 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001523 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001524 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001525 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001526 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001527 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001528}
1529
Willy Tarreau751f2d02018-10-05 09:35:00 +02001530/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1531 * and returns it, or NULL in case of memory allocation error or if the highest
1532 * possible stream ID was reached.
1533 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001534static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001535{
1536 struct h2s *h2s = NULL;
1537
Willy Tarreau7838a792019-08-12 18:42:03 +02001538 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1539
Willy Tarreau86949782019-01-31 10:42:05 +01001540 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001541 goto out;
1542
Willy Tarreaua80dca82019-01-24 17:08:28 +01001543 if (h2_streams_left(h2c) < 1)
1544 goto out;
1545
Willy Tarreau751f2d02018-10-05 09:35:00 +02001546 /* Defer choosing the ID until we send the first message to create the stream */
1547 h2s = h2s_new(h2c, 0);
1548 if (!h2s)
1549 goto out;
1550
1551 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001552 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001553 cs->ctx = h2s;
1554 h2c->nb_cs++;
1555
Willy Tarreau751f2d02018-10-05 09:35:00 +02001556 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001557 if (likely(h2s))
1558 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1559 else
1560 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001561 return h2s;
1562}
1563
Willy Tarreaube5b7152017-09-25 16:25:39 +02001564/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1565 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1566 * the various settings codes.
1567 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001568static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001569{
1570 struct buffer *res;
1571 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001572 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001573 int mfs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001574 int ret = 0;
1575
1576 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001577
1578 if (h2c_mux_busy(h2c, NULL)) {
1579 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001580 goto out;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001581 }
1582
Willy Tarreaube5b7152017-09-25 16:25:39 +02001583 chunk_init(&buf, buf_data, sizeof(buf_data));
1584 chunk_memcpy(&buf,
1585 "\x00\x00\x00" /* length : 0 for now */
1586 "\x04\x00" /* type : 4 (settings), flags : 0 */
1587 "\x00\x00\x00\x00", /* stream ID : 0 */
1588 9);
1589
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001590 if (h2c->flags & H2_CF_IS_BACK) {
1591 /* send settings_enable_push=0 */
1592 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1593 }
1594
Willy Tarreaube5b7152017-09-25 16:25:39 +02001595 if (h2_settings_header_table_size != 4096) {
1596 char str[6] = "\x00\x01"; /* header_table_size */
1597
1598 write_n32(str + 2, h2_settings_header_table_size);
1599 chunk_memcat(&buf, str, 6);
1600 }
1601
1602 if (h2_settings_initial_window_size != 65535) {
1603 char str[6] = "\x00\x04"; /* initial_window_size */
1604
1605 write_n32(str + 2, h2_settings_initial_window_size);
1606 chunk_memcat(&buf, str, 6);
1607 }
1608
1609 if (h2_settings_max_concurrent_streams != 0) {
1610 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1611
1612 /* Note: 0 means "unlimited" for haproxy's config but not for
1613 * the protocol, so never send this value!
1614 */
1615 write_n32(str + 2, h2_settings_max_concurrent_streams);
1616 chunk_memcat(&buf, str, 6);
1617 }
1618
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001619 mfs = h2_settings_max_frame_size;
1620 if (mfs > global.tune.bufsize)
1621 mfs = global.tune.bufsize;
1622
1623 if (!mfs)
1624 mfs = global.tune.bufsize;
1625
1626 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001627 char str[6] = "\x00\x05"; /* max_frame_size */
1628
1629 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1630 * match bufsize - rewrite size, but at the moment it seems
1631 * that clients don't take care of it.
1632 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001633 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001634 chunk_memcat(&buf, str, 6);
1635 }
1636
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001637 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001638
1639 res = br_tail(h2c->mbuf);
1640 retry:
1641 if (!h2_get_buf(h2c, res)) {
1642 h2c->flags |= H2_CF_MUX_MALLOC;
1643 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001644 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001645 }
1646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001647 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001648 if (unlikely(ret <= 0)) {
1649 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001650 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1651 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001652 h2c->flags |= H2_CF_MUX_MFULL;
1653 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001654 }
1655 else {
1656 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001657 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001658 }
1659 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001660 out:
1661 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001662 return ret;
1663}
1664
Willy Tarreau52eed752017-09-22 15:05:09 +02001665/* Try to receive a connection preface, then upon success try to send our
1666 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1667 * missing data. It may return an error in h2c.
1668 */
1669static int h2c_frt_recv_preface(struct h2c *h2c)
1670{
1671 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001672 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001673
Willy Tarreau7838a792019-08-12 18:42:03 +02001674 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1675
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001676 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001677
1678 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001679 if (ret1 < 0)
1680 sess_log(h2c->conn->owner);
1681
Amaury Denoyellea8879232020-10-27 17:16:03 +01001682 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01001683 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 +02001684 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Amaury Denoyellea8879232020-10-27 17:16:03 +01001685 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
1686 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001687 ret2 = 0;
1688 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001689 }
1690
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001691 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001692 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001693 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001694 out:
1695 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001696 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001697}
1698
Willy Tarreau01b44822018-10-03 14:26:37 +02001699/* Try to send a connection preface, then upon success try to send our
1700 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1701 * missing data. It may return an error in h2c.
1702 */
1703static int h2c_bck_send_preface(struct h2c *h2c)
1704{
1705 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001706 int ret = 0;
1707
1708 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001709
1710 if (h2c_mux_busy(h2c, NULL)) {
1711 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001712 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001713 }
1714
Willy Tarreaubcc45952019-05-26 10:05:50 +02001715 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001716 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001717 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001718 h2c->flags |= H2_CF_MUX_MALLOC;
1719 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001720 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001721 }
1722
1723 if (!b_data(res)) {
1724 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001725 ret = b_istput(res, ist(H2_CONN_PREFACE));
1726 if (unlikely(ret <= 0)) {
1727 if (!ret) {
1728 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1729 goto retry;
1730 h2c->flags |= H2_CF_MUX_MFULL;
1731 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001732 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001733 }
1734 else {
1735 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001736 ret = 0;
1737 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001738 }
1739 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001740 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001741 ret = h2c_send_settings(h2c);
1742 out:
1743 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1744 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001745}
1746
Willy Tarreau081d4722017-05-16 21:51:05 +02001747/* try to send a GOAWAY frame on the connection to report an error or a graceful
1748 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1749 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1750 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1751 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1752 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1753 * on unrecoverable failure. It will not attempt to send one again in this last
1754 * case so that it is safe to use h2c_error() to report such errors.
1755 */
1756static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1757{
1758 struct buffer *res;
1759 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001760 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001761
Willy Tarreau7838a792019-08-12 18:42:03 +02001762 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1763
1764 if (h2c->flags & H2_CF_GOAWAY_FAILED) {
1765 ret = 1; // claim that it worked
1766 goto out;
1767 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001768
1769 if (h2c_mux_busy(h2c, h2s)) {
1770 if (h2s)
1771 h2s->flags |= H2_SF_BLK_MBUSY;
1772 else
1773 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001774 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001775 }
1776
Willy Tarreau9c218e72019-05-26 10:08:28 +02001777 /* len: 8, type: 7, flags: none, sid: 0 */
1778 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1779
1780 if (h2c->last_sid < 0)
1781 h2c->last_sid = h2c->max_id;
1782
1783 write_n32(str + 9, h2c->last_sid);
1784 write_n32(str + 13, h2c->errcode);
1785
Willy Tarreaubcc45952019-05-26 10:05:50 +02001786 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001787 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001788 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001789 h2c->flags |= H2_CF_MUX_MALLOC;
1790 if (h2s)
1791 h2s->flags |= H2_SF_BLK_MROOM;
1792 else
1793 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001794 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001795 }
1796
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001797 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001798 if (unlikely(ret <= 0)) {
1799 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001800 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1801 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001802 h2c->flags |= H2_CF_MUX_MFULL;
1803 if (h2s)
1804 h2s->flags |= H2_SF_BLK_MROOM;
1805 else
1806 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001807 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001808 }
1809 else {
1810 /* we cannot report this error using GOAWAY, so we mark
1811 * it and claim a success.
1812 */
1813 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1814 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001815 ret = 1;
1816 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001817 }
1818 }
1819 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreauf965b2a2020-12-01 10:47:18 +01001820
1821 /* some codes are not for real errors, just attempts to close cleanly */
1822 switch (h2c->errcode) {
1823 case H2_ERR_NO_ERROR:
1824 case H2_ERR_ENHANCE_YOUR_CALM:
1825 case H2_ERR_REFUSED_STREAM:
1826 case H2_ERR_CANCEL:
1827 break;
1828 default:
1829 HA_ATOMIC_ADD(&h2c->px_counters->goaway_resp, 1);
1830 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001831 out:
1832 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001833 return ret;
1834}
1835
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001836/* Try to send an RST_STREAM frame on the connection for the indicated stream
1837 * during mux operations. This stream must be valid and cannot be closed
1838 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1839 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1840 * not yet.
1841 *
1842 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1843 * to write the message, it subscribes the stream to future notifications.
1844 */
1845static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1846{
1847 struct buffer *res;
1848 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001849 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001850
Willy Tarreau7838a792019-08-12 18:42:03 +02001851 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1852
1853 if (!h2s || h2s->st == H2_SS_CLOSED) {
1854 ret = 1;
1855 goto out;
1856 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001857
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001858 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1859 * RST_STREAM in response to a RST_STREAM frame.
1860 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001861 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001862 ret = 1;
1863 goto ignore;
1864 }
1865
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001866 if (h2c_mux_busy(h2c, h2s)) {
1867 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001868 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001869 }
1870
Willy Tarreau9c218e72019-05-26 10:08:28 +02001871 /* len: 4, type: 3, flags: none */
1872 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1873 write_n32(str + 5, h2s->id);
1874 write_n32(str + 9, h2s->errcode);
1875
Willy Tarreaubcc45952019-05-26 10:05:50 +02001876 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001877 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001878 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001879 h2c->flags |= H2_CF_MUX_MALLOC;
1880 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001881 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001882 }
1883
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001884 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001885 if (unlikely(ret <= 0)) {
1886 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001887 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1888 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001889 h2c->flags |= H2_CF_MUX_MFULL;
1890 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001891 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001892 }
1893 else {
1894 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001895 ret = 0;
1896 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001897 }
1898 }
1899
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001900 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001901 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001902 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001903 out:
1904 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001905 return ret;
1906}
1907
1908/* Try to send an RST_STREAM frame on the connection for the stream being
1909 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001910 * error code, even if the stream is one of the dummy ones, and will update
1911 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001912 *
1913 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1914 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001915 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001916 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001917 */
1918static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1919{
1920 struct buffer *res;
1921 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001922 int ret = 0;
1923
1924 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001925
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001926 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1927 * RST_STREAM in response to a RST_STREAM frame.
1928 */
1929 if (h2c->dft == H2_FT_RST_STREAM) {
1930 ret = 1;
1931 goto ignore;
1932 }
1933
Willy Tarreau27a84c92017-10-17 08:10:17 +02001934 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001935 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001936 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001937 }
1938
Willy Tarreau9c218e72019-05-26 10:08:28 +02001939 /* len: 4, type: 3, flags: none */
1940 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1941
1942 write_n32(str + 5, h2c->dsi);
1943 write_n32(str + 9, h2s->errcode);
1944
Willy Tarreaubcc45952019-05-26 10:05:50 +02001945 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001946 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001947 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001948 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001949 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001950 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001951 }
1952
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001953 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001954 if (unlikely(ret <= 0)) {
1955 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001956 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1957 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001958 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001959 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001960 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001961 }
1962 else {
1963 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001964 ret = 0;
1965 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001966 }
1967 }
1968
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001969 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001970 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001971 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001972 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001973 }
1974
Willy Tarreau7838a792019-08-12 18:42:03 +02001975 out:
Amaury Denoyellea8879232020-10-27 17:16:03 +01001976 HA_ATOMIC_ADD(&h2c->px_counters->rst_stream_resp, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001977 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001978 return ret;
1979}
1980
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001981/* try to send an empty DATA frame with the ES flag set to notify about the
1982 * end of stream and match a shutdown(write). If an ES was already sent as
1983 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1984 * on success or zero if nothing was done. In case of lack of room to write the
1985 * message, it subscribes the requesting stream to future notifications.
1986 */
1987static int h2_send_empty_data_es(struct h2s *h2s)
1988{
1989 struct h2c *h2c = h2s->h2c;
1990 struct buffer *res;
1991 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02001992 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001993
Willy Tarreau7838a792019-08-12 18:42:03 +02001994 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
1995
1996 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
1997 ret = 1;
1998 goto out;
1999 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002000
2001 if (h2c_mux_busy(h2c, h2s)) {
2002 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002003 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002004 }
2005
Willy Tarreau9c218e72019-05-26 10:08:28 +02002006 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
2007 memcpy(str, "\x00\x00\x00\x00\x01", 5);
2008 write_n32(str + 5, h2s->id);
2009
Willy Tarreaubcc45952019-05-26 10:05:50 +02002010 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002011 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002012 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002013 h2c->flags |= H2_CF_MUX_MALLOC;
2014 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002015 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002016 }
2017
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002018 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002019 if (likely(ret > 0)) {
2020 h2s->flags |= H2_SF_ES_SENT;
2021 }
2022 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002023 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2024 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002025 h2c->flags |= H2_CF_MUX_MFULL;
2026 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01002027 }
2028 else {
2029 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002030 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002031 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002032 out:
2033 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002034 return ret;
2035}
2036
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002037/* wake a specific stream and assign its conn_stream some CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02002038 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002039 * is automatically updated accordingly. If the stream is orphaned, it is
2040 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01002041 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002042static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01002043{
Willy Tarreau7838a792019-08-12 18:42:03 +02002044 struct h2c *h2c = h2s->h2c;
2045
2046 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
2047
Christopher Fauletf02ca002019-03-07 16:21:34 +01002048 if (!h2s->cs) {
2049 /* this stream was already orphaned */
2050 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002051 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002052 return;
2053 }
2054
Christopher Fauletaade4ed2020-10-08 15:38:41 +02002055 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002056 if (h2s->st == H2_SS_OPEN)
2057 h2s->st = H2_SS_HREM;
2058 else if (h2s->st == H2_SS_HLOC)
2059 h2s_close(h2s);
2060 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002061
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002062 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
2063 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
2064 h2s->cs->flags |= CS_FL_ERR_PENDING;
2065 if (h2s->cs->flags & CS_FL_EOS)
2066 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02002067
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02002068 if (h2s->st < H2_SS_ERROR)
2069 h2s->st = H2_SS_ERROR;
2070 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01002071
2072 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002073 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002074}
2075
2076/* wake the streams attached to the connection, whose id is greater than <last>
2077 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002078 */
Willy Tarreau23482912019-05-07 15:23:14 +02002079static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002080{
2081 struct eb32_node *node;
2082 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002083
Willy Tarreau7838a792019-08-12 18:42:03 +02002084 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
2085
Christopher Fauletf02ca002019-03-07 16:21:34 +01002086 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002087 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
2088 while (node) {
2089 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002090 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002091 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01002092 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002093
Christopher Fauletf02ca002019-03-07 16:21:34 +01002094 /* Wake all streams with unassigned ID (ID == 0) */
2095 node = eb32_lookup(&h2c->streams_by_id, 0);
2096 while (node) {
2097 h2s = container_of(node, struct h2s, by_id);
2098 if (h2s->id > 0)
2099 break;
2100 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02002101 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002102 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002103
2104 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002105}
2106
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002107/* Wake up all blocked streams whose window size has become positive after the
2108 * mux's initial window was adjusted. This should be done after having processed
2109 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002110 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002111static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02002112{
2113 struct h2s *h2s;
2114 struct eb32_node *node;
2115
Willy Tarreau7838a792019-08-12 18:42:03 +02002116 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2117
Willy Tarreau3421aba2017-07-27 15:41:03 +02002118 node = eb32_first(&h2c->streams_by_id);
2119 while (node) {
2120 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002121 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002122 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002123 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002124 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2125 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002126 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01002127 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002128 node = eb32_next(node);
2129 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002130
2131 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002132}
2133
2134/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
2135 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01002136 * return an error in h2c. The caller must have already verified frame length
2137 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02002138 */
2139static int h2c_handle_settings(struct h2c *h2c)
2140{
2141 unsigned int offset;
2142 int error;
2143
Willy Tarreau7838a792019-08-12 18:42:03 +02002144 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
2145
Willy Tarreau3421aba2017-07-27 15:41:03 +02002146 if (h2c->dff & H2_F_SETTINGS_ACK) {
2147 if (h2c->dfl) {
2148 error = H2_ERR_FRAME_SIZE_ERROR;
2149 goto fail;
2150 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002151 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002152 }
2153
Willy Tarreau3421aba2017-07-27 15:41:03 +02002154 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002155 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002156 goto out0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002157
2158 /* parse the frame */
2159 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002160 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2161 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002162
2163 switch (type) {
2164 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2165 /* we need to update all existing streams with the
2166 * difference from the previous iws.
2167 */
2168 if (arg < 0) { // RFC7540#6.5.2
2169 error = H2_ERR_FLOW_CONTROL_ERROR;
2170 goto fail;
2171 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002172 h2c->miw = arg;
2173 break;
2174 case H2_SETTINGS_MAX_FRAME_SIZE:
2175 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002176 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 +02002177 error = H2_ERR_PROTOCOL_ERROR;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002178 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002179 goto fail;
2180 }
2181 h2c->mfs = arg;
2182 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002183 case H2_SETTINGS_ENABLE_PUSH:
2184 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002185 TRACE_ERROR("ENABLE_PUSH out of range", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002186 error = H2_ERR_PROTOCOL_ERROR;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002187 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau1b38b462017-12-03 19:02:28 +01002188 goto fail;
2189 }
2190 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002191 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2192 if (h2c->flags & H2_CF_IS_BACK) {
2193 /* the limit is only for the backend; for the frontend it is our limit */
2194 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
2195 arg = h2_settings_max_concurrent_streams;
2196 h2c->streams_limit = arg;
2197 }
2198 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002199 }
2200 }
2201
2202 /* need to ACK this frame now */
2203 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002204 done:
2205 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002206 return 1;
2207 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002208 if (!(h2c->flags & H2_CF_IS_BACK))
2209 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002210 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002211 out0:
2212 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 +02002213 return 0;
2214}
2215
2216/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2217 * success or one of the h2_status values.
2218 */
2219static int h2c_ack_settings(struct h2c *h2c)
2220{
2221 struct buffer *res;
2222 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002223 int ret = 0;
2224
2225 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002226
2227 if (h2c_mux_busy(h2c, NULL)) {
2228 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002229 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002230 }
2231
Willy Tarreau9c218e72019-05-26 10:08:28 +02002232 memcpy(str,
2233 "\x00\x00\x00" /* length : 0 (no data) */
2234 "\x04" "\x01" /* type : 4, flags : ACK */
2235 "\x00\x00\x00\x00" /* stream ID */, 9);
2236
Willy Tarreaubcc45952019-05-26 10:05:50 +02002237 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002238 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002239 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002240 h2c->flags |= H2_CF_MUX_MALLOC;
2241 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002242 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002243 }
2244
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002245 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002246 if (unlikely(ret <= 0)) {
2247 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002248 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2249 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002250 h2c->flags |= H2_CF_MUX_MFULL;
2251 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002252 }
2253 else {
2254 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002255 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002256 }
2257 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002258 out:
2259 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002260 return ret;
2261}
2262
Willy Tarreaucf68c782017-10-10 17:11:41 +02002263/* processes a PING frame and schedules an ACK if needed. The caller must pass
2264 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002265 * missing data. The caller must have already verified frame length
2266 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002267 */
2268static int h2c_handle_ping(struct h2c *h2c)
2269{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002270 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002271 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002272 h2c->st0 = H2_CS_FRAME_A;
2273 return 1;
2274}
2275
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002276/* Try to send a window update for stream id <sid> and value <increment>.
2277 * Returns > 0 on success or zero on missing room or failure. It may return an
2278 * error in h2c.
2279 */
2280static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2281{
2282 struct buffer *res;
2283 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002284 int ret = 0;
2285
2286 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002287
2288 if (h2c_mux_busy(h2c, NULL)) {
2289 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002290 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002291 }
2292
Willy Tarreau9c218e72019-05-26 10:08:28 +02002293 /* length: 4, type: 8, flags: none */
2294 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2295 write_n32(str + 5, sid);
2296 write_n32(str + 9, increment);
2297
Willy Tarreaubcc45952019-05-26 10:05:50 +02002298 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002299 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002300 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002301 h2c->flags |= H2_CF_MUX_MALLOC;
2302 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002303 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002304 }
2305
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002306 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002307 if (unlikely(ret <= 0)) {
2308 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002309 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2310 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002311 h2c->flags |= H2_CF_MUX_MFULL;
2312 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002313 }
2314 else {
2315 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002316 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002317 }
2318 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002319 out:
2320 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002321 return ret;
2322}
2323
2324/* try to send pending window update for the connection. It's safe to call it
2325 * with no pending updates. Returns > 0 on success or zero on missing room or
2326 * failure. It may return an error in h2c.
2327 */
2328static int h2c_send_conn_wu(struct h2c *h2c)
2329{
2330 int ret = 1;
2331
Willy Tarreau7838a792019-08-12 18:42:03 +02002332 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2333
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002334 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002335 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002336
Willy Tarreau97aaa672018-12-23 09:49:04 +01002337 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2338 /* increase the advertised connection window to 2G on
2339 * first update.
2340 */
2341 h2c->flags |= H2_CF_WINDOW_OPENED;
2342 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2343 }
2344
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002345 /* send WU for the connection */
2346 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2347 if (ret > 0)
2348 h2c->rcvd_c = 0;
2349
Willy Tarreau7838a792019-08-12 18:42:03 +02002350 out:
2351 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002352 return ret;
2353}
2354
2355/* try to send pending window update for the current dmux stream. It's safe to
2356 * call it with no pending updates. Returns > 0 on success or zero on missing
2357 * room or failure. It may return an error in h2c.
2358 */
2359static int h2c_send_strm_wu(struct h2c *h2c)
2360{
2361 int ret = 1;
2362
Willy Tarreau7838a792019-08-12 18:42:03 +02002363 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2364
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002365 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002366 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002367
2368 /* send WU for the stream */
2369 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2370 if (ret > 0)
2371 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002372 out:
2373 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002374 return ret;
2375}
2376
Willy Tarreaucf68c782017-10-10 17:11:41 +02002377/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2378 * success, 0 on missing data or one of the h2_status values.
2379 */
2380static int h2c_ack_ping(struct h2c *h2c)
2381{
2382 struct buffer *res;
2383 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002384 int ret = 0;
2385
2386 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002387
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002388 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002389 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002390
2391 if (h2c_mux_busy(h2c, NULL)) {
2392 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002393 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002394 }
2395
Willy Tarreaucf68c782017-10-10 17:11:41 +02002396 memcpy(str,
2397 "\x00\x00\x08" /* length : 8 (same payload) */
2398 "\x06" "\x01" /* type : 6, flags : ACK */
2399 "\x00\x00\x00\x00" /* stream ID */, 9);
2400
2401 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002402 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002403
Willy Tarreau9c218e72019-05-26 10:08:28 +02002404 res = br_tail(h2c->mbuf);
2405 retry:
2406 if (!h2_get_buf(h2c, res)) {
2407 h2c->flags |= H2_CF_MUX_MALLOC;
2408 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002409 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002410 }
2411
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002412 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002413 if (unlikely(ret <= 0)) {
2414 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002415 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2416 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002417 h2c->flags |= H2_CF_MUX_MFULL;
2418 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002419 }
2420 else {
2421 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002422 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002423 }
2424 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002425 out:
2426 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002427 return ret;
2428}
2429
Willy Tarreau26f95952017-07-27 17:18:30 +02002430/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2431 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002432 * h2c or h2s. The caller must have already verified frame length and stream ID
2433 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002434 */
2435static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2436{
2437 int32_t inc;
2438 int error;
2439
Willy Tarreau7838a792019-08-12 18:42:03 +02002440 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2441
Willy Tarreau26f95952017-07-27 17:18:30 +02002442 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002443 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002444 goto out0;
Willy Tarreau26f95952017-07-27 17:18:30 +02002445
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002447
2448 if (h2c->dsi != 0) {
2449 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002450
2451 /* it's not an error to receive WU on a closed stream */
2452 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002453 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002454
2455 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002456 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 +02002457 error = H2_ERR_PROTOCOL_ERROR;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002458 HA_ATOMIC_ADD(&h2c->px_counters->strm_proto_err, 1);
Willy Tarreau26f95952017-07-27 17:18:30 +02002459 goto strm_err;
2460 }
2461
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002462 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002463 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 +02002464 error = H2_ERR_FLOW_CONTROL_ERROR;
Willy Tarreaua3075282020-12-01 10:22:43 +01002465 HA_ATOMIC_ADD(&h2c->px_counters->strm_proto_err, 1);
Willy Tarreau26f95952017-07-27 17:18:30 +02002466 goto strm_err;
2467 }
2468
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002469 h2s->sws += inc;
2470 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002471 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002472 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002473 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2474 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Olivier Houcharddddfe312018-10-10 18:51:00 +02002475 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002476 }
2477 }
2478 else {
2479 /* connection window update */
2480 if (!inc) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002481 TRACE_ERROR("conn WINDOW_UPDATE inc=0", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002482 error = H2_ERR_PROTOCOL_ERROR;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002483 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau26f95952017-07-27 17:18:30 +02002484 goto conn_err;
2485 }
2486
2487 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2488 error = H2_ERR_FLOW_CONTROL_ERROR;
2489 goto conn_err;
2490 }
2491
2492 h2c->mws += inc;
2493 }
2494
Willy Tarreau7838a792019-08-12 18:42:03 +02002495 done:
2496 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002497 return 1;
2498
2499 conn_err:
2500 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002501 out0:
2502 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 +02002503 return 0;
2504
2505 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002506 h2s_error(h2s, error);
2507 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002508 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002509 return 0;
2510}
2511
Willy Tarreaue96b0922017-10-30 00:28:29 +01002512/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002513 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2514 * have already verified frame length and stream ID validity. Described in
2515 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002516 */
2517static int h2c_handle_goaway(struct h2c *h2c)
2518{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002519 int last;
2520
Willy Tarreau7838a792019-08-12 18:42:03 +02002521 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002522 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002523 if (b_data(&h2c->dbuf) < h2c->dfl) {
2524 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002525 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002526 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002527
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002528 last = h2_get_n32(&h2c->dbuf, 0);
2529 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002530 if (h2c->last_sid < 0)
2531 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002532 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002533 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002534 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002535}
2536
Willy Tarreau92153fc2017-12-03 19:46:19 +01002537/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002538 * invalid. Returns > 0 on success or zero on missing data. It may return an
2539 * error in h2c. The caller must have already verified frame length and stream
2540 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002541 */
2542static int h2c_handle_priority(struct h2c *h2c)
2543{
Willy Tarreau7838a792019-08-12 18:42:03 +02002544 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2545
Willy Tarreau92153fc2017-12-03 19:46:19 +01002546 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002547 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002548 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002549 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002550 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002551
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002552 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002553 /* 7540#5.3 : can't depend on itself */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002554 TRACE_ERROR("PRIORITY depends on itself", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002555 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01002556 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02002557 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002558 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002559 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002560 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002561 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002562}
2563
Willy Tarreaucd234e92017-08-18 10:59:39 +02002564/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002565 * Returns > 0 on success or zero on missing data. The caller must have already
2566 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002567 */
2568static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2569{
Willy Tarreau7838a792019-08-12 18:42:03 +02002570 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2571
Willy Tarreaucd234e92017-08-18 10:59:39 +02002572 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002573 if (b_data(&h2c->dbuf) < h2c->dfl) {
2574 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 +02002575 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002576 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002577
2578 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002579 if (h2s->st == H2_SS_CLOSED) {
2580 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 +02002581 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002582 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002583
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002584 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002585 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002586
2587 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002588 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002589 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002590 }
2591
2592 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002593 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002594 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002595}
2596
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002597/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2598 * It may return an error in h2c or h2s. The caller must consider that the
2599 * return value is the new h2s in case one was allocated (most common case).
2600 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002601 * errors here are reported as connection errors since it's impossible to
2602 * recover from such errors after the compression context has been altered.
2603 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002604static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002605{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002606 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002607 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002608 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002609 int error;
2610
Willy Tarreau7838a792019-08-12 18:42:03 +02002611 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2612
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002613 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002614 goto out; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002615
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002616 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002617 goto out; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002618
2619 /* now either the frame is complete or the buffer is complete */
2620 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002621 /* The stream exists/existed, this must be a trailers frame */
2622 if (h2s->st != H2_SS_CLOSED) {
Amaury Denoyelle74162742020-12-11 17:53:05 +01002623 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len, NULL);
Willy Tarreauaab1a602019-05-06 11:12:18 +02002624 /* unrecoverable error ? */
2625 if (h2c->st0 >= H2_CS_ERROR)
2626 goto out;
2627
2628 if (error == 0)
2629 goto out; // missing data
2630
2631 if (error < 0) {
2632 /* Failed to decode this frame (e.g. too large request)
2633 * but the HPACK decompressor is still synchronized.
2634 */
2635 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2636 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002637 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002638 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002639 goto done;
2640 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002641 /* the connection was already killed by an RST, let's consume
2642 * the data and send another RST.
2643 */
Amaury Denoyelle74162742020-12-11 17:53:05 +01002644 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau1f035502019-01-30 11:44:07 +01002645 h2s = (struct h2s*)h2_error_stream;
2646 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002647 }
2648 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2649 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2650 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002651 TRACE_ERROR("HEADERS on invalid stream ID", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Amaury Denoyellea8879232020-10-27 17:16:03 +01002652 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002653 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002654 goto conn_err;
2655 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002656 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2657 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002658
Amaury Denoyelle74162742020-12-11 17:53:05 +01002659 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len, NULL);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002660
Willy Tarreau25919232019-01-03 14:48:18 +01002661 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002662 if (h2c->st0 >= H2_CS_ERROR)
2663 goto out;
2664
Willy Tarreau25919232019-01-03 14:48:18 +01002665 if (error <= 0) {
2666 if (error == 0)
2667 goto out; // missing data
2668
2669 /* Failed to decode this stream (e.g. too large request)
2670 * but the HPACK decompressor is still synchronized.
2671 */
2672 h2s = (struct h2s*)h2_error_stream;
2673 goto send_rst;
2674 }
2675
Willy Tarreau22de8d32018-09-05 19:55:58 +02002676 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002677 * positively from h2c_frt_stream_new(), the stream will report the error,
2678 * and if we return in error, h2c_frt_stream_new() will emit the error.
Christopher Faulet7d013e72020-12-15 16:56:50 +01002679 *
2680 * Xfer the rxbuf to the stream. On success, the new stream owns the
2681 * rxbuf. On error, it is released here.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002682 */
Christopher Faulet7d013e72020-12-15 16:56:50 +01002683 h2s = h2c_frt_stream_new(h2c, h2c->dsi, &rxbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002684 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002685 h2s = (struct h2s*)h2_refused_stream;
2686 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002687 }
2688
2689 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002690 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002691 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002692
Willy Tarreau88d138e2019-01-02 19:38:14 +01002693 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002694 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002695 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002696
2697 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002698 if (h2s->st == H2_SS_OPEN)
2699 h2s->st = H2_SS_HREM;
2700 else
2701 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002702 }
2703
Willy Tarreau3a429f02019-01-03 11:41:50 +01002704 /* update the max stream ID if the request is being processed */
2705 if (h2s->id > h2c->max_id)
2706 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002707
Willy Tarreau022e5e52020-09-10 09:33:15 +02002708 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn, 0, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002709 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002710
2711 conn_err:
2712 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002713 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002714
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002715 out:
2716 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002717 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 +01002718 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002719
2720 send_rst:
2721 /* make the demux send an RST for the current stream. We may only
2722 * do this if we're certain that the HEADERS frame was properly
2723 * decompressed so that the HPACK decoder is still kept up to date.
2724 */
2725 h2_release_buf(h2c, &rxbuf);
2726 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002727
Willy Tarreau022e5e52020-09-10 09:33:15 +02002728 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 +02002729 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002730 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002731}
2732
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002733/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2734 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2735 * errors here are reported as connection errors since it's impossible to
2736 * recover from such errors after the compression context has been altered.
2737 */
2738static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2739{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002740 struct buffer rxbuf = BUF_NULL;
2741 unsigned long long body_len = 0;
2742 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002743 int error;
2744
Willy Tarreau7838a792019-08-12 18:42:03 +02002745 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2746
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002747 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002748 goto fail; // empty buffer
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002749
2750 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002751 goto fail; // incomplete frame
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002752
Christopher Faulet6884aa32019-09-23 15:28:20 +02002753 if (h2s->st != H2_SS_CLOSED) {
Amaury Denoyelle74162742020-12-11 17:53:05 +01002754 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len, h2s->upgrade_protocol);
Christopher Faulet6884aa32019-09-23 15:28:20 +02002755 }
2756 else {
2757 /* the connection was already killed by an RST, let's consume
2758 * the data and send another RST.
2759 */
Amaury Denoyelle74162742020-12-11 17:53:05 +01002760 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len, NULL);
Christopher Fauletea7a7782019-09-26 16:19:13 +02002761 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002762 h2c->st0 = H2_CS_FRAME_E;
2763 goto send_rst;
2764 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002765
Willy Tarreau25919232019-01-03 14:48:18 +01002766 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002767 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002768 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002769
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002770 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2771 /* RFC7540#5.1 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002772 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 +01002773 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2774 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreaua3075282020-12-01 10:22:43 +01002775 HA_ATOMIC_ADD(&h2c->px_counters->strm_proto_err, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02002776 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002777 }
2778
Willy Tarreau25919232019-01-03 14:48:18 +01002779 if (error <= 0) {
2780 if (error == 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002781 goto fail; // missing data
Willy Tarreau25919232019-01-03 14:48:18 +01002782
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002783 /* stream error : send RST_STREAM */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002784 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 +01002785 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002786 h2c->st0 = H2_CS_FRAME_E;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002787 HA_ATOMIC_ADD(&h2c->px_counters->strm_proto_err, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02002788 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002789 }
2790
Christopher Fauletfa922f02019-05-07 10:55:17 +02002791 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002792 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002793
Willy Tarreau927b88b2019-03-04 08:03:25 +01002794 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002795 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002796 else if (h2s->flags & H2_SF_ES_RCVD) {
2797 if (h2s->st == H2_SS_OPEN)
2798 h2s->st = H2_SS_HREM;
2799 else if (h2s->st == H2_SS_HLOC)
2800 h2s_close(h2s);
2801 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002802
Christopher Fauletf95f8762021-01-22 11:59:07 +01002803 /* Unblock busy server h2s waiting for the response headers to validate
2804 * the tunnel establishment or the end of the response of an oborted
2805 * tunnel
2806 */
2807 if ((h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY)) == (H2_SF_BODY_TUNNEL|H2_SF_BLK_MBUSY) ||
2808 (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)) {
2809 TRACE_STATE("Unblock h2s blocked on tunnel establishment/abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2810 h2s->flags &= ~H2_SF_BLK_MBUSY;
2811 }
2812
Willy Tarreau022e5e52020-09-10 09:33:15 +02002813 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 +02002814 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002815 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02002816 fail:
2817 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2818 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002819
2820 send_rst:
2821 /* make the demux send an RST for the current stream. We may only
2822 * do this if we're certain that the HEADERS frame was properly
2823 * decompressed so that the HPACK decoder is still kept up to date.
2824 */
2825 h2_release_buf(h2c, &rxbuf);
2826 h2c->st0 = H2_CS_FRAME_E;
2827
Willy Tarreau022e5e52020-09-10 09:33:15 +02002828 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 +02002829 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2830 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002831}
2832
Willy Tarreau454f9052017-10-26 19:40:35 +02002833/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2834 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2835 */
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01002836static int h2c_handle_data(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002837{
2838 int error;
2839
Willy Tarreau7838a792019-08-12 18:42:03 +02002840 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2841
Willy Tarreau454f9052017-10-26 19:40:35 +02002842 /* note that empty DATA frames are perfectly valid and sometimes used
2843 * to signal an end of stream (with the ES flag).
2844 */
2845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002847 goto fail; // empty buffer
Willy Tarreau454f9052017-10-26 19:40:35 +02002848
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002849 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002850 goto fail; // incomplete frame
Willy Tarreau454f9052017-10-26 19:40:35 +02002851
2852 /* now either the frame is complete or the buffer is complete */
2853
Willy Tarreau454f9052017-10-26 19:40:35 +02002854 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2855 /* RFC7540#6.1 */
2856 error = H2_ERR_STREAM_CLOSED;
2857 goto strm_err;
2858 }
2859
Christopher Faulet4f09ec82019-06-19 09:25:58 +02002860 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002861 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002862 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 +01002863 error = H2_ERR_PROTOCOL_ERROR;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002864 HA_ATOMIC_ADD(&h2c->px_counters->strm_proto_err, 1);
Willy Tarreau1915ca22019-01-24 11:49:37 +01002865 goto strm_err;
2866 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01002867 if (!(h2c->flags & H2_CF_IS_BACK) &&
2868 (h2s->flags & (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT)) == (H2_SF_TUNNEL_ABRT|H2_SF_ES_SENT) &&
2869 ((h2c->dfl - h2c->dpl) || !(h2c->dff & H2_F_DATA_END_STREAM))) {
2870 /* a tunnel attempt was aborted but the client still try to send some raw data.
2871 * Thus the stream is closed with the CANCEL error. Here we take care it is not
2872 * an empty DATA Frame with the ES flag. The error is only handled if ES was
2873 * already sent to the client because depending on the scheduling, these data may
2874 * have been sent before the server respnse but not handle here.
2875 */
2876 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2877 error = H2_ERR_CANCEL;
2878 goto strm_err;
2879 }
Willy Tarreau1915ca22019-01-24 11:49:37 +01002880
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002881 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02002882 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002883
Willy Tarreau454f9052017-10-26 19:40:35 +02002884 /* call the upper layers to process the frame, then let the upper layer
2885 * notify the stream about any change.
2886 */
2887 if (!h2s->cs) {
Willy Tarreau082c4572019-08-06 10:11:02 +02002888 /* The upper layer has already closed, this may happen on
2889 * 4xx/redirects during POST, or when receiving a response
2890 * from an H2 server after the client has aborted.
2891 */
2892 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002893 goto strm_err;
2894 }
2895
Willy Tarreau8f650c32017-11-21 19:36:21 +01002896 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002897 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01002898
Willy Tarreau721c9742017-11-07 11:05:42 +01002899 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002900 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002901 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002902 }
2903
2904 /* check for completion : the callee will change this to FRAME_A or
2905 * FRAME_H once done.
2906 */
2907 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02002908 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02002909
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002910 /* last frame */
2911 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002912 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002913 if (h2s->st == H2_SS_OPEN)
2914 h2s->st = H2_SS_HREM;
2915 else
2916 h2s_close(h2s);
2917
Willy Tarreau1915ca22019-01-24 11:49:37 +01002918 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2919 /* RFC7540#8.1.2 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002920 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 +01002921 error = H2_ERR_PROTOCOL_ERROR;
Amaury Denoyellea8879232020-10-27 17:16:03 +01002922 HA_ATOMIC_ADD(&h2c->px_counters->strm_proto_err, 1);
Willy Tarreau1915ca22019-01-24 11:49:37 +01002923 goto strm_err;
2924 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002925 }
2926
Christopher Fauletf95f8762021-01-22 11:59:07 +01002927 /* Unblock busy server h2s waiting for the end of the response for an
2928 * aborted tunnel
2929 */
2930 if ((h2c->flags & H2_CF_IS_BACK) &&
2931 (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)) {
2932 TRACE_STATE("Unblock h2s blocked on tunnel abort", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2933 h2s->flags &= ~H2_SF_BLK_MBUSY;
2934 }
2935
Willy Tarreau7838a792019-08-12 18:42:03 +02002936 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02002937 return 1;
2938
Willy Tarreau454f9052017-10-26 19:40:35 +02002939 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002940 h2s_error(h2s, error);
2941 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002942 fail:
2943 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 +02002944 return 0;
2945}
2946
Willy Tarreau63864812019-08-07 14:25:20 +02002947/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
2948 * valid for the current stream state. This is needed only after parsing the
2949 * frame header but in practice it can be performed at any time during
2950 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
2951 * or 0 in case of error, in which case either h2s or h2c will carry an error.
2952 */
2953static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
2954{
Willy Tarreau7838a792019-08-12 18:42:03 +02002955 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
2956
Willy Tarreau63864812019-08-07 14:25:20 +02002957 if (h2s->st == H2_SS_IDLE &&
2958 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2959 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2960 * this state MUST be treated as a connection error
2961 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002962 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 +02002963 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002964 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02002965 /* only log if no other stream can report the error */
2966 sess_log(h2c->conn->owner);
2967 }
Willy Tarreaua3075282020-12-01 10:22:43 +01002968 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +02002969 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 +02002970 return 0;
2971 }
2972
Willy Tarreau57a18162019-11-24 14:57:53 +01002973 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
2974 /* only PUSH_PROMISE would be permitted here */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002975 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 +01002976 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01002977 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau57a18162019-11-24 14:57:53 +01002978 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
2979 return 0;
2980 }
2981
Willy Tarreau63864812019-08-07 14:25:20 +02002982 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2983 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2984 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2985 * this state MUST be treated as a stream error.
2986 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2987 * PUSH_PROMISE/CONTINUATION cause connection errors.
2988 */
Amaury Denoyellea8879232020-10-27 17:16:03 +01002989 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01002990 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 +02002991 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01002992 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Amaury Denoyellea8879232020-10-27 17:16:03 +01002993 }
2994 else {
Willy Tarreau63864812019-08-07 14:25:20 +02002995 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Amaury Denoyellea8879232020-10-27 17:16:03 +01002996 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002997 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 +02002998 return 0;
2999 }
3000
3001 /* Below the management of frames received in closed state is a
3002 * bit hackish because the spec makes strong differences between
3003 * streams closed by receiving RST, sending RST, and seeing ES
3004 * in both directions. In addition to this, the creation of a
3005 * new stream reusing the identifier of a closed one will be
3006 * detected here. Given that we cannot keep track of all closed
3007 * streams forever, we consider that unknown closed streams were
3008 * closed on RST received, which allows us to respond with an
3009 * RST without breaking the connection (eg: to abort a transfer).
3010 * Some frames have to be silently ignored as well.
3011 */
3012 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
3013 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
3014 /* #5.1.1: The identifier of a newly
3015 * established stream MUST be numerically
3016 * greater than all streams that the initiating
3017 * endpoint has opened or reserved. This
3018 * governs streams that are opened using a
3019 * HEADERS frame and streams that are reserved
3020 * using PUSH_PROMISE. An endpoint that
3021 * receives an unexpected stream identifier
3022 * MUST respond with a connection error.
3023 */
3024 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003025 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 +02003026 return 0;
3027 }
3028
Willy Tarreau4c08f122019-09-26 08:47:15 +02003029 if (h2s->flags & H2_SF_RST_RCVD &&
3030 !(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 +02003031 /* RFC7540#5.1:closed: an endpoint that
3032 * receives any frame other than PRIORITY after
3033 * receiving a RST_STREAM MUST treat that as a
3034 * stream error of type STREAM_CLOSED.
3035 *
3036 * Note that old streams fall into this category
3037 * and will lead to an RST being sent.
3038 *
3039 * However, we cannot generalize this to all frame types. Those
3040 * carrying compression state must still be processed before
3041 * being dropped or we'll desynchronize the decoder. This can
3042 * happen with request trailers received after sending an
3043 * RST_STREAM, or with header/trailers responses received after
3044 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02003045 *
3046 * In addition, since our CLOSED streams always carry the
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003047 * RST_RCVD bit, we don't want to accidentally catch valid
Willy Tarreau4c08f122019-09-26 08:47:15 +02003048 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02003049 */
3050 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
3051 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02003052 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 +02003053 return 0;
3054 }
3055
3056 /* RFC7540#5.1:closed: if this state is reached as a
3057 * result of sending a RST_STREAM frame, the peer that
3058 * receives the RST_STREAM might have already sent
3059 * frames on the stream that cannot be withdrawn. An
3060 * endpoint MUST ignore frames that it receives on
3061 * closed streams after it has sent a RST_STREAM
3062 * frame. An endpoint MAY choose to limit the period
3063 * over which it ignores frames and treat frames that
3064 * arrive after this time as being in error.
3065 */
3066 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
3067 /* RFC7540#5.1:closed: any frame other than
3068 * PRIO/WU/RST in this state MUST be treated as
3069 * a connection error
3070 */
3071 if (h2c->dft != H2_FT_RST_STREAM &&
3072 h2c->dft != H2_FT_PRIORITY &&
3073 h2c->dft != H2_FT_WINDOW_UPDATE) {
3074 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02003075 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 +02003076 return 0;
3077 }
3078 }
3079 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003080 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02003081 return 1;
3082}
3083
Willy Tarreaubc933932017-10-09 16:21:43 +02003084/* process Rx frames to be demultiplexed */
3085static void h2_process_demux(struct h2c *h2c)
3086{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003087 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003088 struct h2_fh hdr;
3089 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003090 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02003091
Willy Tarreau7838a792019-08-12 18:42:03 +02003092 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3093
Willy Tarreau081d4722017-05-16 21:51:05 +02003094 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02003095 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02003096
3097 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3098 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003099 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02003100 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02003101 goto out;
3102
Willy Tarreau52eed752017-09-22 15:05:09 +02003103 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
3104 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003105 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003106 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003107 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02003108 sess_log(h2c->conn->owner);
3109 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003110 goto fail;
3111 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003112 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003113
3114 h2c->max_id = 0;
3115 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003116 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02003117 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003118
3119 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003120 /* ensure that what is pending is a valid SETTINGS frame
3121 * without an ACK.
3122 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003123 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 +02003124 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003125 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02003126 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003127 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 +02003128 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003129 if (!(h2c->flags & H2_CF_IS_BACK))
3130 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02003131 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003132 goto fail;
3133 }
3134
3135 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
3136 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003137 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 +02003138 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3139 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003140 if (!(h2c->flags & H2_CF_IS_BACK))
3141 sess_log(h2c->conn->owner);
Amaury Denoyellea8879232020-10-27 17:16:03 +01003142 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003143 goto fail;
3144 }
3145
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003146 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003147 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003148 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 +02003149 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3150 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003151 if (!(h2c->flags & H2_CF_IS_BACK))
3152 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003153 goto fail;
3154 }
3155
Willy Tarreau3bf69182018-12-21 15:34:50 +01003156 /* that's OK, switch to FRAME_P to process it. This is
3157 * a SETTINGS frame whose header has already been
3158 * deleted above.
3159 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01003160 padlen = 0;
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +01003161 HA_ATOMIC_ADD(&h2c->px_counters->settings_rcvd, 1);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003162 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02003163 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003164 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003165
3166 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02003167 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02003168 int ret = 0;
3169
Willy Tarreau7838a792019-08-12 18:42:03 +02003170 if (!b_data(&h2c->dbuf)) {
3171 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
3172 break;
3173 }
3174
3175 if (h2c->st0 >= H2_CS_ERROR) {
3176 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003177 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003178 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003179
3180 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau30d05f32019-08-06 15:49:51 +02003181 h2c->rcvd_s = 0;
3182
Willy Tarreau7838a792019-08-12 18:42:03 +02003183 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreaua4428bd2018-12-22 18:11:41 +01003184 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02003185 break;
3186
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02003187 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003188 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 +02003189 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003190 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02003191 /* only log if no other stream can report the error */
3192 sess_log(h2c->conn->owner);
3193 }
Willy Tarreaua3075282020-12-01 10:22:43 +01003194 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003195 break;
3196 }
3197
Christopher Fauletdd2a5622019-06-18 12:22:38 +02003198 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003199 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
3200 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
3201 * we read the pad length and drop it from the remaining
3202 * payload (one byte + the 9 remaining ones = 10 total
3203 * removed), so we have a frame payload starting after the
3204 * pad len. Flow controlled frames (DATA) also count the
3205 * padlen in the flow control, so it must be adjusted.
3206 */
3207 if (hdr.len < 1) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003208 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 +01003209 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003210 if (!(h2c->flags & H2_CF_IS_BACK))
3211 sess_log(h2c->conn->owner);
Willy Tarreaua3075282020-12-01 10:22:43 +01003212 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau3bf69182018-12-21 15:34:50 +01003213 goto fail;
3214 }
3215 hdr.len--;
3216
3217 if (b_data(&h2c->dbuf) < 10)
3218 break; // missing padlen
3219
3220 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3221
3222 if (padlen > hdr.len) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003223 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 +01003224 /* RFC7540#6.1 : pad length = length of
3225 * frame payload or greater => error.
3226 */
3227 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003228 if (!(h2c->flags & H2_CF_IS_BACK))
3229 sess_log(h2c->conn->owner);
Willy Tarreaua3075282020-12-01 10:22:43 +01003230 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau3bf69182018-12-21 15:34:50 +01003231 goto fail;
3232 }
3233
3234 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3235 h2c->rcvd_c++;
3236 h2c->rcvd_s++;
3237 }
3238 b_del(&h2c->dbuf, 1);
3239 }
3240 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003241
3242 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003243 h2c->dfl = hdr.len;
3244 h2c->dsi = hdr.sid;
3245 h2c->dft = hdr.ft;
3246 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003247 h2c->dpl = padlen;
Willy Tarreau73db4342019-09-25 07:28:44 +02003248 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 +02003249 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003250
3251 /* check for minimum basic frame format validity */
3252 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3253 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003254 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 +01003255 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003256 if (!(h2c->flags & H2_CF_IS_BACK))
3257 sess_log(h2c->conn->owner);
Willy Tarreaua3075282020-12-01 10:22:43 +01003258 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003259 goto fail;
3260 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003261 }
3262
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003263 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3264 * H2_CS_FRAME_P indicates an incomplete previous operation
3265 * (most often the first attempt) and requires some validity
3266 * checks for the frame and the current state. The two other
3267 * ones are set after completion (or abortion) and must skip
3268 * validity checks.
3269 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003270 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3271
Willy Tarreau567beb82018-12-18 16:52:44 +01003272 if (tmp_h2s != h2s && h2s && h2s->cs &&
3273 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003274 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003275 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003276 (h2s->flags & H2_SF_ES_RCVD) ||
3277 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003278 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003279 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 +01003280 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003281 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003282 }
3283 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003284
Willy Tarreau63864812019-08-07 14:25:20 +02003285 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003286 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3287 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003288 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003289 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003290
Willy Tarreau7e98c052017-10-10 15:56:59 +02003291 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003292 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003293 if (h2c->st0 == H2_CS_FRAME_P) {
3294 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003295 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003296 }
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +01003297 HA_ATOMIC_ADD(&h2c->px_counters->settings_rcvd, 1);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003298
Willy Tarreau7838a792019-08-12 18:42:03 +02003299 if (h2c->st0 == H2_CS_FRAME_A) {
3300 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 +02003301 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003302 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003303 break;
3304
Willy Tarreaucf68c782017-10-10 17:11:41 +02003305 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003306 if (h2c->st0 == H2_CS_FRAME_P) {
3307 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003308 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003309 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003310
Willy Tarreau7838a792019-08-12 18:42:03 +02003311 if (h2c->st0 == H2_CS_FRAME_A) {
3312 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 +02003313 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003314 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003315 break;
3316
Willy Tarreau26f95952017-07-27 17:18:30 +02003317 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003318 if (h2c->st0 == H2_CS_FRAME_P) {
3319 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 +02003320 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003321 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003322 break;
3323
Willy Tarreau61290ec2017-10-17 08:19:21 +02003324 case H2_FT_CONTINUATION:
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05003325 /* RFC7540#6.10: CONTINUATION may only be preceded by
Willy Tarreauea18f862018-12-22 20:19:26 +01003326 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3327 * frames' parsers consume all following CONTINUATION
3328 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003329 */
Willy Tarreau5dd36ac2020-12-01 10:24:29 +01003330 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 +01003331 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003332 if (!(h2c->flags & H2_CF_IS_BACK))
3333 sess_log(h2c->conn->owner);
Willy Tarreaua3075282020-12-01 10:22:43 +01003334 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreauea18f862018-12-22 20:19:26 +01003335 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003336
Willy Tarreau13278b42017-10-13 19:23:14 +02003337 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003338 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003339 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003340 if (h2c->flags & H2_CF_IS_BACK)
3341 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3342 else
3343 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003344 if (tmp_h2s) {
3345 h2s = tmp_h2s;
3346 ret = 1;
3347 }
3348 }
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +01003349 HA_ATOMIC_ADD(&h2c->px_counters->headers_rcvd, 1);
Willy Tarreau13278b42017-10-13 19:23:14 +02003350 break;
3351
Willy Tarreau454f9052017-10-26 19:40:35 +02003352 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003353 if (h2c->st0 == H2_CS_FRAME_P) {
3354 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Christopher Fauletfac0f8f2020-12-07 18:27:03 +01003355 ret = h2c_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003356 }
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +01003357 HA_ATOMIC_ADD(&h2c->px_counters->data_rcvd, 1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003358
Willy Tarreau7838a792019-08-12 18:42:03 +02003359 if (h2c->st0 == H2_CS_FRAME_A) {
3360 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 +02003361 ret = h2c_send_strm_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003362 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003363 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003364
Willy Tarreau92153fc2017-12-03 19:46:19 +01003365 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003366 if (h2c->st0 == H2_CS_FRAME_P) {
3367 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003368 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003369 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003370 break;
3371
Willy Tarreaucd234e92017-08-18 10:59:39 +02003372 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003373 if (h2c->st0 == H2_CS_FRAME_P) {
3374 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 +02003375 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003376 }
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +01003377 HA_ATOMIC_ADD(&h2c->px_counters->rst_stream_rcvd, 1);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003378 break;
3379
Willy Tarreaue96b0922017-10-30 00:28:29 +01003380 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003381 if (h2c->st0 == H2_CS_FRAME_P) {
3382 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003383 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003384 }
Amaury Denoyelle2dec1eb2020-10-27 17:16:02 +01003385 HA_ATOMIC_ADD(&h2c->px_counters->goaway_rcvd, 1);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003386 break;
3387
Willy Tarreau1c661982017-10-30 13:52:01 +01003388 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003389 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003390 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003391 /* drop frames that we ignore. They may be larger than
3392 * the buffer so we drain all of their contents until
3393 * we reach the end.
3394 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003395 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3396 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003397 h2c->dfl -= ret;
3398 ret = h2c->dfl == 0;
3399 }
3400
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003401 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003402 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003403 if (h2s->st == H2_SS_ERROR) {
3404 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 +01003405 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003406 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003407
Willy Tarreau7838a792019-08-12 18:42:03 +02003408 if (h2c->st0 == H2_CS_FRAME_E) {
3409 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 +01003410 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003411 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003412
Willy Tarreau7e98c052017-10-10 15:56:59 +02003413 /* error or missing data condition met above ? */
Willy Tarreau7838a792019-08-12 18:42:03 +02003414 if (ret <= 0) {
3415 TRACE_DEVEL("insufficient data to proceed", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003416 if (h2c->flags & H2_CF_RCVD_SHUT)
3417 h2c->flags |= H2_CF_END_REACHED;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003418 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003419 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003420
3421 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaubba7a4d2020-09-18 07:41:28 +02003422 if (h2c->dfl)
3423 TRACE_DEVEL("skipping remaining frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
Christopher Faulet5112a602019-09-26 16:38:28 +02003424 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3425 b_del(&h2c->dbuf, ret);
3426 h2c->dfl -= ret;
3427 if (!h2c->dfl) {
3428 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3429 h2c->st0 = H2_CS_FRAME_H;
3430 h2c->dsi = -1;
3431 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003432 }
3433 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003434
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003435 if (h2c->rcvd_c > 0 &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003436 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) {
3437 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003438 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003439 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003440
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003441 done:
Willy Tarreau567beb82018-12-18 16:52:44 +01003442 if (h2s && h2s->cs &&
3443 (b_data(&h2s->rxbuf) ||
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003444 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003445 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003446 (h2s->flags & H2_SF_ES_RCVD) ||
3447 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003448 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003449 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 +01003450 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003451 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003452 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003453
Willy Tarreau7838a792019-08-12 18:42:03 +02003454 if (old_iw != h2c->miw) {
3455 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003456 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003457 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003458
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003459 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003460 out:
3461 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003462 return;
3463
3464 fail:
3465 /* we can go here on missing data, blocked response or error, but we
3466 * need to check if we've met a short read condition.
3467 */
3468 if (h2c->flags & H2_CF_RCVD_SHUT)
3469 h2c->flags |= H2_CF_END_REACHED;
3470 goto done;
Willy Tarreaubc933932017-10-09 16:21:43 +02003471}
3472
Willy Tarreau989539b2020-01-10 17:01:29 +01003473/* resume each h2s eligible for sending in list head <head> */
3474static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3475{
3476 struct h2s *h2s, *h2s_back;
3477
3478 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3479
3480 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3481 if (h2c->mws <= 0 ||
3482 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3483 h2c->st0 >= H2_CS_ERROR)
3484 break;
3485
3486 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003487
Willy Tarreaud9464162020-01-10 18:25:07 +01003488 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003489 continue;
3490
Willy Tarreau5723f292020-01-10 15:16:57 +01003491 /* If the sender changed his mind and unsubscribed, let's just
3492 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003493 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003494 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3495 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003496 LIST_DEL_INIT(&h2s->list);
3497 continue;
3498 }
3499
Willy Tarreauf96508a2020-01-10 11:12:48 +01003500 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003501 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003502 tasklet_wakeup(h2s->subs->tasklet);
3503 h2s->subs->events &= ~SUB_RETRY_SEND;
3504 if (!h2s->subs->events)
3505 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003506 }
3507 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3508 tasklet_wakeup(h2s->shut_tl);
3509 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003510 }
3511
3512 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3513}
3514
Willy Tarreaubc933932017-10-09 16:21:43 +02003515/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3516 * the end.
3517 */
3518static int h2_process_mux(struct h2c *h2c)
3519{
Willy Tarreau7838a792019-08-12 18:42:03 +02003520 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3521
Willy Tarreau01b44822018-10-03 14:26:37 +02003522 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3523 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3524 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3525 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003526 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003527 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003528 goto fail;
3529 }
3530 h2c->st0 = H2_CS_SETTINGS1;
3531 }
3532 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003533 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003534 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003535 }
3536
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003537 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003538 if (h2c->rcvd_s > 0 &&
3539 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3540 h2c_send_strm_wu(h2c) < 0)
3541 goto fail;
3542
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003543 if (h2c->rcvd_c > 0 &&
3544 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3545 h2c_send_conn_wu(h2c) < 0)
3546 goto fail;
3547
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003548 /* First we always process the flow control list because the streams
3549 * waiting there were already elected for immediate emission but were
3550 * blocked just on this.
3551 */
Willy Tarreau989539b2020-01-10 17:01:29 +01003552 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3553 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003554
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003555 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003556 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003557 if (h2c->st0 == H2_CS_ERROR) {
3558 if (h2c->max_id >= 0) {
3559 h2c_send_goaway_error(h2c, NULL);
3560 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003561 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003562 }
3563
3564 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3565 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003566 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003567 done:
3568 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3569 return 1;
3570 out0:
3571 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3572 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003573}
3574
Willy Tarreau62f52692017-10-08 23:01:42 +02003575
Willy Tarreau479998a2018-11-18 06:30:59 +01003576/* Attempt to read data, and subscribe if none available.
3577 * The function returns 1 if data has been received, otherwise zero.
3578 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003579static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003580{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003581 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003582 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003583 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003584 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003585
Willy Tarreau7838a792019-08-12 18:42:03 +02003586 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3587
3588 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3589 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003590 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003591 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003592
Willy Tarreau7838a792019-08-12 18:42:03 +02003593 if (!h2_recv_allowed(h2c)) {
3594 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003595 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003596 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003597
Willy Tarreau44e973f2018-03-01 17:49:30 +01003598 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003599 if (!buf) {
3600 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003601 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003602 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003603 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003604
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003605 if (h2c->flags & H2_CF_RCVD_SHUT) {
3606 TRACE_DEVEL("leaving on rcvd_shut", H2_EV_H2C_RECV, h2c->conn);
3607 return 0;
3608 }
3609
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003610 b_realign_if_empty(buf);
3611 if (!b_data(buf)) {
3612 /* try to pre-align the buffer like the
3613 * rxbufs will be to optimize memory copies. We'll make
3614 * sure that the frame header lands at the end of the
3615 * HTX block to alias it upon recv. We cannot use the
3616 * head because rcv_buf() will realign the buffer if
3617 * it's empty. Thus we cheat and pretend we already
3618 * have a few bytes there.
3619 */
3620 max = buf_room_for_htx_data(buf) + 9;
3621 buf->head = sizeof(struct htx) - 9;
3622 }
3623 else
3624 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003625
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003626 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003627
Willy Tarreau7838a792019-08-12 18:42:03 +02003628 if (max && !ret && h2_recv_allowed(h2c)) {
Willy Tarreau3d4631f2021-01-20 10:53:13 +01003629 if (conn_xprt_read0_pending(h2c->conn)) {
3630 TRACE_DATA("received read0", H2_EV_H2C_RECV, h2c->conn);
3631 h2c->flags |= H2_CF_RCVD_SHUT;
3632 } else {
3633 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
3634 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
3635 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003636 } else if (ret)
Willy Tarreau022e5e52020-09-10 09:33:15 +02003637 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn, 0, 0, (void*)(long)ret);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003638
Olivier Houcharda1411e62018-08-17 18:42:48 +02003639 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003640 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02003641 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard46677732018-11-29 17:06:17 +01003642 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003643 }
3644
Willy Tarreau7838a792019-08-12 18:42:03 +02003645 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003646 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003647 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003648 }
3649
3650 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003651 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003652}
3653
Willy Tarreau479998a2018-11-18 06:30:59 +01003654/* Try to send data if possible.
3655 * The function returns 1 if data have been sent, otherwise zero.
3656 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003657static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003658{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003659 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003660 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003661 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003662
Willy Tarreau7838a792019-08-12 18:42:03 +02003663 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003664
Willy Tarreau7838a792019-08-12 18:42:03 +02003665 if (conn->flags & CO_FL_ERROR) {
3666 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
3667 return 1;
3668 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003669
Willy Tarreau911db9b2020-01-23 16:27:54 +01003670 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreaua2af5122017-10-09 11:56:46 +02003671 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003672 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003673 }
3674
Willy Tarreaubc933932017-10-09 16:21:43 +02003675 /* This loop is quite simple : it tries to fill as much as it can from
3676 * pending streams into the existing buffer until it's reportedly full
3677 * or the end of send requests is reached. Then it tries to send this
3678 * buffer's contents out, marks it not full if at least one byte could
3679 * be sent, and tries again.
3680 *
3681 * The snd_buf() function normally takes a "flags" argument which may
3682 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3683 * data immediately comes and CO_SFL_STREAMER to indicate that the
3684 * connection is streaming lots of data (used to increase TLS record
3685 * size at the expense of latency). The former can be sent any time
3686 * there's a buffer full flag, as it indicates at least one stream
3687 * attempted to send and failed so there are pending data. An
3688 * alternative would be to set it as long as there's an active stream
3689 * but that would be problematic for ACKs until we have an absolute
3690 * guarantee that all waiters have at least one byte to send. The
3691 * latter should possibly not be set for now.
3692 */
3693
3694 done = 0;
3695 while (!done) {
3696 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003697 unsigned int released = 0;
3698 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02003699
3700 /* fill as much as we can into the current buffer */
3701 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3702 done = h2_process_mux(h2c);
3703
Olivier Houchard2b094432019-01-29 18:28:36 +01003704 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003705 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003706
Christopher Faulet9a3d3fc2020-10-22 16:24:58 +02003707 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
3708 (h2c->st0 == H2_CS_ERROR2) || (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02003709 break;
3710
3711 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
3712 flags |= CO_SFL_MSG_MORE;
3713
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003714 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3715 if (b_data(buf)) {
3716 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003717 if (!ret) {
3718 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003719 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003720 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003721 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02003722 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn, 0, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003723 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003724 if (b_data(buf)) {
3725 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003726 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003727 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003728 }
3729 b_free(buf);
3730 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003731 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003732
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003733 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003734 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003735
Willy Tarreaubc933932017-10-09 16:21:43 +02003736 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02003737 if (sent)
3738 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02003739 }
3740
Willy Tarreaua2af5122017-10-09 11:56:46 +02003741 if (conn->flags & CO_FL_SOCK_WR_SH) {
3742 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02003743 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003744 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003745 /* We're not full anymore, so we can wake any task that are waiting
3746 * for us.
3747 */
Willy Tarreau989539b2020-01-10 17:01:29 +01003748 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H)
3749 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01003750
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003751 /* We're done, no more to send */
Willy Tarreau7838a792019-08-12 18:42:03 +02003752 if (!br_data(h2c->mbuf)) {
3753 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003754 return sent;
Willy Tarreau7838a792019-08-12 18:42:03 +02003755 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003756schedule:
Willy Tarreau7838a792019-08-12 18:42:03 +02003757 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
3758 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003759 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003760 }
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003761
Willy Tarreau7838a792019-08-12 18:42:03 +02003762 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003763 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003764}
3765
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003766/* this is the tasklet referenced in h2c->wait_event.tasklet */
Willy Tarreau691d5032021-01-20 14:55:01 +01003767struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003768{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003769 struct connection *conn;
3770 struct tasklet *tl = (struct tasklet *)t;
3771 int conn_in_list;
3772 struct h2c *h2c;
Olivier Houchard7505f942018-08-21 18:10:44 +02003773 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003774
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003775
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003776 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003777 if (t->context == NULL) {
3778 /* The connection has been taken over by another thread,
3779 * we're no longer responsible for it, so just free the
3780 * tasklet, and do nothing.
3781 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003782 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003783 tasklet_free(tl);
Willy Tarreau38468772020-06-28 00:31:13 +02003784 goto leave;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003785 }
3786 h2c = ctx;
3787 conn = h2c->conn;
3788
3789 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3790
3791 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3792
3793 /* Remove the connection from the list, to be sure nobody attempts
3794 * to use it while we handle the I/O events
3795 */
3796 if (conn_in_list)
3797 MT_LIST_DEL(&conn->list);
3798
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003799 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau7838a792019-08-12 18:42:03 +02003800
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003801 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003802 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003803 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003804 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003805 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003806 ret = h2_process(h2c);
3807
3808 /* If we were in an idle list, we want to add it back into it,
3809 * unless h2_process() returned -1, which mean it has destroyed
3810 * the connection (testing !ret is enough, if h2_process() wasn't
3811 * called then ret will be 0 anyway.
3812 */
3813 if (!ret && conn_in_list) {
3814 struct server *srv = objt_server(conn->target);
3815
3816 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02003817 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003818 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02003819 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003820 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003821
Willy Tarreau38468772020-06-28 00:31:13 +02003822leave:
Willy Tarreau7838a792019-08-12 18:42:03 +02003823 TRACE_LEAVE(H2_EV_H2C_WAKE);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003824 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003825}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003826
Willy Tarreau62f52692017-10-08 23:01:42 +02003827/* callback called on any event by the connection handler.
3828 * It applies changes and returns zero, or < 0 if it wants immediate
3829 * destruction of the connection (which normally doesn not happen in h2).
3830 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003831static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003832{
Olivier Houchard7505f942018-08-21 18:10:44 +02003833 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003834
Willy Tarreau7838a792019-08-12 18:42:03 +02003835 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3836
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003837 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003838 h2_process_demux(h2c);
3839
3840 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003841 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003842
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003843 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003844 h2c->flags &= ~H2_CF_DEM_DFULL;
3845 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003846 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003847
Willy Tarreaub1e600c2020-10-13 18:09:15 +02003848 if (unlikely(h2c->proxy->disabled) && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003849 /* frontend is stopping, reload likely in progress, let's try
3850 * to announce a graceful shutdown if not yet done. We don't
3851 * care if it fails, it will be tried again later.
3852 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003853 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
Willy Tarreau8ec14062017-12-30 18:08:13 +01003854 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3855 if (h2c->last_sid < 0)
3856 h2c->last_sid = (1U << 31) - 1;
3857 h2c_send_goaway_error(h2c, NULL);
3858 }
3859 }
3860
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003861 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003862 * If we received early data, and the handshake is done, wake
3863 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003864 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003865 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003866 (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 +01003867 struct eb32_node *node;
3868 struct h2s *h2s;
3869
3870 h2c->flags |= H2_CF_WAIT_FOR_HS;
3871 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3872
3873 while (node) {
3874 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003875 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003876 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003877 node = eb32_next(node);
3878 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003879 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003880
Christopher Fauletaade4ed2020-10-08 15:38:41 +02003881 if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003882 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3883 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3884 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003885 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003886
3887 if (eb_is_empty(&h2c->streams_by_id)) {
3888 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003889 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003890 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003891 return -1;
3892 }
Willy Tarreau4481e262019-10-31 15:36:30 +01003893
3894 /* connections in error must be removed from the idle lists */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003895 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau4481e262019-10-31 15:36:30 +01003896 MT_LIST_DEL((struct mt_list *)&conn->list);
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003897 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau4481e262019-10-31 15:36:30 +01003898 }
3899 else if (h2c->st0 == H2_CS_ERROR) {
3900 /* connections in error must be removed from the idle lists */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003901 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau4481e262019-10-31 15:36:30 +01003902 MT_LIST_DEL((struct mt_list *)&conn->list);
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003903 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003904 }
3905
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003906 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003907 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003908
Olivier Houchard53216e72018-10-10 15:46:36 +02003909 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3910 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3911 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003912 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003913 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3914 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003915 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003916
Willy Tarreau3f133572017-10-31 19:21:06 +01003917 if (h2c->task) {
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02003918 if (h2c_may_expire(h2c))
3919 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3920 else
3921 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003922 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003923 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003924
Olivier Houchard7505f942018-08-21 18:10:44 +02003925 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003926 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003927 return 0;
3928}
3929
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003930/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003931static int h2_wake(struct connection *conn)
3932{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003933 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02003934 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003935
Willy Tarreau7838a792019-08-12 18:42:03 +02003936 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3937 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01003938 if (ret >= 0)
3939 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003940 TRACE_LEAVE(H2_EV_H2C_WAKE);
3941 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003942}
3943
Willy Tarreauea392822017-10-31 10:02:25 +01003944/* Connection timeout management. The principle is that if there's no receipt
3945 * nor sending for a certain amount of time, the connection is closed. If the
3946 * MUX buffer still has lying data or is not allocatable, the connection is
3947 * immediately killed. If it's allocatable and empty, we attempt to send a
3948 * GOAWAY frame.
3949 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003950static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003951{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003952 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003953 int expired = tick_is_expired(t->expire, now_ms);
3954
Willy Tarreau7838a792019-08-12 18:42:03 +02003955 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
3956
Willy Tarreaubd42e922020-06-30 11:19:23 +02003957 if (h2c) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003958 /* Make sure nobody stole the connection from us */
3959 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3960
3961 /* Somebody already stole the connection from us, so we should not
3962 * free it, we just have to free the task.
3963 */
3964 if (!t->context) {
3965 h2c = NULL;
Willy Tarreau46ac7812020-07-04 07:16:18 +02003966 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003967 goto do_leave;
3968 }
3969
3970
Willy Tarreaubd42e922020-06-30 11:19:23 +02003971 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003972 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02003973 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
3974 return t;
3975 }
Willy Tarreauea392822017-10-31 10:02:25 +01003976
Willy Tarreaubd42e922020-06-30 11:19:23 +02003977 if (!h2c_may_expire(h2c)) {
3978 /* we do still have streams but all of them are idle, waiting
3979 * for the data layer, so we must not enforce the timeout here.
3980 */
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003981 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02003982 t->expire = TICK_ETERNITY;
3983 return t;
3984 }
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02003985
Willy Tarreaubd42e922020-06-30 11:19:23 +02003986 /* We're about to destroy the connection, so make sure nobody attempts
3987 * to steal it from us.
3988 */
Willy Tarreaubd42e922020-06-30 11:19:23 +02003989 if (h2c->conn->flags & CO_FL_LIST_MASK)
3990 MT_LIST_DEL(&h2c->conn->list);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003991
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003992 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreaubd42e922020-06-30 11:19:23 +02003993 }
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003994
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003995do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003996 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003997
3998 if (!h2c) {
3999 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02004000 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02004001 return NULL;
4002 }
4003
4004 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01004005 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02004006 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01004007
Willy Tarreau662fafc2019-05-26 09:43:07 +02004008 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01004009 /* don't even try to send a GOAWAY, the buffer is stuck */
4010 h2c->flags |= H2_CF_GOAWAY_FAILED;
4011 }
4012
4013 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01004014 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01004015 if (h2c_send_goaway_error(h2c, NULL) <= 0)
4016 h2c->flags |= H2_CF_GOAWAY_FAILED;
4017
Willy Tarreau662fafc2019-05-26 09:43:07 +02004018 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004019 unsigned int released = 0;
4020 struct buffer *buf;
4021
4022 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
4023 if (b_data(buf)) {
4024 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
4025 if (!ret)
4026 break;
4027 b_del(buf, ret);
4028 if (b_data(buf))
4029 break;
4030 b_free(buf);
4031 released++;
4032 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02004033 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02004034
4035 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02004036 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02004037 }
Willy Tarreauea392822017-10-31 10:02:25 +01004038
Willy Tarreau4481e262019-10-31 15:36:30 +01004039 /* in any case this connection must not be considered idle anymore */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02004040 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau4481e262019-10-31 15:36:30 +01004041 MT_LIST_DEL((struct mt_list *)&h2c->conn->list);
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02004042 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau4481e262019-10-31 15:36:30 +01004043
Willy Tarreau0975f112018-03-29 15:22:59 +02004044 /* either we can release everything now or it will be done later once
4045 * the last stream closes.
4046 */
4047 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02004048 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01004049
Willy Tarreau7838a792019-08-12 18:42:03 +02004050 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01004051 return NULL;
4052}
4053
4054
Willy Tarreau62f52692017-10-08 23:01:42 +02004055/*******************************************/
4056/* functions below are used by the streams */
4057/*******************************************/
4058
4059/*
4060 * Attach a new stream to a connection
4061 * (Used for outgoing connections)
4062 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01004063static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02004064{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004065 struct conn_stream *cs;
4066 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004067 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004068
Willy Tarreau7838a792019-08-12 18:42:03 +02004069 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02004070 cs = cs_new(conn, conn->target);
Willy Tarreau7838a792019-08-12 18:42:03 +02004071 if (!cs) {
4072 TRACE_DEVEL("leaving on CS allocation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004073 return NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004074 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01004075 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004076 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004077 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004078 cs_free(cs);
4079 return NULL;
4080 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004081 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01004082 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02004083}
4084
Willy Tarreaufafd3982018-11-18 21:29:20 +01004085/* Retrieves the first valid conn_stream from this connection, or returns NULL.
4086 * We have to scan because we may have some orphan streams. It might be
4087 * beneficial to scan backwards from the end to reduce the likeliness to find
4088 * orphans.
4089 */
4090static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
4091{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004092 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01004093 struct h2s *h2s;
4094 struct eb32_node *node;
4095
4096 node = eb32_first(&h2c->streams_by_id);
4097 while (node) {
4098 h2s = container_of(node, struct h2s, by_id);
4099 if (h2s->cs)
4100 return h2s->cs;
4101 node = eb32_next(node);
4102 }
4103 return NULL;
4104}
4105
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004106static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4107{
4108 int ret = 0;
4109 struct h2c *h2c = conn->ctx;
4110
4111 switch (mux_ctl) {
4112 case MUX_STATUS:
4113 /* Only consider the mux to be ready if we're done with
4114 * the preface and settings, and we had no error.
4115 */
4116 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
4117 ret |= MUX_STATUS_READY;
4118 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02004119 case MUX_EXIT_STATUS:
4120 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004121 default:
4122 return -1;
4123 }
4124}
4125
Willy Tarreau62f52692017-10-08 23:01:42 +02004126/*
Olivier Houchard060ed432018-11-06 16:32:42 +01004127 * Destroy the mux and the associated connection, if it is no longer used
4128 */
Christopher Faulet73c12072019-04-08 11:23:22 +02004129static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01004130{
Christopher Faulet73c12072019-04-08 11:23:22 +02004131 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01004132
Willy Tarreau7838a792019-08-12 18:42:03 +02004133 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02004134 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02004135 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02004136 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01004137}
4138
4139/*
Willy Tarreau62f52692017-10-08 23:01:42 +02004140 * Detach the stream from the connection and possibly release the connection.
4141 */
4142static void h2_detach(struct conn_stream *cs)
4143{
Willy Tarreau60935142017-10-16 18:11:19 +02004144 struct h2s *h2s = cs->ctx;
4145 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01004146 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004147
Willy Tarreau7838a792019-08-12 18:42:03 +02004148 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
4149
Willy Tarreau60935142017-10-16 18:11:19 +02004150 cs->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004151 if (!h2s) {
4152 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02004153 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004154 }
Willy Tarreau60935142017-10-16 18:11:19 +02004155
Willy Tarreaud9464162020-01-10 18:25:07 +01004156 /* there's no txbuf so we're certain not to be able to send anything */
4157 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02004158
Olivier Houchardf502aca2018-12-14 19:42:40 +01004159 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02004160 h2c = h2s->h2c;
4161 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02004162 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01004163 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
4164 !h2_frt_has_too_many_cs(h2c)) {
4165 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02004166 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004167 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02004168 }
Willy Tarreau60935142017-10-16 18:11:19 +02004169
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004170 /* this stream may be blocked waiting for some data to leave (possibly
4171 * an ES or RST frame), so orphan it in this case.
4172 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02004173 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02004174 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01004175 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01004176 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004177 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004178 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02004179 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01004180
Willy Tarreau45f752e2017-10-30 15:44:59 +01004181 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
4182 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
4183 /* unblock the connection if it was blocked on this
4184 * stream.
4185 */
4186 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
4187 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004188 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01004189 }
4190
Willy Tarreau71049cc2018-03-28 13:56:39 +02004191 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02004192
Christopher Faulet9b79a102019-07-15 11:22:56 +02004193 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01004194 if (!(h2c->conn->flags &
4195 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004196 if (h2c->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02004197 /* Add the connection in the session server list, if not already done */
4198 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
4199 h2c->conn->owner = NULL;
4200 if (eb_is_empty(&h2c->streams_by_id)) {
4201 h2c->conn->mux->destroy(h2c);
4202 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4203 return;
Christopher Fauletc5579d12020-07-01 15:45:41 +02004204 }
4205 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02004206 if (eb_is_empty(&h2c->streams_by_id)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004207 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
4208 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
4209 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houchard351411f2018-12-27 17:20:54 +01004210 return;
4211 }
4212 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004213 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004214 else {
4215 if (eb_is_empty(&h2c->streams_by_id)) {
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004216 /* If the connection is owned by the session, first remove it
4217 * from its list
4218 */
4219 if (h2c->conn->owner) {
4220 session_unown_conn(h2c->conn->owner, h2c->conn);
4221 h2c->conn->owner = NULL;
4222 }
4223
Olivier Houcharddc2f2752020-02-13 19:12:07 +01004224 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01004225 /* The server doesn't want it, let's kill the connection right away */
4226 h2c->conn->mux->destroy(h2c);
4227 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
4228 return;
4229 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01004230 /* At this point, the connection has been added to the
4231 * server idle list, so another thread may already have
4232 * hijacked it, so we can't do anything with it.
4233 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01004234 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
4235 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01004236
Olivier Houchard8a786902018-12-15 16:05:40 +01004237 }
Christopher Fauletc5579d12020-07-01 15:45:41 +02004238 else if (MT_LIST_ISEMPTY(&h2c->conn->list) &&
Amaury Denoyelle6b8daef2020-10-14 18:17:10 +02004239 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target) &&
4240 !LIST_ADDED(&h2c->conn->session_list)) {
Christopher Fauletc5579d12020-07-01 15:45:41 +02004241 LIST_ADD(&__objt_server(h2c->conn->target)->available_conns[tid], mt_list_to_list(&h2c->conn->list));
4242 }
Olivier Houchard8a786902018-12-15 16:05:40 +01004243 }
4244 }
4245 }
4246
Willy Tarreaue323f342018-03-28 13:51:45 +02004247 /* We don't want to close right now unless we're removing the
4248 * last stream, and either the connection is in error, or it
4249 * reached the ID already specified in a GOAWAY frame received
4250 * or sent (as seen by last_sid >= 0).
4251 */
Olivier Houchard7a977432019-03-21 15:47:13 +01004252 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02004253 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02004254 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02004255 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02004256 }
4257 else if (h2c->task) {
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02004258 if (h2c_may_expire(h2c))
4259 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
4260 else
4261 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02004262 task_queue(h2c->task);
Willy Tarreau7838a792019-08-12 18:42:03 +02004263 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02004264 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004265 else
4266 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02004267}
4268
Willy Tarreau88bdba32019-05-13 18:17:53 +02004269/* Performs a synchronous or asynchronous shutr(). */
4270static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004271{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004272 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004273
Willy Tarreauf983d002019-05-14 10:40:21 +02004274 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004275 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004276
Willy Tarreau7838a792019-08-12 18:42:03 +02004277 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4278
Willy Tarreau18059042019-01-31 19:12:48 +01004279 /* a connstream may require us to immediately kill the whole connection
4280 * for example because of a "tcp-request content reject" rule that is
4281 * normally used to limit abuse. In this case we schedule a goaway to
4282 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004283 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004284 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004285 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004286 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004287 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4288 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4289 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004290 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4291 /* Nothing was never sent for this stream, so reset with
4292 * REFUSED_STREAM error to let the client retry the
4293 * request.
4294 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004295 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004296 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4297 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004298 else {
4299 /* a final response was already provided, we don't want this
4300 * stream anymore. This may happen when the server responds
4301 * before the end of an upload and closes quickly (redirect,
4302 * deny, ...)
4303 */
4304 h2s_error(h2s, H2_ERR_CANCEL);
4305 }
Willy Tarreau18059042019-01-31 19:12:48 +01004306
Willy Tarreau90c32322017-11-24 08:00:30 +01004307 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004308 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004309 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004310
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004311 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004312 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004313 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004314 done:
4315 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004316 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004317 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004318add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004319 /* Let the handler know we want to shutr, and add ourselves to the
4320 * most relevant list if not yet done. h2_deferred_shut() will be
4321 * automatically called via the shut_tl tasklet when there's room
4322 * again.
4323 */
4324 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004325 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004326 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004327 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004328 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004329 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004330 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004331 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004332 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004333}
4334
Willy Tarreau88bdba32019-05-13 18:17:53 +02004335/* Performs a synchronous or asynchronous shutw(). */
4336static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004337{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004338 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004339
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004340 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004341 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004342
Willy Tarreau7838a792019-08-12 18:42:03 +02004343 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4344
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004345 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01004346 /* we can cleanly close using an empty data frame only after headers */
4347
4348 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4349 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004350 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004351
4352 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004353 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004354 else
4355 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004356 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004357 /* a connstream may require us to immediately kill the whole connection
4358 * for example because of a "tcp-request content reject" rule that is
4359 * normally used to limit abuse. In this case we schedule a goaway to
4360 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004361 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004362 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004363 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004364 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004365 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4366 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4367 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004368 else {
4369 /* Nothing was never sent for this stream, so reset with
4370 * REFUSED_STREAM error to let the client retry the
4371 * request.
4372 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004373 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004374 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4375 }
Willy Tarreau18059042019-01-31 19:12:48 +01004376
Willy Tarreau90c32322017-11-24 08:00:30 +01004377 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004378 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004379 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004380
Willy Tarreau00dd0782018-03-01 16:31:34 +01004381 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004382 }
4383
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004384 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004385 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004386
4387 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4388
Willy Tarreau88bdba32019-05-13 18:17:53 +02004389 done:
4390 h2s->flags &= ~H2_SF_WANT_SHUTW;
4391 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004392
4393 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004394 /* Let the handler know we want to shutw, and add ourselves to the
4395 * most relevant list if not yet done. h2_deferred_shut() will be
4396 * automatically called via the shut_tl tasklet when there's room
4397 * again.
4398 */
4399 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004400 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004401 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004402 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004403 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004404 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004405 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004406 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004407 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004408}
4409
Willy Tarreau5723f292020-01-10 15:16:57 +01004410/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004411 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4412 * and prevented the last frame from being emitted.
4413 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004414static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
4415{
4416 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004417 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004418
Willy Tarreau7838a792019-08-12 18:42:03 +02004419 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4420
Willy Tarreau5723f292020-01-10 15:16:57 +01004421 if (h2s->flags & H2_SF_NOTIFIED) {
4422 /* some data processing remains to be done first */
4423 goto end;
4424 }
4425
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004426 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004427 h2_do_shutw(h2s);
4428
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004429 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004430 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004431
Willy Tarreau88bdba32019-05-13 18:17:53 +02004432 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004433 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004434 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01004435
Willy Tarreau88bdba32019-05-13 18:17:53 +02004436 if (!h2s->cs) {
4437 h2s_destroy(h2s);
4438 if (h2c_is_dead(h2c))
4439 h2_release(h2c);
4440 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004441 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004442 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004443 TRACE_LEAVE(H2_EV_STRM_SHUT);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004444 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02004445}
4446
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004447/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004448static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
4449{
4450 struct h2s *h2s = cs->ctx;
4451
Willy Tarreau7838a792019-08-12 18:42:03 +02004452 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004453 if (cs->flags & CS_FL_KILL_CONN)
4454 h2s->flags |= H2_SF_KILL_CONN;
4455
Willy Tarreau7838a792019-08-12 18:42:03 +02004456 if (mode)
4457 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004458
Willy Tarreau7838a792019-08-12 18:42:03 +02004459 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004460}
4461
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004462/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004463static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
4464{
4465 struct h2s *h2s = cs->ctx;
4466
Willy Tarreau7838a792019-08-12 18:42:03 +02004467 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004468 if (cs->flags & CS_FL_KILL_CONN)
4469 h2s->flags |= H2_SF_KILL_CONN;
4470
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004471 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004472 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004473}
4474
Christopher Faulet9b79a102019-07-15 11:22:56 +02004475/* Decode the payload of a HEADERS frame and produce the HTX request or response
4476 * depending on the connection's side. Returns a positive value on success, a
4477 * negative value on failure, or 0 if it couldn't proceed. May report connection
4478 * errors in h2c->errcode if the frame is non-decodable and the connection
4479 * unrecoverable. In absence of connection error when a failure is reported, the
4480 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004481 *
4482 * The function may fold CONTINUATION frames into the initial HEADERS frame
4483 * by removing padding and next frame header, then moving the CONTINUATION
4484 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4485 * leaving a hole between the main frame and the beginning of the next one.
4486 * The possibly remaining incomplete or next frame at the end may be moved
4487 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4488 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4489 *
4490 * A buffer at the beginning of processing may look like this :
4491 *
4492 * ,---.---------.-----.--------------.--------------.------.---.
4493 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4494 * `---^---------^-----^--------------^--------------^------^---'
4495 * | | <-----> | |
4496 * area | dpl | wrap
4497 * |<--------------> |
4498 * | dfl |
4499 * |<-------------------------------------------------->|
4500 * head data
4501 *
4502 * Padding is automatically overwritten when folding, participating to the
4503 * hole size after dfl :
4504 *
4505 * ,---.------------------------.-----.--------------.------.---.
4506 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4507 * `---^------------------------^-----^--------------^------^---'
4508 * | | <-----> | |
4509 * area | hole | wrap
4510 * |<-----------------------> |
4511 * | dfl |
4512 * |<-------------------------------------------------->|
4513 * head data
4514 *
4515 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4516 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4517 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004518 *
4519 * The <flags> field must point to either the stream's flags or to a copy of it
4520 * so that the function can update the following flags :
4521 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004522 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004523 *
4524 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4525 * decoding, in order to detect if we're dealing with a headers or a trailers
4526 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004527 */
Amaury Denoyelle74162742020-12-11 17:53:05 +01004528static 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 +02004529{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004530 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004531 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004532 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004533 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004534 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004535 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004536 int flen; // header frame len
4537 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004538 int ret = 0;
4539 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004540 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004541
Willy Tarreau7838a792019-08-12 18:42:03 +02004542 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4543
Willy Tarreauea18f862018-12-22 20:19:26 +01004544next_frame:
4545 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4546 goto leave; // incomplete input frame
4547
4548 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4549 * this case, we'll try to paste it immediately after the initial
4550 * HEADERS frame payload and kill any possible padding. The initial
4551 * frame's length will be increased to represent the concatenation
4552 * of the two frames. The next frame is read from position <tlen>
4553 * and written at position <flen> (minus padding if some is present).
4554 */
4555 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4556 struct h2_fh hdr;
4557 int clen; // CONTINUATION frame's payload length
4558
Willy Tarreau7838a792019-08-12 18:42:03 +02004559 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 +01004560 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4561 /* no more data, the buffer may be full, either due to
4562 * too large a frame or because of too large a hole that
4563 * we're going to compact at the end.
4564 */
4565 goto leave;
4566 }
4567
4568 if (hdr.ft != H2_FT_CONTINUATION) {
4569 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004570 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 +01004571 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01004572 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreauea18f862018-12-22 20:19:26 +01004573 goto fail;
4574 }
4575
4576 if (hdr.sid != h2c->dsi) {
4577 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004578 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 +01004579 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01004580 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreauea18f862018-12-22 20:19:26 +01004581 goto fail;
4582 }
4583
4584 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4585 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004586 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 +01004587 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4588 goto fail;
4589 }
4590
4591 /* detect when we must stop aggragating frames */
4592 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4593
4594 /* Take as much as we can of the CONTINUATION frame's payload */
4595 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4596 if (clen > hdr.len)
4597 clen = hdr.len;
4598
4599 /* Move the frame's payload over the padding, hole and frame
4600 * header. At least one of hole or dpl is null (see diagrams
4601 * above). The hole moves after the new aggragated frame.
4602 */
4603 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
4604 h2c->dfl += clen - h2c->dpl;
4605 hole += h2c->dpl + 9;
4606 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004607 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 +01004608 goto next_frame;
4609 }
4610
4611 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004612
Willy Tarreau13278b42017-10-13 19:23:14 +02004613 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004614 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004615 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004616 copy = alloc_trash_chunk();
4617 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004618 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 +02004619 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4620 goto fail;
4621 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004622 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4623 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4624 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004625 }
4626
Willy Tarreau13278b42017-10-13 19:23:14 +02004627 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4628 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004629 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004630 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004631 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 +01004632 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01004633 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004634 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004635 }
4636
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004637 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004638 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 +01004639 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4640 goto fail;
4641 }
4642
Willy Tarreau13278b42017-10-13 19:23:14 +02004643 hdrs += 5; // stream dep = 4, weight = 1
4644 flen -= 5;
4645 }
4646
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004647 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004648 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 +01004649 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004650 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004651 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004652
Willy Tarreau937f7602018-02-26 15:22:17 +01004653 /* we can't retry a failed decompression operation so we must be very
4654 * careful not to take any risks. In practice the output buffer is
4655 * always empty except maybe for trailers, in which case we simply have
4656 * to wait for the upper layer to finish consuming what is available.
4657 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004658 htx = htx_from_buf(rxbuf);
4659 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004660 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 +02004661 h2c->flags |= H2_CF_DEM_SFULL;
4662 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004663 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004664
Willy Tarreau25919232019-01-03 14:48:18 +01004665 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004666 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4667 sizeof(list)/sizeof(list[0]), tmp);
4668 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004669 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 +01004670 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
4671 goto fail;
4672 }
4673
Willy Tarreau25919232019-01-03 14:48:18 +01004674 /* The PACK decompressor was updated, let's update the input buffer and
4675 * the parser's state to commit these changes and allow us to later
4676 * fail solely on the stream if needed.
4677 */
4678 b_del(&h2c->dbuf, h2c->dfl + hole);
4679 h2c->dfl = hole = 0;
4680 h2c->st0 = H2_CS_FRAME_H;
4681
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004682 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01004683 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Christopher Fauletd0db4232021-01-22 11:46:30 +01004684 msgf |= (*flags & H2_SF_BODY_TUNNEL) ? H2_MSGF_BODY_TUNNEL: 0;
Amaury Denoyelle74162742020-12-11 17:53:05 +01004685 /* If an Extended CONNECT has been sent on this stream, set message flag
4686 * to convert 200 response to 101 htx reponse */
4687 msgf |= (*flags & H2_SF_EXT_CONNECT_SENT) ? H2_MSGF_EXT_CONNECT: 0;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004688
Willy Tarreau88d138e2019-01-02 19:38:14 +01004689 if (*flags & H2_SF_HEADERS_RCVD)
4690 goto trailers;
4691
4692 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004693 if (h2c->flags & H2_CF_IS_BACK)
Amaury Denoyelle74162742020-12-11 17:53:05 +01004694 outlen = h2_make_htx_response(list, htx, &msgf, body_len, upgrade_protocol);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004695 else
4696 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004697
4698 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01004699 /* too large headers? this is a stream error only */
Willy Tarreau7838a792019-08-12 18:42:03 +02004700 TRACE_STATE("request headers too large", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004701 goto fail;
4702 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004703
Willy Tarreau174b06a2018-04-25 18:13:58 +02004704 if (msgf & H2_MSGF_BODY) {
4705 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004706 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004707 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004708 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004709 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02004710 }
Christopher Faulet7d247f02020-12-02 14:26:36 +01004711 if (msgf & H2_MSGF_BODYLESS_RSP)
4712 *flags |= H2_SF_BODYLESS_RESP;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004713
Christopher Fauletd0db4232021-01-22 11:46:30 +01004714 if (msgf & H2_MSGF_BODY_TUNNEL)
4715 *flags |= H2_SF_BODY_TUNNEL;
4716 else {
4717 /* Abort the tunnel attempt, if any */
4718 if (*flags & H2_SF_BODY_TUNNEL)
4719 *flags |= H2_SF_TUNNEL_ABRT;
4720 *flags &= ~H2_SF_BODY_TUNNEL;
4721 }
4722
Willy Tarreau88d138e2019-01-02 19:38:14 +01004723 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01004724 /* indicate that a HEADERS frame was received for this stream, except
4725 * for 1xx responses. For 1xx responses, another HEADERS frame is
4726 * expected.
4727 */
4728 if (!(msgf & H2_MSGF_RSP_1XX))
4729 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004730
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004731 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
4732 /* no more data are expected for this message */
4733 htx->flags |= HTX_FL_EOM;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004734 }
Willy Tarreau937f7602018-02-26 15:22:17 +01004735
Willy Tarreau86277d42019-01-02 15:36:11 +01004736 /* success */
4737 ret = 1;
4738
Willy Tarreau68dd9852017-07-03 14:44:26 +02004739 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01004740 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01004741 * move the remaining data over it.
4742 */
4743 if (hole) {
4744 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
4745 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
4746 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
4747 b_sub(&h2c->dbuf, hole);
4748 }
4749
Willy Tarreau97215ca2019-04-29 10:20:21 +02004750 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01004751 /* too large frames */
4752 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01004753 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01004754 }
4755
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004756 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004757 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02004758 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02004759 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01004760 return ret;
4761
Willy Tarreau68dd9852017-07-03 14:44:26 +02004762 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01004763 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02004764 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004765
4766 trailers:
4767 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01004768 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
4769 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02004770 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 +01004771 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua3075282020-12-01 10:22:43 +01004772 HA_ATOMIC_ADD(&h2c->px_counters->conn_proto_err, 1);
Willy Tarreau88d138e2019-01-02 19:38:14 +01004773 goto fail;
4774 }
4775
Christopher Faulet9b79a102019-07-15 11:22:56 +02004776 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02004777 if (h2_make_htx_trailers(list, htx) <= 0) {
4778 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 +02004779 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004780 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004781 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02004782}
4783
Christopher Faulet9b79a102019-07-15 11:22:56 +02004784/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004785 * parser state is automatically updated. Returns > 0 if it could completely
4786 * send the current frame, 0 if it couldn't complete, in which case
4787 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
4788 * DATA frame can return 0 as a valid result). Stream errors are reported in
4789 * h2s->errcode and connection errors in h2c->errcode. The caller must already
4790 * have checked the frame header and ensured that the frame was complete or the
4791 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02004792 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01004793static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02004794{
4795 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004796 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004797 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004798 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004799 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004800 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02004801
Willy Tarreau7838a792019-08-12 18:42:03 +02004802 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
4803
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004804 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02004805
Olivier Houchard638b7992018-08-16 15:41:52 +02004806 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004807 if (!csbuf) {
4808 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02004809 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 +01004810 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004811 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004812 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004813
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004814try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004815 flen = h2c->dfl - h2c->dpl;
4816 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01004817 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004818
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004819 if (flen > b_data(&h2c->dbuf)) {
4820 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004821 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01004822 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004823 }
4824
Christopher Faulet9b79a102019-07-15 11:22:56 +02004825 block = htx_free_data_space(htx);
4826 if (!block) {
4827 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004828 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 +02004829 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004830 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004831 if (flen > block)
4832 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004833
Christopher Faulet9b79a102019-07-15 11:22:56 +02004834 /* here, flen is the max we can copy into the output buffer */
4835 block = b_contig_data(&h2c->dbuf, 0);
4836 if (flen > block)
4837 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004838
Christopher Faulet9b79a102019-07-15 11:22:56 +02004839 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau022e5e52020-09-10 09:33:15 +02004840 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 +02004841
Christopher Faulet9b79a102019-07-15 11:22:56 +02004842 b_del(&h2c->dbuf, sent);
4843 h2c->dfl -= sent;
4844 h2c->rcvd_c += sent;
4845 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02004846
Christopher Faulet9b79a102019-07-15 11:22:56 +02004847 if (h2s->flags & H2_SF_DATA_CLEN) {
4848 h2s->body_len -= sent;
4849 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004850 }
4851
Christopher Faulet9b79a102019-07-15 11:22:56 +02004852 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01004853 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004854 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 +01004855 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004856 }
4857
Christopher Faulet9b79a102019-07-15 11:22:56 +02004858 goto try_again;
4859
Willy Tarreau4a28da12018-01-04 14:41:00 +01004860 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004861 /* here we're done with the frame, all the payload (except padding) was
4862 * transferred.
4863 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004864
Christopher Faulet5be651d2021-01-22 15:28:03 +01004865 if (!(h2s->flags & H2_SF_BODY_TUNNEL) && (h2c->dff & H2_F_DATA_END_STREAM)) {
4866 /* no more data are expected for this message. This add the EOM
4867 * flag but only on the response path or if no tunnel attempt
4868 * was aborted. Otherwise (request path + tunnel abrted), the
4869 * EOM was already reported.
4870 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004871 if ((h2c->flags & H2_CF_IS_BACK) || !(h2s->flags & H2_SF_TUNNEL_ABRT))
4872 htx->flags |= HTX_FL_EOM;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004873 }
4874
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004875 h2c->rcvd_c += h2c->dpl;
4876 h2c->rcvd_s += h2c->dpl;
4877 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004878 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02004879 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004880 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004881 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004882 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004883 if (htx)
4884 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004885 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004886 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004887}
4888
Willy Tarreau115e83b2018-12-01 19:17:53 +01004889/* Try to send a HEADERS frame matching HTX response present in HTX message
4890 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4891 * must check the stream's status to detect any error which might have happened
4892 * subsequently to a successful send. The htx blocks are automatically removed
4893 * from the message. The htx message is assumed to be valid since produced from
4894 * the internal code, hence it contains a start line, an optional series of
4895 * header blocks and an end of header, otherwise an invalid frame could be
4896 * emitted and the resulting htx message could be left in an inconsistent state.
4897 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004898static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004899{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004900 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004901 struct h2c *h2c = h2s->h2c;
4902 struct htx_blk *blk;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004903 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004904 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004905 struct htx_sl *sl;
4906 enum htx_blk_type type;
4907 int es_now = 0;
4908 int ret = 0;
4909 int hdr;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004910
Willy Tarreau7838a792019-08-12 18:42:03 +02004911 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4912
Willy Tarreau115e83b2018-12-01 19:17:53 +01004913 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004914 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004915 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004916 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004917 return 0;
4918 }
4919
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004920 /* get the start line (we do have one) and the rest of the headers,
4921 * that we dump starting at header 0 */
4922 sl = NULL;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004923 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004924 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004925 type = htx_get_blk_type(blk);
4926
4927 if (type == HTX_BLK_UNUSED)
4928 continue;
4929
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004930 if (type == HTX_BLK_EOH)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004931 break;
4932
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004933 if (type == HTX_BLK_HDR) {
4934 if (!sl) {
4935 TRACE_ERROR("no start-line", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
4936 goto fail;
4937 }
4938 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4939 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
4940 goto fail;
4941 }
4942
4943 list[hdr].n = htx_get_blk_name(htx, blk);
4944 list[hdr].v = htx_get_blk_value(htx, blk);
4945 hdr++;
4946 }
4947 else if (type == HTX_BLK_RES_SL) {
4948 if (sl) {
4949 TRACE_PROTO("multiple start-lines", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
4950 goto fail;
4951 }
4952 sl = htx_get_blk_ptr(htx, blk);
4953 h2s->status = sl->info.res.status;
Christopher Faulet7d247f02020-12-02 14:26:36 +01004954 if (h2s->status == 204 || h2s->status == 304)
4955 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004956 if (h2s->status < 100 || h2s->status > 999) {
4957 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
4958 goto fail;
4959 }
4960 else if (h2s->status == 101) {
4961 /* 101 responses are not supported in H2, so return a error (RFC7540#8.1.1) */
4962 TRACE_ERROR("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
4963 goto fail;
4964 }
4965 else if ((h2s->flags & H2_SF_BODY_TUNNEL) && h2s->status >= 300) {
4966 /* Abort the tunnel attempt */
4967 h2s->flags &= ~H2_SF_BODY_TUNNEL;
4968 h2s->flags |= H2_SF_TUNNEL_ABRT;
4969 }
4970 }
4971 else {
4972 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 +01004973 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004974 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004975 }
4976
4977 /* marker for end of headers */
4978 list[hdr].n = ist("");
4979
Willy Tarreau9c218e72019-05-26 10:08:28 +02004980 mbuf = br_tail(h2c->mbuf);
4981 retry:
4982 if (!h2_get_buf(h2c, mbuf)) {
4983 h2c->flags |= H2_CF_MUX_MALLOC;
4984 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004985 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 +02004986 return 0;
4987 }
4988
Willy Tarreau115e83b2018-12-01 19:17:53 +01004989 chunk_reset(&outbuf);
4990
4991 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004992 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4993 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004994 break;
4995 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004996 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004997 }
4998
4999 if (outbuf.size < 9)
5000 goto full;
5001
5002 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5003 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5004 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5005 outbuf.data = 9;
5006
5007 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01005008 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005009 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005010 goto realign_again;
5011 goto full;
5012 }
5013
5014 /* encode all headers, stop at empty name */
5015 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
5016 /* these ones do not exist in H2 and must be dropped. */
5017 if (isteq(list[hdr].n, ist("connection")) ||
5018 isteq(list[hdr].n, ist("proxy-connection")) ||
5019 isteq(list[hdr].n, ist("keep-alive")) ||
5020 isteq(list[hdr].n, ist("upgrade")) ||
5021 isteq(list[hdr].n, ist("transfer-encoding")))
5022 continue;
5023
Christopher Faulet86d144c2019-08-14 16:32:25 +02005024 /* Skip all pseudo-headers */
5025 if (*(list[hdr].n.ptr) == ':')
5026 continue;
5027
Willy Tarreau115e83b2018-12-01 19:17:53 +01005028 if (isteq(list[hdr].n, ist("")))
5029 break; // end
5030
5031 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
5032 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005033 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01005034 goto realign_again;
5035 goto full;
5036 }
5037 }
5038
Willy Tarreaucb985a42019-10-07 16:56:34 +02005039 /* update the frame's size */
5040 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5041
5042 if (outbuf.data > h2c->mfs + 9) {
5043 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5044 /* output full */
5045 if (b_space_wraps(mbuf))
5046 goto realign_again;
5047 goto full;
5048 }
5049 }
5050
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005051 /* remove all header blocks including the EOH and compute the
5052 * corresponding size.
Willy Tarreau115e83b2018-12-01 19:17:53 +01005053 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005054 ret = 0;
5055 blk = htx_get_head_blk(htx);
5056 while (blk) {
5057 type = htx_get_blk_type(blk);
5058 ret += htx_get_blksz(blk);
5059 blk = htx_remove_blk(htx, blk);
5060 /* The removed block is the EOH */
5061 if (type == HTX_BLK_EOH)
5062 break;
Christopher Faulet5be651d2021-01-22 15:28:03 +01005063 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005064
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005065 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) {
5066 /* Response already closed: add END_STREAM */
5067 es_now = 1;
5068 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005069 else if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx) && h2s->status >= 200) {
5070 /* EOM+empty: we may need to add END_STREAM except for 1xx
Christopher Faulet991febd2020-12-02 15:17:31 +01005071 * responses and tunneled response.
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005072 */
Christopher Faulet991febd2020-12-02 15:17:31 +01005073 if (!(h2s->flags & H2_SF_BODY_TUNNEL) || h2s->status >= 300)
5074 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005075 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01005076
Willy Tarreau115e83b2018-12-01 19:17:53 +01005077 if (es_now)
5078 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5079
5080 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02005081 TRACE_USER("sent H2 response", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005082 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01005083
5084 /* indicates the HEADERS frame was sent, except for 1xx responses. For
5085 * 1xx responses, another HEADERS frame is expected.
5086 */
Christopher Faulet89899422020-12-07 18:24:43 +01005087 if (h2s->status >= 200)
Christopher Faulet0b465482019-02-19 15:14:23 +01005088 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005089
Willy Tarreau115e83b2018-12-01 19:17:53 +01005090 if (es_now) {
5091 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005092 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 +01005093 if (h2s->st == H2_SS_OPEN)
5094 h2s->st = H2_SS_HLOC;
5095 else
5096 h2s_close(h2s);
5097 }
5098
5099 /* OK we could properly deliver the response */
Willy Tarreau115e83b2018-12-01 19:17:53 +01005100 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005101 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005102 return ret;
5103 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005104 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5105 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01005106 h2c->flags |= H2_CF_MUX_MFULL;
5107 h2s->flags |= H2_SF_BLK_MROOM;
5108 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005109 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 +01005110 goto end;
5111 fail:
5112 /* unparsable HTX messages, too large ones to be produced in the local
5113 * list etc go here (unrecoverable errors).
5114 */
5115 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5116 ret = 0;
5117 goto end;
5118}
5119
Willy Tarreau80739692018-10-05 11:35:57 +02005120/* Try to send a HEADERS frame matching HTX request present in HTX message
5121 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
5122 * must check the stream's status to detect any error which might have happened
5123 * subsequently to a successful send. The htx blocks are automatically removed
5124 * from the message. The htx message is assumed to be valid since produced from
5125 * the internal code, hence it contains a start line, an optional series of
5126 * header blocks and an end of header, otherwise an invalid frame could be
5127 * emitted and the resulting htx message could be left in an inconsistent state.
5128 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005129static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02005130{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005131 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02005132 struct h2c *h2c = h2s->h2c;
5133 struct htx_blk *blk;
Willy Tarreau80739692018-10-05 11:35:57 +02005134 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005135 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02005136 struct htx_sl *sl;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005137 struct ist meth, uri, auth, host = IST_NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005138 enum htx_blk_type type;
5139 int es_now = 0;
5140 int ret = 0;
5141 int hdr;
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005142 int extended_connect = 0;
Willy Tarreau80739692018-10-05 11:35:57 +02005143
Willy Tarreau7838a792019-08-12 18:42:03 +02005144 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5145
Willy Tarreau80739692018-10-05 11:35:57 +02005146 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005147 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02005148 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005149 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02005150 return 0;
5151 }
5152
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005153 /* get the start line (we do have one) and the rest of the headers,
5154 * that we dump starting at header 0 */
5155 sl = NULL;
Willy Tarreau80739692018-10-05 11:35:57 +02005156 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005157 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005158 type = htx_get_blk_type(blk);
5159
5160 if (type == HTX_BLK_UNUSED)
5161 continue;
5162
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005163 if (type == HTX_BLK_EOH)
Willy Tarreau80739692018-10-05 11:35:57 +02005164 break;
5165
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005166 if (type == HTX_BLK_REQ_SL) {
5167 if (sl) {
5168 TRACE_ERROR("multiple start-lines", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5169 goto fail;
5170 }
5171 sl = htx_get_blk_ptr(htx, blk);
5172 meth = htx_sl_req_meth(sl);
5173 uri = htx_sl_req_uri(sl);
Christopher Faulet7d247f02020-12-02 14:26:36 +01005174 if (sl->info.req.meth == HTTP_METH_HEAD)
5175 h2s->flags |= H2_SF_BODYLESS_RESP;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005176 if (unlikely(uri.len == 0)) {
5177 TRACE_ERROR("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5178 goto fail;
5179 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005180 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005181 else if (type == HTX_BLK_HDR) {
5182 if (!sl) {
5183 TRACE_ERROR("no start-line", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5184 goto fail;
5185 }
5186 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5187 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5188 goto fail;
5189 }
Willy Tarreau80739692018-10-05 11:35:57 +02005190
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005191 list[hdr].n = htx_get_blk_name(htx, blk);
5192 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02005193
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005194 /* Skip header if same name is used to add the server name */
5195 if ((h2c->flags & H2_CF_IS_BACK) && h2c->proxy->server_id_hdr_name &&
5196 isteq(list[hdr].n, ist2(h2c->proxy->server_id_hdr_name, h2c->proxy->server_id_hdr_len)))
5197 continue;
Christopher Faulet67d58092019-10-02 10:51:38 +02005198
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005199 /* Convert connection: upgrade to Extented connect from rfc 8441 */
5200 if (isteqi(list[hdr].n, ist("connection"))) {
5201 /* rfc 7230 #6.1 Connection = list of tokens */
5202 struct ist connection_ist = list[hdr].v;
5203 do {
5204 if (isteqi(iststop(connection_ist, ','),
5205 ist("upgrade"))) {
5206 h2s->flags |= (H2_SF_BODY_TUNNEL|H2_SF_EXT_CONNECT_SENT);
5207 sl->info.req.meth = HTTP_METH_CONNECT;
5208 meth = ist("CONNECT");
5209
5210 extended_connect = 1;
5211 break;
5212 }
5213
5214 connection_ist = istadv(istfind(connection_ist, ','), 1);
5215 } while (istlen(connection_ist));
5216 }
5217
5218 if (isteq(list[hdr].n, ist("upgrade"))) {
5219 /* rfc 7230 #6.7 Upgrade = list of protocols
5220 * rfc 8441 #4 Extended connect = :protocol is single-valued
5221 *
5222 * only first HTTP/1 protocol is preserved
5223 */
5224 const struct ist protocol = iststop(list[hdr].v, ',');
5225 /* upgrade_protocol field is 16 bytes long in h2s */
5226 istpad(h2s->upgrade_protocol, isttrim(protocol, 15));
5227 }
5228
5229 if (isteq(list[hdr].n, ist("host")))
5230 host = list[hdr].v;
5231
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005232 hdr++;
5233 }
5234 else {
5235 TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5236 goto fail;
5237 }
Willy Tarreau80739692018-10-05 11:35:57 +02005238 }
5239
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02005240 /* Now add the server name to a header (if requested) */
5241 if ((h2c->flags & H2_CF_IS_BACK) && h2c->proxy->server_id_hdr_name) {
5242 struct server *srv = objt_server(h2c->conn->target);
5243
5244 if (srv) {
5245 list[hdr].n = ist2(h2c->proxy->server_id_hdr_name, h2c->proxy->server_id_hdr_len);
5246 list[hdr].v = ist(srv->id);
5247 hdr++;
5248 }
5249 }
5250
Willy Tarreau80739692018-10-05 11:35:57 +02005251 /* marker for end of headers */
5252 list[hdr].n = ist("");
5253
Willy Tarreau9c218e72019-05-26 10:08:28 +02005254 mbuf = br_tail(h2c->mbuf);
5255 retry:
5256 if (!h2_get_buf(h2c, mbuf)) {
5257 h2c->flags |= H2_CF_MUX_MALLOC;
5258 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005259 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 +02005260 return 0;
5261 }
5262
Willy Tarreau80739692018-10-05 11:35:57 +02005263 chunk_reset(&outbuf);
5264
5265 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005266 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5267 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005268 break;
5269 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005270 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02005271 }
5272
5273 if (outbuf.size < 9)
5274 goto full;
5275
5276 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
5277 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
5278 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5279 outbuf.data = 9;
5280
5281 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01005282 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005283 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005284 goto realign_again;
5285 goto full;
5286 }
5287
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005288 auth = ist(NULL);
5289
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005290 /* RFC7540 #8.3: the CONNECT method must have :
5291 * - :authority set to the URI part (host:port)
5292 * - :method set to CONNECT
5293 * - :scheme and :path omitted
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005294 *
5295 * Note that this is not applicable in case of the Extended CONNECT
5296 * protocol from rfc 8441.
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005297 */
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005298 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT) && !extended_connect) {
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005299 auth = uri;
5300
5301 if (!hpack_encode_header(&outbuf, ist(":authority"), auth)) {
5302 /* output full */
5303 if (b_space_wraps(mbuf))
5304 goto realign_again;
5305 goto full;
5306 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005307 h2s->flags |= H2_SF_BODY_TUNNEL;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005308 } else {
5309 /* other methods need a :scheme. If an authority is known from
5310 * the request line, it must be sent, otherwise only host is
5311 * sent. Host is never sent as the authority.
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005312 *
5313 * This code is also applicable for Extended CONNECT protocol
5314 * from rfc 8441.
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005315 */
5316 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02005317
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005318 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
5319 /* the URI seems to start with a scheme */
5320 int len = 1;
5321
5322 while (len < uri.len && uri.ptr[len] != ':')
5323 len++;
5324
5325 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5326 /* make the uri start at the authority now */
5327 scheme.ptr = uri.ptr;
5328 scheme.len = len,
5329 uri.ptr += len + 3;
5330 uri.len -= len + 3;
5331
5332 /* find the auth part of the URI */
5333 auth.ptr = uri.ptr;
5334 auth.len = 0;
5335 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5336 auth.len++;
5337
5338 uri.ptr += auth.len;
5339 uri.len -= auth.len;
5340 }
5341 }
5342
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005343 /* For Extended CONNECT, the :authority must be present.
5344 * Use host value for it.
5345 */
5346 if (unlikely(extended_connect) && isttest(host))
5347 auth = host;
5348
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005349 if (!scheme.len) {
5350 /* no explicit scheme, we're using an origin-form URI,
5351 * probably from an H1 request transcoded to H2 via an
5352 * external layer, then received as H2 without authority.
5353 * So we have to look up the scheme from the HTX flags.
5354 * In such a case only http and https are possible, and
5355 * https is the default (sent by browsers).
5356 */
5357 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5358 scheme = ist("http");
5359 else
5360 scheme = ist("https");
5361 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005362
5363 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005364 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005365 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005366 goto realign_again;
5367 goto full;
5368 }
Willy Tarreau80739692018-10-05 11:35:57 +02005369
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005370 if (auth.len && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005371 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005372 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005373 goto realign_again;
5374 goto full;
5375 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005376
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005377 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5378 * be sent as '/' or '*'.
5379 */
5380 if (unlikely(!uri.len)) {
5381 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5382 uri = ist("*");
5383 else
5384 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005385 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005386
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005387 if (!hpack_encode_path(&outbuf, uri)) {
5388 /* output full */
5389 if (b_space_wraps(mbuf))
5390 goto realign_again;
5391 goto full;
5392 }
Amaury Denoyelle9bf95732020-12-11 17:53:06 +01005393
5394 /* encode the pseudo-header protocol from rfc8441 if using
5395 * Extended CONNECT method.
5396 */
5397 if (unlikely(extended_connect)) {
5398 const struct ist protocol = ist(h2s->upgrade_protocol);
5399 if (isttest(protocol)) {
5400 if (!hpack_encode_header(&outbuf,
5401 ist(":protocol"),
5402 protocol)) {
5403 /* output full */
5404 if (b_space_wraps(mbuf))
5405 goto realign_again;
5406 goto full;
5407 }
5408 }
5409 }
Willy Tarreau80739692018-10-05 11:35:57 +02005410 }
5411
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005412 /* encode all headers, stop at empty name. Host is only sent if we
5413 * do not provide an authority.
5414 */
Willy Tarreau80739692018-10-05 11:35:57 +02005415 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005416 struct ist n = list[hdr].n;
5417 struct ist v = list[hdr].v;
5418
Willy Tarreau80739692018-10-05 11:35:57 +02005419 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005420 if (isteq(n, ist("connection")) ||
5421 (auth.len && isteq(n, ist("host"))) ||
5422 isteq(n, ist("proxy-connection")) ||
5423 isteq(n, ist("keep-alive")) ||
5424 isteq(n, ist("upgrade")) ||
5425 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005426 continue;
5427
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005428 if (isteq(n, ist("te"))) {
5429 /* "te" may only be sent with "trailers" if this value
5430 * is present, otherwise it must be deleted.
5431 */
5432 v = istist(v, ist("trailers"));
5433 if (!v.ptr || (v.len > 8 && v.ptr[8] != ','))
5434 continue;
5435 v = ist("trailers");
5436 }
5437
Christopher Faulet86d144c2019-08-14 16:32:25 +02005438 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005439 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005440 continue;
5441
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005442 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005443 break; // end
5444
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005445 if (!hpack_encode_header(&outbuf, n, v)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005446 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005447 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005448 goto realign_again;
5449 goto full;
5450 }
5451 }
5452
Willy Tarreaucb985a42019-10-07 16:56:34 +02005453 /* update the frame's size */
5454 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5455
5456 if (outbuf.data > h2c->mfs + 9) {
5457 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5458 /* output full */
5459 if (b_space_wraps(mbuf))
5460 goto realign_again;
5461 goto full;
5462 }
5463 }
5464
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005465 /* remove all header blocks including the EOH and compute the
5466 * corresponding size.
Willy Tarreau80739692018-10-05 11:35:57 +02005467 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005468 ret = 0;
5469 blk = htx_get_head_blk(htx);
5470 while (blk) {
5471 type = htx_get_blk_type(blk);
5472 ret += htx_get_blksz(blk);
5473 blk = htx_remove_blk(htx, blk);
5474 /* The removed block is the EOH */
5475 if (type == HTX_BLK_EOH)
5476 break;
Christopher Fauletd0db4232021-01-22 11:46:30 +01005477 }
Willy Tarreau80739692018-10-05 11:35:57 +02005478
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005479 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) {
5480 /* Request already closed: add END_STREAM */
Willy Tarreau80739692018-10-05 11:35:57 +02005481 es_now = 1;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005482 }
5483 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5484 /* EOM+empty: we may need to add END_STREAM (except for CONNECT
5485 * request)
5486 */
5487 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5488 es_now = 1;
5489 }
Willy Tarreau80739692018-10-05 11:35:57 +02005490
Willy Tarreau80739692018-10-05 11:35:57 +02005491 if (es_now)
5492 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5493
5494 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02005495 TRACE_USER("sent H2 request", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005496 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02005497 h2s->flags |= H2_SF_HEADERS_SENT;
5498 h2s->st = H2_SS_OPEN;
5499
Willy Tarreau80739692018-10-05 11:35:57 +02005500 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005501 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 +02005502 // trim any possibly pending data (eg: inconsistent content-length)
5503 h2s->flags |= H2_SF_ES_SENT;
5504 h2s->st = H2_SS_HLOC;
5505 }
5506
Willy Tarreau80739692018-10-05 11:35:57 +02005507 end:
5508 return ret;
5509 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005510 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5511 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02005512 h2c->flags |= H2_CF_MUX_MFULL;
5513 h2s->flags |= H2_SF_BLK_MROOM;
5514 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005515 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 +02005516 goto end;
5517 fail:
5518 /* unparsable HTX messages, too large ones to be produced in the local
5519 * list etc go here (unrecoverable errors).
5520 */
5521 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5522 ret = 0;
5523 goto end;
5524}
5525
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005526/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005527 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5528 * caller must check the stream's status to detect any error which might have
5529 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005530 * consumed, or zero if nothing done.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005531 */
Christopher Faulet142854b2020-12-02 15:12:40 +01005532static size_t h2s_make_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005533{
5534 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005535 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005536 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005537 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005538 size_t total = 0;
5539 int es_now = 0;
5540 int bsize; /* htx block size */
5541 int fsize; /* h2 frame size */
5542 struct htx_blk *blk;
5543 enum htx_blk_type type;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005544 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005545
Willy Tarreau7838a792019-08-12 18:42:03 +02005546 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5547
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005548 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005549 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005550 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005551 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005552 goto end;
5553 }
5554
Willy Tarreau98de12a2018-12-12 07:03:00 +01005555 htx = htx_from_buf(buf);
5556
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005557 /* We only come here with HTX_BLK_DATA blocks */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005558
5559 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005560 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005561 goto end;
5562
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005563 if ((h2c->flags & H2_CF_IS_BACK) &&
Christopher Fauletf95f8762021-01-22 11:59:07 +01005564 (h2s->flags & (H2_SF_HEADERS_RCVD|H2_SF_BODY_TUNNEL)) == H2_SF_BODY_TUNNEL) {
5565 /* The response HEADERS frame not received yet. Thus the tunnel
5566 * is not fully established yet. In this situation, we block
5567 * data sending.
5568 */
5569 h2s->flags |= H2_SF_BLK_MBUSY;
5570 TRACE_STATE("Request DATA frame blocked waiting for tunnel establishment", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5571 goto end;
5572 }
Christopher Faulet91b21dc2021-01-22 12:13:15 +01005573 else if ((h2c->flags & H2_CF_IS_BACK) && (h2s->flags & H2_SF_TUNNEL_ABRT)) {
5574 /* a tunnel attempt was aborted but the is pending raw data to xfer to the server.
5575 * Thus the stream is closed with the CANCEL error. The error will be reported to
5576 * the upper layer as aserver abort. But at this stage there is nothing more we can
5577 * do. We just wait for the end of the response to be sure to not truncate it.
5578 */
5579 if (!(h2s->flags & H2_SF_ES_RCVD)) {
5580 TRACE_STATE("Request DATA frame blocked waiting end of aborted tunnel", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5581 h2s->flags |= H2_SF_BLK_MBUSY;
5582 }
5583 else {
5584 TRACE_ERROR("Request DATA frame for aborted tunnel", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
5585 h2s_error(h2s, H2_ERR_CANCEL);
5586 }
5587 goto end;
5588 }
Willy Tarreau98de12a2018-12-12 07:03:00 +01005589
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005590 blk = htx_get_head_blk(htx);
5591 type = htx_get_blk_type(blk);
5592 bsize = htx_get_blksz(blk);
5593 fsize = bsize;
5594 trunc_out = 0;
5595 if (type != HTX_BLK_DATA)
5596 goto end;
5597
Willy Tarreau9c218e72019-05-26 10:08:28 +02005598 mbuf = br_tail(h2c->mbuf);
5599 retry:
5600 if (!h2_get_buf(h2c, mbuf)) {
5601 h2c->flags |= H2_CF_MUX_MALLOC;
5602 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005603 TRACE_STATE("waiting for room in output buffer", H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau9c218e72019-05-26 10:08:28 +02005604 goto end;
5605 }
5606
Willy Tarreau98de12a2018-12-12 07:03:00 +01005607 /* Perform some optimizations to reduce the number of buffer copies.
5608 * First, if the mux's buffer is empty and the htx area contains
5609 * exactly one data block of the same size as the requested count, and
5610 * this count fits within the frame size, the stream's window size, and
5611 * the connection's window size, then it's possible to simply swap the
5612 * caller's buffer with the mux's output buffer and adjust offsets and
5613 * length to match the entire DATA HTX block in the middle. In this
5614 * case we perform a true zero-copy operation from end-to-end. This is
5615 * the situation that happens all the time with large files. Second, if
5616 * this is not possible, but the mux's output buffer is empty, we still
5617 * have an opportunity to avoid the copy to the intermediary buffer, by
5618 * making the intermediary buffer's area point to the output buffer's
5619 * area. In this case we want to skip the HTX header to make sure that
5620 * copies remain aligned and that this operation remains possible all
5621 * the time. This goes for headers, data blocks and any data extracted
5622 * from the HTX blocks.
5623 */
5624 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02005625 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005626 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005627 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005628
Willy Tarreaubcc45952019-05-26 10:05:50 +02005629 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005630 /* Too bad there are data left there. We're willing to memcpy/memmove
5631 * up to 1/4 of the buffer, which means that it's OK to copy a large
5632 * frame into a buffer containing few data if it needs to be realigned,
5633 * and that it's also OK to copy few data without realigning. Otherwise
5634 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005635 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005636 if (fsize + 9 <= b_room(mbuf) &&
5637 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005638 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
5639 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 +01005640 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02005641 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005642
Willy Tarreau9c218e72019-05-26 10:08:28 +02005643 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5644 goto retry;
5645
Willy Tarreau98de12a2018-12-12 07:03:00 +01005646 h2c->flags |= H2_CF_MUX_MFULL;
5647 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005648 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 +01005649 goto end;
5650 }
5651
5652 /* map an H2 frame to the HTX block so that we can put the
5653 * frame header there.
5654 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005655 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5656 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005657
5658 /* prepend an H2 DATA frame header just before the DATA block */
5659 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5660 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5661 h2_set_frame_size(outbuf.area, fsize);
5662
5663 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005664 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005665 h2c->mws -= fsize;
5666
5667 /* and exchange with our old area */
5668 buf->area = old_area;
5669 buf->data = buf->head = 0;
5670 total += fsize;
Willy Tarreau7838a792019-08-12 18:42:03 +02005671
5672 TRACE_PROTO("sent H2 DATA frame (zero-copy)", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005673 goto end;
5674 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005675
Willy Tarreau98de12a2018-12-12 07:03:00 +01005676 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005677 /* for DATA and EOM we'll have to emit a frame, even if empty */
5678
5679 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005680 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5681 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005682 break;
5683 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005684 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005685 }
5686
5687 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005688 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5689 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005690 h2c->flags |= H2_CF_MUX_MFULL;
5691 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005692 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005693 goto end;
5694 }
5695
5696 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5697 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5698 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5699 outbuf.data = 9;
5700
5701 /* we have in <fsize> the exact number of bytes we need to copy from
5702 * the HTX buffer. We need to check this against the connection's and
5703 * the stream's send windows, and to ensure that this fits in the max
5704 * frame size and in the buffer's available space minus 9 bytes (for
5705 * the frame header). The connection's flow control is applied last so
5706 * that we can use a separate list of streams which are immediately
5707 * unblocked on window opening. Note: we don't implement padding.
5708 */
5709
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005710 if (!fsize)
5711 goto send_empty;
5712
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005713 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005714 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005715 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005716 LIST_DEL_INIT(&h2s->list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +02005717 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02005718 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 +01005719 goto end;
5720 }
5721
Willy Tarreauee573762018-12-04 15:25:57 +01005722 if (fsize > count)
5723 fsize = count;
5724
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005725 if (fsize > h2s_mws(h2s))
5726 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005727
5728 if (h2c->mfs && fsize > h2c->mfs)
5729 fsize = h2c->mfs; // >0
5730
5731 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005732 /* It doesn't fit at once. If it at least fits once split and
5733 * the amount of data to move is low, let's defragment the
5734 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005735 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005736 if (b_space_wraps(mbuf) &&
5737 (fsize + 9 <= b_room(mbuf)) &&
5738 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005739 goto realign_again;
5740 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005741 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005742
5743 if (fsize <= 0) {
5744 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005745 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5746 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005747 h2c->flags |= H2_CF_MUX_MFULL;
5748 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005749 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005750 goto end;
5751 }
5752 }
5753
5754 if (h2c->mws <= 0) {
5755 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005756 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 +01005757 goto end;
5758 }
5759
5760 if (fsize > h2c->mws)
5761 fsize = h2c->mws;
5762
5763 /* now let's copy this this into the output buffer */
5764 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005765 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005766 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005767 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005768
5769 send_empty:
5770 /* update the frame's size */
5771 h2_set_frame_size(outbuf.area, fsize);
5772
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005773 /* consume incoming HTX block */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005774 total += fsize;
5775 if (fsize == bsize) {
5776 htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005777 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) {
5778 /* EOM+empty: we may need to add END_STREAM (except for tunneled
5779 * message)
5780 */
5781 if (!(h2s->flags & H2_SF_BODY_TUNNEL))
5782 es_now = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02005783 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005784 }
5785 else {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005786 /* we've truncated this block */
5787 htx_cut_data_blk(htx, blk, fsize);
5788 }
5789
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005790 if (es_now)
5791 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5792
5793 /* commit the H2 response */
5794 b_add(mbuf, fsize + 9);
5795
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005796 if (es_now) {
5797 if (h2s->st == H2_SS_OPEN)
5798 h2s->st = H2_SS_HLOC;
5799 else
5800 h2s_close(h2s);
5801
5802 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005803 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 +01005804 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005805 else if (fsize) {
5806 if (fsize == bsize) {
5807 TRACE_DEVEL("more data may be available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5808 goto new_frame;
5809 }
5810 else if (trunc_out) {
5811 /* we've truncated this block */
5812 goto new_frame;
5813 }
5814 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005815
5816 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005817 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005818 return total;
5819}
5820
Christopher Faulet991febd2020-12-02 15:17:31 +01005821/* Skip the message payload (DATA blocks) and emit an empty DATA frame with the
5822 * ES flag set for stream <h2s>. This function is called for response known to
5823 * have no payload. Only DATA blocks are skipped. This means the trailers are
5824 * still emited. The caller must check the stream's status to detect any error
5825 * which might have happened subsequently to a successful send. Returns the
5826 * number of data bytes consumed, or zero if nothing done.
5827 */
5828static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count)
5829{
5830 struct h2c *h2c = h2s->h2c;
5831 struct htx *htx;
5832 int bsize; /* htx block size */
5833 int fsize; /* h2 frame size */
5834 struct htx_blk *blk;
5835 enum htx_blk_type type;
5836 size_t total = 0;
5837
5838 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5839
5840 if (h2c_mux_busy(h2c, h2s)) {
5841 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5842 h2s->flags |= H2_SF_BLK_MBUSY;
5843 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5844 goto end;
5845 }
5846
5847 htx = htx_from_buf(buf);
5848
5849 next_data:
5850 if (!count || htx_is_empty(htx))
5851 goto end;
5852 blk = htx_get_head_blk(htx);
5853 type = htx_get_blk_type(blk);
5854 bsize = htx_get_blksz(blk);
5855 fsize = bsize;
5856 if (type != HTX_BLK_DATA)
5857 goto end;
5858
5859 if (fsize > count)
5860 fsize = count;
5861
5862 if (fsize != bsize)
5863 goto skip_data;
5864
5865 if (!(htx->flags & HTX_FL_EOM) || !htx_is_unique_blk(htx, blk))
5866 goto skip_data;
5867
5868 /* Here, it is the last block and it is also the end of the message. So
5869 * we can emit an empty DATA frame with the ES flag set
5870 */
5871 if (h2_send_empty_data_es(h2s) <= 0)
5872 goto end;
5873
5874 if (h2s->st == H2_SS_OPEN)
5875 h2s->st = H2_SS_HLOC;
5876 else
5877 h2s_close(h2s);
5878
5879 skip_data:
5880 /* consume incoming HTX block */
5881 total += fsize;
5882 if (fsize == bsize) {
5883 TRACE_DEVEL("more data may be available, trying to skip another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5884 htx_remove_blk(htx, blk);
5885 goto next_data;
5886 }
5887 else {
5888 /* we've truncated this block */
5889 htx_cut_data_blk(htx, blk, fsize);
5890 }
5891
5892 end:
5893 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5894 return total;
5895}
5896
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005897/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5898 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5899 * processed. The caller must check the stream's status to detect any error
5900 * which might have happened subsequently to a successful send. The htx blocks
5901 * are automatically removed from the message. The htx message is assumed to be
5902 * valid since produced from the internal code. Processing stops when meeting
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005903 * the EOT, which *is* removed. All trailers are processed at once and sent as a
5904 * single frame. The ES flag is always set.
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005905 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005906static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005907{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005908 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005909 struct h2c *h2c = h2s->h2c;
5910 struct htx_blk *blk;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005911 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005912 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005913 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005914 int ret = 0;
5915 int hdr;
5916 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005917
Willy Tarreau7838a792019-08-12 18:42:03 +02005918 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5919
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005920 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005921 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005922 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005923 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005924 goto end;
5925 }
5926
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005927 /* get trailers. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005928 hdr = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005929 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005930 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005931
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005932 if (type == HTX_BLK_UNUSED)
5933 continue;
5934
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005935 if (type == HTX_BLK_EOT)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005936 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005937 if (type == HTX_BLK_TLR) {
5938 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5939 TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
5940 goto fail;
5941 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005942
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005943 list[hdr].n = htx_get_blk_name(htx, blk);
5944 list[hdr].v = htx_get_blk_value(htx, blk);
5945 hdr++;
5946 }
5947 else {
5948 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 +01005949 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005950 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005951 }
5952
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005953 /* marker for end of trailers */
5954 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005955
Willy Tarreau9c218e72019-05-26 10:08:28 +02005956 mbuf = br_tail(h2c->mbuf);
5957 retry:
5958 if (!h2_get_buf(h2c, mbuf)) {
5959 h2c->flags |= H2_CF_MUX_MALLOC;
5960 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005961 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 +02005962 goto end;
5963 }
5964
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005965 chunk_reset(&outbuf);
5966
5967 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005968 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5969 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005970 break;
5971 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005972 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005973 }
5974
5975 if (outbuf.size < 9)
5976 goto full;
5977
5978 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5979 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5980 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5981 outbuf.data = 9;
5982
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005983 /* encode all headers */
5984 for (idx = 0; idx < hdr; idx++) {
5985 /* these ones do not exist in H2 or must not appear in
5986 * trailers and must be dropped.
5987 */
5988 if (isteq(list[idx].n, ist("host")) ||
5989 isteq(list[idx].n, ist("content-length")) ||
5990 isteq(list[idx].n, ist("connection")) ||
5991 isteq(list[idx].n, ist("proxy-connection")) ||
5992 isteq(list[idx].n, ist("keep-alive")) ||
5993 isteq(list[idx].n, ist("upgrade")) ||
5994 isteq(list[idx].n, ist("te")) ||
5995 isteq(list[idx].n, ist("transfer-encoding")))
5996 continue;
5997
Christopher Faulet86d144c2019-08-14 16:32:25 +02005998 /* Skip all pseudo-headers */
5999 if (*(list[idx].n.ptr) == ':')
6000 continue;
6001
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006002 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
6003 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02006004 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006005 goto realign_again;
6006 goto full;
6007 }
6008 }
6009
Willy Tarreau5121e5d2019-05-06 15:13:41 +02006010 if (outbuf.data == 9) {
6011 /* here we have a problem, we have nothing to emit (either we
6012 * received an empty trailers block followed or we removed its
6013 * contents above). Because of this we can't send a HEADERS
6014 * frame, so we have to cheat and instead send an empty DATA
6015 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01006016 */
6017 outbuf.area[3] = H2_FT_DATA;
6018 outbuf.area[4] = H2_F_DATA_END_STREAM;
6019 }
6020
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006021 /* update the frame's size */
6022 h2_set_frame_size(outbuf.area, outbuf.data - 9);
6023
Willy Tarreau572d9f52019-10-11 16:58:37 +02006024 if (outbuf.data > h2c->mfs + 9) {
6025 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
6026 /* output full */
6027 if (b_space_wraps(mbuf))
6028 goto realign_again;
6029 goto full;
6030 }
6031 }
6032
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006033 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02006034 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 +02006035 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006036 h2s->flags |= H2_SF_ES_SENT;
6037
6038 if (h2s->st == H2_SS_OPEN)
6039 h2s->st = H2_SS_HLOC;
6040 else
6041 h2s_close(h2s);
6042
6043 /* OK we could properly deliver the response */
6044 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02006045 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006046 ret = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006047 blk = htx_get_head_blk(htx);
6048 while (blk) {
6049 type = htx_get_blk_type(blk);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006050 ret += htx_get_blksz(blk);
6051 blk = htx_remove_blk(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006052 /* The removed block is the EOT */
6053 if (type == HTX_BLK_EOT)
6054 break;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006055 }
6056
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006057 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02006058 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006059 return ret;
6060 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02006061 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
6062 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006063 h2c->flags |= H2_CF_MUX_MFULL;
6064 h2s->flags |= H2_SF_BLK_MROOM;
6065 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006066 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 +01006067 goto end;
6068 fail:
6069 /* unparsable HTX messages, too large ones to be produced in the local
6070 * list etc go here (unrecoverable errors).
6071 */
6072 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
6073 ret = 0;
6074 goto end;
6075}
6076
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006077/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6078 * event subscriber <es> is not allowed to change from a previous call as long
6079 * as at least one event is still subscribed. The <event_type> must only be a
6080 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006081 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006082static int h2_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02006083{
Olivier Houchard6ff20392018-07-17 18:46:31 +02006084 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02006085 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006086
Willy Tarreau7838a792019-08-12 18:42:03 +02006087 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006088
6089 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006090 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006091
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006092 es->events |= event_type;
6093 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006094
6095 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006096 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006097
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006098 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006099 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02006100 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
6101 !LIST_ADDED(&h2s->list)) {
6102 if (h2s->flags & H2_SF_BLK_MFCTL)
6103 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
6104 else
6105 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02006106 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02006107 }
Willy Tarreau7838a792019-08-12 18:42:03 +02006108 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006109 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02006110}
6111
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006112/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6113 * The <es> pointer is not allowed to differ from the one passed to the
6114 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006115 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006116static int h2_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006117{
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006118 struct h2s *h2s = cs->ctx;
6119
Willy Tarreau7838a792019-08-12 18:42:03 +02006120 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006121
6122 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006123 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01006124
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006125 es->events &= ~event_type;
6126 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01006127 h2s->subs = NULL;
6128
6129 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02006130 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006131
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006132 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006133 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01006134 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01006135 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
6136 LIST_DEL_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02006137 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01006138
Willy Tarreau7838a792019-08-12 18:42:03 +02006139 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006140 return 0;
6141}
6142
6143
Olivier Houchard511efea2018-08-16 15:30:32 +02006144/* Called from the upper layer, to receive data */
6145static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
6146{
Olivier Houchard638b7992018-08-16 15:41:52 +02006147 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01006148 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01006149 struct htx *h2s_htx = NULL;
6150 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02006151 size_t ret = 0;
6152
Willy Tarreau7838a792019-08-12 18:42:03 +02006153 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
6154
Olivier Houchard511efea2018-08-16 15:30:32 +02006155 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006156 h2s_htx = htx_from_buf(&h2s->rxbuf);
6157 if (htx_is_empty(h2s_htx)) {
6158 /* Here htx_to_buf() will set buffer data to 0 because
6159 * the HTX is empty.
6160 */
6161 htx_to_buf(h2s_htx, &h2s->rxbuf);
6162 goto end;
6163 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01006164
Christopher Faulet9b79a102019-07-15 11:22:56 +02006165 ret = h2s_htx->data;
6166 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01006167
Christopher Faulet9b79a102019-07-15 11:22:56 +02006168 /* <buf> is empty and the message is small enough, swap the
6169 * buffers. */
6170 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01006171 htx_to_buf(buf_htx, buf);
6172 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006173 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
6174 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01006175 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02006176
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006177 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_UNUSED);
Christopher Faulet9b79a102019-07-15 11:22:56 +02006178
6179 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
6180 buf_htx->flags |= HTX_FL_PARSING_ERROR;
6181 if (htx_is_empty(buf_htx))
6182 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01006183 }
Christopher Faulet810df062020-07-22 16:20:34 +02006184 else if (htx_is_empty(h2s_htx))
Christopher Faulet42432f32020-11-20 17:43:16 +01006185 buf_htx->flags |= (h2s_htx->flags & HTX_FL_EOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006186
Christopher Faulet9b79a102019-07-15 11:22:56 +02006187 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
6188 htx_to_buf(buf_htx, buf);
6189 htx_to_buf(h2s_htx, &h2s->rxbuf);
6190 ret -= h2s_htx->data;
6191
Christopher Faulet37070b22019-02-14 15:12:14 +01006192 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02006193 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01006194 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02006195 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01006196 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd0db4232021-01-22 11:46:30 +01006197 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02006198 cs->flags |= CS_FL_EOI;
Christopher Fauletd0db4232021-01-22 11:46:30 +01006199 /* Add EOS flag for tunnel */
6200 if (h2s->flags & H2_SF_BODY_TUNNEL)
6201 cs->flags |= CS_FL_EOS;
6202 }
Christopher Fauletaade4ed2020-10-08 15:38:41 +02006203 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02006204 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01006205 if (cs->flags & CS_FL_ERR_PENDING)
6206 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02006207 if (b_size(&h2s->rxbuf)) {
6208 b_free(&h2s->rxbuf);
6209 offer_buffers(NULL, tasks_run_queue);
6210 }
Olivier Houchard511efea2018-08-16 15:30:32 +02006211 }
6212
Willy Tarreau082f5592018-11-25 08:03:32 +01006213 if (ret && h2c->dsi == h2s->id) {
6214 /* demux is blocking on this stream's buffer */
6215 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02006216 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01006217 }
Christopher Faulet37070b22019-02-14 15:12:14 +01006218
Willy Tarreau7838a792019-08-12 18:42:03 +02006219 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02006220 return ret;
6221}
6222
Olivier Houchardd846c262018-10-19 17:24:29 +02006223
Willy Tarreau749f5ca2019-03-21 19:19:36 +01006224/* Called from the upper layer, to send data from buffer <buf> for no more than
6225 * <count> bytes. Returns the number of bytes effectively sent. Some status
6226 * flags may be updated on the conn_stream.
6227 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02006228static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02006229{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006230 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02006231 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02006232 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006233 struct htx *htx;
6234 struct htx_blk *blk;
6235 enum htx_blk_type btype;
6236 uint32_t bsize;
6237 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006238
Willy Tarreau7838a792019-08-12 18:42:03 +02006239 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
6240
Olivier Houchardd360ac62019-03-22 17:37:16 +01006241 /* If we were not just woken because we wanted to send but couldn't,
6242 * and there's somebody else that is waiting to send, do nothing,
6243 * we will subscribe later and be put at the end of the list
6244 */
Willy Tarreaud9464162020-01-10 18:25:07 +01006245 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02006246 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
6247 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 +01006248 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006249 }
Willy Tarreaud9464162020-01-10 18:25:07 +01006250 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02006251
Willy Tarreau7838a792019-08-12 18:42:03 +02006252 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
6253 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 +02006254 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02006255 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02006256
Willy Tarreaucab22952019-10-31 15:48:18 +01006257 if (h2s->h2c->st0 >= H2_CS_ERROR) {
6258 cs->flags |= CS_FL_ERROR;
6259 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);
6260 return 0;
6261 }
6262
Christopher Faulet9b79a102019-07-15 11:22:56 +02006263 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006264
Willy Tarreau0bad0432018-06-14 16:54:01 +02006265 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01006266 h2s->flags |= H2_SF_OUTGOING_DATA;
6267
Willy Tarreau751f2d02018-10-05 09:35:00 +02006268 if (h2s->id == 0) {
6269 int32_t id = h2c_get_next_sid(h2s->h2c);
6270
6271 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02006272 cs->flags |= CS_FL_ERROR;
Willy Tarreau7838a792019-08-12 18:42:03 +02006273 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 +02006274 return 0;
6275 }
6276
6277 eb32_delete(&h2s->by_id);
6278 h2s->by_id.key = h2s->id = id;
6279 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01006280 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02006281 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
6282 }
6283
Christopher Faulet9b79a102019-07-15 11:22:56 +02006284 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
6285 count && !htx_is_empty(htx)) {
6286 idx = htx_get_head(htx);
6287 blk = htx_get_blk(htx, idx);
6288 btype = htx_get_blk_type(blk);
6289 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006290
Christopher Faulet9b79a102019-07-15 11:22:56 +02006291 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02006292 case HTX_BLK_REQ_SL:
6293 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006294 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02006295 if (ret > 0) {
6296 total += ret;
6297 count -= ret;
6298 if (ret < bsize)
6299 goto done;
6300 }
6301 break;
6302
Willy Tarreau115e83b2018-12-01 19:17:53 +01006303 case HTX_BLK_RES_SL:
6304 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006305 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01006306 if (ret > 0) {
6307 total += ret;
6308 count -= ret;
6309 if (ret < bsize)
6310 goto done;
6311 }
6312 break;
6313
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006314 case HTX_BLK_DATA:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006315 /* all these cause the emission of a DATA frame (possibly empty) */
Christopher Faulet991febd2020-12-02 15:17:31 +01006316 if (!(h2s->h2c->flags & H2_CF_IS_BACK) &&
6317 (h2s->flags & (H2_SF_BODY_TUNNEL|H2_SF_BODYLESS_RESP)) == H2_SF_BODYLESS_RESP)
6318 ret = h2s_skip_data(h2s, buf, count);
6319 else
6320 ret = h2s_make_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006321 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01006322 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01006323 total += ret;
6324 count -= ret;
6325 if (ret < bsize)
6326 goto done;
6327 }
6328 break;
6329
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006330 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02006331 case HTX_BLK_EOT:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006332 /* This is the first trailers block, all the subsequent ones */
Christopher Faulet9b79a102019-07-15 11:22:56 +02006333 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01006334 if (ret > 0) {
6335 total += ret;
6336 count -= ret;
6337 if (ret < bsize)
6338 goto done;
6339 }
6340 break;
6341
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006342 default:
6343 htx_remove_blk(htx, blk);
6344 total += bsize;
6345 count -= bsize;
6346 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006347 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01006348 }
6349
Christopher Faulet9b79a102019-07-15 11:22:56 +02006350 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02006351 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02006352 /* trim any possibly pending data after we close (extra CR-LF,
6353 * unprocessed trailers, abnormal extra data, ...)
6354 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02006355 total += count;
6356 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02006357 }
6358
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006359 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01006360 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006361 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 +01006362 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01006363 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01006364 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01006365 }
6366
Christopher Faulet9b79a102019-07-15 11:22:56 +02006367 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02006368
Olivier Houchard7505f942018-08-21 18:10:44 +02006369 if (total > 0) {
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006370 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006371 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 +02006372 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Tim Duesterhus12a08d82020-12-21 19:40:16 +01006373 }
Olivier Houchardd846c262018-10-19 17:24:29 +02006374
Olivier Houchard7505f942018-08-21 18:10:44 +02006375 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01006376 /* If we're waiting for flow control, and we got a shutr on the
6377 * connection, we will never be unlocked, so add an error on
6378 * the conn_stream.
6379 */
6380 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
6381 !b_data(&h2s->h2c->dbuf) &&
6382 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02006383 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 +01006384 if (cs->flags & CS_FL_EOS)
6385 cs->flags |= CS_FL_ERROR;
6386 else
6387 cs->flags |= CS_FL_ERR_PENDING;
6388 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006389
Willy Tarreau5723f292020-01-10 15:16:57 +01006390 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
6391 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006392 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01006393 LIST_DEL_INIT(&h2s->list);
6394 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02006395
Willy Tarreau7838a792019-08-12 18:42:03 +02006396 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02006397 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02006398}
6399
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006400/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01006401static int h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006402{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01006403 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01006404 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006405 struct eb32_node *node;
6406 int fctl_cnt = 0;
6407 int send_cnt = 0;
6408 int tree_cnt = 0;
6409 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02006410 struct buffer *hmbuf, *tmbuf;
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006411 int ret = 0;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006412
6413 if (!h2c)
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006414 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006415
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006416 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006417 fctl_cnt++;
6418
Olivier Houchardfa8aa862018-10-10 18:25:41 +02006419 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006420 send_cnt++;
6421
Willy Tarreau3af37712018-12-18 14:34:41 +01006422 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006423 node = eb32_first(&h2c->streams_by_id);
6424 while (node) {
6425 h2s = container_of(node, struct h2s, by_id);
6426 tree_cnt++;
6427 if (!h2s->cs)
6428 orph_cnt++;
6429 node = eb32_next(node);
6430 }
6431
Willy Tarreau60f62682019-05-26 11:32:27 +02006432 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02006433 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02006434 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau987c0632018-12-18 10:32:05 +01006435 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02006436 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
6437 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreauab2ec452019-08-30 07:07:08 +02006438 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau616ac812018-07-24 14:12:42 +02006439 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006440 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01006441 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
6442 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
6443 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02006444 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
6445 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
6446 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02006447 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
6448 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01006449
6450 if (h2s) {
Willy Tarreaued4464e2021-01-20 15:50:03 +01006451 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 +02006452 h2s, h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
Willy Tarreau987c0632018-12-18 10:32:05 +01006453 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
6454 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
6455 h2s->cs);
6456 if (h2s->cs)
Willy Tarreau98e40b92021-01-20 16:27:01 +01006457 chunk_appendf(msg, "(.flg=0x%08x .data=%p)",
Willy Tarreau987c0632018-12-18 10:32:05 +01006458 h2s->cs->flags, h2s->cs->data);
Willy Tarreau98e40b92021-01-20 16:27:01 +01006459
6460 chunk_appendf(&trash, " .subs=%p", h2s->subs);
6461 if (h2s->subs) {
6462 if (h2s->subs) {
6463 chunk_appendf(&trash, "(ev=%d tl=%p", h2s->subs->events, h2s->subs->tasklet);
6464 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
6465 h2s->subs->tasklet->calls,
6466 h2s->subs->tasklet->context);
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006467 if (h2s->subs->tasklet->calls >= 1000000)
6468 ret = 1;
Willy Tarreau98e40b92021-01-20 16:27:01 +01006469 resolve_sym_name(&trash, NULL, h2s->subs->tasklet->process);
6470 chunk_appendf(&trash, ")");
6471 }
6472 }
Willy Tarreau987c0632018-12-18 10:32:05 +01006473 }
Willy Tarreau06bf83e2021-01-21 09:13:35 +01006474 return ret;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006475}
Willy Tarreau62f52692017-10-08 23:01:42 +02006476
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006477/* Migrate the the connection to the current thread.
6478 * Return 0 if successful, non-zero otherwise.
6479 * Expected to be called with the old thread lock held.
6480 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02006481static int h2_takeover(struct connection *conn, int orig_tid)
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006482{
6483 struct h2c *h2c = conn->ctx;
Willy Tarreau617e80f2020-07-01 16:39:33 +02006484 struct task *task;
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006485
6486 if (fd_takeover(conn->handle.fd, conn) != 0)
6487 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006488
6489 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
6490 /* We failed to takeover the xprt, even if the connection may
6491 * still be valid, flag it as error'd, as we have already
6492 * taken over the fd, and wake the tasklet, so that it will
6493 * destroy it.
6494 */
6495 conn->flags |= CO_FL_ERROR;
6496 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
6497 return -1;
6498 }
6499
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006500 if (h2c->wait_event.events)
6501 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
6502 h2c->wait_event.events, &h2c->wait_event);
6503 /* To let the tasklet know it should free itself, and do nothing else,
6504 * set its context to NULL.
6505 */
6506 h2c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02006507 tasklet_wakeup_on(h2c->wait_event.tasklet, orig_tid);
Willy Tarreau617e80f2020-07-01 16:39:33 +02006508
6509 task = h2c->task;
6510 if (task) {
6511 task->context = NULL;
6512 h2c->task = NULL;
6513 __ha_barrier_store();
6514 task_kill(task);
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006515
6516 h2c->task = task_new(tid_bit);
6517 if (!h2c->task) {
6518 h2_release(h2c);
6519 return -1;
6520 }
6521 h2c->task->process = h2_timeout_task;
6522 h2c->task->context = h2c;
6523 }
6524 h2c->wait_event.tasklet = tasklet_new();
6525 if (!h2c->wait_event.tasklet) {
6526 h2_release(h2c);
6527 return -1;
6528 }
6529 h2c->wait_event.tasklet->process = h2_io_cb;
6530 h2c->wait_event.tasklet->context = h2c;
6531 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
6532 SUB_RETRY_RECV, &h2c->wait_event);
6533
6534 return 0;
6535}
6536
Willy Tarreau62f52692017-10-08 23:01:42 +02006537/*******************************************************/
6538/* functions below are dedicated to the config parsers */
6539/*******************************************************/
6540
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006541/* config parser for global "tune.h2.header-table-size" */
6542static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
6543 struct proxy *defpx, const char *file, int line,
6544 char **err)
6545{
6546 if (too_many_args(1, args, err, NULL))
6547 return -1;
6548
6549 h2_settings_header_table_size = atoi(args[1]);
6550 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
6551 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
6552 return -1;
6553 }
6554 return 0;
6555}
Willy Tarreau62f52692017-10-08 23:01:42 +02006556
Willy Tarreaue6baec02017-07-27 11:45:11 +02006557/* config parser for global "tune.h2.initial-window-size" */
6558static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
6559 struct proxy *defpx, const char *file, int line,
6560 char **err)
6561{
6562 if (too_many_args(1, args, err, NULL))
6563 return -1;
6564
6565 h2_settings_initial_window_size = atoi(args[1]);
6566 if (h2_settings_initial_window_size < 0) {
6567 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
6568 return -1;
6569 }
6570 return 0;
6571}
6572
Willy Tarreau5242ef82017-07-27 11:47:28 +02006573/* config parser for global "tune.h2.max-concurrent-streams" */
6574static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
6575 struct proxy *defpx, const char *file, int line,
6576 char **err)
6577{
6578 if (too_many_args(1, args, err, NULL))
6579 return -1;
6580
6581 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01006582 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02006583 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
6584 return -1;
6585 }
6586 return 0;
6587}
6588
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006589/* config parser for global "tune.h2.max-frame-size" */
6590static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
6591 struct proxy *defpx, const char *file, int line,
6592 char **err)
6593{
6594 if (too_many_args(1, args, err, NULL))
6595 return -1;
6596
6597 h2_settings_max_frame_size = atoi(args[1]);
6598 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
6599 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
6600 return -1;
6601 }
6602 return 0;
6603}
6604
Willy Tarreau62f52692017-10-08 23:01:42 +02006605
6606/****************************************/
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05006607/* MUX initialization and instantiation */
Willy Tarreau62f52692017-10-08 23:01:42 +02006608/***************************************/
6609
6610/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01006611static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02006612 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02006613 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02006614 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02006615 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02006616 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006617 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02006618 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01006619 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02006620 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01006621 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01006622 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01006623 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02006624 .shutr = h2_shutr,
6625 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02006626 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006627 .show_fd = h2_show_fd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006628 .takeover = h2_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02006629 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX|MX_FL_HOL_RISK,
Willy Tarreau62f52692017-10-08 23:01:42 +02006630 .name = "H2",
6631};
6632
Christopher Faulet32f61c02018-04-10 14:33:41 +02006633static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02006634 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02006635
Willy Tarreau0108d902018-11-25 19:14:37 +01006636INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
6637
Willy Tarreau62f52692017-10-08 23:01:42 +02006638/* config keyword parsers */
6639static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006640 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02006641 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006642 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006643 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006644 { 0, NULL, NULL }
6645}};
6646
Willy Tarreau0108d902018-11-25 19:14:37 +01006647INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02006648
6649/* initialize internal structs after the config is parsed.
6650 * Returns zero on success, non-zero on error.
6651 */
6652static int init_h2()
6653{
6654 pool_head_hpack_tbl = create_pool("hpack_tbl",
6655 h2_settings_header_table_size,
6656 MEM_F_SHARED|MEM_F_EXACT);
Christopher Faulet52140992020-11-06 15:23:39 +01006657 if (!pool_head_hpack_tbl) {
6658 ha_alert("failed to allocate hpack_tbl memory pool\n");
6659 return (ERR_ALERT | ERR_FATAL);
6660 }
6661 return ERR_NONE;
Willy Tarreau2bdcc702020-05-19 11:31:11 +02006662}
6663
6664REGISTER_POST_CHECK(init_h2);