blob: 5ad32147adc18580f917eacda6aa597a91f2d583 [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 Tarreau2a856182017-05-16 15:20:39 +020032/* dummy streams returned for idle and closed states */
33static const struct h2s *h2_closed_stream;
34static const struct h2s *h2_idle_stream;
35
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#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 +020063#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010064#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020065
Willy Tarreau5ab6b572017-09-22 08:05:00 +020066/* H2 connection state, in h2c->st0 */
67enum h2_cs {
68 H2_CS_PREFACE, // init done, waiting for connection preface
69 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
70 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
71 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010072 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
73 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020074 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
75 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
76 H2_CS_ENTRIES // must be last
77} __attribute__((packed));
78
79/* H2 connection descriptor */
80struct h2c {
81 struct connection *conn;
82
83 enum h2_cs st0; /* mux state */
84 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
85
86 /* 16 bit hole here */
87 uint32_t flags; /* connection flags: H2_CF_* */
88 int32_t max_id; /* highest ID known on this connection, <0 before preface */
89 uint32_t rcvd_c; /* newly received data to ACK for the connection */
90 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
91
92 /* states for the demux direction */
93 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020094 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095
96 int32_t dsi; /* demux stream ID (<0 = idle) */
97 int32_t dfl; /* demux frame length (if dsi >= 0) */
98 int8_t dft; /* demux frame type (if dsi >= 0) */
99 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100100 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
101 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200102 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
103
104 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200105 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200106 int32_t msi; /* mux stream ID (<0 = idle) */
107 int32_t mfl; /* mux frame length (if dsi >= 0) */
108 int8_t mft; /* mux frame type (if dsi >= 0) */
109 int8_t mff; /* mux frame flags (if dsi >= 0) */
110 /* 16 bit hole here */
111 int32_t miw; /* mux initial window size for all new streams */
112 int32_t mws; /* mux window size. Can be negative. */
113 int32_t mfs; /* mux's max frame size */
114
Willy Tarreauea392822017-10-31 10:02:25 +0100115 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100116 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100117 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200118 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200119 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100120 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200121 struct eb_root streams_by_id; /* all active streams by their ID */
122 struct list send_list; /* list of blocked streams requesting to send */
123 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200124 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100125 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200126 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200127};
128
Willy Tarreau18312642017-10-11 07:57:07 +0200129/* H2 stream state, in h2s->st */
130enum h2_ss {
131 H2_SS_IDLE = 0, // idle
132 H2_SS_RLOC, // reserved(local)
133 H2_SS_RREM, // reserved(remote)
134 H2_SS_OPEN, // open
135 H2_SS_HREM, // half-closed(remote)
136 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200137 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200138 H2_SS_CLOSED, // closed
139 H2_SS_ENTRIES // must be last
140} __attribute__((packed));
141
142/* HTTP/2 stream flags (32 bit), in h2s->flags */
143#define H2_SF_NONE 0x00000000
144#define H2_SF_ES_RCVD 0x00000001
145#define H2_SF_ES_SENT 0x00000002
146
147#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
148#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
149
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200150/* stream flags indicating the reason the stream is blocked */
151#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
152#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
153#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
154#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
155#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
156
Willy Tarreau454f9052017-10-26 19:40:35 +0200157/* stream flags indicating how data is supposed to be sent */
158#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
159#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
160
161/* step we're currently in when sending chunks. This is needed because we may
162 * have to transfer chunks as large as a full buffer so there's no room left
163 * for size nor crlf around.
164 */
165#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
166#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
167#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
168
169#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
170
Willy Tarreau67434202017-11-06 20:20:51 +0100171#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100172#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100173
Willy Tarreau18312642017-10-11 07:57:07 +0200174/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
175 * it is being processed in the internal HTTP representation (H1 for now).
176 */
177struct h2s {
178 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100179 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200180 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200181 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200182 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200183 int32_t id; /* stream ID */
184 uint32_t flags; /* H2_SF_* */
185 int mws; /* mux window size for this stream */
186 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
187 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200188 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200189 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200190 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
191 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
192 struct wait_event *send_wait; /* The streeam is waiting for flow control */
193 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200194};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200195
Willy Tarreauc6405142017-09-21 20:23:50 +0200196/* descriptor for an h2 frame header */
197struct h2_fh {
198 uint32_t len; /* length, host order, 24 bits */
199 uint32_t sid; /* stream id, host order, 31 bits */
200 uint8_t ft; /* frame type */
201 uint8_t ff; /* frame flags */
202};
203
Willy Tarreau8ceae722018-11-26 11:58:30 +0100204/* the h2c connection pool */
205DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
206
207/* the h2s stream pool */
208DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
209
Willy Tarreaudc572362018-12-12 08:08:05 +0100210/* The default connection window size is 65535, it may only be enlarged using
211 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
212 * we'll pretend we already received the difference between the two to send
213 * an equivalent window update to enlarge it to 2G-1.
214 */
215#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
216
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200217/* a few settings from the global section */
218static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200219static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200220static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200221
Willy Tarreau2a856182017-05-16 15:20:39 +0200222/* a dmumy closed stream */
223static const struct h2s *h2_closed_stream = &(const struct h2s){
224 .cs = NULL,
225 .h2c = NULL,
226 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100227 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100228 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200229 .id = 0,
230};
231
232/* and a dummy idle stream for use with any unannounced stream */
233static const struct h2s *h2_idle_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200238 .id = 0,
239};
240
Olivier Houchard9f6af332018-05-25 14:04:04 +0200241static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200242static int h2_send(struct h2c *h2c);
243static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200244static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200245static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100246static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200247static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100248static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200249static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100250static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100251static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200252
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200253/*****************************************************/
254/* functions below are for dynamic buffer management */
255/*****************************************************/
256
Willy Tarreau315d8072017-12-10 22:17:57 +0100257/* indicates whether or not the we may call the h2_recv() function to attempt
258 * to receive data into the buffer and/or demux pending data. The condition is
259 * a bit complex due to some API limits for now. The rules are the following :
260 * - if an error or a shutdown was detected on the connection and the buffer
261 * is empty, we must not attempt to receive
262 * - if the demux buf failed to be allocated, we must not try to receive and
263 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100264 * - if no flag indicates a blocking condition, we may attempt to receive,
265 * regardless of whether the demux buffer is full or not, so that only
266 * de demux part decides whether or not to block. This is needed because
267 * the connection API indeed prevents us from re-enabling receipt that is
268 * already enabled in a polled state, so we must always immediately stop
269 * as soon as the demux can't proceed so as never to hit an end of read
270 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100271 * - otherwise must may not attempt
272 */
273static inline int h2_recv_allowed(const struct h2c *h2c)
274{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200275 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100276 (h2c->st0 >= H2_CS_ERROR ||
277 h2c->conn->flags & CO_FL_ERROR ||
278 conn_xprt_read0_pending(h2c->conn)))
279 return 0;
280
281 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100282 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100283 return 1;
284
285 return 0;
286}
287
Willy Tarreau47b515a2018-12-21 16:09:41 +0100288/* restarts reading on the connection if it was not enabled */
289static inline void h2c_restart_reading(const struct h2c *h2c)
290{
291 if (!h2_recv_allowed(h2c))
292 return;
293 if (h2c->wait_event.events & SUB_RETRY_RECV)
294 return;
295 tasklet_wakeup(h2c->wait_event.task);
296}
297
298
Willy Tarreauf2101912018-07-19 10:11:38 +0200299/* returns true if the connection has too many conn_streams attached */
300static inline int h2_has_too_many_cs(const struct h2c *h2c)
301{
302 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
303}
304
Willy Tarreau44e973f2018-03-01 17:49:30 +0100305/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
306 * flags are used to figure what buffer was requested. It returns 1 if the
307 * allocation succeeds, in which case the connection is woken up, or 0 if it's
308 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200309 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100310static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200311{
312 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200314
Willy Tarreau44e973f2018-03-01 17:49:30 +0100315 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200316 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100317 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200318 return 1;
319 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200320
Willy Tarreau44e973f2018-03-01 17:49:30 +0100321 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
322 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200323
324 if (h2c->flags & H2_CF_DEM_MROOM) {
325 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100326 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200327 }
Willy Tarreau14398122017-09-22 14:26:04 +0200328 return 1;
329 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100330
331 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
332 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200333 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100334 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100335 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100336 return 1;
337 }
338
Willy Tarreau14398122017-09-22 14:26:04 +0200339 return 0;
340}
341
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200342static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200343{
344 struct buffer *buf = NULL;
345
Willy Tarreau44e973f2018-03-01 17:49:30 +0100346 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
347 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
348 h2c->buf_wait.target = h2c;
349 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100350 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100351 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100352 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200353 __conn_xprt_stop_recv(h2c->conn);
354 }
355 return buf;
356}
357
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200358static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200359{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200360 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100361 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200362 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200363 }
364}
365
Olivier Houchardd540b362018-11-05 18:37:53 +0100366static int h2_avail_streams(struct connection *conn)
367{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100368 struct h2c *h2c = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100369
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100370 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100371 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
372}
373
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100374static int h2_max_streams(struct connection *conn)
375{
376 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
377 return h2_settings_max_concurrent_streams;
378}
379
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200380
Willy Tarreau62f52692017-10-08 23:01:42 +0200381/*****************************************************************/
382/* functions below are dedicated to the mux setup and management */
383/*****************************************************************/
384
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200385/* Initialize the mux once it's attached. For outgoing connections, the context
386 * is already initialized before installing the mux, so we detect incoming
387 * connections from the fact that the context is still NULL. Returns < 0 on
388 * error.
389 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100390static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200391{
392 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100393 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200394
Willy Tarreaubafbe012017-11-24 17:34:44 +0100395 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200396 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200397 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200398
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100399 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200400 h2c->flags = H2_CF_IS_BACK;
401 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
402 if (tick_isset(prx->timeout.serverfin))
403 h2c->shut_timeout = prx->timeout.serverfin;
404 } else {
405 h2c->flags = H2_CF_NONE;
406 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
407 if (tick_isset(prx->timeout.clientfin))
408 h2c->shut_timeout = prx->timeout.clientfin;
409 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100410
Willy Tarreau0b37d652018-10-03 10:33:02 +0200411 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100412 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100413 if (tick_isset(h2c->timeout)) {
414 t = task_new(tid_bit);
415 if (!t)
416 goto fail;
417
418 h2c->task = t;
419 t->process = h2_timeout_task;
420 t->context = h2c;
421 t->expire = tick_add(now_ms, h2c->timeout);
422 }
Willy Tarreauea392822017-10-31 10:02:25 +0100423
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200424 h2c->wait_event.task = tasklet_new();
425 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200426 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200427 h2c->wait_event.task->process = h2_io_cb;
428 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100429 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200430
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
432 if (!h2c->ddht)
433 goto fail;
434
435 /* Initialise the context. */
436 h2c->st0 = H2_CS_PREFACE;
437 h2c->conn = conn;
438 h2c->max_id = -1;
439 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100440 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200441 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100442 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200443 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200444
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200445 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200446 h2c->dsi = -1;
447 h2c->msi = -1;
448 h2c->last_sid = -1;
449
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200450 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200451 h2c->miw = 65535; /* mux initial window size */
452 h2c->mws = 65535; /* mux window size */
453 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200454 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455 LIST_INIT(&h2c->send_list);
456 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200457 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100458 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200459
Willy Tarreau3f133572017-10-31 19:21:06 +0100460 if (t)
461 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100462
Willy Tarreau01b44822018-10-03 14:26:37 +0200463 if (h2c->flags & H2_CF_IS_BACK) {
464 /* FIXME: this is temporary, for outgoing connections we need
465 * to immediately allocate a stream until the code is modified
466 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100467 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200468 */
469 struct h2s *h2s;
470
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100471 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200472 if (!h2s)
473 goto fail_stream;
474 }
475
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100476 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200477
Willy Tarreau0f383582018-10-03 14:22:21 +0200478 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100479 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200480 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200481 fail_stream:
482 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200483 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100484 if (t)
485 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200486 if (h2c->wait_event.task)
487 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100488 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200489 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200490 return -1;
491}
492
Willy Tarreau751f2d02018-10-05 09:35:00 +0200493/* returns the next allocatable outgoing stream ID for the H2 connection, or
494 * -1 if no more is allocatable.
495 */
496static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
497{
498 int32_t id = (h2c->max_id + 1) | 1;
499 if (id & 0x80000000U)
500 id = -1;
501 return id;
502}
503
Willy Tarreau2373acc2017-10-12 17:35:14 +0200504/* returns the stream associated with id <id> or NULL if not found */
505static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
506{
507 struct eb32_node *node;
508
Willy Tarreau751f2d02018-10-05 09:35:00 +0200509 if (id == 0)
510 return (struct h2s *)h2_closed_stream;
511
Willy Tarreau2a856182017-05-16 15:20:39 +0200512 if (id > h2c->max_id)
513 return (struct h2s *)h2_idle_stream;
514
Willy Tarreau2373acc2017-10-12 17:35:14 +0200515 node = eb32_lookup(&h2c->streams_by_id, id);
516 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200517 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200518
519 return container_of(node, struct h2s, by_id);
520}
521
Willy Tarreau62f52692017-10-08 23:01:42 +0200522/* release function for a connection. This one should be called to free all
523 * resources allocated to the mux.
524 */
525static void h2_release(struct connection *conn)
526{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100527 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200528
529 LIST_DEL(&conn->list);
530
531 if (h2c) {
532 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200533
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100534 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100535 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100536 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200537
Willy Tarreau44e973f2018-03-01 17:49:30 +0100538 h2_release_buf(h2c, &h2c->dbuf);
539 h2_release_buf(h2c, &h2c->mbuf);
540
Willy Tarreauea392822017-10-31 10:02:25 +0100541 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200542 h2c->task->context = NULL;
543 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100544 h2c->task = NULL;
545 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200546 if (h2c->wait_event.task)
547 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100548 if (h2c->wait_event.events != 0)
549 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200550 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100551
Willy Tarreaubafbe012017-11-24 17:34:44 +0100552 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553 }
554
555 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100556 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200557
558 conn_stop_tracking(conn);
559 conn_full_close(conn);
560 if (conn->destroy_cb)
561 conn->destroy_cb(conn);
562 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200563}
564
565
Willy Tarreau71681172017-10-23 14:39:06 +0200566/******************************************************/
567/* functions below are for the H2 protocol processing */
568/******************************************************/
569
570/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100571static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200572{
573 return h2s ? h2s->id : 0;
574}
575
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200576/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100577static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200578{
579 if (h2c->msi < 0)
580 return 0;
581
582 if (h2c->msi == h2s_id(h2s))
583 return 0;
584
585 return 1;
586}
587
Willy Tarreau741d6df2017-10-17 08:00:59 +0200588/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100589static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200590{
591 h2c->errcode = err;
592 h2c->st0 = H2_CS_ERROR;
593}
594
Willy Tarreau2e43f082017-10-17 08:03:59 +0200595/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100596static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200597{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200598 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200599 h2s->errcode = err;
600 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100601 if (h2s->cs)
602 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200603 }
604}
605
Willy Tarreau7e094452018-12-19 18:08:52 +0100606/* attempt to notify the data layer of recv availability */
607static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
608{
609 struct wait_event *sw;
610
611 if (h2s->recv_wait) {
612 sw = h2s->recv_wait;
613 sw->events &= ~SUB_RETRY_RECV;
614 tasklet_wakeup(sw->task);
615 h2s->recv_wait = NULL;
616 }
617}
618
619/* attempt to notify the data layer of send availability */
620static void __maybe_unused h2s_notify_send(struct h2s *h2s)
621{
622 struct wait_event *sw;
623
624 if (h2s->send_wait) {
625 sw = h2s->send_wait;
626 sw->events &= ~SUB_RETRY_SEND;
627 tasklet_wakeup(sw->task);
628 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100629 LIST_DEL(&h2s->list);
630 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100631 }
632}
633
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100634/* alerts the data layer, trying to wake it up by all means, following
635 * this sequence :
636 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
637 * - if its subscribed to send, then it's woken up for send
638 * - if it was subscribed to neither, its ->wake() callback is called
639 * It is safe to call this function with a closed stream which doesn't have a
640 * conn_stream anymore.
641 */
642static void __maybe_unused h2s_alert(struct h2s *h2s)
643{
644 if (h2s->recv_wait || h2s->send_wait) {
645 h2s_notify_recv(h2s);
646 h2s_notify_send(h2s);
647 }
648 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
649 h2s->cs->data_cb->wake(h2s->cs);
650}
651
Willy Tarreaue4820742017-07-27 13:37:23 +0200652/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100653static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200654{
655 uint8_t *out = frame;
656
657 *out = len >> 16;
658 write_n16(out + 1, len);
659}
660
Willy Tarreau54c15062017-10-10 17:10:03 +0200661/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
662 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
663 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200664 * available in the buffer's input prior to calling this function. The buffer
665 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200666 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100667static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200668 const struct buffer *b, int o)
669{
Willy Tarreau591d4452018-06-15 17:21:00 +0200670 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 +0200671}
672
Willy Tarreau1f094672017-11-20 21:27:45 +0100673static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200674{
Willy Tarreau591d4452018-06-15 17:21:00 +0200675 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200676}
677
Willy Tarreau1f094672017-11-20 21:27:45 +0100678static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200679{
Willy Tarreau591d4452018-06-15 17:21:00 +0200680 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200681}
682
Willy Tarreau1f094672017-11-20 21:27:45 +0100683static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200684{
Willy Tarreau591d4452018-06-15 17:21:00 +0200685 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200686}
687
688
Willy Tarreau715d5312017-07-11 15:20:24 +0200689/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
690 * is not obvious. It turns out that H2 headers are neither aligned nor do they
691 * use regular sizes. And to add to the trouble, the buffer may wrap so each
692 * byte read must be checked. The header is formed like this :
693 *
694 * b0 b1 b2 b3 b4 b5..b8
695 * +----------+---------+--------+----+----+----------------------+
696 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
697 * +----------+---------+--------+----+----+----------------------+
698 *
699 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
700 * we get the sid properly aligned and ordered, and 16 bits of len properly
701 * ordered as well. The type and flags can be extracted using bit shifts from
702 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200703 * Returns zero if some bytes are missing, otherwise non-zero on success. The
704 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200705 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100706static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200707{
708 uint64_t w;
709
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200710 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200711 return 0;
712
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200713 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200714 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200715 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
716 h->ff = w >> 32;
717 h->ft = w >> 40;
718 h->len += w >> 48;
719 return 1;
720}
721
722/* skip the next 9 bytes corresponding to the frame header possibly parsed by
723 * h2_peek_frame_hdr() above.
724 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100725static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200726{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200727 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200728}
729
730/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100731static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200732{
733 int ret;
734
735 ret = h2_peek_frame_hdr(b, h);
736 if (ret > 0)
737 h2_skip_frame_hdr(b);
738 return ret;
739}
740
Willy Tarreau00dd0782018-03-01 16:31:34 +0100741/* marks stream <h2s> as CLOSED and decrement the number of active streams for
742 * its connection if the stream was not yet closed. Please use this exclusively
743 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100744 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100745static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100746{
747 if (h2s->st != H2_SS_CLOSED)
748 h2s->h2c->nb_streams--;
749 h2s->st = H2_SS_CLOSED;
750}
751
Willy Tarreau71049cc2018-03-28 13:56:39 +0200752/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
753static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100754{
755 h2s_close(h2s);
756 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200757 if (b_size(&h2s->rxbuf)) {
758 b_free(&h2s->rxbuf);
759 offer_buffers(NULL, tasks_run_queue);
760 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200761 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100762 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200763 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100764 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800765 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200766 * reference left would be in the h2c send_list/fctl_list, and if
767 * we're in it, we're getting out anyway
768 */
769 LIST_DEL(&h2s->list);
770 LIST_INIT(&h2s->list);
771 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100772 pool_free(pool_head_h2s, h2s);
773}
774
Willy Tarreaua8e49542018-10-03 18:53:55 +0200775/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
776 * stream tree. In case of error, nothing is added and NULL is returned. The
777 * causes of errors can be any failed memory allocation. The caller is
778 * responsible for checking if the connection may support an extra stream
779 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200780 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200781static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200782{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200783 struct h2s *h2s;
784
Willy Tarreaubafbe012017-11-24 17:34:44 +0100785 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200786 if (!h2s)
787 goto out;
788
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200789 h2s->wait_event.task = tasklet_new();
790 if (!h2s->wait_event.task) {
791 pool_free(pool_head_h2s, h2s);
792 goto out;
793 }
794 h2s->send_wait = NULL;
795 h2s->recv_wait = NULL;
796 h2s->wait_event.task->process = h2_deferred_shut;
797 h2s->wait_event.task->context = h2s;
798 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100799 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200800 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200801 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200802 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200803 h2s->mws = h2c->miw;
804 h2s->flags = H2_SF_NONE;
805 h2s->errcode = H2_ERR_NO_ERROR;
806 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200807 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200808 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200809
810 if (h2c->flags & H2_CF_IS_BACK) {
811 h1m_init_req(&h2s->h1m);
812 h2s->h1m.err_pos = -1; // don't care about errors on the request path
813 h2s->h1m.flags |= H1_MF_TOLOWER;
814 } else {
815 h1m_init_res(&h2s->h1m);
816 h2s->h1m.err_pos = -1; // don't care about errors on the response path
817 h2s->h1m.flags |= H1_MF_TOLOWER;
818 }
819
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200820 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200821 if (id > 0)
822 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200823
824 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100825 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200826
827 return h2s;
828
829 out_free_h2s:
830 pool_free(pool_head_h2s, h2s);
831 out:
832 return NULL;
833}
834
835/* creates a new stream <id> on the h2c connection and returns it, or NULL in
836 * case of memory allocation error.
837 */
838static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
839{
840 struct session *sess = h2c->conn->owner;
841 struct conn_stream *cs;
842 struct h2s *h2s;
843
844 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
845 goto out;
846
847 h2s = h2s_new(h2c, id);
848 if (!h2s)
849 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200850
851 cs = cs_new(h2c->conn);
852 if (!cs)
853 goto out_close;
854
Olivier Houchard746fb772018-12-15 19:42:00 +0100855 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200856 h2s->cs = cs;
857 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200858 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200859
860 if (stream_create_from_cs(cs) < 0)
861 goto out_free_cs;
862
Willy Tarreau590a0512018-09-05 11:56:48 +0200863 /* We want the accept date presented to the next stream to be the one
864 * we have now, the handshake time to be null (since the next stream
865 * is not delayed by a handshake), and the idle time to count since
866 * right now.
867 */
868 sess->accept_date = date;
869 sess->tv_accept = now;
870 sess->t_handshake = 0;
871
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200872 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200873 if (h2_has_too_many_cs(h2c))
874 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200875 return h2s;
876
877 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200878 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200879 cs_free(cs);
880 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200881 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200882 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200883 sess_log(sess);
884 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200885}
886
Willy Tarreau751f2d02018-10-05 09:35:00 +0200887/* allocates a new stream associated to conn_stream <cs> on the h2c connection
888 * and returns it, or NULL in case of memory allocation error or if the highest
889 * possible stream ID was reached.
890 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100891static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200892{
893 struct h2s *h2s = NULL;
894
895 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
896 goto out;
897
898 /* Defer choosing the ID until we send the first message to create the stream */
899 h2s = h2s_new(h2c, 0);
900 if (!h2s)
901 goto out;
902
903 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100904 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200905 cs->ctx = h2s;
906 h2c->nb_cs++;
907
Willy Tarreau751f2d02018-10-05 09:35:00 +0200908 out:
909 return h2s;
910}
911
Willy Tarreaube5b7152017-09-25 16:25:39 +0200912/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
913 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
914 * the various settings codes.
915 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200916static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200917{
918 struct buffer *res;
919 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200920 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200921 int ret;
922
923 if (h2c_mux_busy(h2c, NULL)) {
924 h2c->flags |= H2_CF_DEM_MBUSY;
925 return 0;
926 }
927
Willy Tarreau44e973f2018-03-01 17:49:30 +0100928 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200929 if (!res) {
930 h2c->flags |= H2_CF_MUX_MALLOC;
931 h2c->flags |= H2_CF_DEM_MROOM;
932 return 0;
933 }
934
935 chunk_init(&buf, buf_data, sizeof(buf_data));
936 chunk_memcpy(&buf,
937 "\x00\x00\x00" /* length : 0 for now */
938 "\x04\x00" /* type : 4 (settings), flags : 0 */
939 "\x00\x00\x00\x00", /* stream ID : 0 */
940 9);
941
942 if (h2_settings_header_table_size != 4096) {
943 char str[6] = "\x00\x01"; /* header_table_size */
944
945 write_n32(str + 2, h2_settings_header_table_size);
946 chunk_memcat(&buf, str, 6);
947 }
948
949 if (h2_settings_initial_window_size != 65535) {
950 char str[6] = "\x00\x04"; /* initial_window_size */
951
952 write_n32(str + 2, h2_settings_initial_window_size);
953 chunk_memcat(&buf, str, 6);
954 }
955
956 if (h2_settings_max_concurrent_streams != 0) {
957 char str[6] = "\x00\x03"; /* max_concurrent_streams */
958
959 /* Note: 0 means "unlimited" for haproxy's config but not for
960 * the protocol, so never send this value!
961 */
962 write_n32(str + 2, h2_settings_max_concurrent_streams);
963 chunk_memcat(&buf, str, 6);
964 }
965
966 if (global.tune.bufsize != 16384) {
967 char str[6] = "\x00\x05"; /* max_frame_size */
968
969 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
970 * match bufsize - rewrite size, but at the moment it seems
971 * that clients don't take care of it.
972 */
973 write_n32(str + 2, global.tune.bufsize);
974 chunk_memcat(&buf, str, 6);
975 }
976
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200977 h2_set_frame_size(buf.area, buf.data - 9);
978 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200979 if (unlikely(ret <= 0)) {
980 if (!ret) {
981 h2c->flags |= H2_CF_MUX_MFULL;
982 h2c->flags |= H2_CF_DEM_MROOM;
983 return 0;
984 }
985 else {
986 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
987 return 0;
988 }
989 }
990 return ret;
991}
992
Willy Tarreau52eed752017-09-22 15:05:09 +0200993/* Try to receive a connection preface, then upon success try to send our
994 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
995 * missing data. It may return an error in h2c.
996 */
997static int h2c_frt_recv_preface(struct h2c *h2c)
998{
999 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001000 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001001
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001002 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001003
1004 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001005 if (ret1 < 0)
1006 sess_log(h2c->conn->owner);
1007
Willy Tarreau52eed752017-09-22 15:05:09 +02001008 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1009 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1010 return 0;
1011 }
1012
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001013 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001014 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001015 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001016
Willy Tarreaube5b7152017-09-25 16:25:39 +02001017 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001018}
1019
Willy Tarreau01b44822018-10-03 14:26:37 +02001020/* Try to send a connection preface, then upon success try to send our
1021 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1022 * missing data. It may return an error in h2c.
1023 */
1024static int h2c_bck_send_preface(struct h2c *h2c)
1025{
1026 struct buffer *res;
1027
1028 if (h2c_mux_busy(h2c, NULL)) {
1029 h2c->flags |= H2_CF_DEM_MBUSY;
1030 return 0;
1031 }
1032
1033 res = h2_get_buf(h2c, &h2c->mbuf);
1034 if (!res) {
1035 h2c->flags |= H2_CF_MUX_MALLOC;
1036 h2c->flags |= H2_CF_DEM_MROOM;
1037 return 0;
1038 }
1039
1040 if (!b_data(res)) {
1041 /* preface not yet sent */
1042 b_istput(res, ist(H2_CONN_PREFACE));
1043 }
1044
1045 return h2c_send_settings(h2c);
1046}
1047
Willy Tarreau081d4722017-05-16 21:51:05 +02001048/* try to send a GOAWAY frame on the connection to report an error or a graceful
1049 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1050 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1051 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1052 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1053 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1054 * on unrecoverable failure. It will not attempt to send one again in this last
1055 * case so that it is safe to use h2c_error() to report such errors.
1056 */
1057static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1058{
1059 struct buffer *res;
1060 char str[17];
1061 int ret;
1062
1063 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1064 return 1; // claim that it worked
1065
1066 if (h2c_mux_busy(h2c, h2s)) {
1067 if (h2s)
1068 h2s->flags |= H2_SF_BLK_MBUSY;
1069 else
1070 h2c->flags |= H2_CF_DEM_MBUSY;
1071 return 0;
1072 }
1073
Willy Tarreau44e973f2018-03-01 17:49:30 +01001074 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001075 if (!res) {
1076 h2c->flags |= H2_CF_MUX_MALLOC;
1077 if (h2s)
1078 h2s->flags |= H2_SF_BLK_MROOM;
1079 else
1080 h2c->flags |= H2_CF_DEM_MROOM;
1081 return 0;
1082 }
1083
1084 /* len: 8, type: 7, flags: none, sid: 0 */
1085 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1086
1087 if (h2c->last_sid < 0)
1088 h2c->last_sid = h2c->max_id;
1089
1090 write_n32(str + 9, h2c->last_sid);
1091 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001092 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001093 if (unlikely(ret <= 0)) {
1094 if (!ret) {
1095 h2c->flags |= H2_CF_MUX_MFULL;
1096 if (h2s)
1097 h2s->flags |= H2_SF_BLK_MROOM;
1098 else
1099 h2c->flags |= H2_CF_DEM_MROOM;
1100 return 0;
1101 }
1102 else {
1103 /* we cannot report this error using GOAWAY, so we mark
1104 * it and claim a success.
1105 */
1106 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1107 h2c->flags |= H2_CF_GOAWAY_FAILED;
1108 return 1;
1109 }
1110 }
1111 h2c->flags |= H2_CF_GOAWAY_SENT;
1112 return ret;
1113}
1114
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001115/* Try to send an RST_STREAM frame on the connection for the indicated stream
1116 * during mux operations. This stream must be valid and cannot be closed
1117 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1118 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1119 * not yet.
1120 *
1121 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1122 * to write the message, it subscribes the stream to future notifications.
1123 */
1124static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1125{
1126 struct buffer *res;
1127 char str[13];
1128 int ret;
1129
1130 if (!h2s || h2s->st == H2_SS_CLOSED)
1131 return 1;
1132
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001133 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1134 * RST_STREAM in response to a RST_STREAM frame.
1135 */
1136 if (h2c->dft == H2_FT_RST_STREAM) {
1137 ret = 1;
1138 goto ignore;
1139 }
1140
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001141 if (h2c_mux_busy(h2c, h2s)) {
1142 h2s->flags |= H2_SF_BLK_MBUSY;
1143 return 0;
1144 }
1145
Willy Tarreau44e973f2018-03-01 17:49:30 +01001146 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001147 if (!res) {
1148 h2c->flags |= H2_CF_MUX_MALLOC;
1149 h2s->flags |= H2_SF_BLK_MROOM;
1150 return 0;
1151 }
1152
1153 /* len: 4, type: 3, flags: none */
1154 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1155 write_n32(str + 5, h2s->id);
1156 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001157 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001158
1159 if (unlikely(ret <= 0)) {
1160 if (!ret) {
1161 h2c->flags |= H2_CF_MUX_MFULL;
1162 h2s->flags |= H2_SF_BLK_MROOM;
1163 return 0;
1164 }
1165 else {
1166 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1167 return 0;
1168 }
1169 }
1170
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001171 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001172 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001173 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001174 return ret;
1175}
1176
1177/* Try to send an RST_STREAM frame on the connection for the stream being
1178 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1179 * error code unless the stream's state already is IDLE or CLOSED in which
1180 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1181 * it was not yet.
1182 *
1183 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1184 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001185 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001186 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001187 */
1188static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1189{
1190 struct buffer *res;
1191 char str[13];
1192 int ret;
1193
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001194 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1195 * RST_STREAM in response to a RST_STREAM frame.
1196 */
1197 if (h2c->dft == H2_FT_RST_STREAM) {
1198 ret = 1;
1199 goto ignore;
1200 }
1201
Willy Tarreau27a84c92017-10-17 08:10:17 +02001202 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001203 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001204 return 0;
1205 }
1206
Willy Tarreau44e973f2018-03-01 17:49:30 +01001207 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001208 if (!res) {
1209 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001210 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001211 return 0;
1212 }
1213
1214 /* len: 4, type: 3, flags: none */
1215 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001216
Willy Tarreau27a84c92017-10-17 08:10:17 +02001217 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001218 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001219 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001220
Willy Tarreau27a84c92017-10-17 08:10:17 +02001221 if (unlikely(ret <= 0)) {
1222 if (!ret) {
1223 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001224 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001225 return 0;
1226 }
1227 else {
1228 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1229 return 0;
1230 }
1231 }
1232
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001233 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001234 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001235 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001236 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001237 }
1238
Willy Tarreau27a84c92017-10-17 08:10:17 +02001239 return ret;
1240}
1241
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001242/* try to send an empty DATA frame with the ES flag set to notify about the
1243 * end of stream and match a shutdown(write). If an ES was already sent as
1244 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1245 * on success or zero if nothing was done. In case of lack of room to write the
1246 * message, it subscribes the requesting stream to future notifications.
1247 */
1248static int h2_send_empty_data_es(struct h2s *h2s)
1249{
1250 struct h2c *h2c = h2s->h2c;
1251 struct buffer *res;
1252 char str[9];
1253 int ret;
1254
Willy Tarreau721c9742017-11-07 11:05:42 +01001255 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001256 return 1;
1257
1258 if (h2c_mux_busy(h2c, h2s)) {
1259 h2s->flags |= H2_SF_BLK_MBUSY;
1260 return 0;
1261 }
1262
Willy Tarreau44e973f2018-03-01 17:49:30 +01001263 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001264 if (!res) {
1265 h2c->flags |= H2_CF_MUX_MALLOC;
1266 h2s->flags |= H2_SF_BLK_MROOM;
1267 return 0;
1268 }
1269
1270 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1271 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1272 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001273 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001274 if (likely(ret > 0)) {
1275 h2s->flags |= H2_SF_ES_SENT;
1276 }
1277 else if (!ret) {
1278 h2c->flags |= H2_CF_MUX_MFULL;
1279 h2s->flags |= H2_SF_BLK_MROOM;
1280 return 0;
1281 }
1282 else {
1283 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1284 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001285 }
1286 return ret;
1287}
1288
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001289/* wake the streams attached to the connection, whose id is greater than <last>,
1290 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001291 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1292 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001293 */
1294static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1295{
1296 struct eb32_node *node;
1297 struct h2s *h2s;
1298
1299 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001300 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001301
1302 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001303 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001304
1305 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1306 while (node) {
1307 h2s = container_of(node, struct h2s, by_id);
1308 if (h2s->id <= last)
1309 break;
1310 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001311
1312 if (!h2s->cs) {
1313 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001314 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001315 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001316 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001317
1318 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001319 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1320 h2s->cs->flags |= CS_FL_ERROR;
1321
Willy Tarreauf830f012018-12-19 17:44:55 +01001322 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001323
Willy Tarreaua8519352018-12-18 16:44:28 +01001324 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001325 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001326 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001327 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001328 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001329 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001330 }
1331}
1332
Willy Tarreau3421aba2017-07-27 15:41:03 +02001333/* Increase all streams' outgoing window size by the difference passed in
1334 * argument. This is needed upon receipt of the settings frame if the initial
1335 * window size is different. The difference may be negative and the resulting
1336 * window size as well, for the time it takes to receive some window updates.
1337 */
1338static void h2c_update_all_ws(struct h2c *h2c, int diff)
1339{
1340 struct h2s *h2s;
1341 struct eb32_node *node;
1342
1343 if (!diff)
1344 return;
1345
1346 node = eb32_first(&h2c->streams_by_id);
1347 while (node) {
1348 h2s = container_of(node, struct h2s, by_id);
1349 h2s->mws += diff;
1350 node = eb32_next(node);
1351 }
1352}
1353
1354/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1355 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1356 * return an error in h2c. Described in RFC7540#6.5.
1357 */
1358static int h2c_handle_settings(struct h2c *h2c)
1359{
1360 unsigned int offset;
1361 int error;
1362
1363 if (h2c->dff & H2_F_SETTINGS_ACK) {
1364 if (h2c->dfl) {
1365 error = H2_ERR_FRAME_SIZE_ERROR;
1366 goto fail;
1367 }
1368 return 1;
1369 }
1370
1371 if (h2c->dsi != 0) {
1372 error = H2_ERR_PROTOCOL_ERROR;
1373 goto fail;
1374 }
1375
1376 if (h2c->dfl % 6) {
1377 error = H2_ERR_FRAME_SIZE_ERROR;
1378 goto fail;
1379 }
1380
1381 /* that's the limit we can process */
1382 if (h2c->dfl > global.tune.bufsize) {
1383 error = H2_ERR_FRAME_SIZE_ERROR;
1384 goto fail;
1385 }
1386
1387 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001388 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001389 return 0;
1390
1391 /* parse the frame */
1392 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001393 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1394 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001395
1396 switch (type) {
1397 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1398 /* we need to update all existing streams with the
1399 * difference from the previous iws.
1400 */
1401 if (arg < 0) { // RFC7540#6.5.2
1402 error = H2_ERR_FLOW_CONTROL_ERROR;
1403 goto fail;
1404 }
1405 h2c_update_all_ws(h2c, arg - h2c->miw);
1406 h2c->miw = arg;
1407 break;
1408 case H2_SETTINGS_MAX_FRAME_SIZE:
1409 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1410 error = H2_ERR_PROTOCOL_ERROR;
1411 goto fail;
1412 }
1413 h2c->mfs = arg;
1414 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001415 case H2_SETTINGS_ENABLE_PUSH:
1416 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1417 error = H2_ERR_PROTOCOL_ERROR;
1418 goto fail;
1419 }
1420 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001421 }
1422 }
1423
1424 /* need to ACK this frame now */
1425 h2c->st0 = H2_CS_FRAME_A;
1426 return 1;
1427 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001428 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001429 h2c_error(h2c, error);
1430 return 0;
1431}
1432
1433/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1434 * success or one of the h2_status values.
1435 */
1436static int h2c_ack_settings(struct h2c *h2c)
1437{
1438 struct buffer *res;
1439 char str[9];
1440 int ret = -1;
1441
1442 if (h2c_mux_busy(h2c, NULL)) {
1443 h2c->flags |= H2_CF_DEM_MBUSY;
1444 return 0;
1445 }
1446
Willy Tarreau44e973f2018-03-01 17:49:30 +01001447 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001448 if (!res) {
1449 h2c->flags |= H2_CF_MUX_MALLOC;
1450 h2c->flags |= H2_CF_DEM_MROOM;
1451 return 0;
1452 }
1453
1454 memcpy(str,
1455 "\x00\x00\x00" /* length : 0 (no data) */
1456 "\x04" "\x01" /* type : 4, flags : ACK */
1457 "\x00\x00\x00\x00" /* stream ID */, 9);
1458
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001459 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001460 if (unlikely(ret <= 0)) {
1461 if (!ret) {
1462 h2c->flags |= H2_CF_MUX_MFULL;
1463 h2c->flags |= H2_CF_DEM_MROOM;
1464 return 0;
1465 }
1466 else {
1467 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1468 return 0;
1469 }
1470 }
1471 return ret;
1472}
1473
Willy Tarreaucf68c782017-10-10 17:11:41 +02001474/* processes a PING frame and schedules an ACK if needed. The caller must pass
1475 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1476 * missing data. It may return an error in h2c.
1477 */
1478static int h2c_handle_ping(struct h2c *h2c)
1479{
1480 /* frame length must be exactly 8 */
1481 if (h2c->dfl != 8) {
1482 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1483 return 0;
1484 }
1485
1486 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001487 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001488 h2c->st0 = H2_CS_FRAME_A;
1489 return 1;
1490}
1491
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001492/* Try to send a window update for stream id <sid> and value <increment>.
1493 * Returns > 0 on success or zero on missing room or failure. It may return an
1494 * error in h2c.
1495 */
1496static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1497{
1498 struct buffer *res;
1499 char str[13];
1500 int ret = -1;
1501
1502 if (h2c_mux_busy(h2c, NULL)) {
1503 h2c->flags |= H2_CF_DEM_MBUSY;
1504 return 0;
1505 }
1506
Willy Tarreau44e973f2018-03-01 17:49:30 +01001507 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001508 if (!res) {
1509 h2c->flags |= H2_CF_MUX_MALLOC;
1510 h2c->flags |= H2_CF_DEM_MROOM;
1511 return 0;
1512 }
1513
1514 /* length: 4, type: 8, flags: none */
1515 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1516 write_n32(str + 5, sid);
1517 write_n32(str + 9, increment);
1518
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001519 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001520
1521 if (unlikely(ret <= 0)) {
1522 if (!ret) {
1523 h2c->flags |= H2_CF_MUX_MFULL;
1524 h2c->flags |= H2_CF_DEM_MROOM;
1525 return 0;
1526 }
1527 else {
1528 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1529 return 0;
1530 }
1531 }
1532 return ret;
1533}
1534
1535/* try to send pending window update for the connection. It's safe to call it
1536 * with no pending updates. Returns > 0 on success or zero on missing room or
1537 * failure. It may return an error in h2c.
1538 */
1539static int h2c_send_conn_wu(struct h2c *h2c)
1540{
1541 int ret = 1;
1542
1543 if (h2c->rcvd_c <= 0)
1544 return 1;
1545
Willy Tarreau97aaa672018-12-23 09:49:04 +01001546 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1547 /* increase the advertised connection window to 2G on
1548 * first update.
1549 */
1550 h2c->flags |= H2_CF_WINDOW_OPENED;
1551 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1552 }
1553
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001554 /* send WU for the connection */
1555 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1556 if (ret > 0)
1557 h2c->rcvd_c = 0;
1558
1559 return ret;
1560}
1561
1562/* try to send pending window update for the current dmux stream. It's safe to
1563 * call it with no pending updates. Returns > 0 on success or zero on missing
1564 * room or failure. It may return an error in h2c.
1565 */
1566static int h2c_send_strm_wu(struct h2c *h2c)
1567{
1568 int ret = 1;
1569
1570 if (h2c->rcvd_s <= 0)
1571 return 1;
1572
1573 /* send WU for the stream */
1574 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1575 if (ret > 0)
1576 h2c->rcvd_s = 0;
1577
1578 return ret;
1579}
1580
Willy Tarreaucf68c782017-10-10 17:11:41 +02001581/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1582 * success, 0 on missing data or one of the h2_status values.
1583 */
1584static int h2c_ack_ping(struct h2c *h2c)
1585{
1586 struct buffer *res;
1587 char str[17];
1588 int ret = -1;
1589
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001590 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001591 return 0;
1592
1593 if (h2c_mux_busy(h2c, NULL)) {
1594 h2c->flags |= H2_CF_DEM_MBUSY;
1595 return 0;
1596 }
1597
Willy Tarreau44e973f2018-03-01 17:49:30 +01001598 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001599 if (!res) {
1600 h2c->flags |= H2_CF_MUX_MALLOC;
1601 h2c->flags |= H2_CF_DEM_MROOM;
1602 return 0;
1603 }
1604
1605 memcpy(str,
1606 "\x00\x00\x08" /* length : 8 (same payload) */
1607 "\x06" "\x01" /* type : 6, flags : ACK */
1608 "\x00\x00\x00\x00" /* stream ID */, 9);
1609
1610 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001611 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001612
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001613 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001614 if (unlikely(ret <= 0)) {
1615 if (!ret) {
1616 h2c->flags |= H2_CF_MUX_MFULL;
1617 h2c->flags |= H2_CF_DEM_MROOM;
1618 return 0;
1619 }
1620 else {
1621 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1622 return 0;
1623 }
1624 }
1625 return ret;
1626}
1627
Willy Tarreau26f95952017-07-27 17:18:30 +02001628/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1629 * Returns > 0 on success or zero on missing data. It may return an error in
1630 * h2c or h2s. Described in RFC7540#6.9.
1631 */
1632static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1633{
1634 int32_t inc;
1635 int error;
1636
1637 if (h2c->dfl != 4) {
1638 error = H2_ERR_FRAME_SIZE_ERROR;
1639 goto conn_err;
1640 }
1641
1642 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001643 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001644 return 0;
1645
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001646 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001647
1648 if (h2c->dsi != 0) {
1649 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001650
1651 /* it's not an error to receive WU on a closed stream */
1652 if (h2s->st == H2_SS_CLOSED)
1653 return 1;
1654
1655 if (!inc) {
1656 error = H2_ERR_PROTOCOL_ERROR;
1657 goto strm_err;
1658 }
1659
1660 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1661 error = H2_ERR_FLOW_CONTROL_ERROR;
1662 goto strm_err;
1663 }
1664
1665 h2s->mws += inc;
1666 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1667 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001668 if (h2s->send_wait)
1669 LIST_ADDQ(&h2c->send_list, &h2s->list);
1670
Willy Tarreau26f95952017-07-27 17:18:30 +02001671 }
1672 }
1673 else {
1674 /* connection window update */
1675 if (!inc) {
1676 error = H2_ERR_PROTOCOL_ERROR;
1677 goto conn_err;
1678 }
1679
1680 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1681 error = H2_ERR_FLOW_CONTROL_ERROR;
1682 goto conn_err;
1683 }
1684
1685 h2c->mws += inc;
1686 }
1687
1688 return 1;
1689
1690 conn_err:
1691 h2c_error(h2c, error);
1692 return 0;
1693
1694 strm_err:
1695 if (h2s) {
1696 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001697 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001698 }
1699 else
1700 h2c_error(h2c, error);
1701 return 0;
1702}
1703
Willy Tarreaue96b0922017-10-30 00:28:29 +01001704/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1705 * the last ID. Returns > 0 on success or zero on missing data. It may return
1706 * an error in h2c. Described in RFC7540#6.8.
1707 */
1708static int h2c_handle_goaway(struct h2c *h2c)
1709{
1710 int error;
1711 int last;
1712
1713 if (h2c->dsi != 0) {
1714 error = H2_ERR_PROTOCOL_ERROR;
1715 goto conn_err;
1716 }
1717
1718 if (h2c->dfl < 8) {
1719 error = H2_ERR_FRAME_SIZE_ERROR;
1720 goto conn_err;
1721 }
1722
1723 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001724 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001725 return 0;
1726
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001727 last = h2_get_n32(&h2c->dbuf, 0);
1728 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001729 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001730 if (h2c->last_sid < 0)
1731 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001732 return 1;
1733
1734 conn_err:
1735 h2c_error(h2c, error);
1736 return 0;
1737}
1738
Willy Tarreau92153fc2017-12-03 19:46:19 +01001739/* processes a PRIORITY frame, and either skips it or rejects if it is
1740 * invalid. Returns > 0 on success or zero on missing data. It may return
1741 * an error in h2c. Described in RFC7540#6.3.
1742 */
1743static int h2c_handle_priority(struct h2c *h2c)
1744{
1745 int error;
1746
1747 if (h2c->dsi == 0) {
1748 error = H2_ERR_PROTOCOL_ERROR;
1749 goto conn_err;
1750 }
1751
1752 if (h2c->dfl != 5) {
1753 error = H2_ERR_FRAME_SIZE_ERROR;
1754 goto conn_err;
1755 }
1756
1757 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001758 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001759 return 0;
1760
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001761 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001762 /* 7540#5.3 : can't depend on itself */
1763 error = H2_ERR_PROTOCOL_ERROR;
1764 goto conn_err;
1765 }
1766 return 1;
1767
1768 conn_err:
1769 h2c_error(h2c, error);
1770 return 0;
1771}
1772
Willy Tarreaucd234e92017-08-18 10:59:39 +02001773/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1774 * Returns > 0 on success or zero on missing data. It may return an error in
1775 * h2c. Described in RFC7540#6.4.
1776 */
1777static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1778{
1779 int error;
1780
1781 if (h2c->dsi == 0) {
1782 error = H2_ERR_PROTOCOL_ERROR;
1783 goto conn_err;
1784 }
1785
Willy Tarreaucd234e92017-08-18 10:59:39 +02001786 if (h2c->dfl != 4) {
1787 error = H2_ERR_FRAME_SIZE_ERROR;
1788 goto conn_err;
1789 }
1790
1791 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001792 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001793 return 0;
1794
1795 /* late RST, already handled */
1796 if (h2s->st == H2_SS_CLOSED)
1797 return 1;
1798
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001799 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001800 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001801
1802 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001803 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001804 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001805 }
1806
1807 h2s->flags |= H2_SF_RST_RCVD;
1808 return 1;
1809
1810 conn_err:
1811 h2c_error(h2c, error);
1812 return 0;
1813}
1814
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001815/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1816 * It may return an error in h2c or h2s. The caller must consider that the
1817 * return value is the new h2s in case one was allocated (most common case).
1818 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001819 * errors here are reported as connection errors since it's impossible to
1820 * recover from such errors after the compression context has been altered.
1821 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001822static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001823{
1824 int error;
1825
1826 if (!h2c->dfl) {
1827 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001828 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001829 goto strm_err;
1830 }
1831
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001832 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001833 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001834
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001835 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001836 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001837
Willy Tarreauf2101912018-07-19 10:11:38 +02001838 if (h2c->flags & H2_CF_DEM_TOOMANY)
1839 return 0; // too many cs still present
1840
Willy Tarreau13278b42017-10-13 19:23:14 +02001841 /* now either the frame is complete or the buffer is complete */
1842 if (h2s->st != H2_SS_IDLE) {
1843 /* FIXME: stream already exists, this is only allowed for
1844 * trailers (not supported for now).
1845 */
1846 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001847 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001848 goto conn_err;
1849 }
1850 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1851 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1852 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001853 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001854 goto conn_err;
1855 }
1856
Willy Tarreau22de8d32018-09-05 19:55:58 +02001857 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001858 * positively from h2c_frt_stream_new(), the stream will report the error,
1859 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001860 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001861 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001862 if (!h2s) {
1863 error = H2_ERR_INTERNAL_ERROR;
1864 goto conn_err;
1865 }
1866
1867 h2s->st = H2_SS_OPEN;
1868 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1869 h2s->st = H2_SS_HREM;
1870 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001871 /* note: cs cannot be null for now (just created above) */
1872 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001873 }
1874
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001875 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001876 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001877
Willy Tarreau8f650c32017-11-21 19:36:21 +01001878 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001879 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001880
Willy Tarreau721c9742017-11-07 11:05:42 +01001881 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001882 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001883 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001884 }
1885 else {
1886 /* update the max stream ID if the request is being processed */
1887 if (h2s->id > h2c->max_id)
1888 h2c->max_id = h2s->id;
1889 }
1890
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001891 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001892
1893 conn_err:
1894 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001895 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001896
1897 strm_err:
1898 if (h2s) {
1899 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001900 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001901 }
1902 else
1903 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001904 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001905}
1906
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001907/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1908 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1909 * errors here are reported as connection errors since it's impossible to
1910 * recover from such errors after the compression context has been altered.
1911 */
1912static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1913{
1914 int error;
1915
1916 if (!h2c->dfl) {
1917 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1918 sess_log(h2c->conn->owner);
1919 goto strm_err;
1920 }
1921
1922 if (!b_size(&h2c->dbuf))
1923 return NULL; // empty buffer
1924
1925 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1926 return NULL; // incomplete frame
1927
1928 if (h2c->flags & H2_CF_DEM_TOOMANY)
1929 return 0; // too many cs still present
1930
1931 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1932 h2s->flags |= H2_SF_ES_RCVD;
1933 h2s->cs->flags |= CS_FL_REOS;
1934 }
1935
1936 if (!h2s_decode_headers(h2s))
1937 return NULL;
1938
1939 if (h2c->st0 >= H2_CS_ERROR)
1940 return NULL;
1941
1942 if (h2s->st >= H2_SS_ERROR) {
1943 /* stream error : send RST_STREAM */
1944 h2c->st0 = H2_CS_FRAME_E;
1945 }
1946
1947 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1948 h2s->st = H2_SS_ERROR;
1949 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1950 h2s->st = H2_SS_HREM;
1951 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1952 h2s_close(h2s);
1953
1954 return h2s;
1955
1956 conn_err:
1957 h2c_error(h2c, error);
1958 return NULL;
1959
1960 strm_err:
1961 if (h2s) {
1962 h2s_error(h2s, error);
1963 h2c->st0 = H2_CS_FRAME_E;
1964 }
1965 else
1966 h2c_error(h2c, error);
1967 return NULL;
1968}
1969
Willy Tarreau454f9052017-10-26 19:40:35 +02001970/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1971 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1972 */
1973static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1974{
1975 int error;
1976
1977 /* note that empty DATA frames are perfectly valid and sometimes used
1978 * to signal an end of stream (with the ES flag).
1979 */
1980
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001981 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001982 return 0; // empty buffer
1983
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001984 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001985 return 0; // incomplete frame
1986
1987 /* now either the frame is complete or the buffer is complete */
1988
1989 if (!h2c->dsi) {
1990 /* RFC7540#6.1 */
1991 error = H2_ERR_PROTOCOL_ERROR;
1992 goto conn_err;
1993 }
1994
1995 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1996 /* RFC7540#6.1 */
1997 error = H2_ERR_STREAM_CLOSED;
1998 goto strm_err;
1999 }
2000
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002001 if (!h2_frt_transfer_data(h2s))
2002 return 0;
2003
Willy Tarreau454f9052017-10-26 19:40:35 +02002004 /* call the upper layers to process the frame, then let the upper layer
2005 * notify the stream about any change.
2006 */
2007 if (!h2s->cs) {
2008 error = H2_ERR_STREAM_CLOSED;
2009 goto strm_err;
2010 }
2011
Willy Tarreau8f650c32017-11-21 19:36:21 +01002012 if (h2c->st0 >= H2_CS_ERROR)
2013 return 0;
2014
Willy Tarreau721c9742017-11-07 11:05:42 +01002015 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002016 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002017 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002018 }
2019
2020 /* check for completion : the callee will change this to FRAME_A or
2021 * FRAME_H once done.
2022 */
2023 if (h2c->st0 == H2_CS_FRAME_P)
2024 return 0;
2025
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002026
2027 /* last frame */
2028 if (h2c->dff & H2_F_DATA_END_STREAM) {
2029 h2s->st = H2_SS_HREM;
2030 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002031 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002032 }
2033
Willy Tarreau454f9052017-10-26 19:40:35 +02002034 return 1;
2035
2036 conn_err:
2037 h2c_error(h2c, error);
2038 return 0;
2039
2040 strm_err:
2041 if (h2s) {
2042 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002043 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002044 }
2045 else
2046 h2c_error(h2c, error);
2047 return 0;
2048}
2049
Willy Tarreaubc933932017-10-09 16:21:43 +02002050/* process Rx frames to be demultiplexed */
2051static void h2_process_demux(struct h2c *h2c)
2052{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002053 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002054
Willy Tarreau081d4722017-05-16 21:51:05 +02002055 if (h2c->st0 >= H2_CS_ERROR)
2056 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002057
2058 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2059 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002060 if (h2c->flags & H2_CF_IS_BACK)
2061 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002062 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2063 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002064 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002065 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002066 sess_log(h2c->conn->owner);
2067 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002068 goto fail;
2069 }
2070
2071 h2c->max_id = 0;
2072 h2c->st0 = H2_CS_SETTINGS1;
2073 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002074
2075 if (h2c->st0 == H2_CS_SETTINGS1) {
2076 struct h2_fh hdr;
2077
2078 /* ensure that what is pending is a valid SETTINGS frame
2079 * without an ACK.
2080 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002081 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002082 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002083 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002084 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002085 sess_log(h2c->conn->owner);
2086 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002087 goto fail;
2088 }
2089
2090 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2091 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2092 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2093 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002094 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002095 goto fail;
2096 }
2097
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002098 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002099 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2100 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2101 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002102 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002103 goto fail;
2104 }
2105
2106 /* that's OK, switch to FRAME_P to process it */
2107 h2c->dfl = hdr.len;
2108 h2c->dsi = hdr.sid;
2109 h2c->dft = hdr.ft;
2110 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002111 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002112 h2c->st0 = H2_CS_FRAME_P;
2113 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002114 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002115
2116 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002117 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002118 int ret = 0;
2119
2120 if (h2c->st0 >= H2_CS_ERROR)
2121 break;
2122
2123 if (h2c->st0 == H2_CS_FRAME_H) {
2124 struct h2_fh hdr;
2125
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002126 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002127 break;
2128
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002129 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002130 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2131 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002132 if (!h2c->nb_streams) {
2133 /* only log if no other stream can report the error */
2134 sess_log(h2c->conn->owner);
2135 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002136 break;
2137 }
2138
2139 h2c->dfl = hdr.len;
2140 h2c->dsi = hdr.sid;
2141 h2c->dft = hdr.ft;
2142 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002143 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002144 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002145 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002146 }
2147
2148 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002149 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2150
Willy Tarreau567beb82018-12-18 16:52:44 +01002151 if (tmp_h2s != h2s && h2s && h2s->cs &&
2152 (b_data(&h2s->rxbuf) ||
2153 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002154 /* we may have to signal the upper layers */
2155 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002156 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002157 }
2158 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002159
Willy Tarreaud7901432017-12-29 11:34:40 +01002160 if (h2c->st0 == H2_CS_FRAME_E)
2161 goto strm_err;
2162
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002163 if (h2s->st == H2_SS_IDLE &&
2164 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2165 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2166 * this state MUST be treated as a connection error
2167 */
2168 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2169 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002170 if (!h2c->nb_streams) {
2171 /* only log if no other stream can report the error */
2172 sess_log(h2c->conn->owner);
2173 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002174 break;
2175 }
2176
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002177 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2178 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2179 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2180 * this state MUST be treated as a stream error
2181 */
2182 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2183 goto strm_err;
2184 }
2185
Willy Tarreauab837502017-12-27 15:07:30 +01002186 /* Below the management of frames received in closed state is a
2187 * bit hackish because the spec makes strong differences between
2188 * streams closed by receiving RST, sending RST, and seeing ES
2189 * in both directions. In addition to this, the creation of a
2190 * new stream reusing the identifier of a closed one will be
2191 * detected here. Given that we cannot keep track of all closed
2192 * streams forever, we consider that unknown closed streams were
2193 * closed on RST received, which allows us to respond with an
2194 * RST without breaking the connection (eg: to abort a transfer).
2195 * Some frames have to be silently ignored as well.
2196 */
2197 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2198 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2199 /* #5.1.1: The identifier of a newly
2200 * established stream MUST be numerically
2201 * greater than all streams that the initiating
2202 * endpoint has opened or reserved. This
2203 * governs streams that are opened using a
2204 * HEADERS frame and streams that are reserved
2205 * using PUSH_PROMISE. An endpoint that
2206 * receives an unexpected stream identifier
2207 * MUST respond with a connection error.
2208 */
2209 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2210 goto strm_err;
2211 }
2212
2213 if (h2s->flags & H2_SF_RST_RCVD) {
2214 /* RFC7540#5.1:closed: an endpoint that
2215 * receives any frame other than PRIORITY after
2216 * receiving a RST_STREAM MUST treat that as a
2217 * stream error of type STREAM_CLOSED.
2218 *
2219 * Note that old streams fall into this category
2220 * and will lead to an RST being sent.
2221 */
2222 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2223 h2c->st0 = H2_CS_FRAME_E;
2224 goto strm_err;
2225 }
2226
2227 /* RFC7540#5.1:closed: if this state is reached as a
2228 * result of sending a RST_STREAM frame, the peer that
2229 * receives the RST_STREAM might have already sent
2230 * frames on the stream that cannot be withdrawn. An
2231 * endpoint MUST ignore frames that it receives on
2232 * closed streams after it has sent a RST_STREAM
2233 * frame. An endpoint MAY choose to limit the period
2234 * over which it ignores frames and treat frames that
2235 * arrive after this time as being in error.
2236 */
2237 if (!(h2s->flags & H2_SF_RST_SENT)) {
2238 /* RFC7540#5.1:closed: any frame other than
2239 * PRIO/WU/RST in this state MUST be treated as
2240 * a connection error
2241 */
2242 if (h2c->dft != H2_FT_RST_STREAM &&
2243 h2c->dft != H2_FT_PRIORITY &&
2244 h2c->dft != H2_FT_WINDOW_UPDATE) {
2245 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2246 goto strm_err;
2247 }
2248 }
2249 }
2250
Willy Tarreauc0da1962017-10-30 18:38:00 +01002251#if 0
2252 // problem below: it is not possible to completely ignore such
2253 // streams as we need to maintain the compression state as well
2254 // and for this we need to completely process these frames (eg:
2255 // HEADERS frames) as well as counting DATA frames to emit
2256 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2257 // This is a typical case of layer violation where the
2258 // transported contents are critical to the connection's
2259 // validity and must be ignored at the same time :-(
2260
2261 /* graceful shutdown, ignore streams whose ID is higher than
2262 * the one advertised in GOAWAY. RFC7540#6.8.
2263 */
2264 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002265 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2266 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002267 h2c->dfl -= ret;
2268 ret = h2c->dfl == 0;
2269 goto strm_err;
2270 }
2271#endif
2272
Willy Tarreau7e98c052017-10-10 15:56:59 +02002273 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002274 case H2_FT_SETTINGS:
2275 if (h2c->st0 == H2_CS_FRAME_P)
2276 ret = h2c_handle_settings(h2c);
2277
2278 if (h2c->st0 == H2_CS_FRAME_A)
2279 ret = h2c_ack_settings(h2c);
2280 break;
2281
Willy Tarreaucf68c782017-10-10 17:11:41 +02002282 case H2_FT_PING:
2283 if (h2c->st0 == H2_CS_FRAME_P)
2284 ret = h2c_handle_ping(h2c);
2285
2286 if (h2c->st0 == H2_CS_FRAME_A)
2287 ret = h2c_ack_ping(h2c);
2288 break;
2289
Willy Tarreau26f95952017-07-27 17:18:30 +02002290 case H2_FT_WINDOW_UPDATE:
2291 if (h2c->st0 == H2_CS_FRAME_P)
2292 ret = h2c_handle_window_update(h2c, h2s);
2293 break;
2294
Willy Tarreau61290ec2017-10-17 08:19:21 +02002295 case H2_FT_CONTINUATION:
2296 /* we currently don't support CONTINUATION frames since
2297 * we have nowhere to store the partial HEADERS frame.
2298 * Let's abort the stream on an INTERNAL_ERROR here.
2299 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002300 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002301 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002302 h2c->st0 = H2_CS_FRAME_E;
2303 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002304 break;
2305
Willy Tarreau13278b42017-10-13 19:23:14 +02002306 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002307 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002308 if (h2c->flags & H2_CF_IS_BACK)
2309 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2310 else
2311 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002312 if (tmp_h2s) {
2313 h2s = tmp_h2s;
2314 ret = 1;
2315 }
2316 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002317 break;
2318
Willy Tarreau454f9052017-10-26 19:40:35 +02002319 case H2_FT_DATA:
2320 if (h2c->st0 == H2_CS_FRAME_P)
2321 ret = h2c_frt_handle_data(h2c, h2s);
2322
2323 if (h2c->st0 == H2_CS_FRAME_A)
2324 ret = h2c_send_strm_wu(h2c);
2325 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002326
Willy Tarreau92153fc2017-12-03 19:46:19 +01002327 case H2_FT_PRIORITY:
2328 if (h2c->st0 == H2_CS_FRAME_P)
2329 ret = h2c_handle_priority(h2c);
2330 break;
2331
Willy Tarreaucd234e92017-08-18 10:59:39 +02002332 case H2_FT_RST_STREAM:
2333 if (h2c->st0 == H2_CS_FRAME_P)
2334 ret = h2c_handle_rst_stream(h2c, h2s);
2335 break;
2336
Willy Tarreaue96b0922017-10-30 00:28:29 +01002337 case H2_FT_GOAWAY:
2338 if (h2c->st0 == H2_CS_FRAME_P)
2339 ret = h2c_handle_goaway(h2c);
2340 break;
2341
Willy Tarreau1c661982017-10-30 13:52:01 +01002342 case H2_FT_PUSH_PROMISE:
2343 /* not permitted here, RFC7540#5.1 */
2344 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002345 if (!h2c->nb_streams) {
2346 /* only log if no other stream can report the error */
2347 sess_log(h2c->conn->owner);
2348 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002349 break;
2350
2351 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002352 default:
2353 /* drop frames that we ignore. They may be larger than
2354 * the buffer so we drain all of their contents until
2355 * we reach the end.
2356 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002357 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2358 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002359 h2c->dfl -= ret;
2360 ret = h2c->dfl == 0;
2361 }
2362
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002363 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002364 /* We may have to send an RST if not done yet */
2365 if (h2s->st == H2_SS_ERROR)
2366 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002367
Willy Tarreaua20a5192017-12-27 11:02:06 +01002368 if (h2c->st0 == H2_CS_FRAME_E)
2369 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002370
Willy Tarreau7e98c052017-10-10 15:56:59 +02002371 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002372 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002373 break;
2374
2375 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002376 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002377 h2c->st0 = H2_CS_FRAME_H;
2378 }
2379 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002380
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002381 if (h2c->rcvd_c > 0 &&
2382 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2383 h2c_send_conn_wu(h2c);
2384
Willy Tarreau52eed752017-09-22 15:05:09 +02002385 fail:
2386 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002387 if (h2s && h2s->cs &&
2388 (b_data(&h2s->rxbuf) ||
2389 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002390 /* we may have to signal the upper layers */
2391 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002392 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002393 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002394
Willy Tarreau47b515a2018-12-21 16:09:41 +01002395 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002396}
2397
2398/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2399 * the end.
2400 */
2401static int h2_process_mux(struct h2c *h2c)
2402{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002403 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002404
Willy Tarreau01b44822018-10-03 14:26:37 +02002405 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2406 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2407 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2408 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2409 if (h2c->st0 == H2_CS_ERROR) {
2410 h2c->st0 = H2_CS_ERROR2;
2411 sess_log(h2c->conn->owner);
2412 }
2413 goto fail;
2414 }
2415 h2c->st0 = H2_CS_SETTINGS1;
2416 }
2417 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002418 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002419 return 1;
2420 }
2421
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002422 /* start by sending possibly pending window updates */
2423 if (h2c->rcvd_c > 0 &&
2424 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2425 h2c_send_conn_wu(h2c) < 0)
2426 goto fail;
2427
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002428 /* First we always process the flow control list because the streams
2429 * waiting there were already elected for immediate emission but were
2430 * blocked just on this.
2431 */
2432
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002433 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002434 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2435 h2c->st0 >= H2_CS_ERROR)
2436 break;
2437
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002438 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002439 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2440 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002441 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002442 LIST_DEL(&h2s->list);
2443 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002444 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002445 }
2446
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002447 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002448 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2449 break;
2450
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002451 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002452 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2453 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002454 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002455 LIST_DEL(&h2s->list);
2456 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002457 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002458 }
2459
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002460 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002461 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002462 if (h2c->st0 == H2_CS_ERROR) {
2463 if (h2c->max_id >= 0) {
2464 h2c_send_goaway_error(h2c, NULL);
2465 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2466 return 0;
2467 }
2468
2469 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2470 }
2471 return 1;
2472 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002473 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002474}
2475
Willy Tarreau62f52692017-10-08 23:01:42 +02002476
Willy Tarreau479998a2018-11-18 06:30:59 +01002477/* Attempt to read data, and subscribe if none available.
2478 * The function returns 1 if data has been received, otherwise zero.
2479 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002480static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002481{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002482 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002483 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002484 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002485 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002486
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002487 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002488 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002489
Willy Tarreau315d8072017-12-10 22:17:57 +01002490 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002491 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002492
Willy Tarreau44e973f2018-03-01 17:49:30 +01002493 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002494 if (!buf) {
2495 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002496 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002497 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002498
Olivier Houchard7505f942018-08-21 18:10:44 +02002499 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002500 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002501 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2502 /* HTX in use : try to pre-align the buffer like the
2503 * rxbufs will be to optimize memory copies. We'll make
2504 * sure that the frame header lands at the end of the
2505 * HTX block to alias it upon recv. We cannot use the
2506 * head because rcv_buf() will realign the buffer if
2507 * it's empty. Thus we cheat and pretend we already
2508 * have a few bytes there.
2509 */
2510 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002511 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002512 }
2513 else
2514 max = b_room(buf);
2515
Olivier Houchard7505f942018-08-21 18:10:44 +02002516 if (max)
2517 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2518 else
2519 ret = 0;
2520 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002521
Olivier Houchard53216e72018-10-10 15:46:36 +02002522 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002523 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002524
Olivier Houcharda1411e62018-08-17 18:42:48 +02002525 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002526 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002527 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002528 }
2529
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002530 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002531 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002532 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002533}
2534
Willy Tarreau479998a2018-11-18 06:30:59 +01002535/* Try to send data if possible.
2536 * The function returns 1 if data have been sent, otherwise zero.
2537 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002538static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002539{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002540 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002541 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002542 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002543
2544 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002545 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002546
Olivier Houchard7505f942018-08-21 18:10:44 +02002547
Willy Tarreaua2af5122017-10-09 11:56:46 +02002548 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2549 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002550 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002551 }
2552
Willy Tarreaubc933932017-10-09 16:21:43 +02002553 /* This loop is quite simple : it tries to fill as much as it can from
2554 * pending streams into the existing buffer until it's reportedly full
2555 * or the end of send requests is reached. Then it tries to send this
2556 * buffer's contents out, marks it not full if at least one byte could
2557 * be sent, and tries again.
2558 *
2559 * The snd_buf() function normally takes a "flags" argument which may
2560 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2561 * data immediately comes and CO_SFL_STREAMER to indicate that the
2562 * connection is streaming lots of data (used to increase TLS record
2563 * size at the expense of latency). The former can be sent any time
2564 * there's a buffer full flag, as it indicates at least one stream
2565 * attempted to send and failed so there are pending data. An
2566 * alternative would be to set it as long as there's an active stream
2567 * but that would be problematic for ACKs until we have an absolute
2568 * guarantee that all waiters have at least one byte to send. The
2569 * latter should possibly not be set for now.
2570 */
2571
2572 done = 0;
2573 while (!done) {
2574 unsigned int flags = 0;
2575
2576 /* fill as much as we can into the current buffer */
2577 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2578 done = h2_process_mux(h2c);
2579
2580 if (conn->flags & CO_FL_ERROR)
2581 break;
2582
2583 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2584 flags |= CO_SFL_MSG_MORE;
2585
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002586 if (b_data(&h2c->mbuf)) {
2587 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002588 if (!ret)
2589 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002590 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002591 b_del(&h2c->mbuf, ret);
2592 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002593 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002594
2595 /* wrote at least one byte, the buffer is not full anymore */
2596 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2597 }
2598
Willy Tarreaua2af5122017-10-09 11:56:46 +02002599 if (conn->flags & CO_FL_SOCK_WR_SH) {
2600 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002601 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002602 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002603 /* We're not full anymore, so we can wake any task that are waiting
2604 * for us.
2605 */
2606 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002607 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002608 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2609 struct h2s *, list);
2610 LIST_DEL(&h2s->list);
2611 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002612 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002613 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2614 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002615 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002616 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002617 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002618 /* We're done, no more to send */
2619 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002620 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002621schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002622 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2623 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002624 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002625}
2626
2627static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2628{
2629 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002630 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002631
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002632 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002633 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002634 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002635 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002636 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002637 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002638 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002639}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002640
Willy Tarreau62f52692017-10-08 23:01:42 +02002641/* callback called on any event by the connection handler.
2642 * It applies changes and returns zero, or < 0 if it wants immediate
2643 * destruction of the connection (which normally doesn not happen in h2).
2644 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002645static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002646{
Olivier Houchard7505f942018-08-21 18:10:44 +02002647 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002648
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002649 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002650 h2_process_demux(h2c);
2651
2652 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002653 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002654
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002656 h2c->flags &= ~H2_CF_DEM_DFULL;
2657 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002658 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002659
Willy Tarreau0b37d652018-10-03 10:33:02 +02002660 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002661 /* frontend is stopping, reload likely in progress, let's try
2662 * to announce a graceful shutdown if not yet done. We don't
2663 * care if it fails, it will be tried again later.
2664 */
2665 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2666 if (h2c->last_sid < 0)
2667 h2c->last_sid = (1U << 31) - 1;
2668 h2c_send_goaway_error(h2c, NULL);
2669 }
2670 }
2671
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002672 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002673 * If we received early data, and the handshake is done, wake
2674 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002675 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002676 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2677 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2678 struct eb32_node *node;
2679 struct h2s *h2s;
2680
2681 h2c->flags |= H2_CF_WAIT_FOR_HS;
2682 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2683
2684 while (node) {
2685 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002686 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002687 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002688 node = eb32_next(node);
2689 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002690 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002691
Willy Tarreau26bd7612017-10-09 16:47:04 +02002692 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002693 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2694 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2695 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002696 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002697
2698 if (eb_is_empty(&h2c->streams_by_id)) {
2699 /* no more stream, kill the connection now */
2700 h2_release(conn);
2701 return -1;
2702 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002703 }
2704
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002705 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002706 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002707
Olivier Houchard53216e72018-10-10 15:46:36 +02002708 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2709 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2710 (h2c->st0 != H2_CS_ERROR &&
2711 !b_data(&h2c->mbuf) &&
2712 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2713 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002714 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002715
Willy Tarreau3f133572017-10-31 19:21:06 +01002716 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002717 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002718 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002719 task_queue(h2c->task);
2720 }
2721 else
2722 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002723 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002724
Olivier Houchard7505f942018-08-21 18:10:44 +02002725 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002726 return 0;
2727}
2728
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002729static int h2_wake(struct connection *conn)
2730{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002731 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002732
2733 return (h2_process(h2c));
2734}
2735
Willy Tarreauea392822017-10-31 10:02:25 +01002736/* Connection timeout management. The principle is that if there's no receipt
2737 * nor sending for a certain amount of time, the connection is closed. If the
2738 * MUX buffer still has lying data or is not allocatable, the connection is
2739 * immediately killed. If it's allocatable and empty, we attempt to send a
2740 * GOAWAY frame.
2741 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002742static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002743{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002744 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002745 int expired = tick_is_expired(t->expire, now_ms);
2746
Willy Tarreau0975f112018-03-29 15:22:59 +02002747 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002748 return t;
2749
Willy Tarreau0975f112018-03-29 15:22:59 +02002750 task_delete(t);
2751 task_free(t);
2752
2753 if (!h2c) {
2754 /* resources were already deleted */
2755 return NULL;
2756 }
2757
2758 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002759 h2c_error(h2c, H2_ERR_NO_ERROR);
2760 h2_wake_some_streams(h2c, 0, 0);
2761
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002762 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002763 /* don't even try to send a GOAWAY, the buffer is stuck */
2764 h2c->flags |= H2_CF_GOAWAY_FAILED;
2765 }
2766
2767 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002768 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002769 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2770 h2c->flags |= H2_CF_GOAWAY_FAILED;
2771
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002772 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2773 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002774 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002775 b_del(&h2c->mbuf, ret);
2776 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002777 }
2778 }
Willy Tarreauea392822017-10-31 10:02:25 +01002779
Willy Tarreau0975f112018-03-29 15:22:59 +02002780 /* either we can release everything now or it will be done later once
2781 * the last stream closes.
2782 */
2783 if (eb_is_empty(&h2c->streams_by_id))
2784 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002785
Willy Tarreauea392822017-10-31 10:02:25 +01002786 return NULL;
2787}
2788
2789
Willy Tarreau62f52692017-10-08 23:01:42 +02002790/*******************************************/
2791/* functions below are used by the streams */
2792/*******************************************/
2793
2794/*
2795 * Attach a new stream to a connection
2796 * (Used for outgoing connections)
2797 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002798static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002799{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002800 struct conn_stream *cs;
2801 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002802 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002803
2804 cs = cs_new(conn);
2805 if (!cs)
2806 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002807 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002808 if (!h2s) {
2809 cs_free(cs);
2810 return NULL;
2811 }
2812 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002813}
2814
Willy Tarreaufafd3982018-11-18 21:29:20 +01002815/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2816 * We have to scan because we may have some orphan streams. It might be
2817 * beneficial to scan backwards from the end to reduce the likeliness to find
2818 * orphans.
2819 */
2820static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2821{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002822 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002823 struct h2s *h2s;
2824 struct eb32_node *node;
2825
2826 node = eb32_first(&h2c->streams_by_id);
2827 while (node) {
2828 h2s = container_of(node, struct h2s, by_id);
2829 if (h2s->cs)
2830 return h2s->cs;
2831 node = eb32_next(node);
2832 }
2833 return NULL;
2834}
2835
Willy Tarreau62f52692017-10-08 23:01:42 +02002836/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002837 * Destroy the mux and the associated connection, if it is no longer used
2838 */
2839static void h2_destroy(struct connection *conn)
2840{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002841 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002842
2843 if (eb_is_empty(&h2c->streams_by_id))
2844 h2_release(h2c->conn);
2845}
2846
2847/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002848 * Detach the stream from the connection and possibly release the connection.
2849 */
2850static void h2_detach(struct conn_stream *cs)
2851{
Willy Tarreau60935142017-10-16 18:11:19 +02002852 struct h2s *h2s = cs->ctx;
2853 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002854 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002855
2856 cs->ctx = NULL;
2857 if (!h2s)
2858 return;
2859
Olivier Houchardf502aca2018-12-14 19:42:40 +01002860 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002861 h2c = h2s->h2c;
2862 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002863 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002864 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2865 !h2_has_too_many_cs(h2c)) {
2866 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002867 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002868 }
Willy Tarreau60935142017-10-16 18:11:19 +02002869
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002870 /* this stream may be blocked waiting for some data to leave (possibly
2871 * an ES or RST frame), so orphan it in this case.
2872 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002873 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002874 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002875 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002876 return;
2877
Willy Tarreau45f752e2017-10-30 15:44:59 +01002878 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2879 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2880 /* unblock the connection if it was blocked on this
2881 * stream.
2882 */
2883 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2884 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002885 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002886 }
2887
Willy Tarreau71049cc2018-03-28 13:56:39 +02002888 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002889
Olivier Houchard8a786902018-12-15 16:05:40 +01002890 if (h2c->flags & H2_CF_IS_BACK &&
2891 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002892 if (!(h2c->conn->flags &
2893 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2894 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002895 h2c->conn->owner = sess;
2896 session_add_conn(sess, h2c->conn, h2c->conn->target);
Olivier Houchard8a786902018-12-15 16:05:40 +01002897 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002898 if (eb_is_empty(&h2c->streams_by_id)) {
2899 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2900 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2901 return;
2902 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002903 /* Never ever allow to reuse a connection from a non-reuse backend */
2904 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2905 h2c->conn->flags |= CO_FL_PRIVATE;
2906 if (LIST_ISEMPTY(&h2c->conn->list)) {
2907 struct server *srv = objt_server(h2c->conn->target);
2908
2909 if (srv) {
2910 if (h2c->conn->flags & CO_FL_PRIVATE)
2911 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2912 else
2913 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2914 }
2915
2916 }
2917 }
2918 }
2919
Willy Tarreaue323f342018-03-28 13:51:45 +02002920 /* We don't want to close right now unless we're removing the
2921 * last stream, and either the connection is in error, or it
2922 * reached the ID already specified in a GOAWAY frame received
2923 * or sent (as seen by last_sid >= 0).
2924 */
2925 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2926 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002927 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002928 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002929 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002931 (conn_xprt_read0_pending(h2c->conn) ||
2932 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2933 /* no more stream will come, kill it now */
2934 h2_release(h2c->conn);
2935 }
2936 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002937 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002938 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2939 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002940 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002941 else
2942 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002943 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002944}
2945
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002946static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002947{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002948 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002949 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002950
Willy Tarreau721c9742017-11-07 11:05:42 +01002951 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002952 return;
2953
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002954 /* if no outgoing data was seen on this stream, it means it was
2955 * closed with a "tcp-request content" rule that is normally
2956 * used to kill the connection ASAP (eg: limit abuse). In this
2957 * case we send a goaway to close the connection.
2958 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002959 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002960 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002961 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002962
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002963 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2964 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002965 h2c_send_goaway_error(h2c, h2s) <= 0)
2966 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002967
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002968 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002969 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002970 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002971
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002972 return;
2973add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002974 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002975 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002976 if (h2s->flags & H2_SF_BLK_MFCTL) {
2977 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2978 h2s->send_wait = sw;
2979 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2980 h2s->send_wait = sw;
2981 LIST_ADDQ(&h2c->send_list, &h2s->list);
2982 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002983 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002984 /* Let the handler know we want shutr */
2985 sw->handle = (void *)((long)sw->handle | 1);
2986
Willy Tarreau62f52692017-10-08 23:01:42 +02002987}
2988
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002989static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002990{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002991 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002992 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002993
Willy Tarreau721c9742017-11-07 11:05:42 +01002994 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002995 return;
2996
Willy Tarreau67434202017-11-06 20:20:51 +01002997 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002998 /* we can cleanly close using an empty data frame only after headers */
2999
3000 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3001 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003002 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003003
3004 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003005 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003006 else
3007 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003008 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003009 /* if no outgoing data was seen on this stream, it means it was
3010 * closed with a "tcp-request content" rule that is normally
3011 * used to kill the connection ASAP (eg: limit abuse). In this
3012 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003013 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003014 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003015 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003016 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003017
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003018 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3019 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003020 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003021 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003022
Willy Tarreau00dd0782018-03-01 16:31:34 +01003023 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003024 }
3025
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003026 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003027 tasklet_wakeup(h2c->wait_event.task);
3028 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003029
3030 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003031 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003032 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003033 if (h2s->flags & H2_SF_BLK_MFCTL) {
3034 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3035 h2s->send_wait = sw;
3036 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3037 h2s->send_wait = sw;
3038 LIST_ADDQ(&h2c->send_list, &h2s->list);
3039 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003040 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003041 /* let the handler know we want to shutw */
3042 sw->handle = (void *)((long)(sw->handle) | 2);
3043
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003044}
3045
3046static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3047{
3048 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003049 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003050
Olivier Houchard2c68a462018-12-15 22:42:20 +01003051 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003052 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003053 h2s->send_wait = NULL;
3054 LIST_DEL(&h2s->list);
3055 LIST_INIT(&h2s->list);
3056 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003057 if (reason & 2)
3058 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003059 if (reason & 1)
3060 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003061
Olivier Houchard2c68a462018-12-15 22:42:20 +01003062 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003063 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003064 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003065 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003066}
3067
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003068static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3069{
3070 struct h2s *h2s = cs->ctx;
3071
3072 if (!mode)
3073 return;
3074
3075 h2_do_shutr(h2s);
3076}
3077
3078static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3079{
3080 struct h2s *h2s = cs->ctx;
3081
3082 h2_do_shutw(h2s);
3083}
3084
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003085/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003086 * HTX request or response depending on the connection's side. Returns the
3087 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3088 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003089 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003090static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003091{
3092 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003093 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003094 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003095 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003096 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003097 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003098 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003099 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003100 int flen = h2c->dfl;
3101 int outlen = 0;
3102 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003103 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003104
3105 if (!h2c->dfl) {
3106 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003107 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003108 return 0;
3109 }
3110
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003111 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003112 return 0; // incomplete input frame
3113
Willy Tarreau13278b42017-10-13 19:23:14 +02003114 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003115 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003116 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003117 copy = alloc_trash_chunk();
3118 if (!copy) {
3119 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3120 goto fail;
3121 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003122 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3123 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3124 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003125 }
3126
3127 /* The padlen is the first byte before data, and the padding appears
3128 * after data. padlen+data+padding are included in flen.
3129 */
3130 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003131 h2c->dpl = *hdrs;
3132 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003133 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3134 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003135 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003136 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003137 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003138 hdrs += 1; // skip Pad Length
3139 }
3140
3141 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3142 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003143 if (read_n32(hdrs) == h2s->id) {
3144 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3145 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003146 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003147 }
3148
Willy Tarreau13278b42017-10-13 19:23:14 +02003149 hdrs += 5; // stream dep = 4, weight = 1
3150 flen -= 5;
3151 }
3152
3153 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3154 * don't support this for now and can't even decompress so we have to
3155 * break the connection.
3156 */
3157 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3158 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003159 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003160 }
3161
Olivier Houchard638b7992018-08-16 15:41:52 +02003162 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003163 if (!csbuf) {
3164 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003165 goto fail;
3166 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003167
Willy Tarreau937f7602018-02-26 15:22:17 +01003168 /* we can't retry a failed decompression operation so we must be very
3169 * careful not to take any risks. In practice the output buffer is
3170 * always empty except maybe for trailers, in which case we simply have
3171 * to wait for the upper layer to finish consuming what is available.
3172 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003173
3174 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3175 htx = htx_from_buf(&h2s->rxbuf);
3176 if (!htx_is_empty(htx))
3177 goto fail;
3178 } else {
3179 if (b_data(csbuf))
3180 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003181
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003182 csbuf->head = 0;
3183 try = b_size(csbuf);
3184 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003185
3186 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3187 sizeof(list)/sizeof(list[0]), tmp);
3188 if (outlen < 0) {
3189 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3190 goto fail;
3191 }
3192
3193 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003194 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003195
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003196 if (htx) {
3197 /* HTX mode */
3198 if (h2c->flags & H2_CF_IS_BACK)
3199 outlen = h2_make_htx_response(list, htx, &msgf);
3200 else
3201 outlen = h2_make_htx_request(list, htx, &msgf);
3202 } else {
3203 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003204 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003205 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003206
3207 if (outlen < 0) {
3208 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3209 goto fail;
3210 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003211
Willy Tarreau174b06a2018-04-25 18:13:58 +02003212 if (msgf & H2_MSGF_BODY) {
3213 /* a payload is present */
3214 if (msgf & H2_MSGF_BODY_CL)
3215 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003216 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003217 h2s->flags |= H2_SF_DATA_CHNK;
3218 }
3219
Willy Tarreau13278b42017-10-13 19:23:14 +02003220 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003221 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003222 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003223 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003224
Willy Tarreau39d68502018-03-02 12:26:37 +01003225 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003226 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003227 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003228 if (htx)
3229 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003230 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003231
Willy Tarreau68dd9852017-07-03 14:44:26 +02003232 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003233 if (htx)
3234 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003235 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003236 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003237 fail:
3238 outlen = 0;
3239 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003240}
3241
Willy Tarreau454f9052017-10-26 19:40:35 +02003242/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3243 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3244 * in use, a new chunk is emitted for each frame. This is supposed to fit
3245 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3246 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3247 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003248 * parser state is automatically updated. Returns > 0 if it could completely
3249 * send the current frame, 0 if it couldn't complete, in which case
3250 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3251 * DATA frame can return 0 as a valid result). Stream errors are reported in
3252 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3253 * have checked the frame header and ensured that the frame was complete or the
3254 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003255 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003256static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003257{
3258 struct h2c *h2c = h2s->h2c;
3259 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003260 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003261 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003262 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003263 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003264
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003265 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003266
3267 /* The padlen is the first byte before data, and the padding appears
3268 * after data. padlen+data+padding are included in flen.
3269 */
Willy Tarreau79127812017-12-03 21:06:59 +01003270 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003271 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003272 return 0;
3273
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003274 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003275 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003276 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3277 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003278 return 0;
3279 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003280
3281 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003282 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003283 h2c->dfl--;
3284 h2c->rcvd_c++; h2c->rcvd_s++;
3285 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003286 }
3287
Olivier Houchard638b7992018-08-16 15:41:52 +02003288 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003289 if (!csbuf) {
3290 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003291 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003292 }
3293
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003294try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003295 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003296 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3297 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003298 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003299 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003300
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003301 if (flen > b_data(&h2c->dbuf)) {
3302 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003303 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003304 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003305 }
3306
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003307 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003308 block1 = htx_free_data_space(htx);
3309 if (!block1) {
3310 h2c->flags |= H2_CF_DEM_SFULL;
3311 goto fail;
3312 }
3313 if (flen > block1)
3314 flen = block1;
3315
3316 /* here, flen is the max we can copy into the output buffer */
3317 block1 = b_contig_data(&h2c->dbuf, 0);
3318 if (flen > block1)
3319 flen = block1;
3320
3321 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3322 h2c->flags |= H2_CF_DEM_SFULL;
3323 goto fail;
3324 }
3325
3326 b_del(&h2c->dbuf, flen);
3327 h2c->dfl -= flen;
3328 h2c->rcvd_c += flen;
3329 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3330 goto try_again;
3331 }
3332 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003333 /* it doesn't fit and the buffer is fragmented,
3334 * so let's defragment it and try again.
3335 */
3336 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003337 }
3338
Willy Tarreaueba10f22018-04-25 20:44:22 +02003339 /* chunked-encoding requires more room */
3340 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003341 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003342 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3343 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3344 (chklen < 1048576) ? 4 : 8;
3345 chklen += 4; // CRLF, CRLF
3346 }
3347
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003348 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003349 if (flen + chklen > b_room(csbuf)) {
3350 if (chklen >= b_room(csbuf)) {
3351 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003352 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003353 }
3354 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003355 }
3356
3357 if (h2s->flags & H2_SF_DATA_CHNK) {
3358 /* emit the chunk size */
3359 unsigned int chksz = flen;
3360 char str[10];
3361 char *beg;
3362
3363 beg = str + sizeof(str);
3364 *--beg = '\n';
3365 *--beg = '\r';
3366 do {
3367 *--beg = hextab[chksz & 0xF];
3368 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003369 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003370 }
3371
Willy Tarreau454f9052017-10-26 19:40:35 +02003372 /* Block1 is the length of the first block before the buffer wraps,
3373 * block2 is the optional second block to reach the end of the frame.
3374 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003375 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003376 if (block1 > flen)
3377 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003378 block2 = flen - block1;
3379
3380 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003381 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003382
3383 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003384 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003385
Willy Tarreaueba10f22018-04-25 20:44:22 +02003386 if (h2s->flags & H2_SF_DATA_CHNK) {
3387 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003388 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003389 }
3390
Willy Tarreau454f9052017-10-26 19:40:35 +02003391 /* now mark the input data as consumed (will be deleted from the buffer
3392 * by the caller when seeing FRAME_A after sending the window update).
3393 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003394 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003395 h2c->dfl -= flen;
3396 h2c->rcvd_c += flen;
3397 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3398
3399 if (h2c->dfl > h2c->dpl) {
3400 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003401 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003402 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003403 }
3404
Willy Tarreau4a28da12018-01-04 14:41:00 +01003405 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003406 /* here we're done with the frame, all the payload (except padding) was
3407 * transferred.
3408 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003409
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003410 if (h2c->dff & H2_F_DATA_END_STREAM) {
3411 if (htx) {
3412 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3413 h2c->flags |= H2_CF_DEM_SFULL;
3414 goto fail;
3415 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003416 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003417 else if (h2s->flags & H2_SF_DATA_CHNK) {
3418 /* emit the trailing 0 CRLF CRLF */
3419 if (b_room(csbuf) < 5) {
3420 h2c->flags |= H2_CF_DEM_SFULL;
3421 goto fail;
3422 }
3423 chklen += 5;
3424 b_putblk(csbuf, "0\r\n\r\n", 5);
3425 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003426 }
3427
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003428 h2c->rcvd_c += h2c->dpl;
3429 h2c->rcvd_s += h2c->dpl;
3430 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003431 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3432
Willy Tarreau39d68502018-03-02 12:26:37 +01003433 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003434 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003435 h2s->cs->flags |= CS_FL_REOS;
3436 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003437 if (htx)
3438 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003439 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003440 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003441 if (htx)
3442 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003443 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003444}
3445
Willy Tarreau5dd17352018-06-14 13:33:30 +02003446/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3447 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3448 * number of bytes sent. The caller must check the stream's status to detect
3449 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003450 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003451static 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 +02003452{
3453 struct http_hdr list[MAX_HTTP_HDR];
3454 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003455 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003456 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003457 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003458 int es_now = 0;
3459 int ret = 0;
3460 int hdr;
3461
3462 if (h2c_mux_busy(h2c, h2s)) {
3463 h2s->flags |= H2_SF_BLK_MBUSY;
3464 return 0;
3465 }
3466
Willy Tarreau44e973f2018-03-01 17:49:30 +01003467 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003468 h2c->flags |= H2_CF_MUX_MALLOC;
3469 h2s->flags |= H2_SF_BLK_MROOM;
3470 return 0;
3471 }
3472
3473 /* First, try to parse the H1 response and index it into <list>.
3474 * NOTE! Since it comes from haproxy, we *know* that a response header
3475 * block does not wrap and we can safely read it this way without
3476 * having to realign the buffer.
3477 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003478 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003479 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003480 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003481 /* incomplete or invalid response, this is abnormal coming from
3482 * haproxy and may only result in a bad errorfile or bad Lua code
3483 * so that won't be fixed, raise an error now.
3484 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003485 * FIXME: we should instead add the ability to only return a
3486 * 502 bad gateway. But in theory this is not supposed to
3487 * happen.
3488 */
3489 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3490 ret = 0;
3491 goto end;
3492 }
3493
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003494 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003495
3496 /* certain statuses have no body or an empty one, regardless of
3497 * what the headers say.
3498 */
3499 if (sl.st.status >= 100 && sl.st.status < 200) {
3500 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3501 h1m->curr_len = h1m->body_len = 0;
3502 }
3503 else if (sl.st.status == 204 || sl.st.status == 304) {
3504 /* no contents, claim c-len is present and set to zero */
3505 h1m->flags &= ~H1_MF_CHNK;
3506 h1m->flags |= H1_MF_CLEN;
3507 h1m->curr_len = h1m->body_len = 0;
3508 }
3509
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003510 chunk_reset(&outbuf);
3511
3512 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003513 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003514 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003515 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003516
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003517 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003518 break;
3519 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003520 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003521 }
3522
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003523 if (outbuf.size < 9)
3524 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003525
3526 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003527 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3528 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3529 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003530
3531 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003532 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003533 /* this is an unparsable response */
3534 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3535 ret = 0;
3536 goto end;
3537 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003538
3539 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003540 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003541 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003542 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003543 }
3544
3545 /* encode all headers, stop at empty name */
3546 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003547 /* these ones do not exist in H2 and must be dropped. */
3548 if (isteq(list[hdr].n, ist("connection")) ||
3549 isteq(list[hdr].n, ist("proxy-connection")) ||
3550 isteq(list[hdr].n, ist("keep-alive")) ||
3551 isteq(list[hdr].n, ist("upgrade")) ||
3552 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003553 continue;
3554
3555 if (isteq(list[hdr].n, ist("")))
3556 break; // end
3557
3558 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3559 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003560 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003561 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003562 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003563 }
3564 }
3565
3566 /* we may need to add END_STREAM */
3567 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3568 es_now = 1;
3569
3570 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003571 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003572
3573 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003574 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003575
3576 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003577 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003578
3579 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003580 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003581 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003582
3583 /* for now we don't implemented CONTINUATION, so we wait for a
3584 * body or directly end in TRL2.
3585 */
3586 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003587 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003588 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003589
Willy Tarreau801250e2018-09-11 11:45:04 +02003590 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003591 h2s->flags |= H2_SF_ES_SENT;
3592 if (h2s->st == H2_SS_OPEN)
3593 h2s->st = H2_SS_HLOC;
3594 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003595 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003596 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003597 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003598 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003599 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003600 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003601 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003602 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003603 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003604
3605 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003606
3607 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003608 //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 +02003609 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003610 full:
3611 h1m_init_res(h1m);
3612 h1m->err_pos = -1; // don't care about errors on the response path
3613 h2c->flags |= H2_CF_MUX_MFULL;
3614 h2s->flags |= H2_SF_BLK_MROOM;
3615 ret = 0;
3616 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003617}
3618
Willy Tarreau5dd17352018-06-14 13:33:30 +02003619/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3620 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3621 * the number of bytes sent. The caller must check the stream's status to
3622 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003623 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003624static 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 +02003625{
3626 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003627 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003628 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003629 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003630 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003631 int es_now = 0;
3632 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003633 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003634 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003635
3636 if (h2c_mux_busy(h2c, h2s)) {
3637 h2s->flags |= H2_SF_BLK_MBUSY;
3638 goto end;
3639 }
3640
Willy Tarreau44e973f2018-03-01 17:49:30 +01003641 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003642 h2c->flags |= H2_CF_MUX_MALLOC;
3643 h2s->flags |= H2_SF_BLK_MROOM;
3644 goto end;
3645 }
3646
3647 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003648 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003649 goto end;
3650
3651 chunk_reset(&outbuf);
3652
3653 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003654 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003655 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003656 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003657
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003658 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003659 break;
3660 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003661 /* If there are pending data in the output buffer, and we have
3662 * less than 1/4 of the mbuf's size and everything fits, we'll
3663 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3664 * is full and wait, to save some slow realign calls.
3665 */
3666 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3667 h2c->flags |= H2_CF_MUX_MFULL;
3668 h2s->flags |= H2_SF_BLK_MROOM;
3669 goto end;
3670 }
3671
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003672 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003673 }
3674
3675 if (outbuf.size < 9) {
3676 h2c->flags |= H2_CF_MUX_MFULL;
3677 h2s->flags |= H2_SF_BLK_MROOM;
3678 goto end;
3679 }
3680
3681 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003682 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3683 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3684 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003685
3686 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3687 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003688 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003689 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003690 break;
3691 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003692 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003693 if ((long long)size > h1m->curr_len)
3694 size = h1m->curr_len;
3695 break;
3696 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003697 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003698 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003699 if (!ret)
3700 goto end;
3701
3702 if (ret < 0) {
3703 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003704 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003705 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3706 goto end;
3707 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003708 max -= ret;
3709 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003710 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003711 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003712 }
3713
Willy Tarreau801250e2018-09-11 11:45:04 +02003714 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003715 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003716 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003717 if (!ret)
3718 goto end;
3719
3720 if (ret < 0) {
3721 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003722 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003723 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3724 goto end;
3725 }
3726
3727 size = chunk;
3728 h1m->curr_len = chunk;
3729 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003730 max -= ret;
3731 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003732 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003733 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003734 if (!size)
3735 goto send_empty;
3736 }
3737
3738 /* in MSG_DATA state, continue below */
3739 size = h1m->curr_len;
3740 break;
3741 }
3742
3743 /* we have in <size> the exact number of bytes we need to copy from
3744 * the H1 buffer. We need to check this against the connection's and
3745 * the stream's send windows, and to ensure that this fits in the max
3746 * frame size and in the buffer's available space minus 9 bytes (for
3747 * the frame header). The connection's flow control is applied last so
3748 * that we can use a separate list of streams which are immediately
3749 * unblocked on window opening. Note: we don't implement padding.
3750 */
3751
Willy Tarreau5dd17352018-06-14 13:33:30 +02003752 if (size > max)
3753 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003754
3755 if (size > h2s->mws)
3756 size = h2s->mws;
3757
3758 if (size <= 0) {
3759 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003760 if (h2s->send_wait) {
3761 LIST_DEL(&h2s->list);
3762 LIST_INIT(&h2s->list);
3763 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003764 goto end;
3765 }
3766
3767 if (h2c->mfs && size > h2c->mfs)
3768 size = h2c->mfs;
3769
3770 if (size + 9 > outbuf.size) {
3771 /* we have an opportunity for enlarging the too small
3772 * available space, let's try.
3773 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003774 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003775 goto realign_again;
3776 size = outbuf.size - 9;
3777 }
3778
3779 if (size <= 0) {
3780 h2c->flags |= H2_CF_MUX_MFULL;
3781 h2s->flags |= H2_SF_BLK_MROOM;
3782 goto end;
3783 }
3784
3785 if (size > h2c->mws)
3786 size = h2c->mws;
3787
3788 if (size <= 0) {
3789 h2s->flags |= H2_SF_BLK_MFCTL;
3790 goto end;
3791 }
3792
3793 /* copy whatever we can */
3794 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003795 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003796 if (ret == 1)
3797 len2 = 0;
3798
3799 if (!ret || len1 + len2 < size) {
3800 /* FIXME: must normally never happen */
3801 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3802 goto end;
3803 }
3804
3805 /* limit len1/len2 to size */
3806 if (len1 + len2 > size) {
3807 int sub = len1 + len2 - size;
3808
3809 if (len2 > sub)
3810 len2 -= sub;
3811 else {
3812 sub -= len2;
3813 len2 = 0;
3814 len1 -= sub;
3815 }
3816 }
3817
3818 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003819 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003820 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003821 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003822
3823 send_empty:
3824 /* we may need to add END_STREAM */
3825 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3826 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003827 *
3828 * FIXME: what we do here is not correct because we send end_stream
3829 * before knowing if we'll have to send a HEADERS frame for the
3830 * trailers. More importantly we're not consuming the trailing CRLF
3831 * after the end of trailers, so it will be left to the caller to
3832 * eat it. The right way to do it would be to measure trailers here
3833 * and to send ES only if there are no trailers.
3834 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003835 */
3836 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003837 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003838 es_now = 1;
3839
3840 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003841 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003842
3843 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003844 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003845
3846 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003847 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003848
3849 /* consume incoming H1 response */
3850 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003851 max -= size;
3852 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003853 total += size;
3854 h1m->curr_len -= size;
3855 h2s->mws -= size;
3856 h2c->mws -= size;
3857
3858 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003859 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003860 goto new_frame;
3861 }
3862 }
3863
3864 if (es_now) {
3865 if (h2s->st == H2_SS_OPEN)
3866 h2s->st = H2_SS_HLOC;
3867 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003868 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003869
Willy Tarreau35a62702018-02-27 15:37:25 +01003870 if (!(h1m->flags & H1_MF_CHNK)) {
3871 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003872 total += max;
3873 ofs += max;
3874 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003875
Willy Tarreau801250e2018-09-11 11:45:04 +02003876 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003877 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003878
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003879 h2s->flags |= H2_SF_ES_SENT;
3880 }
3881
3882 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003883 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 +02003884 return total;
3885}
3886
Willy Tarreau115e83b2018-12-01 19:17:53 +01003887/* Try to send a HEADERS frame matching HTX response present in HTX message
3888 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3889 * must check the stream's status to detect any error which might have happened
3890 * subsequently to a successful send. The htx blocks are automatically removed
3891 * from the message. The htx message is assumed to be valid since produced from
3892 * the internal code, hence it contains a start line, an optional series of
3893 * header blocks and an end of header, otherwise an invalid frame could be
3894 * emitted and the resulting htx message could be left in an inconsistent state.
3895 */
3896static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3897{
3898 struct http_hdr list[MAX_HTTP_HDR];
3899 struct h2c *h2c = h2s->h2c;
3900 struct htx_blk *blk;
3901 struct htx_blk *blk_end;
3902 struct buffer outbuf;
3903 struct htx_sl *sl;
3904 enum htx_blk_type type;
3905 int es_now = 0;
3906 int ret = 0;
3907 int hdr;
3908 int idx;
3909
3910 if (h2c_mux_busy(h2c, h2s)) {
3911 h2s->flags |= H2_SF_BLK_MBUSY;
3912 return 0;
3913 }
3914
3915 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3916 h2c->flags |= H2_CF_MUX_MALLOC;
3917 h2s->flags |= H2_SF_BLK_MROOM;
3918 return 0;
3919 }
3920
3921 /* determine the first block which must not be deleted, blk_end may
3922 * be NULL if all blocks have to be deleted.
3923 */
3924 idx = htx_get_head(htx);
3925 blk_end = NULL;
3926 while (idx != -1) {
3927 type = htx_get_blk_type(htx_get_blk(htx, idx));
3928 idx = htx_get_next(htx, idx);
3929 if (type == HTX_BLK_EOH) {
3930 if (idx != -1)
3931 blk_end = htx_get_blk(htx, idx);
3932 break;
3933 }
3934 }
3935
3936 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003937 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003938 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003939 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003940 if (h2s->status < 100 || h2s->status > 999)
3941 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003942
3943 /* and the rest of the headers, that we dump starting at header 0 */
3944 hdr = 0;
3945
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003946 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003947 while ((idx = htx_get_next(htx, idx)) != -1) {
3948 blk = htx_get_blk(htx, idx);
3949 type = htx_get_blk_type(blk);
3950
3951 if (type == HTX_BLK_UNUSED)
3952 continue;
3953
3954 if (type != HTX_BLK_HDR)
3955 break;
3956
3957 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3958 goto fail;
3959
3960 list[hdr].n = htx_get_blk_name(htx, blk);
3961 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003962 hdr++;
3963 }
3964
3965 /* marker for end of headers */
3966 list[hdr].n = ist("");
3967
3968 if (h2s->status == 204 || h2s->status == 304) {
3969 /* no contents, claim c-len is present and set to zero */
3970 es_now = 1;
3971 }
3972
3973 chunk_reset(&outbuf);
3974
3975 while (1) {
3976 outbuf.area = b_tail(&h2c->mbuf);
3977 outbuf.size = b_contig_space(&h2c->mbuf);
3978 outbuf.data = 0;
3979
3980 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3981 break;
3982 realign_again:
3983 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3984 }
3985
3986 if (outbuf.size < 9)
3987 goto full;
3988
3989 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3990 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3991 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3992 outbuf.data = 9;
3993
3994 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003995 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003996 if (b_space_wraps(&h2c->mbuf))
3997 goto realign_again;
3998 goto full;
3999 }
4000
4001 /* encode all headers, stop at empty name */
4002 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4003 /* these ones do not exist in H2 and must be dropped. */
4004 if (isteq(list[hdr].n, ist("connection")) ||
4005 isteq(list[hdr].n, ist("proxy-connection")) ||
4006 isteq(list[hdr].n, ist("keep-alive")) ||
4007 isteq(list[hdr].n, ist("upgrade")) ||
4008 isteq(list[hdr].n, ist("transfer-encoding")))
4009 continue;
4010
4011 if (isteq(list[hdr].n, ist("")))
4012 break; // end
4013
4014 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4015 /* output full */
4016 if (b_space_wraps(&h2c->mbuf))
4017 goto realign_again;
4018 goto full;
4019 }
4020 }
4021
4022 /* we may need to add END_STREAM.
4023 * FIXME: we should also set it when we know for sure that the
4024 * content-length is zero as well as on 204/304
4025 */
4026 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4027 es_now = 1;
4028
4029 if (h2s->cs->flags & CS_FL_SHW)
4030 es_now = 1;
4031
4032 /* update the frame's size */
4033 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4034
4035 if (es_now)
4036 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4037
4038 /* commit the H2 response */
4039 b_add(&h2c->mbuf, outbuf.data);
4040 h2s->flags |= H2_SF_HEADERS_SENT;
4041
4042 /* for now we don't implemented CONTINUATION, so we wait for a
4043 * body or directly end in TRL2.
4044 */
4045 if (es_now) {
4046 h2s->flags |= H2_SF_ES_SENT;
4047 if (h2s->st == H2_SS_OPEN)
4048 h2s->st = H2_SS_HLOC;
4049 else
4050 h2s_close(h2s);
4051 }
4052
4053 /* OK we could properly deliver the response */
4054
4055 /* remove all header blocks including the EOH and compute the
4056 * corresponding size.
4057 *
4058 * FIXME: We should remove everything when es_now is set.
4059 */
4060 ret = 0;
4061 idx = htx_get_head(htx);
4062 blk = htx_get_blk(htx, idx);
4063 while (blk != blk_end) {
4064 ret += htx_get_blksz(blk);
4065 blk = htx_remove_blk(htx, blk);
4066 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004067
4068 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4069 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004070 end:
4071 return ret;
4072 full:
4073 h2c->flags |= H2_CF_MUX_MFULL;
4074 h2s->flags |= H2_SF_BLK_MROOM;
4075 ret = 0;
4076 goto end;
4077 fail:
4078 /* unparsable HTX messages, too large ones to be produced in the local
4079 * list etc go here (unrecoverable errors).
4080 */
4081 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4082 ret = 0;
4083 goto end;
4084}
4085
Willy Tarreau80739692018-10-05 11:35:57 +02004086/* Try to send a HEADERS frame matching HTX request present in HTX message
4087 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4088 * must check the stream's status to detect any error which might have happened
4089 * subsequently to a successful send. The htx blocks are automatically removed
4090 * from the message. The htx message is assumed to be valid since produced from
4091 * the internal code, hence it contains a start line, an optional series of
4092 * header blocks and an end of header, otherwise an invalid frame could be
4093 * emitted and the resulting htx message could be left in an inconsistent state.
4094 */
4095static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4096{
4097 struct http_hdr list[MAX_HTTP_HDR];
4098 struct h2c *h2c = h2s->h2c;
4099 struct htx_blk *blk;
4100 struct htx_blk *blk_end;
4101 struct buffer outbuf;
4102 struct htx_sl *sl;
4103 struct ist meth, path;
4104 enum htx_blk_type type;
4105 int es_now = 0;
4106 int ret = 0;
4107 int hdr;
4108 int idx;
4109
4110 if (h2c_mux_busy(h2c, h2s)) {
4111 h2s->flags |= H2_SF_BLK_MBUSY;
4112 return 0;
4113 }
4114
4115 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4116 h2c->flags |= H2_CF_MUX_MALLOC;
4117 h2s->flags |= H2_SF_BLK_MROOM;
4118 return 0;
4119 }
4120
4121 /* determine the first block which must not be deleted, blk_end may
4122 * be NULL if all blocks have to be deleted.
4123 */
4124 idx = htx_get_head(htx);
4125 blk_end = NULL;
4126 while (idx != -1) {
4127 type = htx_get_blk_type(htx_get_blk(htx, idx));
4128 idx = htx_get_next(htx, idx);
4129 if (type == HTX_BLK_EOH) {
4130 if (idx != -1)
4131 blk_end = htx_get_blk(htx, idx);
4132 break;
4133 }
4134 }
4135
4136 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004137 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004138 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004139 meth = htx_sl_req_meth(sl);
4140 path = htx_sl_req_uri(sl);
4141
4142 /* and the rest of the headers, that we dump starting at header 0 */
4143 hdr = 0;
4144
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004145 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004146 while ((idx = htx_get_next(htx, idx)) != -1) {
4147 blk = htx_get_blk(htx, idx);
4148 type = htx_get_blk_type(blk);
4149
4150 if (type == HTX_BLK_UNUSED)
4151 continue;
4152
4153 if (type != HTX_BLK_HDR)
4154 break;
4155
4156 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4157 goto fail;
4158
4159 list[hdr].n = htx_get_blk_name(htx, blk);
4160 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004161 hdr++;
4162 }
4163
4164 /* marker for end of headers */
4165 list[hdr].n = ist("");
4166
4167 chunk_reset(&outbuf);
4168
4169 while (1) {
4170 outbuf.area = b_tail(&h2c->mbuf);
4171 outbuf.size = b_contig_space(&h2c->mbuf);
4172 outbuf.data = 0;
4173
4174 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4175 break;
4176 realign_again:
4177 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4178 }
4179
4180 if (outbuf.size < 9)
4181 goto full;
4182
4183 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4184 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4185 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4186 outbuf.data = 9;
4187
4188 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004189 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004190 if (b_space_wraps(&h2c->mbuf))
4191 goto realign_again;
4192 goto full;
4193 }
4194
4195 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004196 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4197 /* output full */
4198 if (b_space_wraps(&h2c->mbuf))
4199 goto realign_again;
4200 goto full;
4201 }
Willy Tarreau80739692018-10-05 11:35:57 +02004202
4203 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004204 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004205 /* output full */
4206 if (b_space_wraps(&h2c->mbuf))
4207 goto realign_again;
4208 goto full;
4209 }
4210
4211 /* encode all headers, stop at empty name */
4212 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4213 /* these ones do not exist in H2 and must be dropped. */
4214 if (isteq(list[hdr].n, ist("connection")) ||
4215 isteq(list[hdr].n, ist("proxy-connection")) ||
4216 isteq(list[hdr].n, ist("keep-alive")) ||
4217 isteq(list[hdr].n, ist("upgrade")) ||
4218 isteq(list[hdr].n, ist("transfer-encoding")))
4219 continue;
4220
4221 if (isteq(list[hdr].n, ist("")))
4222 break; // end
4223
4224 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4225 /* output full */
4226 if (b_space_wraps(&h2c->mbuf))
4227 goto realign_again;
4228 goto full;
4229 }
4230 }
4231
4232 /* we may need to add END_STREAM if we have no body :
4233 * - request already closed, or :
4234 * - no transfer-encoding, and :
4235 * - no content-length or content-length:0
4236 * Fixme: this doesn't take into account CONNECT requests.
4237 */
4238 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4239 es_now = 1;
4240
4241 if (sl->flags & HTX_SL_F_BODYLESS)
4242 es_now = 1;
4243
4244 if (h2s->cs->flags & CS_FL_SHW)
4245 es_now = 1;
4246
4247 /* update the frame's size */
4248 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4249
4250 if (es_now)
4251 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4252
4253 /* commit the H2 response */
4254 b_add(&h2c->mbuf, outbuf.data);
4255 h2s->flags |= H2_SF_HEADERS_SENT;
4256 h2s->st = H2_SS_OPEN;
4257
4258 /* for now we don't implemented CONTINUATION, so we wait for a
4259 * body or directly end in TRL2.
4260 */
4261 if (es_now) {
4262 // trim any possibly pending data (eg: inconsistent content-length)
4263 h2s->flags |= H2_SF_ES_SENT;
4264 h2s->st = H2_SS_HLOC;
4265 }
4266
4267 /* remove all header blocks including the EOH and compute the
4268 * corresponding size.
4269 *
4270 * FIXME: We should remove everything when es_now is set.
4271 */
4272 ret = 0;
4273 idx = htx_get_head(htx);
4274 blk = htx_get_blk(htx, idx);
4275 while (blk != blk_end) {
4276 ret += htx_get_blksz(blk);
4277 blk = htx_remove_blk(htx, blk);
4278 }
4279
4280 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4281 htx_remove_blk(htx, blk_end);
4282
4283 end:
4284 return ret;
4285 full:
4286 h2c->flags |= H2_CF_MUX_MFULL;
4287 h2s->flags |= H2_SF_BLK_MROOM;
4288 ret = 0;
4289 goto end;
4290 fail:
4291 /* unparsable HTX messages, too large ones to be produced in the local
4292 * list etc go here (unrecoverable errors).
4293 */
4294 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4295 ret = 0;
4296 goto end;
4297}
4298
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004299/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004300 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4301 * caller must check the stream's status to detect any error which might have
4302 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004303 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4304 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004305static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004306{
4307 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004308 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004309 struct buffer outbuf;
4310 size_t total = 0;
4311 int es_now = 0;
4312 int bsize; /* htx block size */
4313 int fsize; /* h2 frame size */
4314 struct htx_blk *blk;
4315 enum htx_blk_type type;
4316 int idx;
4317
4318 if (h2c_mux_busy(h2c, h2s)) {
4319 h2s->flags |= H2_SF_BLK_MBUSY;
4320 goto end;
4321 }
4322
4323 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4324 h2c->flags |= H2_CF_MUX_MALLOC;
4325 h2s->flags |= H2_SF_BLK_MROOM;
4326 goto end;
4327 }
4328
Willy Tarreau98de12a2018-12-12 07:03:00 +01004329 htx = htx_from_buf(buf);
4330
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004331 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4332 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4333 * the caller to handle.
4334 */
4335
4336 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004337 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004338 goto end;
4339
4340 idx = htx_get_head(htx);
4341 blk = htx_get_blk(htx, idx);
4342 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4343 bsize = htx_get_blksz(blk);
4344 fsize = bsize;
4345
4346 if (type == HTX_BLK_EOD) {
4347 /* if we have an EOD, we're dealing with chunked data. We may
4348 * have a set of trailers after us that the caller will want to
4349 * deal with. Let's simply remove the EOD and return.
4350 */
4351 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004352 total++; // EOD counts as one byte
4353 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004354 goto end;
4355 }
4356
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004357 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4358 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004359
4360 /* Perform some optimizations to reduce the number of buffer copies.
4361 * First, if the mux's buffer is empty and the htx area contains
4362 * exactly one data block of the same size as the requested count, and
4363 * this count fits within the frame size, the stream's window size, and
4364 * the connection's window size, then it's possible to simply swap the
4365 * caller's buffer with the mux's output buffer and adjust offsets and
4366 * length to match the entire DATA HTX block in the middle. In this
4367 * case we perform a true zero-copy operation from end-to-end. This is
4368 * the situation that happens all the time with large files. Second, if
4369 * this is not possible, but the mux's output buffer is empty, we still
4370 * have an opportunity to avoid the copy to the intermediary buffer, by
4371 * making the intermediary buffer's area point to the output buffer's
4372 * area. In this case we want to skip the HTX header to make sure that
4373 * copies remain aligned and that this operation remains possible all
4374 * the time. This goes for headers, data blocks and any data extracted
4375 * from the HTX blocks.
4376 */
4377 if (unlikely(fsize == count &&
4378 htx->used == 1 && type == HTX_BLK_DATA &&
4379 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4380 void *old_area = h2c->mbuf.area;
4381
4382 if (b_data(&h2c->mbuf)) {
4383 /* too bad there are data left there. If we have less
4384 * than 1/4 of the mbuf's size and everything fits,
4385 * we'll perform a copy anyway. Otherwise we'll pretend
4386 * the mbuf is full and wait.
4387 */
4388 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4389 goto copy;
4390 h2c->flags |= H2_CF_MUX_MFULL;
4391 h2s->flags |= H2_SF_BLK_MROOM;
4392 goto end;
4393 }
4394
4395 /* map an H2 frame to the HTX block so that we can put the
4396 * frame header there.
4397 */
4398 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004399 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004400 h2c->mbuf.data = fsize + 9;
4401 outbuf.area = b_head(&h2c->mbuf);
4402
4403 /* prepend an H2 DATA frame header just before the DATA block */
4404 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4405 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4406 h2_set_frame_size(outbuf.area, fsize);
4407
4408 /* update windows */
4409 h2s->mws -= fsize;
4410 h2c->mws -= fsize;
4411
4412 /* and exchange with our old area */
4413 buf->area = old_area;
4414 buf->data = buf->head = 0;
4415 total += fsize;
4416 goto end;
4417 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004418
Willy Tarreau98de12a2018-12-12 07:03:00 +01004419 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004420 /* for DATA and EOM we'll have to emit a frame, even if empty */
4421
4422 while (1) {
4423 outbuf.area = b_tail(&h2c->mbuf);
4424 outbuf.size = b_contig_space(&h2c->mbuf);
4425 outbuf.data = 0;
4426
4427 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4428 break;
4429 realign_again:
4430 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4431 }
4432
4433 if (outbuf.size < 9) {
4434 h2c->flags |= H2_CF_MUX_MFULL;
4435 h2s->flags |= H2_SF_BLK_MROOM;
4436 goto end;
4437 }
4438
4439 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4440 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4441 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4442 outbuf.data = 9;
4443
4444 /* we have in <fsize> the exact number of bytes we need to copy from
4445 * the HTX buffer. We need to check this against the connection's and
4446 * the stream's send windows, and to ensure that this fits in the max
4447 * frame size and in the buffer's available space minus 9 bytes (for
4448 * the frame header). The connection's flow control is applied last so
4449 * that we can use a separate list of streams which are immediately
4450 * unblocked on window opening. Note: we don't implement padding.
4451 */
4452
4453 /* EOM is presented with bsize==1 but would lead to the emission of an
4454 * empty frame, thus we force it to zero here.
4455 */
4456 if (type == HTX_BLK_EOM)
4457 bsize = fsize = 0;
4458
4459 if (!fsize)
4460 goto send_empty;
4461
4462 if (h2s->mws <= 0) {
4463 h2s->flags |= H2_SF_BLK_SFCTL;
4464 if (h2s->send_wait) {
4465 LIST_DEL(&h2s->list);
4466 LIST_INIT(&h2s->list);
4467 }
4468 goto end;
4469 }
4470
Willy Tarreauee573762018-12-04 15:25:57 +01004471 if (fsize > count)
4472 fsize = count;
4473
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004474 if (fsize > h2s->mws)
4475 fsize = h2s->mws; // >0
4476
4477 if (h2c->mfs && fsize > h2c->mfs)
4478 fsize = h2c->mfs; // >0
4479
4480 if (fsize + 9 > outbuf.size) {
4481 /* we have an opportunity for enlarging the too small
4482 * available space, let's try.
4483 * FIXME: is this really interesting to do? Maybe we'll
4484 * spend lots of time realigning instead of using two
4485 * frames.
4486 */
4487 if (b_space_wraps(&h2c->mbuf))
4488 goto realign_again;
4489 fsize = outbuf.size - 9;
4490
4491 if (fsize <= 0) {
4492 /* no need to send an empty frame here */
4493 h2c->flags |= H2_CF_MUX_MFULL;
4494 h2s->flags |= H2_SF_BLK_MROOM;
4495 goto end;
4496 }
4497 }
4498
4499 if (h2c->mws <= 0) {
4500 h2s->flags |= H2_SF_BLK_MFCTL;
4501 goto end;
4502 }
4503
4504 if (fsize > h2c->mws)
4505 fsize = h2c->mws;
4506
4507 /* now let's copy this this into the output buffer */
4508 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004509 h2s->mws -= fsize;
4510 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004511 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004512
4513 send_empty:
4514 /* update the frame's size */
4515 h2_set_frame_size(outbuf.area, fsize);
4516
4517 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4518 * meeting EOM. We should optimize this later.
4519 */
4520 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004521 total++; // EOM counts as one byte
4522 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004523 es_now = 1;
4524 }
4525
4526 if (es_now)
4527 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4528
4529 /* commit the H2 response */
4530 b_add(&h2c->mbuf, fsize + 9);
4531
4532 /* consume incoming HTX block, including EOM */
4533 total += fsize;
4534 if (fsize == bsize) {
4535 htx_remove_blk(htx, blk);
4536 if (fsize)
4537 goto new_frame;
4538 } else {
4539 /* we've truncated this block */
4540 htx_cut_data_blk(htx, blk, fsize);
4541 }
4542
4543 if (es_now) {
4544 if (h2s->st == H2_SS_OPEN)
4545 h2s->st = H2_SS_HLOC;
4546 else
4547 h2s_close(h2s);
4548
4549 h2s->flags |= H2_SF_ES_SENT;
4550 }
4551
4552 end:
4553 return total;
4554}
4555
Olivier Houchard6ff20392018-07-17 18:46:31 +02004556/* Called from the upper layer, to subscribe to events, such as being able to send */
4557static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4558{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004559 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004560 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004561 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004562
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004563 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004564 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004565 if (!(sw->events & SUB_RETRY_RECV)) {
4566 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004567 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004568 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004569 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004570 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004571 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004572 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004573 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004574 if (!(sw->events & SUB_RETRY_SEND)) {
4575 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004576 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004577 h2s->send_wait = sw;
4578 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4579 if (h2s->flags & H2_SF_BLK_MFCTL)
4580 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4581 else
4582 LIST_ADDQ(&h2c->send_list, &h2s->list);
4583 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004584 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004585 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004586 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004587 if (event_type != 0)
4588 return -1;
4589 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004590
4591
4592}
4593
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004594static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4595{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004596 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004597 struct h2s *h2s = cs->ctx;
4598
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004599 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004600 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004601 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004602 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004603 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004604 }
4605 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004606 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004607 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004608 if (h2s->send_wait == sw) {
4609 LIST_DEL(&h2s->list);
4610 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004611 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004612 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004613 }
4614 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004615 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4616 sw = param;
4617 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004618 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004619 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01004620 LIST_DEL(&h2s->list);
4621 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004622 }
4623 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004624 return 0;
4625}
4626
4627
Olivier Houchard511efea2018-08-16 15:30:32 +02004628/* Called from the upper layer, to receive data */
4629static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4630{
Olivier Houchard638b7992018-08-16 15:41:52 +02004631 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004632 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004633 struct htx *h2s_htx = NULL;
4634 struct htx *buf_htx = NULL;
4635 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004636 size_t ret = 0;
4637
4638 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004639 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4640 /* in HTX mode we ignore the count argument */
4641 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004642 if (htx_is_empty(h2s_htx)) {
4643 if (cs->flags & CS_FL_REOS)
4644 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004645 if (cs->flags & CS_FL_ERR_PENDING)
4646 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004647 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004648 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004649
4650 buf_htx = htx_from_buf(buf);
4651 count = htx_free_space(buf_htx);
4652
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004653 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004654
4655 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004656 htx_to_buf(buf_htx, buf);
4657 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004658 ret = htx_ret.ret;
4659 }
4660 else {
4661 ret = b_xfer(buf, &h2s->rxbuf, count);
4662 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004663
Olivier Houchard638b7992018-08-16 15:41:52 +02004664 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004665 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004666 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004667 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004668 if (cs->flags & CS_FL_REOS)
4669 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004670 if (cs->flags & CS_FL_ERR_PENDING)
4671 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004672 if (b_size(&h2s->rxbuf)) {
4673 b_free(&h2s->rxbuf);
4674 offer_buffers(NULL, tasks_run_queue);
4675 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004676 }
4677
Willy Tarreau082f5592018-11-25 08:03:32 +01004678 if (ret && h2c->dsi == h2s->id) {
4679 /* demux is blocking on this stream's buffer */
4680 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau47b515a2018-12-21 16:09:41 +01004681 if (b_data(&h2c->dbuf) || !(h2c->wait_event.events & SUB_RETRY_RECV))
4682 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01004683 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004684end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004685 return ret;
4686}
4687
Olivier Houchardd846c262018-10-19 17:24:29 +02004688static void h2_stop_senders(struct h2c *h2c)
4689{
4690 struct h2s *h2s, *h2s_back;
4691
4692 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4693 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4694 if (h2c->msi == h2s_id(h2s))
4695 continue;
4696 LIST_DEL(&h2s->list);
4697 LIST_INIT(&h2s->list);
4698 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004699 h2s->send_wait->events |= SUB_RETRY_SEND;
4700 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004701 LIST_ADD(&h2c->send_list, &h2s->list);
4702 }
4703}
4704
Willy Tarreau62f52692017-10-08 23:01:42 +02004705/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004706static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004707{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004708 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004709 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004710 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004711 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004712 struct htx *htx;
4713 struct htx_blk *blk;
4714 enum htx_blk_type btype;
4715 uint32_t bsize;
4716 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004717
Olivier Houchardd846c262018-10-19 17:24:29 +02004718 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004719 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004720 h2s->send_wait = NULL;
4721 LIST_DEL(&h2s->list);
4722 LIST_INIT(&h2s->list);
4723 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004724 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4725 return 0;
4726
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004727 /* htx will be enough to decide if we're using HTX or legacy */
4728 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4729
Willy Tarreau0bad0432018-06-14 16:54:01 +02004730 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004731 h2s->flags |= H2_SF_OUTGOING_DATA;
4732
Willy Tarreau751f2d02018-10-05 09:35:00 +02004733 if (h2s->id == 0) {
4734 int32_t id = h2c_get_next_sid(h2s->h2c);
4735
4736 if (id < 0) {
4737 cs->ctx = NULL;
4738 cs->flags |= CS_FL_ERROR;
4739 h2s_destroy(h2s);
4740 return 0;
4741 }
4742
4743 eb32_delete(&h2s->by_id);
4744 h2s->by_id.key = h2s->id = id;
4745 h2s->h2c->max_id = id;
4746 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4747 }
4748
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004749 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004750 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4751 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004752 idx = htx_get_head(htx);
4753 blk = htx_get_blk(htx, idx);
4754 btype = htx_get_blk_type(blk);
4755 bsize = htx_get_blksz(blk);
4756
4757 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004758 case HTX_BLK_REQ_SL:
4759 /* start-line before headers */
4760 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4761 if (ret > 0) {
4762 total += ret;
4763 count -= ret;
4764 if (ret < bsize)
4765 goto done;
4766 }
4767 break;
4768
Willy Tarreau115e83b2018-12-01 19:17:53 +01004769 case HTX_BLK_RES_SL:
4770 /* start-line before headers */
4771 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4772 if (ret > 0) {
4773 total += ret;
4774 count -= ret;
4775 if (ret < bsize)
4776 goto done;
4777 }
4778 break;
4779
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004780 case HTX_BLK_DATA:
4781 case HTX_BLK_EOD:
4782 case HTX_BLK_EOM:
4783 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004784 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004785 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004786 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004787 total += ret;
4788 count -= ret;
4789 if (ret < bsize)
4790 goto done;
4791 }
4792 break;
4793
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004794 default:
4795 htx_remove_blk(htx, blk);
4796 total += bsize;
4797 count -= bsize;
4798 break;
4799 }
4800 }
4801 goto done;
4802 }
4803
4804 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004805 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004806 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004807 if (h2s->h2c->flags & H2_CF_IS_BACK)
4808 ret = -1;
4809 else
4810 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004811 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004812 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004813 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004814 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004815 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004816 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004817 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004818
Willy Tarreau5dd17352018-06-14 13:33:30 +02004819 if (unlikely((int)ret <= 0)) {
4820 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004821 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4822 break;
4823 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004824 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004825 total += count;
4826 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004827 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004828 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004829 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004830 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004831 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004832 break;
4833 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004834
4835 total += ret;
4836 count -= ret;
4837
4838 if (h2s->st >= H2_SS_ERROR)
4839 break;
4840
4841 if (h2s->flags & H2_SF_BLK_ANY)
4842 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004843 }
4844
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004845 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004846 if (h2s->st >= H2_SS_ERROR) {
4847 /* trim any possibly pending data after we close (extra CR-LF,
4848 * unprocessed trailers, abnormal extra data, ...)
4849 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004850 total += count;
4851 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004852 }
4853
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004854 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004855 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004856 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004857 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004858 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004859 }
4860
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004861 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004862 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004863 } else {
4864 b_del(buf, total);
4865 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004866
4867 /* The mux is full, cancel the pending tasks */
4868 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4869 (h2s->flags & H2_SF_BLK_MBUSY))
4870 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004871
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004872 /* If we're running HTX, and we read the whole buffer, then pretend
4873 * we read exactly what the caller specified, as with HTX the caller
4874 * will always give the buffer size, instead of the amount of data
4875 * available.
4876 */
4877 if (htx && !b_data(buf))
4878 total = orig_count;
4879
Olivier Houchard7505f942018-08-21 18:10:44 +02004880 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004881 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004882 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004883
Olivier Houchard7505f942018-08-21 18:10:44 +02004884 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01004885 /* If we're waiting for flow control, and we got a shutr on the
4886 * connection, we will never be unlocked, so add an error on
4887 * the conn_stream.
4888 */
4889 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
4890 !b_data(&h2s->h2c->dbuf) &&
4891 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
4892 if (cs->flags & CS_FL_EOS)
4893 cs->flags |= CS_FL_ERROR;
4894 else
4895 cs->flags |= CS_FL_ERR_PENDING;
4896 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004897 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004898}
4899
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004900/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004901static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004902{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004903 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01004904 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004905 struct eb32_node *node;
4906 int fctl_cnt = 0;
4907 int send_cnt = 0;
4908 int tree_cnt = 0;
4909 int orph_cnt = 0;
4910
4911 if (!h2c)
4912 return;
4913
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004914 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004915 fctl_cnt++;
4916
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004917 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004918 send_cnt++;
4919
Willy Tarreau3af37712018-12-18 14:34:41 +01004920 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004921 node = eb32_first(&h2c->streams_by_id);
4922 while (node) {
4923 h2s = container_of(node, struct h2s, by_id);
4924 tree_cnt++;
4925 if (!h2s->cs)
4926 orph_cnt++;
4927 node = eb32_next(node);
4928 }
4929
Willy Tarreau987c0632018-12-18 10:32:05 +01004930 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
4931 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
4932 " .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 +02004933 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4934 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004935 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01004936 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
4937 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4938 h2c->msi,
4939 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
4940 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
4941
4942 if (h2s) {
4943 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4944 h2s, h2s->id, h2s->flags,
4945 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
4946 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
4947 h2s->cs);
4948 if (h2s->cs)
4949 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4950 h2s->cs->flags, h2s->cs->data);
4951 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004952}
Willy Tarreau62f52692017-10-08 23:01:42 +02004953
4954/*******************************************************/
4955/* functions below are dedicated to the config parsers */
4956/*******************************************************/
4957
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004958/* config parser for global "tune.h2.header-table-size" */
4959static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4960 struct proxy *defpx, const char *file, int line,
4961 char **err)
4962{
4963 if (too_many_args(1, args, err, NULL))
4964 return -1;
4965
4966 h2_settings_header_table_size = atoi(args[1]);
4967 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4968 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4969 return -1;
4970 }
4971 return 0;
4972}
Willy Tarreau62f52692017-10-08 23:01:42 +02004973
Willy Tarreaue6baec02017-07-27 11:45:11 +02004974/* config parser for global "tune.h2.initial-window-size" */
4975static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4976 struct proxy *defpx, const char *file, int line,
4977 char **err)
4978{
4979 if (too_many_args(1, args, err, NULL))
4980 return -1;
4981
4982 h2_settings_initial_window_size = atoi(args[1]);
4983 if (h2_settings_initial_window_size < 0) {
4984 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4985 return -1;
4986 }
4987 return 0;
4988}
4989
Willy Tarreau5242ef82017-07-27 11:47:28 +02004990/* config parser for global "tune.h2.max-concurrent-streams" */
4991static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4992 struct proxy *defpx, const char *file, int line,
4993 char **err)
4994{
4995 if (too_many_args(1, args, err, NULL))
4996 return -1;
4997
4998 h2_settings_max_concurrent_streams = atoi(args[1]);
4999 if (h2_settings_max_concurrent_streams < 0) {
5000 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5001 return -1;
5002 }
5003 return 0;
5004}
5005
Willy Tarreau62f52692017-10-08 23:01:42 +02005006
5007/****************************************/
5008/* MUX initialization and instanciation */
5009/***************************************/
5010
5011/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005012static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005013 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005014 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005015 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005016 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005017 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005018 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005019 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005020 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005021 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005022 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005023 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005024 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005025 .shutr = h2_shutr,
5026 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005027 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005028 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005029 .name = "H2",
5030};
5031
Christopher Faulet32f61c02018-04-10 14:33:41 +02005032/* PROTO selection : this mux registers PROTO token "h2" */
5033static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005034 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005035
Willy Tarreau0108d902018-11-25 19:14:37 +01005036INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5037
Willy Tarreauf8957272018-10-03 10:25:20 +02005038static struct mux_proto_list mux_proto_h2_htx =
5039 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5040
5041INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5042
Willy Tarreau62f52692017-10-08 23:01:42 +02005043/* config keyword parsers */
5044static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005045 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005046 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005047 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005048 { 0, NULL, NULL }
5049}};
5050
Willy Tarreau0108d902018-11-25 19:14:37 +01005051INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);