blob: 2972ca296bf296e9858d60b9f073d11448918d80 [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
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100418 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100419 ret1 = h2_settings_max_concurrent_streams - h2c->nb_streams;
420
421 /* we must also consider the limit imposed by stream IDs */
422 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100423 ret1 = MIN(ret1, ret2);
424 if (ret1 && srv && srv->max_reuse >= 0) {
425 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
426 ret1 = MIN(ret1, ret2);
427 }
428 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100429}
430
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100431static int h2_max_streams(struct connection *conn)
432{
433 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
434 return h2_settings_max_concurrent_streams;
435}
436
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200437
Willy Tarreau62f52692017-10-08 23:01:42 +0200438/*****************************************************************/
439/* functions below are dedicated to the mux setup and management */
440/*****************************************************************/
441
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200442/* Initialize the mux once it's attached. For outgoing connections, the context
443 * is already initialized before installing the mux, so we detect incoming
444 * connections from the fact that the context is still NULL. Returns < 0 on
445 * error.
446 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100447static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448{
449 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100450 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200451
Willy Tarreaubafbe012017-11-24 17:34:44 +0100452 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200453 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200454 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100456 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200457 h2c->flags = H2_CF_IS_BACK;
458 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
459 if (tick_isset(prx->timeout.serverfin))
460 h2c->shut_timeout = prx->timeout.serverfin;
461 } else {
462 h2c->flags = H2_CF_NONE;
463 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
464 if (tick_isset(prx->timeout.clientfin))
465 h2c->shut_timeout = prx->timeout.clientfin;
466 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100467
Willy Tarreau0b37d652018-10-03 10:33:02 +0200468 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100469 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100470 if (tick_isset(h2c->timeout)) {
471 t = task_new(tid_bit);
472 if (!t)
473 goto fail;
474
475 h2c->task = t;
476 t->process = h2_timeout_task;
477 t->context = h2c;
478 t->expire = tick_add(now_ms, h2c->timeout);
479 }
Willy Tarreauea392822017-10-31 10:02:25 +0100480
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200481 h2c->wait_event.task = tasklet_new();
482 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200483 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200484 h2c->wait_event.task->process = h2_io_cb;
485 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100486 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200487
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
489 if (!h2c->ddht)
490 goto fail;
491
492 /* Initialise the context. */
493 h2c->st0 = H2_CS_PREFACE;
494 h2c->conn = conn;
495 h2c->max_id = -1;
496 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100497 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200498 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100499 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200500 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100501 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100502 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200503
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200504 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200505 h2c->dsi = -1;
506 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100507
Willy Tarreau32218eb2017-09-22 08:07:25 +0200508 h2c->last_sid = -1;
509
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200510 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200511 h2c->miw = 65535; /* mux initial window size */
512 h2c->mws = 65535; /* mux window size */
513 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200514 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200515 LIST_INIT(&h2c->send_list);
516 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200517 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100518 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200519
Willy Tarreau3f133572017-10-31 19:21:06 +0100520 if (t)
521 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100522
Willy Tarreau01b44822018-10-03 14:26:37 +0200523 if (h2c->flags & H2_CF_IS_BACK) {
524 /* FIXME: this is temporary, for outgoing connections we need
525 * to immediately allocate a stream until the code is modified
526 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100527 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200528 */
529 struct h2s *h2s;
530
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100531 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200532 if (!h2s)
533 goto fail_stream;
534 }
535
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100536 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200537
Willy Tarreau0f383582018-10-03 14:22:21 +0200538 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100539 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200540 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200541 fail_stream:
542 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200543 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100544 if (t)
545 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200546 if (h2c->wait_event.task)
547 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100548 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200549 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550 return -1;
551}
552
Willy Tarreau751f2d02018-10-05 09:35:00 +0200553/* returns the next allocatable outgoing stream ID for the H2 connection, or
554 * -1 if no more is allocatable.
555 */
556static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
557{
558 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100559
560 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200561 id = -1;
562 return id;
563}
564
Willy Tarreau2373acc2017-10-12 17:35:14 +0200565/* returns the stream associated with id <id> or NULL if not found */
566static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
567{
568 struct eb32_node *node;
569
Willy Tarreau751f2d02018-10-05 09:35:00 +0200570 if (id == 0)
571 return (struct h2s *)h2_closed_stream;
572
Willy Tarreau2a856182017-05-16 15:20:39 +0200573 if (id > h2c->max_id)
574 return (struct h2s *)h2_idle_stream;
575
Willy Tarreau2373acc2017-10-12 17:35:14 +0200576 node = eb32_lookup(&h2c->streams_by_id, id);
577 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200578 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200579
580 return container_of(node, struct h2s, by_id);
581}
582
Willy Tarreau62f52692017-10-08 23:01:42 +0200583/* release function for a connection. This one should be called to free all
584 * resources allocated to the mux.
585 */
586static void h2_release(struct connection *conn)
587{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100588 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200589
590 LIST_DEL(&conn->list);
591
592 if (h2c) {
593 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200594
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100595 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100596 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100597 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200598
Willy Tarreau44e973f2018-03-01 17:49:30 +0100599 h2_release_buf(h2c, &h2c->dbuf);
600 h2_release_buf(h2c, &h2c->mbuf);
601
Willy Tarreauea392822017-10-31 10:02:25 +0100602 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200603 h2c->task->context = NULL;
604 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100605 h2c->task = NULL;
606 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200607 if (h2c->wait_event.task)
608 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100609 if (h2c->wait_event.events != 0)
610 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200611 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100612
Willy Tarreaubafbe012017-11-24 17:34:44 +0100613 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200614 }
615
616 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100617 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200618
619 conn_stop_tracking(conn);
620 conn_full_close(conn);
621 if (conn->destroy_cb)
622 conn->destroy_cb(conn);
623 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200624}
625
626
Willy Tarreau71681172017-10-23 14:39:06 +0200627/******************************************************/
628/* functions below are for the H2 protocol processing */
629/******************************************************/
630
631/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100632static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200633{
634 return h2s ? h2s->id : 0;
635}
636
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200637/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100638static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200639{
640 if (h2c->msi < 0)
641 return 0;
642
643 if (h2c->msi == h2s_id(h2s))
644 return 0;
645
646 return 1;
647}
648
Willy Tarreau741d6df2017-10-17 08:00:59 +0200649/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100650static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200651{
652 h2c->errcode = err;
653 h2c->st0 = H2_CS_ERROR;
654}
655
Willy Tarreau175cebb2019-01-24 10:02:24 +0100656/* marks an error on the stream. It may also update an already closed stream
657 * (e.g. to report an error after an RST was received).
658 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100659static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200660{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100661 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200662 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100663 if (h2s->st < H2_SS_ERROR)
664 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100665 if (h2s->cs)
666 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200667 }
668}
669
Willy Tarreau7e094452018-12-19 18:08:52 +0100670/* attempt to notify the data layer of recv availability */
671static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
672{
673 struct wait_event *sw;
674
675 if (h2s->recv_wait) {
676 sw = h2s->recv_wait;
677 sw->events &= ~SUB_RETRY_RECV;
678 tasklet_wakeup(sw->task);
679 h2s->recv_wait = NULL;
680 }
681}
682
683/* attempt to notify the data layer of send availability */
684static void __maybe_unused h2s_notify_send(struct h2s *h2s)
685{
686 struct wait_event *sw;
687
688 if (h2s->send_wait) {
689 sw = h2s->send_wait;
690 sw->events &= ~SUB_RETRY_SEND;
691 tasklet_wakeup(sw->task);
692 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100693 LIST_DEL(&h2s->list);
694 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100695 }
696}
697
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100698/* alerts the data layer, trying to wake it up by all means, following
699 * this sequence :
700 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
701 * - if its subscribed to send, then it's woken up for send
702 * - if it was subscribed to neither, its ->wake() callback is called
703 * It is safe to call this function with a closed stream which doesn't have a
704 * conn_stream anymore.
705 */
706static void __maybe_unused h2s_alert(struct h2s *h2s)
707{
708 if (h2s->recv_wait || h2s->send_wait) {
709 h2s_notify_recv(h2s);
710 h2s_notify_send(h2s);
711 }
712 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
713 h2s->cs->data_cb->wake(h2s->cs);
714}
715
Willy Tarreaue4820742017-07-27 13:37:23 +0200716/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100717static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200718{
719 uint8_t *out = frame;
720
721 *out = len >> 16;
722 write_n16(out + 1, len);
723}
724
Willy Tarreau54c15062017-10-10 17:10:03 +0200725/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
726 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
727 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200728 * available in the buffer's input prior to calling this function. The buffer
729 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200730 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100731static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200732 const struct buffer *b, int o)
733{
Willy Tarreau591d4452018-06-15 17:21:00 +0200734 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 +0200735}
736
Willy Tarreau1f094672017-11-20 21:27:45 +0100737static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200738{
Willy Tarreau591d4452018-06-15 17:21:00 +0200739 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200740}
741
Willy Tarreau1f094672017-11-20 21:27:45 +0100742static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200743{
Willy Tarreau591d4452018-06-15 17:21:00 +0200744 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200745}
746
Willy Tarreau1f094672017-11-20 21:27:45 +0100747static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200748{
Willy Tarreau591d4452018-06-15 17:21:00 +0200749 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200750}
751
752
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100753/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
754 * The algorithm is not obvious. It turns out that H2 headers are neither
755 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
756 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200757 *
758 * b0 b1 b2 b3 b4 b5..b8
759 * +----------+---------+--------+----+----+----------------------+
760 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
761 * +----------+---------+--------+----+----+----------------------+
762 *
763 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
764 * we get the sid properly aligned and ordered, and 16 bits of len properly
765 * ordered as well. The type and flags can be extracted using bit shifts from
766 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200767 * Returns zero if some bytes are missing, otherwise non-zero on success. The
768 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200769 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100770static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200771{
772 uint64_t w;
773
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100774 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200775 return 0;
776
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100777 w = h2_get_n64(b, o + 1);
778 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200779 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
780 h->ff = w >> 32;
781 h->ft = w >> 40;
782 h->len += w >> 48;
783 return 1;
784}
785
786/* skip the next 9 bytes corresponding to the frame header possibly parsed by
787 * h2_peek_frame_hdr() above.
788 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100789static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200790{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200791 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200792}
793
794/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100795static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200796{
797 int ret;
798
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100799 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200800 if (ret > 0)
801 h2_skip_frame_hdr(b);
802 return ret;
803}
804
Willy Tarreau00dd0782018-03-01 16:31:34 +0100805/* marks stream <h2s> as CLOSED and decrement the number of active streams for
806 * its connection if the stream was not yet closed. Please use this exclusively
807 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100808 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100809static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100810{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100811 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100812 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100813 if (!h2s->id)
814 h2s->h2c->nb_reserved--;
815 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100816 h2s->st = H2_SS_CLOSED;
817}
818
Willy Tarreau71049cc2018-03-28 13:56:39 +0200819/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
820static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100821{
822 h2s_close(h2s);
823 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200824 if (b_size(&h2s->rxbuf)) {
825 b_free(&h2s->rxbuf);
826 offer_buffers(NULL, tasks_run_queue);
827 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200828 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100829 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200830 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100831 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800832 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200833 * reference left would be in the h2c send_list/fctl_list, and if
834 * we're in it, we're getting out anyway
835 */
836 LIST_DEL(&h2s->list);
837 LIST_INIT(&h2s->list);
838 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100839 pool_free(pool_head_h2s, h2s);
840}
841
Willy Tarreaua8e49542018-10-03 18:53:55 +0200842/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
843 * stream tree. In case of error, nothing is added and NULL is returned. The
844 * causes of errors can be any failed memory allocation. The caller is
845 * responsible for checking if the connection may support an extra stream
846 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200847 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200848static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200849{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200850 struct h2s *h2s;
851
Willy Tarreaubafbe012017-11-24 17:34:44 +0100852 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200853 if (!h2s)
854 goto out;
855
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200856 h2s->wait_event.task = tasklet_new();
857 if (!h2s->wait_event.task) {
858 pool_free(pool_head_h2s, h2s);
859 goto out;
860 }
861 h2s->send_wait = NULL;
862 h2s->recv_wait = NULL;
863 h2s->wait_event.task->process = h2_deferred_shut;
864 h2s->wait_event.task->context = h2s;
865 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100866 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200867 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200868 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200869 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200870 h2s->mws = h2c->miw;
871 h2s->flags = H2_SF_NONE;
872 h2s->errcode = H2_ERR_NO_ERROR;
873 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200874 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100875 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200876 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200877
878 if (h2c->flags & H2_CF_IS_BACK) {
879 h1m_init_req(&h2s->h1m);
880 h2s->h1m.err_pos = -1; // don't care about errors on the request path
881 h2s->h1m.flags |= H1_MF_TOLOWER;
882 } else {
883 h1m_init_res(&h2s->h1m);
884 h2s->h1m.err_pos = -1; // don't care about errors on the response path
885 h2s->h1m.flags |= H1_MF_TOLOWER;
886 }
887
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200888 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200889 if (id > 0)
890 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100891 else
892 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893
894 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100895 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100896 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200897
898 return h2s;
899
900 out_free_h2s:
901 pool_free(pool_head_h2s, h2s);
902 out:
903 return NULL;
904}
905
906/* creates a new stream <id> on the h2c connection and returns it, or NULL in
907 * case of memory allocation error.
908 */
909static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
910{
911 struct session *sess = h2c->conn->owner;
912 struct conn_stream *cs;
913 struct h2s *h2s;
914
915 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
916 goto out;
917
918 h2s = h2s_new(h2c, id);
919 if (!h2s)
920 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200921
922 cs = cs_new(h2c->conn);
923 if (!cs)
924 goto out_close;
925
Olivier Houchard746fb772018-12-15 19:42:00 +0100926 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200927 h2s->cs = cs;
928 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200929 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200930
931 if (stream_create_from_cs(cs) < 0)
932 goto out_free_cs;
933
Willy Tarreau590a0512018-09-05 11:56:48 +0200934 /* We want the accept date presented to the next stream to be the one
935 * we have now, the handshake time to be null (since the next stream
936 * is not delayed by a handshake), and the idle time to count since
937 * right now.
938 */
939 sess->accept_date = date;
940 sess->tv_accept = now;
941 sess->t_handshake = 0;
942
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200943 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200944 if (h2_has_too_many_cs(h2c))
945 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200946 return h2s;
947
948 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200949 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200950 cs_free(cs);
951 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200952 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200953 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200954 sess_log(sess);
955 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200956}
957
Willy Tarreau751f2d02018-10-05 09:35:00 +0200958/* allocates a new stream associated to conn_stream <cs> on the h2c connection
959 * and returns it, or NULL in case of memory allocation error or if the highest
960 * possible stream ID was reached.
961 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100962static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200963{
964 struct h2s *h2s = NULL;
965
966 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
967 goto out;
968
Willy Tarreaua80dca82019-01-24 17:08:28 +0100969 if (h2_streams_left(h2c) < 1)
970 goto out;
971
Willy Tarreau751f2d02018-10-05 09:35:00 +0200972 /* Defer choosing the ID until we send the first message to create the stream */
973 h2s = h2s_new(h2c, 0);
974 if (!h2s)
975 goto out;
976
977 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100978 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200979 cs->ctx = h2s;
980 h2c->nb_cs++;
981
Willy Tarreau751f2d02018-10-05 09:35:00 +0200982 out:
983 return h2s;
984}
985
Willy Tarreaube5b7152017-09-25 16:25:39 +0200986/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
987 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
988 * the various settings codes.
989 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200990static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200991{
992 struct buffer *res;
993 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200994 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200995 int ret;
996
997 if (h2c_mux_busy(h2c, NULL)) {
998 h2c->flags |= H2_CF_DEM_MBUSY;
999 return 0;
1000 }
1001
Willy Tarreau44e973f2018-03-01 17:49:30 +01001002 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001003 if (!res) {
1004 h2c->flags |= H2_CF_MUX_MALLOC;
1005 h2c->flags |= H2_CF_DEM_MROOM;
1006 return 0;
1007 }
1008
1009 chunk_init(&buf, buf_data, sizeof(buf_data));
1010 chunk_memcpy(&buf,
1011 "\x00\x00\x00" /* length : 0 for now */
1012 "\x04\x00" /* type : 4 (settings), flags : 0 */
1013 "\x00\x00\x00\x00", /* stream ID : 0 */
1014 9);
1015
1016 if (h2_settings_header_table_size != 4096) {
1017 char str[6] = "\x00\x01"; /* header_table_size */
1018
1019 write_n32(str + 2, h2_settings_header_table_size);
1020 chunk_memcat(&buf, str, 6);
1021 }
1022
1023 if (h2_settings_initial_window_size != 65535) {
1024 char str[6] = "\x00\x04"; /* initial_window_size */
1025
1026 write_n32(str + 2, h2_settings_initial_window_size);
1027 chunk_memcat(&buf, str, 6);
1028 }
1029
1030 if (h2_settings_max_concurrent_streams != 0) {
1031 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1032
1033 /* Note: 0 means "unlimited" for haproxy's config but not for
1034 * the protocol, so never send this value!
1035 */
1036 write_n32(str + 2, h2_settings_max_concurrent_streams);
1037 chunk_memcat(&buf, str, 6);
1038 }
1039
1040 if (global.tune.bufsize != 16384) {
1041 char str[6] = "\x00\x05"; /* max_frame_size */
1042
1043 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1044 * match bufsize - rewrite size, but at the moment it seems
1045 * that clients don't take care of it.
1046 */
1047 write_n32(str + 2, global.tune.bufsize);
1048 chunk_memcat(&buf, str, 6);
1049 }
1050
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001051 h2_set_frame_size(buf.area, buf.data - 9);
1052 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001053 if (unlikely(ret <= 0)) {
1054 if (!ret) {
1055 h2c->flags |= H2_CF_MUX_MFULL;
1056 h2c->flags |= H2_CF_DEM_MROOM;
1057 return 0;
1058 }
1059 else {
1060 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1061 return 0;
1062 }
1063 }
1064 return ret;
1065}
1066
Willy Tarreau52eed752017-09-22 15:05:09 +02001067/* Try to receive a connection preface, then upon success try to send our
1068 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1069 * missing data. It may return an error in h2c.
1070 */
1071static int h2c_frt_recv_preface(struct h2c *h2c)
1072{
1073 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001074 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001075
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001076 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001077
1078 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001079 if (ret1 < 0)
1080 sess_log(h2c->conn->owner);
1081
Willy Tarreau52eed752017-09-22 15:05:09 +02001082 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1083 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1084 return 0;
1085 }
1086
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001087 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001088 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001089 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001090
Willy Tarreaube5b7152017-09-25 16:25:39 +02001091 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001092}
1093
Willy Tarreau01b44822018-10-03 14:26:37 +02001094/* Try to send a connection preface, then upon success try to send our
1095 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1096 * missing data. It may return an error in h2c.
1097 */
1098static int h2c_bck_send_preface(struct h2c *h2c)
1099{
1100 struct buffer *res;
1101
1102 if (h2c_mux_busy(h2c, NULL)) {
1103 h2c->flags |= H2_CF_DEM_MBUSY;
1104 return 0;
1105 }
1106
1107 res = h2_get_buf(h2c, &h2c->mbuf);
1108 if (!res) {
1109 h2c->flags |= H2_CF_MUX_MALLOC;
1110 h2c->flags |= H2_CF_DEM_MROOM;
1111 return 0;
1112 }
1113
1114 if (!b_data(res)) {
1115 /* preface not yet sent */
1116 b_istput(res, ist(H2_CONN_PREFACE));
1117 }
1118
1119 return h2c_send_settings(h2c);
1120}
1121
Willy Tarreau081d4722017-05-16 21:51:05 +02001122/* try to send a GOAWAY frame on the connection to report an error or a graceful
1123 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1124 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1125 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1126 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1127 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1128 * on unrecoverable failure. It will not attempt to send one again in this last
1129 * case so that it is safe to use h2c_error() to report such errors.
1130 */
1131static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1132{
1133 struct buffer *res;
1134 char str[17];
1135 int ret;
1136
1137 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1138 return 1; // claim that it worked
1139
1140 if (h2c_mux_busy(h2c, h2s)) {
1141 if (h2s)
1142 h2s->flags |= H2_SF_BLK_MBUSY;
1143 else
1144 h2c->flags |= H2_CF_DEM_MBUSY;
1145 return 0;
1146 }
1147
Willy Tarreau44e973f2018-03-01 17:49:30 +01001148 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001149 if (!res) {
1150 h2c->flags |= H2_CF_MUX_MALLOC;
1151 if (h2s)
1152 h2s->flags |= H2_SF_BLK_MROOM;
1153 else
1154 h2c->flags |= H2_CF_DEM_MROOM;
1155 return 0;
1156 }
1157
1158 /* len: 8, type: 7, flags: none, sid: 0 */
1159 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1160
1161 if (h2c->last_sid < 0)
1162 h2c->last_sid = h2c->max_id;
1163
1164 write_n32(str + 9, h2c->last_sid);
1165 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001166 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001167 if (unlikely(ret <= 0)) {
1168 if (!ret) {
1169 h2c->flags |= H2_CF_MUX_MFULL;
1170 if (h2s)
1171 h2s->flags |= H2_SF_BLK_MROOM;
1172 else
1173 h2c->flags |= H2_CF_DEM_MROOM;
1174 return 0;
1175 }
1176 else {
1177 /* we cannot report this error using GOAWAY, so we mark
1178 * it and claim a success.
1179 */
1180 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1181 h2c->flags |= H2_CF_GOAWAY_FAILED;
1182 return 1;
1183 }
1184 }
1185 h2c->flags |= H2_CF_GOAWAY_SENT;
1186 return ret;
1187}
1188
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001189/* Try to send an RST_STREAM frame on the connection for the indicated stream
1190 * during mux operations. This stream must be valid and cannot be closed
1191 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1192 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1193 * not yet.
1194 *
1195 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1196 * to write the message, it subscribes the stream to future notifications.
1197 */
1198static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1199{
1200 struct buffer *res;
1201 char str[13];
1202 int ret;
1203
1204 if (!h2s || h2s->st == H2_SS_CLOSED)
1205 return 1;
1206
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001207 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1208 * RST_STREAM in response to a RST_STREAM frame.
1209 */
1210 if (h2c->dft == H2_FT_RST_STREAM) {
1211 ret = 1;
1212 goto ignore;
1213 }
1214
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001215 if (h2c_mux_busy(h2c, h2s)) {
1216 h2s->flags |= H2_SF_BLK_MBUSY;
1217 return 0;
1218 }
1219
Willy Tarreau44e973f2018-03-01 17:49:30 +01001220 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001221 if (!res) {
1222 h2c->flags |= H2_CF_MUX_MALLOC;
1223 h2s->flags |= H2_SF_BLK_MROOM;
1224 return 0;
1225 }
1226
1227 /* len: 4, type: 3, flags: none */
1228 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1229 write_n32(str + 5, h2s->id);
1230 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001231 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001232
1233 if (unlikely(ret <= 0)) {
1234 if (!ret) {
1235 h2c->flags |= H2_CF_MUX_MFULL;
1236 h2s->flags |= H2_SF_BLK_MROOM;
1237 return 0;
1238 }
1239 else {
1240 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1241 return 0;
1242 }
1243 }
1244
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001245 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001246 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001247 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001248 return ret;
1249}
1250
1251/* Try to send an RST_STREAM frame on the connection for the stream being
1252 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001253 * error code, even if the stream is one of the dummy ones, and will update
1254 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001255 *
1256 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1257 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001258 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001259 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001260 */
1261static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1262{
1263 struct buffer *res;
1264 char str[13];
1265 int ret;
1266
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001267 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1268 * RST_STREAM in response to a RST_STREAM frame.
1269 */
1270 if (h2c->dft == H2_FT_RST_STREAM) {
1271 ret = 1;
1272 goto ignore;
1273 }
1274
Willy Tarreau27a84c92017-10-17 08:10:17 +02001275 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001276 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001277 return 0;
1278 }
1279
Willy Tarreau44e973f2018-03-01 17:49:30 +01001280 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001281 if (!res) {
1282 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001283 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001284 return 0;
1285 }
1286
1287 /* len: 4, type: 3, flags: none */
1288 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001289
Willy Tarreau27a84c92017-10-17 08:10:17 +02001290 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001291 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001292 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001293
Willy Tarreau27a84c92017-10-17 08:10:17 +02001294 if (unlikely(ret <= 0)) {
1295 if (!ret) {
1296 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001297 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001298 return 0;
1299 }
1300 else {
1301 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1302 return 0;
1303 }
1304 }
1305
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001306 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001307 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001308 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001309 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001310 }
1311
Willy Tarreau27a84c92017-10-17 08:10:17 +02001312 return ret;
1313}
1314
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001315/* try to send an empty DATA frame with the ES flag set to notify about the
1316 * end of stream and match a shutdown(write). If an ES was already sent as
1317 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1318 * on success or zero if nothing was done. In case of lack of room to write the
1319 * message, it subscribes the requesting stream to future notifications.
1320 */
1321static int h2_send_empty_data_es(struct h2s *h2s)
1322{
1323 struct h2c *h2c = h2s->h2c;
1324 struct buffer *res;
1325 char str[9];
1326 int ret;
1327
Willy Tarreau721c9742017-11-07 11:05:42 +01001328 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001329 return 1;
1330
1331 if (h2c_mux_busy(h2c, h2s)) {
1332 h2s->flags |= H2_SF_BLK_MBUSY;
1333 return 0;
1334 }
1335
Willy Tarreau44e973f2018-03-01 17:49:30 +01001336 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001337 if (!res) {
1338 h2c->flags |= H2_CF_MUX_MALLOC;
1339 h2s->flags |= H2_SF_BLK_MROOM;
1340 return 0;
1341 }
1342
1343 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1344 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1345 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001346 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001347 if (likely(ret > 0)) {
1348 h2s->flags |= H2_SF_ES_SENT;
1349 }
1350 else if (!ret) {
1351 h2c->flags |= H2_CF_MUX_MFULL;
1352 h2s->flags |= H2_SF_BLK_MROOM;
1353 return 0;
1354 }
1355 else {
1356 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1357 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001358 }
1359 return ret;
1360}
1361
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001362/* wake the streams attached to the connection, whose id is greater than <last>,
1363 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001364 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1365 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001366 */
1367static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1368{
1369 struct eb32_node *node;
1370 struct h2s *h2s;
1371
1372 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001373 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001374
1375 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001376 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001377
1378 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1379 while (node) {
1380 h2s = container_of(node, struct h2s, by_id);
1381 if (h2s->id <= last)
1382 break;
1383 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001384
1385 if (!h2s->cs) {
1386 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001387 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001388 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001389 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001390
1391 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001392 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1393 h2s->cs->flags |= CS_FL_ERROR;
1394
Willy Tarreauf830f012018-12-19 17:44:55 +01001395 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001396
Willy Tarreaua8519352018-12-18 16:44:28 +01001397 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001398 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001399 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001400 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001401 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001402 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001403 }
1404}
1405
Willy Tarreau3421aba2017-07-27 15:41:03 +02001406/* Increase all streams' outgoing window size by the difference passed in
1407 * argument. This is needed upon receipt of the settings frame if the initial
1408 * window size is different. The difference may be negative and the resulting
1409 * window size as well, for the time it takes to receive some window updates.
1410 */
1411static void h2c_update_all_ws(struct h2c *h2c, int diff)
1412{
1413 struct h2s *h2s;
1414 struct eb32_node *node;
1415
1416 if (!diff)
1417 return;
1418
1419 node = eb32_first(&h2c->streams_by_id);
1420 while (node) {
1421 h2s = container_of(node, struct h2s, by_id);
1422 h2s->mws += diff;
1423 node = eb32_next(node);
1424 }
1425}
1426
1427/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1428 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1429 * return an error in h2c. Described in RFC7540#6.5.
1430 */
1431static int h2c_handle_settings(struct h2c *h2c)
1432{
1433 unsigned int offset;
1434 int error;
1435
1436 if (h2c->dff & H2_F_SETTINGS_ACK) {
1437 if (h2c->dfl) {
1438 error = H2_ERR_FRAME_SIZE_ERROR;
1439 goto fail;
1440 }
1441 return 1;
1442 }
1443
1444 if (h2c->dsi != 0) {
1445 error = H2_ERR_PROTOCOL_ERROR;
1446 goto fail;
1447 }
1448
1449 if (h2c->dfl % 6) {
1450 error = H2_ERR_FRAME_SIZE_ERROR;
1451 goto fail;
1452 }
1453
1454 /* that's the limit we can process */
1455 if (h2c->dfl > global.tune.bufsize) {
1456 error = H2_ERR_FRAME_SIZE_ERROR;
1457 goto fail;
1458 }
1459
1460 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001461 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001462 return 0;
1463
1464 /* parse the frame */
1465 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001466 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1467 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001468
1469 switch (type) {
1470 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1471 /* we need to update all existing streams with the
1472 * difference from the previous iws.
1473 */
1474 if (arg < 0) { // RFC7540#6.5.2
1475 error = H2_ERR_FLOW_CONTROL_ERROR;
1476 goto fail;
1477 }
1478 h2c_update_all_ws(h2c, arg - h2c->miw);
1479 h2c->miw = arg;
1480 break;
1481 case H2_SETTINGS_MAX_FRAME_SIZE:
1482 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1483 error = H2_ERR_PROTOCOL_ERROR;
1484 goto fail;
1485 }
1486 h2c->mfs = arg;
1487 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001488 case H2_SETTINGS_ENABLE_PUSH:
1489 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1490 error = H2_ERR_PROTOCOL_ERROR;
1491 goto fail;
1492 }
1493 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001494 }
1495 }
1496
1497 /* need to ACK this frame now */
1498 h2c->st0 = H2_CS_FRAME_A;
1499 return 1;
1500 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001501 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001502 h2c_error(h2c, error);
1503 return 0;
1504}
1505
1506/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1507 * success or one of the h2_status values.
1508 */
1509static int h2c_ack_settings(struct h2c *h2c)
1510{
1511 struct buffer *res;
1512 char str[9];
1513 int ret = -1;
1514
1515 if (h2c_mux_busy(h2c, NULL)) {
1516 h2c->flags |= H2_CF_DEM_MBUSY;
1517 return 0;
1518 }
1519
Willy Tarreau44e973f2018-03-01 17:49:30 +01001520 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001521 if (!res) {
1522 h2c->flags |= H2_CF_MUX_MALLOC;
1523 h2c->flags |= H2_CF_DEM_MROOM;
1524 return 0;
1525 }
1526
1527 memcpy(str,
1528 "\x00\x00\x00" /* length : 0 (no data) */
1529 "\x04" "\x01" /* type : 4, flags : ACK */
1530 "\x00\x00\x00\x00" /* stream ID */, 9);
1531
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001532 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001533 if (unlikely(ret <= 0)) {
1534 if (!ret) {
1535 h2c->flags |= H2_CF_MUX_MFULL;
1536 h2c->flags |= H2_CF_DEM_MROOM;
1537 return 0;
1538 }
1539 else {
1540 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1541 return 0;
1542 }
1543 }
1544 return ret;
1545}
1546
Willy Tarreaucf68c782017-10-10 17:11:41 +02001547/* processes a PING frame and schedules an ACK if needed. The caller must pass
1548 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1549 * missing data. It may return an error in h2c.
1550 */
1551static int h2c_handle_ping(struct h2c *h2c)
1552{
1553 /* frame length must be exactly 8 */
1554 if (h2c->dfl != 8) {
1555 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1556 return 0;
1557 }
1558
1559 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001560 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001561 h2c->st0 = H2_CS_FRAME_A;
1562 return 1;
1563}
1564
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001565/* Try to send a window update for stream id <sid> and value <increment>.
1566 * Returns > 0 on success or zero on missing room or failure. It may return an
1567 * error in h2c.
1568 */
1569static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1570{
1571 struct buffer *res;
1572 char str[13];
1573 int ret = -1;
1574
1575 if (h2c_mux_busy(h2c, NULL)) {
1576 h2c->flags |= H2_CF_DEM_MBUSY;
1577 return 0;
1578 }
1579
Willy Tarreau44e973f2018-03-01 17:49:30 +01001580 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001581 if (!res) {
1582 h2c->flags |= H2_CF_MUX_MALLOC;
1583 h2c->flags |= H2_CF_DEM_MROOM;
1584 return 0;
1585 }
1586
1587 /* length: 4, type: 8, flags: none */
1588 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1589 write_n32(str + 5, sid);
1590 write_n32(str + 9, increment);
1591
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001592 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001593
1594 if (unlikely(ret <= 0)) {
1595 if (!ret) {
1596 h2c->flags |= H2_CF_MUX_MFULL;
1597 h2c->flags |= H2_CF_DEM_MROOM;
1598 return 0;
1599 }
1600 else {
1601 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1602 return 0;
1603 }
1604 }
1605 return ret;
1606}
1607
1608/* try to send pending window update for the connection. It's safe to call it
1609 * with no pending updates. Returns > 0 on success or zero on missing room or
1610 * failure. It may return an error in h2c.
1611 */
1612static int h2c_send_conn_wu(struct h2c *h2c)
1613{
1614 int ret = 1;
1615
1616 if (h2c->rcvd_c <= 0)
1617 return 1;
1618
Willy Tarreau97aaa672018-12-23 09:49:04 +01001619 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1620 /* increase the advertised connection window to 2G on
1621 * first update.
1622 */
1623 h2c->flags |= H2_CF_WINDOW_OPENED;
1624 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1625 }
1626
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001627 /* send WU for the connection */
1628 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1629 if (ret > 0)
1630 h2c->rcvd_c = 0;
1631
1632 return ret;
1633}
1634
1635/* try to send pending window update for the current dmux stream. It's safe to
1636 * call it with no pending updates. Returns > 0 on success or zero on missing
1637 * room or failure. It may return an error in h2c.
1638 */
1639static int h2c_send_strm_wu(struct h2c *h2c)
1640{
1641 int ret = 1;
1642
1643 if (h2c->rcvd_s <= 0)
1644 return 1;
1645
1646 /* send WU for the stream */
1647 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1648 if (ret > 0)
1649 h2c->rcvd_s = 0;
1650
1651 return ret;
1652}
1653
Willy Tarreaucf68c782017-10-10 17:11:41 +02001654/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1655 * success, 0 on missing data or one of the h2_status values.
1656 */
1657static int h2c_ack_ping(struct h2c *h2c)
1658{
1659 struct buffer *res;
1660 char str[17];
1661 int ret = -1;
1662
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001663 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001664 return 0;
1665
1666 if (h2c_mux_busy(h2c, NULL)) {
1667 h2c->flags |= H2_CF_DEM_MBUSY;
1668 return 0;
1669 }
1670
Willy Tarreau44e973f2018-03-01 17:49:30 +01001671 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001672 if (!res) {
1673 h2c->flags |= H2_CF_MUX_MALLOC;
1674 h2c->flags |= H2_CF_DEM_MROOM;
1675 return 0;
1676 }
1677
1678 memcpy(str,
1679 "\x00\x00\x08" /* length : 8 (same payload) */
1680 "\x06" "\x01" /* type : 6, flags : ACK */
1681 "\x00\x00\x00\x00" /* stream ID */, 9);
1682
1683 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001684 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001685
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001686 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001687 if (unlikely(ret <= 0)) {
1688 if (!ret) {
1689 h2c->flags |= H2_CF_MUX_MFULL;
1690 h2c->flags |= H2_CF_DEM_MROOM;
1691 return 0;
1692 }
1693 else {
1694 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1695 return 0;
1696 }
1697 }
1698 return ret;
1699}
1700
Willy Tarreau26f95952017-07-27 17:18:30 +02001701/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1702 * Returns > 0 on success or zero on missing data. It may return an error in
1703 * h2c or h2s. Described in RFC7540#6.9.
1704 */
1705static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1706{
1707 int32_t inc;
1708 int error;
1709
1710 if (h2c->dfl != 4) {
1711 error = H2_ERR_FRAME_SIZE_ERROR;
1712 goto conn_err;
1713 }
1714
1715 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001716 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001717 return 0;
1718
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001720
1721 if (h2c->dsi != 0) {
1722 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001723
1724 /* it's not an error to receive WU on a closed stream */
1725 if (h2s->st == H2_SS_CLOSED)
1726 return 1;
1727
1728 if (!inc) {
1729 error = H2_ERR_PROTOCOL_ERROR;
1730 goto strm_err;
1731 }
1732
1733 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1734 error = H2_ERR_FLOW_CONTROL_ERROR;
1735 goto strm_err;
1736 }
1737
1738 h2s->mws += inc;
1739 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1740 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001741 if (h2s->send_wait)
1742 LIST_ADDQ(&h2c->send_list, &h2s->list);
1743
Willy Tarreau26f95952017-07-27 17:18:30 +02001744 }
1745 }
1746 else {
1747 /* connection window update */
1748 if (!inc) {
1749 error = H2_ERR_PROTOCOL_ERROR;
1750 goto conn_err;
1751 }
1752
1753 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1754 error = H2_ERR_FLOW_CONTROL_ERROR;
1755 goto conn_err;
1756 }
1757
1758 h2c->mws += inc;
1759 }
1760
1761 return 1;
1762
1763 conn_err:
1764 h2c_error(h2c, error);
1765 return 0;
1766
1767 strm_err:
1768 if (h2s) {
1769 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001770 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001771 }
1772 else
1773 h2c_error(h2c, error);
1774 return 0;
1775}
1776
Willy Tarreaue96b0922017-10-30 00:28:29 +01001777/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1778 * the last ID. Returns > 0 on success or zero on missing data. It may return
1779 * an error in h2c. Described in RFC7540#6.8.
1780 */
1781static int h2c_handle_goaway(struct h2c *h2c)
1782{
1783 int error;
1784 int last;
1785
1786 if (h2c->dsi != 0) {
1787 error = H2_ERR_PROTOCOL_ERROR;
1788 goto conn_err;
1789 }
1790
1791 if (h2c->dfl < 8) {
1792 error = H2_ERR_FRAME_SIZE_ERROR;
1793 goto conn_err;
1794 }
1795
1796 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001797 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001798 return 0;
1799
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001800 last = h2_get_n32(&h2c->dbuf, 0);
1801 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001802 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001803 if (h2c->last_sid < 0)
1804 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001805 return 1;
1806
1807 conn_err:
1808 h2c_error(h2c, error);
1809 return 0;
1810}
1811
Willy Tarreau92153fc2017-12-03 19:46:19 +01001812/* processes a PRIORITY frame, and either skips it or rejects if it is
1813 * invalid. Returns > 0 on success or zero on missing data. It may return
1814 * an error in h2c. Described in RFC7540#6.3.
1815 */
1816static int h2c_handle_priority(struct h2c *h2c)
1817{
1818 int error;
1819
1820 if (h2c->dsi == 0) {
1821 error = H2_ERR_PROTOCOL_ERROR;
1822 goto conn_err;
1823 }
1824
1825 if (h2c->dfl != 5) {
1826 error = H2_ERR_FRAME_SIZE_ERROR;
1827 goto conn_err;
1828 }
1829
1830 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001831 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001832 return 0;
1833
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001834 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001835 /* 7540#5.3 : can't depend on itself */
1836 error = H2_ERR_PROTOCOL_ERROR;
1837 goto conn_err;
1838 }
1839 return 1;
1840
1841 conn_err:
1842 h2c_error(h2c, error);
1843 return 0;
1844}
1845
Willy Tarreaucd234e92017-08-18 10:59:39 +02001846/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1847 * Returns > 0 on success or zero on missing data. It may return an error in
1848 * h2c. Described in RFC7540#6.4.
1849 */
1850static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1851{
1852 int error;
1853
1854 if (h2c->dsi == 0) {
1855 error = H2_ERR_PROTOCOL_ERROR;
1856 goto conn_err;
1857 }
1858
Willy Tarreaucd234e92017-08-18 10:59:39 +02001859 if (h2c->dfl != 4) {
1860 error = H2_ERR_FRAME_SIZE_ERROR;
1861 goto conn_err;
1862 }
1863
1864 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001865 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001866 return 0;
1867
1868 /* late RST, already handled */
1869 if (h2s->st == H2_SS_CLOSED)
1870 return 1;
1871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001872 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001873 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001874
1875 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001876 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001877 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001878 }
1879
1880 h2s->flags |= H2_SF_RST_RCVD;
1881 return 1;
1882
1883 conn_err:
1884 h2c_error(h2c, error);
1885 return 0;
1886}
1887
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001888/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1889 * It may return an error in h2c or h2s. The caller must consider that the
1890 * return value is the new h2s in case one was allocated (most common case).
1891 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001892 * errors here are reported as connection errors since it's impossible to
1893 * recover from such errors after the compression context has been altered.
1894 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001895static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001896{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001897 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001898 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001899 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001900 int error;
1901
1902 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001903 /* RFC7540#4.2 */
1904 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001905 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001906 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001907 }
1908
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001909 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001910 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001911
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001912 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001913 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001914
1915 /* now either the frame is complete or the buffer is complete */
1916 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001917 /* The stream exists/existed, this must be a trailers frame */
1918 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001919 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001920 goto out;
1921 goto done;
1922 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001923 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001924 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001925 goto conn_err;
1926 }
1927 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1928 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1929 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001930 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001931 goto conn_err;
1932 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001933 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1934 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001935
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001936 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001937
Willy Tarreau25919232019-01-03 14:48:18 +01001938 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001939 if (h2c->st0 >= H2_CS_ERROR)
1940 goto out;
1941
Willy Tarreau25919232019-01-03 14:48:18 +01001942 if (error <= 0) {
1943 if (error == 0)
1944 goto out; // missing data
1945
1946 /* Failed to decode this stream (e.g. too large request)
1947 * but the HPACK decompressor is still synchronized.
1948 */
1949 h2s = (struct h2s*)h2_error_stream;
1950 goto send_rst;
1951 }
1952
Willy Tarreau22de8d32018-09-05 19:55:58 +02001953 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001954 * positively from h2c_frt_stream_new(), the stream will report the error,
1955 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001956 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001957 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001958 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001959 h2s = (struct h2s*)h2_refused_stream;
1960 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001961 }
1962
1963 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001964 h2s->rxbuf = rxbuf;
1965 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001966 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001967
Willy Tarreau88d138e2019-01-02 19:38:14 +01001968 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001969 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001970 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001971
1972 if (h2s->flags & H2_SF_ES_RCVD) {
1973 h2s->st = H2_SS_HREM;
Willy Tarreau39d68502018-03-02 12:26:37 +01001974 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001975 }
1976
Willy Tarreau3a429f02019-01-03 11:41:50 +01001977 /* update the max stream ID if the request is being processed */
1978 if (h2s->id > h2c->max_id)
1979 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001980
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001981 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001982
1983 conn_err:
1984 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001985 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001986
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001987 out:
1988 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001989 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001990
1991 send_rst:
1992 /* make the demux send an RST for the current stream. We may only
1993 * do this if we're certain that the HEADERS frame was properly
1994 * decompressed so that the HPACK decoder is still kept up to date.
1995 */
1996 h2_release_buf(h2c, &rxbuf);
1997 h2c->st0 = H2_CS_FRAME_E;
1998 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001999}
2000
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002001/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2002 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2003 * errors here are reported as connection errors since it's impossible to
2004 * recover from such errors after the compression context has been altered.
2005 */
2006static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2007{
2008 int error;
2009
2010 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01002011 /* RFC7540#4.2 */
2012 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002013 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01002014 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002015 }
2016
2017 if (!b_size(&h2c->dbuf))
2018 return NULL; // empty buffer
2019
2020 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2021 return NULL; // incomplete frame
2022
Willy Tarreau1915ca22019-01-24 11:49:37 +01002023 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002024
Willy Tarreau25919232019-01-03 14:48:18 +01002025 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002026 if (h2c->st0 >= H2_CS_ERROR)
2027 return NULL;
2028
Willy Tarreau25919232019-01-03 14:48:18 +01002029 if (error <= 0) {
2030 if (error == 0)
2031 return NULL; // missing data
2032
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002033 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002034 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002035 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002036 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002037 }
2038
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002039 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2040 h2s->flags |= H2_SF_ES_RCVD;
2041 h2s->cs->flags |= CS_FL_REOS;
2042 }
2043
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002044 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
2045 h2s->st = H2_SS_ERROR;
2046 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2047 h2s->st = H2_SS_HREM;
2048 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2049 h2s_close(h2s);
2050
2051 return h2s;
2052
2053 conn_err:
2054 h2c_error(h2c, error);
2055 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002056}
2057
Willy Tarreau454f9052017-10-26 19:40:35 +02002058/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2059 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2060 */
2061static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2062{
2063 int error;
2064
2065 /* note that empty DATA frames are perfectly valid and sometimes used
2066 * to signal an end of stream (with the ES flag).
2067 */
2068
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002069 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002070 return 0; // empty buffer
2071
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002072 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002073 return 0; // incomplete frame
2074
2075 /* now either the frame is complete or the buffer is complete */
2076
2077 if (!h2c->dsi) {
2078 /* RFC7540#6.1 */
2079 error = H2_ERR_PROTOCOL_ERROR;
2080 goto conn_err;
2081 }
2082
2083 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2084 /* RFC7540#6.1 */
2085 error = H2_ERR_STREAM_CLOSED;
2086 goto strm_err;
2087 }
2088
Willy Tarreau1915ca22019-01-24 11:49:37 +01002089 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2090 /* RFC7540#8.1.2 */
2091 error = H2_ERR_PROTOCOL_ERROR;
2092 goto strm_err;
2093 }
2094
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002095 if (!h2_frt_transfer_data(h2s))
2096 return 0;
2097
Willy Tarreau454f9052017-10-26 19:40:35 +02002098 /* call the upper layers to process the frame, then let the upper layer
2099 * notify the stream about any change.
2100 */
2101 if (!h2s->cs) {
2102 error = H2_ERR_STREAM_CLOSED;
2103 goto strm_err;
2104 }
2105
Willy Tarreau8f650c32017-11-21 19:36:21 +01002106 if (h2c->st0 >= H2_CS_ERROR)
2107 return 0;
2108
Willy Tarreau721c9742017-11-07 11:05:42 +01002109 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002110 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002111 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002112 }
2113
2114 /* check for completion : the callee will change this to FRAME_A or
2115 * FRAME_H once done.
2116 */
2117 if (h2c->st0 == H2_CS_FRAME_P)
2118 return 0;
2119
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002120 /* last frame */
2121 if (h2c->dff & H2_F_DATA_END_STREAM) {
2122 h2s->st = H2_SS_HREM;
2123 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002124 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002125
2126 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2127 /* RFC7540#8.1.2 */
2128 error = H2_ERR_PROTOCOL_ERROR;
2129 goto strm_err;
2130 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002131 }
2132
Willy Tarreau454f9052017-10-26 19:40:35 +02002133 return 1;
2134
2135 conn_err:
2136 h2c_error(h2c, error);
2137 return 0;
2138
2139 strm_err:
2140 if (h2s) {
2141 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002142 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002143 }
2144 else
2145 h2c_error(h2c, error);
2146 return 0;
2147}
2148
Willy Tarreaubc933932017-10-09 16:21:43 +02002149/* process Rx frames to be demultiplexed */
2150static void h2_process_demux(struct h2c *h2c)
2151{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002152 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002153
Willy Tarreau081d4722017-05-16 21:51:05 +02002154 if (h2c->st0 >= H2_CS_ERROR)
2155 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002156
2157 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2158 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002159 if (h2c->flags & H2_CF_IS_BACK)
2160 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002161 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2162 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002163 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002164 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002165 sess_log(h2c->conn->owner);
2166 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002167 goto fail;
2168 }
2169
2170 h2c->max_id = 0;
2171 h2c->st0 = H2_CS_SETTINGS1;
2172 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002173
2174 if (h2c->st0 == H2_CS_SETTINGS1) {
2175 struct h2_fh hdr;
2176
2177 /* ensure that what is pending is a valid SETTINGS frame
2178 * without an ACK.
2179 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002180 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002181 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002182 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002183 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002184 sess_log(h2c->conn->owner);
2185 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002186 goto fail;
2187 }
2188
2189 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2190 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2191 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2192 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002193 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002194 goto fail;
2195 }
2196
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002197 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002198 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2199 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2200 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002201 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002202 goto fail;
2203 }
2204
Willy Tarreau3bf69182018-12-21 15:34:50 +01002205 /* that's OK, switch to FRAME_P to process it. This is
2206 * a SETTINGS frame whose header has already been
2207 * deleted above.
2208 */
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002209 h2c->dfl = hdr.len;
2210 h2c->dsi = hdr.sid;
2211 h2c->dft = hdr.ft;
2212 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002213 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002214 h2c->st0 = H2_CS_FRAME_P;
2215 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002216 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002217
2218 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002219 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 int ret = 0;
2221
2222 if (h2c->st0 >= H2_CS_ERROR)
2223 break;
2224
2225 if (h2c->st0 == H2_CS_FRAME_H) {
2226 struct h2_fh hdr;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002227 unsigned int padlen = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002228
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002229 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002230 break;
2231
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002232 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2234 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002235 if (!h2c->nb_streams) {
2236 /* only log if no other stream can report the error */
2237 sess_log(h2c->conn->owner);
2238 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002239 break;
2240 }
2241
Willy Tarreau3bf69182018-12-21 15:34:50 +01002242 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2243 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2244 * we read the pad length and drop it from the remaining
2245 * payload (one byte + the 9 remaining ones = 10 total
2246 * removed), so we have a frame payload starting after the
2247 * pad len. Flow controlled frames (DATA) also count the
2248 * padlen in the flow control, so it must be adjusted.
2249 */
2250 if (hdr.len < 1) {
2251 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2252 sess_log(h2c->conn->owner);
2253 goto fail;
2254 }
2255 hdr.len--;
2256
2257 if (b_data(&h2c->dbuf) < 10)
2258 break; // missing padlen
2259
2260 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2261
2262 if (padlen > hdr.len) {
2263 /* RFC7540#6.1 : pad length = length of
2264 * frame payload or greater => error.
2265 */
2266 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2267 sess_log(h2c->conn->owner);
2268 goto fail;
2269 }
2270
2271 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2272 h2c->rcvd_c++;
2273 h2c->rcvd_s++;
2274 }
2275 b_del(&h2c->dbuf, 1);
2276 }
2277 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002278 h2c->dfl = hdr.len;
2279 h2c->dsi = hdr.sid;
2280 h2c->dft = hdr.ft;
2281 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002282 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002283 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002284 }
2285
2286 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002287 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2288
Willy Tarreau567beb82018-12-18 16:52:44 +01002289 if (tmp_h2s != h2s && h2s && h2s->cs &&
2290 (b_data(&h2s->rxbuf) ||
2291 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002292 /* we may have to signal the upper layers */
2293 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002294 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002295 }
2296 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002297
Willy Tarreaud7901432017-12-29 11:34:40 +01002298 if (h2c->st0 == H2_CS_FRAME_E)
2299 goto strm_err;
2300
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002301 if (h2s->st == H2_SS_IDLE &&
2302 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2303 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2304 * this state MUST be treated as a connection error
2305 */
2306 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2307 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002308 if (!h2c->nb_streams) {
2309 /* only log if no other stream can report the error */
2310 sess_log(h2c->conn->owner);
2311 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002312 break;
2313 }
2314
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002315 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2316 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2317 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002318 * this state MUST be treated as a stream error.
2319 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2320 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002321 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002322 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2323 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2324 else
2325 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002326 goto strm_err;
2327 }
2328
Willy Tarreauab837502017-12-27 15:07:30 +01002329 /* Below the management of frames received in closed state is a
2330 * bit hackish because the spec makes strong differences between
2331 * streams closed by receiving RST, sending RST, and seeing ES
2332 * in both directions. In addition to this, the creation of a
2333 * new stream reusing the identifier of a closed one will be
2334 * detected here. Given that we cannot keep track of all closed
2335 * streams forever, we consider that unknown closed streams were
2336 * closed on RST received, which allows us to respond with an
2337 * RST without breaking the connection (eg: to abort a transfer).
2338 * Some frames have to be silently ignored as well.
2339 */
2340 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau113c7a22019-01-24 09:36:53 +01002341 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002342 /* #5.1.1: The identifier of a newly
2343 * established stream MUST be numerically
2344 * greater than all streams that the initiating
2345 * endpoint has opened or reserved. This
2346 * governs streams that are opened using a
2347 * HEADERS frame and streams that are reserved
2348 * using PUSH_PROMISE. An endpoint that
2349 * receives an unexpected stream identifier
2350 * MUST respond with a connection error.
2351 */
2352 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2353 goto strm_err;
2354 }
2355
2356 if (h2s->flags & H2_SF_RST_RCVD) {
2357 /* RFC7540#5.1:closed: an endpoint that
2358 * receives any frame other than PRIORITY after
2359 * receiving a RST_STREAM MUST treat that as a
2360 * stream error of type STREAM_CLOSED.
2361 *
2362 * Note that old streams fall into this category
2363 * and will lead to an RST being sent.
2364 */
2365 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2366 h2c->st0 = H2_CS_FRAME_E;
2367 goto strm_err;
2368 }
2369
2370 /* RFC7540#5.1:closed: if this state is reached as a
2371 * result of sending a RST_STREAM frame, the peer that
2372 * receives the RST_STREAM might have already sent
2373 * frames on the stream that cannot be withdrawn. An
2374 * endpoint MUST ignore frames that it receives on
2375 * closed streams after it has sent a RST_STREAM
2376 * frame. An endpoint MAY choose to limit the period
2377 * over which it ignores frames and treat frames that
2378 * arrive after this time as being in error.
2379 */
2380 if (!(h2s->flags & H2_SF_RST_SENT)) {
2381 /* RFC7540#5.1:closed: any frame other than
2382 * PRIO/WU/RST in this state MUST be treated as
2383 * a connection error
2384 */
2385 if (h2c->dft != H2_FT_RST_STREAM &&
2386 h2c->dft != H2_FT_PRIORITY &&
2387 h2c->dft != H2_FT_WINDOW_UPDATE) {
2388 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2389 goto strm_err;
2390 }
2391 }
2392 }
2393
Willy Tarreauc0da1962017-10-30 18:38:00 +01002394#if 0
2395 // problem below: it is not possible to completely ignore such
2396 // streams as we need to maintain the compression state as well
2397 // and for this we need to completely process these frames (eg:
2398 // HEADERS frames) as well as counting DATA frames to emit
2399 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2400 // This is a typical case of layer violation where the
2401 // transported contents are critical to the connection's
2402 // validity and must be ignored at the same time :-(
2403
2404 /* graceful shutdown, ignore streams whose ID is higher than
2405 * the one advertised in GOAWAY. RFC7540#6.8.
2406 */
2407 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002408 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2409 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002410 h2c->dfl -= ret;
2411 ret = h2c->dfl == 0;
2412 goto strm_err;
2413 }
2414#endif
2415
Willy Tarreau7e98c052017-10-10 15:56:59 +02002416 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002417 case H2_FT_SETTINGS:
2418 if (h2c->st0 == H2_CS_FRAME_P)
2419 ret = h2c_handle_settings(h2c);
2420
2421 if (h2c->st0 == H2_CS_FRAME_A)
2422 ret = h2c_ack_settings(h2c);
2423 break;
2424
Willy Tarreaucf68c782017-10-10 17:11:41 +02002425 case H2_FT_PING:
2426 if (h2c->st0 == H2_CS_FRAME_P)
2427 ret = h2c_handle_ping(h2c);
2428
2429 if (h2c->st0 == H2_CS_FRAME_A)
2430 ret = h2c_ack_ping(h2c);
2431 break;
2432
Willy Tarreau26f95952017-07-27 17:18:30 +02002433 case H2_FT_WINDOW_UPDATE:
2434 if (h2c->st0 == H2_CS_FRAME_P)
2435 ret = h2c_handle_window_update(h2c, h2s);
2436 break;
2437
Willy Tarreau61290ec2017-10-17 08:19:21 +02002438 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002439 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2440 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2441 * frames' parsers consume all following CONTINUATION
2442 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002443 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002444 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2445 sess_log(h2c->conn->owner);
2446 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002447
Willy Tarreau13278b42017-10-13 19:23:14 +02002448 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002449 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002450 if (h2c->flags & H2_CF_IS_BACK)
2451 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2452 else
2453 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002454 if (tmp_h2s) {
2455 h2s = tmp_h2s;
2456 ret = 1;
2457 }
2458 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002459 break;
2460
Willy Tarreau454f9052017-10-26 19:40:35 +02002461 case H2_FT_DATA:
2462 if (h2c->st0 == H2_CS_FRAME_P)
2463 ret = h2c_frt_handle_data(h2c, h2s);
2464
2465 if (h2c->st0 == H2_CS_FRAME_A)
2466 ret = h2c_send_strm_wu(h2c);
2467 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002468
Willy Tarreau92153fc2017-12-03 19:46:19 +01002469 case H2_FT_PRIORITY:
2470 if (h2c->st0 == H2_CS_FRAME_P)
2471 ret = h2c_handle_priority(h2c);
2472 break;
2473
Willy Tarreaucd234e92017-08-18 10:59:39 +02002474 case H2_FT_RST_STREAM:
2475 if (h2c->st0 == H2_CS_FRAME_P)
2476 ret = h2c_handle_rst_stream(h2c, h2s);
2477 break;
2478
Willy Tarreaue96b0922017-10-30 00:28:29 +01002479 case H2_FT_GOAWAY:
2480 if (h2c->st0 == H2_CS_FRAME_P)
2481 ret = h2c_handle_goaway(h2c);
2482 break;
2483
Willy Tarreau1c661982017-10-30 13:52:01 +01002484 case H2_FT_PUSH_PROMISE:
2485 /* not permitted here, RFC7540#5.1 */
2486 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002487 if (!h2c->nb_streams) {
2488 /* only log if no other stream can report the error */
2489 sess_log(h2c->conn->owner);
2490 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002491 break;
2492
2493 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002494 default:
2495 /* drop frames that we ignore. They may be larger than
2496 * the buffer so we drain all of their contents until
2497 * we reach the end.
2498 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002499 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2500 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002501 h2c->dfl -= ret;
2502 ret = h2c->dfl == 0;
2503 }
2504
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002505 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002506 /* We may have to send an RST if not done yet */
2507 if (h2s->st == H2_SS_ERROR)
2508 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002509
Willy Tarreaua20a5192017-12-27 11:02:06 +01002510 if (h2c->st0 == H2_CS_FRAME_E)
2511 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002512
Willy Tarreau7e98c052017-10-10 15:56:59 +02002513 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002514 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002515 break;
2516
2517 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002518 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002519 h2c->st0 = H2_CS_FRAME_H;
2520 }
2521 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002522
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002523 if (h2c->rcvd_c > 0 &&
2524 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2525 h2c_send_conn_wu(h2c);
2526
Willy Tarreau52eed752017-09-22 15:05:09 +02002527 fail:
2528 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002529 if (h2s && h2s->cs &&
2530 (b_data(&h2s->rxbuf) ||
2531 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002532 /* we may have to signal the upper layers */
2533 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002534 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002535 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002536
Willy Tarreau47b515a2018-12-21 16:09:41 +01002537 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002538}
2539
2540/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2541 * the end.
2542 */
2543static int h2_process_mux(struct h2c *h2c)
2544{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002545 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002546
Willy Tarreau01b44822018-10-03 14:26:37 +02002547 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2548 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2549 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2550 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2551 if (h2c->st0 == H2_CS_ERROR) {
2552 h2c->st0 = H2_CS_ERROR2;
2553 sess_log(h2c->conn->owner);
2554 }
2555 goto fail;
2556 }
2557 h2c->st0 = H2_CS_SETTINGS1;
2558 }
2559 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002560 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002561 return 1;
2562 }
2563
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002564 /* start by sending possibly pending window updates */
2565 if (h2c->rcvd_c > 0 &&
2566 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2567 h2c_send_conn_wu(h2c) < 0)
2568 goto fail;
2569
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002570 /* First we always process the flow control list because the streams
2571 * waiting there were already elected for immediate emission but were
2572 * blocked just on this.
2573 */
2574
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002575 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002576 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2577 h2c->st0 >= H2_CS_ERROR)
2578 break;
2579
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002580 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002581 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2582 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002583 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002584 LIST_DEL(&h2s->list);
2585 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002586 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002587 }
2588
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002589 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002590 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2591 break;
2592
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002593 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002594 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2595 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002596 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002597 LIST_DEL(&h2s->list);
2598 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002599 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002600 }
2601
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002602 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002603 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002604 if (h2c->st0 == H2_CS_ERROR) {
2605 if (h2c->max_id >= 0) {
2606 h2c_send_goaway_error(h2c, NULL);
2607 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2608 return 0;
2609 }
2610
2611 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2612 }
2613 return 1;
2614 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002615 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002616}
2617
Willy Tarreau62f52692017-10-08 23:01:42 +02002618
Willy Tarreau479998a2018-11-18 06:30:59 +01002619/* Attempt to read data, and subscribe if none available.
2620 * The function returns 1 if data has been received, otherwise zero.
2621 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002622static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002623{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002624 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002625 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002626 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002627 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002628
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002629 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002630 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002631
Willy Tarreau315d8072017-12-10 22:17:57 +01002632 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002633 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002634
Willy Tarreau44e973f2018-03-01 17:49:30 +01002635 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002636 if (!buf) {
2637 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002638 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002639 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002640
Olivier Houchard7505f942018-08-21 18:10:44 +02002641 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002642 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002643 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2644 /* HTX in use : try to pre-align the buffer like the
2645 * rxbufs will be to optimize memory copies. We'll make
2646 * sure that the frame header lands at the end of the
2647 * HTX block to alias it upon recv. We cannot use the
2648 * head because rcv_buf() will realign the buffer if
2649 * it's empty. Thus we cheat and pretend we already
2650 * have a few bytes there.
2651 */
2652 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002653 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002654 }
2655 else
2656 max = b_room(buf);
2657
Olivier Houchard7505f942018-08-21 18:10:44 +02002658 if (max)
2659 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2660 else
2661 ret = 0;
2662 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002663
Olivier Houchard53216e72018-10-10 15:46:36 +02002664 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002665 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002666
Olivier Houcharda1411e62018-08-17 18:42:48 +02002667 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002668 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002669 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002670 }
2671
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002672 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002673 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002674 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002675}
2676
Willy Tarreau479998a2018-11-18 06:30:59 +01002677/* Try to send data if possible.
2678 * The function returns 1 if data have been sent, otherwise zero.
2679 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002680static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002681{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002682 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002683 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002684 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002685
2686 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002687 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002688
Olivier Houchard7505f942018-08-21 18:10:44 +02002689
Willy Tarreaua2af5122017-10-09 11:56:46 +02002690 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2691 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002692 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002693 }
2694
Willy Tarreaubc933932017-10-09 16:21:43 +02002695 /* This loop is quite simple : it tries to fill as much as it can from
2696 * pending streams into the existing buffer until it's reportedly full
2697 * or the end of send requests is reached. Then it tries to send this
2698 * buffer's contents out, marks it not full if at least one byte could
2699 * be sent, and tries again.
2700 *
2701 * The snd_buf() function normally takes a "flags" argument which may
2702 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2703 * data immediately comes and CO_SFL_STREAMER to indicate that the
2704 * connection is streaming lots of data (used to increase TLS record
2705 * size at the expense of latency). The former can be sent any time
2706 * there's a buffer full flag, as it indicates at least one stream
2707 * attempted to send and failed so there are pending data. An
2708 * alternative would be to set it as long as there's an active stream
2709 * but that would be problematic for ACKs until we have an absolute
2710 * guarantee that all waiters have at least one byte to send. The
2711 * latter should possibly not be set for now.
2712 */
2713
2714 done = 0;
2715 while (!done) {
2716 unsigned int flags = 0;
2717
2718 /* fill as much as we can into the current buffer */
2719 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2720 done = h2_process_mux(h2c);
2721
2722 if (conn->flags & CO_FL_ERROR)
2723 break;
2724
2725 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2726 flags |= CO_SFL_MSG_MORE;
2727
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002728 if (b_data(&h2c->mbuf)) {
2729 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002730 if (!ret)
2731 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002732 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002733 b_del(&h2c->mbuf, ret);
2734 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002735 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002736
2737 /* wrote at least one byte, the buffer is not full anymore */
2738 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2739 }
2740
Willy Tarreaua2af5122017-10-09 11:56:46 +02002741 if (conn->flags & CO_FL_SOCK_WR_SH) {
2742 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002743 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002744 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002745 /* We're not full anymore, so we can wake any task that are waiting
2746 * for us.
2747 */
2748 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002749 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002750 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2751 struct h2s *, list);
2752 LIST_DEL(&h2s->list);
2753 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002754 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002755 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2756 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002757 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002758 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002759 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002760 /* We're done, no more to send */
2761 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002762 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002763schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002764 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2765 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002766 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002767}
2768
2769static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2770{
2771 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002772 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002773
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002774 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002775 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002776 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002777 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002778 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002779 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002780 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002781}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002782
Willy Tarreau62f52692017-10-08 23:01:42 +02002783/* callback called on any event by the connection handler.
2784 * It applies changes and returns zero, or < 0 if it wants immediate
2785 * destruction of the connection (which normally doesn not happen in h2).
2786 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002787static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002788{
Olivier Houchard7505f942018-08-21 18:10:44 +02002789 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002790
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002792 h2_process_demux(h2c);
2793
2794 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002795 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002796
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002797 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002798 h2c->flags &= ~H2_CF_DEM_DFULL;
2799 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002800 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002801
Willy Tarreau0b37d652018-10-03 10:33:02 +02002802 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002803 /* frontend is stopping, reload likely in progress, let's try
2804 * to announce a graceful shutdown if not yet done. We don't
2805 * care if it fails, it will be tried again later.
2806 */
2807 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2808 if (h2c->last_sid < 0)
2809 h2c->last_sid = (1U << 31) - 1;
2810 h2c_send_goaway_error(h2c, NULL);
2811 }
2812 }
2813
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002814 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002815 * If we received early data, and the handshake is done, wake
2816 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002817 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002818 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2819 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2820 struct eb32_node *node;
2821 struct h2s *h2s;
2822
2823 h2c->flags |= H2_CF_WAIT_FOR_HS;
2824 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2825
2826 while (node) {
2827 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002828 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002829 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002830 node = eb32_next(node);
2831 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002832 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002833
Willy Tarreau26bd7612017-10-09 16:47:04 +02002834 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002835 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2836 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2837 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002838 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002839
2840 if (eb_is_empty(&h2c->streams_by_id)) {
2841 /* no more stream, kill the connection now */
2842 h2_release(conn);
2843 return -1;
2844 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002845 }
2846
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002848 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002849
Olivier Houchard53216e72018-10-10 15:46:36 +02002850 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2851 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2852 (h2c->st0 != H2_CS_ERROR &&
2853 !b_data(&h2c->mbuf) &&
2854 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2855 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002856 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002857
Willy Tarreau3f133572017-10-31 19:21:06 +01002858 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002859 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002860 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002861 task_queue(h2c->task);
2862 }
2863 else
2864 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002865 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002866
Olivier Houchard7505f942018-08-21 18:10:44 +02002867 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002868 return 0;
2869}
2870
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002871static int h2_wake(struct connection *conn)
2872{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002873 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002874
2875 return (h2_process(h2c));
2876}
2877
Willy Tarreauea392822017-10-31 10:02:25 +01002878/* Connection timeout management. The principle is that if there's no receipt
2879 * nor sending for a certain amount of time, the connection is closed. If the
2880 * MUX buffer still has lying data or is not allocatable, the connection is
2881 * immediately killed. If it's allocatable and empty, we attempt to send a
2882 * GOAWAY frame.
2883 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002884static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002885{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002886 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002887 int expired = tick_is_expired(t->expire, now_ms);
2888
Willy Tarreau0975f112018-03-29 15:22:59 +02002889 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002890 return t;
2891
Willy Tarreau0975f112018-03-29 15:22:59 +02002892 task_delete(t);
2893 task_free(t);
2894
2895 if (!h2c) {
2896 /* resources were already deleted */
2897 return NULL;
2898 }
2899
2900 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002901 h2c_error(h2c, H2_ERR_NO_ERROR);
2902 h2_wake_some_streams(h2c, 0, 0);
2903
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002904 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002905 /* don't even try to send a GOAWAY, the buffer is stuck */
2906 h2c->flags |= H2_CF_GOAWAY_FAILED;
2907 }
2908
2909 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002910 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002911 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2912 h2c->flags |= H2_CF_GOAWAY_FAILED;
2913
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002914 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2915 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002916 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002917 b_del(&h2c->mbuf, ret);
2918 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002919 }
2920 }
Willy Tarreauea392822017-10-31 10:02:25 +01002921
Willy Tarreau0975f112018-03-29 15:22:59 +02002922 /* either we can release everything now or it will be done later once
2923 * the last stream closes.
2924 */
2925 if (eb_is_empty(&h2c->streams_by_id))
2926 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002927
Willy Tarreauea392822017-10-31 10:02:25 +01002928 return NULL;
2929}
2930
2931
Willy Tarreau62f52692017-10-08 23:01:42 +02002932/*******************************************/
2933/* functions below are used by the streams */
2934/*******************************************/
2935
2936/*
2937 * Attach a new stream to a connection
2938 * (Used for outgoing connections)
2939 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002940static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002941{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002942 struct conn_stream *cs;
2943 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002944 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002945
2946 cs = cs_new(conn);
2947 if (!cs)
2948 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002949 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002950 if (!h2s) {
2951 cs_free(cs);
2952 return NULL;
2953 }
2954 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002955}
2956
Willy Tarreaufafd3982018-11-18 21:29:20 +01002957/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2958 * We have to scan because we may have some orphan streams. It might be
2959 * beneficial to scan backwards from the end to reduce the likeliness to find
2960 * orphans.
2961 */
2962static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2963{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002964 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002965 struct h2s *h2s;
2966 struct eb32_node *node;
2967
2968 node = eb32_first(&h2c->streams_by_id);
2969 while (node) {
2970 h2s = container_of(node, struct h2s, by_id);
2971 if (h2s->cs)
2972 return h2s->cs;
2973 node = eb32_next(node);
2974 }
2975 return NULL;
2976}
2977
Willy Tarreau62f52692017-10-08 23:01:42 +02002978/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002979 * Destroy the mux and the associated connection, if it is no longer used
2980 */
2981static void h2_destroy(struct connection *conn)
2982{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002983 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002984
2985 if (eb_is_empty(&h2c->streams_by_id))
2986 h2_release(h2c->conn);
2987}
2988
2989/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002990 * Detach the stream from the connection and possibly release the connection.
2991 */
2992static void h2_detach(struct conn_stream *cs)
2993{
Willy Tarreau60935142017-10-16 18:11:19 +02002994 struct h2s *h2s = cs->ctx;
2995 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002996 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002997
2998 cs->ctx = NULL;
2999 if (!h2s)
3000 return;
3001
Olivier Houchardf502aca2018-12-14 19:42:40 +01003002 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003003 h2c = h2s->h2c;
3004 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003005 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02003006 if (h2c->flags & H2_CF_DEM_TOOMANY &&
3007 !h2_has_too_many_cs(h2c)) {
3008 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003009 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02003010 }
Willy Tarreau60935142017-10-16 18:11:19 +02003011
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003012 /* this stream may be blocked waiting for some data to leave (possibly
3013 * an ES or RST frame), so orphan it in this case.
3014 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003015 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003016 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003017 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003018 return;
3019
Willy Tarreau45f752e2017-10-30 15:44:59 +01003020 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3021 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3022 /* unblock the connection if it was blocked on this
3023 * stream.
3024 */
3025 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3026 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003027 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003028 }
3029
Willy Tarreau71049cc2018-03-28 13:56:39 +02003030 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003031
Olivier Houchard8a786902018-12-15 16:05:40 +01003032 if (h2c->flags & H2_CF_IS_BACK &&
3033 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003034 if (!(h2c->conn->flags &
3035 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3036 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003037 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003038 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3039 h2c->conn->owner = NULL;
3040 if (eb_is_empty(&h2c->streams_by_id)) {
3041 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3042 /* The server doesn't want it, let's kill the connection right away */
3043 h2c->conn->mux->destroy(h2c->conn);
3044 return;
3045 }
3046 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003047 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003048 if (eb_is_empty(&h2c->streams_by_id)) {
3049 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3050 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3051 return;
3052 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003053 /* Never ever allow to reuse a connection from a non-reuse backend */
3054 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3055 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01003056 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003057 struct server *srv = objt_server(h2c->conn->target);
3058
3059 if (srv) {
3060 if (h2c->conn->flags & CO_FL_PRIVATE)
3061 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3062 else
3063 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3064 }
3065
3066 }
3067 }
3068 }
3069
Willy Tarreaue323f342018-03-28 13:51:45 +02003070 /* We don't want to close right now unless we're removing the
3071 * last stream, and either the connection is in error, or it
3072 * reached the ID already specified in a GOAWAY frame received
3073 * or sent (as seen by last_sid >= 0).
3074 */
3075 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3076 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003077 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003078 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003079 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003080 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003081 (conn_xprt_read0_pending(h2c->conn) ||
3082 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3083 /* no more stream will come, kill it now */
3084 h2_release(h2c->conn);
3085 }
3086 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003087 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003088 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3089 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003090 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003091 else
3092 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003093 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003094}
3095
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003096static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003097{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003098 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003099 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003100
Willy Tarreau721c9742017-11-07 11:05:42 +01003101 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003102 return;
3103
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003104 /* if no outgoing data was seen on this stream, it means it was
3105 * closed with a "tcp-request content" rule that is normally
3106 * used to kill the connection ASAP (eg: limit abuse). In this
3107 * case we send a goaway to close the connection.
3108 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003109 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003110 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003111 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003112
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003113 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3114 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003115 h2c_send_goaway_error(h2c, h2s) <= 0)
3116 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003117
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003118 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003119 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003120 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003121
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003122 return;
3123add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003124 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003125 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003126 if (h2s->flags & H2_SF_BLK_MFCTL) {
3127 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3128 h2s->send_wait = sw;
3129 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3130 h2s->send_wait = sw;
3131 LIST_ADDQ(&h2c->send_list, &h2s->list);
3132 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003133 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003134 /* Let the handler know we want shutr */
3135 sw->handle = (void *)((long)sw->handle | 1);
3136
Willy Tarreau62f52692017-10-08 23:01:42 +02003137}
3138
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003139static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003140{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003141 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003142 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003143
Willy Tarreau721c9742017-11-07 11:05:42 +01003144 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003145 return;
3146
Willy Tarreau67434202017-11-06 20:20:51 +01003147 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003148 /* we can cleanly close using an empty data frame only after headers */
3149
3150 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3151 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003152 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003153
3154 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003155 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003156 else
3157 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003158 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003159 /* if no outgoing data was seen on this stream, it means it was
3160 * closed with a "tcp-request content" rule that is normally
3161 * used to kill the connection ASAP (eg: limit abuse). In this
3162 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003163 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003164 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003165 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003166 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003167
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003168 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3169 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003170 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003171 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003172
Willy Tarreau00dd0782018-03-01 16:31:34 +01003173 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003174 }
3175
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003176 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003177 tasklet_wakeup(h2c->wait_event.task);
3178 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003179
3180 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003181 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003182 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003183 if (h2s->flags & H2_SF_BLK_MFCTL) {
3184 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3185 h2s->send_wait = sw;
3186 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3187 h2s->send_wait = sw;
3188 LIST_ADDQ(&h2c->send_list, &h2s->list);
3189 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003190 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003191 /* let the handler know we want to shutw */
3192 sw->handle = (void *)((long)(sw->handle) | 2);
3193
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194}
3195
3196static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3197{
3198 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003199 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003200
Olivier Houchard2c68a462018-12-15 22:42:20 +01003201 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003202 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003203 h2s->send_wait = NULL;
3204 LIST_DEL(&h2s->list);
3205 LIST_INIT(&h2s->list);
3206 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003207 if (reason & 2)
3208 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003209 if (reason & 1)
3210 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003211
Olivier Houchard2c68a462018-12-15 22:42:20 +01003212 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003213 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003214 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003215 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003216}
3217
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003218static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3219{
3220 struct h2s *h2s = cs->ctx;
3221
3222 if (!mode)
3223 return;
3224
3225 h2_do_shutr(h2s);
3226}
3227
3228static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3229{
3230 struct h2s *h2s = cs->ctx;
3231
3232 h2_do_shutw(h2s);
3233}
3234
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003235/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003236 * HTX request or response depending on the connection's side. Returns a
3237 * positive value on success, a negative value on failure, or 0 if it couldn't
3238 * proceed. May report connection errors in h2c->errcode if the frame is
3239 * non-decodable and the connection unrecoverable. In absence of connection
3240 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003241 *
3242 * The function may fold CONTINUATION frames into the initial HEADERS frame
3243 * by removing padding and next frame header, then moving the CONTINUATION
3244 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3245 * leaving a hole between the main frame and the beginning of the next one.
3246 * The possibly remaining incomplete or next frame at the end may be moved
3247 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3248 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3249 *
3250 * A buffer at the beginning of processing may look like this :
3251 *
3252 * ,---.---------.-----.--------------.--------------.------.---.
3253 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3254 * `---^---------^-----^--------------^--------------^------^---'
3255 * | | <-----> | |
3256 * area | dpl | wrap
3257 * |<--------------> |
3258 * | dfl |
3259 * |<-------------------------------------------------->|
3260 * head data
3261 *
3262 * Padding is automatically overwritten when folding, participating to the
3263 * hole size after dfl :
3264 *
3265 * ,---.------------------------.-----.--------------.------.---.
3266 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3267 * `---^------------------------^-----^--------------^------^---'
3268 * | | <-----> | |
3269 * area | hole | wrap
3270 * |<-----------------------> |
3271 * | dfl |
3272 * |<-------------------------------------------------->|
3273 * head data
3274 *
3275 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3276 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3277 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003278 *
3279 * The <flags> field must point to either the stream's flags or to a copy of it
3280 * so that the function can update the following flags :
3281 * - H2_SF_DATA_CLEN when content-length is seen
3282 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3283 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003284 *
3285 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3286 * decoding, in order to detect if we're dealing with a headers or a trailers
3287 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003288 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003289static 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 +02003290{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003291 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003292 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003293 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003294 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003295 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003296 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003297 int flen; // header frame len
3298 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003299 int ret = 0;
3300 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003301 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003302 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003303
Willy Tarreauea18f862018-12-22 20:19:26 +01003304next_frame:
3305 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3306 goto leave; // incomplete input frame
3307
3308 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3309 * this case, we'll try to paste it immediately after the initial
3310 * HEADERS frame payload and kill any possible padding. The initial
3311 * frame's length will be increased to represent the concatenation
3312 * of the two frames. The next frame is read from position <tlen>
3313 * and written at position <flen> (minus padding if some is present).
3314 */
3315 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3316 struct h2_fh hdr;
3317 int clen; // CONTINUATION frame's payload length
3318
3319 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3320 /* no more data, the buffer may be full, either due to
3321 * too large a frame or because of too large a hole that
3322 * we're going to compact at the end.
3323 */
3324 goto leave;
3325 }
3326
3327 if (hdr.ft != H2_FT_CONTINUATION) {
3328 /* RFC7540#6.10: frame of unexpected type */
3329 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3330 goto fail;
3331 }
3332
3333 if (hdr.sid != h2c->dsi) {
3334 /* RFC7540#6.10: frame of different stream */
3335 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3336 goto fail;
3337 }
3338
3339 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3340 /* RFC7540#4.2: invalid frame length */
3341 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3342 goto fail;
3343 }
3344
3345 /* detect when we must stop aggragating frames */
3346 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3347
3348 /* Take as much as we can of the CONTINUATION frame's payload */
3349 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3350 if (clen > hdr.len)
3351 clen = hdr.len;
3352
3353 /* Move the frame's payload over the padding, hole and frame
3354 * header. At least one of hole or dpl is null (see diagrams
3355 * above). The hole moves after the new aggragated frame.
3356 */
3357 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3358 h2c->dfl += clen - h2c->dpl;
3359 hole += h2c->dpl + 9;
3360 h2c->dpl = 0;
3361 goto next_frame;
3362 }
3363
3364 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003365
Willy Tarreau13278b42017-10-13 19:23:14 +02003366 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003367 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003368 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003369 copy = alloc_trash_chunk();
3370 if (!copy) {
3371 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3372 goto fail;
3373 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003374 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3375 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3376 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003377 }
3378
Willy Tarreau13278b42017-10-13 19:23:14 +02003379 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3380 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003381 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003382 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3383 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003384 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003385 }
3386
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003387 if (flen < 5) {
3388 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3389 goto fail;
3390 }
3391
Willy Tarreau13278b42017-10-13 19:23:14 +02003392 hdrs += 5; // stream dep = 4, weight = 1
3393 flen -= 5;
3394 }
3395
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003396 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003397 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003398 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003399 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003400
Willy Tarreau937f7602018-02-26 15:22:17 +01003401 /* we can't retry a failed decompression operation so we must be very
3402 * careful not to take any risks. In practice the output buffer is
3403 * always empty except maybe for trailers, in which case we simply have
3404 * to wait for the upper layer to finish consuming what is available.
3405 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003406
3407 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003408 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003409 if (!htx_is_empty(htx)) {
3410 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003411 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003412 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003413 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003414 if (b_data(rxbuf)) {
3415 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003416 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003417 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003418
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003419 rxbuf->head = 0;
3420 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003421 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003422
Willy Tarreau25919232019-01-03 14:48:18 +01003423 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003424 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3425 sizeof(list)/sizeof(list[0]), tmp);
3426 if (outlen < 0) {
3427 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3428 goto fail;
3429 }
3430
Willy Tarreau25919232019-01-03 14:48:18 +01003431 /* The PACK decompressor was updated, let's update the input buffer and
3432 * the parser's state to commit these changes and allow us to later
3433 * fail solely on the stream if needed.
3434 */
3435 b_del(&h2c->dbuf, h2c->dfl + hole);
3436 h2c->dfl = hole = 0;
3437 h2c->st0 = H2_CS_FRAME_H;
3438
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003439 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003440 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003441
Willy Tarreau88d138e2019-01-02 19:38:14 +01003442 if (*flags & H2_SF_HEADERS_RCVD)
3443 goto trailers;
3444
3445 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003446 if (htx) {
3447 /* HTX mode */
3448 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003449 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003450 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003451 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003452 } else {
3453 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003454 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003455 if (outlen > 0)
3456 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003457 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003458
3459 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003460 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003461 goto fail;
3462 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003463
Willy Tarreau174b06a2018-04-25 18:13:58 +02003464 if (msgf & H2_MSGF_BODY) {
3465 /* a payload is present */
3466 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003467 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003468 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003469 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003470 }
3471
Willy Tarreau88d138e2019-01-02 19:38:14 +01003472 done:
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003473 /* indicate that a HEADERS frame was received for this stream */
3474 *flags |= H2_SF_HEADERS_RCVD;
3475
Willy Tarreau88d138e2019-01-02 19:38:14 +01003476 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
3477 /* Mark the end of message, either using EOM in HTX or with the
3478 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3479 * is not set during headers with END_STREAM.
3480 */
3481 if (htx) {
3482 if (!htx_add_endof(htx, HTX_BLK_EOM))
3483 goto fail;
3484 }
3485 else if (*flags & H2_SF_DATA_CHNK) {
3486 if (!b_putblk(rxbuf, "\r\n", 2))
3487 goto fail;
3488 }
3489 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003490
Willy Tarreau86277d42019-01-02 15:36:11 +01003491 /* success */
3492 ret = 1;
3493
Willy Tarreau68dd9852017-07-03 14:44:26 +02003494 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003495 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003496 * move the remaining data over it.
3497 */
3498 if (hole) {
3499 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3500 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3501 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3502 b_sub(&h2c->dbuf, hole);
3503 }
3504
3505 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3506 /* too large frames */
3507 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003508 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003509 }
3510
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003511 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003512 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003513 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003514 return ret;
3515
Willy Tarreau68dd9852017-07-03 14:44:26 +02003516 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003517 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003518 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003519
3520 trailers:
3521 /* This is the last HEADERS frame hence a trailer */
3522
3523 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3524 /* It's a trailer but it's missing ES flag */
3525 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3526 goto fail;
3527 }
3528
3529 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3530 * block, and when using chunks we must send the 0 CRLF marker. For
3531 * other modes, the trailers are silently dropped.
3532 */
3533 if (htx) {
3534 if (!htx_add_endof(htx, HTX_BLK_EOD))
3535 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003536 if (h2_make_htx_trailers(list, htx) <= 0)
3537 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003538 }
3539 else if (*flags & H2_SF_DATA_CHNK) {
3540 /* Legacy mode with chunked encoding : we must finalize the
3541 * data block message emit the trailing CRLF */
3542 if (!b_putblk(rxbuf, "0\r\n", 3))
3543 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003544
3545 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3546 if (outlen > 0)
3547 b_add(rxbuf, outlen);
3548 else
3549 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003550 }
3551
3552 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003553}
3554
Willy Tarreau454f9052017-10-26 19:40:35 +02003555/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3556 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3557 * in use, a new chunk is emitted for each frame. This is supposed to fit
3558 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3559 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3560 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003561 * parser state is automatically updated. Returns > 0 if it could completely
3562 * send the current frame, 0 if it couldn't complete, in which case
3563 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3564 * DATA frame can return 0 as a valid result). Stream errors are reported in
3565 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3566 * have checked the frame header and ensured that the frame was complete or the
3567 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003568 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003569static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003570{
3571 struct h2c *h2c = h2s->h2c;
3572 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003573 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003574 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003575 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003576 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003577
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003578 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003579
Olivier Houchard638b7992018-08-16 15:41:52 +02003580 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003581 if (!csbuf) {
3582 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003583 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003584 }
3585
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003586try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003587 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003588 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3589 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003590 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003591 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003592
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003593 if (flen > b_data(&h2c->dbuf)) {
3594 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003595 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003596 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003597 }
3598
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003599 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003600 block1 = htx_free_data_space(htx);
3601 if (!block1) {
3602 h2c->flags |= H2_CF_DEM_SFULL;
3603 goto fail;
3604 }
3605 if (flen > block1)
3606 flen = block1;
3607
3608 /* here, flen is the max we can copy into the output buffer */
3609 block1 = b_contig_data(&h2c->dbuf, 0);
3610 if (flen > block1)
3611 flen = block1;
3612
3613 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3614 h2c->flags |= H2_CF_DEM_SFULL;
3615 goto fail;
3616 }
3617
3618 b_del(&h2c->dbuf, flen);
3619 h2c->dfl -= flen;
3620 h2c->rcvd_c += flen;
3621 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003622
3623 if (h2s->flags & H2_SF_DATA_CLEN)
3624 h2s->body_len -= flen;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003625 goto try_again;
3626 }
3627 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003628 /* it doesn't fit and the buffer is fragmented,
3629 * so let's defragment it and try again.
3630 */
3631 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003632 }
3633
Willy Tarreaueba10f22018-04-25 20:44:22 +02003634 /* chunked-encoding requires more room */
3635 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003636 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003637 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3638 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3639 (chklen < 1048576) ? 4 : 8;
3640 chklen += 4; // CRLF, CRLF
3641 }
3642
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003643 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003644 if (flen + chklen > b_room(csbuf)) {
3645 if (chklen >= b_room(csbuf)) {
3646 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003647 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003648 }
3649 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003650 }
3651
3652 if (h2s->flags & H2_SF_DATA_CHNK) {
3653 /* emit the chunk size */
3654 unsigned int chksz = flen;
3655 char str[10];
3656 char *beg;
3657
3658 beg = str + sizeof(str);
3659 *--beg = '\n';
3660 *--beg = '\r';
3661 do {
3662 *--beg = hextab[chksz & 0xF];
3663 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003664 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003665 }
3666
Willy Tarreau454f9052017-10-26 19:40:35 +02003667 /* Block1 is the length of the first block before the buffer wraps,
3668 * block2 is the optional second block to reach the end of the frame.
3669 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003670 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003671 if (block1 > flen)
3672 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003673 block2 = flen - block1;
3674
3675 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003676 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003677
3678 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003679 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003680
Willy Tarreaueba10f22018-04-25 20:44:22 +02003681 if (h2s->flags & H2_SF_DATA_CHNK) {
3682 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003683 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003684 }
3685
Willy Tarreau454f9052017-10-26 19:40:35 +02003686 /* now mark the input data as consumed (will be deleted from the buffer
3687 * by the caller when seeing FRAME_A after sending the window update).
3688 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003689 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003690 h2c->dfl -= flen;
3691 h2c->rcvd_c += flen;
3692 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3693
Willy Tarreau1915ca22019-01-24 11:49:37 +01003694 if (h2s->flags & H2_SF_DATA_CLEN)
3695 h2s->body_len -= flen;
3696
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003697 if (h2c->dfl > h2c->dpl) {
3698 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003699 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003700 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003701 }
3702
Willy Tarreau4a28da12018-01-04 14:41:00 +01003703 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003704 /* here we're done with the frame, all the payload (except padding) was
3705 * transferred.
3706 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003707
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003708 if (h2c->dff & H2_F_DATA_END_STREAM) {
3709 if (htx) {
3710 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3711 h2c->flags |= H2_CF_DEM_SFULL;
3712 goto fail;
3713 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003714 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003715 else if (h2s->flags & H2_SF_DATA_CHNK) {
3716 /* emit the trailing 0 CRLF CRLF */
3717 if (b_room(csbuf) < 5) {
3718 h2c->flags |= H2_CF_DEM_SFULL;
3719 goto fail;
3720 }
3721 chklen += 5;
3722 b_putblk(csbuf, "0\r\n\r\n", 5);
3723 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003724 }
3725
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003726 h2c->rcvd_c += h2c->dpl;
3727 h2c->rcvd_s += h2c->dpl;
3728 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003729 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3730
Willy Tarreau39d68502018-03-02 12:26:37 +01003731 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003732 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003733 h2s->cs->flags |= CS_FL_REOS;
3734 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003735 if (htx)
3736 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003737 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003738 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003739 if (htx)
3740 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003741 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003742}
3743
Willy Tarreau5dd17352018-06-14 13:33:30 +02003744/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3745 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3746 * number of bytes sent. The caller must check the stream's status to detect
3747 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003748 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003749static 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 +02003750{
3751 struct http_hdr list[MAX_HTTP_HDR];
3752 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003753 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003754 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003755 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003756 int es_now = 0;
3757 int ret = 0;
3758 int hdr;
3759
3760 if (h2c_mux_busy(h2c, h2s)) {
3761 h2s->flags |= H2_SF_BLK_MBUSY;
3762 return 0;
3763 }
3764
Willy Tarreau44e973f2018-03-01 17:49:30 +01003765 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003766 h2c->flags |= H2_CF_MUX_MALLOC;
3767 h2s->flags |= H2_SF_BLK_MROOM;
3768 return 0;
3769 }
3770
3771 /* First, try to parse the H1 response and index it into <list>.
3772 * NOTE! Since it comes from haproxy, we *know* that a response header
3773 * block does not wrap and we can safely read it this way without
3774 * having to realign the buffer.
3775 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003776 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003777 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003778 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003779 /* incomplete or invalid response, this is abnormal coming from
3780 * haproxy and may only result in a bad errorfile or bad Lua code
3781 * so that won't be fixed, raise an error now.
3782 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003783 * FIXME: we should instead add the ability to only return a
3784 * 502 bad gateway. But in theory this is not supposed to
3785 * happen.
3786 */
3787 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3788 ret = 0;
3789 goto end;
3790 }
3791
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003792 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003793
3794 /* certain statuses have no body or an empty one, regardless of
3795 * what the headers say.
3796 */
3797 if (sl.st.status >= 100 && sl.st.status < 200) {
3798 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3799 h1m->curr_len = h1m->body_len = 0;
3800 }
3801 else if (sl.st.status == 204 || sl.st.status == 304) {
3802 /* no contents, claim c-len is present and set to zero */
3803 h1m->flags &= ~H1_MF_CHNK;
3804 h1m->flags |= H1_MF_CLEN;
3805 h1m->curr_len = h1m->body_len = 0;
3806 }
3807
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003808 chunk_reset(&outbuf);
3809
3810 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003811 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003812 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003813 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003814
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003815 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003816 break;
3817 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003818 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003819 }
3820
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003821 if (outbuf.size < 9)
3822 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003823
3824 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003825 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3826 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3827 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003828
3829 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003830 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003831 /* this is an unparsable response */
3832 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3833 ret = 0;
3834 goto end;
3835 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003836
3837 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003838 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003839 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003840 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003841 }
3842
3843 /* encode all headers, stop at empty name */
3844 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003845 /* these ones do not exist in H2 and must be dropped. */
3846 if (isteq(list[hdr].n, ist("connection")) ||
3847 isteq(list[hdr].n, ist("proxy-connection")) ||
3848 isteq(list[hdr].n, ist("keep-alive")) ||
3849 isteq(list[hdr].n, ist("upgrade")) ||
3850 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003851 continue;
3852
3853 if (isteq(list[hdr].n, ist("")))
3854 break; // end
3855
3856 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3857 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003858 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003860 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 }
3862 }
3863
3864 /* we may need to add END_STREAM */
3865 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3866 es_now = 1;
3867
3868 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003869 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003870
3871 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003872 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003873
3874 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003875 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003876
3877 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003878 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003879 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003880
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003881 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003882 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003883 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003884
Willy Tarreau801250e2018-09-11 11:45:04 +02003885 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003886 h2s->flags |= H2_SF_ES_SENT;
3887 if (h2s->st == H2_SS_OPEN)
3888 h2s->st = H2_SS_HLOC;
3889 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003890 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003891 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003892 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003893 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003894 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003895 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003896 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003897 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003898 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003899
3900 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003901
3902 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003903 //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 +02003904 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003905 full:
3906 h1m_init_res(h1m);
3907 h1m->err_pos = -1; // don't care about errors on the response path
3908 h2c->flags |= H2_CF_MUX_MFULL;
3909 h2s->flags |= H2_SF_BLK_MROOM;
3910 ret = 0;
3911 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912}
3913
Willy Tarreau5dd17352018-06-14 13:33:30 +02003914/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3915 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3916 * the number of bytes sent. The caller must check the stream's status to
3917 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003918 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003919static 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 +02003920{
3921 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003922 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003923 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003924 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003925 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003926 int es_now = 0;
3927 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003928 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003929 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003930
3931 if (h2c_mux_busy(h2c, h2s)) {
3932 h2s->flags |= H2_SF_BLK_MBUSY;
3933 goto end;
3934 }
3935
Willy Tarreau44e973f2018-03-01 17:49:30 +01003936 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003937 h2c->flags |= H2_CF_MUX_MALLOC;
3938 h2s->flags |= H2_SF_BLK_MROOM;
3939 goto end;
3940 }
3941
3942 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003943 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003944 goto end;
3945
3946 chunk_reset(&outbuf);
3947
3948 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003949 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003950 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003951 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003952
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003953 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003954 break;
3955 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003956 /* If there are pending data in the output buffer, and we have
3957 * less than 1/4 of the mbuf's size and everything fits, we'll
3958 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3959 * is full and wait, to save some slow realign calls.
3960 */
3961 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3962 h2c->flags |= H2_CF_MUX_MFULL;
3963 h2s->flags |= H2_SF_BLK_MROOM;
3964 goto end;
3965 }
3966
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003967 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 }
3969
3970 if (outbuf.size < 9) {
3971 h2c->flags |= H2_CF_MUX_MFULL;
3972 h2s->flags |= H2_SF_BLK_MROOM;
3973 goto end;
3974 }
3975
3976 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3978 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3979 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980
3981 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3982 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003983 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003984 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003985 break;
3986 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003987 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003988 if ((long long)size > h1m->curr_len)
3989 size = h1m->curr_len;
3990 break;
3991 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003992 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003993 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003994 if (!ret)
3995 goto end;
3996
3997 if (ret < 0) {
3998 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003999 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004000 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4001 goto end;
4002 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004003 max -= ret;
4004 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004005 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004006 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004007 }
4008
Willy Tarreau801250e2018-09-11 11:45:04 +02004009 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004010 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004011 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004012 if (!ret)
4013 goto end;
4014
4015 if (ret < 0) {
4016 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004017 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004018 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4019 goto end;
4020 }
4021
4022 size = chunk;
4023 h1m->curr_len = chunk;
4024 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004025 max -= ret;
4026 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004028 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 if (!size)
4030 goto send_empty;
4031 }
4032
4033 /* in MSG_DATA state, continue below */
4034 size = h1m->curr_len;
4035 break;
4036 }
4037
4038 /* we have in <size> the exact number of bytes we need to copy from
4039 * the H1 buffer. We need to check this against the connection's and
4040 * the stream's send windows, and to ensure that this fits in the max
4041 * frame size and in the buffer's available space minus 9 bytes (for
4042 * the frame header). The connection's flow control is applied last so
4043 * that we can use a separate list of streams which are immediately
4044 * unblocked on window opening. Note: we don't implement padding.
4045 */
4046
Willy Tarreau5dd17352018-06-14 13:33:30 +02004047 if (size > max)
4048 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049
4050 if (size > h2s->mws)
4051 size = h2s->mws;
4052
4053 if (size <= 0) {
4054 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004055 if (h2s->send_wait) {
4056 LIST_DEL(&h2s->list);
4057 LIST_INIT(&h2s->list);
4058 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 goto end;
4060 }
4061
4062 if (h2c->mfs && size > h2c->mfs)
4063 size = h2c->mfs;
4064
4065 if (size + 9 > outbuf.size) {
4066 /* we have an opportunity for enlarging the too small
4067 * available space, let's try.
4068 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004069 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004070 goto realign_again;
4071 size = outbuf.size - 9;
4072 }
4073
4074 if (size <= 0) {
4075 h2c->flags |= H2_CF_MUX_MFULL;
4076 h2s->flags |= H2_SF_BLK_MROOM;
4077 goto end;
4078 }
4079
4080 if (size > h2c->mws)
4081 size = h2c->mws;
4082
4083 if (size <= 0) {
4084 h2s->flags |= H2_SF_BLK_MFCTL;
4085 goto end;
4086 }
4087
4088 /* copy whatever we can */
4089 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004090 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004091 if (ret == 1)
4092 len2 = 0;
4093
4094 if (!ret || len1 + len2 < size) {
4095 /* FIXME: must normally never happen */
4096 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4097 goto end;
4098 }
4099
4100 /* limit len1/len2 to size */
4101 if (len1 + len2 > size) {
4102 int sub = len1 + len2 - size;
4103
4104 if (len2 > sub)
4105 len2 -= sub;
4106 else {
4107 sub -= len2;
4108 len2 = 0;
4109 len1 -= sub;
4110 }
4111 }
4112
4113 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004114 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004115 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004116 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004117
4118 send_empty:
4119 /* we may need to add END_STREAM */
4120 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4121 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004122 *
4123 * FIXME: what we do here is not correct because we send end_stream
4124 * before knowing if we'll have to send a HEADERS frame for the
4125 * trailers. More importantly we're not consuming the trailing CRLF
4126 * after the end of trailers, so it will be left to the caller to
4127 * eat it. The right way to do it would be to measure trailers here
4128 * and to send ES only if there are no trailers.
4129 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004130 */
4131 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004132 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 es_now = 1;
4134
4135 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004136 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004137
4138 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004139 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004140
4141 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004142 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004143
4144 /* consume incoming H1 response */
4145 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004146 max -= size;
4147 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004148 total += size;
4149 h1m->curr_len -= size;
4150 h2s->mws -= size;
4151 h2c->mws -= size;
4152
4153 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004154 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004155 goto new_frame;
4156 }
4157 }
4158
4159 if (es_now) {
4160 if (h2s->st == H2_SS_OPEN)
4161 h2s->st = H2_SS_HLOC;
4162 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004163 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004164
Willy Tarreau35a62702018-02-27 15:37:25 +01004165 if (!(h1m->flags & H1_MF_CHNK)) {
4166 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004167 total += max;
4168 ofs += max;
4169 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004170
Willy Tarreau801250e2018-09-11 11:45:04 +02004171 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004172 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004173
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004174 h2s->flags |= H2_SF_ES_SENT;
4175 }
4176
4177 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004178 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 +02004179 return total;
4180}
4181
Willy Tarreau115e83b2018-12-01 19:17:53 +01004182/* Try to send a HEADERS frame matching HTX response present in HTX message
4183 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4184 * must check the stream's status to detect any error which might have happened
4185 * subsequently to a successful send. The htx blocks are automatically removed
4186 * from the message. The htx message is assumed to be valid since produced from
4187 * the internal code, hence it contains a start line, an optional series of
4188 * header blocks and an end of header, otherwise an invalid frame could be
4189 * emitted and the resulting htx message could be left in an inconsistent state.
4190 */
4191static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4192{
4193 struct http_hdr list[MAX_HTTP_HDR];
4194 struct h2c *h2c = h2s->h2c;
4195 struct htx_blk *blk;
4196 struct htx_blk *blk_end;
4197 struct buffer outbuf;
4198 struct htx_sl *sl;
4199 enum htx_blk_type type;
4200 int es_now = 0;
4201 int ret = 0;
4202 int hdr;
4203 int idx;
4204
4205 if (h2c_mux_busy(h2c, h2s)) {
4206 h2s->flags |= H2_SF_BLK_MBUSY;
4207 return 0;
4208 }
4209
4210 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4211 h2c->flags |= H2_CF_MUX_MALLOC;
4212 h2s->flags |= H2_SF_BLK_MROOM;
4213 return 0;
4214 }
4215
4216 /* determine the first block which must not be deleted, blk_end may
4217 * be NULL if all blocks have to be deleted.
4218 */
4219 idx = htx_get_head(htx);
4220 blk_end = NULL;
4221 while (idx != -1) {
4222 type = htx_get_blk_type(htx_get_blk(htx, idx));
4223 idx = htx_get_next(htx, idx);
4224 if (type == HTX_BLK_EOH) {
4225 if (idx != -1)
4226 blk_end = htx_get_blk(htx, idx);
4227 break;
4228 }
4229 }
4230
4231 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004232 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004233 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004234 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004235 if (h2s->status < 100 || h2s->status > 999)
4236 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004237
4238 /* and the rest of the headers, that we dump starting at header 0 */
4239 hdr = 0;
4240
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004241 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004242 while ((idx = htx_get_next(htx, idx)) != -1) {
4243 blk = htx_get_blk(htx, idx);
4244 type = htx_get_blk_type(blk);
4245
4246 if (type == HTX_BLK_UNUSED)
4247 continue;
4248
4249 if (type != HTX_BLK_HDR)
4250 break;
4251
4252 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4253 goto fail;
4254
4255 list[hdr].n = htx_get_blk_name(htx, blk);
4256 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004257 hdr++;
4258 }
4259
4260 /* marker for end of headers */
4261 list[hdr].n = ist("");
4262
4263 if (h2s->status == 204 || h2s->status == 304) {
4264 /* no contents, claim c-len is present and set to zero */
4265 es_now = 1;
4266 }
4267
4268 chunk_reset(&outbuf);
4269
4270 while (1) {
4271 outbuf.area = b_tail(&h2c->mbuf);
4272 outbuf.size = b_contig_space(&h2c->mbuf);
4273 outbuf.data = 0;
4274
4275 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4276 break;
4277 realign_again:
4278 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4279 }
4280
4281 if (outbuf.size < 9)
4282 goto full;
4283
4284 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4285 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4286 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4287 outbuf.data = 9;
4288
4289 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004290 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004291 if (b_space_wraps(&h2c->mbuf))
4292 goto realign_again;
4293 goto full;
4294 }
4295
4296 /* encode all headers, stop at empty name */
4297 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4298 /* these ones do not exist in H2 and must be dropped. */
4299 if (isteq(list[hdr].n, ist("connection")) ||
4300 isteq(list[hdr].n, ist("proxy-connection")) ||
4301 isteq(list[hdr].n, ist("keep-alive")) ||
4302 isteq(list[hdr].n, ist("upgrade")) ||
4303 isteq(list[hdr].n, ist("transfer-encoding")))
4304 continue;
4305
4306 if (isteq(list[hdr].n, ist("")))
4307 break; // end
4308
4309 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4310 /* output full */
4311 if (b_space_wraps(&h2c->mbuf))
4312 goto realign_again;
4313 goto full;
4314 }
4315 }
4316
4317 /* we may need to add END_STREAM.
4318 * FIXME: we should also set it when we know for sure that the
4319 * content-length is zero as well as on 204/304
4320 */
4321 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4322 es_now = 1;
4323
4324 if (h2s->cs->flags & CS_FL_SHW)
4325 es_now = 1;
4326
4327 /* update the frame's size */
4328 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4329
4330 if (es_now)
4331 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4332
4333 /* commit the H2 response */
4334 b_add(&h2c->mbuf, outbuf.data);
4335 h2s->flags |= H2_SF_HEADERS_SENT;
4336
Willy Tarreau115e83b2018-12-01 19:17:53 +01004337 if (es_now) {
4338 h2s->flags |= H2_SF_ES_SENT;
4339 if (h2s->st == H2_SS_OPEN)
4340 h2s->st = H2_SS_HLOC;
4341 else
4342 h2s_close(h2s);
4343 }
4344
4345 /* OK we could properly deliver the response */
4346
4347 /* remove all header blocks including the EOH and compute the
4348 * corresponding size.
4349 *
4350 * FIXME: We should remove everything when es_now is set.
4351 */
4352 ret = 0;
4353 idx = htx_get_head(htx);
4354 blk = htx_get_blk(htx, idx);
4355 while (blk != blk_end) {
4356 ret += htx_get_blksz(blk);
4357 blk = htx_remove_blk(htx, blk);
4358 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004359
4360 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4361 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004362 end:
4363 return ret;
4364 full:
4365 h2c->flags |= H2_CF_MUX_MFULL;
4366 h2s->flags |= H2_SF_BLK_MROOM;
4367 ret = 0;
4368 goto end;
4369 fail:
4370 /* unparsable HTX messages, too large ones to be produced in the local
4371 * list etc go here (unrecoverable errors).
4372 */
4373 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4374 ret = 0;
4375 goto end;
4376}
4377
Willy Tarreau80739692018-10-05 11:35:57 +02004378/* Try to send a HEADERS frame matching HTX request present in HTX message
4379 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4380 * must check the stream's status to detect any error which might have happened
4381 * subsequently to a successful send. The htx blocks are automatically removed
4382 * from the message. The htx message is assumed to be valid since produced from
4383 * the internal code, hence it contains a start line, an optional series of
4384 * header blocks and an end of header, otherwise an invalid frame could be
4385 * emitted and the resulting htx message could be left in an inconsistent state.
4386 */
4387static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4388{
4389 struct http_hdr list[MAX_HTTP_HDR];
4390 struct h2c *h2c = h2s->h2c;
4391 struct htx_blk *blk;
4392 struct htx_blk *blk_end;
4393 struct buffer outbuf;
4394 struct htx_sl *sl;
4395 struct ist meth, path;
4396 enum htx_blk_type type;
4397 int es_now = 0;
4398 int ret = 0;
4399 int hdr;
4400 int idx;
4401
4402 if (h2c_mux_busy(h2c, h2s)) {
4403 h2s->flags |= H2_SF_BLK_MBUSY;
4404 return 0;
4405 }
4406
4407 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4408 h2c->flags |= H2_CF_MUX_MALLOC;
4409 h2s->flags |= H2_SF_BLK_MROOM;
4410 return 0;
4411 }
4412
4413 /* determine the first block which must not be deleted, blk_end may
4414 * be NULL if all blocks have to be deleted.
4415 */
4416 idx = htx_get_head(htx);
4417 blk_end = NULL;
4418 while (idx != -1) {
4419 type = htx_get_blk_type(htx_get_blk(htx, idx));
4420 idx = htx_get_next(htx, idx);
4421 if (type == HTX_BLK_EOH) {
4422 if (idx != -1)
4423 blk_end = htx_get_blk(htx, idx);
4424 break;
4425 }
4426 }
4427
4428 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004429 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004430 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004431 meth = htx_sl_req_meth(sl);
4432 path = htx_sl_req_uri(sl);
4433
4434 /* and the rest of the headers, that we dump starting at header 0 */
4435 hdr = 0;
4436
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004437 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004438 while ((idx = htx_get_next(htx, idx)) != -1) {
4439 blk = htx_get_blk(htx, idx);
4440 type = htx_get_blk_type(blk);
4441
4442 if (type == HTX_BLK_UNUSED)
4443 continue;
4444
4445 if (type != HTX_BLK_HDR)
4446 break;
4447
4448 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4449 goto fail;
4450
4451 list[hdr].n = htx_get_blk_name(htx, blk);
4452 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004453 hdr++;
4454 }
4455
4456 /* marker for end of headers */
4457 list[hdr].n = ist("");
4458
4459 chunk_reset(&outbuf);
4460
4461 while (1) {
4462 outbuf.area = b_tail(&h2c->mbuf);
4463 outbuf.size = b_contig_space(&h2c->mbuf);
4464 outbuf.data = 0;
4465
4466 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4467 break;
4468 realign_again:
4469 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4470 }
4471
4472 if (outbuf.size < 9)
4473 goto full;
4474
4475 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4476 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4477 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4478 outbuf.data = 9;
4479
4480 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004481 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004482 if (b_space_wraps(&h2c->mbuf))
4483 goto realign_again;
4484 goto full;
4485 }
4486
4487 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004488 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4489 /* output full */
4490 if (b_space_wraps(&h2c->mbuf))
4491 goto realign_again;
4492 goto full;
4493 }
Willy Tarreau80739692018-10-05 11:35:57 +02004494
4495 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004496 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004497 /* output full */
4498 if (b_space_wraps(&h2c->mbuf))
4499 goto realign_again;
4500 goto full;
4501 }
4502
4503 /* encode all headers, stop at empty name */
4504 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4505 /* these ones do not exist in H2 and must be dropped. */
4506 if (isteq(list[hdr].n, ist("connection")) ||
4507 isteq(list[hdr].n, ist("proxy-connection")) ||
4508 isteq(list[hdr].n, ist("keep-alive")) ||
4509 isteq(list[hdr].n, ist("upgrade")) ||
4510 isteq(list[hdr].n, ist("transfer-encoding")))
4511 continue;
4512
4513 if (isteq(list[hdr].n, ist("")))
4514 break; // end
4515
4516 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4517 /* output full */
4518 if (b_space_wraps(&h2c->mbuf))
4519 goto realign_again;
4520 goto full;
4521 }
4522 }
4523
4524 /* we may need to add END_STREAM if we have no body :
4525 * - request already closed, or :
4526 * - no transfer-encoding, and :
4527 * - no content-length or content-length:0
4528 * Fixme: this doesn't take into account CONNECT requests.
4529 */
4530 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4531 es_now = 1;
4532
4533 if (sl->flags & HTX_SL_F_BODYLESS)
4534 es_now = 1;
4535
4536 if (h2s->cs->flags & CS_FL_SHW)
4537 es_now = 1;
4538
4539 /* update the frame's size */
4540 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4541
4542 if (es_now)
4543 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4544
4545 /* commit the H2 response */
4546 b_add(&h2c->mbuf, outbuf.data);
4547 h2s->flags |= H2_SF_HEADERS_SENT;
4548 h2s->st = H2_SS_OPEN;
4549
Willy Tarreau80739692018-10-05 11:35:57 +02004550 if (es_now) {
4551 // trim any possibly pending data (eg: inconsistent content-length)
4552 h2s->flags |= H2_SF_ES_SENT;
4553 h2s->st = H2_SS_HLOC;
4554 }
4555
4556 /* remove all header blocks including the EOH and compute the
4557 * corresponding size.
4558 *
4559 * FIXME: We should remove everything when es_now is set.
4560 */
4561 ret = 0;
4562 idx = htx_get_head(htx);
4563 blk = htx_get_blk(htx, idx);
4564 while (blk != blk_end) {
4565 ret += htx_get_blksz(blk);
4566 blk = htx_remove_blk(htx, blk);
4567 }
4568
4569 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4570 htx_remove_blk(htx, blk_end);
4571
4572 end:
4573 return ret;
4574 full:
4575 h2c->flags |= H2_CF_MUX_MFULL;
4576 h2s->flags |= H2_SF_BLK_MROOM;
4577 ret = 0;
4578 goto end;
4579 fail:
4580 /* unparsable HTX messages, too large ones to be produced in the local
4581 * list etc go here (unrecoverable errors).
4582 */
4583 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4584 ret = 0;
4585 goto end;
4586}
4587
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004588/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004589 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4590 * caller must check the stream's status to detect any error which might have
4591 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004592 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4593 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004594static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004595{
4596 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004597 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004598 struct buffer outbuf;
4599 size_t total = 0;
4600 int es_now = 0;
4601 int bsize; /* htx block size */
4602 int fsize; /* h2 frame size */
4603 struct htx_blk *blk;
4604 enum htx_blk_type type;
4605 int idx;
4606
4607 if (h2c_mux_busy(h2c, h2s)) {
4608 h2s->flags |= H2_SF_BLK_MBUSY;
4609 goto end;
4610 }
4611
4612 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4613 h2c->flags |= H2_CF_MUX_MALLOC;
4614 h2s->flags |= H2_SF_BLK_MROOM;
4615 goto end;
4616 }
4617
Willy Tarreau98de12a2018-12-12 07:03:00 +01004618 htx = htx_from_buf(buf);
4619
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004620 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4621 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4622 * the caller to handle.
4623 */
4624
4625 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004626 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004627 goto end;
4628
4629 idx = htx_get_head(htx);
4630 blk = htx_get_blk(htx, idx);
4631 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4632 bsize = htx_get_blksz(blk);
4633 fsize = bsize;
4634
4635 if (type == HTX_BLK_EOD) {
4636 /* if we have an EOD, we're dealing with chunked data. We may
4637 * have a set of trailers after us that the caller will want to
4638 * deal with. Let's simply remove the EOD and return.
4639 */
4640 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004641 total++; // EOD counts as one byte
4642 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004643 goto end;
4644 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004645 else if (type == HTX_BLK_EOM) {
4646 if (h2s->flags & H2_SF_ES_SENT) {
4647 /* ES already sent */
4648 htx_remove_blk(htx, blk);
4649 total++; // EOM counts as one byte
4650 count--;
4651 goto end;
4652 }
4653 }
4654 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004655 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004656
4657 /* Perform some optimizations to reduce the number of buffer copies.
4658 * First, if the mux's buffer is empty and the htx area contains
4659 * exactly one data block of the same size as the requested count, and
4660 * this count fits within the frame size, the stream's window size, and
4661 * the connection's window size, then it's possible to simply swap the
4662 * caller's buffer with the mux's output buffer and adjust offsets and
4663 * length to match the entire DATA HTX block in the middle. In this
4664 * case we perform a true zero-copy operation from end-to-end. This is
4665 * the situation that happens all the time with large files. Second, if
4666 * this is not possible, but the mux's output buffer is empty, we still
4667 * have an opportunity to avoid the copy to the intermediary buffer, by
4668 * making the intermediary buffer's area point to the output buffer's
4669 * area. In this case we want to skip the HTX header to make sure that
4670 * copies remain aligned and that this operation remains possible all
4671 * the time. This goes for headers, data blocks and any data extracted
4672 * from the HTX blocks.
4673 */
4674 if (unlikely(fsize == count &&
4675 htx->used == 1 && type == HTX_BLK_DATA &&
4676 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4677 void *old_area = h2c->mbuf.area;
4678
4679 if (b_data(&h2c->mbuf)) {
4680 /* too bad there are data left there. If we have less
4681 * than 1/4 of the mbuf's size and everything fits,
4682 * we'll perform a copy anyway. Otherwise we'll pretend
4683 * the mbuf is full and wait.
4684 */
4685 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4686 goto copy;
4687 h2c->flags |= H2_CF_MUX_MFULL;
4688 h2s->flags |= H2_SF_BLK_MROOM;
4689 goto end;
4690 }
4691
4692 /* map an H2 frame to the HTX block so that we can put the
4693 * frame header there.
4694 */
4695 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004696 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004697 h2c->mbuf.data = fsize + 9;
4698 outbuf.area = b_head(&h2c->mbuf);
4699
4700 /* prepend an H2 DATA frame header just before the DATA block */
4701 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4702 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4703 h2_set_frame_size(outbuf.area, fsize);
4704
4705 /* update windows */
4706 h2s->mws -= fsize;
4707 h2c->mws -= fsize;
4708
4709 /* and exchange with our old area */
4710 buf->area = old_area;
4711 buf->data = buf->head = 0;
4712 total += fsize;
4713 goto end;
4714 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004715
Willy Tarreau98de12a2018-12-12 07:03:00 +01004716 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004717 /* for DATA and EOM we'll have to emit a frame, even if empty */
4718
4719 while (1) {
4720 outbuf.area = b_tail(&h2c->mbuf);
4721 outbuf.size = b_contig_space(&h2c->mbuf);
4722 outbuf.data = 0;
4723
4724 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4725 break;
4726 realign_again:
4727 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4728 }
4729
4730 if (outbuf.size < 9) {
4731 h2c->flags |= H2_CF_MUX_MFULL;
4732 h2s->flags |= H2_SF_BLK_MROOM;
4733 goto end;
4734 }
4735
4736 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4737 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4738 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4739 outbuf.data = 9;
4740
4741 /* we have in <fsize> the exact number of bytes we need to copy from
4742 * the HTX buffer. We need to check this against the connection's and
4743 * the stream's send windows, and to ensure that this fits in the max
4744 * frame size and in the buffer's available space minus 9 bytes (for
4745 * the frame header). The connection's flow control is applied last so
4746 * that we can use a separate list of streams which are immediately
4747 * unblocked on window opening. Note: we don't implement padding.
4748 */
4749
4750 /* EOM is presented with bsize==1 but would lead to the emission of an
4751 * empty frame, thus we force it to zero here.
4752 */
4753 if (type == HTX_BLK_EOM)
4754 bsize = fsize = 0;
4755
4756 if (!fsize)
4757 goto send_empty;
4758
4759 if (h2s->mws <= 0) {
4760 h2s->flags |= H2_SF_BLK_SFCTL;
4761 if (h2s->send_wait) {
4762 LIST_DEL(&h2s->list);
4763 LIST_INIT(&h2s->list);
4764 }
4765 goto end;
4766 }
4767
Willy Tarreauee573762018-12-04 15:25:57 +01004768 if (fsize > count)
4769 fsize = count;
4770
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004771 if (fsize > h2s->mws)
4772 fsize = h2s->mws; // >0
4773
4774 if (h2c->mfs && fsize > h2c->mfs)
4775 fsize = h2c->mfs; // >0
4776
4777 if (fsize + 9 > outbuf.size) {
4778 /* we have an opportunity for enlarging the too small
4779 * available space, let's try.
4780 * FIXME: is this really interesting to do? Maybe we'll
4781 * spend lots of time realigning instead of using two
4782 * frames.
4783 */
4784 if (b_space_wraps(&h2c->mbuf))
4785 goto realign_again;
4786 fsize = outbuf.size - 9;
4787
4788 if (fsize <= 0) {
4789 /* no need to send an empty frame here */
4790 h2c->flags |= H2_CF_MUX_MFULL;
4791 h2s->flags |= H2_SF_BLK_MROOM;
4792 goto end;
4793 }
4794 }
4795
4796 if (h2c->mws <= 0) {
4797 h2s->flags |= H2_SF_BLK_MFCTL;
4798 goto end;
4799 }
4800
4801 if (fsize > h2c->mws)
4802 fsize = h2c->mws;
4803
4804 /* now let's copy this this into the output buffer */
4805 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004806 h2s->mws -= fsize;
4807 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004808 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004809
4810 send_empty:
4811 /* update the frame's size */
4812 h2_set_frame_size(outbuf.area, fsize);
4813
4814 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4815 * meeting EOM. We should optimize this later.
4816 */
4817 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004818 total++; // EOM counts as one byte
4819 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004820 es_now = 1;
4821 }
4822
4823 if (es_now)
4824 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4825
4826 /* commit the H2 response */
4827 b_add(&h2c->mbuf, fsize + 9);
4828
4829 /* consume incoming HTX block, including EOM */
4830 total += fsize;
4831 if (fsize == bsize) {
4832 htx_remove_blk(htx, blk);
4833 if (fsize)
4834 goto new_frame;
4835 } else {
4836 /* we've truncated this block */
4837 htx_cut_data_blk(htx, blk, fsize);
4838 }
4839
4840 if (es_now) {
4841 if (h2s->st == H2_SS_OPEN)
4842 h2s->st = H2_SS_HLOC;
4843 else
4844 h2s_close(h2s);
4845
4846 h2s->flags |= H2_SF_ES_SENT;
4847 }
4848
4849 end:
4850 return total;
4851}
4852
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004853/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4854 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4855 * processed. The caller must check the stream's status to detect any error
4856 * which might have happened subsequently to a successful send. The htx blocks
4857 * are automatically removed from the message. The htx message is assumed to be
4858 * valid since produced from the internal code. Processing stops when meeting
4859 * the EOM, which is also removed. All trailers are processed at once and sent
4860 * as a single frame. The ES flag is always set.
4861 */
4862static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4863{
4864 struct http_hdr list[MAX_HTTP_HDR];
4865 struct h2c *h2c = h2s->h2c;
4866 struct htx_blk *blk;
4867 struct htx_blk *blk_end;
4868 struct buffer outbuf;
4869 struct h1m h1m;
4870 enum htx_blk_type type;
4871 uint32_t size;
4872 int ret = 0;
4873 int hdr;
4874 int idx;
4875 void *start;
4876
4877 if (h2c_mux_busy(h2c, h2s)) {
4878 h2s->flags |= H2_SF_BLK_MBUSY;
4879 goto end;
4880 }
4881
4882 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4883 h2c->flags |= H2_CF_MUX_MALLOC;
4884 h2s->flags |= H2_SF_BLK_MROOM;
4885 goto end;
4886 }
4887
4888 /* The principle is that we parse each and every trailers block using
4889 * the H1 headers parser, and append it to the list. We don't proceed
4890 * until EOM is met. blk_end will point to the EOM block.
4891 */
4892 hdr = 0;
4893 memset(list, 0, sizeof(list));
4894 blk_end = NULL;
4895
4896 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4897 blk = htx_get_blk(htx, idx);
4898 type = htx_get_blk_type(blk);
4899
4900 if (type == HTX_BLK_UNUSED)
4901 continue;
4902
4903 if (type != HTX_BLK_TLR) {
4904 if (type == HTX_BLK_EOM)
4905 blk_end = blk;
4906 break;
4907 }
4908
4909 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4910 goto fail;
4911
4912 size = htx_get_blksz(blk);
4913 start = htx_get_blk_ptr(htx, blk);
4914
4915 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4916 h1m.err_pos = 0;
4917 ret = h1_headers_to_hdr_list(start, start + size,
4918 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4919 &h1m, NULL);
4920 if (ret < 0)
4921 goto fail;
4922
4923 /* ret == 0 if an incomplete trailers block was found (missing
4924 * empty line), or > 0 if it was found. We have to continue on
4925 * incomplete messages because the trailers block might be
4926 * incomplete.
4927 */
4928
4929 /* search the new end */
4930 while (hdr <= sizeof(list)/sizeof(list[0])) {
4931 if (!list[hdr].n.len)
4932 break;
4933 hdr++;
4934 }
4935 }
4936
4937 if (!blk_end)
4938 goto end; // end not found yet
4939
4940 if (!hdr)
4941 goto done;
4942
4943 chunk_reset(&outbuf);
4944
4945 while (1) {
4946 outbuf.area = b_tail(&h2c->mbuf);
4947 outbuf.size = b_contig_space(&h2c->mbuf);
4948 outbuf.data = 0;
4949
4950 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4951 break;
4952 realign_again:
4953 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4954 }
4955
4956 if (outbuf.size < 9)
4957 goto full;
4958
4959 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4960 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4961 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4962 outbuf.data = 9;
4963
4964 /* encode status, which necessarily is the first one */
4965 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
4966 if (b_space_wraps(&h2c->mbuf))
4967 goto realign_again;
4968 goto full;
4969 }
4970
4971 /* encode all headers */
4972 for (idx = 0; idx < hdr; idx++) {
4973 /* these ones do not exist in H2 or must not appear in
4974 * trailers and must be dropped.
4975 */
4976 if (isteq(list[idx].n, ist("host")) ||
4977 isteq(list[idx].n, ist("content-length")) ||
4978 isteq(list[idx].n, ist("connection")) ||
4979 isteq(list[idx].n, ist("proxy-connection")) ||
4980 isteq(list[idx].n, ist("keep-alive")) ||
4981 isteq(list[idx].n, ist("upgrade")) ||
4982 isteq(list[idx].n, ist("te")) ||
4983 isteq(list[idx].n, ist("transfer-encoding")))
4984 continue;
4985
4986 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4987 /* output full */
4988 if (b_space_wraps(&h2c->mbuf))
4989 goto realign_again;
4990 goto full;
4991 }
4992 }
4993
4994 /* update the frame's size */
4995 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4996
4997 /* commit the H2 response */
4998 b_add(&h2c->mbuf, outbuf.data);
4999 h2s->flags |= H2_SF_ES_SENT;
5000
5001 if (h2s->st == H2_SS_OPEN)
5002 h2s->st = H2_SS_HLOC;
5003 else
5004 h2s_close(h2s);
5005
5006 /* OK we could properly deliver the response */
5007 done:
5008 /* remove all header blocks including EOM and compute the corresponding size. */
5009 ret = 0;
5010 idx = htx_get_head(htx);
5011 blk = htx_get_blk(htx, idx);
5012 while (blk != blk_end) {
5013 ret += htx_get_blksz(blk);
5014 blk = htx_remove_blk(htx, blk);
5015 }
5016 blk = htx_remove_blk(htx, blk);
5017 end:
5018 return ret;
5019 full:
5020 h2c->flags |= H2_CF_MUX_MFULL;
5021 h2s->flags |= H2_SF_BLK_MROOM;
5022 ret = 0;
5023 goto end;
5024 fail:
5025 /* unparsable HTX messages, too large ones to be produced in the local
5026 * list etc go here (unrecoverable errors).
5027 */
5028 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5029 ret = 0;
5030 goto end;
5031}
5032
Olivier Houchard6ff20392018-07-17 18:46:31 +02005033/* Called from the upper layer, to subscribe to events, such as being able to send */
5034static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5035{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005036 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005037 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005038 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005039
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005040 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005041 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005042 if (!(sw->events & SUB_RETRY_RECV)) {
5043 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005044 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005045 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005046 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005047 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005048 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005049 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005050 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005051 if (!(sw->events & SUB_RETRY_SEND)) {
5052 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005053 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005054 h2s->send_wait = sw;
5055 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5056 if (h2s->flags & H2_SF_BLK_MFCTL)
5057 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5058 else
5059 LIST_ADDQ(&h2c->send_list, &h2s->list);
5060 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005061 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005062 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005063 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005064 if (event_type != 0)
5065 return -1;
5066 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005067
5068
5069}
5070
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005071static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5072{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005073 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005074 struct h2s *h2s = cs->ctx;
5075
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005076 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005077 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005078 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005079 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005080 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005081 }
5082 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005083 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005084 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005085 if (h2s->send_wait == sw) {
5086 LIST_DEL(&h2s->list);
5087 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005088 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005089 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005090 }
5091 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005092 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5093 sw = param;
5094 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005095 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005096 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005097 LIST_DEL(&h2s->list);
5098 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005099 }
5100 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005101 return 0;
5102}
5103
5104
Olivier Houchard511efea2018-08-16 15:30:32 +02005105/* Called from the upper layer, to receive data */
5106static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5107{
Olivier Houchard638b7992018-08-16 15:41:52 +02005108 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005109 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005110 struct htx *h2s_htx = NULL;
5111 struct htx *buf_htx = NULL;
5112 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005113 size_t ret = 0;
5114
5115 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005116 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5117 /* in HTX mode we ignore the count argument */
5118 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005119 if (htx_is_empty(h2s_htx)) {
5120 if (cs->flags & CS_FL_REOS)
5121 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005122 if (cs->flags & CS_FL_ERR_PENDING)
5123 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01005124 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005125 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005126
5127 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005128 count = htx_free_data_space(buf_htx);
5129 if (flags & CO_RFL_KEEP_RSV) {
5130 if (count <= global.tune.maxrewrite)
5131 goto end;
5132 count -= global.tune.maxrewrite;
5133 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005134
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005135 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01005136
5137 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005138 htx_to_buf(buf_htx, buf);
5139 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005140 ret = htx_ret.ret;
5141 }
5142 else {
5143 ret = b_xfer(buf, &h2s->rxbuf, count);
5144 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005145
Olivier Houchard638b7992018-08-16 15:41:52 +02005146 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005147 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005148 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005149 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005150 if (cs->flags & CS_FL_REOS)
5151 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005152 if (cs->flags & CS_FL_ERR_PENDING)
5153 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005154 if (b_size(&h2s->rxbuf)) {
5155 b_free(&h2s->rxbuf);
5156 offer_buffers(NULL, tasks_run_queue);
5157 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005158 }
5159
Willy Tarreau082f5592018-11-25 08:03:32 +01005160 if (ret && h2c->dsi == h2s->id) {
5161 /* demux is blocking on this stream's buffer */
5162 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005163 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005164 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005165end:
Olivier Houchard511efea2018-08-16 15:30:32 +02005166 return ret;
5167}
5168
Olivier Houchardd846c262018-10-19 17:24:29 +02005169static void h2_stop_senders(struct h2c *h2c)
5170{
5171 struct h2s *h2s, *h2s_back;
5172
5173 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5174 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5175 if (h2c->msi == h2s_id(h2s))
5176 continue;
5177 LIST_DEL(&h2s->list);
5178 LIST_INIT(&h2s->list);
5179 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005180 h2s->send_wait->events |= SUB_RETRY_SEND;
5181 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005182 LIST_ADD(&h2c->send_list, &h2s->list);
5183 }
5184}
5185
Willy Tarreau62f52692017-10-08 23:01:42 +02005186/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005187static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005188{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005189 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005190 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005191 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005192 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005193 struct htx *htx;
5194 struct htx_blk *blk;
5195 enum htx_blk_type btype;
5196 uint32_t bsize;
5197 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005198
Olivier Houchardd846c262018-10-19 17:24:29 +02005199 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005200 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005201 h2s->send_wait = NULL;
5202 LIST_DEL(&h2s->list);
5203 LIST_INIT(&h2s->list);
5204 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005205 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5206 return 0;
5207
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005208 /* htx will be enough to decide if we're using HTX or legacy */
5209 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5210
Willy Tarreau0bad0432018-06-14 16:54:01 +02005211 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005212 h2s->flags |= H2_SF_OUTGOING_DATA;
5213
Willy Tarreau751f2d02018-10-05 09:35:00 +02005214 if (h2s->id == 0) {
5215 int32_t id = h2c_get_next_sid(h2s->h2c);
5216
5217 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005218 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005219 return 0;
5220 }
5221
5222 eb32_delete(&h2s->by_id);
5223 h2s->by_id.key = h2s->id = id;
5224 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005225 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005226 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5227 }
5228
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005229 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005230 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5231 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005232 idx = htx_get_head(htx);
5233 blk = htx_get_blk(htx, idx);
5234 btype = htx_get_blk_type(blk);
5235 bsize = htx_get_blksz(blk);
5236
5237 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005238 case HTX_BLK_REQ_SL:
5239 /* start-line before headers */
5240 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5241 if (ret > 0) {
5242 total += ret;
5243 count -= ret;
5244 if (ret < bsize)
5245 goto done;
5246 }
5247 break;
5248
Willy Tarreau115e83b2018-12-01 19:17:53 +01005249 case HTX_BLK_RES_SL:
5250 /* start-line before headers */
5251 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5252 if (ret > 0) {
5253 total += ret;
5254 count -= ret;
5255 if (ret < bsize)
5256 goto done;
5257 }
5258 break;
5259
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005260 case HTX_BLK_DATA:
5261 case HTX_BLK_EOD:
5262 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005263 /* all these cause the emission of a DATA frame (possibly empty).
5264 * This EOM necessarily is one before trailers, as the EOM following
5265 * trailers would have been consumed by the trailers parser.
5266 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005267 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005268 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005269 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005270 total += ret;
5271 count -= ret;
5272 if (ret < bsize)
5273 goto done;
5274 }
5275 break;
5276
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005277 case HTX_BLK_TLR:
5278 /* This is the first trailers block, all the subsequent ones AND
5279 * the EOM will be swallowed by the parser.
5280 */
5281 ret = h2s_htx_make_trailers(h2s, htx);
5282 if (ret > 0) {
5283 total += ret;
5284 count -= ret;
5285 if (ret < bsize)
5286 goto done;
5287 }
5288 break;
5289
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005290 default:
5291 htx_remove_blk(htx, blk);
5292 total += bsize;
5293 count -= bsize;
5294 break;
5295 }
5296 }
5297 goto done;
5298 }
5299
5300 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005301 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005302 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005303 if (h2s->h2c->flags & H2_CF_IS_BACK)
5304 ret = -1;
5305 else
5306 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005307 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005308 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005309 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005310 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005311 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005312 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005313 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005314
Willy Tarreau5dd17352018-06-14 13:33:30 +02005315 if (unlikely((int)ret <= 0)) {
5316 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005317 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5318 break;
5319 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005320 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005321 total += count;
5322 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005323 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005324 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005325 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005326 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005327 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005328 break;
5329 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005330
5331 total += ret;
5332 count -= ret;
5333
5334 if (h2s->st >= H2_SS_ERROR)
5335 break;
5336
5337 if (h2s->flags & H2_SF_BLK_ANY)
5338 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005339 }
5340
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005341 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005342 if (h2s->st >= H2_SS_ERROR) {
5343 /* trim any possibly pending data after we close (extra CR-LF,
5344 * unprocessed trailers, abnormal extra data, ...)
5345 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005346 total += count;
5347 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005348 }
5349
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005350 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005351 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005352 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005353 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005354 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005355 }
5356
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005357 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005358 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005359 } else {
5360 b_del(buf, total);
5361 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005362
5363 /* The mux is full, cancel the pending tasks */
5364 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5365 (h2s->flags & H2_SF_BLK_MBUSY))
5366 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005367
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005368 /* If we're running HTX, and we read the whole buffer, then pretend
5369 * we read exactly what the caller specified, as with HTX the caller
5370 * will always give the buffer size, instead of the amount of data
5371 * available.
5372 */
5373 if (htx && !b_data(buf))
5374 total = orig_count;
5375
Olivier Houchard7505f942018-08-21 18:10:44 +02005376 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005377 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005378 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005379
Olivier Houchard7505f942018-08-21 18:10:44 +02005380 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005381 /* If we're waiting for flow control, and we got a shutr on the
5382 * connection, we will never be unlocked, so add an error on
5383 * the conn_stream.
5384 */
5385 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5386 !b_data(&h2s->h2c->dbuf) &&
5387 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5388 if (cs->flags & CS_FL_EOS)
5389 cs->flags |= CS_FL_ERROR;
5390 else
5391 cs->flags |= CS_FL_ERR_PENDING;
5392 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005393 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005394}
5395
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005396/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005397static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005398{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005399 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005400 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005401 struct eb32_node *node;
5402 int fctl_cnt = 0;
5403 int send_cnt = 0;
5404 int tree_cnt = 0;
5405 int orph_cnt = 0;
5406
5407 if (!h2c)
5408 return;
5409
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005410 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005411 fctl_cnt++;
5412
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005413 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005414 send_cnt++;
5415
Willy Tarreau3af37712018-12-18 14:34:41 +01005416 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005417 node = eb32_first(&h2c->streams_by_id);
5418 while (node) {
5419 h2s = container_of(node, struct h2s, by_id);
5420 tree_cnt++;
5421 if (!h2s->cs)
5422 orph_cnt++;
5423 node = eb32_next(node);
5424 }
5425
Willy Tarreau987c0632018-12-18 10:32:05 +01005426 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5427 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5428 " .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 +02005429 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5430 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005431 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005432 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5433 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5434 h2c->msi,
5435 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5436 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5437
5438 if (h2s) {
5439 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5440 h2s, h2s->id, h2s->flags,
5441 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5442 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5443 h2s->cs);
5444 if (h2s->cs)
5445 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5446 h2s->cs->flags, h2s->cs->data);
5447 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005448}
Willy Tarreau62f52692017-10-08 23:01:42 +02005449
5450/*******************************************************/
5451/* functions below are dedicated to the config parsers */
5452/*******************************************************/
5453
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005454/* config parser for global "tune.h2.header-table-size" */
5455static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5456 struct proxy *defpx, const char *file, int line,
5457 char **err)
5458{
5459 if (too_many_args(1, args, err, NULL))
5460 return -1;
5461
5462 h2_settings_header_table_size = atoi(args[1]);
5463 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5464 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5465 return -1;
5466 }
5467 return 0;
5468}
Willy Tarreau62f52692017-10-08 23:01:42 +02005469
Willy Tarreaue6baec02017-07-27 11:45:11 +02005470/* config parser for global "tune.h2.initial-window-size" */
5471static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5472 struct proxy *defpx, const char *file, int line,
5473 char **err)
5474{
5475 if (too_many_args(1, args, err, NULL))
5476 return -1;
5477
5478 h2_settings_initial_window_size = atoi(args[1]);
5479 if (h2_settings_initial_window_size < 0) {
5480 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5481 return -1;
5482 }
5483 return 0;
5484}
5485
Willy Tarreau5242ef82017-07-27 11:47:28 +02005486/* config parser for global "tune.h2.max-concurrent-streams" */
5487static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5488 struct proxy *defpx, const char *file, int line,
5489 char **err)
5490{
5491 if (too_many_args(1, args, err, NULL))
5492 return -1;
5493
5494 h2_settings_max_concurrent_streams = atoi(args[1]);
5495 if (h2_settings_max_concurrent_streams < 0) {
5496 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5497 return -1;
5498 }
5499 return 0;
5500}
5501
Willy Tarreau62f52692017-10-08 23:01:42 +02005502
5503/****************************************/
5504/* MUX initialization and instanciation */
5505/***************************************/
5506
5507/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005508static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005509 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005510 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005511 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005512 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005513 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005514 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005515 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005516 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005517 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005518 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005519 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005520 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005521 .shutr = h2_shutr,
5522 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005523 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005524 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005525 .name = "H2",
5526};
5527
Christopher Faulet32f61c02018-04-10 14:33:41 +02005528/* PROTO selection : this mux registers PROTO token "h2" */
5529static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005530 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005531
Willy Tarreau0108d902018-11-25 19:14:37 +01005532INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5533
Willy Tarreauf8957272018-10-03 10:25:20 +02005534static struct mux_proto_list mux_proto_h2_htx =
5535 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5536
5537INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5538
Willy Tarreau62f52692017-10-08 23:01:42 +02005539/* config keyword parsers */
5540static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005541 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005542 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005543 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005544 { 0, NULL, NULL }
5545}};
5546
Willy Tarreau0108d902018-11-25 19:14:37 +01005547INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);