blob: ec5fbcaaf463a7f2a6675eebef9b461343e7f7f4 [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>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#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 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
90 int32_t max_id; /* highest ID known on this connection, <0 before preface */
91 uint32_t rcvd_c; /* newly received data to ACK for the connection */
92 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
93
94 /* states for the demux direction */
95 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020096 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020097
98 int32_t dsi; /* demux stream ID (<0 = idle) */
99 int32_t dfl; /* demux frame length (if dsi >= 0) */
100 int8_t dft; /* demux frame type (if dsi >= 0) */
101 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100102 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
103 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200104 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
105
106 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200108 int32_t msi; /* mux stream ID (<0 = idle) */
109 int32_t mfl; /* mux frame length (if dsi >= 0) */
110 int8_t mft; /* mux frame type (if dsi >= 0) */
111 int8_t mff; /* mux frame flags (if dsi >= 0) */
112 /* 16 bit hole here */
113 int32_t miw; /* mux initial window size for all new streams */
114 int32_t mws; /* mux window size. Can be negative. */
115 int32_t mfs; /* mux's max frame size */
116
Willy Tarreauea392822017-10-31 10:02:25 +0100117 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100118 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100119 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200120 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100121 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100122 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200123 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100124 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125 struct eb_root streams_by_id; /* all active streams by their ID */
126 struct list send_list; /* list of blocked streams requesting to send */
127 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200128 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100129 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200130 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200131};
132
Willy Tarreau18312642017-10-11 07:57:07 +0200133/* H2 stream state, in h2s->st */
134enum h2_ss {
135 H2_SS_IDLE = 0, // idle
136 H2_SS_RLOC, // reserved(local)
137 H2_SS_RREM, // reserved(remote)
138 H2_SS_OPEN, // open
139 H2_SS_HREM, // half-closed(remote)
140 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200141 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200142 H2_SS_CLOSED, // closed
143 H2_SS_ENTRIES // must be last
144} __attribute__((packed));
145
146/* HTTP/2 stream flags (32 bit), in h2s->flags */
147#define H2_SF_NONE 0x00000000
148#define H2_SF_ES_RCVD 0x00000001
149#define H2_SF_ES_SENT 0x00000002
150
151#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
152#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
153
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200154/* stream flags indicating the reason the stream is blocked */
155#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
156#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
157#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
158#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
159#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
160
Willy Tarreau454f9052017-10-26 19:40:35 +0200161/* stream flags indicating how data is supposed to be sent */
162#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
163#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
164
165/* step we're currently in when sending chunks. This is needed because we may
166 * have to transfer chunks as large as a full buffer so there's no room left
167 * for size nor crlf around.
168 */
169#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
170#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
171#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
172
173#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
174
Willy Tarreau67434202017-11-06 20:20:51 +0100175#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100176#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100177
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100178#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
179
Willy Tarreau18312642017-10-11 07:57:07 +0200180/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
181 * it is being processed in the internal HTTP representation (H1 for now).
182 */
183struct h2s {
184 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100185 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200186 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200187 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200188 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 int32_t id; /* stream ID */
190 uint32_t flags; /* H2_SF_* */
191 int mws; /* mux window size for this stream */
192 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
193 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200194 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100195 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200196 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200197 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
198 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
199 struct wait_event *send_wait; /* The streeam is waiting for flow control */
200 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200201};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200202
Willy Tarreauc6405142017-09-21 20:23:50 +0200203/* descriptor for an h2 frame header */
204struct h2_fh {
205 uint32_t len; /* length, host order, 24 bits */
206 uint32_t sid; /* stream id, host order, 31 bits */
207 uint8_t ft; /* frame type */
208 uint8_t ff; /* frame flags */
209};
210
Willy Tarreau8ceae722018-11-26 11:58:30 +0100211/* the h2c connection pool */
212DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
213
214/* the h2s stream pool */
215DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
216
Willy Tarreaudc572362018-12-12 08:08:05 +0100217/* The default connection window size is 65535, it may only be enlarged using
218 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
219 * we'll pretend we already received the difference between the two to send
220 * an equivalent window update to enlarge it to 2G-1.
221 */
222#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
223
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200224/* a few settings from the global section */
225static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200226static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200227static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200228
Willy Tarreau2a856182017-05-16 15:20:39 +0200229/* a dmumy closed stream */
230static const struct h2s *h2_closed_stream = &(const struct h2s){
231 .cs = NULL,
232 .h2c = NULL,
233 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100234 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100235 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200236 .id = 0,
237};
238
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100239/* a dmumy closed stream returning a PROTOCOL_ERROR error */
240static const struct h2s *h2_error_stream = &(const struct h2s){
241 .cs = NULL,
242 .h2c = NULL,
243 .st = H2_SS_CLOSED,
244 .errcode = H2_ERR_PROTOCOL_ERROR,
245 .flags = 0,
246 .id = 0,
247};
248
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100249/* a dmumy closed stream returning a REFUSED_STREAM error */
250static const struct h2s *h2_refused_stream = &(const struct h2s){
251 .cs = NULL,
252 .h2c = NULL,
253 .st = H2_SS_CLOSED,
254 .errcode = H2_ERR_REFUSED_STREAM,
255 .flags = 0,
256 .id = 0,
257};
258
Willy Tarreau2a856182017-05-16 15:20:39 +0200259/* and a dummy idle stream for use with any unannounced stream */
260static const struct h2s *h2_idle_stream = &(const struct h2s){
261 .cs = NULL,
262 .h2c = NULL,
263 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100264 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200265 .id = 0,
266};
267
Olivier Houchard9f6af332018-05-25 14:04:04 +0200268static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200269static int h2_send(struct h2c *h2c);
270static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200271static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200272static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100273static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100274static 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 +0100275static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200276static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100277static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100278static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200279
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200280/*****************************************************/
281/* functions below are for dynamic buffer management */
282/*****************************************************/
283
Willy Tarreau315d8072017-12-10 22:17:57 +0100284/* indicates whether or not the we may call the h2_recv() function to attempt
285 * to receive data into the buffer and/or demux pending data. The condition is
286 * a bit complex due to some API limits for now. The rules are the following :
287 * - if an error or a shutdown was detected on the connection and the buffer
288 * is empty, we must not attempt to receive
289 * - if the demux buf failed to be allocated, we must not try to receive and
290 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100291 * - if no flag indicates a blocking condition, we may attempt to receive,
292 * regardless of whether the demux buffer is full or not, so that only
293 * de demux part decides whether or not to block. This is needed because
294 * the connection API indeed prevents us from re-enabling receipt that is
295 * already enabled in a polled state, so we must always immediately stop
296 * as soon as the demux can't proceed so as never to hit an end of read
297 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100298 * - otherwise must may not attempt
299 */
300static inline int h2_recv_allowed(const struct h2c *h2c)
301{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200302 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100303 (h2c->st0 >= H2_CS_ERROR ||
304 h2c->conn->flags & CO_FL_ERROR ||
305 conn_xprt_read0_pending(h2c->conn)))
306 return 0;
307
308 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100309 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100310 return 1;
311
312 return 0;
313}
314
Willy Tarreau47b515a2018-12-21 16:09:41 +0100315/* restarts reading on the connection if it was not enabled */
316static inline void h2c_restart_reading(const struct h2c *h2c)
317{
318 if (!h2_recv_allowed(h2c))
319 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100320 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100321 return;
322 tasklet_wakeup(h2c->wait_event.task);
323}
324
325
Willy Tarreauf2101912018-07-19 10:11:38 +0200326/* returns true if the connection has too many conn_streams attached */
327static inline int h2_has_too_many_cs(const struct h2c *h2c)
328{
Willy Tarreaua8754662018-12-23 20:43:58 +0100329 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200330}
331
Willy Tarreau44e973f2018-03-01 17:49:30 +0100332/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
333 * flags are used to figure what buffer was requested. It returns 1 if the
334 * allocation succeeds, in which case the connection is woken up, or 0 if it's
335 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200336 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100337static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200338{
339 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100340 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200341
Willy Tarreau44e973f2018-03-01 17:49:30 +0100342 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200343 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100344 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200345 return 1;
346 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200347
Willy Tarreau44e973f2018-03-01 17:49:30 +0100348 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
349 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200350
351 if (h2c->flags & H2_CF_DEM_MROOM) {
352 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100353 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200354 }
Willy Tarreau14398122017-09-22 14:26:04 +0200355 return 1;
356 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100357
358 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
359 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200360 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100361 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100362 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100363 return 1;
364 }
365
Willy Tarreau14398122017-09-22 14:26:04 +0200366 return 0;
367}
368
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200369static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200370{
371 struct buffer *buf = NULL;
372
Willy Tarreau44e973f2018-03-01 17:49:30 +0100373 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
374 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
375 h2c->buf_wait.target = h2c;
376 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100377 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100378 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100379 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200380 __conn_xprt_stop_recv(h2c->conn);
381 }
382 return buf;
383}
384
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200385static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200386{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200387 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100388 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200389 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200390 }
391}
392
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100393/* returns the number of allocatable outgoing streams for the connection taking
394 * the last_sid and the reserved ones into account.
395 */
396static inline int h2_streams_left(const struct h2c *h2c)
397{
398 int ret;
399
400 /* consider the number of outgoing streams we're allowed to create before
401 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
402 * nb_reserved is the number of streams which don't yet have an ID.
403 */
404 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
405 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
406 if (ret < 0)
407 ret = 0;
408 return ret;
409}
410
411/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100412static int h2_avail_streams(struct connection *conn)
413{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100414 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100415 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100416 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100417
Willy Tarreau6afec462019-01-28 06:40:19 +0100418 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
419 * streams on the connection.
420 */
421 if (h2c->last_sid >= 0)
422 return 0;
423
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100424 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100425 ret1 = h2_settings_max_concurrent_streams - h2c->nb_streams;
426
427 /* we must also consider the limit imposed by stream IDs */
428 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100429 ret1 = MIN(ret1, ret2);
430 if (ret1 && srv && srv->max_reuse >= 0) {
431 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
432 ret1 = MIN(ret1, ret2);
433 }
434 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100435}
436
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100437static int h2_max_streams(struct connection *conn)
438{
439 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
440 return h2_settings_max_concurrent_streams;
441}
442
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200443
Willy Tarreau62f52692017-10-08 23:01:42 +0200444/*****************************************************************/
445/* functions below are dedicated to the mux setup and management */
446/*****************************************************************/
447
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200448/* Initialize the mux once it's attached. For outgoing connections, the context
449 * is already initialized before installing the mux, so we detect incoming
450 * connections from the fact that the context is still NULL. Returns < 0 on
451 * error.
452 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100453static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200454{
455 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100456 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200457
Willy Tarreaubafbe012017-11-24 17:34:44 +0100458 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200459 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200460 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200461
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100462 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200463 h2c->flags = H2_CF_IS_BACK;
464 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
465 if (tick_isset(prx->timeout.serverfin))
466 h2c->shut_timeout = prx->timeout.serverfin;
467 } else {
468 h2c->flags = H2_CF_NONE;
469 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
470 if (tick_isset(prx->timeout.clientfin))
471 h2c->shut_timeout = prx->timeout.clientfin;
472 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100473
Willy Tarreau0b37d652018-10-03 10:33:02 +0200474 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100475 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100476 if (tick_isset(h2c->timeout)) {
477 t = task_new(tid_bit);
478 if (!t)
479 goto fail;
480
481 h2c->task = t;
482 t->process = h2_timeout_task;
483 t->context = h2c;
484 t->expire = tick_add(now_ms, h2c->timeout);
485 }
Willy Tarreauea392822017-10-31 10:02:25 +0100486
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200487 h2c->wait_event.task = tasklet_new();
488 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200489 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200490 h2c->wait_event.task->process = h2_io_cb;
491 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100492 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200493
Willy Tarreau32218eb2017-09-22 08:07:25 +0200494 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
495 if (!h2c->ddht)
496 goto fail;
497
498 /* Initialise the context. */
499 h2c->st0 = H2_CS_PREFACE;
500 h2c->conn = conn;
501 h2c->max_id = -1;
502 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100503 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200504 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100505 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200506 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100507 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100508 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200509
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200510 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200511 h2c->dsi = -1;
512 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100513
Willy Tarreau32218eb2017-09-22 08:07:25 +0200514 h2c->last_sid = -1;
515
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200516 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200517 h2c->miw = 65535; /* mux initial window size */
518 h2c->mws = 65535; /* mux window size */
519 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200520 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 LIST_INIT(&h2c->send_list);
522 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200523 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100524 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200525
Willy Tarreau3f133572017-10-31 19:21:06 +0100526 if (t)
527 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100528
Willy Tarreau01b44822018-10-03 14:26:37 +0200529 if (h2c->flags & H2_CF_IS_BACK) {
530 /* FIXME: this is temporary, for outgoing connections we need
531 * to immediately allocate a stream until the code is modified
532 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100533 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200534 */
535 struct h2s *h2s;
536
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100537 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200538 if (!h2s)
539 goto fail_stream;
540 }
541
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100542 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200543
Willy Tarreau0f383582018-10-03 14:22:21 +0200544 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100545 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200547 fail_stream:
548 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200549 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100550 if (t)
551 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200552 if (h2c->wait_event.task)
553 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100554 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200555 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200556 return -1;
557}
558
Willy Tarreau751f2d02018-10-05 09:35:00 +0200559/* returns the next allocatable outgoing stream ID for the H2 connection, or
560 * -1 if no more is allocatable.
561 */
562static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
563{
564 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100565
566 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200567 id = -1;
568 return id;
569}
570
Willy Tarreau2373acc2017-10-12 17:35:14 +0200571/* returns the stream associated with id <id> or NULL if not found */
572static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
573{
574 struct eb32_node *node;
575
Willy Tarreau751f2d02018-10-05 09:35:00 +0200576 if (id == 0)
577 return (struct h2s *)h2_closed_stream;
578
Willy Tarreau2a856182017-05-16 15:20:39 +0200579 if (id > h2c->max_id)
580 return (struct h2s *)h2_idle_stream;
581
Willy Tarreau2373acc2017-10-12 17:35:14 +0200582 node = eb32_lookup(&h2c->streams_by_id, id);
583 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200584 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200585
586 return container_of(node, struct h2s, by_id);
587}
588
Willy Tarreau62f52692017-10-08 23:01:42 +0200589/* release function for a connection. This one should be called to free all
590 * resources allocated to the mux.
591 */
592static void h2_release(struct connection *conn)
593{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100594 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200595
596 LIST_DEL(&conn->list);
597
598 if (h2c) {
599 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200600
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100601 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100602 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100603 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200604
Willy Tarreau44e973f2018-03-01 17:49:30 +0100605 h2_release_buf(h2c, &h2c->dbuf);
606 h2_release_buf(h2c, &h2c->mbuf);
607
Willy Tarreauea392822017-10-31 10:02:25 +0100608 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200609 h2c->task->context = NULL;
610 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100611 h2c->task = NULL;
612 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200613 if (h2c->wait_event.task)
614 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100615 if (h2c->wait_event.events != 0)
616 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200617 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100618
Willy Tarreaubafbe012017-11-24 17:34:44 +0100619 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200620 }
621
622 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100623 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624
625 conn_stop_tracking(conn);
626 conn_full_close(conn);
627 if (conn->destroy_cb)
628 conn->destroy_cb(conn);
629 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200630}
631
632
Willy Tarreau71681172017-10-23 14:39:06 +0200633/******************************************************/
634/* functions below are for the H2 protocol processing */
635/******************************************************/
636
637/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100638static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200639{
640 return h2s ? h2s->id : 0;
641}
642
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200643/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100644static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200645{
646 if (h2c->msi < 0)
647 return 0;
648
649 if (h2c->msi == h2s_id(h2s))
650 return 0;
651
652 return 1;
653}
654
Willy Tarreau741d6df2017-10-17 08:00:59 +0200655/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100656static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200657{
658 h2c->errcode = err;
659 h2c->st0 = H2_CS_ERROR;
660}
661
Willy Tarreau175cebb2019-01-24 10:02:24 +0100662/* marks an error on the stream. It may also update an already closed stream
663 * (e.g. to report an error after an RST was received).
664 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100665static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200666{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100667 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200668 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100669 if (h2s->st < H2_SS_ERROR)
670 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100671 if (h2s->cs)
672 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200673 }
674}
675
Willy Tarreau7e094452018-12-19 18:08:52 +0100676/* attempt to notify the data layer of recv availability */
677static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
678{
679 struct wait_event *sw;
680
681 if (h2s->recv_wait) {
682 sw = h2s->recv_wait;
683 sw->events &= ~SUB_RETRY_RECV;
684 tasklet_wakeup(sw->task);
685 h2s->recv_wait = NULL;
686 }
687}
688
689/* attempt to notify the data layer of send availability */
690static void __maybe_unused h2s_notify_send(struct h2s *h2s)
691{
692 struct wait_event *sw;
693
694 if (h2s->send_wait) {
695 sw = h2s->send_wait;
696 sw->events &= ~SUB_RETRY_SEND;
697 tasklet_wakeup(sw->task);
698 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100699 LIST_DEL(&h2s->list);
700 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100701 }
702}
703
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100704/* alerts the data layer, trying to wake it up by all means, following
705 * this sequence :
706 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
707 * - if its subscribed to send, then it's woken up for send
708 * - if it was subscribed to neither, its ->wake() callback is called
709 * It is safe to call this function with a closed stream which doesn't have a
710 * conn_stream anymore.
711 */
712static void __maybe_unused h2s_alert(struct h2s *h2s)
713{
714 if (h2s->recv_wait || h2s->send_wait) {
715 h2s_notify_recv(h2s);
716 h2s_notify_send(h2s);
717 }
718 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
719 h2s->cs->data_cb->wake(h2s->cs);
720}
721
Willy Tarreaue4820742017-07-27 13:37:23 +0200722/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100723static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200724{
725 uint8_t *out = frame;
726
727 *out = len >> 16;
728 write_n16(out + 1, len);
729}
730
Willy Tarreau54c15062017-10-10 17:10:03 +0200731/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
732 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
733 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200734 * available in the buffer's input prior to calling this function. The buffer
735 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200736 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100737static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200738 const struct buffer *b, int o)
739{
Willy Tarreau591d4452018-06-15 17:21:00 +0200740 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 +0200741}
742
Willy Tarreau1f094672017-11-20 21:27:45 +0100743static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200744{
Willy Tarreau591d4452018-06-15 17:21:00 +0200745 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200746}
747
Willy Tarreau1f094672017-11-20 21:27:45 +0100748static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200749{
Willy Tarreau591d4452018-06-15 17:21:00 +0200750 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200751}
752
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200754{
Willy Tarreau591d4452018-06-15 17:21:00 +0200755 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200756}
757
758
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100759/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
760 * The algorithm is not obvious. It turns out that H2 headers are neither
761 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
762 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200763 *
764 * b0 b1 b2 b3 b4 b5..b8
765 * +----------+---------+--------+----+----+----------------------+
766 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
767 * +----------+---------+--------+----+----+----------------------+
768 *
769 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
770 * we get the sid properly aligned and ordered, and 16 bits of len properly
771 * ordered as well. The type and flags can be extracted using bit shifts from
772 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200773 * Returns zero if some bytes are missing, otherwise non-zero on success. The
774 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200775 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100776static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200777{
778 uint64_t w;
779
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100780 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200781 return 0;
782
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100783 w = h2_get_n64(b, o + 1);
784 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200785 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
786 h->ff = w >> 32;
787 h->ft = w >> 40;
788 h->len += w >> 48;
789 return 1;
790}
791
792/* skip the next 9 bytes corresponding to the frame header possibly parsed by
793 * h2_peek_frame_hdr() above.
794 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100795static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200796{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200797 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200798}
799
800/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100801static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200802{
803 int ret;
804
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200806 if (ret > 0)
807 h2_skip_frame_hdr(b);
808 return ret;
809}
810
Willy Tarreau00dd0782018-03-01 16:31:34 +0100811/* marks stream <h2s> as CLOSED and decrement the number of active streams for
812 * its connection if the stream was not yet closed. Please use this exclusively
813 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100814 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100815static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100816{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100817 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100818 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100819 if (!h2s->id)
820 h2s->h2c->nb_reserved--;
821 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100822 h2s->st = H2_SS_CLOSED;
823}
824
Willy Tarreau71049cc2018-03-28 13:56:39 +0200825/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
826static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100827{
828 h2s_close(h2s);
829 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200830 if (b_size(&h2s->rxbuf)) {
831 b_free(&h2s->rxbuf);
832 offer_buffers(NULL, tasks_run_queue);
833 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200834 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100835 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200836 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100837 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800838 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200839 * reference left would be in the h2c send_list/fctl_list, and if
840 * we're in it, we're getting out anyway
841 */
842 LIST_DEL(&h2s->list);
843 LIST_INIT(&h2s->list);
844 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100845 pool_free(pool_head_h2s, h2s);
846}
847
Willy Tarreaua8e49542018-10-03 18:53:55 +0200848/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
849 * stream tree. In case of error, nothing is added and NULL is returned. The
850 * causes of errors can be any failed memory allocation. The caller is
851 * responsible for checking if the connection may support an extra stream
852 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200853 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200854static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200855{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200856 struct h2s *h2s;
857
Willy Tarreaubafbe012017-11-24 17:34:44 +0100858 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200859 if (!h2s)
860 goto out;
861
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200862 h2s->wait_event.task = tasklet_new();
863 if (!h2s->wait_event.task) {
864 pool_free(pool_head_h2s, h2s);
865 goto out;
866 }
867 h2s->send_wait = NULL;
868 h2s->recv_wait = NULL;
869 h2s->wait_event.task->process = h2_deferred_shut;
870 h2s->wait_event.task->context = h2s;
871 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100872 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200873 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200874 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200875 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200876 h2s->mws = h2c->miw;
877 h2s->flags = H2_SF_NONE;
878 h2s->errcode = H2_ERR_NO_ERROR;
879 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200880 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100881 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200882 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200883
884 if (h2c->flags & H2_CF_IS_BACK) {
885 h1m_init_req(&h2s->h1m);
886 h2s->h1m.err_pos = -1; // don't care about errors on the request path
887 h2s->h1m.flags |= H1_MF_TOLOWER;
888 } else {
889 h1m_init_res(&h2s->h1m);
890 h2s->h1m.err_pos = -1; // don't care about errors on the response path
891 h2s->h1m.flags |= H1_MF_TOLOWER;
892 }
893
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200895 if (id > 0)
896 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100897 else
898 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200899
900 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100901 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100902 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200903
904 return h2s;
905
906 out_free_h2s:
907 pool_free(pool_head_h2s, h2s);
908 out:
909 return NULL;
910}
911
912/* creates a new stream <id> on the h2c connection and returns it, or NULL in
913 * case of memory allocation error.
914 */
915static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
916{
917 struct session *sess = h2c->conn->owner;
918 struct conn_stream *cs;
919 struct h2s *h2s;
920
921 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
922 goto out;
923
924 h2s = h2s_new(h2c, id);
925 if (!h2s)
926 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200927
928 cs = cs_new(h2c->conn);
929 if (!cs)
930 goto out_close;
931
Olivier Houchard746fb772018-12-15 19:42:00 +0100932 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933 h2s->cs = cs;
934 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200935 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200936
937 if (stream_create_from_cs(cs) < 0)
938 goto out_free_cs;
939
Willy Tarreau590a0512018-09-05 11:56:48 +0200940 /* We want the accept date presented to the next stream to be the one
941 * we have now, the handshake time to be null (since the next stream
942 * is not delayed by a handshake), and the idle time to count since
943 * right now.
944 */
945 sess->accept_date = date;
946 sess->tv_accept = now;
947 sess->t_handshake = 0;
948
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200949 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200950 if (h2_has_too_many_cs(h2c))
951 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200952 return h2s;
953
954 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200955 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200956 cs_free(cs);
957 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200958 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200959 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200960 sess_log(sess);
961 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200962}
963
Willy Tarreau751f2d02018-10-05 09:35:00 +0200964/* allocates a new stream associated to conn_stream <cs> on the h2c connection
965 * and returns it, or NULL in case of memory allocation error or if the highest
966 * possible stream ID was reached.
967 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100968static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200969{
970 struct h2s *h2s = NULL;
971
972 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
973 goto out;
974
Willy Tarreaua80dca82019-01-24 17:08:28 +0100975 if (h2_streams_left(h2c) < 1)
976 goto out;
977
Willy Tarreau751f2d02018-10-05 09:35:00 +0200978 /* Defer choosing the ID until we send the first message to create the stream */
979 h2s = h2s_new(h2c, 0);
980 if (!h2s)
981 goto out;
982
983 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100984 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200985 cs->ctx = h2s;
986 h2c->nb_cs++;
987
Willy Tarreau751f2d02018-10-05 09:35:00 +0200988 out:
989 return h2s;
990}
991
Willy Tarreaube5b7152017-09-25 16:25:39 +0200992/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
993 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
994 * the various settings codes.
995 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200996static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200997{
998 struct buffer *res;
999 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001000 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001001 int ret;
1002
1003 if (h2c_mux_busy(h2c, NULL)) {
1004 h2c->flags |= H2_CF_DEM_MBUSY;
1005 return 0;
1006 }
1007
Willy Tarreau44e973f2018-03-01 17:49:30 +01001008 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001009 if (!res) {
1010 h2c->flags |= H2_CF_MUX_MALLOC;
1011 h2c->flags |= H2_CF_DEM_MROOM;
1012 return 0;
1013 }
1014
1015 chunk_init(&buf, buf_data, sizeof(buf_data));
1016 chunk_memcpy(&buf,
1017 "\x00\x00\x00" /* length : 0 for now */
1018 "\x04\x00" /* type : 4 (settings), flags : 0 */
1019 "\x00\x00\x00\x00", /* stream ID : 0 */
1020 9);
1021
1022 if (h2_settings_header_table_size != 4096) {
1023 char str[6] = "\x00\x01"; /* header_table_size */
1024
1025 write_n32(str + 2, h2_settings_header_table_size);
1026 chunk_memcat(&buf, str, 6);
1027 }
1028
1029 if (h2_settings_initial_window_size != 65535) {
1030 char str[6] = "\x00\x04"; /* initial_window_size */
1031
1032 write_n32(str + 2, h2_settings_initial_window_size);
1033 chunk_memcat(&buf, str, 6);
1034 }
1035
1036 if (h2_settings_max_concurrent_streams != 0) {
1037 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1038
1039 /* Note: 0 means "unlimited" for haproxy's config but not for
1040 * the protocol, so never send this value!
1041 */
1042 write_n32(str + 2, h2_settings_max_concurrent_streams);
1043 chunk_memcat(&buf, str, 6);
1044 }
1045
1046 if (global.tune.bufsize != 16384) {
1047 char str[6] = "\x00\x05"; /* max_frame_size */
1048
1049 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1050 * match bufsize - rewrite size, but at the moment it seems
1051 * that clients don't take care of it.
1052 */
1053 write_n32(str + 2, global.tune.bufsize);
1054 chunk_memcat(&buf, str, 6);
1055 }
1056
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001057 h2_set_frame_size(buf.area, buf.data - 9);
1058 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001059 if (unlikely(ret <= 0)) {
1060 if (!ret) {
1061 h2c->flags |= H2_CF_MUX_MFULL;
1062 h2c->flags |= H2_CF_DEM_MROOM;
1063 return 0;
1064 }
1065 else {
1066 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1067 return 0;
1068 }
1069 }
1070 return ret;
1071}
1072
Willy Tarreau52eed752017-09-22 15:05:09 +02001073/* Try to receive a connection preface, then upon success try to send our
1074 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1075 * missing data. It may return an error in h2c.
1076 */
1077static int h2c_frt_recv_preface(struct h2c *h2c)
1078{
1079 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001080 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001081
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001082 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001083
1084 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001085 if (ret1 < 0)
1086 sess_log(h2c->conn->owner);
1087
Willy Tarreau52eed752017-09-22 15:05:09 +02001088 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1089 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1090 return 0;
1091 }
1092
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001093 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001094 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001095 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001096
Willy Tarreaube5b7152017-09-25 16:25:39 +02001097 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001098}
1099
Willy Tarreau01b44822018-10-03 14:26:37 +02001100/* Try to send a connection preface, then upon success try to send our
1101 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1102 * missing data. It may return an error in h2c.
1103 */
1104static int h2c_bck_send_preface(struct h2c *h2c)
1105{
1106 struct buffer *res;
1107
1108 if (h2c_mux_busy(h2c, NULL)) {
1109 h2c->flags |= H2_CF_DEM_MBUSY;
1110 return 0;
1111 }
1112
1113 res = h2_get_buf(h2c, &h2c->mbuf);
1114 if (!res) {
1115 h2c->flags |= H2_CF_MUX_MALLOC;
1116 h2c->flags |= H2_CF_DEM_MROOM;
1117 return 0;
1118 }
1119
1120 if (!b_data(res)) {
1121 /* preface not yet sent */
1122 b_istput(res, ist(H2_CONN_PREFACE));
1123 }
1124
1125 return h2c_send_settings(h2c);
1126}
1127
Willy Tarreau081d4722017-05-16 21:51:05 +02001128/* try to send a GOAWAY frame on the connection to report an error or a graceful
1129 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1130 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1131 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1132 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1133 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1134 * on unrecoverable failure. It will not attempt to send one again in this last
1135 * case so that it is safe to use h2c_error() to report such errors.
1136 */
1137static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1138{
1139 struct buffer *res;
1140 char str[17];
1141 int ret;
1142
1143 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1144 return 1; // claim that it worked
1145
1146 if (h2c_mux_busy(h2c, h2s)) {
1147 if (h2s)
1148 h2s->flags |= H2_SF_BLK_MBUSY;
1149 else
1150 h2c->flags |= H2_CF_DEM_MBUSY;
1151 return 0;
1152 }
1153
Willy Tarreau44e973f2018-03-01 17:49:30 +01001154 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001155 if (!res) {
1156 h2c->flags |= H2_CF_MUX_MALLOC;
1157 if (h2s)
1158 h2s->flags |= H2_SF_BLK_MROOM;
1159 else
1160 h2c->flags |= H2_CF_DEM_MROOM;
1161 return 0;
1162 }
1163
1164 /* len: 8, type: 7, flags: none, sid: 0 */
1165 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1166
1167 if (h2c->last_sid < 0)
1168 h2c->last_sid = h2c->max_id;
1169
1170 write_n32(str + 9, h2c->last_sid);
1171 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001172 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001173 if (unlikely(ret <= 0)) {
1174 if (!ret) {
1175 h2c->flags |= H2_CF_MUX_MFULL;
1176 if (h2s)
1177 h2s->flags |= H2_SF_BLK_MROOM;
1178 else
1179 h2c->flags |= H2_CF_DEM_MROOM;
1180 return 0;
1181 }
1182 else {
1183 /* we cannot report this error using GOAWAY, so we mark
1184 * it and claim a success.
1185 */
1186 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1187 h2c->flags |= H2_CF_GOAWAY_FAILED;
1188 return 1;
1189 }
1190 }
1191 h2c->flags |= H2_CF_GOAWAY_SENT;
1192 return ret;
1193}
1194
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001195/* Try to send an RST_STREAM frame on the connection for the indicated stream
1196 * during mux operations. This stream must be valid and cannot be closed
1197 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1198 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1199 * not yet.
1200 *
1201 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1202 * to write the message, it subscribes the stream to future notifications.
1203 */
1204static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1205{
1206 struct buffer *res;
1207 char str[13];
1208 int ret;
1209
1210 if (!h2s || h2s->st == H2_SS_CLOSED)
1211 return 1;
1212
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001213 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1214 * RST_STREAM in response to a RST_STREAM frame.
1215 */
1216 if (h2c->dft == H2_FT_RST_STREAM) {
1217 ret = 1;
1218 goto ignore;
1219 }
1220
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001221 if (h2c_mux_busy(h2c, h2s)) {
1222 h2s->flags |= H2_SF_BLK_MBUSY;
1223 return 0;
1224 }
1225
Willy Tarreau44e973f2018-03-01 17:49:30 +01001226 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001227 if (!res) {
1228 h2c->flags |= H2_CF_MUX_MALLOC;
1229 h2s->flags |= H2_SF_BLK_MROOM;
1230 return 0;
1231 }
1232
1233 /* len: 4, type: 3, flags: none */
1234 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1235 write_n32(str + 5, h2s->id);
1236 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001237 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001238
1239 if (unlikely(ret <= 0)) {
1240 if (!ret) {
1241 h2c->flags |= H2_CF_MUX_MFULL;
1242 h2s->flags |= H2_SF_BLK_MROOM;
1243 return 0;
1244 }
1245 else {
1246 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1247 return 0;
1248 }
1249 }
1250
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001251 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001252 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001253 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001254 return ret;
1255}
1256
1257/* Try to send an RST_STREAM frame on the connection for the stream being
1258 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001259 * error code, even if the stream is one of the dummy ones, and will update
1260 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001261 *
1262 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1263 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001264 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001265 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001266 */
1267static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1268{
1269 struct buffer *res;
1270 char str[13];
1271 int ret;
1272
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001273 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1274 * RST_STREAM in response to a RST_STREAM frame.
1275 */
1276 if (h2c->dft == H2_FT_RST_STREAM) {
1277 ret = 1;
1278 goto ignore;
1279 }
1280
Willy Tarreau27a84c92017-10-17 08:10:17 +02001281 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001282 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001283 return 0;
1284 }
1285
Willy Tarreau44e973f2018-03-01 17:49:30 +01001286 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001287 if (!res) {
1288 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001289 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001290 return 0;
1291 }
1292
1293 /* len: 4, type: 3, flags: none */
1294 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001295
Willy Tarreau27a84c92017-10-17 08:10:17 +02001296 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001297 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001298 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001299
Willy Tarreau27a84c92017-10-17 08:10:17 +02001300 if (unlikely(ret <= 0)) {
1301 if (!ret) {
1302 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001304 return 0;
1305 }
1306 else {
1307 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1308 return 0;
1309 }
1310 }
1311
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001312 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001313 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001314 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001315 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001316 }
1317
Willy Tarreau27a84c92017-10-17 08:10:17 +02001318 return ret;
1319}
1320
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001321/* try to send an empty DATA frame with the ES flag set to notify about the
1322 * end of stream and match a shutdown(write). If an ES was already sent as
1323 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1324 * on success or zero if nothing was done. In case of lack of room to write the
1325 * message, it subscribes the requesting stream to future notifications.
1326 */
1327static int h2_send_empty_data_es(struct h2s *h2s)
1328{
1329 struct h2c *h2c = h2s->h2c;
1330 struct buffer *res;
1331 char str[9];
1332 int ret;
1333
Willy Tarreau721c9742017-11-07 11:05:42 +01001334 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001335 return 1;
1336
1337 if (h2c_mux_busy(h2c, h2s)) {
1338 h2s->flags |= H2_SF_BLK_MBUSY;
1339 return 0;
1340 }
1341
Willy Tarreau44e973f2018-03-01 17:49:30 +01001342 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001343 if (!res) {
1344 h2c->flags |= H2_CF_MUX_MALLOC;
1345 h2s->flags |= H2_SF_BLK_MROOM;
1346 return 0;
1347 }
1348
1349 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1350 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1351 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001352 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001353 if (likely(ret > 0)) {
1354 h2s->flags |= H2_SF_ES_SENT;
1355 }
1356 else if (!ret) {
1357 h2c->flags |= H2_CF_MUX_MFULL;
1358 h2s->flags |= H2_SF_BLK_MROOM;
1359 return 0;
1360 }
1361 else {
1362 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1363 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001364 }
1365 return ret;
1366}
1367
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001368/* wake the streams attached to the connection, whose id is greater than <last>,
1369 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001370 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1371 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001372 */
1373static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1374{
1375 struct eb32_node *node;
1376 struct h2s *h2s;
1377
1378 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001379 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001380
1381 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001382 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001383
1384 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1385 while (node) {
1386 h2s = container_of(node, struct h2s, by_id);
1387 if (h2s->id <= last)
1388 break;
1389 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001390
1391 if (!h2s->cs) {
1392 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001393 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001394 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001395 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001396
1397 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001398 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1399 h2s->cs->flags |= CS_FL_ERROR;
1400
Willy Tarreauf830f012018-12-19 17:44:55 +01001401 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001402
Willy Tarreaua8519352018-12-18 16:44:28 +01001403 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001404 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001405 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001406 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001407 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001408 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001409 }
1410}
1411
Willy Tarreau3421aba2017-07-27 15:41:03 +02001412/* Increase all streams' outgoing window size by the difference passed in
1413 * argument. This is needed upon receipt of the settings frame if the initial
1414 * window size is different. The difference may be negative and the resulting
1415 * window size as well, for the time it takes to receive some window updates.
1416 */
1417static void h2c_update_all_ws(struct h2c *h2c, int diff)
1418{
1419 struct h2s *h2s;
1420 struct eb32_node *node;
1421
1422 if (!diff)
1423 return;
1424
1425 node = eb32_first(&h2c->streams_by_id);
1426 while (node) {
1427 h2s = container_of(node, struct h2s, by_id);
1428 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001429
1430 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1431 h2s->flags &= ~H2_SF_BLK_SFCTL;
1432 if (h2s->send_wait)
1433 LIST_ADDQ(&h2c->send_list, &h2s->list);
1434
1435 }
1436
Willy Tarreau3421aba2017-07-27 15:41:03 +02001437 node = eb32_next(node);
1438 }
1439}
1440
1441/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1442 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1443 * return an error in h2c. Described in RFC7540#6.5.
1444 */
1445static int h2c_handle_settings(struct h2c *h2c)
1446{
1447 unsigned int offset;
1448 int error;
1449
1450 if (h2c->dff & H2_F_SETTINGS_ACK) {
1451 if (h2c->dfl) {
1452 error = H2_ERR_FRAME_SIZE_ERROR;
1453 goto fail;
1454 }
1455 return 1;
1456 }
1457
1458 if (h2c->dsi != 0) {
1459 error = H2_ERR_PROTOCOL_ERROR;
1460 goto fail;
1461 }
1462
1463 if (h2c->dfl % 6) {
1464 error = H2_ERR_FRAME_SIZE_ERROR;
1465 goto fail;
1466 }
1467
1468 /* that's the limit we can process */
1469 if (h2c->dfl > global.tune.bufsize) {
1470 error = H2_ERR_FRAME_SIZE_ERROR;
1471 goto fail;
1472 }
1473
1474 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001475 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001476 return 0;
1477
1478 /* parse the frame */
1479 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001480 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1481 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001482
1483 switch (type) {
1484 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1485 /* we need to update all existing streams with the
1486 * difference from the previous iws.
1487 */
1488 if (arg < 0) { // RFC7540#6.5.2
1489 error = H2_ERR_FLOW_CONTROL_ERROR;
1490 goto fail;
1491 }
1492 h2c_update_all_ws(h2c, arg - h2c->miw);
1493 h2c->miw = arg;
1494 break;
1495 case H2_SETTINGS_MAX_FRAME_SIZE:
1496 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1497 error = H2_ERR_PROTOCOL_ERROR;
1498 goto fail;
1499 }
1500 h2c->mfs = arg;
1501 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001502 case H2_SETTINGS_ENABLE_PUSH:
1503 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1504 error = H2_ERR_PROTOCOL_ERROR;
1505 goto fail;
1506 }
1507 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001508 }
1509 }
1510
1511 /* need to ACK this frame now */
1512 h2c->st0 = H2_CS_FRAME_A;
1513 return 1;
1514 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001515 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001516 h2c_error(h2c, error);
1517 return 0;
1518}
1519
1520/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1521 * success or one of the h2_status values.
1522 */
1523static int h2c_ack_settings(struct h2c *h2c)
1524{
1525 struct buffer *res;
1526 char str[9];
1527 int ret = -1;
1528
1529 if (h2c_mux_busy(h2c, NULL)) {
1530 h2c->flags |= H2_CF_DEM_MBUSY;
1531 return 0;
1532 }
1533
Willy Tarreau44e973f2018-03-01 17:49:30 +01001534 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001535 if (!res) {
1536 h2c->flags |= H2_CF_MUX_MALLOC;
1537 h2c->flags |= H2_CF_DEM_MROOM;
1538 return 0;
1539 }
1540
1541 memcpy(str,
1542 "\x00\x00\x00" /* length : 0 (no data) */
1543 "\x04" "\x01" /* type : 4, flags : ACK */
1544 "\x00\x00\x00\x00" /* stream ID */, 9);
1545
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001546 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001547 if (unlikely(ret <= 0)) {
1548 if (!ret) {
1549 h2c->flags |= H2_CF_MUX_MFULL;
1550 h2c->flags |= H2_CF_DEM_MROOM;
1551 return 0;
1552 }
1553 else {
1554 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1555 return 0;
1556 }
1557 }
1558 return ret;
1559}
1560
Willy Tarreaucf68c782017-10-10 17:11:41 +02001561/* processes a PING frame and schedules an ACK if needed. The caller must pass
1562 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1563 * missing data. It may return an error in h2c.
1564 */
1565static int h2c_handle_ping(struct h2c *h2c)
1566{
1567 /* frame length must be exactly 8 */
1568 if (h2c->dfl != 8) {
1569 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1570 return 0;
1571 }
1572
1573 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001574 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001575 h2c->st0 = H2_CS_FRAME_A;
1576 return 1;
1577}
1578
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001579/* Try to send a window update for stream id <sid> and value <increment>.
1580 * Returns > 0 on success or zero on missing room or failure. It may return an
1581 * error in h2c.
1582 */
1583static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1584{
1585 struct buffer *res;
1586 char str[13];
1587 int ret = -1;
1588
1589 if (h2c_mux_busy(h2c, NULL)) {
1590 h2c->flags |= H2_CF_DEM_MBUSY;
1591 return 0;
1592 }
1593
Willy Tarreau44e973f2018-03-01 17:49:30 +01001594 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001595 if (!res) {
1596 h2c->flags |= H2_CF_MUX_MALLOC;
1597 h2c->flags |= H2_CF_DEM_MROOM;
1598 return 0;
1599 }
1600
1601 /* length: 4, type: 8, flags: none */
1602 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1603 write_n32(str + 5, sid);
1604 write_n32(str + 9, increment);
1605
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001606 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001607
1608 if (unlikely(ret <= 0)) {
1609 if (!ret) {
1610 h2c->flags |= H2_CF_MUX_MFULL;
1611 h2c->flags |= H2_CF_DEM_MROOM;
1612 return 0;
1613 }
1614 else {
1615 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1616 return 0;
1617 }
1618 }
1619 return ret;
1620}
1621
1622/* try to send pending window update for the connection. It's safe to call it
1623 * with no pending updates. Returns > 0 on success or zero on missing room or
1624 * failure. It may return an error in h2c.
1625 */
1626static int h2c_send_conn_wu(struct h2c *h2c)
1627{
1628 int ret = 1;
1629
1630 if (h2c->rcvd_c <= 0)
1631 return 1;
1632
Willy Tarreau97aaa672018-12-23 09:49:04 +01001633 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1634 /* increase the advertised connection window to 2G on
1635 * first update.
1636 */
1637 h2c->flags |= H2_CF_WINDOW_OPENED;
1638 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1639 }
1640
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001641 /* send WU for the connection */
1642 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1643 if (ret > 0)
1644 h2c->rcvd_c = 0;
1645
1646 return ret;
1647}
1648
1649/* try to send pending window update for the current dmux stream. It's safe to
1650 * call it with no pending updates. Returns > 0 on success or zero on missing
1651 * room or failure. It may return an error in h2c.
1652 */
1653static int h2c_send_strm_wu(struct h2c *h2c)
1654{
1655 int ret = 1;
1656
1657 if (h2c->rcvd_s <= 0)
1658 return 1;
1659
1660 /* send WU for the stream */
1661 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1662 if (ret > 0)
1663 h2c->rcvd_s = 0;
1664
1665 return ret;
1666}
1667
Willy Tarreaucf68c782017-10-10 17:11:41 +02001668/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1669 * success, 0 on missing data or one of the h2_status values.
1670 */
1671static int h2c_ack_ping(struct h2c *h2c)
1672{
1673 struct buffer *res;
1674 char str[17];
1675 int ret = -1;
1676
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001677 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001678 return 0;
1679
1680 if (h2c_mux_busy(h2c, NULL)) {
1681 h2c->flags |= H2_CF_DEM_MBUSY;
1682 return 0;
1683 }
1684
Willy Tarreau44e973f2018-03-01 17:49:30 +01001685 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001686 if (!res) {
1687 h2c->flags |= H2_CF_MUX_MALLOC;
1688 h2c->flags |= H2_CF_DEM_MROOM;
1689 return 0;
1690 }
1691
1692 memcpy(str,
1693 "\x00\x00\x08" /* length : 8 (same payload) */
1694 "\x06" "\x01" /* type : 6, flags : ACK */
1695 "\x00\x00\x00\x00" /* stream ID */, 9);
1696
1697 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001698 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001699
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001700 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001701 if (unlikely(ret <= 0)) {
1702 if (!ret) {
1703 h2c->flags |= H2_CF_MUX_MFULL;
1704 h2c->flags |= H2_CF_DEM_MROOM;
1705 return 0;
1706 }
1707 else {
1708 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1709 return 0;
1710 }
1711 }
1712 return ret;
1713}
1714
Willy Tarreau26f95952017-07-27 17:18:30 +02001715/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1716 * Returns > 0 on success or zero on missing data. It may return an error in
1717 * h2c or h2s. Described in RFC7540#6.9.
1718 */
1719static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1720{
1721 int32_t inc;
1722 int error;
1723
1724 if (h2c->dfl != 4) {
1725 error = H2_ERR_FRAME_SIZE_ERROR;
1726 goto conn_err;
1727 }
1728
1729 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001730 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001731 return 0;
1732
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001734
1735 if (h2c->dsi != 0) {
1736 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001737
1738 /* it's not an error to receive WU on a closed stream */
1739 if (h2s->st == H2_SS_CLOSED)
1740 return 1;
1741
1742 if (!inc) {
1743 error = H2_ERR_PROTOCOL_ERROR;
1744 goto strm_err;
1745 }
1746
1747 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1748 error = H2_ERR_FLOW_CONTROL_ERROR;
1749 goto strm_err;
1750 }
1751
1752 h2s->mws += inc;
1753 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1754 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001755 if (h2s->send_wait)
1756 LIST_ADDQ(&h2c->send_list, &h2s->list);
1757
Willy Tarreau26f95952017-07-27 17:18:30 +02001758 }
1759 }
1760 else {
1761 /* connection window update */
1762 if (!inc) {
1763 error = H2_ERR_PROTOCOL_ERROR;
1764 goto conn_err;
1765 }
1766
1767 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1768 error = H2_ERR_FLOW_CONTROL_ERROR;
1769 goto conn_err;
1770 }
1771
1772 h2c->mws += inc;
1773 }
1774
1775 return 1;
1776
1777 conn_err:
1778 h2c_error(h2c, error);
1779 return 0;
1780
1781 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001782 h2s_error(h2s, error);
1783 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001784 return 0;
1785}
1786
Willy Tarreaue96b0922017-10-30 00:28:29 +01001787/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1788 * the last ID. Returns > 0 on success or zero on missing data. It may return
1789 * an error in h2c. Described in RFC7540#6.8.
1790 */
1791static int h2c_handle_goaway(struct h2c *h2c)
1792{
1793 int error;
1794 int last;
1795
1796 if (h2c->dsi != 0) {
1797 error = H2_ERR_PROTOCOL_ERROR;
1798 goto conn_err;
1799 }
1800
1801 if (h2c->dfl < 8) {
1802 error = H2_ERR_FRAME_SIZE_ERROR;
1803 goto conn_err;
1804 }
1805
1806 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001807 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001808 return 0;
1809
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001810 last = h2_get_n32(&h2c->dbuf, 0);
1811 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001812 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001813 if (h2c->last_sid < 0)
1814 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001815 return 1;
1816
1817 conn_err:
1818 h2c_error(h2c, error);
1819 return 0;
1820}
1821
Willy Tarreau92153fc2017-12-03 19:46:19 +01001822/* processes a PRIORITY frame, and either skips it or rejects if it is
1823 * invalid. Returns > 0 on success or zero on missing data. It may return
1824 * an error in h2c. Described in RFC7540#6.3.
1825 */
1826static int h2c_handle_priority(struct h2c *h2c)
1827{
1828 int error;
1829
1830 if (h2c->dsi == 0) {
1831 error = H2_ERR_PROTOCOL_ERROR;
1832 goto conn_err;
1833 }
1834
1835 if (h2c->dfl != 5) {
1836 error = H2_ERR_FRAME_SIZE_ERROR;
1837 goto conn_err;
1838 }
1839
1840 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001841 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001842 return 0;
1843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001845 /* 7540#5.3 : can't depend on itself */
1846 error = H2_ERR_PROTOCOL_ERROR;
1847 goto conn_err;
1848 }
1849 return 1;
1850
1851 conn_err:
1852 h2c_error(h2c, error);
1853 return 0;
1854}
1855
Willy Tarreaucd234e92017-08-18 10:59:39 +02001856/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1857 * Returns > 0 on success or zero on missing data. It may return an error in
1858 * h2c. Described in RFC7540#6.4.
1859 */
1860static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1861{
1862 int error;
1863
1864 if (h2c->dsi == 0) {
1865 error = H2_ERR_PROTOCOL_ERROR;
1866 goto conn_err;
1867 }
1868
Willy Tarreaucd234e92017-08-18 10:59:39 +02001869 if (h2c->dfl != 4) {
1870 error = H2_ERR_FRAME_SIZE_ERROR;
1871 goto conn_err;
1872 }
1873
1874 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001875 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001876 return 0;
1877
1878 /* late RST, already handled */
1879 if (h2s->st == H2_SS_CLOSED)
1880 return 1;
1881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001883 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884
1885 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001886 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001887 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001888 }
1889
1890 h2s->flags |= H2_SF_RST_RCVD;
1891 return 1;
1892
1893 conn_err:
1894 h2c_error(h2c, error);
1895 return 0;
1896}
1897
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001898/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1899 * It may return an error in h2c or h2s. The caller must consider that the
1900 * return value is the new h2s in case one was allocated (most common case).
1901 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001902 * errors here are reported as connection errors since it's impossible to
1903 * recover from such errors after the compression context has been altered.
1904 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001906{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001907 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001908 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001909 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001910 int error;
1911
1912 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001913 /* RFC7540#4.2 */
1914 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001915 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001916 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001917 }
1918
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001919 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001920 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001921
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001922 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001923 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001924
1925 /* now either the frame is complete or the buffer is complete */
1926 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001927 /* The stream exists/existed, this must be a trailers frame */
1928 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001929 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001930 goto out;
1931 goto done;
1932 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001933 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001934 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001935 goto conn_err;
1936 }
1937 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1938 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1939 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001940 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001941 goto conn_err;
1942 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001943 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1944 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001945
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001946 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001947
Willy Tarreau25919232019-01-03 14:48:18 +01001948 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001949 if (h2c->st0 >= H2_CS_ERROR)
1950 goto out;
1951
Willy Tarreau25919232019-01-03 14:48:18 +01001952 if (error <= 0) {
1953 if (error == 0)
1954 goto out; // missing data
1955
1956 /* Failed to decode this stream (e.g. too large request)
1957 * but the HPACK decompressor is still synchronized.
1958 */
1959 h2s = (struct h2s*)h2_error_stream;
1960 goto send_rst;
1961 }
1962
Willy Tarreau22de8d32018-09-05 19:55:58 +02001963 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001964 * positively from h2c_frt_stream_new(), the stream will report the error,
1965 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001966 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001967 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001968 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001969 h2s = (struct h2s*)h2_refused_stream;
1970 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001971 }
1972
1973 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001974 h2s->rxbuf = rxbuf;
1975 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001976 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001977
Willy Tarreau88d138e2019-01-02 19:38:14 +01001978 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001979 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001980 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001981
1982 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001983 if (h2s->st == H2_SS_OPEN)
1984 h2s->st = H2_SS_HREM;
1985 else
1986 h2s_close(h2s);
Willy Tarreau39d68502018-03-02 12:26:37 +01001987 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001988 }
1989
Willy Tarreau3a429f02019-01-03 11:41:50 +01001990 /* update the max stream ID if the request is being processed */
1991 if (h2s->id > h2c->max_id)
1992 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001993
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001994 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001995
1996 conn_err:
1997 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001998 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001999
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002000 out:
2001 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002002 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002003
2004 send_rst:
2005 /* make the demux send an RST for the current stream. We may only
2006 * do this if we're certain that the HEADERS frame was properly
2007 * decompressed so that the HPACK decoder is still kept up to date.
2008 */
2009 h2_release_buf(h2c, &rxbuf);
2010 h2c->st0 = H2_CS_FRAME_E;
2011 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002012}
2013
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002014/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2015 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2016 * errors here are reported as connection errors since it's impossible to
2017 * recover from such errors after the compression context has been altered.
2018 */
2019static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2020{
2021 int error;
2022
2023 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01002024 /* RFC7540#4.2 */
2025 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002026 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01002027 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002028 }
2029
2030 if (!b_size(&h2c->dbuf))
2031 return NULL; // empty buffer
2032
2033 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2034 return NULL; // incomplete frame
2035
Willy Tarreau1915ca22019-01-24 11:49:37 +01002036 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002037
Willy Tarreau25919232019-01-03 14:48:18 +01002038 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002039 if (h2c->st0 >= H2_CS_ERROR)
2040 return NULL;
2041
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002042 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2043 /* RFC7540#5.1 */
2044 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2045 h2c->st0 = H2_CS_FRAME_E;
2046 return NULL;
2047 }
2048
Willy Tarreau25919232019-01-03 14:48:18 +01002049 if (error <= 0) {
2050 if (error == 0)
2051 return NULL; // missing data
2052
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002053 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002054 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002055 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002056 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002057 }
2058
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002059 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2060 h2s->flags |= H2_SF_ES_RCVD;
2061 h2s->cs->flags |= CS_FL_REOS;
2062 }
2063
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002064 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
2065 h2s->st = H2_SS_ERROR;
2066 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2067 h2s->st = H2_SS_HREM;
2068 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2069 h2s_close(h2s);
2070
2071 return h2s;
2072
2073 conn_err:
2074 h2c_error(h2c, error);
2075 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002076}
2077
Willy Tarreau454f9052017-10-26 19:40:35 +02002078/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2079 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2080 */
2081static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2082{
2083 int error;
2084
2085 /* note that empty DATA frames are perfectly valid and sometimes used
2086 * to signal an end of stream (with the ES flag).
2087 */
2088
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002089 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002090 return 0; // empty buffer
2091
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002092 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002093 return 0; // incomplete frame
2094
2095 /* now either the frame is complete or the buffer is complete */
2096
2097 if (!h2c->dsi) {
2098 /* RFC7540#6.1 */
2099 error = H2_ERR_PROTOCOL_ERROR;
2100 goto conn_err;
2101 }
2102
2103 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2104 /* RFC7540#6.1 */
2105 error = H2_ERR_STREAM_CLOSED;
2106 goto strm_err;
2107 }
2108
Willy Tarreau1915ca22019-01-24 11:49:37 +01002109 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2110 /* RFC7540#8.1.2 */
2111 error = H2_ERR_PROTOCOL_ERROR;
2112 goto strm_err;
2113 }
2114
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002115 if (!h2_frt_transfer_data(h2s))
2116 return 0;
2117
Willy Tarreau454f9052017-10-26 19:40:35 +02002118 /* call the upper layers to process the frame, then let the upper layer
2119 * notify the stream about any change.
2120 */
2121 if (!h2s->cs) {
2122 error = H2_ERR_STREAM_CLOSED;
2123 goto strm_err;
2124 }
2125
Willy Tarreau8f650c32017-11-21 19:36:21 +01002126 if (h2c->st0 >= H2_CS_ERROR)
2127 return 0;
2128
Willy Tarreau721c9742017-11-07 11:05:42 +01002129 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002130 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002131 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002132 }
2133
2134 /* check for completion : the callee will change this to FRAME_A or
2135 * FRAME_H once done.
2136 */
2137 if (h2c->st0 == H2_CS_FRAME_P)
2138 return 0;
2139
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002140 /* last frame */
2141 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002142 if (h2s->st == H2_SS_OPEN)
2143 h2s->st = H2_SS_HREM;
2144 else
2145 h2s_close(h2s);
2146
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002147 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002148 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002149
2150 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2151 /* RFC7540#8.1.2 */
2152 error = H2_ERR_PROTOCOL_ERROR;
2153 goto strm_err;
2154 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002155 }
2156
Willy Tarreau454f9052017-10-26 19:40:35 +02002157 return 1;
2158
2159 conn_err:
2160 h2c_error(h2c, error);
2161 return 0;
2162
2163 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002164 h2s_error(h2s, error);
2165 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002166 return 0;
2167}
2168
Willy Tarreaubc933932017-10-09 16:21:43 +02002169/* process Rx frames to be demultiplexed */
2170static void h2_process_demux(struct h2c *h2c)
2171{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002172 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002173 struct h2_fh hdr;
2174 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002175
Willy Tarreau081d4722017-05-16 21:51:05 +02002176 if (h2c->st0 >= H2_CS_ERROR)
2177 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002178
2179 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2180 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002181 if (h2c->flags & H2_CF_IS_BACK)
2182 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002183 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2184 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002185 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002186 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002187 sess_log(h2c->conn->owner);
2188 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002189 goto fail;
2190 }
2191
2192 h2c->max_id = 0;
2193 h2c->st0 = H2_CS_SETTINGS1;
2194 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002195
2196 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002197 /* ensure that what is pending is a valid SETTINGS frame
2198 * without an ACK.
2199 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002200 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002202 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002203 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002204 sess_log(h2c->conn->owner);
2205 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002206 goto fail;
2207 }
2208
2209 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2210 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2211 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2212 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002213 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002214 goto fail;
2215 }
2216
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002217 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002218 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2219 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2220 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002221 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002222 goto fail;
2223 }
2224
Willy Tarreau3bf69182018-12-21 15:34:50 +01002225 /* that's OK, switch to FRAME_P to process it. This is
2226 * a SETTINGS frame whose header has already been
2227 * deleted above.
2228 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002229 padlen = 0;
2230 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002231 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002232 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233
2234 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002235 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002236 int ret = 0;
2237
2238 if (h2c->st0 >= H2_CS_ERROR)
2239 break;
2240
2241 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002242 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002243 break;
2244
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002245 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002246 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002247 if (!h2c->nb_streams) {
2248 /* only log if no other stream can report the error */
2249 sess_log(h2c->conn->owner);
2250 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002251 break;
2252 }
2253
Willy Tarreau3bf69182018-12-21 15:34:50 +01002254 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2255 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2256 * we read the pad length and drop it from the remaining
2257 * payload (one byte + the 9 remaining ones = 10 total
2258 * removed), so we have a frame payload starting after the
2259 * pad len. Flow controlled frames (DATA) also count the
2260 * padlen in the flow control, so it must be adjusted.
2261 */
2262 if (hdr.len < 1) {
2263 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2264 sess_log(h2c->conn->owner);
2265 goto fail;
2266 }
2267 hdr.len--;
2268
2269 if (b_data(&h2c->dbuf) < 10)
2270 break; // missing padlen
2271
2272 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2273
2274 if (padlen > hdr.len) {
2275 /* RFC7540#6.1 : pad length = length of
2276 * frame payload or greater => error.
2277 */
2278 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2279 sess_log(h2c->conn->owner);
2280 goto fail;
2281 }
2282
2283 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2284 h2c->rcvd_c++;
2285 h2c->rcvd_s++;
2286 }
2287 b_del(&h2c->dbuf, 1);
2288 }
2289 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002290
2291 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002292 h2c->dfl = hdr.len;
2293 h2c->dsi = hdr.sid;
2294 h2c->dft = hdr.ft;
2295 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002296 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002297 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002298
2299 /* check for minimum basic frame format validity */
2300 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2301 if (ret != H2_ERR_NO_ERROR) {
2302 h2c_error(h2c, ret);
2303 sess_log(h2c->conn->owner);
2304 goto fail;
2305 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 }
2307
2308 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002309 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2310
Willy Tarreau567beb82018-12-18 16:52:44 +01002311 if (tmp_h2s != h2s && h2s && h2s->cs &&
2312 (b_data(&h2s->rxbuf) ||
2313 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002314 /* we may have to signal the upper layers */
2315 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002316 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002317 }
2318 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002319
Willy Tarreaud7901432017-12-29 11:34:40 +01002320 if (h2c->st0 == H2_CS_FRAME_E)
2321 goto strm_err;
2322
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002323 if (h2s->st == H2_SS_IDLE &&
2324 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2325 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2326 * this state MUST be treated as a connection error
2327 */
2328 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002329 if (!h2c->nb_streams) {
2330 /* only log if no other stream can report the error */
2331 sess_log(h2c->conn->owner);
2332 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002333 break;
2334 }
2335
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002336 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2337 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2338 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002339 * this state MUST be treated as a stream error.
2340 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2341 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002342 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002343 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2344 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2345 else
2346 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002347 goto strm_err;
2348 }
2349
Willy Tarreauab837502017-12-27 15:07:30 +01002350 /* Below the management of frames received in closed state is a
2351 * bit hackish because the spec makes strong differences between
2352 * streams closed by receiving RST, sending RST, and seeing ES
2353 * in both directions. In addition to this, the creation of a
2354 * new stream reusing the identifier of a closed one will be
2355 * detected here. Given that we cannot keep track of all closed
2356 * streams forever, we consider that unknown closed streams were
2357 * closed on RST received, which allows us to respond with an
2358 * RST without breaking the connection (eg: to abort a transfer).
2359 * Some frames have to be silently ignored as well.
2360 */
2361 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002362 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002363 /* #5.1.1: The identifier of a newly
2364 * established stream MUST be numerically
2365 * greater than all streams that the initiating
2366 * endpoint has opened or reserved. This
2367 * governs streams that are opened using a
2368 * HEADERS frame and streams that are reserved
2369 * using PUSH_PROMISE. An endpoint that
2370 * receives an unexpected stream identifier
2371 * MUST respond with a connection error.
2372 */
2373 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2374 goto strm_err;
2375 }
2376
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002377 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002378 /* RFC7540#5.1:closed: an endpoint that
2379 * receives any frame other than PRIORITY after
2380 * receiving a RST_STREAM MUST treat that as a
2381 * stream error of type STREAM_CLOSED.
2382 *
2383 * Note that old streams fall into this category
2384 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002385 *
2386 * However, we cannot generalize this to all frame types. Those
2387 * carrying compression state must still be processed before
2388 * being dropped or we'll desynchronize the decoder. This can
2389 * happen with request trailers received after sending an
2390 * RST_STREAM, or with header/trailers responses received after
2391 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002392 */
2393 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2394 h2c->st0 = H2_CS_FRAME_E;
2395 goto strm_err;
2396 }
2397
2398 /* RFC7540#5.1:closed: if this state is reached as a
2399 * result of sending a RST_STREAM frame, the peer that
2400 * receives the RST_STREAM might have already sent
2401 * frames on the stream that cannot be withdrawn. An
2402 * endpoint MUST ignore frames that it receives on
2403 * closed streams after it has sent a RST_STREAM
2404 * frame. An endpoint MAY choose to limit the period
2405 * over which it ignores frames and treat frames that
2406 * arrive after this time as being in error.
2407 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002408 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002409 /* RFC7540#5.1:closed: any frame other than
2410 * PRIO/WU/RST in this state MUST be treated as
2411 * a connection error
2412 */
2413 if (h2c->dft != H2_FT_RST_STREAM &&
2414 h2c->dft != H2_FT_PRIORITY &&
2415 h2c->dft != H2_FT_WINDOW_UPDATE) {
2416 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2417 goto strm_err;
2418 }
2419 }
2420 }
2421
Willy Tarreauc0da1962017-10-30 18:38:00 +01002422#if 0
2423 // problem below: it is not possible to completely ignore such
2424 // streams as we need to maintain the compression state as well
2425 // and for this we need to completely process these frames (eg:
2426 // HEADERS frames) as well as counting DATA frames to emit
2427 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2428 // This is a typical case of layer violation where the
2429 // transported contents are critical to the connection's
2430 // validity and must be ignored at the same time :-(
2431
2432 /* graceful shutdown, ignore streams whose ID is higher than
2433 * the one advertised in GOAWAY. RFC7540#6.8.
2434 */
2435 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002436 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2437 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002438 h2c->dfl -= ret;
2439 ret = h2c->dfl == 0;
2440 goto strm_err;
2441 }
2442#endif
2443
Willy Tarreau7e98c052017-10-10 15:56:59 +02002444 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002445 case H2_FT_SETTINGS:
2446 if (h2c->st0 == H2_CS_FRAME_P)
2447 ret = h2c_handle_settings(h2c);
2448
2449 if (h2c->st0 == H2_CS_FRAME_A)
2450 ret = h2c_ack_settings(h2c);
2451 break;
2452
Willy Tarreaucf68c782017-10-10 17:11:41 +02002453 case H2_FT_PING:
2454 if (h2c->st0 == H2_CS_FRAME_P)
2455 ret = h2c_handle_ping(h2c);
2456
2457 if (h2c->st0 == H2_CS_FRAME_A)
2458 ret = h2c_ack_ping(h2c);
2459 break;
2460
Willy Tarreau26f95952017-07-27 17:18:30 +02002461 case H2_FT_WINDOW_UPDATE:
2462 if (h2c->st0 == H2_CS_FRAME_P)
2463 ret = h2c_handle_window_update(h2c, h2s);
2464 break;
2465
Willy Tarreau61290ec2017-10-17 08:19:21 +02002466 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002467 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2468 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2469 * frames' parsers consume all following CONTINUATION
2470 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002471 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002472 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2473 sess_log(h2c->conn->owner);
2474 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002475
Willy Tarreau13278b42017-10-13 19:23:14 +02002476 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002477 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002478 if (h2c->flags & H2_CF_IS_BACK)
2479 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2480 else
2481 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002482 if (tmp_h2s) {
2483 h2s = tmp_h2s;
2484 ret = 1;
2485 }
2486 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002487 break;
2488
Willy Tarreau454f9052017-10-26 19:40:35 +02002489 case H2_FT_DATA:
2490 if (h2c->st0 == H2_CS_FRAME_P)
2491 ret = h2c_frt_handle_data(h2c, h2s);
2492
2493 if (h2c->st0 == H2_CS_FRAME_A)
2494 ret = h2c_send_strm_wu(h2c);
2495 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002496
Willy Tarreau92153fc2017-12-03 19:46:19 +01002497 case H2_FT_PRIORITY:
2498 if (h2c->st0 == H2_CS_FRAME_P)
2499 ret = h2c_handle_priority(h2c);
2500 break;
2501
Willy Tarreaucd234e92017-08-18 10:59:39 +02002502 case H2_FT_RST_STREAM:
2503 if (h2c->st0 == H2_CS_FRAME_P)
2504 ret = h2c_handle_rst_stream(h2c, h2s);
2505 break;
2506
Willy Tarreaue96b0922017-10-30 00:28:29 +01002507 case H2_FT_GOAWAY:
2508 if (h2c->st0 == H2_CS_FRAME_P)
2509 ret = h2c_handle_goaway(h2c);
2510 break;
2511
Willy Tarreau1c661982017-10-30 13:52:01 +01002512 case H2_FT_PUSH_PROMISE:
2513 /* not permitted here, RFC7540#5.1 */
2514 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002515 if (!h2c->nb_streams) {
2516 /* only log if no other stream can report the error */
2517 sess_log(h2c->conn->owner);
2518 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002519 break;
2520
2521 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002522 default:
2523 /* drop frames that we ignore. They may be larger than
2524 * the buffer so we drain all of their contents until
2525 * we reach the end.
2526 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002527 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2528 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002529 h2c->dfl -= ret;
2530 ret = h2c->dfl == 0;
2531 }
2532
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002533 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002534 /* We may have to send an RST if not done yet */
2535 if (h2s->st == H2_SS_ERROR)
2536 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002537
Willy Tarreaua20a5192017-12-27 11:02:06 +01002538 if (h2c->st0 == H2_CS_FRAME_E)
2539 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002540
Willy Tarreau7e98c052017-10-10 15:56:59 +02002541 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002542 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002543 break;
2544
2545 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002546 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002547 h2c->st0 = H2_CS_FRAME_H;
2548 }
2549 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002550
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002551 if (h2c->rcvd_c > 0 &&
2552 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2553 h2c_send_conn_wu(h2c);
2554
Willy Tarreau52eed752017-09-22 15:05:09 +02002555 fail:
2556 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002557 if (h2s && h2s->cs &&
2558 (b_data(&h2s->rxbuf) ||
2559 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002560 /* we may have to signal the upper layers */
2561 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002562 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002563 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002564
Willy Tarreau47b515a2018-12-21 16:09:41 +01002565 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002566}
2567
2568/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2569 * the end.
2570 */
2571static int h2_process_mux(struct h2c *h2c)
2572{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002573 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002574
Willy Tarreau01b44822018-10-03 14:26:37 +02002575 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2576 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2577 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2578 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2579 if (h2c->st0 == H2_CS_ERROR) {
2580 h2c->st0 = H2_CS_ERROR2;
2581 sess_log(h2c->conn->owner);
2582 }
2583 goto fail;
2584 }
2585 h2c->st0 = H2_CS_SETTINGS1;
2586 }
2587 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002588 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002589 return 1;
2590 }
2591
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002592 /* start by sending possibly pending window updates */
2593 if (h2c->rcvd_c > 0 &&
2594 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2595 h2c_send_conn_wu(h2c) < 0)
2596 goto fail;
2597
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002598 /* First we always process the flow control list because the streams
2599 * waiting there were already elected for immediate emission but were
2600 * blocked just on this.
2601 */
2602
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002603 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002604 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2605 h2c->st0 >= H2_CS_ERROR)
2606 break;
2607
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002608 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002609 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2610 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002611 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002612 LIST_DEL(&h2s->list);
2613 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002614 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002615 }
2616
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002617 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002618 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2619 break;
2620
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002621 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002622 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2623 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002624 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002625 LIST_DEL(&h2s->list);
2626 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002627 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002628 }
2629
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002630 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002631 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002632 if (h2c->st0 == H2_CS_ERROR) {
2633 if (h2c->max_id >= 0) {
2634 h2c_send_goaway_error(h2c, NULL);
2635 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2636 return 0;
2637 }
2638
2639 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2640 }
2641 return 1;
2642 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002643 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002644}
2645
Willy Tarreau62f52692017-10-08 23:01:42 +02002646
Willy Tarreau479998a2018-11-18 06:30:59 +01002647/* Attempt to read data, and subscribe if none available.
2648 * The function returns 1 if data has been received, otherwise zero.
2649 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002650static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002651{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002652 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002653 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002654 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002655 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002656
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002657 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002658 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002659
Willy Tarreau315d8072017-12-10 22:17:57 +01002660 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002661 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002662
Willy Tarreau44e973f2018-03-01 17:49:30 +01002663 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002664 if (!buf) {
2665 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002666 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002667 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002668
Olivier Houchard7505f942018-08-21 18:10:44 +02002669 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002670 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002671 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2672 /* HTX in use : try to pre-align the buffer like the
2673 * rxbufs will be to optimize memory copies. We'll make
2674 * sure that the frame header lands at the end of the
2675 * HTX block to alias it upon recv. We cannot use the
2676 * head because rcv_buf() will realign the buffer if
2677 * it's empty. Thus we cheat and pretend we already
2678 * have a few bytes there.
2679 */
2680 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002681 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002682 }
2683 else
2684 max = b_room(buf);
2685
Olivier Houchard7505f942018-08-21 18:10:44 +02002686 if (max)
2687 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2688 else
2689 ret = 0;
2690 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002691
Olivier Houchard53216e72018-10-10 15:46:36 +02002692 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002693 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002694
Olivier Houcharda1411e62018-08-17 18:42:48 +02002695 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002696 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002697 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002698 }
2699
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002700 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002701 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002702 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002703}
2704
Willy Tarreau479998a2018-11-18 06:30:59 +01002705/* Try to send data if possible.
2706 * The function returns 1 if data have been sent, otherwise zero.
2707 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002708static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002709{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002710 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002711 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002712 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002713
2714 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002715 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002716
Olivier Houchard7505f942018-08-21 18:10:44 +02002717
Willy Tarreaua2af5122017-10-09 11:56:46 +02002718 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2719 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002720 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002721 }
2722
Willy Tarreaubc933932017-10-09 16:21:43 +02002723 /* This loop is quite simple : it tries to fill as much as it can from
2724 * pending streams into the existing buffer until it's reportedly full
2725 * or the end of send requests is reached. Then it tries to send this
2726 * buffer's contents out, marks it not full if at least one byte could
2727 * be sent, and tries again.
2728 *
2729 * The snd_buf() function normally takes a "flags" argument which may
2730 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2731 * data immediately comes and CO_SFL_STREAMER to indicate that the
2732 * connection is streaming lots of data (used to increase TLS record
2733 * size at the expense of latency). The former can be sent any time
2734 * there's a buffer full flag, as it indicates at least one stream
2735 * attempted to send and failed so there are pending data. An
2736 * alternative would be to set it as long as there's an active stream
2737 * but that would be problematic for ACKs until we have an absolute
2738 * guarantee that all waiters have at least one byte to send. The
2739 * latter should possibly not be set for now.
2740 */
2741
2742 done = 0;
2743 while (!done) {
2744 unsigned int flags = 0;
2745
2746 /* fill as much as we can into the current buffer */
2747 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2748 done = h2_process_mux(h2c);
2749
Olivier Houchard2b094432019-01-29 18:28:36 +01002750 if (h2c->flags & H2_CF_MUX_MALLOC)
2751 break;
2752
Willy Tarreaubc933932017-10-09 16:21:43 +02002753 if (conn->flags & CO_FL_ERROR)
2754 break;
2755
2756 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2757 flags |= CO_SFL_MSG_MORE;
2758
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002759 if (b_data(&h2c->mbuf)) {
2760 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002761 if (!ret)
2762 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002763 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002764 b_del(&h2c->mbuf, ret);
2765 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002766 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002767
2768 /* wrote at least one byte, the buffer is not full anymore */
2769 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2770 }
2771
Willy Tarreaua2af5122017-10-09 11:56:46 +02002772 if (conn->flags & CO_FL_SOCK_WR_SH) {
2773 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002774 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002775 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002776 /* We're not full anymore, so we can wake any task that are waiting
2777 * for us.
2778 */
2779 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002780 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002781 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2782 struct h2s *, list);
2783 LIST_DEL(&h2s->list);
2784 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002785 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002786 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2787 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002788 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002789 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002790 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002791 /* We're done, no more to send */
2792 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002793 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002794schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002795 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2796 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002797 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002798}
2799
2800static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2801{
2802 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002803 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002804
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002805 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002806 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002807 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002808 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002809 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002810 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002811 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002812}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002813
Willy Tarreau62f52692017-10-08 23:01:42 +02002814/* callback called on any event by the connection handler.
2815 * It applies changes and returns zero, or < 0 if it wants immediate
2816 * destruction of the connection (which normally doesn not happen in h2).
2817 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002818static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002819{
Olivier Houchard7505f942018-08-21 18:10:44 +02002820 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002821
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002822 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002823 h2_process_demux(h2c);
2824
2825 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002827
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002828 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002829 h2c->flags &= ~H2_CF_DEM_DFULL;
2830 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002831 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002832
Willy Tarreau0b37d652018-10-03 10:33:02 +02002833 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002834 /* frontend is stopping, reload likely in progress, let's try
2835 * to announce a graceful shutdown if not yet done. We don't
2836 * care if it fails, it will be tried again later.
2837 */
2838 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2839 if (h2c->last_sid < 0)
2840 h2c->last_sid = (1U << 31) - 1;
2841 h2c_send_goaway_error(h2c, NULL);
2842 }
2843 }
2844
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002845 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002846 * If we received early data, and the handshake is done, wake
2847 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002848 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002849 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2850 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2851 struct eb32_node *node;
2852 struct h2s *h2s;
2853
2854 h2c->flags |= H2_CF_WAIT_FOR_HS;
2855 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2856
2857 while (node) {
2858 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002859 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002860 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002861 node = eb32_next(node);
2862 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002863 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002864
Willy Tarreau26bd7612017-10-09 16:47:04 +02002865 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002866 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2867 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2868 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002869 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002870
2871 if (eb_is_empty(&h2c->streams_by_id)) {
2872 /* no more stream, kill the connection now */
2873 h2_release(conn);
2874 return -1;
2875 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002876 }
2877
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002878 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002879 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002880
Olivier Houchard53216e72018-10-10 15:46:36 +02002881 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2882 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2883 (h2c->st0 != H2_CS_ERROR &&
2884 !b_data(&h2c->mbuf) &&
2885 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2886 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002887 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002888
Willy Tarreau3f133572017-10-31 19:21:06 +01002889 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002890 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002891 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002892 task_queue(h2c->task);
2893 }
2894 else
2895 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002896 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002897
Olivier Houchard7505f942018-08-21 18:10:44 +02002898 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002899 return 0;
2900}
2901
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002902static int h2_wake(struct connection *conn)
2903{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002904 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002905
2906 return (h2_process(h2c));
2907}
2908
Willy Tarreauea392822017-10-31 10:02:25 +01002909/* Connection timeout management. The principle is that if there's no receipt
2910 * nor sending for a certain amount of time, the connection is closed. If the
2911 * MUX buffer still has lying data or is not allocatable, the connection is
2912 * immediately killed. If it's allocatable and empty, we attempt to send a
2913 * GOAWAY frame.
2914 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002915static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002916{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002917 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002918 int expired = tick_is_expired(t->expire, now_ms);
2919
Willy Tarreau0975f112018-03-29 15:22:59 +02002920 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002921 return t;
2922
Willy Tarreau0975f112018-03-29 15:22:59 +02002923 task_delete(t);
2924 task_free(t);
2925
2926 if (!h2c) {
2927 /* resources were already deleted */
2928 return NULL;
2929 }
2930
2931 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002932 h2c_error(h2c, H2_ERR_NO_ERROR);
2933 h2_wake_some_streams(h2c, 0, 0);
2934
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002936 /* don't even try to send a GOAWAY, the buffer is stuck */
2937 h2c->flags |= H2_CF_GOAWAY_FAILED;
2938 }
2939
2940 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002941 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002942 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2943 h2c->flags |= H2_CF_GOAWAY_FAILED;
2944
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002945 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2946 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002947 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002948 b_del(&h2c->mbuf, ret);
2949 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002950 }
2951 }
Willy Tarreauea392822017-10-31 10:02:25 +01002952
Willy Tarreau0975f112018-03-29 15:22:59 +02002953 /* either we can release everything now or it will be done later once
2954 * the last stream closes.
2955 */
2956 if (eb_is_empty(&h2c->streams_by_id))
2957 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002958
Willy Tarreauea392822017-10-31 10:02:25 +01002959 return NULL;
2960}
2961
2962
Willy Tarreau62f52692017-10-08 23:01:42 +02002963/*******************************************/
2964/* functions below are used by the streams */
2965/*******************************************/
2966
2967/*
2968 * Attach a new stream to a connection
2969 * (Used for outgoing connections)
2970 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002971static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002972{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002973 struct conn_stream *cs;
2974 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002975 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002976
2977 cs = cs_new(conn);
2978 if (!cs)
2979 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002980 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002981 if (!h2s) {
2982 cs_free(cs);
2983 return NULL;
2984 }
2985 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002986}
2987
Willy Tarreaufafd3982018-11-18 21:29:20 +01002988/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2989 * We have to scan because we may have some orphan streams. It might be
2990 * beneficial to scan backwards from the end to reduce the likeliness to find
2991 * orphans.
2992 */
2993static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2994{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002995 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002996 struct h2s *h2s;
2997 struct eb32_node *node;
2998
2999 node = eb32_first(&h2c->streams_by_id);
3000 while (node) {
3001 h2s = container_of(node, struct h2s, by_id);
3002 if (h2s->cs)
3003 return h2s->cs;
3004 node = eb32_next(node);
3005 }
3006 return NULL;
3007}
3008
Willy Tarreau62f52692017-10-08 23:01:42 +02003009/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003010 * Destroy the mux and the associated connection, if it is no longer used
3011 */
3012static void h2_destroy(struct connection *conn)
3013{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003014 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003015
3016 if (eb_is_empty(&h2c->streams_by_id))
3017 h2_release(h2c->conn);
3018}
3019
3020/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003021 * Detach the stream from the connection and possibly release the connection.
3022 */
3023static void h2_detach(struct conn_stream *cs)
3024{
Willy Tarreau60935142017-10-16 18:11:19 +02003025 struct h2s *h2s = cs->ctx;
3026 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003027 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003028
3029 cs->ctx = NULL;
3030 if (!h2s)
3031 return;
3032
Olivier Houchardf502aca2018-12-14 19:42:40 +01003033 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003034 h2c = h2s->h2c;
3035 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003036 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02003037 if (h2c->flags & H2_CF_DEM_TOOMANY &&
3038 !h2_has_too_many_cs(h2c)) {
3039 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003040 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02003041 }
Willy Tarreau60935142017-10-16 18:11:19 +02003042
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003043 /* this stream may be blocked waiting for some data to leave (possibly
3044 * an ES or RST frame), so orphan it in this case.
3045 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003046 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003047 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003048 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003049 return;
3050
Willy Tarreau45f752e2017-10-30 15:44:59 +01003051 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3052 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3053 /* unblock the connection if it was blocked on this
3054 * stream.
3055 */
3056 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3057 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003058 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003059 }
3060
Willy Tarreau71049cc2018-03-28 13:56:39 +02003061 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003062
Olivier Houchard8a786902018-12-15 16:05:40 +01003063 if (h2c->flags & H2_CF_IS_BACK &&
3064 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003065 if (!(h2c->conn->flags &
3066 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3067 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003068 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003069 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3070 h2c->conn->owner = NULL;
3071 if (eb_is_empty(&h2c->streams_by_id)) {
3072 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3073 /* The server doesn't want it, let's kill the connection right away */
3074 h2c->conn->mux->destroy(h2c->conn);
3075 return;
3076 }
3077 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003078 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003079 if (eb_is_empty(&h2c->streams_by_id)) {
3080 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3081 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3082 return;
3083 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003084 /* Never ever allow to reuse a connection from a non-reuse backend */
3085 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3086 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01003087 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003088 struct server *srv = objt_server(h2c->conn->target);
3089
3090 if (srv) {
3091 if (h2c->conn->flags & CO_FL_PRIVATE)
3092 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3093 else
3094 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3095 }
3096
3097 }
3098 }
3099 }
3100
Willy Tarreaue323f342018-03-28 13:51:45 +02003101 /* We don't want to close right now unless we're removing the
3102 * last stream, and either the connection is in error, or it
3103 * reached the ID already specified in a GOAWAY frame received
3104 * or sent (as seen by last_sid >= 0).
3105 */
3106 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3107 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003108 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003109 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003110 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003111 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003112 (conn_xprt_read0_pending(h2c->conn) ||
3113 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3114 /* no more stream will come, kill it now */
3115 h2_release(h2c->conn);
3116 }
3117 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003118 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003119 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3120 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003121 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003122 else
3123 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003124 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003125}
3126
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003127static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003128{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003129 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003130 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003131
Willy Tarreau721c9742017-11-07 11:05:42 +01003132 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003133 return;
3134
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003135 /* if no outgoing data was seen on this stream, it means it was
3136 * closed with a "tcp-request content" rule that is normally
3137 * used to kill the connection ASAP (eg: limit abuse). In this
3138 * case we send a goaway to close the connection.
3139 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003140 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003141 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003142 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003143
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003144 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3145 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003146 h2c_send_goaway_error(h2c, h2s) <= 0)
3147 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003148
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003149 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003150 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003151 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003152
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003153 return;
3154add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003155 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003156 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003157 if (h2s->flags & H2_SF_BLK_MFCTL) {
3158 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3159 h2s->send_wait = sw;
3160 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3161 h2s->send_wait = sw;
3162 LIST_ADDQ(&h2c->send_list, &h2s->list);
3163 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003164 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003165 /* Let the handler know we want shutr */
3166 sw->handle = (void *)((long)sw->handle | 1);
3167
Willy Tarreau62f52692017-10-08 23:01:42 +02003168}
3169
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003170static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003171{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003172 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003173 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003174
Willy Tarreau721c9742017-11-07 11:05:42 +01003175 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003176 return;
3177
Willy Tarreau67434202017-11-06 20:20:51 +01003178 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003179 /* we can cleanly close using an empty data frame only after headers */
3180
3181 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3182 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003183 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003184
3185 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003186 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003187 else
3188 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003189 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003190 /* if no outgoing data was seen on this stream, it means it was
3191 * closed with a "tcp-request content" rule that is normally
3192 * used to kill the connection ASAP (eg: limit abuse). In this
3193 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003194 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003195 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003196 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003197 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003198
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003199 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3200 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003201 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003202 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003203
Willy Tarreau00dd0782018-03-01 16:31:34 +01003204 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003205 }
3206
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003207 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003208 tasklet_wakeup(h2c->wait_event.task);
3209 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003210
3211 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003212 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003213 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003214 if (h2s->flags & H2_SF_BLK_MFCTL) {
3215 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3216 h2s->send_wait = sw;
3217 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3218 h2s->send_wait = sw;
3219 LIST_ADDQ(&h2c->send_list, &h2s->list);
3220 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003221 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003222 /* let the handler know we want to shutw */
3223 sw->handle = (void *)((long)(sw->handle) | 2);
3224
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003225}
3226
3227static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3228{
3229 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003230 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003231
Olivier Houchard2c68a462018-12-15 22:42:20 +01003232 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003233 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003234 h2s->send_wait = NULL;
3235 LIST_DEL(&h2s->list);
3236 LIST_INIT(&h2s->list);
3237 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003238 if (reason & 2)
3239 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003240 if (reason & 1)
3241 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003242
Olivier Houchard2c68a462018-12-15 22:42:20 +01003243 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003244 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003245 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003246 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003247}
3248
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003249static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3250{
3251 struct h2s *h2s = cs->ctx;
3252
3253 if (!mode)
3254 return;
3255
3256 h2_do_shutr(h2s);
3257}
3258
3259static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3260{
3261 struct h2s *h2s = cs->ctx;
3262
3263 h2_do_shutw(h2s);
3264}
3265
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003266/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003267 * HTX request or response depending on the connection's side. Returns a
3268 * positive value on success, a negative value on failure, or 0 if it couldn't
3269 * proceed. May report connection errors in h2c->errcode if the frame is
3270 * non-decodable and the connection unrecoverable. In absence of connection
3271 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003272 *
3273 * The function may fold CONTINUATION frames into the initial HEADERS frame
3274 * by removing padding and next frame header, then moving the CONTINUATION
3275 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3276 * leaving a hole between the main frame and the beginning of the next one.
3277 * The possibly remaining incomplete or next frame at the end may be moved
3278 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3279 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3280 *
3281 * A buffer at the beginning of processing may look like this :
3282 *
3283 * ,---.---------.-----.--------------.--------------.------.---.
3284 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3285 * `---^---------^-----^--------------^--------------^------^---'
3286 * | | <-----> | |
3287 * area | dpl | wrap
3288 * |<--------------> |
3289 * | dfl |
3290 * |<-------------------------------------------------->|
3291 * head data
3292 *
3293 * Padding is automatically overwritten when folding, participating to the
3294 * hole size after dfl :
3295 *
3296 * ,---.------------------------.-----.--------------.------.---.
3297 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3298 * `---^------------------------^-----^--------------^------^---'
3299 * | | <-----> | |
3300 * area | hole | wrap
3301 * |<-----------------------> |
3302 * | dfl |
3303 * |<-------------------------------------------------->|
3304 * head data
3305 *
3306 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3307 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3308 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003309 *
3310 * The <flags> field must point to either the stream's flags or to a copy of it
3311 * so that the function can update the following flags :
3312 * - H2_SF_DATA_CLEN when content-length is seen
3313 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3314 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003315 *
3316 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3317 * decoding, in order to detect if we're dealing with a headers or a trailers
3318 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003319 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003320static 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 +02003321{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003322 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003323 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003324 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003325 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003326 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003327 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003328 int flen; // header frame len
3329 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003330 int ret = 0;
3331 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003332 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003333 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003334
Willy Tarreauea18f862018-12-22 20:19:26 +01003335next_frame:
3336 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3337 goto leave; // incomplete input frame
3338
3339 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3340 * this case, we'll try to paste it immediately after the initial
3341 * HEADERS frame payload and kill any possible padding. The initial
3342 * frame's length will be increased to represent the concatenation
3343 * of the two frames. The next frame is read from position <tlen>
3344 * and written at position <flen> (minus padding if some is present).
3345 */
3346 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3347 struct h2_fh hdr;
3348 int clen; // CONTINUATION frame's payload length
3349
3350 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3351 /* no more data, the buffer may be full, either due to
3352 * too large a frame or because of too large a hole that
3353 * we're going to compact at the end.
3354 */
3355 goto leave;
3356 }
3357
3358 if (hdr.ft != H2_FT_CONTINUATION) {
3359 /* RFC7540#6.10: frame of unexpected type */
3360 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3361 goto fail;
3362 }
3363
3364 if (hdr.sid != h2c->dsi) {
3365 /* RFC7540#6.10: frame of different stream */
3366 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3367 goto fail;
3368 }
3369
3370 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3371 /* RFC7540#4.2: invalid frame length */
3372 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3373 goto fail;
3374 }
3375
3376 /* detect when we must stop aggragating frames */
3377 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3378
3379 /* Take as much as we can of the CONTINUATION frame's payload */
3380 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3381 if (clen > hdr.len)
3382 clen = hdr.len;
3383
3384 /* Move the frame's payload over the padding, hole and frame
3385 * header. At least one of hole or dpl is null (see diagrams
3386 * above). The hole moves after the new aggragated frame.
3387 */
3388 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3389 h2c->dfl += clen - h2c->dpl;
3390 hole += h2c->dpl + 9;
3391 h2c->dpl = 0;
3392 goto next_frame;
3393 }
3394
3395 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003396
Willy Tarreau13278b42017-10-13 19:23:14 +02003397 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003398 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003399 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003400 copy = alloc_trash_chunk();
3401 if (!copy) {
3402 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3403 goto fail;
3404 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003405 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3406 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3407 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003408 }
3409
Willy Tarreau13278b42017-10-13 19:23:14 +02003410 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3411 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003412 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003413 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3414 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003415 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003416 }
3417
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003418 if (flen < 5) {
3419 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3420 goto fail;
3421 }
3422
Willy Tarreau13278b42017-10-13 19:23:14 +02003423 hdrs += 5; // stream dep = 4, weight = 1
3424 flen -= 5;
3425 }
3426
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003427 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003428 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003429 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003430 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003431
Willy Tarreau937f7602018-02-26 15:22:17 +01003432 /* we can't retry a failed decompression operation so we must be very
3433 * careful not to take any risks. In practice the output buffer is
3434 * always empty except maybe for trailers, in which case we simply have
3435 * to wait for the upper layer to finish consuming what is available.
3436 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003437
3438 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003439 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003440 if (!htx_is_empty(htx)) {
3441 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003442 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003443 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003444 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003445 if (b_data(rxbuf)) {
3446 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003447 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003448 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003449
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003450 rxbuf->head = 0;
3451 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003452 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003453
Willy Tarreau25919232019-01-03 14:48:18 +01003454 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003455 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3456 sizeof(list)/sizeof(list[0]), tmp);
3457 if (outlen < 0) {
3458 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3459 goto fail;
3460 }
3461
Willy Tarreau25919232019-01-03 14:48:18 +01003462 /* The PACK decompressor was updated, let's update the input buffer and
3463 * the parser's state to commit these changes and allow us to later
3464 * fail solely on the stream if needed.
3465 */
3466 b_del(&h2c->dbuf, h2c->dfl + hole);
3467 h2c->dfl = hole = 0;
3468 h2c->st0 = H2_CS_FRAME_H;
3469
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003470 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003471 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003472
Willy Tarreau88d138e2019-01-02 19:38:14 +01003473 if (*flags & H2_SF_HEADERS_RCVD)
3474 goto trailers;
3475
3476 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003477 if (htx) {
3478 /* HTX mode */
3479 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003480 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003481 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003482 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003483 } else {
3484 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003485 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003486 if (outlen > 0)
3487 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003488 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003489
3490 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003491 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003492 goto fail;
3493 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003494
Willy Tarreau174b06a2018-04-25 18:13:58 +02003495 if (msgf & H2_MSGF_BODY) {
3496 /* a payload is present */
3497 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003498 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003499 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003500 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003501 }
3502
Willy Tarreau88d138e2019-01-02 19:38:14 +01003503 done:
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003504 /* indicate that a HEADERS frame was received for this stream */
3505 *flags |= H2_SF_HEADERS_RCVD;
3506
Willy Tarreau88d138e2019-01-02 19:38:14 +01003507 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
3508 /* Mark the end of message, either using EOM in HTX or with the
3509 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3510 * is not set during headers with END_STREAM.
3511 */
3512 if (htx) {
3513 if (!htx_add_endof(htx, HTX_BLK_EOM))
3514 goto fail;
3515 }
3516 else if (*flags & H2_SF_DATA_CHNK) {
3517 if (!b_putblk(rxbuf, "\r\n", 2))
3518 goto fail;
3519 }
3520 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003521
Willy Tarreau86277d42019-01-02 15:36:11 +01003522 /* success */
3523 ret = 1;
3524
Willy Tarreau68dd9852017-07-03 14:44:26 +02003525 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003526 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003527 * move the remaining data over it.
3528 */
3529 if (hole) {
3530 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3531 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3532 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3533 b_sub(&h2c->dbuf, hole);
3534 }
3535
3536 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3537 /* too large frames */
3538 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003539 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003540 }
3541
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003542 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003543 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003544 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003545 return ret;
3546
Willy Tarreau68dd9852017-07-03 14:44:26 +02003547 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003548 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003549 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003550
3551 trailers:
3552 /* This is the last HEADERS frame hence a trailer */
3553
3554 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3555 /* It's a trailer but it's missing ES flag */
3556 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3557 goto fail;
3558 }
3559
3560 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3561 * block, and when using chunks we must send the 0 CRLF marker. For
3562 * other modes, the trailers are silently dropped.
3563 */
3564 if (htx) {
3565 if (!htx_add_endof(htx, HTX_BLK_EOD))
3566 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003567 if (h2_make_htx_trailers(list, htx) <= 0)
3568 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003569 }
3570 else if (*flags & H2_SF_DATA_CHNK) {
3571 /* Legacy mode with chunked encoding : we must finalize the
3572 * data block message emit the trailing CRLF */
3573 if (!b_putblk(rxbuf, "0\r\n", 3))
3574 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003575
3576 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3577 if (outlen > 0)
3578 b_add(rxbuf, outlen);
3579 else
3580 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003581 }
3582
3583 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003584}
3585
Willy Tarreau454f9052017-10-26 19:40:35 +02003586/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3587 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3588 * in use, a new chunk is emitted for each frame. This is supposed to fit
3589 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3590 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3591 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003592 * parser state is automatically updated. Returns > 0 if it could completely
3593 * send the current frame, 0 if it couldn't complete, in which case
3594 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3595 * DATA frame can return 0 as a valid result). Stream errors are reported in
3596 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3597 * have checked the frame header and ensured that the frame was complete or the
3598 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003599 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003600static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003601{
3602 struct h2c *h2c = h2s->h2c;
3603 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003604 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003605 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003606 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003607 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003608
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003609 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003610
Olivier Houchard638b7992018-08-16 15:41:52 +02003611 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003612 if (!csbuf) {
3613 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003614 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003615 }
3616
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003617try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003618 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003619 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3620 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003621 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003622 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003623
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003624 if (flen > b_data(&h2c->dbuf)) {
3625 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003626 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003627 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003628 }
3629
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003630 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003631 block1 = htx_free_data_space(htx);
3632 if (!block1) {
3633 h2c->flags |= H2_CF_DEM_SFULL;
3634 goto fail;
3635 }
3636 if (flen > block1)
3637 flen = block1;
3638
3639 /* here, flen is the max we can copy into the output buffer */
3640 block1 = b_contig_data(&h2c->dbuf, 0);
3641 if (flen > block1)
3642 flen = block1;
3643
3644 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3645 h2c->flags |= H2_CF_DEM_SFULL;
3646 goto fail;
3647 }
3648
3649 b_del(&h2c->dbuf, flen);
3650 h2c->dfl -= flen;
3651 h2c->rcvd_c += flen;
3652 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003653
3654 if (h2s->flags & H2_SF_DATA_CLEN)
3655 h2s->body_len -= flen;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003656 goto try_again;
3657 }
3658 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003659 /* it doesn't fit and the buffer is fragmented,
3660 * so let's defragment it and try again.
3661 */
3662 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003663 }
3664
Willy Tarreaueba10f22018-04-25 20:44:22 +02003665 /* chunked-encoding requires more room */
3666 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003667 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003668 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3669 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3670 (chklen < 1048576) ? 4 : 8;
3671 chklen += 4; // CRLF, CRLF
3672 }
3673
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003674 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003675 if (flen + chklen > b_room(csbuf)) {
3676 if (chklen >= b_room(csbuf)) {
3677 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003678 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003679 }
3680 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003681 }
3682
3683 if (h2s->flags & H2_SF_DATA_CHNK) {
3684 /* emit the chunk size */
3685 unsigned int chksz = flen;
3686 char str[10];
3687 char *beg;
3688
3689 beg = str + sizeof(str);
3690 *--beg = '\n';
3691 *--beg = '\r';
3692 do {
3693 *--beg = hextab[chksz & 0xF];
3694 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003695 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003696 }
3697
Willy Tarreau454f9052017-10-26 19:40:35 +02003698 /* Block1 is the length of the first block before the buffer wraps,
3699 * block2 is the optional second block to reach the end of the frame.
3700 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003701 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003702 if (block1 > flen)
3703 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003704 block2 = flen - block1;
3705
3706 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003707 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003708
3709 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003710 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003711
Willy Tarreaueba10f22018-04-25 20:44:22 +02003712 if (h2s->flags & H2_SF_DATA_CHNK) {
3713 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003714 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003715 }
3716
Willy Tarreau454f9052017-10-26 19:40:35 +02003717 /* now mark the input data as consumed (will be deleted from the buffer
3718 * by the caller when seeing FRAME_A after sending the window update).
3719 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003720 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003721 h2c->dfl -= flen;
3722 h2c->rcvd_c += flen;
3723 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3724
Willy Tarreau1915ca22019-01-24 11:49:37 +01003725 if (h2s->flags & H2_SF_DATA_CLEN)
3726 h2s->body_len -= flen;
3727
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003728 if (h2c->dfl > h2c->dpl) {
3729 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003730 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003731 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003732 }
3733
Willy Tarreau4a28da12018-01-04 14:41:00 +01003734 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003735 /* here we're done with the frame, all the payload (except padding) was
3736 * transferred.
3737 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003738
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003739 if (h2c->dff & H2_F_DATA_END_STREAM) {
3740 if (htx) {
3741 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3742 h2c->flags |= H2_CF_DEM_SFULL;
3743 goto fail;
3744 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003745 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003746 else if (h2s->flags & H2_SF_DATA_CHNK) {
3747 /* emit the trailing 0 CRLF CRLF */
3748 if (b_room(csbuf) < 5) {
3749 h2c->flags |= H2_CF_DEM_SFULL;
3750 goto fail;
3751 }
3752 chklen += 5;
3753 b_putblk(csbuf, "0\r\n\r\n", 5);
3754 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003755 }
3756
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003757 h2c->rcvd_c += h2c->dpl;
3758 h2c->rcvd_s += h2c->dpl;
3759 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003760 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3761
Willy Tarreau39d68502018-03-02 12:26:37 +01003762 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003763 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003764 h2s->cs->flags |= CS_FL_REOS;
3765 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003766 if (htx)
3767 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003768 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003769 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003770 if (htx)
3771 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003772 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003773}
3774
Willy Tarreau5dd17352018-06-14 13:33:30 +02003775/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3776 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3777 * number of bytes sent. The caller must check the stream's status to detect
3778 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003779 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003780static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003781{
3782 struct http_hdr list[MAX_HTTP_HDR];
3783 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003784 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003785 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003786 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003787 int es_now = 0;
3788 int ret = 0;
3789 int hdr;
3790
3791 if (h2c_mux_busy(h2c, h2s)) {
3792 h2s->flags |= H2_SF_BLK_MBUSY;
3793 return 0;
3794 }
3795
Willy Tarreau44e973f2018-03-01 17:49:30 +01003796 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003797 h2c->flags |= H2_CF_MUX_MALLOC;
3798 h2s->flags |= H2_SF_BLK_MROOM;
3799 return 0;
3800 }
3801
3802 /* First, try to parse the H1 response and index it into <list>.
3803 * NOTE! Since it comes from haproxy, we *know* that a response header
3804 * block does not wrap and we can safely read it this way without
3805 * having to realign the buffer.
3806 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003807 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003808 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003809 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003810 /* incomplete or invalid response, this is abnormal coming from
3811 * haproxy and may only result in a bad errorfile or bad Lua code
3812 * so that won't be fixed, raise an error now.
3813 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003814 * FIXME: we should instead add the ability to only return a
3815 * 502 bad gateway. But in theory this is not supposed to
3816 * happen.
3817 */
3818 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3819 ret = 0;
3820 goto end;
3821 }
3822
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003823 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003824
3825 /* certain statuses have no body or an empty one, regardless of
3826 * what the headers say.
3827 */
3828 if (sl.st.status >= 100 && sl.st.status < 200) {
3829 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3830 h1m->curr_len = h1m->body_len = 0;
3831 }
3832 else if (sl.st.status == 204 || sl.st.status == 304) {
3833 /* no contents, claim c-len is present and set to zero */
3834 h1m->flags &= ~H1_MF_CHNK;
3835 h1m->flags |= H1_MF_CLEN;
3836 h1m->curr_len = h1m->body_len = 0;
3837 }
3838
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003839 chunk_reset(&outbuf);
3840
3841 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003842 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003843 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003844 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003846 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003847 break;
3848 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003849 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003850 }
3851
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003852 if (outbuf.size < 9)
3853 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003854
3855 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003856 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3857 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3858 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859
3860 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003861 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003862 /* this is an unparsable response */
3863 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3864 ret = 0;
3865 goto end;
3866 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003867
3868 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003869 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003870 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003871 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003872 }
3873
3874 /* encode all headers, stop at empty name */
3875 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003876 /* these ones do not exist in H2 and must be dropped. */
3877 if (isteq(list[hdr].n, ist("connection")) ||
3878 isteq(list[hdr].n, ist("proxy-connection")) ||
3879 isteq(list[hdr].n, ist("keep-alive")) ||
3880 isteq(list[hdr].n, ist("upgrade")) ||
3881 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003882 continue;
3883
3884 if (isteq(list[hdr].n, ist("")))
3885 break; // end
3886
3887 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3888 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003889 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003890 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003891 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003892 }
3893 }
3894
3895 /* we may need to add END_STREAM */
3896 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3897 es_now = 1;
3898
3899 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003901
3902 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003903 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003904
3905 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003906 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003907
3908 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003909 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003910 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003911
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003913 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003914 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003915
Willy Tarreau801250e2018-09-11 11:45:04 +02003916 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003917 h2s->flags |= H2_SF_ES_SENT;
3918 if (h2s->st == H2_SS_OPEN)
3919 h2s->st = H2_SS_HLOC;
3920 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003921 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003922 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003923 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003924 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003925 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003926 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003927 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003928 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003929 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003930
3931 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003932
3933 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003934 //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003935 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003936 full:
3937 h1m_init_res(h1m);
3938 h1m->err_pos = -1; // don't care about errors on the response path
3939 h2c->flags |= H2_CF_MUX_MFULL;
3940 h2s->flags |= H2_SF_BLK_MROOM;
3941 ret = 0;
3942 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003943}
3944
Willy Tarreau5dd17352018-06-14 13:33:30 +02003945/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3946 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3947 * the number of bytes sent. The caller must check the stream's status to
3948 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003949 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003950static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003951{
3952 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003953 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003954 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003955 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003956 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003957 int es_now = 0;
3958 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003959 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003960 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003961
3962 if (h2c_mux_busy(h2c, h2s)) {
3963 h2s->flags |= H2_SF_BLK_MBUSY;
3964 goto end;
3965 }
3966
Willy Tarreau44e973f2018-03-01 17:49:30 +01003967 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 h2c->flags |= H2_CF_MUX_MALLOC;
3969 h2s->flags |= H2_SF_BLK_MROOM;
3970 goto end;
3971 }
3972
3973 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003974 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975 goto end;
3976
3977 chunk_reset(&outbuf);
3978
3979 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003980 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003981 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003982 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003983
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003984 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003985 break;
3986 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003987 /* If there are pending data in the output buffer, and we have
3988 * less than 1/4 of the mbuf's size and everything fits, we'll
3989 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3990 * is full and wait, to save some slow realign calls.
3991 */
3992 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3993 h2c->flags |= H2_CF_MUX_MFULL;
3994 h2s->flags |= H2_SF_BLK_MROOM;
3995 goto end;
3996 }
3997
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003998 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003999 }
4000
4001 if (outbuf.size < 9) {
4002 h2c->flags |= H2_CF_MUX_MFULL;
4003 h2s->flags |= H2_SF_BLK_MROOM;
4004 goto end;
4005 }
4006
4007 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004008 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4009 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4010 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004011
4012 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4013 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004014 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004015 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004016 break;
4017 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004018 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004019 if ((long long)size > h1m->curr_len)
4020 size = h1m->curr_len;
4021 break;
4022 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004023 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004024 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004025 if (!ret)
4026 goto end;
4027
4028 if (ret < 0) {
4029 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004030 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004031 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4032 goto end;
4033 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004034 max -= ret;
4035 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004036 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004037 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038 }
4039
Willy Tarreau801250e2018-09-11 11:45:04 +02004040 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004041 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004042 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 if (!ret)
4044 goto end;
4045
4046 if (ret < 0) {
4047 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004048 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4050 goto end;
4051 }
4052
4053 size = chunk;
4054 h1m->curr_len = chunk;
4055 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004056 max -= ret;
4057 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004058 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004059 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060 if (!size)
4061 goto send_empty;
4062 }
4063
4064 /* in MSG_DATA state, continue below */
4065 size = h1m->curr_len;
4066 break;
4067 }
4068
4069 /* we have in <size> the exact number of bytes we need to copy from
4070 * the H1 buffer. We need to check this against the connection's and
4071 * the stream's send windows, and to ensure that this fits in the max
4072 * frame size and in the buffer's available space minus 9 bytes (for
4073 * the frame header). The connection's flow control is applied last so
4074 * that we can use a separate list of streams which are immediately
4075 * unblocked on window opening. Note: we don't implement padding.
4076 */
4077
Willy Tarreau5dd17352018-06-14 13:33:30 +02004078 if (size > max)
4079 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004080
4081 if (size > h2s->mws)
4082 size = h2s->mws;
4083
4084 if (size <= 0) {
4085 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004086 if (h2s->send_wait) {
4087 LIST_DEL(&h2s->list);
4088 LIST_INIT(&h2s->list);
4089 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004090 goto end;
4091 }
4092
4093 if (h2c->mfs && size > h2c->mfs)
4094 size = h2c->mfs;
4095
4096 if (size + 9 > outbuf.size) {
4097 /* we have an opportunity for enlarging the too small
4098 * available space, let's try.
4099 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004100 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101 goto realign_again;
4102 size = outbuf.size - 9;
4103 }
4104
4105 if (size <= 0) {
4106 h2c->flags |= H2_CF_MUX_MFULL;
4107 h2s->flags |= H2_SF_BLK_MROOM;
4108 goto end;
4109 }
4110
4111 if (size > h2c->mws)
4112 size = h2c->mws;
4113
4114 if (size <= 0) {
4115 h2s->flags |= H2_SF_BLK_MFCTL;
4116 goto end;
4117 }
4118
4119 /* copy whatever we can */
4120 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004121 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004122 if (ret == 1)
4123 len2 = 0;
4124
4125 if (!ret || len1 + len2 < size) {
4126 /* FIXME: must normally never happen */
4127 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4128 goto end;
4129 }
4130
4131 /* limit len1/len2 to size */
4132 if (len1 + len2 > size) {
4133 int sub = len1 + len2 - size;
4134
4135 if (len2 > sub)
4136 len2 -= sub;
4137 else {
4138 sub -= len2;
4139 len2 = 0;
4140 len1 -= sub;
4141 }
4142 }
4143
4144 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004145 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004146 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004147 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004148
4149 send_empty:
4150 /* we may need to add END_STREAM */
4151 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4152 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004153 *
4154 * FIXME: what we do here is not correct because we send end_stream
4155 * before knowing if we'll have to send a HEADERS frame for the
4156 * trailers. More importantly we're not consuming the trailing CRLF
4157 * after the end of trailers, so it will be left to the caller to
4158 * eat it. The right way to do it would be to measure trailers here
4159 * and to send ES only if there are no trailers.
4160 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004161 */
4162 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004163 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004164 es_now = 1;
4165
4166 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004167 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004168
4169 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004170 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004171
4172 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004173 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004174
4175 /* consume incoming H1 response */
4176 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004177 max -= size;
4178 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004179 total += size;
4180 h1m->curr_len -= size;
4181 h2s->mws -= size;
4182 h2c->mws -= size;
4183
4184 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004185 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004186 goto new_frame;
4187 }
4188 }
4189
4190 if (es_now) {
4191 if (h2s->st == H2_SS_OPEN)
4192 h2s->st = H2_SS_HLOC;
4193 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004194 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004195
Willy Tarreau35a62702018-02-27 15:37:25 +01004196 if (!(h1m->flags & H1_MF_CHNK)) {
4197 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004198 total += max;
4199 ofs += max;
4200 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004201
Willy Tarreau801250e2018-09-11 11:45:04 +02004202 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004203 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004204
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004205 h2s->flags |= H2_SF_ES_SENT;
4206 }
4207
4208 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004209 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004210 return total;
4211}
4212
Willy Tarreau115e83b2018-12-01 19:17:53 +01004213/* Try to send a HEADERS frame matching HTX response present in HTX message
4214 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4215 * must check the stream's status to detect any error which might have happened
4216 * subsequently to a successful send. The htx blocks are automatically removed
4217 * from the message. The htx message is assumed to be valid since produced from
4218 * the internal code, hence it contains a start line, an optional series of
4219 * header blocks and an end of header, otherwise an invalid frame could be
4220 * emitted and the resulting htx message could be left in an inconsistent state.
4221 */
4222static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4223{
4224 struct http_hdr list[MAX_HTTP_HDR];
4225 struct h2c *h2c = h2s->h2c;
4226 struct htx_blk *blk;
4227 struct htx_blk *blk_end;
4228 struct buffer outbuf;
4229 struct htx_sl *sl;
4230 enum htx_blk_type type;
4231 int es_now = 0;
4232 int ret = 0;
4233 int hdr;
4234 int idx;
4235
4236 if (h2c_mux_busy(h2c, h2s)) {
4237 h2s->flags |= H2_SF_BLK_MBUSY;
4238 return 0;
4239 }
4240
4241 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4242 h2c->flags |= H2_CF_MUX_MALLOC;
4243 h2s->flags |= H2_SF_BLK_MROOM;
4244 return 0;
4245 }
4246
4247 /* determine the first block which must not be deleted, blk_end may
4248 * be NULL if all blocks have to be deleted.
4249 */
4250 idx = htx_get_head(htx);
4251 blk_end = NULL;
4252 while (idx != -1) {
4253 type = htx_get_blk_type(htx_get_blk(htx, idx));
4254 idx = htx_get_next(htx, idx);
4255 if (type == HTX_BLK_EOH) {
4256 if (idx != -1)
4257 blk_end = htx_get_blk(htx, idx);
4258 break;
4259 }
4260 }
4261
4262 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004263 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004264 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004265 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004266 if (h2s->status < 100 || h2s->status > 999)
4267 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004268
4269 /* and the rest of the headers, that we dump starting at header 0 */
4270 hdr = 0;
4271
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004272 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004273 while ((idx = htx_get_next(htx, idx)) != -1) {
4274 blk = htx_get_blk(htx, idx);
4275 type = htx_get_blk_type(blk);
4276
4277 if (type == HTX_BLK_UNUSED)
4278 continue;
4279
4280 if (type != HTX_BLK_HDR)
4281 break;
4282
4283 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4284 goto fail;
4285
4286 list[hdr].n = htx_get_blk_name(htx, blk);
4287 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004288 hdr++;
4289 }
4290
4291 /* marker for end of headers */
4292 list[hdr].n = ist("");
4293
4294 if (h2s->status == 204 || h2s->status == 304) {
4295 /* no contents, claim c-len is present and set to zero */
4296 es_now = 1;
4297 }
4298
4299 chunk_reset(&outbuf);
4300
4301 while (1) {
4302 outbuf.area = b_tail(&h2c->mbuf);
4303 outbuf.size = b_contig_space(&h2c->mbuf);
4304 outbuf.data = 0;
4305
4306 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4307 break;
4308 realign_again:
4309 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4310 }
4311
4312 if (outbuf.size < 9)
4313 goto full;
4314
4315 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4316 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4317 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4318 outbuf.data = 9;
4319
4320 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004321 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004322 if (b_space_wraps(&h2c->mbuf))
4323 goto realign_again;
4324 goto full;
4325 }
4326
4327 /* encode all headers, stop at empty name */
4328 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4329 /* these ones do not exist in H2 and must be dropped. */
4330 if (isteq(list[hdr].n, ist("connection")) ||
4331 isteq(list[hdr].n, ist("proxy-connection")) ||
4332 isteq(list[hdr].n, ist("keep-alive")) ||
4333 isteq(list[hdr].n, ist("upgrade")) ||
4334 isteq(list[hdr].n, ist("transfer-encoding")))
4335 continue;
4336
4337 if (isteq(list[hdr].n, ist("")))
4338 break; // end
4339
4340 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4341 /* output full */
4342 if (b_space_wraps(&h2c->mbuf))
4343 goto realign_again;
4344 goto full;
4345 }
4346 }
4347
4348 /* we may need to add END_STREAM.
4349 * FIXME: we should also set it when we know for sure that the
4350 * content-length is zero as well as on 204/304
4351 */
4352 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4353 es_now = 1;
4354
4355 if (h2s->cs->flags & CS_FL_SHW)
4356 es_now = 1;
4357
4358 /* update the frame's size */
4359 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4360
4361 if (es_now)
4362 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4363
4364 /* commit the H2 response */
4365 b_add(&h2c->mbuf, outbuf.data);
4366 h2s->flags |= H2_SF_HEADERS_SENT;
4367
Willy Tarreau115e83b2018-12-01 19:17:53 +01004368 if (es_now) {
4369 h2s->flags |= H2_SF_ES_SENT;
4370 if (h2s->st == H2_SS_OPEN)
4371 h2s->st = H2_SS_HLOC;
4372 else
4373 h2s_close(h2s);
4374 }
4375
4376 /* OK we could properly deliver the response */
4377
4378 /* remove all header blocks including the EOH and compute the
4379 * corresponding size.
4380 *
4381 * FIXME: We should remove everything when es_now is set.
4382 */
4383 ret = 0;
4384 idx = htx_get_head(htx);
4385 blk = htx_get_blk(htx, idx);
4386 while (blk != blk_end) {
4387 ret += htx_get_blksz(blk);
4388 blk = htx_remove_blk(htx, blk);
4389 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004390
4391 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4392 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004393 end:
4394 return ret;
4395 full:
4396 h2c->flags |= H2_CF_MUX_MFULL;
4397 h2s->flags |= H2_SF_BLK_MROOM;
4398 ret = 0;
4399 goto end;
4400 fail:
4401 /* unparsable HTX messages, too large ones to be produced in the local
4402 * list etc go here (unrecoverable errors).
4403 */
4404 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4405 ret = 0;
4406 goto end;
4407}
4408
Willy Tarreau80739692018-10-05 11:35:57 +02004409/* Try to send a HEADERS frame matching HTX request present in HTX message
4410 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4411 * must check the stream's status to detect any error which might have happened
4412 * subsequently to a successful send. The htx blocks are automatically removed
4413 * from the message. The htx message is assumed to be valid since produced from
4414 * the internal code, hence it contains a start line, an optional series of
4415 * header blocks and an end of header, otherwise an invalid frame could be
4416 * emitted and the resulting htx message could be left in an inconsistent state.
4417 */
4418static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4419{
4420 struct http_hdr list[MAX_HTTP_HDR];
4421 struct h2c *h2c = h2s->h2c;
4422 struct htx_blk *blk;
4423 struct htx_blk *blk_end;
4424 struct buffer outbuf;
4425 struct htx_sl *sl;
4426 struct ist meth, path;
4427 enum htx_blk_type type;
4428 int es_now = 0;
4429 int ret = 0;
4430 int hdr;
4431 int idx;
4432
4433 if (h2c_mux_busy(h2c, h2s)) {
4434 h2s->flags |= H2_SF_BLK_MBUSY;
4435 return 0;
4436 }
4437
4438 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4439 h2c->flags |= H2_CF_MUX_MALLOC;
4440 h2s->flags |= H2_SF_BLK_MROOM;
4441 return 0;
4442 }
4443
4444 /* determine the first block which must not be deleted, blk_end may
4445 * be NULL if all blocks have to be deleted.
4446 */
4447 idx = htx_get_head(htx);
4448 blk_end = NULL;
4449 while (idx != -1) {
4450 type = htx_get_blk_type(htx_get_blk(htx, idx));
4451 idx = htx_get_next(htx, idx);
4452 if (type == HTX_BLK_EOH) {
4453 if (idx != -1)
4454 blk_end = htx_get_blk(htx, idx);
4455 break;
4456 }
4457 }
4458
4459 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004460 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004461 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004462 meth = htx_sl_req_meth(sl);
4463 path = htx_sl_req_uri(sl);
4464
4465 /* and the rest of the headers, that we dump starting at header 0 */
4466 hdr = 0;
4467
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004468 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004469 while ((idx = htx_get_next(htx, idx)) != -1) {
4470 blk = htx_get_blk(htx, idx);
4471 type = htx_get_blk_type(blk);
4472
4473 if (type == HTX_BLK_UNUSED)
4474 continue;
4475
4476 if (type != HTX_BLK_HDR)
4477 break;
4478
4479 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4480 goto fail;
4481
4482 list[hdr].n = htx_get_blk_name(htx, blk);
4483 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004484 hdr++;
4485 }
4486
4487 /* marker for end of headers */
4488 list[hdr].n = ist("");
4489
4490 chunk_reset(&outbuf);
4491
4492 while (1) {
4493 outbuf.area = b_tail(&h2c->mbuf);
4494 outbuf.size = b_contig_space(&h2c->mbuf);
4495 outbuf.data = 0;
4496
4497 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4498 break;
4499 realign_again:
4500 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4501 }
4502
4503 if (outbuf.size < 9)
4504 goto full;
4505
4506 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4507 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4508 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4509 outbuf.data = 9;
4510
4511 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004512 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004513 if (b_space_wraps(&h2c->mbuf))
4514 goto realign_again;
4515 goto full;
4516 }
4517
4518 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004519 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4520 /* output full */
4521 if (b_space_wraps(&h2c->mbuf))
4522 goto realign_again;
4523 goto full;
4524 }
Willy Tarreau80739692018-10-05 11:35:57 +02004525
4526 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004527 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004528 /* output full */
4529 if (b_space_wraps(&h2c->mbuf))
4530 goto realign_again;
4531 goto full;
4532 }
4533
4534 /* encode all headers, stop at empty name */
4535 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4536 /* these ones do not exist in H2 and must be dropped. */
4537 if (isteq(list[hdr].n, ist("connection")) ||
4538 isteq(list[hdr].n, ist("proxy-connection")) ||
4539 isteq(list[hdr].n, ist("keep-alive")) ||
4540 isteq(list[hdr].n, ist("upgrade")) ||
4541 isteq(list[hdr].n, ist("transfer-encoding")))
4542 continue;
4543
4544 if (isteq(list[hdr].n, ist("")))
4545 break; // end
4546
4547 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4548 /* output full */
4549 if (b_space_wraps(&h2c->mbuf))
4550 goto realign_again;
4551 goto full;
4552 }
4553 }
4554
4555 /* we may need to add END_STREAM if we have no body :
4556 * - request already closed, or :
4557 * - no transfer-encoding, and :
4558 * - no content-length or content-length:0
4559 * Fixme: this doesn't take into account CONNECT requests.
4560 */
4561 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4562 es_now = 1;
4563
4564 if (sl->flags & HTX_SL_F_BODYLESS)
4565 es_now = 1;
4566
4567 if (h2s->cs->flags & CS_FL_SHW)
4568 es_now = 1;
4569
4570 /* update the frame's size */
4571 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4572
4573 if (es_now)
4574 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4575
4576 /* commit the H2 response */
4577 b_add(&h2c->mbuf, outbuf.data);
4578 h2s->flags |= H2_SF_HEADERS_SENT;
4579 h2s->st = H2_SS_OPEN;
4580
Willy Tarreau80739692018-10-05 11:35:57 +02004581 if (es_now) {
4582 // trim any possibly pending data (eg: inconsistent content-length)
4583 h2s->flags |= H2_SF_ES_SENT;
4584 h2s->st = H2_SS_HLOC;
4585 }
4586
4587 /* remove all header blocks including the EOH and compute the
4588 * corresponding size.
4589 *
4590 * FIXME: We should remove everything when es_now is set.
4591 */
4592 ret = 0;
4593 idx = htx_get_head(htx);
4594 blk = htx_get_blk(htx, idx);
4595 while (blk != blk_end) {
4596 ret += htx_get_blksz(blk);
4597 blk = htx_remove_blk(htx, blk);
4598 }
4599
4600 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4601 htx_remove_blk(htx, blk_end);
4602
4603 end:
4604 return ret;
4605 full:
4606 h2c->flags |= H2_CF_MUX_MFULL;
4607 h2s->flags |= H2_SF_BLK_MROOM;
4608 ret = 0;
4609 goto end;
4610 fail:
4611 /* unparsable HTX messages, too large ones to be produced in the local
4612 * list etc go here (unrecoverable errors).
4613 */
4614 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4615 ret = 0;
4616 goto end;
4617}
4618
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004619/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004620 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4621 * caller must check the stream's status to detect any error which might have
4622 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004623 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4624 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004625static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004626{
4627 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004628 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004629 struct buffer outbuf;
4630 size_t total = 0;
4631 int es_now = 0;
4632 int bsize; /* htx block size */
4633 int fsize; /* h2 frame size */
4634 struct htx_blk *blk;
4635 enum htx_blk_type type;
4636 int idx;
4637
4638 if (h2c_mux_busy(h2c, h2s)) {
4639 h2s->flags |= H2_SF_BLK_MBUSY;
4640 goto end;
4641 }
4642
4643 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4644 h2c->flags |= H2_CF_MUX_MALLOC;
4645 h2s->flags |= H2_SF_BLK_MROOM;
4646 goto end;
4647 }
4648
Willy Tarreau98de12a2018-12-12 07:03:00 +01004649 htx = htx_from_buf(buf);
4650
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004651 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4652 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4653 * the caller to handle.
4654 */
4655
4656 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004657 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004658 goto end;
4659
4660 idx = htx_get_head(htx);
4661 blk = htx_get_blk(htx, idx);
4662 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4663 bsize = htx_get_blksz(blk);
4664 fsize = bsize;
4665
4666 if (type == HTX_BLK_EOD) {
4667 /* if we have an EOD, we're dealing with chunked data. We may
4668 * have a set of trailers after us that the caller will want to
4669 * deal with. Let's simply remove the EOD and return.
4670 */
4671 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004672 total++; // EOD counts as one byte
4673 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004674 goto end;
4675 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004676 else if (type == HTX_BLK_EOM) {
4677 if (h2s->flags & H2_SF_ES_SENT) {
4678 /* ES already sent */
4679 htx_remove_blk(htx, blk);
4680 total++; // EOM counts as one byte
4681 count--;
4682 goto end;
4683 }
4684 }
4685 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004686 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004687
4688 /* Perform some optimizations to reduce the number of buffer copies.
4689 * First, if the mux's buffer is empty and the htx area contains
4690 * exactly one data block of the same size as the requested count, and
4691 * this count fits within the frame size, the stream's window size, and
4692 * the connection's window size, then it's possible to simply swap the
4693 * caller's buffer with the mux's output buffer and adjust offsets and
4694 * length to match the entire DATA HTX block in the middle. In this
4695 * case we perform a true zero-copy operation from end-to-end. This is
4696 * the situation that happens all the time with large files. Second, if
4697 * this is not possible, but the mux's output buffer is empty, we still
4698 * have an opportunity to avoid the copy to the intermediary buffer, by
4699 * making the intermediary buffer's area point to the output buffer's
4700 * area. In this case we want to skip the HTX header to make sure that
4701 * copies remain aligned and that this operation remains possible all
4702 * the time. This goes for headers, data blocks and any data extracted
4703 * from the HTX blocks.
4704 */
4705 if (unlikely(fsize == count &&
4706 htx->used == 1 && type == HTX_BLK_DATA &&
4707 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4708 void *old_area = h2c->mbuf.area;
4709
4710 if (b_data(&h2c->mbuf)) {
4711 /* too bad there are data left there. If we have less
4712 * than 1/4 of the mbuf's size and everything fits,
4713 * we'll perform a copy anyway. Otherwise we'll pretend
4714 * the mbuf is full and wait.
4715 */
4716 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4717 goto copy;
4718 h2c->flags |= H2_CF_MUX_MFULL;
4719 h2s->flags |= H2_SF_BLK_MROOM;
4720 goto end;
4721 }
4722
4723 /* map an H2 frame to the HTX block so that we can put the
4724 * frame header there.
4725 */
4726 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004727 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004728 h2c->mbuf.data = fsize + 9;
4729 outbuf.area = b_head(&h2c->mbuf);
4730
4731 /* prepend an H2 DATA frame header just before the DATA block */
4732 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4733 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4734 h2_set_frame_size(outbuf.area, fsize);
4735
4736 /* update windows */
4737 h2s->mws -= fsize;
4738 h2c->mws -= fsize;
4739
4740 /* and exchange with our old area */
4741 buf->area = old_area;
4742 buf->data = buf->head = 0;
4743 total += fsize;
4744 goto end;
4745 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004746
Willy Tarreau98de12a2018-12-12 07:03:00 +01004747 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004748 /* for DATA and EOM we'll have to emit a frame, even if empty */
4749
4750 while (1) {
4751 outbuf.area = b_tail(&h2c->mbuf);
4752 outbuf.size = b_contig_space(&h2c->mbuf);
4753 outbuf.data = 0;
4754
4755 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4756 break;
4757 realign_again:
4758 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4759 }
4760
4761 if (outbuf.size < 9) {
4762 h2c->flags |= H2_CF_MUX_MFULL;
4763 h2s->flags |= H2_SF_BLK_MROOM;
4764 goto end;
4765 }
4766
4767 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4768 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4769 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4770 outbuf.data = 9;
4771
4772 /* we have in <fsize> the exact number of bytes we need to copy from
4773 * the HTX buffer. We need to check this against the connection's and
4774 * the stream's send windows, and to ensure that this fits in the max
4775 * frame size and in the buffer's available space minus 9 bytes (for
4776 * the frame header). The connection's flow control is applied last so
4777 * that we can use a separate list of streams which are immediately
4778 * unblocked on window opening. Note: we don't implement padding.
4779 */
4780
4781 /* EOM is presented with bsize==1 but would lead to the emission of an
4782 * empty frame, thus we force it to zero here.
4783 */
4784 if (type == HTX_BLK_EOM)
4785 bsize = fsize = 0;
4786
4787 if (!fsize)
4788 goto send_empty;
4789
4790 if (h2s->mws <= 0) {
4791 h2s->flags |= H2_SF_BLK_SFCTL;
4792 if (h2s->send_wait) {
4793 LIST_DEL(&h2s->list);
4794 LIST_INIT(&h2s->list);
4795 }
4796 goto end;
4797 }
4798
Willy Tarreauee573762018-12-04 15:25:57 +01004799 if (fsize > count)
4800 fsize = count;
4801
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004802 if (fsize > h2s->mws)
4803 fsize = h2s->mws; // >0
4804
4805 if (h2c->mfs && fsize > h2c->mfs)
4806 fsize = h2c->mfs; // >0
4807
4808 if (fsize + 9 > outbuf.size) {
4809 /* we have an opportunity for enlarging the too small
4810 * available space, let's try.
4811 * FIXME: is this really interesting to do? Maybe we'll
4812 * spend lots of time realigning instead of using two
4813 * frames.
4814 */
4815 if (b_space_wraps(&h2c->mbuf))
4816 goto realign_again;
4817 fsize = outbuf.size - 9;
4818
4819 if (fsize <= 0) {
4820 /* no need to send an empty frame here */
4821 h2c->flags |= H2_CF_MUX_MFULL;
4822 h2s->flags |= H2_SF_BLK_MROOM;
4823 goto end;
4824 }
4825 }
4826
4827 if (h2c->mws <= 0) {
4828 h2s->flags |= H2_SF_BLK_MFCTL;
4829 goto end;
4830 }
4831
4832 if (fsize > h2c->mws)
4833 fsize = h2c->mws;
4834
4835 /* now let's copy this this into the output buffer */
4836 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004837 h2s->mws -= fsize;
4838 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004839 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004840
4841 send_empty:
4842 /* update the frame's size */
4843 h2_set_frame_size(outbuf.area, fsize);
4844
4845 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4846 * meeting EOM. We should optimize this later.
4847 */
4848 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004849 total++; // EOM counts as one byte
4850 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004851 es_now = 1;
4852 }
4853
4854 if (es_now)
4855 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4856
4857 /* commit the H2 response */
4858 b_add(&h2c->mbuf, fsize + 9);
4859
4860 /* consume incoming HTX block, including EOM */
4861 total += fsize;
4862 if (fsize == bsize) {
4863 htx_remove_blk(htx, blk);
4864 if (fsize)
4865 goto new_frame;
4866 } else {
4867 /* we've truncated this block */
4868 htx_cut_data_blk(htx, blk, fsize);
4869 }
4870
4871 if (es_now) {
4872 if (h2s->st == H2_SS_OPEN)
4873 h2s->st = H2_SS_HLOC;
4874 else
4875 h2s_close(h2s);
4876
4877 h2s->flags |= H2_SF_ES_SENT;
4878 }
4879
4880 end:
4881 return total;
4882}
4883
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004884/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4885 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4886 * processed. The caller must check the stream's status to detect any error
4887 * which might have happened subsequently to a successful send. The htx blocks
4888 * are automatically removed from the message. The htx message is assumed to be
4889 * valid since produced from the internal code. Processing stops when meeting
4890 * the EOM, which is also removed. All trailers are processed at once and sent
4891 * as a single frame. The ES flag is always set.
4892 */
4893static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4894{
4895 struct http_hdr list[MAX_HTTP_HDR];
4896 struct h2c *h2c = h2s->h2c;
4897 struct htx_blk *blk;
4898 struct htx_blk *blk_end;
4899 struct buffer outbuf;
4900 struct h1m h1m;
4901 enum htx_blk_type type;
4902 uint32_t size;
4903 int ret = 0;
4904 int hdr;
4905 int idx;
4906 void *start;
4907
4908 if (h2c_mux_busy(h2c, h2s)) {
4909 h2s->flags |= H2_SF_BLK_MBUSY;
4910 goto end;
4911 }
4912
4913 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4914 h2c->flags |= H2_CF_MUX_MALLOC;
4915 h2s->flags |= H2_SF_BLK_MROOM;
4916 goto end;
4917 }
4918
4919 /* The principle is that we parse each and every trailers block using
4920 * the H1 headers parser, and append it to the list. We don't proceed
4921 * until EOM is met. blk_end will point to the EOM block.
4922 */
4923 hdr = 0;
4924 memset(list, 0, sizeof(list));
4925 blk_end = NULL;
4926
4927 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4928 blk = htx_get_blk(htx, idx);
4929 type = htx_get_blk_type(blk);
4930
4931 if (type == HTX_BLK_UNUSED)
4932 continue;
4933
4934 if (type != HTX_BLK_TLR) {
4935 if (type == HTX_BLK_EOM)
4936 blk_end = blk;
4937 break;
4938 }
4939
4940 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4941 goto fail;
4942
4943 size = htx_get_blksz(blk);
4944 start = htx_get_blk_ptr(htx, blk);
4945
4946 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4947 h1m.err_pos = 0;
4948 ret = h1_headers_to_hdr_list(start, start + size,
4949 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4950 &h1m, NULL);
4951 if (ret < 0)
4952 goto fail;
4953
4954 /* ret == 0 if an incomplete trailers block was found (missing
4955 * empty line), or > 0 if it was found. We have to continue on
4956 * incomplete messages because the trailers block might be
4957 * incomplete.
4958 */
4959
4960 /* search the new end */
4961 while (hdr <= sizeof(list)/sizeof(list[0])) {
4962 if (!list[hdr].n.len)
4963 break;
4964 hdr++;
4965 }
4966 }
4967
4968 if (!blk_end)
4969 goto end; // end not found yet
4970
4971 if (!hdr)
4972 goto done;
4973
4974 chunk_reset(&outbuf);
4975
4976 while (1) {
4977 outbuf.area = b_tail(&h2c->mbuf);
4978 outbuf.size = b_contig_space(&h2c->mbuf);
4979 outbuf.data = 0;
4980
4981 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4982 break;
4983 realign_again:
4984 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4985 }
4986
4987 if (outbuf.size < 9)
4988 goto full;
4989
4990 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4991 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4992 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4993 outbuf.data = 9;
4994
4995 /* encode status, which necessarily is the first one */
4996 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
4997 if (b_space_wraps(&h2c->mbuf))
4998 goto realign_again;
4999 goto full;
5000 }
5001
5002 /* encode all headers */
5003 for (idx = 0; idx < hdr; idx++) {
5004 /* these ones do not exist in H2 or must not appear in
5005 * trailers and must be dropped.
5006 */
5007 if (isteq(list[idx].n, ist("host")) ||
5008 isteq(list[idx].n, ist("content-length")) ||
5009 isteq(list[idx].n, ist("connection")) ||
5010 isteq(list[idx].n, ist("proxy-connection")) ||
5011 isteq(list[idx].n, ist("keep-alive")) ||
5012 isteq(list[idx].n, ist("upgrade")) ||
5013 isteq(list[idx].n, ist("te")) ||
5014 isteq(list[idx].n, ist("transfer-encoding")))
5015 continue;
5016
5017 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5018 /* output full */
5019 if (b_space_wraps(&h2c->mbuf))
5020 goto realign_again;
5021 goto full;
5022 }
5023 }
5024
5025 /* update the frame's size */
5026 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5027
5028 /* commit the H2 response */
5029 b_add(&h2c->mbuf, outbuf.data);
5030 h2s->flags |= H2_SF_ES_SENT;
5031
5032 if (h2s->st == H2_SS_OPEN)
5033 h2s->st = H2_SS_HLOC;
5034 else
5035 h2s_close(h2s);
5036
5037 /* OK we could properly deliver the response */
5038 done:
5039 /* remove all header blocks including EOM and compute the corresponding size. */
5040 ret = 0;
5041 idx = htx_get_head(htx);
5042 blk = htx_get_blk(htx, idx);
5043 while (blk != blk_end) {
5044 ret += htx_get_blksz(blk);
5045 blk = htx_remove_blk(htx, blk);
5046 }
5047 blk = htx_remove_blk(htx, blk);
5048 end:
5049 return ret;
5050 full:
5051 h2c->flags |= H2_CF_MUX_MFULL;
5052 h2s->flags |= H2_SF_BLK_MROOM;
5053 ret = 0;
5054 goto end;
5055 fail:
5056 /* unparsable HTX messages, too large ones to be produced in the local
5057 * list etc go here (unrecoverable errors).
5058 */
5059 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5060 ret = 0;
5061 goto end;
5062}
5063
Olivier Houchard6ff20392018-07-17 18:46:31 +02005064/* Called from the upper layer, to subscribe to events, such as being able to send */
5065static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5066{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005067 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005068 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005069 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005070
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005071 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005072 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005073 if (!(sw->events & SUB_RETRY_RECV)) {
5074 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005075 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005076 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005077 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005078 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005079 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005080 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005081 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005082 if (!(sw->events & SUB_RETRY_SEND)) {
5083 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005084 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005085 h2s->send_wait = sw;
5086 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5087 if (h2s->flags & H2_SF_BLK_MFCTL)
5088 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5089 else
5090 LIST_ADDQ(&h2c->send_list, &h2s->list);
5091 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005092 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005093 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005094 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005095 if (event_type != 0)
5096 return -1;
5097 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005098
5099
5100}
5101
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005102static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5103{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005104 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005105 struct h2s *h2s = cs->ctx;
5106
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005107 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005108 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005109 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005110 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005111 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005112 }
5113 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005114 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005115 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005116 if (h2s->send_wait == sw) {
5117 LIST_DEL(&h2s->list);
5118 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005119 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005120 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005121 }
5122 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005123 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5124 sw = param;
5125 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005126 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005127 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005128 LIST_DEL(&h2s->list);
5129 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005130 }
5131 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005132 return 0;
5133}
5134
5135
Olivier Houchard511efea2018-08-16 15:30:32 +02005136/* Called from the upper layer, to receive data */
5137static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5138{
Olivier Houchard638b7992018-08-16 15:41:52 +02005139 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005140 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005141 struct htx *h2s_htx = NULL;
5142 struct htx *buf_htx = NULL;
5143 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005144 size_t ret = 0;
5145
5146 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005147 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5148 /* in HTX mode we ignore the count argument */
5149 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005150 if (htx_is_empty(h2s_htx)) {
5151 if (cs->flags & CS_FL_REOS)
5152 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005153 if (cs->flags & CS_FL_ERR_PENDING)
5154 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01005155 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005156 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005157
5158 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005159 count = htx_free_data_space(buf_htx);
5160 if (flags & CO_RFL_KEEP_RSV) {
5161 if (count <= global.tune.maxrewrite)
5162 goto end;
5163 count -= global.tune.maxrewrite;
5164 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005165
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005166 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01005167
5168 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005169 htx_to_buf(buf_htx, buf);
5170 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005171 ret = htx_ret.ret;
5172 }
5173 else {
5174 ret = b_xfer(buf, &h2s->rxbuf, count);
5175 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005176
Olivier Houchard638b7992018-08-16 15:41:52 +02005177 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005178 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005179 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005180 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005181 if (cs->flags & CS_FL_REOS)
5182 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005183 if (cs->flags & CS_FL_ERR_PENDING)
5184 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005185 if (b_size(&h2s->rxbuf)) {
5186 b_free(&h2s->rxbuf);
5187 offer_buffers(NULL, tasks_run_queue);
5188 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005189 }
5190
Willy Tarreau082f5592018-11-25 08:03:32 +01005191 if (ret && h2c->dsi == h2s->id) {
5192 /* demux is blocking on this stream's buffer */
5193 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005194 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005195 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005196end:
Olivier Houchard511efea2018-08-16 15:30:32 +02005197 return ret;
5198}
5199
Olivier Houchardd846c262018-10-19 17:24:29 +02005200static void h2_stop_senders(struct h2c *h2c)
5201{
5202 struct h2s *h2s, *h2s_back;
5203
5204 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5205 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5206 if (h2c->msi == h2s_id(h2s))
5207 continue;
5208 LIST_DEL(&h2s->list);
5209 LIST_INIT(&h2s->list);
5210 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005211 h2s->send_wait->events |= SUB_RETRY_SEND;
5212 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005213 LIST_ADD(&h2c->send_list, &h2s->list);
5214 }
5215}
5216
Willy Tarreau62f52692017-10-08 23:01:42 +02005217/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005218static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005219{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005220 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005221 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005222 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005223 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005224 struct htx *htx;
5225 struct htx_blk *blk;
5226 enum htx_blk_type btype;
5227 uint32_t bsize;
5228 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005229
Olivier Houchardd846c262018-10-19 17:24:29 +02005230 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005231 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005232 h2s->send_wait = NULL;
5233 LIST_DEL(&h2s->list);
5234 LIST_INIT(&h2s->list);
5235 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005236 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5237 return 0;
5238
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005239 /* htx will be enough to decide if we're using HTX or legacy */
5240 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5241
Willy Tarreau0bad0432018-06-14 16:54:01 +02005242 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005243 h2s->flags |= H2_SF_OUTGOING_DATA;
5244
Willy Tarreau751f2d02018-10-05 09:35:00 +02005245 if (h2s->id == 0) {
5246 int32_t id = h2c_get_next_sid(h2s->h2c);
5247
5248 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005249 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005250 return 0;
5251 }
5252
5253 eb32_delete(&h2s->by_id);
5254 h2s->by_id.key = h2s->id = id;
5255 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005256 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005257 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5258 }
5259
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005260 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005261 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5262 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005263 idx = htx_get_head(htx);
5264 blk = htx_get_blk(htx, idx);
5265 btype = htx_get_blk_type(blk);
5266 bsize = htx_get_blksz(blk);
5267
5268 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005269 case HTX_BLK_REQ_SL:
5270 /* start-line before headers */
5271 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5272 if (ret > 0) {
5273 total += ret;
5274 count -= ret;
5275 if (ret < bsize)
5276 goto done;
5277 }
5278 break;
5279
Willy Tarreau115e83b2018-12-01 19:17:53 +01005280 case HTX_BLK_RES_SL:
5281 /* start-line before headers */
5282 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5283 if (ret > 0) {
5284 total += ret;
5285 count -= ret;
5286 if (ret < bsize)
5287 goto done;
5288 }
5289 break;
5290
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005291 case HTX_BLK_DATA:
5292 case HTX_BLK_EOD:
5293 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005294 /* all these cause the emission of a DATA frame (possibly empty).
5295 * This EOM necessarily is one before trailers, as the EOM following
5296 * trailers would have been consumed by the trailers parser.
5297 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005298 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005299 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005300 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005301 total += ret;
5302 count -= ret;
5303 if (ret < bsize)
5304 goto done;
5305 }
5306 break;
5307
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005308 case HTX_BLK_TLR:
5309 /* This is the first trailers block, all the subsequent ones AND
5310 * the EOM will be swallowed by the parser.
5311 */
5312 ret = h2s_htx_make_trailers(h2s, htx);
5313 if (ret > 0) {
5314 total += ret;
5315 count -= ret;
5316 if (ret < bsize)
5317 goto done;
5318 }
5319 break;
5320
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005321 default:
5322 htx_remove_blk(htx, blk);
5323 total += bsize;
5324 count -= bsize;
5325 break;
5326 }
5327 }
5328 goto done;
5329 }
5330
5331 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005332 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005333 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005334 if (h2s->h2c->flags & H2_CF_IS_BACK)
5335 ret = -1;
5336 else
5337 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005338 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005339 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005340 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005341 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005342 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005343 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005344 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005345
Willy Tarreau5dd17352018-06-14 13:33:30 +02005346 if (unlikely((int)ret <= 0)) {
5347 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005348 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5349 break;
5350 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005351 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005352 total += count;
5353 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005354 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005355 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005356 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005357 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005358 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005359 break;
5360 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005361
5362 total += ret;
5363 count -= ret;
5364
5365 if (h2s->st >= H2_SS_ERROR)
5366 break;
5367
5368 if (h2s->flags & H2_SF_BLK_ANY)
5369 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005370 }
5371
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005372 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005373 if (h2s->st >= H2_SS_ERROR) {
5374 /* trim any possibly pending data after we close (extra CR-LF,
5375 * unprocessed trailers, abnormal extra data, ...)
5376 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005377 total += count;
5378 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005379 }
5380
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005381 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005382 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005383 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005384 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005385 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005386 }
5387
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005388 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005389 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005390 } else {
5391 b_del(buf, total);
5392 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005393
5394 /* The mux is full, cancel the pending tasks */
5395 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5396 (h2s->flags & H2_SF_BLK_MBUSY))
5397 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005398
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005399 /* If we're running HTX, and we read the whole buffer, then pretend
5400 * we read exactly what the caller specified, as with HTX the caller
5401 * will always give the buffer size, instead of the amount of data
5402 * available.
5403 */
5404 if (htx && !b_data(buf))
5405 total = orig_count;
5406
Olivier Houchard7505f942018-08-21 18:10:44 +02005407 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005408 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005409 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005410
Olivier Houchard7505f942018-08-21 18:10:44 +02005411 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005412 /* If we're waiting for flow control, and we got a shutr on the
5413 * connection, we will never be unlocked, so add an error on
5414 * the conn_stream.
5415 */
5416 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5417 !b_data(&h2s->h2c->dbuf) &&
5418 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5419 if (cs->flags & CS_FL_EOS)
5420 cs->flags |= CS_FL_ERROR;
5421 else
5422 cs->flags |= CS_FL_ERR_PENDING;
5423 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005424 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005425}
5426
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005427/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005428static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005429{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005430 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005431 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005432 struct eb32_node *node;
5433 int fctl_cnt = 0;
5434 int send_cnt = 0;
5435 int tree_cnt = 0;
5436 int orph_cnt = 0;
5437
5438 if (!h2c)
5439 return;
5440
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005441 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005442 fctl_cnt++;
5443
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005444 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005445 send_cnt++;
5446
Willy Tarreau3af37712018-12-18 14:34:41 +01005447 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005448 node = eb32_first(&h2c->streams_by_id);
5449 while (node) {
5450 h2s = container_of(node, struct h2s, by_id);
5451 tree_cnt++;
5452 if (!h2s->cs)
5453 orph_cnt++;
5454 node = eb32_next(node);
5455 }
5456
Willy Tarreau987c0632018-12-18 10:32:05 +01005457 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5458 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5459 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005460 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5461 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005462 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005463 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5464 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5465 h2c->msi,
5466 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5467 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5468
5469 if (h2s) {
5470 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5471 h2s, h2s->id, h2s->flags,
5472 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5473 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5474 h2s->cs);
5475 if (h2s->cs)
5476 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5477 h2s->cs->flags, h2s->cs->data);
5478 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005479}
Willy Tarreau62f52692017-10-08 23:01:42 +02005480
5481/*******************************************************/
5482/* functions below are dedicated to the config parsers */
5483/*******************************************************/
5484
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005485/* config parser for global "tune.h2.header-table-size" */
5486static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5487 struct proxy *defpx, const char *file, int line,
5488 char **err)
5489{
5490 if (too_many_args(1, args, err, NULL))
5491 return -1;
5492
5493 h2_settings_header_table_size = atoi(args[1]);
5494 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5495 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5496 return -1;
5497 }
5498 return 0;
5499}
Willy Tarreau62f52692017-10-08 23:01:42 +02005500
Willy Tarreaue6baec02017-07-27 11:45:11 +02005501/* config parser for global "tune.h2.initial-window-size" */
5502static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5503 struct proxy *defpx, const char *file, int line,
5504 char **err)
5505{
5506 if (too_many_args(1, args, err, NULL))
5507 return -1;
5508
5509 h2_settings_initial_window_size = atoi(args[1]);
5510 if (h2_settings_initial_window_size < 0) {
5511 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5512 return -1;
5513 }
5514 return 0;
5515}
5516
Willy Tarreau5242ef82017-07-27 11:47:28 +02005517/* config parser for global "tune.h2.max-concurrent-streams" */
5518static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5519 struct proxy *defpx, const char *file, int line,
5520 char **err)
5521{
5522 if (too_many_args(1, args, err, NULL))
5523 return -1;
5524
5525 h2_settings_max_concurrent_streams = atoi(args[1]);
5526 if (h2_settings_max_concurrent_streams < 0) {
5527 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5528 return -1;
5529 }
5530 return 0;
5531}
5532
Willy Tarreau62f52692017-10-08 23:01:42 +02005533
5534/****************************************/
5535/* MUX initialization and instanciation */
5536/***************************************/
5537
5538/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005539static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005540 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005541 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005542 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005543 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005544 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005545 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005546 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005547 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005548 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005549 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005550 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005551 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005552 .shutr = h2_shutr,
5553 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005554 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005555 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005556 .name = "H2",
5557};
5558
Christopher Faulet32f61c02018-04-10 14:33:41 +02005559/* PROTO selection : this mux registers PROTO token "h2" */
5560static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005561 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005562
Willy Tarreau0108d902018-11-25 19:14:37 +01005563INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5564
Willy Tarreauf8957272018-10-03 10:25:20 +02005565static struct mux_proto_list mux_proto_h2_htx =
5566 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5567
5568INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5569
Willy Tarreau62f52692017-10-08 23:01:42 +02005570/* config keyword parsers */
5571static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005572 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005573 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005574 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005575 { 0, NULL, NULL }
5576}};
5577
Willy Tarreau0108d902018-11-25 19:14:37 +01005578INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);