blob: 9ea56c2c365eabd56a2d6133c9c57493c6e69be2 [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
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Willy Tarreau12ae2122019-08-08 18:23:12 +020025#include <proto/trace.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010026#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020027#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010028#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010029#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020030#include <eb32tree.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 Tarreau97aaa672018-12-23 09:49:04 +010067#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020068
Willy Tarreau5ab6b572017-09-22 08:05:00 +020069/* H2 connection state, in h2c->st0 */
70enum h2_cs {
71 H2_CS_PREFACE, // init done, waiting for connection preface
72 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
73 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
74 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010075 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
76 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020077 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
78 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
79 H2_CS_ENTRIES // must be last
80} __attribute__((packed));
81
Willy Tarreau51330962019-05-26 09:38:07 +020082
Willy Tarreau9c218e72019-05-26 10:08:28 +020083/* 32 buffers: one for the ring's root, rest for the mbuf itself */
84#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020085
Willy Tarreau5ab6b572017-09-22 08:05:00 +020086/* H2 connection descriptor */
87struct h2c {
88 struct connection *conn;
89
90 enum h2_cs st0; /* mux state */
91 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
92
93 /* 16 bit hole here */
94 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010095 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020096 int32_t max_id; /* highest ID known on this connection, <0 before preface */
97 uint32_t rcvd_c; /* newly received data to ACK for the connection */
98 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
99
100 /* states for the demux direction */
101 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103
104 int32_t dsi; /* demux stream ID (<0 = idle) */
105 int32_t dfl; /* demux frame length (if dsi >= 0) */
106 int8_t dft; /* demux frame type (if dsi >= 0) */
107 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100108 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
109 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200110 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
111
112 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200113 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200114 int32_t msi; /* mux stream ID (<0 = idle) */
115 int32_t mfl; /* mux frame length (if dsi >= 0) */
116 int8_t mft; /* mux frame type (if dsi >= 0) */
117 int8_t mff; /* mux frame flags (if dsi >= 0) */
118 /* 16 bit hole here */
119 int32_t miw; /* mux initial window size for all new streams */
120 int32_t mws; /* mux window size. Can be negative. */
121 int32_t mfs; /* mux's max frame size */
122
Willy Tarreauea392822017-10-31 10:02:25 +0100123 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100124 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100125 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200126 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100127 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100128 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200129 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100130 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200131 struct eb_root streams_by_id; /* all active streams by their ID */
132 struct list send_list; /* list of blocked streams requesting to send */
133 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200134 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100135 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200136 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200137};
138
Willy Tarreau18312642017-10-11 07:57:07 +0200139/* H2 stream state, in h2s->st */
140enum h2_ss {
141 H2_SS_IDLE = 0, // idle
142 H2_SS_RLOC, // reserved(local)
143 H2_SS_RREM, // reserved(remote)
144 H2_SS_OPEN, // open
145 H2_SS_HREM, // half-closed(remote)
146 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200147 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200148 H2_SS_CLOSED, // closed
149 H2_SS_ENTRIES // must be last
150} __attribute__((packed));
151
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200152#define H2_SS_MASK(state) (1UL << (state))
153#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
154#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
155#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
156#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
157#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
158#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
159#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
160#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200161
Willy Tarreau18312642017-10-11 07:57:07 +0200162/* HTTP/2 stream flags (32 bit), in h2s->flags */
163#define H2_SF_NONE 0x00000000
164#define H2_SF_ES_RCVD 0x00000001
165#define H2_SF_ES_SENT 0x00000002
166
167#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
168#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
169
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200170/* stream flags indicating the reason the stream is blocked */
171#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
172#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
173#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
174#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
175#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
176
Willy Tarreau454f9052017-10-26 19:40:35 +0200177/* stream flags indicating how data is supposed to be sent */
178#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
Willy Tarreau454f9052017-10-26 19:40:35 +0200179
Christopher Faulet3e395632019-09-13 09:37:21 +0200180/* unused flags: 0x00000200, 0x00000400, 0x00000800 */
Willy Tarreau454f9052017-10-26 19:40:35 +0200181
Willy Tarreau67434202017-11-06 20:20:51 +0100182#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100183#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100184
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100185#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
186
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200187#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
188#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200189#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200190
191
Willy Tarreau18312642017-10-11 07:57:07 +0200192/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
193 * it is being processed in the internal HTTP representation (H1 for now).
194 */
195struct h2s {
196 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100197 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200198 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200199 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200200 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200201 int32_t id; /* stream ID */
202 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200203 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200204 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
205 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200206 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100207 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200208 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200209 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100210 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
211 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200212 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100213 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200214};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200215
Willy Tarreauc6405142017-09-21 20:23:50 +0200216/* descriptor for an h2 frame header */
217struct h2_fh {
218 uint32_t len; /* length, host order, 24 bits */
219 uint32_t sid; /* stream id, host order, 31 bits */
220 uint8_t ft; /* frame type */
221 uint8_t ff; /* frame flags */
222};
223
Willy Tarreau12ae2122019-08-08 18:23:12 +0200224/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200225static void h2_trace(enum trace_level level, uint64_t mask, \
226 const struct trace_source *src,
227 const struct ist where, const struct ist func,
228 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200229
230/* The event representation is split like this :
231 * strm - application layer
232 * h2s - internal H2 stream
233 * h2c - internal H2 connection
234 * conn - external connection
235 *
236 */
237static const struct trace_event h2_trace_events[] = {
238#define H2_EV_H2C_NEW (1ULL << 0)
Willy Tarreau87951942019-08-30 07:34:36 +0200239 { .mask = H2_EV_H2C_NEW, .name = "h2c_new", .desc = "new H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200240#define H2_EV_H2C_RECV (1ULL << 1)
Willy Tarreau87951942019-08-30 07:34:36 +0200241 { .mask = H2_EV_H2C_RECV, .name = "h2c_recv", .desc = "Rx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200242#define H2_EV_H2C_SEND (1ULL << 2)
Willy Tarreau87951942019-08-30 07:34:36 +0200243 { .mask = H2_EV_H2C_SEND, .name = "h2c_send", .desc = "Tx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200244#define H2_EV_H2C_FCTL (1ULL << 3)
Willy Tarreau87951942019-08-30 07:34:36 +0200245 { .mask = H2_EV_H2C_FCTL, .name = "h2c_fctl", .desc = "H2 connection flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200246#define H2_EV_H2C_BLK (1ULL << 4)
Willy Tarreau87951942019-08-30 07:34:36 +0200247 { .mask = H2_EV_H2C_BLK, .name = "h2c_blk", .desc = "H2 connection blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200248#define H2_EV_H2C_WAKE (1ULL << 5)
Willy Tarreau87951942019-08-30 07:34:36 +0200249 { .mask = H2_EV_H2C_WAKE, .name = "h2c_wake", .desc = "H2 connection woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200250#define H2_EV_H2C_END (1ULL << 6)
Willy Tarreau87951942019-08-30 07:34:36 +0200251 { .mask = H2_EV_H2C_END, .name = "h2c_end", .desc = "H2 connection terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200252#define H2_EV_H2C_ERR (1ULL << 7)
Willy Tarreau87951942019-08-30 07:34:36 +0200253 { .mask = H2_EV_H2C_ERR, .name = "h2c_err", .desc = "error on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200254#define H2_EV_RX_FHDR (1ULL << 8)
Willy Tarreau87951942019-08-30 07:34:36 +0200255 { .mask = H2_EV_RX_FHDR, .name = "rx_fhdr", .desc = "H2 frame header received" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200256#define H2_EV_RX_FRAME (1ULL << 9)
Willy Tarreau87951942019-08-30 07:34:36 +0200257 { .mask = H2_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200258#define H2_EV_RX_EOI (1ULL << 10)
Willy Tarreau87951942019-08-30 07:34:36 +0200259 { .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 +0200260#define H2_EV_RX_PREFACE (1ULL << 11)
Willy Tarreau87951942019-08-30 07:34:36 +0200261 { .mask = H2_EV_RX_PREFACE, .name = "rx_preface", .desc = "receipt of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200262#define H2_EV_RX_DATA (1ULL << 12)
Willy Tarreau87951942019-08-30 07:34:36 +0200263 { .mask = H2_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200264#define H2_EV_RX_HDR (1ULL << 13)
Willy Tarreau87951942019-08-30 07:34:36 +0200265 { .mask = H2_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200266#define H2_EV_RX_PRIO (1ULL << 14)
Willy Tarreau87951942019-08-30 07:34:36 +0200267 { .mask = H2_EV_RX_PRIO, .name = "rx_prio", .desc = "receipt of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200268#define H2_EV_RX_RST (1ULL << 15)
Willy Tarreau87951942019-08-30 07:34:36 +0200269 { .mask = H2_EV_RX_RST, .name = "rx_rst", .desc = "receipt of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200270#define H2_EV_RX_SETTINGS (1ULL << 16)
Willy Tarreau87951942019-08-30 07:34:36 +0200271 { .mask = H2_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200272#define H2_EV_RX_PUSH (1ULL << 17)
Willy Tarreau87951942019-08-30 07:34:36 +0200273 { .mask = H2_EV_RX_PUSH, .name = "rx_push", .desc = "receipt of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200274#define H2_EV_RX_PING (1ULL << 18)
Willy Tarreau87951942019-08-30 07:34:36 +0200275 { .mask = H2_EV_RX_PING, .name = "rx_ping", .desc = "receipt of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200276#define H2_EV_RX_GOAWAY (1ULL << 19)
Willy Tarreau87951942019-08-30 07:34:36 +0200277 { .mask = H2_EV_RX_GOAWAY, .name = "rx_goaway", .desc = "receipt of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200278#define H2_EV_RX_WU (1ULL << 20)
Willy Tarreau87951942019-08-30 07:34:36 +0200279 { .mask = H2_EV_RX_WU, .name = "rx_wu", .desc = "receipt of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200280#define H2_EV_RX_CONT (1ULL << 21)
Willy Tarreau87951942019-08-30 07:34:36 +0200281 { .mask = H2_EV_RX_CONT, .name = "rx_cont", .desc = "receipt of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200282#define H2_EV_TX_FRAME (1ULL << 22)
Willy Tarreau87951942019-08-30 07:34:36 +0200283 { .mask = H2_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200284#define H2_EV_TX_EOI (1ULL << 23)
Willy Tarreau87951942019-08-30 07:34:36 +0200285 { .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 +0200286#define H2_EV_TX_PREFACE (1ULL << 24)
Willy Tarreau87951942019-08-30 07:34:36 +0200287 { .mask = H2_EV_TX_PREFACE, .name = "tx_preface", .desc = "transmission of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200288#define H2_EV_TX_DATA (1ULL << 25)
Willy Tarreau87951942019-08-30 07:34:36 +0200289 { .mask = H2_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200290#define H2_EV_TX_HDR (1ULL << 26)
Willy Tarreau87951942019-08-30 07:34:36 +0200291 { .mask = H2_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200292#define H2_EV_TX_PRIO (1ULL << 27)
Willy Tarreau87951942019-08-30 07:34:36 +0200293 { .mask = H2_EV_TX_PRIO, .name = "tx_prio", .desc = "transmission of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200294#define H2_EV_TX_RST (1ULL << 28)
Willy Tarreau87951942019-08-30 07:34:36 +0200295 { .mask = H2_EV_TX_RST, .name = "tx_rst", .desc = "transmission of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200296#define H2_EV_TX_SETTINGS (1ULL << 29)
Willy Tarreau87951942019-08-30 07:34:36 +0200297 { .mask = H2_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200298#define H2_EV_TX_PUSH (1ULL << 30)
Willy Tarreau87951942019-08-30 07:34:36 +0200299 { .mask = H2_EV_TX_PUSH, .name = "tx_push", .desc = "transmission of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200300#define H2_EV_TX_PING (1ULL << 31)
Willy Tarreau87951942019-08-30 07:34:36 +0200301 { .mask = H2_EV_TX_PING, .name = "tx_ping", .desc = "transmission of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200302#define H2_EV_TX_GOAWAY (1ULL << 32)
Willy Tarreau87951942019-08-30 07:34:36 +0200303 { .mask = H2_EV_TX_GOAWAY, .name = "tx_goaway", .desc = "transmission of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200304#define H2_EV_TX_WU (1ULL << 33)
Willy Tarreau87951942019-08-30 07:34:36 +0200305 { .mask = H2_EV_TX_WU, .name = "tx_wu", .desc = "transmission of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200306#define H2_EV_TX_CONT (1ULL << 34)
Willy Tarreau87951942019-08-30 07:34:36 +0200307 { .mask = H2_EV_TX_CONT, .name = "tx_cont", .desc = "transmission of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200308#define H2_EV_H2S_NEW (1ULL << 35)
Willy Tarreau87951942019-08-30 07:34:36 +0200309 { .mask = H2_EV_H2S_NEW, .name = "h2s_new", .desc = "new H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200310#define H2_EV_H2S_RECV (1ULL << 36)
Willy Tarreau87951942019-08-30 07:34:36 +0200311 { .mask = H2_EV_H2S_RECV, .name = "h2s_recv", .desc = "Rx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200312#define H2_EV_H2S_SEND (1ULL << 37)
Willy Tarreau87951942019-08-30 07:34:36 +0200313 { .mask = H2_EV_H2S_SEND, .name = "h2s_send", .desc = "Tx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200314#define H2_EV_H2S_FCTL (1ULL << 38)
Willy Tarreau87951942019-08-30 07:34:36 +0200315 { .mask = H2_EV_H2S_FCTL, .name = "h2s_fctl", .desc = "H2 stream flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200316#define H2_EV_H2S_BLK (1ULL << 39)
Willy Tarreau87951942019-08-30 07:34:36 +0200317 { .mask = H2_EV_H2S_BLK, .name = "h2s_blk", .desc = "H2 stream blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200318#define H2_EV_H2S_WAKE (1ULL << 40)
Willy Tarreau87951942019-08-30 07:34:36 +0200319 { .mask = H2_EV_H2S_WAKE, .name = "h2s_wake", .desc = "H2 stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200320#define H2_EV_H2S_END (1ULL << 41)
Willy Tarreau87951942019-08-30 07:34:36 +0200321 { .mask = H2_EV_H2S_END, .name = "h2s_end", .desc = "H2 stream terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200322#define H2_EV_H2S_ERR (1ULL << 42)
Willy Tarreau87951942019-08-30 07:34:36 +0200323 { .mask = H2_EV_H2S_ERR, .name = "h2s_err", .desc = "error on H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200324#define H2_EV_STRM_NEW (1ULL << 43)
Willy Tarreau87951942019-08-30 07:34:36 +0200325 { .mask = H2_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200326#define H2_EV_STRM_RECV (1ULL << 44)
Willy Tarreau87951942019-08-30 07:34:36 +0200327 { .mask = H2_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200328#define H2_EV_STRM_SEND (1ULL << 45)
Willy Tarreau87951942019-08-30 07:34:36 +0200329 { .mask = H2_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200330#define H2_EV_STRM_FULL (1ULL << 46)
Willy Tarreau87951942019-08-30 07:34:36 +0200331 { .mask = H2_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200332#define H2_EV_STRM_WAKE (1ULL << 47)
Willy Tarreau87951942019-08-30 07:34:36 +0200333 { .mask = H2_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200334#define H2_EV_STRM_SHUT (1ULL << 48)
Willy Tarreau87951942019-08-30 07:34:36 +0200335 { .mask = H2_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200336#define H2_EV_STRM_END (1ULL << 49)
Willy Tarreau87951942019-08-30 07:34:36 +0200337 { .mask = H2_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200338#define H2_EV_STRM_ERR (1ULL << 50)
Willy Tarreau87951942019-08-30 07:34:36 +0200339 { .mask = H2_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200340#define H2_EV_PROTO_ERR (1ULL << 51)
Willy Tarreau87951942019-08-30 07:34:36 +0200341 { .mask = H2_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200342 { }
343};
344
345static const struct name_desc h2_trace_lockon_args[4] = {
346 /* arg1 */ { /* already used by the connection */ },
347 /* arg2 */ { .name="h2s", .desc="H2 stream" },
348 /* arg3 */ { },
349 /* arg4 */ { }
350};
351
352static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200353#define H2_VERB_CLEAN 1
354 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
355#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200356 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200357#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200358 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200359#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200360 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200361#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200362 { .name="complete", .desc="add full data dump when available" },
363 { /* end */ }
364};
365
366static struct trace_source trace_h2 = {
367 .name = IST("h2"),
368 .desc = "HTTP/2 multiplexer",
369 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200370 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200371 .known_events = h2_trace_events,
372 .lockon_args = h2_trace_lockon_args,
373 .decoding = h2_trace_decoding,
374 .report_events = ~0, // report everything by default
375};
376
377#define TRACE_SOURCE &trace_h2
378INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
379
Willy Tarreau8ceae722018-11-26 11:58:30 +0100380/* the h2c connection pool */
381DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
382
383/* the h2s stream pool */
384DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
385
Willy Tarreaudc572362018-12-12 08:08:05 +0100386/* The default connection window size is 65535, it may only be enlarged using
387 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
388 * we'll pretend we already received the difference between the two to send
389 * an equivalent window update to enlarge it to 2G-1.
390 */
391#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
392
Willy Tarreau455d5682019-05-24 19:42:18 +0200393/* maximum amount of data we're OK with re-aligning for buffer optimizations */
394#define MAX_DATA_REALIGN 1024
395
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200396/* a few settings from the global section */
397static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200398static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100399static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100400static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200401
Willy Tarreau2a856182017-05-16 15:20:39 +0200402/* a dmumy closed stream */
403static const struct h2s *h2_closed_stream = &(const struct h2s){
404 .cs = NULL,
405 .h2c = NULL,
406 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100407 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100408 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200409 .id = 0,
410};
411
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100412/* a dmumy closed stream returning a PROTOCOL_ERROR error */
413static const struct h2s *h2_error_stream = &(const struct h2s){
414 .cs = NULL,
415 .h2c = NULL,
416 .st = H2_SS_CLOSED,
417 .errcode = H2_ERR_PROTOCOL_ERROR,
418 .flags = 0,
419 .id = 0,
420};
421
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100422/* a dmumy closed stream returning a REFUSED_STREAM error */
423static const struct h2s *h2_refused_stream = &(const struct h2s){
424 .cs = NULL,
425 .h2c = NULL,
426 .st = H2_SS_CLOSED,
427 .errcode = H2_ERR_REFUSED_STREAM,
428 .flags = 0,
429 .id = 0,
430};
431
Willy Tarreau2a856182017-05-16 15:20:39 +0200432/* and a dummy idle stream for use with any unannounced stream */
433static const struct h2s *h2_idle_stream = &(const struct h2s){
434 .cs = NULL,
435 .h2c = NULL,
436 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100437 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200438 .id = 0,
439};
440
Olivier Houchard9f6af332018-05-25 14:04:04 +0200441static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200442static int h2_send(struct h2c *h2c);
443static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200444static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200445static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100446static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100447static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100448static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200449static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100450static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100451static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200452
Willy Tarreauab2ec452019-08-30 07:07:08 +0200453/* returns a h2c state as an abbreviated 3-letter string, or "???" if unknown */
454static inline const char *h2c_st_to_str(enum h2_cs st)
455{
456 switch (st) {
457 case H2_CS_PREFACE: return "PRF";
458 case H2_CS_SETTINGS1: return "STG";
459 case H2_CS_FRAME_H: return "FRH";
460 case H2_CS_FRAME_P: return "FRP";
461 case H2_CS_FRAME_A: return "FRA";
462 case H2_CS_FRAME_E: return "FRE";
463 case H2_CS_ERROR: return "ERR";
464 case H2_CS_ERROR2: return "ER2";
465 default: return "???";
466 }
467}
468
469/* returns a h2s state as an abbreviated 3-letter string, or "???" if unknown */
470static inline const char *h2s_st_to_str(enum h2_ss st)
471{
472 switch (st) {
473 case H2_SS_IDLE: return "IDL"; // idle
474 case H2_SS_RLOC: return "RSL"; // reserved local
475 case H2_SS_RREM: return "RSR"; // reserved remote
476 case H2_SS_OPEN: return "OPN"; // open
477 case H2_SS_HREM: return "HCR"; // half-closed remote
478 case H2_SS_HLOC: return "HCL"; // half-closed local
479 case H2_SS_ERROR : return "ERR"; // error
480 case H2_SS_CLOSED: return "CLO"; // closed
481 default: return "???";
482 }
483}
484
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200485/* the H2 traces always expect that arg1, if non-null, is of type connection
486 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
487 * that arg3, if non-null, is either of type htx for tx headers, or of type
488 * buffer for everything else.
489 */
490static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
491 const struct ist where, const struct ist func,
492 const void *a1, const void *a2, const void *a3, const void *a4)
493{
494 const struct connection *conn = a1;
495 const struct h2c *h2c = conn ? conn->ctx : NULL;
496 const struct h2s *h2s = a2;
497 const struct buffer *buf = a3;
498 const struct htx *htx;
499 int pos;
500
501 if (!h2c) // nothing to add
502 return;
503
Willy Tarreau17104d42019-08-30 07:12:55 +0200504 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau70b1e502019-08-30 11:52:59 +0200505 if (!h2s || h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau17104d42019-08-30 07:12:55 +0200506 chunk_appendf(&trace_buf, " : h2c=%p(%s)", h2c, h2c_st_to_str(h2c->st0));
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200507 else if (h2s->id <= 0)
Willy Tarreau17104d42019-08-30 07:12:55 +0200508 chunk_appendf(&trace_buf, " : h2c=%p(%s) dsi=%d h2s=%p(%d,%s)", h2c, h2c_st_to_str(h2c->st0), h2c->dsi, h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200509 else
Willy Tarreau17104d42019-08-30 07:12:55 +0200510 chunk_appendf(&trace_buf, " : h2c=%p(%s) h2s=%p(%d,%s)", h2c, h2c_st_to_str(h2c->st0), h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200511 }
512
513 /* Let's dump decoded requests and responses right after parsing. They
514 * are traced at level USER with a few recognizable flags.
515 */
516 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
517 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
518 htx = htxbuf(buf); // recv req/res
519 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
520 htx = a3; // send req/res
521 else
522 htx = NULL;
523
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200524 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200525 const struct htx_blk *blk = htx_get_blk(htx, pos);
526 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
527 enum htx_blk_type type = htx_get_blk_type(blk);
528
529 if (type == HTX_BLK_REQ_SL)
530 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200531 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200532 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
533 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
534 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
535 else if (type == HTX_BLK_RES_SL)
536 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200537 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200538 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
539 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
540 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
541 }
542}
543
Olivier Houchard7a977432019-03-21 15:47:13 +0100544static __inline int
545h2c_is_dead(struct h2c *h2c)
546{
547 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
548 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
549 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
550 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200551 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100552 (conn_xprt_read0_pending(h2c->conn) ||
553 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
554 return 1;
555
556 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100557}
558
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200559/*****************************************************/
560/* functions below are for dynamic buffer management */
561/*****************************************************/
562
Willy Tarreau315d8072017-12-10 22:17:57 +0100563/* indicates whether or not the we may call the h2_recv() function to attempt
564 * to receive data into the buffer and/or demux pending data. The condition is
565 * a bit complex due to some API limits for now. The rules are the following :
566 * - if an error or a shutdown was detected on the connection and the buffer
567 * is empty, we must not attempt to receive
568 * - if the demux buf failed to be allocated, we must not try to receive and
569 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100570 * - if no flag indicates a blocking condition, we may attempt to receive,
571 * regardless of whether the demux buffer is full or not, so that only
572 * de demux part decides whether or not to block. This is needed because
573 * the connection API indeed prevents us from re-enabling receipt that is
574 * already enabled in a polled state, so we must always immediately stop
575 * as soon as the demux can't proceed so as never to hit an end of read
576 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100577 * - otherwise must may not attempt
578 */
579static inline int h2_recv_allowed(const struct h2c *h2c)
580{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200581 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100582 (h2c->st0 >= H2_CS_ERROR ||
583 h2c->conn->flags & CO_FL_ERROR ||
584 conn_xprt_read0_pending(h2c->conn)))
585 return 0;
586
587 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100588 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100589 return 1;
590
591 return 0;
592}
593
Willy Tarreau47b515a2018-12-21 16:09:41 +0100594/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200595static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100596{
597 if (!h2_recv_allowed(h2c))
598 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200599 if ((!consider_buffer || !b_data(&h2c->dbuf))
600 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100601 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200602 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100603}
604
605
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100606/* returns true if the front connection has too many conn_streams attached */
607static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200608{
Willy Tarreaua8754662018-12-23 20:43:58 +0100609 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200610}
611
Willy Tarreau44e973f2018-03-01 17:49:30 +0100612/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
613 * flags are used to figure what buffer was requested. It returns 1 if the
614 * allocation succeeds, in which case the connection is woken up, or 0 if it's
615 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200616 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100617static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200618{
619 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100620 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200621
Willy Tarreau44e973f2018-03-01 17:49:30 +0100622 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200623 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200624 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200625 return 1;
626 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200627
Willy Tarreau51330962019-05-26 09:38:07 +0200628 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100629 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200630
631 if (h2c->flags & H2_CF_DEM_MROOM) {
632 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200633 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200634 }
Willy Tarreau14398122017-09-22 14:26:04 +0200635 return 1;
636 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100637
638 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
639 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200640 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100641 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200642 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100643 return 1;
644 }
645
Willy Tarreau14398122017-09-22 14:26:04 +0200646 return 0;
647}
648
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200649static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200650{
651 struct buffer *buf = NULL;
652
Willy Tarreauc234ae32019-05-13 17:56:11 +0200653 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100654 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
655 h2c->buf_wait.target = h2c;
656 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100657 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100658 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100659 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200660 __conn_xprt_stop_recv(h2c->conn);
661 }
662 return buf;
663}
664
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200665static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200666{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200667 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100668 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200669 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200670 }
671}
672
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200673static inline void h2_release_mbuf(struct h2c *h2c)
674{
675 struct buffer *buf;
676 unsigned int count = 0;
677
678 while (b_size(buf = br_head_pick(h2c->mbuf))) {
679 b_free(buf);
680 count++;
681 }
682 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200683 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200684}
685
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100686/* returns the number of allocatable outgoing streams for the connection taking
687 * the last_sid and the reserved ones into account.
688 */
689static inline int h2_streams_left(const struct h2c *h2c)
690{
691 int ret;
692
693 /* consider the number of outgoing streams we're allowed to create before
694 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
695 * nb_reserved is the number of streams which don't yet have an ID.
696 */
697 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
698 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
699 if (ret < 0)
700 ret = 0;
701 return ret;
702}
703
Willy Tarreau00f18a32019-01-26 12:19:01 +0100704/* returns the number of streams in use on a connection to figure if it's
705 * idle or not. We check nb_cs and not nb_streams as the caller will want
706 * to know if it was the last one after a detach().
707 */
708static int h2_used_streams(struct connection *conn)
709{
710 struct h2c *h2c = conn->ctx;
711
712 return h2c->nb_cs;
713}
714
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100715/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100716static int h2_avail_streams(struct connection *conn)
717{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100718 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100719 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100720 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100721
Willy Tarreau6afec462019-01-28 06:40:19 +0100722 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
723 * streams on the connection.
724 */
725 if (h2c->last_sid >= 0)
726 return 0;
727
Willy Tarreau86949782019-01-31 10:42:05 +0100728 /* note: may be negative if a SETTINGS frame changes the limit */
729 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100730
731 /* we must also consider the limit imposed by stream IDs */
732 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100733 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100734 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100735 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
736 ret1 = MIN(ret1, ret2);
737 }
738 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100739}
740
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200741
Willy Tarreau62f52692017-10-08 23:01:42 +0200742/*****************************************************************/
743/* functions below are dedicated to the mux setup and management */
744/*****************************************************************/
745
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200746/* Initialize the mux once it's attached. For outgoing connections, the context
747 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200748 * connections from the fact that the context is still NULL (even during mux
749 * upgrades). <input> is always used as Input buffer and may contain data. It is
750 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200751 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200752static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
753 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200754{
755 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100756 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200757
Willy Tarreau7838a792019-08-12 18:42:03 +0200758 TRACE_ENTER(H2_EV_H2C_NEW, conn);
759
Willy Tarreaubafbe012017-11-24 17:34:44 +0100760 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200761 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200762 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200763
Christopher Faulete9b70722019-04-08 10:46:02 +0200764 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200765 h2c->flags = H2_CF_IS_BACK;
766 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
767 if (tick_isset(prx->timeout.serverfin))
768 h2c->shut_timeout = prx->timeout.serverfin;
769 } else {
770 h2c->flags = H2_CF_NONE;
771 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
772 if (tick_isset(prx->timeout.clientfin))
773 h2c->shut_timeout = prx->timeout.clientfin;
774 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100775
Willy Tarreau0b37d652018-10-03 10:33:02 +0200776 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100777 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100778 if (tick_isset(h2c->timeout)) {
779 t = task_new(tid_bit);
780 if (!t)
781 goto fail;
782
783 h2c->task = t;
784 t->process = h2_timeout_task;
785 t->context = h2c;
786 t->expire = tick_add(now_ms, h2c->timeout);
787 }
Willy Tarreauea392822017-10-31 10:02:25 +0100788
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200789 h2c->wait_event.tasklet = tasklet_new();
790 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200791 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200792 h2c->wait_event.tasklet->process = h2_io_cb;
793 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100794 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200795
Willy Tarreau32218eb2017-09-22 08:07:25 +0200796 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
797 if (!h2c->ddht)
798 goto fail;
799
800 /* Initialise the context. */
801 h2c->st0 = H2_CS_PREFACE;
802 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100803 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200804 h2c->max_id = -1;
805 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100806 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200807 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100808 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200809 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100810 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100811 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200812
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200813 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200814 h2c->dsi = -1;
815 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100816
Willy Tarreau32218eb2017-09-22 08:07:25 +0200817 h2c->last_sid = -1;
818
Willy Tarreau51330962019-05-26 09:38:07 +0200819 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200820 h2c->miw = 65535; /* mux initial window size */
821 h2c->mws = 65535; /* mux window size */
822 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200823 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200824 LIST_INIT(&h2c->send_list);
825 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200826 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100827 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200828
Willy Tarreau3f133572017-10-31 19:21:06 +0100829 if (t)
830 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100831
Willy Tarreau01b44822018-10-03 14:26:37 +0200832 if (h2c->flags & H2_CF_IS_BACK) {
833 /* FIXME: this is temporary, for outgoing connections we need
834 * to immediately allocate a stream until the code is modified
835 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100836 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200837 */
838 struct h2s *h2s;
839
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100840 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200841 if (!h2s)
842 goto fail_stream;
843 }
844
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100845 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200846
Willy Tarreau0f383582018-10-03 14:22:21 +0200847 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200848 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +0200849 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200850 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200851 fail_stream:
852 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200853 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200854 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200855 if (h2c->wait_event.tasklet)
856 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100857 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200858 fail_no_h2c:
Willy Tarreau7838a792019-08-12 18:42:03 +0200859 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200860 return -1;
861}
862
Willy Tarreau751f2d02018-10-05 09:35:00 +0200863/* returns the next allocatable outgoing stream ID for the H2 connection, or
864 * -1 if no more is allocatable.
865 */
866static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
867{
868 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100869
870 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200871 id = -1;
872 return id;
873}
874
Willy Tarreau2373acc2017-10-12 17:35:14 +0200875/* returns the stream associated with id <id> or NULL if not found */
876static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
877{
878 struct eb32_node *node;
879
Willy Tarreau751f2d02018-10-05 09:35:00 +0200880 if (id == 0)
881 return (struct h2s *)h2_closed_stream;
882
Willy Tarreau2a856182017-05-16 15:20:39 +0200883 if (id > h2c->max_id)
884 return (struct h2s *)h2_idle_stream;
885
Willy Tarreau2373acc2017-10-12 17:35:14 +0200886 node = eb32_lookup(&h2c->streams_by_id, id);
887 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200888 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200889
890 return container_of(node, struct h2s, by_id);
891}
892
Christopher Faulet73c12072019-04-08 11:23:22 +0200893/* release function. This one should be called to free all resources allocated
894 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200895 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200896static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200897{
Christopher Faulet61840e72019-04-15 09:33:32 +0200898 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200899
Willy Tarreau7838a792019-08-12 18:42:03 +0200900 TRACE_ENTER(H2_EV_H2C_END);
901
Willy Tarreau32218eb2017-09-22 08:07:25 +0200902 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200903 /* The connection must be aattached to this mux to be released */
904 if (h2c->conn && h2c->conn->ctx == h2c)
905 conn = h2c->conn;
906
Willy Tarreau7838a792019-08-12 18:42:03 +0200907 TRACE_DEVEL("freeing h2c", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200908 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200909
Willy Tarreau186e96e2019-05-28 17:21:18 +0200910 if (LIST_ADDED(&h2c->buf_wait.list)) {
911 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
912 LIST_DEL(&h2c->buf_wait.list);
913 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
914 }
Willy Tarreau14398122017-09-22 14:26:04 +0200915
Willy Tarreau44e973f2018-03-01 17:49:30 +0100916 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200917 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100918
Willy Tarreauea392822017-10-31 10:02:25 +0100919 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200920 h2c->task->context = NULL;
921 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100922 h2c->task = NULL;
923 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200924 if (h2c->wait_event.tasklet)
925 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet21d849f2019-09-18 11:07:20 +0200926 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100927 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet21d849f2019-09-18 11:07:20 +0200928 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100929
Willy Tarreaubafbe012017-11-24 17:34:44 +0100930 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200931 }
932
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200933 if (conn) {
934 conn->mux = NULL;
935 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +0200936 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200937
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200938 conn_stop_tracking(conn);
939 conn_full_close(conn);
940 if (conn->destroy_cb)
941 conn->destroy_cb(conn);
942 conn_free(conn);
943 }
Willy Tarreau7838a792019-08-12 18:42:03 +0200944
945 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +0200946}
947
948
Willy Tarreau71681172017-10-23 14:39:06 +0200949/******************************************************/
950/* functions below are for the H2 protocol processing */
951/******************************************************/
952
953/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100954static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200955{
956 return h2s ? h2s->id : 0;
957}
958
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200959/* returns the sum of the stream's own window size and the mux's initial
960 * window, which together form the stream's effective window size.
961 */
962static inline int h2s_mws(const struct h2s *h2s)
963{
964 return h2s->sws + h2s->h2c->miw;
965}
966
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200967/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100968static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200969{
970 if (h2c->msi < 0)
971 return 0;
972
973 if (h2c->msi == h2s_id(h2s))
974 return 0;
975
976 return 1;
977}
978
Willy Tarreau741d6df2017-10-17 08:00:59 +0200979/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100980static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200981{
Willy Tarreau7838a792019-08-12 18:42:03 +0200982 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn,,, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +0200983 h2c->errcode = err;
984 h2c->st0 = H2_CS_ERROR;
985}
986
Willy Tarreau175cebb2019-01-24 10:02:24 +0100987/* marks an error on the stream. It may also update an already closed stream
988 * (e.g. to report an error after an RST was received).
989 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100990static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200991{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100992 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +0200993 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s,, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +0200994 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100995 if (h2s->st < H2_SS_ERROR)
996 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100997 if (h2s->cs)
998 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200999 }
1000}
1001
Willy Tarreau7e094452018-12-19 18:08:52 +01001002/* attempt to notify the data layer of recv availability */
1003static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1004{
1005 struct wait_event *sw;
1006
1007 if (h2s->recv_wait) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001008 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau7e094452018-12-19 18:08:52 +01001009 sw = h2s->recv_wait;
1010 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001011 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +01001012 h2s->recv_wait = NULL;
1013 }
1014}
1015
1016/* attempt to notify the data layer of send availability */
1017static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1018{
1019 struct wait_event *sw;
1020
Willy Tarreauc234ae32019-05-13 17:56:11 +02001021 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001022 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau7e094452018-12-19 18:08:52 +01001023 sw = h2s->send_wait;
1024 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +01001025 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001026 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +01001027 }
1028}
1029
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001030/* alerts the data layer, trying to wake it up by all means, following
1031 * this sequence :
1032 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1033 * - if its subscribed to send, then it's woken up for send
1034 * - if it was subscribed to neither, its ->wake() callback is called
1035 * It is safe to call this function with a closed stream which doesn't have a
1036 * conn_stream anymore.
1037 */
1038static void __maybe_unused h2s_alert(struct h2s *h2s)
1039{
Willy Tarreau7838a792019-08-12 18:42:03 +02001040 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1041
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001042 if (h2s->recv_wait || h2s->send_wait) {
1043 h2s_notify_recv(h2s);
1044 h2s_notify_send(h2s);
1045 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001046 else if (h2s->cs && h2s->cs->data_cb->wake != NULL) {
1047 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001048 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau7838a792019-08-12 18:42:03 +02001049 }
1050
1051 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001052}
1053
Willy Tarreaue4820742017-07-27 13:37:23 +02001054/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001055static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001056{
1057 uint8_t *out = frame;
1058
1059 *out = len >> 16;
1060 write_n16(out + 1, len);
1061}
1062
Willy Tarreau54c15062017-10-10 17:10:03 +02001063/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1064 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1065 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001066 * available in the buffer's input prior to calling this function. The buffer
1067 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001068 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001069static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001070 const struct buffer *b, int o)
1071{
Willy Tarreau591d4452018-06-15 17:21:00 +02001072 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 +02001073}
1074
Willy Tarreau1f094672017-11-20 21:27:45 +01001075static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001076{
Willy Tarreau591d4452018-06-15 17:21:00 +02001077 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001078}
1079
Willy Tarreau1f094672017-11-20 21:27:45 +01001080static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001081{
Willy Tarreau591d4452018-06-15 17:21:00 +02001082 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001083}
1084
Willy Tarreau1f094672017-11-20 21:27:45 +01001085static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001086{
Willy Tarreau591d4452018-06-15 17:21:00 +02001087 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001088}
1089
1090
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001091/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1092 * The algorithm is not obvious. It turns out that H2 headers are neither
1093 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1094 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001095 *
1096 * b0 b1 b2 b3 b4 b5..b8
1097 * +----------+---------+--------+----+----+----------------------+
1098 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1099 * +----------+---------+--------+----+----+----------------------+
1100 *
1101 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1102 * we get the sid properly aligned and ordered, and 16 bits of len properly
1103 * ordered as well. The type and flags can be extracted using bit shifts from
1104 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001105 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1106 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001107 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001108static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001109{
1110 uint64_t w;
1111
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001112 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001113 return 0;
1114
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001115 w = h2_get_n64(b, o + 1);
1116 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001117 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1118 h->ff = w >> 32;
1119 h->ft = w >> 40;
1120 h->len += w >> 48;
1121 return 1;
1122}
1123
1124/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1125 * h2_peek_frame_hdr() above.
1126 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001127static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001128{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001129 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001130}
1131
1132/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001133static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001134{
1135 int ret;
1136
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001137 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001138 if (ret > 0)
1139 h2_skip_frame_hdr(b);
1140 return ret;
1141}
1142
Willy Tarreau00dd0782018-03-01 16:31:34 +01001143/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1144 * its connection if the stream was not yet closed. Please use this exclusively
1145 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001146 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001147static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001148{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001149 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001150 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001151 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001152 if (!h2s->id)
1153 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +01001154 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +01001155 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
1156 h2s_notify_recv(h2s);
1157 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001158 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001159 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001160 h2s->st = H2_SS_CLOSED;
1161}
1162
Willy Tarreau71049cc2018-03-28 13:56:39 +02001163/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
1164static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001165{
Willy Tarreau7838a792019-08-12 18:42:03 +02001166 struct connection *conn = h2s->h2c->conn;
1167
1168 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1169
Willy Tarreau0a10de62018-03-01 16:27:53 +01001170 h2s_close(h2s);
1171 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001172 if (b_size(&h2s->rxbuf)) {
1173 b_free(&h2s->rxbuf);
1174 offer_buffers(NULL, tasks_run_queue);
1175 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001176 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001177 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001178 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001179 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -08001180 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001181 * reference left would be in the h2c send_list/fctl_list, and if
1182 * we're in it, we're getting out anyway
1183 */
Olivier Houchardd360ac62019-03-22 17:37:16 +01001184 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +02001185 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +02001186 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02001187 LIST_DEL_INIT(&h2s->sending_list);
1188 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001189 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001190 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001191
1192 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001193}
1194
Willy Tarreaua8e49542018-10-03 18:53:55 +02001195/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1196 * stream tree. In case of error, nothing is added and NULL is returned. The
1197 * causes of errors can be any failed memory allocation. The caller is
1198 * responsible for checking if the connection may support an extra stream
1199 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001200 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001201static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001202{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001203 struct h2s *h2s;
1204
Willy Tarreau7838a792019-08-12 18:42:03 +02001205 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1206
Willy Tarreaubafbe012017-11-24 17:34:44 +01001207 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001208 if (!h2s)
1209 goto out;
1210
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001211 h2s->wait_event.tasklet = tasklet_new();
1212 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001213 pool_free(pool_head_h2s, h2s);
1214 goto out;
1215 }
1216 h2s->send_wait = NULL;
1217 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001218 h2s->wait_event.tasklet->process = h2_deferred_shut;
1219 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001220 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001221 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01001222 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001223 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001224 h2s->cs = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001225 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001226 h2s->flags = H2_SF_NONE;
1227 h2s->errcode = H2_ERR_NO_ERROR;
1228 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001229 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001230 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001231 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001232
1233 if (h2c->flags & H2_CF_IS_BACK) {
1234 h1m_init_req(&h2s->h1m);
1235 h2s->h1m.err_pos = -1; // don't care about errors on the request path
1236 h2s->h1m.flags |= H1_MF_TOLOWER;
1237 } else {
1238 h1m_init_res(&h2s->h1m);
1239 h2s->h1m.err_pos = -1; // don't care about errors on the response path
1240 h2s->h1m.flags |= H1_MF_TOLOWER;
1241 }
1242
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001243 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001244 if (id > 0)
1245 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001246 else
1247 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001248
1249 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001250 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001251 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001252
Willy Tarreau7838a792019-08-12 18:42:03 +02001253 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001254 return h2s;
1255
1256 out_free_h2s:
1257 pool_free(pool_head_h2s, h2s);
1258 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001259 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001260 return NULL;
1261}
1262
1263/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1264 * case of memory allocation error.
1265 */
1266static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1267{
1268 struct session *sess = h2c->conn->owner;
1269 struct conn_stream *cs;
1270 struct h2s *h2s;
1271
Willy Tarreau7838a792019-08-12 18:42:03 +02001272 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1273
Willy Tarreaua8e49542018-10-03 18:53:55 +02001274 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1275 goto out;
1276
1277 h2s = h2s_new(h2c, id);
1278 if (!h2s)
1279 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001280
1281 cs = cs_new(h2c->conn);
1282 if (!cs)
1283 goto out_close;
1284
Olivier Houchard746fb772018-12-15 19:42:00 +01001285 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001286 h2s->cs = cs;
1287 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001288 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001289
1290 if (stream_create_from_cs(cs) < 0)
1291 goto out_free_cs;
1292
Willy Tarreau590a0512018-09-05 11:56:48 +02001293 /* We want the accept date presented to the next stream to be the one
1294 * we have now, the handshake time to be null (since the next stream
1295 * is not delayed by a handshake), and the idle time to count since
1296 * right now.
1297 */
1298 sess->accept_date = date;
1299 sess->tv_accept = now;
1300 sess->t_handshake = 0;
1301
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001302 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001303 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001304 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001305 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001306 return h2s;
1307
1308 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001309 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001310 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001311 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001312 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001313 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001314 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001315 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001316 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001317 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001318}
1319
Willy Tarreau751f2d02018-10-05 09:35:00 +02001320/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1321 * and returns it, or NULL in case of memory allocation error or if the highest
1322 * possible stream ID was reached.
1323 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001324static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001325{
1326 struct h2s *h2s = NULL;
1327
Willy Tarreau7838a792019-08-12 18:42:03 +02001328 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1329
Willy Tarreau86949782019-01-31 10:42:05 +01001330 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001331 goto out;
1332
Willy Tarreaua80dca82019-01-24 17:08:28 +01001333 if (h2_streams_left(h2c) < 1)
1334 goto out;
1335
Willy Tarreau751f2d02018-10-05 09:35:00 +02001336 /* Defer choosing the ID until we send the first message to create the stream */
1337 h2s = h2s_new(h2c, 0);
1338 if (!h2s)
1339 goto out;
1340
1341 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001342 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001343 cs->ctx = h2s;
1344 h2c->nb_cs++;
1345
Willy Tarreau751f2d02018-10-05 09:35:00 +02001346 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001347 if (likely(h2s))
1348 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1349 else
1350 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001351 return h2s;
1352}
1353
Willy Tarreaube5b7152017-09-25 16:25:39 +02001354/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1355 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1356 * the various settings codes.
1357 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001358static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001359{
1360 struct buffer *res;
1361 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001362 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001363 int mfs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001364 int ret = 0;
1365
1366 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001367
1368 if (h2c_mux_busy(h2c, NULL)) {
1369 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001370 goto out;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001371 }
1372
Willy Tarreaube5b7152017-09-25 16:25:39 +02001373 chunk_init(&buf, buf_data, sizeof(buf_data));
1374 chunk_memcpy(&buf,
1375 "\x00\x00\x00" /* length : 0 for now */
1376 "\x04\x00" /* type : 4 (settings), flags : 0 */
1377 "\x00\x00\x00\x00", /* stream ID : 0 */
1378 9);
1379
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001380 if (h2c->flags & H2_CF_IS_BACK) {
1381 /* send settings_enable_push=0 */
1382 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1383 }
1384
Willy Tarreaube5b7152017-09-25 16:25:39 +02001385 if (h2_settings_header_table_size != 4096) {
1386 char str[6] = "\x00\x01"; /* header_table_size */
1387
1388 write_n32(str + 2, h2_settings_header_table_size);
1389 chunk_memcat(&buf, str, 6);
1390 }
1391
1392 if (h2_settings_initial_window_size != 65535) {
1393 char str[6] = "\x00\x04"; /* initial_window_size */
1394
1395 write_n32(str + 2, h2_settings_initial_window_size);
1396 chunk_memcat(&buf, str, 6);
1397 }
1398
1399 if (h2_settings_max_concurrent_streams != 0) {
1400 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1401
1402 /* Note: 0 means "unlimited" for haproxy's config but not for
1403 * the protocol, so never send this value!
1404 */
1405 write_n32(str + 2, h2_settings_max_concurrent_streams);
1406 chunk_memcat(&buf, str, 6);
1407 }
1408
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001409 mfs = h2_settings_max_frame_size;
1410 if (mfs > global.tune.bufsize)
1411 mfs = global.tune.bufsize;
1412
1413 if (!mfs)
1414 mfs = global.tune.bufsize;
1415
1416 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001417 char str[6] = "\x00\x05"; /* max_frame_size */
1418
1419 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1420 * match bufsize - rewrite size, but at the moment it seems
1421 * that clients don't take care of it.
1422 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001423 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001424 chunk_memcat(&buf, str, 6);
1425 }
1426
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001427 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001428
1429 res = br_tail(h2c->mbuf);
1430 retry:
1431 if (!h2_get_buf(h2c, res)) {
1432 h2c->flags |= H2_CF_MUX_MALLOC;
1433 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001434 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001435 }
1436
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001437 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001438 if (unlikely(ret <= 0)) {
1439 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001440 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1441 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001442 h2c->flags |= H2_CF_MUX_MFULL;
1443 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001444 }
1445 else {
1446 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001447 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001448 }
1449 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001450 out:
1451 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001452 return ret;
1453}
1454
Willy Tarreau52eed752017-09-22 15:05:09 +02001455/* Try to receive a connection preface, then upon success try to send our
1456 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1457 * missing data. It may return an error in h2c.
1458 */
1459static int h2c_frt_recv_preface(struct h2c *h2c)
1460{
1461 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001462 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001463
Willy Tarreau7838a792019-08-12 18:42:03 +02001464 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1465
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001466 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001467
1468 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001469 if (ret1 < 0)
1470 sess_log(h2c->conn->owner);
1471
Willy Tarreau52eed752017-09-22 15:05:09 +02001472 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1473 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001474 ret2 = 0;
1475 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001476 }
1477
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001478 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001479 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001480 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001481 out:
1482 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001483 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001484}
1485
Willy Tarreau01b44822018-10-03 14:26:37 +02001486/* Try to send a connection preface, then upon success try to send our
1487 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1488 * missing data. It may return an error in h2c.
1489 */
1490static int h2c_bck_send_preface(struct h2c *h2c)
1491{
1492 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001493 int ret = 0;
1494
1495 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001496
1497 if (h2c_mux_busy(h2c, NULL)) {
1498 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001499 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001500 }
1501
Willy Tarreaubcc45952019-05-26 10:05:50 +02001502 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001503 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001504 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001505 h2c->flags |= H2_CF_MUX_MALLOC;
1506 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001507 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001508 }
1509
1510 if (!b_data(res)) {
1511 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001512 ret = b_istput(res, ist(H2_CONN_PREFACE));
1513 if (unlikely(ret <= 0)) {
1514 if (!ret) {
1515 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1516 goto retry;
1517 h2c->flags |= H2_CF_MUX_MFULL;
1518 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001519 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001520 }
1521 else {
1522 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001523 ret = 0;
1524 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001525 }
1526 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001527 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001528 ret = h2c_send_settings(h2c);
1529 out:
1530 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1531 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001532}
1533
Willy Tarreau081d4722017-05-16 21:51:05 +02001534/* try to send a GOAWAY frame on the connection to report an error or a graceful
1535 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1536 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1537 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1538 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1539 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1540 * on unrecoverable failure. It will not attempt to send one again in this last
1541 * case so that it is safe to use h2c_error() to report such errors.
1542 */
1543static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1544{
1545 struct buffer *res;
1546 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001547 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001548
Willy Tarreau7838a792019-08-12 18:42:03 +02001549 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1550
1551 if (h2c->flags & H2_CF_GOAWAY_FAILED) {
1552 ret = 1; // claim that it worked
1553 goto out;
1554 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001555
1556 if (h2c_mux_busy(h2c, h2s)) {
1557 if (h2s)
1558 h2s->flags |= H2_SF_BLK_MBUSY;
1559 else
1560 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001561 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001562 }
1563
Willy Tarreau9c218e72019-05-26 10:08:28 +02001564 /* len: 8, type: 7, flags: none, sid: 0 */
1565 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1566
1567 if (h2c->last_sid < 0)
1568 h2c->last_sid = h2c->max_id;
1569
1570 write_n32(str + 9, h2c->last_sid);
1571 write_n32(str + 13, h2c->errcode);
1572
Willy Tarreaubcc45952019-05-26 10:05:50 +02001573 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001574 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001575 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001576 h2c->flags |= H2_CF_MUX_MALLOC;
1577 if (h2s)
1578 h2s->flags |= H2_SF_BLK_MROOM;
1579 else
1580 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001581 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001582 }
1583
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001584 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001585 if (unlikely(ret <= 0)) {
1586 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001587 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1588 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001589 h2c->flags |= H2_CF_MUX_MFULL;
1590 if (h2s)
1591 h2s->flags |= H2_SF_BLK_MROOM;
1592 else
1593 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001594 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001595 }
1596 else {
1597 /* we cannot report this error using GOAWAY, so we mark
1598 * it and claim a success.
1599 */
1600 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1601 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001602 ret = 1;
1603 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001604 }
1605 }
1606 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02001607 out:
1608 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001609 return ret;
1610}
1611
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001612/* Try to send an RST_STREAM frame on the connection for the indicated stream
1613 * during mux operations. This stream must be valid and cannot be closed
1614 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1615 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1616 * not yet.
1617 *
1618 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1619 * to write the message, it subscribes the stream to future notifications.
1620 */
1621static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1622{
1623 struct buffer *res;
1624 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001625 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001626
Willy Tarreau7838a792019-08-12 18:42:03 +02001627 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1628
1629 if (!h2s || h2s->st == H2_SS_CLOSED) {
1630 ret = 1;
1631 goto out;
1632 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001633
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001634 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1635 * RST_STREAM in response to a RST_STREAM frame.
1636 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001637 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001638 ret = 1;
1639 goto ignore;
1640 }
1641
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001642 if (h2c_mux_busy(h2c, h2s)) {
1643 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001644 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001645 }
1646
Willy Tarreau9c218e72019-05-26 10:08:28 +02001647 /* len: 4, type: 3, flags: none */
1648 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1649 write_n32(str + 5, h2s->id);
1650 write_n32(str + 9, h2s->errcode);
1651
Willy Tarreaubcc45952019-05-26 10:05:50 +02001652 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001653 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001654 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001655 h2c->flags |= H2_CF_MUX_MALLOC;
1656 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001657 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001658 }
1659
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001660 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001661 if (unlikely(ret <= 0)) {
1662 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001663 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1664 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001665 h2c->flags |= H2_CF_MUX_MFULL;
1666 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001667 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001668 }
1669 else {
1670 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001671 ret = 0;
1672 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001673 }
1674 }
1675
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001676 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001677 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001678 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001679 out:
1680 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001681 return ret;
1682}
1683
1684/* Try to send an RST_STREAM frame on the connection for the stream being
1685 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001686 * error code, even if the stream is one of the dummy ones, and will update
1687 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001688 *
1689 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1690 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001691 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001692 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001693 */
1694static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1695{
1696 struct buffer *res;
1697 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001698 int ret = 0;
1699
1700 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001701
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001702 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1703 * RST_STREAM in response to a RST_STREAM frame.
1704 */
1705 if (h2c->dft == H2_FT_RST_STREAM) {
1706 ret = 1;
1707 goto ignore;
1708 }
1709
Willy Tarreau27a84c92017-10-17 08:10:17 +02001710 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001711 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001712 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001713 }
1714
Willy Tarreau9c218e72019-05-26 10:08:28 +02001715 /* len: 4, type: 3, flags: none */
1716 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1717
1718 write_n32(str + 5, h2c->dsi);
1719 write_n32(str + 9, h2s->errcode);
1720
Willy Tarreaubcc45952019-05-26 10:05:50 +02001721 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001722 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001723 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001724 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001725 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001726 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001727 }
1728
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001729 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001730 if (unlikely(ret <= 0)) {
1731 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001732 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1733 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001734 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001735 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001736 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001737 }
1738 else {
1739 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001740 ret = 0;
1741 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001742 }
1743 }
1744
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001745 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001746 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001747 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001748 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001749 }
1750
Willy Tarreau7838a792019-08-12 18:42:03 +02001751 out:
1752 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001753 return ret;
1754}
1755
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001756/* try to send an empty DATA frame with the ES flag set to notify about the
1757 * end of stream and match a shutdown(write). If an ES was already sent as
1758 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1759 * on success or zero if nothing was done. In case of lack of room to write the
1760 * message, it subscribes the requesting stream to future notifications.
1761 */
1762static int h2_send_empty_data_es(struct h2s *h2s)
1763{
1764 struct h2c *h2c = h2s->h2c;
1765 struct buffer *res;
1766 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02001767 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001768
Willy Tarreau7838a792019-08-12 18:42:03 +02001769 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
1770
1771 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
1772 ret = 1;
1773 goto out;
1774 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001775
1776 if (h2c_mux_busy(h2c, h2s)) {
1777 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001778 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001779 }
1780
Willy Tarreau9c218e72019-05-26 10:08:28 +02001781 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1782 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1783 write_n32(str + 5, h2s->id);
1784
Willy Tarreaubcc45952019-05-26 10:05:50 +02001785 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001786 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001787 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001788 h2c->flags |= H2_CF_MUX_MALLOC;
1789 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001790 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001791 }
1792
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001793 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001794 if (likely(ret > 0)) {
1795 h2s->flags |= H2_SF_ES_SENT;
1796 }
1797 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001798 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1799 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001800 h2c->flags |= H2_CF_MUX_MFULL;
1801 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001802 }
1803 else {
1804 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001805 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001806 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001807 out:
1808 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001809 return ret;
1810}
1811
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001812/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001813 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001814 * is automatically updated accordingly. If the stream is orphaned, it is
1815 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001816 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001817static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001818{
Willy Tarreau7838a792019-08-12 18:42:03 +02001819 struct h2c *h2c = h2s->h2c;
1820
1821 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
1822
Christopher Fauletf02ca002019-03-07 16:21:34 +01001823 if (!h2s->cs) {
1824 /* this stream was already orphaned */
1825 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001826 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001827 return;
1828 }
1829
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001830 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001831 if (h2s->st == H2_SS_OPEN)
1832 h2s->st = H2_SS_HREM;
1833 else if (h2s->st == H2_SS_HLOC)
1834 h2s_close(h2s);
1835 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001836
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001837 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1838 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1839 h2s->cs->flags |= CS_FL_ERR_PENDING;
1840 if (h2s->cs->flags & CS_FL_EOS)
1841 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001842
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001843 if (h2s->st < H2_SS_ERROR)
1844 h2s->st = H2_SS_ERROR;
1845 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001846
1847 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001848 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001849}
1850
1851/* wake the streams attached to the connection, whose id is greater than <last>
1852 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001853 */
Willy Tarreau23482912019-05-07 15:23:14 +02001854static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001855{
1856 struct eb32_node *node;
1857 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001858
Willy Tarreau7838a792019-08-12 18:42:03 +02001859 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
1860
Christopher Fauletf02ca002019-03-07 16:21:34 +01001861 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001862 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1863 while (node) {
1864 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001865 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001866 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001867 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001868
Christopher Fauletf02ca002019-03-07 16:21:34 +01001869 /* Wake all streams with unassigned ID (ID == 0) */
1870 node = eb32_lookup(&h2c->streams_by_id, 0);
1871 while (node) {
1872 h2s = container_of(node, struct h2s, by_id);
1873 if (h2s->id > 0)
1874 break;
1875 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001876 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001877 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001878
1879 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001880}
1881
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001882/* Wake up all blocked streams whose window size has become positive after the
1883 * mux's initial window was adjusted. This should be done after having processed
1884 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001885 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001886static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001887{
1888 struct h2s *h2s;
1889 struct eb32_node *node;
1890
Willy Tarreau7838a792019-08-12 18:42:03 +02001891 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
1892
Willy Tarreau3421aba2017-07-27 15:41:03 +02001893 node = eb32_first(&h2c->streams_by_id);
1894 while (node) {
1895 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001896 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001897 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001898 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001899 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001900 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001901 node = eb32_next(node);
1902 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001903
1904 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001905}
1906
1907/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1908 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001909 * return an error in h2c. The caller must have already verified frame length
1910 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001911 */
1912static int h2c_handle_settings(struct h2c *h2c)
1913{
1914 unsigned int offset;
1915 int error;
1916
Willy Tarreau7838a792019-08-12 18:42:03 +02001917 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
1918
Willy Tarreau3421aba2017-07-27 15:41:03 +02001919 if (h2c->dff & H2_F_SETTINGS_ACK) {
1920 if (h2c->dfl) {
1921 error = H2_ERR_FRAME_SIZE_ERROR;
1922 goto fail;
1923 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001924 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001925 }
1926
Willy Tarreau3421aba2017-07-27 15:41:03 +02001927 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001928 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02001929 goto out0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001930
1931 /* parse the frame */
1932 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001933 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1934 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001935
1936 switch (type) {
1937 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1938 /* we need to update all existing streams with the
1939 * difference from the previous iws.
1940 */
1941 if (arg < 0) { // RFC7540#6.5.2
1942 error = H2_ERR_FLOW_CONTROL_ERROR;
1943 goto fail;
1944 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001945 h2c->miw = arg;
1946 break;
1947 case H2_SETTINGS_MAX_FRAME_SIZE:
1948 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1949 error = H2_ERR_PROTOCOL_ERROR;
1950 goto fail;
1951 }
1952 h2c->mfs = arg;
1953 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001954 case H2_SETTINGS_ENABLE_PUSH:
1955 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1956 error = H2_ERR_PROTOCOL_ERROR;
1957 goto fail;
1958 }
1959 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001960 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1961 if (h2c->flags & H2_CF_IS_BACK) {
1962 /* the limit is only for the backend; for the frontend it is our limit */
1963 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1964 arg = h2_settings_max_concurrent_streams;
1965 h2c->streams_limit = arg;
1966 }
1967 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001968 }
1969 }
1970
1971 /* need to ACK this frame now */
1972 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02001973 done:
1974 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001975 return 1;
1976 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001977 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001978 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02001979 out0:
1980 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 +02001981 return 0;
1982}
1983
1984/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1985 * success or one of the h2_status values.
1986 */
1987static int h2c_ack_settings(struct h2c *h2c)
1988{
1989 struct buffer *res;
1990 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02001991 int ret = 0;
1992
1993 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001994
1995 if (h2c_mux_busy(h2c, NULL)) {
1996 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001997 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001998 }
1999
Willy Tarreau9c218e72019-05-26 10:08:28 +02002000 memcpy(str,
2001 "\x00\x00\x00" /* length : 0 (no data) */
2002 "\x04" "\x01" /* type : 4, flags : ACK */
2003 "\x00\x00\x00\x00" /* stream ID */, 9);
2004
Willy Tarreaubcc45952019-05-26 10:05:50 +02002005 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002006 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002007 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002008 h2c->flags |= H2_CF_MUX_MALLOC;
2009 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002010 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002011 }
2012
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002013 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002014 if (unlikely(ret <= 0)) {
2015 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002016 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2017 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002018 h2c->flags |= H2_CF_MUX_MFULL;
2019 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002020 }
2021 else {
2022 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002023 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002024 }
2025 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002026 out:
2027 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002028 return ret;
2029}
2030
Willy Tarreaucf68c782017-10-10 17:11:41 +02002031/* processes a PING frame and schedules an ACK if needed. The caller must pass
2032 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002033 * missing data. The caller must have already verified frame length
2034 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002035 */
2036static int h2c_handle_ping(struct h2c *h2c)
2037{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002038 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002039 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002040 h2c->st0 = H2_CS_FRAME_A;
2041 return 1;
2042}
2043
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002044/* Try to send a window update for stream id <sid> and value <increment>.
2045 * Returns > 0 on success or zero on missing room or failure. It may return an
2046 * error in h2c.
2047 */
2048static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2049{
2050 struct buffer *res;
2051 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002052 int ret = 0;
2053
2054 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002055
2056 if (h2c_mux_busy(h2c, NULL)) {
2057 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002058 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002059 }
2060
Willy Tarreau9c218e72019-05-26 10:08:28 +02002061 /* length: 4, type: 8, flags: none */
2062 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2063 write_n32(str + 5, sid);
2064 write_n32(str + 9, increment);
2065
Willy Tarreaubcc45952019-05-26 10:05:50 +02002066 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002067 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002068 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002069 h2c->flags |= H2_CF_MUX_MALLOC;
2070 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002071 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002072 }
2073
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002074 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002075 if (unlikely(ret <= 0)) {
2076 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002077 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2078 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002079 h2c->flags |= H2_CF_MUX_MFULL;
2080 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002081 }
2082 else {
2083 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002084 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002085 }
2086 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002087 out:
2088 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002089 return ret;
2090}
2091
2092/* try to send pending window update for the connection. It's safe to call it
2093 * with no pending updates. Returns > 0 on success or zero on missing room or
2094 * failure. It may return an error in h2c.
2095 */
2096static int h2c_send_conn_wu(struct h2c *h2c)
2097{
2098 int ret = 1;
2099
Willy Tarreau7838a792019-08-12 18:42:03 +02002100 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2101
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002102 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002103 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002104
Willy Tarreau97aaa672018-12-23 09:49:04 +01002105 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2106 /* increase the advertised connection window to 2G on
2107 * first update.
2108 */
2109 h2c->flags |= H2_CF_WINDOW_OPENED;
2110 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2111 }
2112
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002113 /* send WU for the connection */
2114 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2115 if (ret > 0)
2116 h2c->rcvd_c = 0;
2117
Willy Tarreau7838a792019-08-12 18:42:03 +02002118 out:
2119 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002120 return ret;
2121}
2122
2123/* try to send pending window update for the current dmux stream. It's safe to
2124 * call it with no pending updates. Returns > 0 on success or zero on missing
2125 * room or failure. It may return an error in h2c.
2126 */
2127static int h2c_send_strm_wu(struct h2c *h2c)
2128{
2129 int ret = 1;
2130
Willy Tarreau7838a792019-08-12 18:42:03 +02002131 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2132
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002133 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002134 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002135
2136 /* send WU for the stream */
2137 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2138 if (ret > 0)
2139 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002140 out:
2141 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002142 return ret;
2143}
2144
Willy Tarreaucf68c782017-10-10 17:11:41 +02002145/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2146 * success, 0 on missing data or one of the h2_status values.
2147 */
2148static int h2c_ack_ping(struct h2c *h2c)
2149{
2150 struct buffer *res;
2151 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002152 int ret = 0;
2153
2154 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002155
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002156 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002157 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002158
2159 if (h2c_mux_busy(h2c, NULL)) {
2160 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002161 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002162 }
2163
Willy Tarreaucf68c782017-10-10 17:11:41 +02002164 memcpy(str,
2165 "\x00\x00\x08" /* length : 8 (same payload) */
2166 "\x06" "\x01" /* type : 6, flags : ACK */
2167 "\x00\x00\x00\x00" /* stream ID */, 9);
2168
2169 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002170 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002171
Willy Tarreau9c218e72019-05-26 10:08:28 +02002172 res = br_tail(h2c->mbuf);
2173 retry:
2174 if (!h2_get_buf(h2c, res)) {
2175 h2c->flags |= H2_CF_MUX_MALLOC;
2176 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002177 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002178 }
2179
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002180 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002181 if (unlikely(ret <= 0)) {
2182 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002183 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2184 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002185 h2c->flags |= H2_CF_MUX_MFULL;
2186 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002187 }
2188 else {
2189 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002190 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002191 }
2192 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002193 out:
2194 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002195 return ret;
2196}
2197
Willy Tarreau26f95952017-07-27 17:18:30 +02002198/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2199 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002200 * h2c or h2s. The caller must have already verified frame length and stream ID
2201 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002202 */
2203static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2204{
2205 int32_t inc;
2206 int error;
2207
Willy Tarreau7838a792019-08-12 18:42:03 +02002208 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2209
Willy Tarreau26f95952017-07-27 17:18:30 +02002210 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002211 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002212 goto out0;
Willy Tarreau26f95952017-07-27 17:18:30 +02002213
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002214 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002215
2216 if (h2c->dsi != 0) {
2217 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002218
2219 /* it's not an error to receive WU on a closed stream */
2220 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002221 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002222
2223 if (!inc) {
2224 error = H2_ERR_PROTOCOL_ERROR;
2225 goto strm_err;
2226 }
2227
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002228 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002229 error = H2_ERR_FLOW_CONTROL_ERROR;
2230 goto strm_err;
2231 }
2232
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002233 h2s->sws += inc;
2234 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002235 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02002236 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02002237 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002238 }
2239 }
2240 else {
2241 /* connection window update */
2242 if (!inc) {
2243 error = H2_ERR_PROTOCOL_ERROR;
2244 goto conn_err;
2245 }
2246
2247 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2248 error = H2_ERR_FLOW_CONTROL_ERROR;
2249 goto conn_err;
2250 }
2251
2252 h2c->mws += inc;
2253 }
2254
Willy Tarreau7838a792019-08-12 18:42:03 +02002255 done:
2256 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002257 return 1;
2258
2259 conn_err:
2260 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002261 out0:
2262 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 +02002263 return 0;
2264
2265 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002266 h2s_error(h2s, error);
2267 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002268 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002269 return 0;
2270}
2271
Willy Tarreaue96b0922017-10-30 00:28:29 +01002272/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002273 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2274 * have already verified frame length and stream ID validity. Described in
2275 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002276 */
2277static int h2c_handle_goaway(struct h2c *h2c)
2278{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002279 int last;
2280
Willy Tarreau7838a792019-08-12 18:42:03 +02002281 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002282 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002283 if (b_data(&h2c->dbuf) < h2c->dfl) {
2284 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002285 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002286 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002287
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002288 last = h2_get_n32(&h2c->dbuf, 0);
2289 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002290 if (h2c->last_sid < 0)
2291 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002292 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002293 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002294 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002295}
2296
Willy Tarreau92153fc2017-12-03 19:46:19 +01002297/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002298 * invalid. Returns > 0 on success or zero on missing data. It may return an
2299 * error in h2c. The caller must have already verified frame length and stream
2300 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002301 */
2302static int h2c_handle_priority(struct h2c *h2c)
2303{
Willy Tarreau7838a792019-08-12 18:42:03 +02002304 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2305
Willy Tarreau92153fc2017-12-03 19:46:19 +01002306 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002307 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002308 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002309 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002310 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002311
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002312 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002313 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01002314 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002315 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002316 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002317 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002318 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002319 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002320}
2321
Willy Tarreaucd234e92017-08-18 10:59:39 +02002322/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002323 * Returns > 0 on success or zero on missing data. The caller must have already
2324 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002325 */
2326static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2327{
Willy Tarreau7838a792019-08-12 18:42:03 +02002328 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2329
Willy Tarreaucd234e92017-08-18 10:59:39 +02002330 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002331 if (b_data(&h2c->dbuf) < h2c->dfl) {
2332 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 +02002333 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002334 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002335
2336 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002337 if (h2s->st == H2_SS_CLOSED) {
2338 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 +02002339 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002340 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002341
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002342 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002343 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002344
2345 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002346 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002347 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002348 }
2349
2350 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002351 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002352 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002353}
2354
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002355/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2356 * It may return an error in h2c or h2s. The caller must consider that the
2357 * return value is the new h2s in case one was allocated (most common case).
2358 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002359 * errors here are reported as connection errors since it's impossible to
2360 * recover from such errors after the compression context has been altered.
2361 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002362static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002363{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002364 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002365 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002366 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002367 int error;
2368
Willy Tarreau7838a792019-08-12 18:42:03 +02002369 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2370
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002371 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002372 goto out; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002373
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002374 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002375 goto out; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002376
2377 /* now either the frame is complete or the buffer is complete */
2378 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002379 /* The stream exists/existed, this must be a trailers frame */
2380 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002381 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2382 /* unrecoverable error ? */
2383 if (h2c->st0 >= H2_CS_ERROR)
2384 goto out;
2385
2386 if (error == 0)
2387 goto out; // missing data
2388
2389 if (error < 0) {
2390 /* Failed to decode this frame (e.g. too large request)
2391 * but the HPACK decompressor is still synchronized.
2392 */
2393 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2394 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002395 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002396 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002397 goto done;
2398 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002399 /* the connection was already killed by an RST, let's consume
2400 * the data and send another RST.
2401 */
2402 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2403 h2s = (struct h2s*)h2_error_stream;
2404 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002405 }
2406 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2407 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2408 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002409 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002410 goto conn_err;
2411 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002412 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2413 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002414
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002415 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002416
Willy Tarreau25919232019-01-03 14:48:18 +01002417 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002418 if (h2c->st0 >= H2_CS_ERROR)
2419 goto out;
2420
Willy Tarreau25919232019-01-03 14:48:18 +01002421 if (error <= 0) {
2422 if (error == 0)
2423 goto out; // missing data
2424
2425 /* Failed to decode this stream (e.g. too large request)
2426 * but the HPACK decompressor is still synchronized.
2427 */
2428 h2s = (struct h2s*)h2_error_stream;
2429 goto send_rst;
2430 }
2431
Willy Tarreau22de8d32018-09-05 19:55:58 +02002432 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002433 * positively from h2c_frt_stream_new(), the stream will report the error,
2434 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002435 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002436 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002437 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002438 h2s = (struct h2s*)h2_refused_stream;
2439 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002440 }
2441
2442 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002443 h2s->rxbuf = rxbuf;
2444 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002445 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002446
Willy Tarreau88d138e2019-01-02 19:38:14 +01002447 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002448 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002449 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002450
2451 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002452 if (h2s->st == H2_SS_OPEN)
2453 h2s->st = H2_SS_HREM;
2454 else
2455 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002456 }
2457
Willy Tarreau3a429f02019-01-03 11:41:50 +01002458 /* update the max stream ID if the request is being processed */
2459 if (h2s->id > h2c->max_id)
2460 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002461
Willy Tarreau8fecec22019-08-30 07:29:53 +02002462 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn,, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002463 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002464
2465 conn_err:
2466 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002467 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002468
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002469 out:
2470 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002471 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 +01002472 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002473
2474 send_rst:
2475 /* make the demux send an RST for the current stream. We may only
2476 * do this if we're certain that the HEADERS frame was properly
2477 * decompressed so that the HPACK decoder is still kept up to date.
2478 */
2479 h2_release_buf(h2c, &rxbuf);
2480 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002481
2482 TRACE_USER("rejected H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn,, &rxbuf);
2483 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002484 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002485}
2486
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002487/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2488 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2489 * errors here are reported as connection errors since it's impossible to
2490 * recover from such errors after the compression context has been altered.
2491 */
2492static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2493{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002494 struct buffer rxbuf = BUF_NULL;
2495 unsigned long long body_len = 0;
2496 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002497 int error;
2498
Willy Tarreau7838a792019-08-12 18:42:03 +02002499 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2500
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002501 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002502 goto fail; // empty buffer
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002503
2504 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002505 goto fail; // incomplete frame
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002506
Christopher Faulet6884aa32019-09-23 15:28:20 +02002507 if (h2s->st != H2_SS_CLOSED) {
2508 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2509 }
2510 else {
2511 /* the connection was already killed by an RST, let's consume
2512 * the data and send another RST.
2513 */
2514 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2515 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2516 h2c->st0 = H2_CS_FRAME_E;
2517 goto send_rst;
2518 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002519
Willy Tarreau25919232019-01-03 14:48:18 +01002520 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002521 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002522 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002523
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002524 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2525 /* RFC7540#5.1 */
2526 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2527 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002528 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002529 }
2530
Willy Tarreau25919232019-01-03 14:48:18 +01002531 if (error <= 0) {
2532 if (error == 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002533 goto fail; // missing data
Willy Tarreau25919232019-01-03 14:48:18 +01002534
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002535 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002536 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002537 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002538 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002539 }
2540
Christopher Fauletfa922f02019-05-07 10:55:17 +02002541 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002542 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002543
Willy Tarreau927b88b2019-03-04 08:03:25 +01002544 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002545 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002546 else if (h2s->flags & H2_SF_ES_RCVD) {
2547 if (h2s->st == H2_SS_OPEN)
2548 h2s->st = H2_SS_HREM;
2549 else if (h2s->st == H2_SS_HLOC)
2550 h2s_close(h2s);
2551 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002552
Willy Tarreau8fecec22019-08-30 07:29:53 +02002553 TRACE_USER("rcvd H2 response", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn,, &h2s->rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002554 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002555 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02002556 fail:
2557 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2558 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002559
2560 send_rst:
2561 /* make the demux send an RST for the current stream. We may only
2562 * do this if we're certain that the HEADERS frame was properly
2563 * decompressed so that the HPACK decoder is still kept up to date.
2564 */
2565 h2_release_buf(h2c, &rxbuf);
2566 h2c->st0 = H2_CS_FRAME_E;
2567
2568 TRACE_USER("rejected H2 response", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn,, &rxbuf);
2569 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2570 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002571}
2572
Willy Tarreau454f9052017-10-26 19:40:35 +02002573/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2574 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2575 */
2576static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2577{
2578 int error;
2579
Willy Tarreau7838a792019-08-12 18:42:03 +02002580 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2581
Willy Tarreau454f9052017-10-26 19:40:35 +02002582 /* note that empty DATA frames are perfectly valid and sometimes used
2583 * to signal an end of stream (with the ES flag).
2584 */
2585
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002586 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002587 goto fail; // empty buffer
Willy Tarreau454f9052017-10-26 19:40:35 +02002588
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002589 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002590 goto fail; // incomplete frame
Willy Tarreau454f9052017-10-26 19:40:35 +02002591
2592 /* now either the frame is complete or the buffer is complete */
2593
Willy Tarreau454f9052017-10-26 19:40:35 +02002594 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2595 /* RFC7540#6.1 */
2596 error = H2_ERR_STREAM_CLOSED;
2597 goto strm_err;
2598 }
2599
Christopher Faulet4f09ec82019-06-19 09:25:58 +02002600 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002601 /* RFC7540#8.1.2 */
2602 error = H2_ERR_PROTOCOL_ERROR;
2603 goto strm_err;
2604 }
2605
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002606 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02002607 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002608
Willy Tarreau454f9052017-10-26 19:40:35 +02002609 /* call the upper layers to process the frame, then let the upper layer
2610 * notify the stream about any change.
2611 */
2612 if (!h2s->cs) {
Willy Tarreau082c4572019-08-06 10:11:02 +02002613 /* The upper layer has already closed, this may happen on
2614 * 4xx/redirects during POST, or when receiving a response
2615 * from an H2 server after the client has aborted.
2616 */
2617 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002618 goto strm_err;
2619 }
2620
Willy Tarreau8f650c32017-11-21 19:36:21 +01002621 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002622 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01002623
Willy Tarreau721c9742017-11-07 11:05:42 +01002624 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002625 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002626 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002627 }
2628
2629 /* check for completion : the callee will change this to FRAME_A or
2630 * FRAME_H once done.
2631 */
2632 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02002633 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02002634
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002635 /* last frame */
2636 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002637 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002638 if (h2s->st == H2_SS_OPEN)
2639 h2s->st = H2_SS_HREM;
2640 else
2641 h2s_close(h2s);
2642
Willy Tarreau1915ca22019-01-24 11:49:37 +01002643 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2644 /* RFC7540#8.1.2 */
2645 error = H2_ERR_PROTOCOL_ERROR;
2646 goto strm_err;
2647 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002648 }
2649
Willy Tarreau7838a792019-08-12 18:42:03 +02002650 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02002651 return 1;
2652
Willy Tarreau454f9052017-10-26 19:40:35 +02002653 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002654 h2s_error(h2s, error);
2655 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002656 fail:
2657 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 +02002658 return 0;
2659}
2660
Willy Tarreau63864812019-08-07 14:25:20 +02002661/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
2662 * valid for the current stream state. This is needed only after parsing the
2663 * frame header but in practice it can be performed at any time during
2664 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
2665 * or 0 in case of error, in which case either h2s or h2c will carry an error.
2666 */
2667static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
2668{
Willy Tarreau7838a792019-08-12 18:42:03 +02002669 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
2670
Willy Tarreau63864812019-08-07 14:25:20 +02002671 if (h2s->st == H2_SS_IDLE &&
2672 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2673 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2674 * this state MUST be treated as a connection error
2675 */
2676 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2677 if (!h2c->nb_streams) {
2678 /* only log if no other stream can report the error */
2679 sess_log(h2c->conn->owner);
2680 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002681 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 +02002682 return 0;
2683 }
2684
2685 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2686 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2687 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2688 * this state MUST be treated as a stream error.
2689 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2690 * PUSH_PROMISE/CONTINUATION cause connection errors.
2691 */
2692 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2693 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2694 else
2695 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002696 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 +02002697 return 0;
2698 }
2699
2700 /* Below the management of frames received in closed state is a
2701 * bit hackish because the spec makes strong differences between
2702 * streams closed by receiving RST, sending RST, and seeing ES
2703 * in both directions. In addition to this, the creation of a
2704 * new stream reusing the identifier of a closed one will be
2705 * detected here. Given that we cannot keep track of all closed
2706 * streams forever, we consider that unknown closed streams were
2707 * closed on RST received, which allows us to respond with an
2708 * RST without breaking the connection (eg: to abort a transfer).
2709 * Some frames have to be silently ignored as well.
2710 */
2711 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2712 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
2713 /* #5.1.1: The identifier of a newly
2714 * established stream MUST be numerically
2715 * greater than all streams that the initiating
2716 * endpoint has opened or reserved. This
2717 * governs streams that are opened using a
2718 * HEADERS frame and streams that are reserved
2719 * using PUSH_PROMISE. An endpoint that
2720 * receives an unexpected stream identifier
2721 * MUST respond with a connection error.
2722 */
2723 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002724 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 +02002725 return 0;
2726 }
2727
Christopher Faulet6884aa32019-09-23 15:28:20 +02002728 if (h2s->flags & H2_SF_RST_RCVD && !(h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02002729 /* RFC7540#5.1:closed: an endpoint that
2730 * receives any frame other than PRIORITY after
2731 * receiving a RST_STREAM MUST treat that as a
2732 * stream error of type STREAM_CLOSED.
2733 *
2734 * Note that old streams fall into this category
2735 * and will lead to an RST being sent.
2736 *
2737 * However, we cannot generalize this to all frame types. Those
2738 * carrying compression state must still be processed before
2739 * being dropped or we'll desynchronize the decoder. This can
2740 * happen with request trailers received after sending an
2741 * RST_STREAM, or with header/trailers responses received after
2742 * sending RST_STREAM (aborted stream).
2743 */
2744 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2745 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002746 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 +02002747 return 0;
2748 }
2749
2750 /* RFC7540#5.1:closed: if this state is reached as a
2751 * result of sending a RST_STREAM frame, the peer that
2752 * receives the RST_STREAM might have already sent
2753 * frames on the stream that cannot be withdrawn. An
2754 * endpoint MUST ignore frames that it receives on
2755 * closed streams after it has sent a RST_STREAM
2756 * frame. An endpoint MAY choose to limit the period
2757 * over which it ignores frames and treat frames that
2758 * arrive after this time as being in error.
2759 */
2760 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
2761 /* RFC7540#5.1:closed: any frame other than
2762 * PRIO/WU/RST in this state MUST be treated as
2763 * a connection error
2764 */
2765 if (h2c->dft != H2_FT_RST_STREAM &&
2766 h2c->dft != H2_FT_PRIORITY &&
2767 h2c->dft != H2_FT_WINDOW_UPDATE) {
2768 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002769 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 +02002770 return 0;
2771 }
2772 }
2773 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002774 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002775 return 1;
2776}
2777
Willy Tarreaubc933932017-10-09 16:21:43 +02002778/* process Rx frames to be demultiplexed */
2779static void h2_process_demux(struct h2c *h2c)
2780{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002781 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002782 struct h2_fh hdr;
2783 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002784 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002785
Willy Tarreau7838a792019-08-12 18:42:03 +02002786 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2787
Willy Tarreau081d4722017-05-16 21:51:05 +02002788 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002789 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02002790
2791 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2792 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002793 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02002794 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02002795 goto out;
2796
Willy Tarreau52eed752017-09-22 15:05:09 +02002797 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2798 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002799 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002800 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002801 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002802 sess_log(h2c->conn->owner);
2803 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002804 goto fail;
2805 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002806 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002807
2808 h2c->max_id = 0;
2809 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002810 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002811 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002812
2813 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002814 /* ensure that what is pending is a valid SETTINGS frame
2815 * without an ACK.
2816 */
Willy Tarreau7838a792019-08-12 18:42:03 +02002817 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 +02002818 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002819 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002820 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002821 TRACE_PROTO("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 +02002822 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002823 sess_log(h2c->conn->owner);
2824 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002825 goto fail;
2826 }
2827
2828 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2829 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau7838a792019-08-12 18:42:03 +02002830 TRACE_PROTO("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 +02002831 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2832 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002833 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002834 goto fail;
2835 }
2836
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002837 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002838 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau7838a792019-08-12 18:42:03 +02002839 TRACE_PROTO("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 +02002840 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2841 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002842 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002843 goto fail;
2844 }
2845
Willy Tarreau3bf69182018-12-21 15:34:50 +01002846 /* that's OK, switch to FRAME_P to process it. This is
2847 * a SETTINGS frame whose header has already been
2848 * deleted above.
2849 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002850 padlen = 0;
2851 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002852 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002853 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002854
2855 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02002856 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002857 int ret = 0;
2858
Willy Tarreau7838a792019-08-12 18:42:03 +02002859 if (!b_data(&h2c->dbuf)) {
2860 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
2861 break;
2862 }
2863
2864 if (h2c->st0 >= H2_CS_ERROR) {
2865 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002866 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02002867 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002868
2869 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau30d05f32019-08-06 15:49:51 +02002870 h2c->rcvd_s = 0;
2871
Willy Tarreau7838a792019-08-12 18:42:03 +02002872 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002873 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002874 break;
2875
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002876 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002877 TRACE_PROTO("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 +02002878 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002879 if (!h2c->nb_streams) {
2880 /* only log if no other stream can report the error */
2881 sess_log(h2c->conn->owner);
2882 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002883 break;
2884 }
2885
Christopher Fauletdd2a5622019-06-18 12:22:38 +02002886 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002887 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2888 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2889 * we read the pad length and drop it from the remaining
2890 * payload (one byte + the 9 remaining ones = 10 total
2891 * removed), so we have a frame payload starting after the
2892 * pad len. Flow controlled frames (DATA) also count the
2893 * padlen in the flow control, so it must be adjusted.
2894 */
2895 if (hdr.len < 1) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002896 TRACE_PROTO("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 +01002897 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2898 sess_log(h2c->conn->owner);
2899 goto fail;
2900 }
2901 hdr.len--;
2902
2903 if (b_data(&h2c->dbuf) < 10)
2904 break; // missing padlen
2905
2906 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2907
2908 if (padlen > hdr.len) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002909 TRACE_PROTO("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 +01002910 /* RFC7540#6.1 : pad length = length of
2911 * frame payload or greater => error.
2912 */
2913 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2914 sess_log(h2c->conn->owner);
2915 goto fail;
2916 }
2917
2918 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2919 h2c->rcvd_c++;
2920 h2c->rcvd_s++;
2921 }
2922 b_del(&h2c->dbuf, 1);
2923 }
2924 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau8fecec22019-08-30 07:29:53 +02002925 TRACE_STATE("rcvd H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002926
2927 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002928 h2c->dfl = hdr.len;
2929 h2c->dsi = hdr.sid;
2930 h2c->dft = hdr.ft;
2931 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002932 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002933 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7838a792019-08-12 18:42:03 +02002934 TRACE_STATE("switching to FRAME_P state", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002935
2936 /* check for minimum basic frame format validity */
2937 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2938 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002939 TRACE_PROTO("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 +01002940 h2c_error(h2c, ret);
2941 sess_log(h2c->conn->owner);
2942 goto fail;
2943 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002944 }
2945
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02002946 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2947 * H2_CS_FRAME_P indicates an incomplete previous operation
2948 * (most often the first attempt) and requires some validity
2949 * checks for the frame and the current state. The two other
2950 * ones are set after completion (or abortion) and must skip
2951 * validity checks.
2952 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002953 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2954
Willy Tarreau567beb82018-12-18 16:52:44 +01002955 if (tmp_h2s != h2s && h2s && h2s->cs &&
2956 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002957 conn_xprt_read0_pending(h2c->conn) ||
2958 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002959 (h2s->flags & H2_SF_ES_RCVD) ||
2960 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002961 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02002962 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 +01002963 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002964 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002965 }
2966 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002967
Willy Tarreau63864812019-08-07 14:25:20 +02002968 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02002969 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
2970 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002971 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02002972 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01002973
Willy Tarreau7e98c052017-10-10 15:56:59 +02002974 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002975 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02002976 if (h2c->st0 == H2_CS_FRAME_P) {
2977 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002978 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002979 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002980
Willy Tarreau7838a792019-08-12 18:42:03 +02002981 if (h2c->st0 == H2_CS_FRAME_A) {
2982 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 +02002983 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002984 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002985 break;
2986
Willy Tarreaucf68c782017-10-10 17:11:41 +02002987 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02002988 if (h2c->st0 == H2_CS_FRAME_P) {
2989 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002990 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002991 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02002992
Willy Tarreau7838a792019-08-12 18:42:03 +02002993 if (h2c->st0 == H2_CS_FRAME_A) {
2994 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 +02002995 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002996 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02002997 break;
2998
Willy Tarreau26f95952017-07-27 17:18:30 +02002999 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003000 if (h2c->st0 == H2_CS_FRAME_P) {
3001 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 +02003002 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003003 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003004 break;
3005
Willy Tarreau61290ec2017-10-17 08:19:21 +02003006 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01003007 /* RFC7540#6.10: CONTINUATION may only be preceeded by
3008 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3009 * frames' parsers consume all following CONTINUATION
3010 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003011 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003012 TRACE_PROTO("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 +01003013 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3014 sess_log(h2c->conn->owner);
3015 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003016
Willy Tarreau13278b42017-10-13 19:23:14 +02003017 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003018 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003019 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003020 if (h2c->flags & H2_CF_IS_BACK)
3021 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3022 else
3023 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003024 if (tmp_h2s) {
3025 h2s = tmp_h2s;
3026 ret = 1;
3027 }
3028 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003029 break;
3030
Willy Tarreau454f9052017-10-26 19:40:35 +02003031 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003032 if (h2c->st0 == H2_CS_FRAME_P) {
3033 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003034 ret = h2c_frt_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003035 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003036
Willy Tarreau7838a792019-08-12 18:42:03 +02003037 if (h2c->st0 == H2_CS_FRAME_A) {
3038 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 +02003039 ret = h2c_send_strm_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003040 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003041 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003042
Willy Tarreau92153fc2017-12-03 19:46:19 +01003043 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003044 if (h2c->st0 == H2_CS_FRAME_P) {
3045 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003046 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003047 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003048 break;
3049
Willy Tarreaucd234e92017-08-18 10:59:39 +02003050 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003051 if (h2c->st0 == H2_CS_FRAME_P) {
3052 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 +02003053 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003054 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02003055 break;
3056
Willy Tarreaue96b0922017-10-30 00:28:29 +01003057 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003058 if (h2c->st0 == H2_CS_FRAME_P) {
3059 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003060 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003061 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01003062 break;
3063
Willy Tarreau1c661982017-10-30 13:52:01 +01003064 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003065 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003066 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003067 /* drop frames that we ignore. They may be larger than
3068 * the buffer so we drain all of their contents until
3069 * we reach the end.
3070 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003071 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3072 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003073 h2c->dfl -= ret;
3074 ret = h2c->dfl == 0;
3075 }
3076
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003077 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003078 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003079 if (h2s->st == H2_SS_ERROR) {
3080 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 +01003081 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003082 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003083
Willy Tarreau7838a792019-08-12 18:42:03 +02003084 if (h2c->st0 == H2_CS_FRAME_E) {
3085 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 +01003086 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003087 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003088
Willy Tarreau7e98c052017-10-10 15:56:59 +02003089 /* error or missing data condition met above ? */
Willy Tarreau7838a792019-08-12 18:42:03 +02003090 if (ret <= 0) {
3091 TRACE_DEVEL("insufficient data to proceed", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003092 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003093 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003094
3095 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003096 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003097 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003098 h2c->st0 = H2_CS_FRAME_H;
3099 }
3100 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003101
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003102 if (h2c->rcvd_c > 0 &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003103 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) {
3104 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003105 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003106 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003107
Willy Tarreau52eed752017-09-22 15:05:09 +02003108 fail:
3109 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01003110 if (h2s && h2s->cs &&
3111 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003112 conn_xprt_read0_pending(h2c->conn) ||
3113 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003114 (h2s->flags & H2_SF_ES_RCVD) ||
3115 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003116 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003117 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 +01003118 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003119 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003120 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003121
Willy Tarreau7838a792019-08-12 18:42:03 +02003122 if (old_iw != h2c->miw) {
3123 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003124 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003125 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003126
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003127 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003128 out:
3129 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreaubc933932017-10-09 16:21:43 +02003130}
3131
3132/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3133 * the end.
3134 */
3135static int h2_process_mux(struct h2c *h2c)
3136{
Olivier Houchard998410a2019-04-15 19:23:37 +02003137 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003138
Willy Tarreau7838a792019-08-12 18:42:03 +02003139 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3140
Willy Tarreau01b44822018-10-03 14:26:37 +02003141 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3142 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3143 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3144 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
3145 if (h2c->st0 == H2_CS_ERROR) {
3146 h2c->st0 = H2_CS_ERROR2;
3147 sess_log(h2c->conn->owner);
3148 }
3149 goto fail;
3150 }
3151 h2c->st0 = H2_CS_SETTINGS1;
3152 }
3153 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003154 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003155 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003156 }
3157
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003158 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003159 if (h2c->rcvd_s > 0 &&
3160 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3161 h2c_send_strm_wu(h2c) < 0)
3162 goto fail;
3163
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003164 if (h2c->rcvd_c > 0 &&
3165 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3166 h2c_send_conn_wu(h2c) < 0)
3167 goto fail;
3168
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003169 /* First we always process the flow control list because the streams
3170 * waiting there were already elected for immediate emission but were
3171 * blocked just on this.
3172 */
3173
Olivier Houchard998410a2019-04-15 19:23:37 +02003174 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003175 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3176 h2c->st0 >= H2_CS_ERROR)
3177 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02003178
Willy Tarreauc234ae32019-05-13 17:56:11 +02003179 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01003180 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003181
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003182 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02003183 /* For some reason, the upper layer failed to subsribe again,
3184 * so remove it from the send_list
3185 */
3186 if (!h2s->send_wait) {
3187 LIST_DEL_INIT(&h2s->list);
3188 continue;
3189 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003190 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01003191 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003192 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003193 }
3194
Olivier Houchard998410a2019-04-15 19:23:37 +02003195 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003196 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
3197 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02003198
Willy Tarreauc234ae32019-05-13 17:56:11 +02003199 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01003200 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003201
Olivier Houchard998410a2019-04-15 19:23:37 +02003202 /* For some reason, the upper layer failed to subsribe again,
3203 * so remove it from the send_list
3204 */
3205 if (!h2s->send_wait) {
3206 LIST_DEL_INIT(&h2s->list);
3207 continue;
3208 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003209 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003210 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01003211 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003212 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003213 }
3214
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003215 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003216 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003217 if (h2c->st0 == H2_CS_ERROR) {
3218 if (h2c->max_id >= 0) {
3219 h2c_send_goaway_error(h2c, NULL);
3220 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003221 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003222 }
3223
3224 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3225 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003226 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003227 done:
3228 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3229 return 1;
3230 out0:
3231 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3232 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003233}
3234
Willy Tarreau62f52692017-10-08 23:01:42 +02003235
Willy Tarreau479998a2018-11-18 06:30:59 +01003236/* Attempt to read data, and subscribe if none available.
3237 * The function returns 1 if data has been received, otherwise zero.
3238 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003239static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003240{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003241 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003242 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003243 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003244 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003245
Willy Tarreau7838a792019-08-12 18:42:03 +02003246 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3247
3248 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3249 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003250 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003251 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003252
Willy Tarreau7838a792019-08-12 18:42:03 +02003253 if (!h2_recv_allowed(h2c)) {
3254 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003255 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003256 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003257
Willy Tarreau44e973f2018-03-01 17:49:30 +01003258 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003259 if (!buf) {
3260 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003261 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003262 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003263 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003264
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003265 b_realign_if_empty(buf);
3266 if (!b_data(buf)) {
3267 /* try to pre-align the buffer like the
3268 * rxbufs will be to optimize memory copies. We'll make
3269 * sure that the frame header lands at the end of the
3270 * HTX block to alias it upon recv. We cannot use the
3271 * head because rcv_buf() will realign the buffer if
3272 * it's empty. Thus we cheat and pretend we already
3273 * have a few bytes there.
3274 */
3275 max = buf_room_for_htx_data(buf) + 9;
3276 buf->head = sizeof(struct htx) - 9;
3277 }
3278 else
3279 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003280
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003281 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003282
Willy Tarreau7838a792019-08-12 18:42:03 +02003283 if (max && !ret && h2_recv_allowed(h2c)) {
3284 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003285 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003286 } else if (ret)
3287 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn,,, (void*)(long)ret);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003288
Olivier Houcharda1411e62018-08-17 18:42:48 +02003289 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003290 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02003291 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard46677732018-11-29 17:06:17 +01003292 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003293 }
3294
Willy Tarreau7838a792019-08-12 18:42:03 +02003295 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003296 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003297 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK);
3298 }
3299
3300 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003301 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003302}
3303
Willy Tarreau479998a2018-11-18 06:30:59 +01003304/* Try to send data if possible.
3305 * The function returns 1 if data have been sent, otherwise zero.
3306 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003307static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003308{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003309 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003310 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003311 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003312
Willy Tarreau7838a792019-08-12 18:42:03 +02003313 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003314
Willy Tarreau7838a792019-08-12 18:42:03 +02003315 if (conn->flags & CO_FL_ERROR) {
3316 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
3317 return 1;
3318 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003319
Willy Tarreaua2af5122017-10-09 11:56:46 +02003320 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
3321 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003322 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003323 }
3324
Willy Tarreaubc933932017-10-09 16:21:43 +02003325 /* This loop is quite simple : it tries to fill as much as it can from
3326 * pending streams into the existing buffer until it's reportedly full
3327 * or the end of send requests is reached. Then it tries to send this
3328 * buffer's contents out, marks it not full if at least one byte could
3329 * be sent, and tries again.
3330 *
3331 * The snd_buf() function normally takes a "flags" argument which may
3332 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3333 * data immediately comes and CO_SFL_STREAMER to indicate that the
3334 * connection is streaming lots of data (used to increase TLS record
3335 * size at the expense of latency). The former can be sent any time
3336 * there's a buffer full flag, as it indicates at least one stream
3337 * attempted to send and failed so there are pending data. An
3338 * alternative would be to set it as long as there's an active stream
3339 * but that would be problematic for ACKs until we have an absolute
3340 * guarantee that all waiters have at least one byte to send. The
3341 * latter should possibly not be set for now.
3342 */
3343
3344 done = 0;
3345 while (!done) {
3346 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003347 unsigned int released = 0;
3348 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02003349
3350 /* fill as much as we can into the current buffer */
3351 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3352 done = h2_process_mux(h2c);
3353
Olivier Houchard2b094432019-01-29 18:28:36 +01003354 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003355 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003356
Willy Tarreaubc933932017-10-09 16:21:43 +02003357 if (conn->flags & CO_FL_ERROR)
3358 break;
3359
3360 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
3361 flags |= CO_SFL_MSG_MORE;
3362
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003363 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3364 if (b_data(buf)) {
3365 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003366 if (!ret) {
3367 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003368 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003369 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003370 sent = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003371 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn,, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003372 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003373 if (b_data(buf)) {
3374 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003375 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003376 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003377 }
3378 b_free(buf);
3379 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003380 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003381
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003382 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003383 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003384
Willy Tarreaubc933932017-10-09 16:21:43 +02003385 /* wrote at least one byte, the buffer is not full anymore */
3386 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
3387 }
3388
Willy Tarreaua2af5122017-10-09 11:56:46 +02003389 if (conn->flags & CO_FL_SOCK_WR_SH) {
3390 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02003391 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003392 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003393 /* We're not full anymore, so we can wake any task that are waiting
3394 * for us.
3395 */
3396 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01003397 struct h2s *h2s;
3398
3399 list_for_each_entry(h2s, &h2c->send_list, list) {
3400 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
3401 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02003402
Willy Tarreauc234ae32019-05-13 17:56:11 +02003403 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01003404 continue;
3405
Olivier Houchard998410a2019-04-15 19:23:37 +02003406 /* For some reason, the upper layer failed to subsribe again,
3407 * so remove it from the send_list
3408 */
3409 if (!h2s->send_wait) {
3410 LIST_DEL_INIT(&h2s->list);
3411 continue;
3412 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01003413 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003414 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau7838a792019-08-12 18:42:03 +02003415 TRACE_DEVEL("waking up pending stream", H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn, h2s);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003416 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01003417 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003418 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003419 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003420 /* We're done, no more to send */
Willy Tarreau7838a792019-08-12 18:42:03 +02003421 if (!br_data(h2c->mbuf)) {
3422 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003423 return sent;
Willy Tarreau7838a792019-08-12 18:42:03 +02003424 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003425schedule:
Willy Tarreau7838a792019-08-12 18:42:03 +02003426 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
3427 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003428 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003429 }
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003430
Willy Tarreau7838a792019-08-12 18:42:03 +02003431 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003432 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003433}
3434
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003435/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003436static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
3437{
3438 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02003439 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003440
Willy Tarreau7838a792019-08-12 18:42:03 +02003441 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3442
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003443 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003444 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003445 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003446 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003447 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02003448 h2_process(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003449
3450 TRACE_LEAVE(H2_EV_H2C_WAKE);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003451 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003452}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003453
Willy Tarreau62f52692017-10-08 23:01:42 +02003454/* callback called on any event by the connection handler.
3455 * It applies changes and returns zero, or < 0 if it wants immediate
3456 * destruction of the connection (which normally doesn not happen in h2).
3457 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003458static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003459{
Olivier Houchard7505f942018-08-21 18:10:44 +02003460 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003461
Willy Tarreau7838a792019-08-12 18:42:03 +02003462 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3463
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003464 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003465 h2_process_demux(h2c);
3466
3467 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003468 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003469
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003470 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003471 h2c->flags &= ~H2_CF_DEM_DFULL;
3472 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003473 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003474
Willy Tarreau0b37d652018-10-03 10:33:02 +02003475 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003476 /* frontend is stopping, reload likely in progress, let's try
3477 * to announce a graceful shutdown if not yet done. We don't
3478 * care if it fails, it will be tried again later.
3479 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003480 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
Willy Tarreau8ec14062017-12-30 18:08:13 +01003481 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3482 if (h2c->last_sid < 0)
3483 h2c->last_sid = (1U << 31) - 1;
3484 h2c_send_goaway_error(h2c, NULL);
3485 }
3486 }
3487
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003488 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003489 * If we received early data, and the handshake is done, wake
3490 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003491 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003492 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3493 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3494 struct eb32_node *node;
3495 struct h2s *h2s;
3496
3497 h2c->flags |= H2_CF_WAIT_FOR_HS;
3498 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3499
3500 while (node) {
3501 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003502 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003503 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003504 node = eb32_next(node);
3505 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003506 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003507
Willy Tarreau26bd7612017-10-09 16:47:04 +02003508 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003509 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3510 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3511 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003512 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003513
3514 if (eb_is_empty(&h2c->streams_by_id)) {
3515 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003516 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003517 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003518 return -1;
3519 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003520 }
3521
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003522 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003523 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003524
Olivier Houchard53216e72018-10-10 15:46:36 +02003525 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3526 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3527 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003528 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003529 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3530 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003531 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003532
Willy Tarreau3f133572017-10-31 19:21:06 +01003533 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003534 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3535 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003536 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003537
Olivier Houchard7505f942018-08-21 18:10:44 +02003538 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003539 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003540 return 0;
3541}
3542
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003543/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003544static int h2_wake(struct connection *conn)
3545{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003546 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02003547 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003548
Willy Tarreau7838a792019-08-12 18:42:03 +02003549 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3550 ret = h2_process(h2c);
3551 TRACE_LEAVE(H2_EV_H2C_WAKE);
3552 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003553}
3554
Willy Tarreauea392822017-10-31 10:02:25 +01003555/* Connection timeout management. The principle is that if there's no receipt
3556 * nor sending for a certain amount of time, the connection is closed. If the
3557 * MUX buffer still has lying data or is not allocatable, the connection is
3558 * immediately killed. If it's allocatable and empty, we attempt to send a
3559 * GOAWAY frame.
3560 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003561static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003562{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003563 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003564 int expired = tick_is_expired(t->expire, now_ms);
3565
Willy Tarreau7838a792019-08-12 18:42:03 +02003566 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
3567
3568 if (!expired && h2c) {
3569 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01003570 return t;
Willy Tarreau7838a792019-08-12 18:42:03 +02003571 }
Willy Tarreauea392822017-10-31 10:02:25 +01003572
Olivier Houchard3f795f72019-04-17 22:51:06 +02003573 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003574
3575 if (!h2c) {
3576 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02003577 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02003578 return NULL;
3579 }
3580
3581 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003582 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003583 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003584
Willy Tarreau662fafc2019-05-26 09:43:07 +02003585 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003586 /* don't even try to send a GOAWAY, the buffer is stuck */
3587 h2c->flags |= H2_CF_GOAWAY_FAILED;
3588 }
3589
3590 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003591 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003592 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3593 h2c->flags |= H2_CF_GOAWAY_FAILED;
3594
Willy Tarreau662fafc2019-05-26 09:43:07 +02003595 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003596 unsigned int released = 0;
3597 struct buffer *buf;
3598
3599 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3600 if (b_data(buf)) {
3601 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3602 if (!ret)
3603 break;
3604 b_del(buf, ret);
3605 if (b_data(buf))
3606 break;
3607 b_free(buf);
3608 released++;
3609 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003610 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003611
3612 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003613 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003614 }
Willy Tarreauea392822017-10-31 10:02:25 +01003615
Willy Tarreau0975f112018-03-29 15:22:59 +02003616 /* either we can release everything now or it will be done later once
3617 * the last stream closes.
3618 */
3619 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003620 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003621
Willy Tarreau7838a792019-08-12 18:42:03 +02003622 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01003623 return NULL;
3624}
3625
3626
Willy Tarreau62f52692017-10-08 23:01:42 +02003627/*******************************************/
3628/* functions below are used by the streams */
3629/*******************************************/
3630
3631/*
3632 * Attach a new stream to a connection
3633 * (Used for outgoing connections)
3634 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003635static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003636{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003637 struct conn_stream *cs;
3638 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003639 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003640
Willy Tarreau7838a792019-08-12 18:42:03 +02003641 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003642 cs = cs_new(conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003643 if (!cs) {
3644 TRACE_DEVEL("leaving on CS allocation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003645 return NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003646 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01003647 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003648 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003649 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003650 cs_free(cs);
3651 return NULL;
3652 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003653 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003654 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003655}
3656
Willy Tarreaufafd3982018-11-18 21:29:20 +01003657/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3658 * We have to scan because we may have some orphan streams. It might be
3659 * beneficial to scan backwards from the end to reduce the likeliness to find
3660 * orphans.
3661 */
3662static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3663{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003664 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003665 struct h2s *h2s;
3666 struct eb32_node *node;
3667
3668 node = eb32_first(&h2c->streams_by_id);
3669 while (node) {
3670 h2s = container_of(node, struct h2s, by_id);
3671 if (h2s->cs)
3672 return h2s->cs;
3673 node = eb32_next(node);
3674 }
3675 return NULL;
3676}
3677
Willy Tarreau62f52692017-10-08 23:01:42 +02003678/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003679 * Destroy the mux and the associated connection, if it is no longer used
3680 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003681static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003682{
Christopher Faulet73c12072019-04-08 11:23:22 +02003683 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003684
Willy Tarreau7838a792019-08-12 18:42:03 +02003685 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003686 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003687 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003688 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01003689}
3690
3691/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003692 * Detach the stream from the connection and possibly release the connection.
3693 */
3694static void h2_detach(struct conn_stream *cs)
3695{
Willy Tarreau60935142017-10-16 18:11:19 +02003696 struct h2s *h2s = cs->ctx;
3697 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003698 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003699
Willy Tarreau7838a792019-08-12 18:42:03 +02003700 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
3701
Willy Tarreau60935142017-10-16 18:11:19 +02003702 cs->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003703 if (!h2s) {
3704 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02003705 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02003706 }
Willy Tarreau60935142017-10-16 18:11:19 +02003707
Olivier Houchard998410a2019-04-15 19:23:37 +02003708 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003709 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003710 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003711 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003712 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003713 /*
3714 * At this point, the stream_interface is supposed to have called
3715 * h2_unsubscribe(), so the only way there's still a
3716 * subscription that came from the stream_interface (as we
3717 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3718 * without the stream_interface involved) is that we subscribed
3719 * for sending, we woke the tasklet up and removed the
3720 * SUB_RETRY_SEND flag, so the stream_interface would not
3721 * know it has to unsubscribe for send, but the tasklet hasn't
3722 * run yet. Make sure to handle that by explicitely setting
3723 * send_wait to NULL, as nothing else will do it for us.
3724 */
3725 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003726 }
3727
Olivier Houchardf502aca2018-12-14 19:42:40 +01003728 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003729 h2c = h2s->h2c;
3730 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003731 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003732 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3733 !h2_frt_has_too_many_cs(h2c)) {
3734 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003735 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003736 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003737 }
Willy Tarreau60935142017-10-16 18:11:19 +02003738
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003739 /* this stream may be blocked waiting for some data to leave (possibly
3740 * an ES or RST frame), so orphan it in this case.
3741 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003742 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003743 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003744 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait)) {
3745 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003746 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02003747 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003748
Willy Tarreau45f752e2017-10-30 15:44:59 +01003749 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3750 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3751 /* unblock the connection if it was blocked on this
3752 * stream.
3753 */
3754 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3755 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003756 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003757 }
3758
Willy Tarreau71049cc2018-03-28 13:56:39 +02003759 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003760
Christopher Faulet9b79a102019-07-15 11:22:56 +02003761 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003762 if (!(h2c->conn->flags &
3763 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3764 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003765 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003766 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3767 h2c->conn->owner = NULL;
3768 if (eb_is_empty(&h2c->streams_by_id)) {
3769 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3770 /* The server doesn't want it, let's kill the connection right away */
3771 h2c->conn->mux->destroy(h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003772 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
Olivier Houchard351411f2018-12-27 17:20:54 +01003773 return;
3774 }
3775 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003776 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003777 if (eb_is_empty(&h2c->streams_by_id)) {
3778 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3779 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
Willy Tarreaufe1c9082019-08-30 14:57:17 +02003780 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003781 return;
3782 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003783 /* Never ever allow to reuse a connection from a non-reuse backend */
3784 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3785 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003786 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003787 struct server *srv = objt_server(h2c->conn->target);
3788
3789 if (srv) {
3790 if (h2c->conn->flags & CO_FL_PRIVATE)
3791 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3792 else
3793 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3794 }
3795
3796 }
3797 }
3798 }
3799
Willy Tarreaue323f342018-03-28 13:51:45 +02003800 /* We don't want to close right now unless we're removing the
3801 * last stream, and either the connection is in error, or it
3802 * reached the ID already specified in a GOAWAY frame received
3803 * or sent (as seen by last_sid >= 0).
3804 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003805 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003806 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02003807 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003808 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003809 }
3810 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003811 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3812 task_queue(h2c->task);
Willy Tarreau7838a792019-08-12 18:42:03 +02003813 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02003814 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003815 else
3816 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003817}
3818
Willy Tarreau88bdba32019-05-13 18:17:53 +02003819/* Performs a synchronous or asynchronous shutr(). */
3820static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003821{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003822 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003823 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003824
Willy Tarreauf983d002019-05-14 10:40:21 +02003825 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003826 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003827
Willy Tarreau7838a792019-08-12 18:42:03 +02003828 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
3829
Willy Tarreau18059042019-01-31 19:12:48 +01003830 /* a connstream may require us to immediately kill the whole connection
3831 * for example because of a "tcp-request content reject" rule that is
3832 * normally used to limit abuse. In this case we schedule a goaway to
3833 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003834 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003835 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003836 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003837 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01003838 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3839 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3840 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003841 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3842 /* Nothing was never sent for this stream, so reset with
3843 * REFUSED_STREAM error to let the client retry the
3844 * request.
3845 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003846 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01003847 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3848 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02003849 else {
3850 /* a final response was already provided, we don't want this
3851 * stream anymore. This may happen when the server responds
3852 * before the end of an upload and closes quickly (redirect,
3853 * deny, ...)
3854 */
3855 h2s_error(h2s, H2_ERR_CANCEL);
3856 }
Willy Tarreau18059042019-01-31 19:12:48 +01003857
Willy Tarreau90c32322017-11-24 08:00:30 +01003858 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003859 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003860 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003861
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003862 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003863 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003864 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003865 done:
3866 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02003867 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003868 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003869add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003870 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003871 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003872 if (h2s->flags & H2_SF_BLK_MFCTL) {
3873 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3874 h2s->send_wait = sw;
3875 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3876 h2s->send_wait = sw;
3877 LIST_ADDQ(&h2c->send_list, &h2s->list);
3878 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003879 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003880 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003881 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02003882 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003883 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003884}
3885
Willy Tarreau88bdba32019-05-13 18:17:53 +02003886/* Performs a synchronous or asynchronous shutw(). */
3887static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003888{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003889 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003890 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003891
Willy Tarreaucfba9d62019-08-06 10:30:58 +02003892 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003893 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003894
Willy Tarreau7838a792019-08-12 18:42:03 +02003895 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
3896
Willy Tarreaucfba9d62019-08-06 10:30:58 +02003897 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003898 /* we can cleanly close using an empty data frame only after headers */
3899
3900 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3901 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003902 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003903
3904 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003905 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003906 else
3907 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003908 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003909 /* a connstream may require us to immediately kill the whole connection
3910 * for example because of a "tcp-request content reject" rule that is
3911 * normally used to limit abuse. In this case we schedule a goaway to
3912 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003913 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003914 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003915 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003916 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01003917 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3918 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3919 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003920 else {
3921 /* Nothing was never sent for this stream, so reset with
3922 * REFUSED_STREAM error to let the client retry the
3923 * request.
3924 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003925 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01003926 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3927 }
Willy Tarreau18059042019-01-31 19:12:48 +01003928
Willy Tarreau90c32322017-11-24 08:00:30 +01003929 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003930 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003931 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003932
Willy Tarreau00dd0782018-03-01 16:31:34 +01003933 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003934 }
3935
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003936 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003937 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02003938
3939 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
3940
Willy Tarreau88bdba32019-05-13 18:17:53 +02003941 done:
3942 h2s->flags &= ~H2_SF_WANT_SHUTW;
3943 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003944
3945 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003946 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003947 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003948 if (h2s->flags & H2_SF_BLK_MFCTL) {
3949 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3950 h2s->send_wait = sw;
3951 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3952 h2s->send_wait = sw;
3953 LIST_ADDQ(&h2c->send_list, &h2s->list);
3954 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003955 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003956 /* let the handler know we want to shutw */
3957 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau7838a792019-08-12 18:42:03 +02003958 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003959 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003960}
3961
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003962/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003963 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3964 * and prevented the last frame from being emitted.
3965 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003966static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3967{
3968 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003969 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003970
Willy Tarreau7838a792019-08-12 18:42:03 +02003971 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
3972
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003973 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003974 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003975 h2_do_shutw(h2s);
3976
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003977 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003978 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003979
Willy Tarreau88bdba32019-05-13 18:17:53 +02003980 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003981 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003982 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003983
Willy Tarreau88bdba32019-05-13 18:17:53 +02003984 if (!h2s->cs) {
3985 h2s_destroy(h2s);
3986 if (h2c_is_dead(h2c))
3987 h2_release(h2c);
3988 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003989 }
3990
Willy Tarreau7838a792019-08-12 18:42:03 +02003991 TRACE_LEAVE(H2_EV_STRM_SHUT);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003992 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003993}
3994
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003995/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003996static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3997{
3998 struct h2s *h2s = cs->ctx;
3999
Willy Tarreau7838a792019-08-12 18:42:03 +02004000 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004001 if (cs->flags & CS_FL_KILL_CONN)
4002 h2s->flags |= H2_SF_KILL_CONN;
4003
Willy Tarreau7838a792019-08-12 18:42:03 +02004004 if (mode)
4005 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004006
Willy Tarreau7838a792019-08-12 18:42:03 +02004007 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004008}
4009
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004010/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004011static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
4012{
4013 struct h2s *h2s = cs->ctx;
4014
Willy Tarreau7838a792019-08-12 18:42:03 +02004015 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004016 if (cs->flags & CS_FL_KILL_CONN)
4017 h2s->flags |= H2_SF_KILL_CONN;
4018
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004019 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004020 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004021}
4022
Christopher Faulet9b79a102019-07-15 11:22:56 +02004023/* Decode the payload of a HEADERS frame and produce the HTX request or response
4024 * depending on the connection's side. Returns a positive value on success, a
4025 * negative value on failure, or 0 if it couldn't proceed. May report connection
4026 * errors in h2c->errcode if the frame is non-decodable and the connection
4027 * unrecoverable. In absence of connection error when a failure is reported, the
4028 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004029 *
4030 * The function may fold CONTINUATION frames into the initial HEADERS frame
4031 * by removing padding and next frame header, then moving the CONTINUATION
4032 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4033 * leaving a hole between the main frame and the beginning of the next one.
4034 * The possibly remaining incomplete or next frame at the end may be moved
4035 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4036 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4037 *
4038 * A buffer at the beginning of processing may look like this :
4039 *
4040 * ,---.---------.-----.--------------.--------------.------.---.
4041 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4042 * `---^---------^-----^--------------^--------------^------^---'
4043 * | | <-----> | |
4044 * area | dpl | wrap
4045 * |<--------------> |
4046 * | dfl |
4047 * |<-------------------------------------------------->|
4048 * head data
4049 *
4050 * Padding is automatically overwritten when folding, participating to the
4051 * hole size after dfl :
4052 *
4053 * ,---.------------------------.-----.--------------.------.---.
4054 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4055 * `---^------------------------^-----^--------------^------^---'
4056 * | | <-----> | |
4057 * area | hole | wrap
4058 * |<-----------------------> |
4059 * | dfl |
4060 * |<-------------------------------------------------->|
4061 * head data
4062 *
4063 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4064 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4065 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004066 *
4067 * The <flags> field must point to either the stream's flags or to a copy of it
4068 * so that the function can update the following flags :
4069 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004070 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004071 *
4072 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4073 * decoding, in order to detect if we're dealing with a headers or a trailers
4074 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004075 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01004076static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len)
Willy Tarreau13278b42017-10-13 19:23:14 +02004077{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004078 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004079 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004080 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004081 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004082 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004083 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004084 int flen; // header frame len
4085 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004086 int ret = 0;
4087 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004088 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004089
Willy Tarreau7838a792019-08-12 18:42:03 +02004090 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4091
Willy Tarreauea18f862018-12-22 20:19:26 +01004092next_frame:
4093 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4094 goto leave; // incomplete input frame
4095
4096 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4097 * this case, we'll try to paste it immediately after the initial
4098 * HEADERS frame payload and kill any possible padding. The initial
4099 * frame's length will be increased to represent the concatenation
4100 * of the two frames. The next frame is read from position <tlen>
4101 * and written at position <flen> (minus padding if some is present).
4102 */
4103 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4104 struct h2_fh hdr;
4105 int clen; // CONTINUATION frame's payload length
4106
Willy Tarreau7838a792019-08-12 18:42:03 +02004107 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 +01004108 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4109 /* no more data, the buffer may be full, either due to
4110 * too large a frame or because of too large a hole that
4111 * we're going to compact at the end.
4112 */
4113 goto leave;
4114 }
4115
4116 if (hdr.ft != H2_FT_CONTINUATION) {
4117 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004118 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 +01004119 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4120 goto fail;
4121 }
4122
4123 if (hdr.sid != h2c->dsi) {
4124 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004125 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 +01004126 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4127 goto fail;
4128 }
4129
4130 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4131 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004132 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 +01004133 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4134 goto fail;
4135 }
4136
4137 /* detect when we must stop aggragating frames */
4138 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4139
4140 /* Take as much as we can of the CONTINUATION frame's payload */
4141 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4142 if (clen > hdr.len)
4143 clen = hdr.len;
4144
4145 /* Move the frame's payload over the padding, hole and frame
4146 * header. At least one of hole or dpl is null (see diagrams
4147 * above). The hole moves after the new aggragated frame.
4148 */
4149 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
4150 h2c->dfl += clen - h2c->dpl;
4151 hole += h2c->dpl + 9;
4152 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004153 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 +01004154 goto next_frame;
4155 }
4156
4157 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004158
Willy Tarreau13278b42017-10-13 19:23:14 +02004159 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004160 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004161 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004162 copy = alloc_trash_chunk();
4163 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004164 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 +02004165 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4166 goto fail;
4167 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004168 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4169 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4170 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004171 }
4172
Willy Tarreau13278b42017-10-13 19:23:14 +02004173 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4174 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004175 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004176 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004177 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 +01004178 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004179 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004180 }
4181
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004182 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004183 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 +01004184 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4185 goto fail;
4186 }
4187
Willy Tarreau13278b42017-10-13 19:23:14 +02004188 hdrs += 5; // stream dep = 4, weight = 1
4189 flen -= 5;
4190 }
4191
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004192 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004193 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 +01004194 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004195 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004196 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004197
Willy Tarreau937f7602018-02-26 15:22:17 +01004198 /* we can't retry a failed decompression operation so we must be very
4199 * careful not to take any risks. In practice the output buffer is
4200 * always empty except maybe for trailers, in which case we simply have
4201 * to wait for the upper layer to finish consuming what is available.
4202 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004203 htx = htx_from_buf(rxbuf);
4204 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004205 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 +02004206 h2c->flags |= H2_CF_DEM_SFULL;
4207 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004208 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004209
Willy Tarreau25919232019-01-03 14:48:18 +01004210 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004211 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4212 sizeof(list)/sizeof(list[0]), tmp);
4213 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004214 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 +01004215 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
4216 goto fail;
4217 }
4218
Willy Tarreau25919232019-01-03 14:48:18 +01004219 /* The PACK decompressor was updated, let's update the input buffer and
4220 * the parser's state to commit these changes and allow us to later
4221 * fail solely on the stream if needed.
4222 */
4223 b_del(&h2c->dbuf, h2c->dfl + hole);
4224 h2c->dfl = hole = 0;
4225 h2c->st0 = H2_CS_FRAME_H;
4226
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004227 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01004228 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004229
Willy Tarreau88d138e2019-01-02 19:38:14 +01004230 if (*flags & H2_SF_HEADERS_RCVD)
4231 goto trailers;
4232
4233 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004234 if (h2c->flags & H2_CF_IS_BACK)
4235 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
4236 else
4237 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004238
4239 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01004240 /* too large headers? this is a stream error only */
Willy Tarreau7838a792019-08-12 18:42:03 +02004241 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 +01004242 goto fail;
4243 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004244
Willy Tarreau174b06a2018-04-25 18:13:58 +02004245 if (msgf & H2_MSGF_BODY) {
4246 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004247 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004248 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004249 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004250 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02004251 }
4252
Willy Tarreau88d138e2019-01-02 19:38:14 +01004253 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01004254 /* indicate that a HEADERS frame was received for this stream, except
4255 * for 1xx responses. For 1xx responses, another HEADERS frame is
4256 * expected.
4257 */
4258 if (!(msgf & H2_MSGF_RSP_1XX))
4259 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004260
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02004261 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02004262 /* Mark the end of message using EOM */
Willy Tarreau7838a792019-08-12 18:42:03 +02004263 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4264 TRACE_STATE("failed to append HTX EOM block into rxbuf", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR, h2c->conn);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004265 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004266 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004267 }
Willy Tarreau937f7602018-02-26 15:22:17 +01004268
Willy Tarreau86277d42019-01-02 15:36:11 +01004269 /* success */
4270 ret = 1;
4271
Willy Tarreau68dd9852017-07-03 14:44:26 +02004272 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01004273 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01004274 * move the remaining data over it.
4275 */
4276 if (hole) {
4277 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
4278 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
4279 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
4280 b_sub(&h2c->dbuf, hole);
4281 }
4282
Willy Tarreau97215ca2019-04-29 10:20:21 +02004283 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01004284 /* too large frames */
4285 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01004286 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01004287 }
4288
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004289 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004290 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02004291 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02004292 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01004293 return ret;
4294
Willy Tarreau68dd9852017-07-03 14:44:26 +02004295 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01004296 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02004297 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004298
4299 trailers:
4300 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01004301 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
4302 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02004303 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 +01004304 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4305 goto fail;
4306 }
4307
Christopher Faulet9b79a102019-07-15 11:22:56 +02004308 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02004309 if (h2_make_htx_trailers(list, htx) <= 0) {
4310 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 +02004311 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004312 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004313 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02004314}
4315
Christopher Faulet9b79a102019-07-15 11:22:56 +02004316/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004317 * parser state is automatically updated. Returns > 0 if it could completely
4318 * send the current frame, 0 if it couldn't complete, in which case
4319 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
4320 * DATA frame can return 0 as a valid result). Stream errors are reported in
4321 * h2s->errcode and connection errors in h2c->errcode. The caller must already
4322 * have checked the frame header and ensured that the frame was complete or the
4323 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02004324 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01004325static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02004326{
4327 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004328 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004329 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004330 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004331 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004332 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02004333
Willy Tarreau7838a792019-08-12 18:42:03 +02004334 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
4335
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004336 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02004337
Olivier Houchard638b7992018-08-16 15:41:52 +02004338 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004339 if (!csbuf) {
4340 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02004341 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 +01004342 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004343 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004344 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004345
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004346try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004347 flen = h2c->dfl - h2c->dpl;
4348 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01004349 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004350
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004351 if (flen > b_data(&h2c->dbuf)) {
4352 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004353 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01004354 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004355 }
4356
Christopher Faulet9b79a102019-07-15 11:22:56 +02004357 block = htx_free_data_space(htx);
4358 if (!block) {
4359 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004360 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 +02004361 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004362 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004363 if (flen > block)
4364 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004365
Christopher Faulet9b79a102019-07-15 11:22:56 +02004366 /* here, flen is the max we can copy into the output buffer */
4367 block = b_contig_data(&h2c->dbuf, 0);
4368 if (flen > block)
4369 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004370
Christopher Faulet9b79a102019-07-15 11:22:56 +02004371 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau7838a792019-08-12 18:42:03 +02004372 TRACE_DATA("move some data to h2s rxbuf", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s,, (void *)(long)sent);
Willy Tarreau454f9052017-10-26 19:40:35 +02004373
Christopher Faulet9b79a102019-07-15 11:22:56 +02004374 b_del(&h2c->dbuf, sent);
4375 h2c->dfl -= sent;
4376 h2c->rcvd_c += sent;
4377 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02004378
Christopher Faulet9b79a102019-07-15 11:22:56 +02004379 if (h2s->flags & H2_SF_DATA_CLEN) {
4380 h2s->body_len -= sent;
4381 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004382 }
4383
Christopher Faulet9b79a102019-07-15 11:22:56 +02004384 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01004385 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004386 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 +01004387 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004388 }
4389
Christopher Faulet9b79a102019-07-15 11:22:56 +02004390 goto try_again;
4391
Willy Tarreau4a28da12018-01-04 14:41:00 +01004392 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004393 /* here we're done with the frame, all the payload (except padding) was
4394 * transferred.
4395 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004396
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004397 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02004398 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004399 TRACE_STATE("h2s rxbuf is full, failed to add EOM", H2_EV_RX_FRAME|H2_EV_RX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004400 h2c->flags |= H2_CF_DEM_SFULL;
4401 goto fail;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004402 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004403 }
4404
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004405 h2c->rcvd_c += h2c->dpl;
4406 h2c->rcvd_s += h2c->dpl;
4407 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004408 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02004409 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004410 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004411 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004412 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004413 if (htx)
4414 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004415 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004416 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004417}
4418
Willy Tarreau115e83b2018-12-01 19:17:53 +01004419/* Try to send a HEADERS frame matching HTX response present in HTX message
4420 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4421 * must check the stream's status to detect any error which might have happened
4422 * subsequently to a successful send. The htx blocks are automatically removed
4423 * from the message. The htx message is assumed to be valid since produced from
4424 * the internal code, hence it contains a start line, an optional series of
4425 * header blocks and an end of header, otherwise an invalid frame could be
4426 * emitted and the resulting htx message could be left in an inconsistent state.
4427 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004428static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004429{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004430 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004431 struct h2c *h2c = h2s->h2c;
4432 struct htx_blk *blk;
4433 struct htx_blk *blk_end;
4434 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004435 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004436 struct htx_sl *sl;
4437 enum htx_blk_type type;
4438 int es_now = 0;
4439 int ret = 0;
4440 int hdr;
4441 int idx;
4442
Willy Tarreau7838a792019-08-12 18:42:03 +02004443 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4444
Willy Tarreau115e83b2018-12-01 19:17:53 +01004445 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004446 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004447 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004448 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004449 return 0;
4450 }
4451
Willy Tarreau115e83b2018-12-01 19:17:53 +01004452 /* determine the first block which must not be deleted, blk_end may
4453 * be NULL if all blocks have to be deleted.
4454 */
4455 idx = htx_get_head(htx);
4456 blk_end = NULL;
4457 while (idx != -1) {
4458 type = htx_get_blk_type(htx_get_blk(htx, idx));
4459 idx = htx_get_next(htx, idx);
4460 if (type == HTX_BLK_EOH) {
4461 if (idx != -1)
4462 blk_end = htx_get_blk(htx, idx);
4463 break;
4464 }
4465 }
4466
4467 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004468 blk = htx_get_head_blk(htx);
4469 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4470 ALREADY_CHECKED(blk);
4471 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004472 h2s->status = sl->info.res.status;
Willy Tarreau7838a792019-08-12 18:42:03 +02004473 if (h2s->status < 100 || h2s->status > 999) {
4474 TRACE_PROTO("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreauaafdf582018-12-10 18:06:40 +01004475 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004476 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004477
4478 /* and the rest of the headers, that we dump starting at header 0 */
4479 hdr = 0;
4480
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004481 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004482 while ((idx = htx_get_next(htx, idx)) != -1) {
4483 blk = htx_get_blk(htx, idx);
4484 type = htx_get_blk_type(blk);
4485
4486 if (type == HTX_BLK_UNUSED)
4487 continue;
4488
4489 if (type != HTX_BLK_HDR)
4490 break;
4491
Willy Tarreau7838a792019-08-12 18:42:03 +02004492 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4493 TRACE_PROTO("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004494 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004495 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004496
4497 list[hdr].n = htx_get_blk_name(htx, blk);
4498 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004499 hdr++;
4500 }
4501
4502 /* marker for end of headers */
4503 list[hdr].n = ist("");
4504
4505 if (h2s->status == 204 || h2s->status == 304) {
4506 /* no contents, claim c-len is present and set to zero */
4507 es_now = 1;
4508 }
4509
Willy Tarreau9c218e72019-05-26 10:08:28 +02004510 mbuf = br_tail(h2c->mbuf);
4511 retry:
4512 if (!h2_get_buf(h2c, mbuf)) {
4513 h2c->flags |= H2_CF_MUX_MALLOC;
4514 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004515 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 +02004516 return 0;
4517 }
4518
Willy Tarreau115e83b2018-12-01 19:17:53 +01004519 chunk_reset(&outbuf);
4520
4521 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004522 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4523 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004524 break;
4525 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004526 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004527 }
4528
4529 if (outbuf.size < 9)
4530 goto full;
4531
4532 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4533 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4534 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4535 outbuf.data = 9;
4536
4537 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004538 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004539 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004540 goto realign_again;
4541 goto full;
4542 }
4543
4544 /* encode all headers, stop at empty name */
4545 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4546 /* these ones do not exist in H2 and must be dropped. */
4547 if (isteq(list[hdr].n, ist("connection")) ||
4548 isteq(list[hdr].n, ist("proxy-connection")) ||
4549 isteq(list[hdr].n, ist("keep-alive")) ||
4550 isteq(list[hdr].n, ist("upgrade")) ||
4551 isteq(list[hdr].n, ist("transfer-encoding")))
4552 continue;
4553
Christopher Faulet86d144c2019-08-14 16:32:25 +02004554 /* Skip all pseudo-headers */
4555 if (*(list[hdr].n.ptr) == ':')
4556 continue;
4557
Willy Tarreau115e83b2018-12-01 19:17:53 +01004558 if (isteq(list[hdr].n, ist("")))
4559 break; // end
4560
4561 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4562 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004563 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004564 goto realign_again;
4565 goto full;
4566 }
4567 }
4568
Christopher Faulet0b465482019-02-19 15:14:23 +01004569 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004570 * FIXME: we should also set it when we know for sure that the
4571 * content-length is zero as well as on 204/304
4572 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004573 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4574 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004575 es_now = 1;
4576
Willy Tarreau927b88b2019-03-04 08:03:25 +01004577 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004578 es_now = 1;
4579
4580 /* update the frame's size */
4581 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4582
4583 if (es_now)
4584 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4585
4586 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02004587 TRACE_USER("sent H2 response", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02004588 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004589
4590 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4591 * 1xx responses, another HEADERS frame is expected.
4592 */
4593 if (h2s->status >= 200 || h2s->status == 101)
4594 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004595
Willy Tarreau115e83b2018-12-01 19:17:53 +01004596 if (es_now) {
4597 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02004598 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 +01004599 if (h2s->st == H2_SS_OPEN)
4600 h2s->st = H2_SS_HLOC;
4601 else
4602 h2s_close(h2s);
4603 }
4604
4605 /* OK we could properly deliver the response */
4606
4607 /* remove all header blocks including the EOH and compute the
4608 * corresponding size.
4609 *
4610 * FIXME: We should remove everything when es_now is set.
4611 */
4612 ret = 0;
4613 idx = htx_get_head(htx);
4614 blk = htx_get_blk(htx, idx);
4615 while (blk != blk_end) {
4616 ret += htx_get_blksz(blk);
4617 blk = htx_remove_blk(htx, blk);
4618 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004619
Christopher Faulet316934d2019-05-15 14:22:48 +02004620 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4621 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004622 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004623 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004624 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004625 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004626 return ret;
4627 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004628 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4629 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004630 h2c->flags |= H2_CF_MUX_MFULL;
4631 h2s->flags |= H2_SF_BLK_MROOM;
4632 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004633 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 +01004634 goto end;
4635 fail:
4636 /* unparsable HTX messages, too large ones to be produced in the local
4637 * list etc go here (unrecoverable errors).
4638 */
4639 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4640 ret = 0;
4641 goto end;
4642}
4643
Willy Tarreau80739692018-10-05 11:35:57 +02004644/* Try to send a HEADERS frame matching HTX request present in HTX message
4645 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4646 * must check the stream's status to detect any error which might have happened
4647 * subsequently to a successful send. The htx blocks are automatically removed
4648 * from the message. The htx message is assumed to be valid since produced from
4649 * the internal code, hence it contains a start line, an optional series of
4650 * header blocks and an end of header, otherwise an invalid frame could be
4651 * emitted and the resulting htx message could be left in an inconsistent state.
4652 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004653static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02004654{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004655 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004656 struct h2c *h2c = h2s->h2c;
4657 struct htx_blk *blk;
4658 struct htx_blk *blk_end;
4659 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004660 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004661 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004662 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004663 enum htx_blk_type type;
4664 int es_now = 0;
4665 int ret = 0;
4666 int hdr;
4667 int idx;
4668
Willy Tarreau7838a792019-08-12 18:42:03 +02004669 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4670
Willy Tarreau80739692018-10-05 11:35:57 +02004671 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004672 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004673 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004674 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004675 return 0;
4676 }
4677
Willy Tarreau80739692018-10-05 11:35:57 +02004678 /* determine the first block which must not be deleted, blk_end may
4679 * be NULL if all blocks have to be deleted.
4680 */
4681 idx = htx_get_head(htx);
4682 blk_end = NULL;
4683 while (idx != -1) {
4684 type = htx_get_blk_type(htx_get_blk(htx, idx));
4685 idx = htx_get_next(htx, idx);
4686 if (type == HTX_BLK_EOH) {
4687 if (idx != -1)
4688 blk_end = htx_get_blk(htx, idx);
4689 break;
4690 }
4691 }
4692
4693 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004694 blk = htx_get_head_blk(htx);
4695 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4696 ALREADY_CHECKED(blk);
4697 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004698 meth = htx_sl_req_meth(sl);
4699 path = htx_sl_req_uri(sl);
4700
4701 /* and the rest of the headers, that we dump starting at header 0 */
4702 hdr = 0;
4703
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004704 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004705 while ((idx = htx_get_next(htx, idx)) != -1) {
4706 blk = htx_get_blk(htx, idx);
4707 type = htx_get_blk_type(blk);
4708
4709 if (type == HTX_BLK_UNUSED)
4710 continue;
4711
4712 if (type != HTX_BLK_HDR)
4713 break;
4714
Willy Tarreau7838a792019-08-12 18:42:03 +02004715 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4716 TRACE_PROTO("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004717 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004718 }
Willy Tarreau80739692018-10-05 11:35:57 +02004719
4720 list[hdr].n = htx_get_blk_name(htx, blk);
4721 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004722 hdr++;
4723 }
4724
4725 /* marker for end of headers */
4726 list[hdr].n = ist("");
4727
Willy Tarreau9c218e72019-05-26 10:08:28 +02004728 mbuf = br_tail(h2c->mbuf);
4729 retry:
4730 if (!h2_get_buf(h2c, mbuf)) {
4731 h2c->flags |= H2_CF_MUX_MALLOC;
4732 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004733 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 +02004734 return 0;
4735 }
4736
Willy Tarreau80739692018-10-05 11:35:57 +02004737 chunk_reset(&outbuf);
4738
4739 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004740 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4741 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004742 break;
4743 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004744 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004745 }
4746
4747 if (outbuf.size < 9)
4748 goto full;
4749
4750 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4751 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4752 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4753 outbuf.data = 9;
4754
4755 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004756 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004757 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004758 goto realign_again;
4759 goto full;
4760 }
4761
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004762 /* RFC7540 #8.3: the CONNECT method must have :
4763 * - :authority set to the URI part (host:port)
4764 * - :method set to CONNECT
4765 * - :scheme and :path omitted
4766 */
4767 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4768 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004769 struct ist scheme;
4770
4771 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4772 scheme = ist("http");
4773 else
4774 scheme = ist("https");
4775
4776 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004777 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004778 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004779 goto realign_again;
4780 goto full;
4781 }
Willy Tarreau80739692018-10-05 11:35:57 +02004782
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004783 /* encode the path, which necessarily is the second one */
4784 if (!hpack_encode_path(&outbuf, path)) {
4785 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004786 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004787 goto realign_again;
4788 goto full;
4789 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004790
4791 /* look for the Host header and place it in :authority */
4792 auth = ist2(NULL, 0);
4793 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4794 if (isteq(list[hdr].n, ist("")))
4795 break; // end
4796
4797 if (isteq(list[hdr].n, ist("host"))) {
4798 auth = list[hdr].v;
4799 break;
4800 }
4801 }
4802 }
4803 else {
4804 /* for CONNECT, :authority is taken from the path */
4805 auth = path;
4806 }
4807
4808 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4809 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004810 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004811 goto realign_again;
4812 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004813 }
4814
4815 /* encode all headers, stop at empty name */
4816 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4817 /* these ones do not exist in H2 and must be dropped. */
4818 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004819 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004820 isteq(list[hdr].n, ist("proxy-connection")) ||
4821 isteq(list[hdr].n, ist("keep-alive")) ||
4822 isteq(list[hdr].n, ist("upgrade")) ||
4823 isteq(list[hdr].n, ist("transfer-encoding")))
4824 continue;
4825
Christopher Faulet86d144c2019-08-14 16:32:25 +02004826 /* Skip all pseudo-headers */
4827 if (*(list[hdr].n.ptr) == ':')
4828 continue;
4829
Willy Tarreau80739692018-10-05 11:35:57 +02004830 if (isteq(list[hdr].n, ist("")))
4831 break; // end
4832
4833 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4834 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004835 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004836 goto realign_again;
4837 goto full;
4838 }
4839 }
4840
4841 /* we may need to add END_STREAM if we have no body :
4842 * - request already closed, or :
4843 * - no transfer-encoding, and :
4844 * - no content-length or content-length:0
4845 * Fixme: this doesn't take into account CONNECT requests.
4846 */
4847 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4848 es_now = 1;
4849
4850 if (sl->flags & HTX_SL_F_BODYLESS)
4851 es_now = 1;
4852
Willy Tarreau927b88b2019-03-04 08:03:25 +01004853 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004854 es_now = 1;
4855
4856 /* update the frame's size */
4857 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4858
4859 if (es_now)
4860 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4861
4862 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02004863 TRACE_USER("sent H2 request", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02004864 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004865 h2s->flags |= H2_SF_HEADERS_SENT;
4866 h2s->st = H2_SS_OPEN;
4867
Willy Tarreau80739692018-10-05 11:35:57 +02004868 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004869 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 +02004870 // trim any possibly pending data (eg: inconsistent content-length)
4871 h2s->flags |= H2_SF_ES_SENT;
4872 h2s->st = H2_SS_HLOC;
4873 }
4874
4875 /* remove all header blocks including the EOH and compute the
4876 * corresponding size.
4877 *
4878 * FIXME: We should remove everything when es_now is set.
4879 */
4880 ret = 0;
4881 idx = htx_get_head(htx);
4882 blk = htx_get_blk(htx, idx);
4883 while (blk != blk_end) {
4884 ret += htx_get_blksz(blk);
4885 blk = htx_remove_blk(htx, blk);
4886 }
4887
Christopher Faulet316934d2019-05-15 14:22:48 +02004888 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4889 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004890 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004891 }
Willy Tarreau80739692018-10-05 11:35:57 +02004892
4893 end:
4894 return ret;
4895 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004896 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4897 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004898 h2c->flags |= H2_CF_MUX_MFULL;
4899 h2s->flags |= H2_SF_BLK_MROOM;
4900 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004901 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 +02004902 goto end;
4903 fail:
4904 /* unparsable HTX messages, too large ones to be produced in the local
4905 * list etc go here (unrecoverable errors).
4906 */
4907 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4908 ret = 0;
4909 goto end;
4910}
4911
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004912/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004913 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4914 * caller must check the stream's status to detect any error which might have
4915 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004916 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004917 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004918static size_t h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004919{
4920 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004921 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004922 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004923 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004924 size_t total = 0;
4925 int es_now = 0;
4926 int bsize; /* htx block size */
4927 int fsize; /* h2 frame size */
4928 struct htx_blk *blk;
4929 enum htx_blk_type type;
4930 int idx;
4931
Willy Tarreau7838a792019-08-12 18:42:03 +02004932 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
4933
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004934 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004935 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004936 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004937 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004938 goto end;
4939 }
4940
Willy Tarreau98de12a2018-12-12 07:03:00 +01004941 htx = htx_from_buf(buf);
4942
Christopher Faulet54b5e212019-06-04 10:08:28 +02004943 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4944 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4945 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004946 */
4947
4948 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004949 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004950 goto end;
4951
4952 idx = htx_get_head(htx);
4953 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004954 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004955 bsize = htx_get_blksz(blk);
4956 fsize = bsize;
4957
Christopher Faulet54b5e212019-06-04 10:08:28 +02004958 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004959 if (h2s->flags & H2_SF_ES_SENT) {
4960 /* ES already sent */
4961 htx_remove_blk(htx, blk);
4962 total++; // EOM counts as one byte
4963 count--;
4964 goto end;
4965 }
4966 }
4967 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004968 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004969
Willy Tarreau9c218e72019-05-26 10:08:28 +02004970 mbuf = br_tail(h2c->mbuf);
4971 retry:
4972 if (!h2_get_buf(h2c, mbuf)) {
4973 h2c->flags |= H2_CF_MUX_MALLOC;
4974 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004975 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 +02004976 goto end;
4977 }
4978
Willy Tarreau98de12a2018-12-12 07:03:00 +01004979 /* Perform some optimizations to reduce the number of buffer copies.
4980 * First, if the mux's buffer is empty and the htx area contains
4981 * exactly one data block of the same size as the requested count, and
4982 * this count fits within the frame size, the stream's window size, and
4983 * the connection's window size, then it's possible to simply swap the
4984 * caller's buffer with the mux's output buffer and adjust offsets and
4985 * length to match the entire DATA HTX block in the middle. In this
4986 * case we perform a true zero-copy operation from end-to-end. This is
4987 * the situation that happens all the time with large files. Second, if
4988 * this is not possible, but the mux's output buffer is empty, we still
4989 * have an opportunity to avoid the copy to the intermediary buffer, by
4990 * making the intermediary buffer's area point to the output buffer's
4991 * area. In this case we want to skip the HTX header to make sure that
4992 * copies remain aligned and that this operation remains possible all
4993 * the time. This goes for headers, data blocks and any data extracted
4994 * from the HTX blocks.
4995 */
4996 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02004997 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02004998 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004999 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005000
Willy Tarreaubcc45952019-05-26 10:05:50 +02005001 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005002 /* Too bad there are data left there. We're willing to memcpy/memmove
5003 * up to 1/4 of the buffer, which means that it's OK to copy a large
5004 * frame into a buffer containing few data if it needs to be realigned,
5005 * and that it's also OK to copy few data without realigning. Otherwise
5006 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005007 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005008 if (fsize + 9 <= b_room(mbuf) &&
5009 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005010 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
5011 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 +01005012 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02005013 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005014
Willy Tarreau9c218e72019-05-26 10:08:28 +02005015 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5016 goto retry;
5017
Willy Tarreau98de12a2018-12-12 07:03:00 +01005018 h2c->flags |= H2_CF_MUX_MFULL;
5019 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005020 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 +01005021 goto end;
5022 }
5023
5024 /* map an H2 frame to the HTX block so that we can put the
5025 * frame header there.
5026 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005027 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5028 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005029
5030 /* prepend an H2 DATA frame header just before the DATA block */
5031 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5032 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5033 h2_set_frame_size(outbuf.area, fsize);
5034
5035 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005036 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005037 h2c->mws -= fsize;
5038
5039 /* and exchange with our old area */
5040 buf->area = old_area;
5041 buf->data = buf->head = 0;
5042 total += fsize;
Willy Tarreau7838a792019-08-12 18:42:03 +02005043
5044 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 +01005045 goto end;
5046 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005047
Willy Tarreau98de12a2018-12-12 07:03:00 +01005048 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005049 /* for DATA and EOM we'll have to emit a frame, even if empty */
5050
5051 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005052 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5053 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005054 break;
5055 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005056 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005057 }
5058
5059 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005060 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5061 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005062 h2c->flags |= H2_CF_MUX_MFULL;
5063 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005064 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005065 goto end;
5066 }
5067
5068 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5069 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5070 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5071 outbuf.data = 9;
5072
5073 /* we have in <fsize> the exact number of bytes we need to copy from
5074 * the HTX buffer. We need to check this against the connection's and
5075 * the stream's send windows, and to ensure that this fits in the max
5076 * frame size and in the buffer's available space minus 9 bytes (for
5077 * the frame header). The connection's flow control is applied last so
5078 * that we can use a separate list of streams which are immediately
5079 * unblocked on window opening. Note: we don't implement padding.
5080 */
5081
5082 /* EOM is presented with bsize==1 but would lead to the emission of an
5083 * empty frame, thus we force it to zero here.
5084 */
5085 if (type == HTX_BLK_EOM)
5086 bsize = fsize = 0;
5087
5088 if (!fsize)
5089 goto send_empty;
5090
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005091 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005092 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005093 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005094 LIST_DEL_INIT(&h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02005095 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 +01005096 goto end;
5097 }
5098
Willy Tarreauee573762018-12-04 15:25:57 +01005099 if (fsize > count)
5100 fsize = count;
5101
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005102 if (fsize > h2s_mws(h2s))
5103 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005104
5105 if (h2c->mfs && fsize > h2c->mfs)
5106 fsize = h2c->mfs; // >0
5107
5108 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005109 /* It doesn't fit at once. If it at least fits once split and
5110 * the amount of data to move is low, let's defragment the
5111 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005112 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005113 if (b_space_wraps(mbuf) &&
5114 (fsize + 9 <= b_room(mbuf)) &&
5115 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005116 goto realign_again;
5117 fsize = outbuf.size - 9;
5118
5119 if (fsize <= 0) {
5120 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005121 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5122 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005123 h2c->flags |= H2_CF_MUX_MFULL;
5124 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005125 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005126 goto end;
5127 }
5128 }
5129
5130 if (h2c->mws <= 0) {
5131 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005132 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 +01005133 goto end;
5134 }
5135
5136 if (fsize > h2c->mws)
5137 fsize = h2c->mws;
5138
5139 /* now let's copy this this into the output buffer */
5140 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005141 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005142 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005143 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005144
5145 send_empty:
5146 /* update the frame's size */
5147 h2_set_frame_size(outbuf.area, fsize);
5148
5149 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5150 * meeting EOM. We should optimize this later.
5151 */
5152 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005153 total++; // EOM counts as one byte
5154 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005155 es_now = 1;
5156 }
5157
5158 if (es_now)
5159 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5160
5161 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005162 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005163
5164 /* consume incoming HTX block, including EOM */
5165 total += fsize;
5166 if (fsize == bsize) {
5167 htx_remove_blk(htx, blk);
Willy Tarreau7838a792019-08-12 18:42:03 +02005168 if (fsize) {
5169 TRACE_DEVEL("more data available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005170 goto new_frame;
Willy Tarreau7838a792019-08-12 18:42:03 +02005171 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005172 } else {
5173 /* we've truncated this block */
5174 htx_cut_data_blk(htx, blk, fsize);
5175 }
5176
5177 if (es_now) {
5178 if (h2s->st == H2_SS_OPEN)
5179 h2s->st = H2_SS_HLOC;
5180 else
5181 h2s_close(h2s);
5182
5183 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005184 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 +01005185 }
5186
5187 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005188 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005189 return total;
5190}
5191
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005192/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5193 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5194 * processed. The caller must check the stream's status to detect any error
5195 * which might have happened subsequently to a successful send. The htx blocks
5196 * are automatically removed from the message. The htx message is assumed to be
5197 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005198 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005199 * as a single frame. The ES flag is always set.
5200 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005201static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005202{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005203 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005204 struct h2c *h2c = h2s->h2c;
5205 struct htx_blk *blk;
5206 struct htx_blk *blk_end;
5207 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005208 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005209 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005210 int ret = 0;
5211 int hdr;
5212 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005213
Willy Tarreau7838a792019-08-12 18:42:03 +02005214 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5215
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005216 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005217 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005218 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005219 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005220 goto end;
5221 }
5222
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005223 /* determine the first block which must not be deleted, blk_end may
5224 * be NULL if all blocks have to be deleted. also get trailers.
5225 */
5226 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005227 blk_end = NULL;
5228
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005229 hdr = 0;
5230 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005231 blk = htx_get_blk(htx, idx);
5232 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005233 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005234 if (type == HTX_BLK_UNUSED)
5235 continue;
5236
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005237 if (type == HTX_BLK_EOT) {
5238 if (idx != -1)
5239 blk_end = blk;
5240 break;
5241 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005242 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005243 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005244
Willy Tarreau7838a792019-08-12 18:42:03 +02005245 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5246 TRACE_PROTO("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005247 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005248 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005249
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005250 list[hdr].n = htx_get_blk_name(htx, blk);
5251 list[hdr].v = htx_get_blk_value(htx, blk);
5252 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005253 }
5254
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005255 /* marker for end of trailers */
5256 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005257
Willy Tarreau9c218e72019-05-26 10:08:28 +02005258 mbuf = br_tail(h2c->mbuf);
5259 retry:
5260 if (!h2_get_buf(h2c, mbuf)) {
5261 h2c->flags |= H2_CF_MUX_MALLOC;
5262 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005263 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 +02005264 goto end;
5265 }
5266
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005267 chunk_reset(&outbuf);
5268
5269 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005270 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5271 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005272 break;
5273 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005274 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005275 }
5276
5277 if (outbuf.size < 9)
5278 goto full;
5279
5280 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5281 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5282 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5283 outbuf.data = 9;
5284
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005285 /* encode all headers */
5286 for (idx = 0; idx < hdr; idx++) {
5287 /* these ones do not exist in H2 or must not appear in
5288 * trailers and must be dropped.
5289 */
5290 if (isteq(list[idx].n, ist("host")) ||
5291 isteq(list[idx].n, ist("content-length")) ||
5292 isteq(list[idx].n, ist("connection")) ||
5293 isteq(list[idx].n, ist("proxy-connection")) ||
5294 isteq(list[idx].n, ist("keep-alive")) ||
5295 isteq(list[idx].n, ist("upgrade")) ||
5296 isteq(list[idx].n, ist("te")) ||
5297 isteq(list[idx].n, ist("transfer-encoding")))
5298 continue;
5299
Christopher Faulet86d144c2019-08-14 16:32:25 +02005300 /* Skip all pseudo-headers */
5301 if (*(list[idx].n.ptr) == ':')
5302 continue;
5303
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005304 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5305 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005306 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005307 goto realign_again;
5308 goto full;
5309 }
5310 }
5311
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005312 if (outbuf.data == 9) {
5313 /* here we have a problem, we have nothing to emit (either we
5314 * received an empty trailers block followed or we removed its
5315 * contents above). Because of this we can't send a HEADERS
5316 * frame, so we have to cheat and instead send an empty DATA
5317 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005318 */
5319 outbuf.area[3] = H2_FT_DATA;
5320 outbuf.area[4] = H2_F_DATA_END_STREAM;
5321 }
5322
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005323 /* update the frame's size */
5324 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5325
5326 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02005327 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 +02005328 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005329 h2s->flags |= H2_SF_ES_SENT;
5330
5331 if (h2s->st == H2_SS_OPEN)
5332 h2s->st = H2_SS_HLOC;
5333 else
5334 h2s_close(h2s);
5335
5336 /* OK we could properly deliver the response */
5337 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005338 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005339 ret = 0;
5340 idx = htx_get_head(htx);
5341 blk = htx_get_blk(htx, idx);
5342 while (blk != blk_end) {
5343 ret += htx_get_blksz(blk);
5344 blk = htx_remove_blk(htx, blk);
5345 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005346
5347 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5348 ret += htx_get_blksz(blk_end);
5349 htx_remove_blk(htx, blk_end);
5350 }
5351
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005352 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005353 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005354 return ret;
5355 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005356 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5357 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005358 h2c->flags |= H2_CF_MUX_MFULL;
5359 h2s->flags |= H2_SF_BLK_MROOM;
5360 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005361 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 +01005362 goto end;
5363 fail:
5364 /* unparsable HTX messages, too large ones to be produced in the local
5365 * list etc go here (unrecoverable errors).
5366 */
5367 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5368 ret = 0;
5369 goto end;
5370}
5371
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005372/* Called from the upper layer, to subscribe to events, such as being able to send.
5373 * The <param> argument here is supposed to be a pointer to a wait_event struct
5374 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5375 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5376 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5377 * returns 0 except for the error above.
5378 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005379static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5380{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005381 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005382 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005383 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005384
Willy Tarreau7838a792019-08-12 18:42:03 +02005385 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005386 if (event_type & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005387 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005388 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005389 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5390 sw->events |= SUB_RETRY_RECV;
5391 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005392 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005393 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005394 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005395 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchard6ff20392018-07-17 18:46:31 +02005396 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005397 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5398 sw->events |= SUB_RETRY_SEND;
5399 h2s->send_wait = sw;
5400 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5401 !LIST_ADDED(&h2s->list)) {
5402 if (h2s->flags & H2_SF_BLK_MFCTL)
5403 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5404 else
5405 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005406 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005407 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005408 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005409 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005410 if (event_type != 0)
5411 return -1;
5412 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005413}
5414
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005415/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5416 * The <param> argument here is supposed to be a pointer to the same wait_event
5417 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5418 * It always returns zero.
5419 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005420static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5421{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005422 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005423 struct h2s *h2s = cs->ctx;
5424
Willy Tarreau7838a792019-08-12 18:42:03 +02005425 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005426 if (event_type & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005427 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005428 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005429 BUG_ON(h2s->recv_wait != sw);
5430 sw->events &= ~SUB_RETRY_RECV;
5431 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005432 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005433 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005434 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005435 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005436 BUG_ON(h2s->send_wait != sw);
5437 LIST_DEL(&h2s->list);
5438 LIST_INIT(&h2s->list);
5439 sw->events &= ~SUB_RETRY_SEND;
5440 /* We were about to send, make sure it does not happen */
5441 if (LIST_ADDED(&h2s->sending_list) &&
5442 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005443 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005444 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005445 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005446 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005447 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005448 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005449 return 0;
5450}
5451
5452
Olivier Houchard511efea2018-08-16 15:30:32 +02005453/* Called from the upper layer, to receive data */
5454static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5455{
Olivier Houchard638b7992018-08-16 15:41:52 +02005456 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005457 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005458 struct htx *h2s_htx = NULL;
5459 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005460 size_t ret = 0;
5461
Willy Tarreau7838a792019-08-12 18:42:03 +02005462 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
5463
Olivier Houchard511efea2018-08-16 15:30:32 +02005464 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005465 h2s_htx = htx_from_buf(&h2s->rxbuf);
5466 if (htx_is_empty(h2s_htx)) {
5467 /* Here htx_to_buf() will set buffer data to 0 because
5468 * the HTX is empty.
5469 */
5470 htx_to_buf(h2s_htx, &h2s->rxbuf);
5471 goto end;
5472 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005473
Christopher Faulet9b79a102019-07-15 11:22:56 +02005474 ret = h2s_htx->data;
5475 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005476
Christopher Faulet9b79a102019-07-15 11:22:56 +02005477 /* <buf> is empty and the message is small enough, swap the
5478 * buffers. */
5479 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005480 htx_to_buf(buf_htx, buf);
5481 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02005482 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5483 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01005484 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005485
5486 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
5487
5488 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
5489 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5490 if (htx_is_empty(buf_htx))
5491 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01005492 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005493
Christopher Faulet9b79a102019-07-15 11:22:56 +02005494 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
5495 htx_to_buf(buf_htx, buf);
5496 htx_to_buf(h2s_htx, &h2s->rxbuf);
5497 ret -= h2s_htx->data;
5498
Christopher Faulet37070b22019-02-14 15:12:14 +01005499 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005500 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005501 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005502 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005503 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005504 if (h2s->flags & H2_SF_ES_RCVD)
5505 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005506 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005507 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005508 if (cs->flags & CS_FL_ERR_PENDING)
5509 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005510 if (b_size(&h2s->rxbuf)) {
5511 b_free(&h2s->rxbuf);
5512 offer_buffers(NULL, tasks_run_queue);
5513 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005514 }
5515
Willy Tarreau082f5592018-11-25 08:03:32 +01005516 if (ret && h2c->dsi == h2s->id) {
5517 /* demux is blocking on this stream's buffer */
5518 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005519 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005520 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005521
Willy Tarreau7838a792019-08-12 18:42:03 +02005522 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02005523 return ret;
5524}
5525
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005526/* stops all senders of this connection for example when the mux buffer is full.
5527 * They are moved from the sending_list to either fctl_list or send_list.
5528 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005529static void h2_stop_senders(struct h2c *h2c)
5530{
5531 struct h2s *h2s, *h2s_back;
5532
Olivier Houchardd360ac62019-03-22 17:37:16 +01005533 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5534 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005535 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005536 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005537 }
5538}
5539
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005540/* Called from the upper layer, to send data from buffer <buf> for no more than
5541 * <count> bytes. Returns the number of bytes effectively sent. Some status
5542 * flags may be updated on the conn_stream.
5543 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005544static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005545{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005546 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005547 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005548 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005549 struct htx *htx;
5550 struct htx_blk *blk;
5551 enum htx_blk_type btype;
5552 uint32_t bsize;
5553 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005554
Willy Tarreau7838a792019-08-12 18:42:03 +02005555 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
5556
Olivier Houchardd360ac62019-03-22 17:37:16 +01005557 /* If we were not just woken because we wanted to send but couldn't,
5558 * and there's somebody else that is waiting to send, do nothing,
5559 * we will subscribe later and be put at the end of the list
5560 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005561 if (!LIST_ADDED(&h2s->sending_list) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02005562 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
5563 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 +01005564 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005565 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005566 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005567
Olivier Houchard998410a2019-04-15 19:23:37 +02005568 /* We couldn't set it to NULL before, because we needed it in case
5569 * we had to cancel the tasklet
5570 */
5571 h2s->send_wait = NULL;
5572
Willy Tarreau7838a792019-08-12 18:42:03 +02005573 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
5574 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 +02005575 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005576 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005577
Christopher Faulet9b79a102019-07-15 11:22:56 +02005578 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005579
Willy Tarreau0bad0432018-06-14 16:54:01 +02005580 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005581 h2s->flags |= H2_SF_OUTGOING_DATA;
5582
Willy Tarreau751f2d02018-10-05 09:35:00 +02005583 if (h2s->id == 0) {
5584 int32_t id = h2c_get_next_sid(h2s->h2c);
5585
5586 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005587 cs->flags |= CS_FL_ERROR;
Willy Tarreau7838a792019-08-12 18:42:03 +02005588 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 +02005589 return 0;
5590 }
5591
5592 eb32_delete(&h2s->by_id);
5593 h2s->by_id.key = h2s->id = id;
5594 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005595 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005596 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5597 }
5598
Christopher Faulet9b79a102019-07-15 11:22:56 +02005599 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
5600 count && !htx_is_empty(htx)) {
5601 idx = htx_get_head(htx);
5602 blk = htx_get_blk(htx, idx);
5603 btype = htx_get_blk_type(blk);
5604 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005605
Christopher Faulet9b79a102019-07-15 11:22:56 +02005606 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005607 case HTX_BLK_REQ_SL:
5608 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005609 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02005610 if (ret > 0) {
5611 total += ret;
5612 count -= ret;
5613 if (ret < bsize)
5614 goto done;
5615 }
5616 break;
5617
Willy Tarreau115e83b2018-12-01 19:17:53 +01005618 case HTX_BLK_RES_SL:
5619 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005620 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005621 if (ret > 0) {
5622 total += ret;
5623 count -= ret;
5624 if (ret < bsize)
5625 goto done;
5626 }
5627 break;
5628
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005629 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005630 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005631 /* all these cause the emission of a DATA frame (possibly empty).
5632 * This EOM necessarily is one before trailers, as the EOM following
5633 * trailers would have been consumed by the trailers parser.
5634 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005635 ret = h2s_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005636 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005637 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005638 total += ret;
5639 count -= ret;
5640 if (ret < bsize)
5641 goto done;
5642 }
5643 break;
5644
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005645 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005646 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005647 /* This is the first trailers block, all the subsequent ones AND
5648 * the EOM will be swallowed by the parser.
5649 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005650 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005651 if (ret > 0) {
5652 total += ret;
5653 count -= ret;
5654 if (ret < bsize)
5655 goto done;
5656 }
5657 break;
5658
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005659 default:
5660 htx_remove_blk(htx, blk);
5661 total += bsize;
5662 count -= bsize;
5663 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005664 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005665 }
5666
Christopher Faulet9b79a102019-07-15 11:22:56 +02005667 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005668 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005669 /* trim any possibly pending data after we close (extra CR-LF,
5670 * unprocessed trailers, abnormal extra data, ...)
5671 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005672 total += count;
5673 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005674 }
5675
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005676 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005677 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005678 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 +01005679 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005680 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005681 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005682 }
5683
Christopher Faulet9b79a102019-07-15 11:22:56 +02005684 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02005685
5686 /* The mux is full, cancel the pending tasks */
5687 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005688 (h2s->flags & H2_SF_BLK_MBUSY)) {
5689 TRACE_DEVEL("mux full, stopping senders", H2_EV_H2S_SEND|H2_EV_H2C_BLK|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
Olivier Houchardd846c262018-10-19 17:24:29 +02005690 h2_stop_senders(h2s->h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02005691 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005692
Olivier Houchard7505f942018-08-21 18:10:44 +02005693 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005694 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau7838a792019-08-12 18:42:03 +02005695 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 +02005696 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005697
Olivier Houchard7505f942018-08-21 18:10:44 +02005698 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005699 /* If we're waiting for flow control, and we got a shutr on the
5700 * connection, we will never be unlocked, so add an error on
5701 * the conn_stream.
5702 */
5703 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5704 !b_data(&h2s->h2c->dbuf) &&
5705 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005706 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 +01005707 if (cs->flags & CS_FL_EOS)
5708 cs->flags |= CS_FL_ERROR;
5709 else
5710 cs->flags |= CS_FL_ERR_PENDING;
5711 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005712 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005713 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005714 LIST_DEL_INIT(&h2s->list);
5715 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005716 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005717 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005718}
5719
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005720/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005721static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005722{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005723 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005724 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005725 struct eb32_node *node;
5726 int fctl_cnt = 0;
5727 int send_cnt = 0;
5728 int tree_cnt = 0;
5729 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005730 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005731
5732 if (!h2c)
5733 return;
5734
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005735 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005736 fctl_cnt++;
5737
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005738 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005739 send_cnt++;
5740
Willy Tarreau3af37712018-12-18 14:34:41 +01005741 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005742 node = eb32_first(&h2c->streams_by_id);
5743 while (node) {
5744 h2s = container_of(node, struct h2s, by_id);
5745 tree_cnt++;
5746 if (!h2s->cs)
5747 orph_cnt++;
5748 node = eb32_next(node);
5749 }
5750
Willy Tarreau60f62682019-05-26 11:32:27 +02005751 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005752 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02005753 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau987c0632018-12-18 10:32:05 +01005754 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005755 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5756 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreauab2ec452019-08-30 07:07:08 +02005757 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau616ac812018-07-24 14:12:42 +02005758 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005759 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005760 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5761 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5762 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005763 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5764 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5765 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005766 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5767 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005768
5769 if (h2s) {
Willy Tarreauab2ec452019-08-30 07:07:08 +02005770 chunk_appendf(msg, " last_h2s=%p .id=%d .st=%s.flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5771 h2s, h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
Willy Tarreau987c0632018-12-18 10:32:05 +01005772 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5773 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5774 h2s->cs);
5775 if (h2s->cs)
5776 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5777 h2s->cs->flags, h2s->cs->data);
5778 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005779}
Willy Tarreau62f52692017-10-08 23:01:42 +02005780
5781/*******************************************************/
5782/* functions below are dedicated to the config parsers */
5783/*******************************************************/
5784
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005785/* config parser for global "tune.h2.header-table-size" */
5786static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5787 struct proxy *defpx, const char *file, int line,
5788 char **err)
5789{
5790 if (too_many_args(1, args, err, NULL))
5791 return -1;
5792
5793 h2_settings_header_table_size = atoi(args[1]);
5794 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5795 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5796 return -1;
5797 }
5798 return 0;
5799}
Willy Tarreau62f52692017-10-08 23:01:42 +02005800
Willy Tarreaue6baec02017-07-27 11:45:11 +02005801/* config parser for global "tune.h2.initial-window-size" */
5802static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5803 struct proxy *defpx, const char *file, int line,
5804 char **err)
5805{
5806 if (too_many_args(1, args, err, NULL))
5807 return -1;
5808
5809 h2_settings_initial_window_size = atoi(args[1]);
5810 if (h2_settings_initial_window_size < 0) {
5811 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5812 return -1;
5813 }
5814 return 0;
5815}
5816
Willy Tarreau5242ef82017-07-27 11:47:28 +02005817/* config parser for global "tune.h2.max-concurrent-streams" */
5818static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5819 struct proxy *defpx, const char *file, int line,
5820 char **err)
5821{
5822 if (too_many_args(1, args, err, NULL))
5823 return -1;
5824
5825 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005826 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005827 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5828 return -1;
5829 }
5830 return 0;
5831}
5832
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005833/* config parser for global "tune.h2.max-frame-size" */
5834static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5835 struct proxy *defpx, const char *file, int line,
5836 char **err)
5837{
5838 if (too_many_args(1, args, err, NULL))
5839 return -1;
5840
5841 h2_settings_max_frame_size = atoi(args[1]);
5842 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5843 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5844 return -1;
5845 }
5846 return 0;
5847}
5848
Willy Tarreau62f52692017-10-08 23:01:42 +02005849
5850/****************************************/
5851/* MUX initialization and instanciation */
5852/***************************************/
5853
5854/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005855static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005856 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005857 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005858 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005859 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005860 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005861 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005862 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005863 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005864 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005865 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005866 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005867 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005868 .shutr = h2_shutr,
5869 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005870 .show_fd = h2_show_fd,
Christopher Faulet9b79a102019-07-15 11:22:56 +02005871 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Willy Tarreau62f52692017-10-08 23:01:42 +02005872 .name = "H2",
5873};
5874
Christopher Faulet32f61c02018-04-10 14:33:41 +02005875static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02005876 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005877
Willy Tarreau0108d902018-11-25 19:14:37 +01005878INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5879
Willy Tarreau62f52692017-10-08 23:01:42 +02005880/* config keyword parsers */
5881static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005882 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005883 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005884 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005885 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005886 { 0, NULL, NULL }
5887}};
5888
Willy Tarreau0108d902018-11-25 19:14:37 +01005889INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);