blob: 03c900da10614111ba86d747413516e0c6f17ac9 [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) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001827 /* RFC7540#4.2 */
1828 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001829 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001830 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001831 }
1832
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001833 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001834 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001835
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001836 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001837 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001838
Willy Tarreauf2101912018-07-19 10:11:38 +02001839 if (h2c->flags & H2_CF_DEM_TOOMANY)
1840 return 0; // too many cs still present
1841
Willy Tarreau13278b42017-10-13 19:23:14 +02001842 /* now either the frame is complete or the buffer is complete */
1843 if (h2s->st != H2_SS_IDLE) {
1844 /* FIXME: stream already exists, this is only allowed for
1845 * trailers (not supported for now).
1846 */
1847 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001848 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001849 goto conn_err;
1850 }
1851 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1852 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1853 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001854 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001855 goto conn_err;
1856 }
1857
Willy Tarreau22de8d32018-09-05 19:55:58 +02001858 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001859 * positively from h2c_frt_stream_new(), the stream will report the error,
1860 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001861 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001862 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001863 if (!h2s) {
1864 error = H2_ERR_INTERNAL_ERROR;
1865 goto conn_err;
1866 }
1867
1868 h2s->st = H2_SS_OPEN;
1869 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1870 h2s->st = H2_SS_HREM;
1871 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001872 /* note: cs cannot be null for now (just created above) */
1873 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001874 }
1875
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001876 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001877 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001878
Willy Tarreau8f650c32017-11-21 19:36:21 +01001879 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001880 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001881
Willy Tarreau721c9742017-11-07 11:05:42 +01001882 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001883 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001884 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001885 }
1886 else {
1887 /* update the max stream ID if the request is being processed */
1888 if (h2s->id > h2c->max_id)
1889 h2c->max_id = h2s->id;
1890 }
1891
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001892 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001893
1894 conn_err:
1895 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001896 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001897
1898 strm_err:
1899 if (h2s) {
1900 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001901 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001902 }
1903 else
1904 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001906}
1907
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001908/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1909 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1910 * errors here are reported as connection errors since it's impossible to
1911 * recover from such errors after the compression context has been altered.
1912 */
1913static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1914{
1915 int error;
1916
1917 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001918 /* RFC7540#4.2 */
1919 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001920 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001921 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001922 }
1923
1924 if (!b_size(&h2c->dbuf))
1925 return NULL; // empty buffer
1926
1927 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1928 return NULL; // incomplete frame
1929
1930 if (h2c->flags & H2_CF_DEM_TOOMANY)
1931 return 0; // too many cs still present
1932
1933 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1934 h2s->flags |= H2_SF_ES_RCVD;
1935 h2s->cs->flags |= CS_FL_REOS;
1936 }
1937
1938 if (!h2s_decode_headers(h2s))
1939 return NULL;
1940
1941 if (h2c->st0 >= H2_CS_ERROR)
1942 return NULL;
1943
1944 if (h2s->st >= H2_SS_ERROR) {
1945 /* stream error : send RST_STREAM */
1946 h2c->st0 = H2_CS_FRAME_E;
1947 }
1948
1949 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1950 h2s->st = H2_SS_ERROR;
1951 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1952 h2s->st = H2_SS_HREM;
1953 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1954 h2s_close(h2s);
1955
1956 return h2s;
1957
1958 conn_err:
1959 h2c_error(h2c, error);
1960 return NULL;
1961
1962 strm_err:
1963 if (h2s) {
1964 h2s_error(h2s, error);
1965 h2c->st0 = H2_CS_FRAME_E;
1966 }
1967 else
1968 h2c_error(h2c, error);
1969 return NULL;
1970}
1971
Willy Tarreau454f9052017-10-26 19:40:35 +02001972/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1973 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1974 */
1975static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1976{
1977 int error;
1978
1979 /* note that empty DATA frames are perfectly valid and sometimes used
1980 * to signal an end of stream (with the ES flag).
1981 */
1982
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001983 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001984 return 0; // empty buffer
1985
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001986 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001987 return 0; // incomplete frame
1988
1989 /* now either the frame is complete or the buffer is complete */
1990
1991 if (!h2c->dsi) {
1992 /* RFC7540#6.1 */
1993 error = H2_ERR_PROTOCOL_ERROR;
1994 goto conn_err;
1995 }
1996
1997 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1998 /* RFC7540#6.1 */
1999 error = H2_ERR_STREAM_CLOSED;
2000 goto strm_err;
2001 }
2002
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002003 if (!h2_frt_transfer_data(h2s))
2004 return 0;
2005
Willy Tarreau454f9052017-10-26 19:40:35 +02002006 /* call the upper layers to process the frame, then let the upper layer
2007 * notify the stream about any change.
2008 */
2009 if (!h2s->cs) {
2010 error = H2_ERR_STREAM_CLOSED;
2011 goto strm_err;
2012 }
2013
Willy Tarreau8f650c32017-11-21 19:36:21 +01002014 if (h2c->st0 >= H2_CS_ERROR)
2015 return 0;
2016
Willy Tarreau721c9742017-11-07 11:05:42 +01002017 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002018 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002019 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002020 }
2021
2022 /* check for completion : the callee will change this to FRAME_A or
2023 * FRAME_H once done.
2024 */
2025 if (h2c->st0 == H2_CS_FRAME_P)
2026 return 0;
2027
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002028
2029 /* last frame */
2030 if (h2c->dff & H2_F_DATA_END_STREAM) {
2031 h2s->st = H2_SS_HREM;
2032 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002033 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002034 }
2035
Willy Tarreau454f9052017-10-26 19:40:35 +02002036 return 1;
2037
2038 conn_err:
2039 h2c_error(h2c, error);
2040 return 0;
2041
2042 strm_err:
2043 if (h2s) {
2044 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002045 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002046 }
2047 else
2048 h2c_error(h2c, error);
2049 return 0;
2050}
2051
Willy Tarreaubc933932017-10-09 16:21:43 +02002052/* process Rx frames to be demultiplexed */
2053static void h2_process_demux(struct h2c *h2c)
2054{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002055 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002056
Willy Tarreau081d4722017-05-16 21:51:05 +02002057 if (h2c->st0 >= H2_CS_ERROR)
2058 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002059
2060 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2061 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002062 if (h2c->flags & H2_CF_IS_BACK)
2063 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002064 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2065 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002066 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002067 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002068 sess_log(h2c->conn->owner);
2069 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002070 goto fail;
2071 }
2072
2073 h2c->max_id = 0;
2074 h2c->st0 = H2_CS_SETTINGS1;
2075 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002076
2077 if (h2c->st0 == H2_CS_SETTINGS1) {
2078 struct h2_fh hdr;
2079
2080 /* ensure that what is pending is a valid SETTINGS frame
2081 * without an ACK.
2082 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002083 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002084 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002085 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002086 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002087 sess_log(h2c->conn->owner);
2088 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002089 goto fail;
2090 }
2091
2092 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2093 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2094 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2095 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002096 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002097 goto fail;
2098 }
2099
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002100 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002101 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2102 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2103 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002104 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002105 goto fail;
2106 }
2107
2108 /* that's OK, switch to FRAME_P to process it */
2109 h2c->dfl = hdr.len;
2110 h2c->dsi = hdr.sid;
2111 h2c->dft = hdr.ft;
2112 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002113 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002114 h2c->st0 = H2_CS_FRAME_P;
2115 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002116 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002117
2118 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002119 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002120 int ret = 0;
2121
2122 if (h2c->st0 >= H2_CS_ERROR)
2123 break;
2124
2125 if (h2c->st0 == H2_CS_FRAME_H) {
2126 struct h2_fh hdr;
2127
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002128 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002129 break;
2130
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002131 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002132 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2133 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002134 if (!h2c->nb_streams) {
2135 /* only log if no other stream can report the error */
2136 sess_log(h2c->conn->owner);
2137 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002138 break;
2139 }
2140
2141 h2c->dfl = hdr.len;
2142 h2c->dsi = hdr.sid;
2143 h2c->dft = hdr.ft;
2144 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002145 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002146 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002147 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002148 }
2149
2150 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002151 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2152
Willy Tarreau567beb82018-12-18 16:52:44 +01002153 if (tmp_h2s != h2s && h2s && h2s->cs &&
2154 (b_data(&h2s->rxbuf) ||
2155 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002156 /* we may have to signal the upper layers */
2157 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002158 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002159 }
2160 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002161
Willy Tarreaud7901432017-12-29 11:34:40 +01002162 if (h2c->st0 == H2_CS_FRAME_E)
2163 goto strm_err;
2164
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002165 if (h2s->st == H2_SS_IDLE &&
2166 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2167 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2168 * this state MUST be treated as a connection error
2169 */
2170 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2171 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002172 if (!h2c->nb_streams) {
2173 /* only log if no other stream can report the error */
2174 sess_log(h2c->conn->owner);
2175 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002176 break;
2177 }
2178
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002179 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2180 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2181 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2182 * this state MUST be treated as a stream error
2183 */
2184 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2185 goto strm_err;
2186 }
2187
Willy Tarreauab837502017-12-27 15:07:30 +01002188 /* Below the management of frames received in closed state is a
2189 * bit hackish because the spec makes strong differences between
2190 * streams closed by receiving RST, sending RST, and seeing ES
2191 * in both directions. In addition to this, the creation of a
2192 * new stream reusing the identifier of a closed one will be
2193 * detected here. Given that we cannot keep track of all closed
2194 * streams forever, we consider that unknown closed streams were
2195 * closed on RST received, which allows us to respond with an
2196 * RST without breaking the connection (eg: to abort a transfer).
2197 * Some frames have to be silently ignored as well.
2198 */
2199 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2200 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2201 /* #5.1.1: The identifier of a newly
2202 * established stream MUST be numerically
2203 * greater than all streams that the initiating
2204 * endpoint has opened or reserved. This
2205 * governs streams that are opened using a
2206 * HEADERS frame and streams that are reserved
2207 * using PUSH_PROMISE. An endpoint that
2208 * receives an unexpected stream identifier
2209 * MUST respond with a connection error.
2210 */
2211 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2212 goto strm_err;
2213 }
2214
2215 if (h2s->flags & H2_SF_RST_RCVD) {
2216 /* RFC7540#5.1:closed: an endpoint that
2217 * receives any frame other than PRIORITY after
2218 * receiving a RST_STREAM MUST treat that as a
2219 * stream error of type STREAM_CLOSED.
2220 *
2221 * Note that old streams fall into this category
2222 * and will lead to an RST being sent.
2223 */
2224 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2225 h2c->st0 = H2_CS_FRAME_E;
2226 goto strm_err;
2227 }
2228
2229 /* RFC7540#5.1:closed: if this state is reached as a
2230 * result of sending a RST_STREAM frame, the peer that
2231 * receives the RST_STREAM might have already sent
2232 * frames on the stream that cannot be withdrawn. An
2233 * endpoint MUST ignore frames that it receives on
2234 * closed streams after it has sent a RST_STREAM
2235 * frame. An endpoint MAY choose to limit the period
2236 * over which it ignores frames and treat frames that
2237 * arrive after this time as being in error.
2238 */
2239 if (!(h2s->flags & H2_SF_RST_SENT)) {
2240 /* RFC7540#5.1:closed: any frame other than
2241 * PRIO/WU/RST in this state MUST be treated as
2242 * a connection error
2243 */
2244 if (h2c->dft != H2_FT_RST_STREAM &&
2245 h2c->dft != H2_FT_PRIORITY &&
2246 h2c->dft != H2_FT_WINDOW_UPDATE) {
2247 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2248 goto strm_err;
2249 }
2250 }
2251 }
2252
Willy Tarreauc0da1962017-10-30 18:38:00 +01002253#if 0
2254 // problem below: it is not possible to completely ignore such
2255 // streams as we need to maintain the compression state as well
2256 // and for this we need to completely process these frames (eg:
2257 // HEADERS frames) as well as counting DATA frames to emit
2258 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2259 // This is a typical case of layer violation where the
2260 // transported contents are critical to the connection's
2261 // validity and must be ignored at the same time :-(
2262
2263 /* graceful shutdown, ignore streams whose ID is higher than
2264 * the one advertised in GOAWAY. RFC7540#6.8.
2265 */
2266 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002267 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2268 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002269 h2c->dfl -= ret;
2270 ret = h2c->dfl == 0;
2271 goto strm_err;
2272 }
2273#endif
2274
Willy Tarreau7e98c052017-10-10 15:56:59 +02002275 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002276 case H2_FT_SETTINGS:
2277 if (h2c->st0 == H2_CS_FRAME_P)
2278 ret = h2c_handle_settings(h2c);
2279
2280 if (h2c->st0 == H2_CS_FRAME_A)
2281 ret = h2c_ack_settings(h2c);
2282 break;
2283
Willy Tarreaucf68c782017-10-10 17:11:41 +02002284 case H2_FT_PING:
2285 if (h2c->st0 == H2_CS_FRAME_P)
2286 ret = h2c_handle_ping(h2c);
2287
2288 if (h2c->st0 == H2_CS_FRAME_A)
2289 ret = h2c_ack_ping(h2c);
2290 break;
2291
Willy Tarreau26f95952017-07-27 17:18:30 +02002292 case H2_FT_WINDOW_UPDATE:
2293 if (h2c->st0 == H2_CS_FRAME_P)
2294 ret = h2c_handle_window_update(h2c, h2s);
2295 break;
2296
Willy Tarreau61290ec2017-10-17 08:19:21 +02002297 case H2_FT_CONTINUATION:
2298 /* we currently don't support CONTINUATION frames since
2299 * we have nowhere to store the partial HEADERS frame.
2300 * Let's abort the stream on an INTERNAL_ERROR here.
2301 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002302 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002303 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002304 h2c->st0 = H2_CS_FRAME_E;
2305 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002306 break;
2307
Willy Tarreau13278b42017-10-13 19:23:14 +02002308 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002309 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002310 if (h2c->flags & H2_CF_IS_BACK)
2311 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2312 else
2313 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002314 if (tmp_h2s) {
2315 h2s = tmp_h2s;
2316 ret = 1;
2317 }
2318 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002319 break;
2320
Willy Tarreau454f9052017-10-26 19:40:35 +02002321 case H2_FT_DATA:
2322 if (h2c->st0 == H2_CS_FRAME_P)
2323 ret = h2c_frt_handle_data(h2c, h2s);
2324
2325 if (h2c->st0 == H2_CS_FRAME_A)
2326 ret = h2c_send_strm_wu(h2c);
2327 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002328
Willy Tarreau92153fc2017-12-03 19:46:19 +01002329 case H2_FT_PRIORITY:
2330 if (h2c->st0 == H2_CS_FRAME_P)
2331 ret = h2c_handle_priority(h2c);
2332 break;
2333
Willy Tarreaucd234e92017-08-18 10:59:39 +02002334 case H2_FT_RST_STREAM:
2335 if (h2c->st0 == H2_CS_FRAME_P)
2336 ret = h2c_handle_rst_stream(h2c, h2s);
2337 break;
2338
Willy Tarreaue96b0922017-10-30 00:28:29 +01002339 case H2_FT_GOAWAY:
2340 if (h2c->st0 == H2_CS_FRAME_P)
2341 ret = h2c_handle_goaway(h2c);
2342 break;
2343
Willy Tarreau1c661982017-10-30 13:52:01 +01002344 case H2_FT_PUSH_PROMISE:
2345 /* not permitted here, RFC7540#5.1 */
2346 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002347 if (!h2c->nb_streams) {
2348 /* only log if no other stream can report the error */
2349 sess_log(h2c->conn->owner);
2350 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002351 break;
2352
2353 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002354 default:
2355 /* drop frames that we ignore. They may be larger than
2356 * the buffer so we drain all of their contents until
2357 * we reach the end.
2358 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002359 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2360 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002361 h2c->dfl -= ret;
2362 ret = h2c->dfl == 0;
2363 }
2364
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002365 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002366 /* We may have to send an RST if not done yet */
2367 if (h2s->st == H2_SS_ERROR)
2368 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002369
Willy Tarreaua20a5192017-12-27 11:02:06 +01002370 if (h2c->st0 == H2_CS_FRAME_E)
2371 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002372
Willy Tarreau7e98c052017-10-10 15:56:59 +02002373 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002374 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002375 break;
2376
2377 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002378 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002379 h2c->st0 = H2_CS_FRAME_H;
2380 }
2381 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002382
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002383 if (h2c->rcvd_c > 0 &&
2384 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2385 h2c_send_conn_wu(h2c);
2386
Willy Tarreau52eed752017-09-22 15:05:09 +02002387 fail:
2388 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002389 if (h2s && h2s->cs &&
2390 (b_data(&h2s->rxbuf) ||
2391 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002392 /* we may have to signal the upper layers */
2393 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002394 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002395 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002396
Willy Tarreau47b515a2018-12-21 16:09:41 +01002397 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002398}
2399
2400/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2401 * the end.
2402 */
2403static int h2_process_mux(struct h2c *h2c)
2404{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002405 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002406
Willy Tarreau01b44822018-10-03 14:26:37 +02002407 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2408 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2409 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2410 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2411 if (h2c->st0 == H2_CS_ERROR) {
2412 h2c->st0 = H2_CS_ERROR2;
2413 sess_log(h2c->conn->owner);
2414 }
2415 goto fail;
2416 }
2417 h2c->st0 = H2_CS_SETTINGS1;
2418 }
2419 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002420 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002421 return 1;
2422 }
2423
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002424 /* start by sending possibly pending window updates */
2425 if (h2c->rcvd_c > 0 &&
2426 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2427 h2c_send_conn_wu(h2c) < 0)
2428 goto fail;
2429
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002430 /* First we always process the flow control list because the streams
2431 * waiting there were already elected for immediate emission but were
2432 * blocked just on this.
2433 */
2434
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002435 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002436 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2437 h2c->st0 >= H2_CS_ERROR)
2438 break;
2439
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002440 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002441 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2442 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002443 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002444 LIST_DEL(&h2s->list);
2445 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002446 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002447 }
2448
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002449 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002450 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2451 break;
2452
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002453 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002454 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2455 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002456 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002457 LIST_DEL(&h2s->list);
2458 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002459 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002460 }
2461
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002462 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002463 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002464 if (h2c->st0 == H2_CS_ERROR) {
2465 if (h2c->max_id >= 0) {
2466 h2c_send_goaway_error(h2c, NULL);
2467 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2468 return 0;
2469 }
2470
2471 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2472 }
2473 return 1;
2474 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002475 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002476}
2477
Willy Tarreau62f52692017-10-08 23:01:42 +02002478
Willy Tarreau479998a2018-11-18 06:30:59 +01002479/* Attempt to read data, and subscribe if none available.
2480 * The function returns 1 if data has been received, otherwise zero.
2481 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002482static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002483{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002484 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002485 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002486 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002487 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002488
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002489 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002490 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002491
Willy Tarreau315d8072017-12-10 22:17:57 +01002492 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002493 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002494
Willy Tarreau44e973f2018-03-01 17:49:30 +01002495 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002496 if (!buf) {
2497 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002498 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002499 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002500
Olivier Houchard7505f942018-08-21 18:10:44 +02002501 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002502 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002503 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2504 /* HTX in use : try to pre-align the buffer like the
2505 * rxbufs will be to optimize memory copies. We'll make
2506 * sure that the frame header lands at the end of the
2507 * HTX block to alias it upon recv. We cannot use the
2508 * head because rcv_buf() will realign the buffer if
2509 * it's empty. Thus we cheat and pretend we already
2510 * have a few bytes there.
2511 */
2512 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002513 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002514 }
2515 else
2516 max = b_room(buf);
2517
Olivier Houchard7505f942018-08-21 18:10:44 +02002518 if (max)
2519 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2520 else
2521 ret = 0;
2522 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002523
Olivier Houchard53216e72018-10-10 15:46:36 +02002524 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002525 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002526
Olivier Houcharda1411e62018-08-17 18:42:48 +02002527 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002528 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002529 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002530 }
2531
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002532 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002533 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002534 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002535}
2536
Willy Tarreau479998a2018-11-18 06:30:59 +01002537/* Try to send data if possible.
2538 * The function returns 1 if data have been sent, otherwise zero.
2539 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002540static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002541{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002542 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002543 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002544 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002545
2546 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002547 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002548
Olivier Houchard7505f942018-08-21 18:10:44 +02002549
Willy Tarreaua2af5122017-10-09 11:56:46 +02002550 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2551 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002552 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002553 }
2554
Willy Tarreaubc933932017-10-09 16:21:43 +02002555 /* This loop is quite simple : it tries to fill as much as it can from
2556 * pending streams into the existing buffer until it's reportedly full
2557 * or the end of send requests is reached. Then it tries to send this
2558 * buffer's contents out, marks it not full if at least one byte could
2559 * be sent, and tries again.
2560 *
2561 * The snd_buf() function normally takes a "flags" argument which may
2562 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2563 * data immediately comes and CO_SFL_STREAMER to indicate that the
2564 * connection is streaming lots of data (used to increase TLS record
2565 * size at the expense of latency). The former can be sent any time
2566 * there's a buffer full flag, as it indicates at least one stream
2567 * attempted to send and failed so there are pending data. An
2568 * alternative would be to set it as long as there's an active stream
2569 * but that would be problematic for ACKs until we have an absolute
2570 * guarantee that all waiters have at least one byte to send. The
2571 * latter should possibly not be set for now.
2572 */
2573
2574 done = 0;
2575 while (!done) {
2576 unsigned int flags = 0;
2577
2578 /* fill as much as we can into the current buffer */
2579 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2580 done = h2_process_mux(h2c);
2581
2582 if (conn->flags & CO_FL_ERROR)
2583 break;
2584
2585 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2586 flags |= CO_SFL_MSG_MORE;
2587
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002588 if (b_data(&h2c->mbuf)) {
2589 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002590 if (!ret)
2591 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002592 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002593 b_del(&h2c->mbuf, ret);
2594 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002595 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002596
2597 /* wrote at least one byte, the buffer is not full anymore */
2598 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2599 }
2600
Willy Tarreaua2af5122017-10-09 11:56:46 +02002601 if (conn->flags & CO_FL_SOCK_WR_SH) {
2602 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002603 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002604 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002605 /* We're not full anymore, so we can wake any task that are waiting
2606 * for us.
2607 */
2608 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002609 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002610 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2611 struct h2s *, list);
2612 LIST_DEL(&h2s->list);
2613 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002614 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002615 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2616 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002617 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002618 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002619 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002620 /* We're done, no more to send */
2621 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002622 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002623schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002624 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2625 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002626 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002627}
2628
2629static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2630{
2631 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002632 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002633
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002634 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002635 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002636 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002637 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002638 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002639 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002640 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002641}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002642
Willy Tarreau62f52692017-10-08 23:01:42 +02002643/* callback called on any event by the connection handler.
2644 * It applies changes and returns zero, or < 0 if it wants immediate
2645 * destruction of the connection (which normally doesn not happen in h2).
2646 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002647static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002648{
Olivier Houchard7505f942018-08-21 18:10:44 +02002649 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002650
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002651 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002652 h2_process_demux(h2c);
2653
2654 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002656
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002657 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002658 h2c->flags &= ~H2_CF_DEM_DFULL;
2659 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002660 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002661
Willy Tarreau0b37d652018-10-03 10:33:02 +02002662 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002663 /* frontend is stopping, reload likely in progress, let's try
2664 * to announce a graceful shutdown if not yet done. We don't
2665 * care if it fails, it will be tried again later.
2666 */
2667 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2668 if (h2c->last_sid < 0)
2669 h2c->last_sid = (1U << 31) - 1;
2670 h2c_send_goaway_error(h2c, NULL);
2671 }
2672 }
2673
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002674 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002675 * If we received early data, and the handshake is done, wake
2676 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002677 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002678 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2679 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2680 struct eb32_node *node;
2681 struct h2s *h2s;
2682
2683 h2c->flags |= H2_CF_WAIT_FOR_HS;
2684 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2685
2686 while (node) {
2687 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002688 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002689 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002690 node = eb32_next(node);
2691 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002692 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002693
Willy Tarreau26bd7612017-10-09 16:47:04 +02002694 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002695 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2696 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2697 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002698 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002699
2700 if (eb_is_empty(&h2c->streams_by_id)) {
2701 /* no more stream, kill the connection now */
2702 h2_release(conn);
2703 return -1;
2704 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002705 }
2706
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002707 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002708 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002709
Olivier Houchard53216e72018-10-10 15:46:36 +02002710 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2711 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2712 (h2c->st0 != H2_CS_ERROR &&
2713 !b_data(&h2c->mbuf) &&
2714 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2715 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002716 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002717
Willy Tarreau3f133572017-10-31 19:21:06 +01002718 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002719 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002720 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002721 task_queue(h2c->task);
2722 }
2723 else
2724 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002725 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002726
Olivier Houchard7505f942018-08-21 18:10:44 +02002727 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002728 return 0;
2729}
2730
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002731static int h2_wake(struct connection *conn)
2732{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002733 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002734
2735 return (h2_process(h2c));
2736}
2737
Willy Tarreauea392822017-10-31 10:02:25 +01002738/* Connection timeout management. The principle is that if there's no receipt
2739 * nor sending for a certain amount of time, the connection is closed. If the
2740 * MUX buffer still has lying data or is not allocatable, the connection is
2741 * immediately killed. If it's allocatable and empty, we attempt to send a
2742 * GOAWAY frame.
2743 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002744static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002745{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002746 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002747 int expired = tick_is_expired(t->expire, now_ms);
2748
Willy Tarreau0975f112018-03-29 15:22:59 +02002749 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002750 return t;
2751
Willy Tarreau0975f112018-03-29 15:22:59 +02002752 task_delete(t);
2753 task_free(t);
2754
2755 if (!h2c) {
2756 /* resources were already deleted */
2757 return NULL;
2758 }
2759
2760 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002761 h2c_error(h2c, H2_ERR_NO_ERROR);
2762 h2_wake_some_streams(h2c, 0, 0);
2763
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002764 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002765 /* don't even try to send a GOAWAY, the buffer is stuck */
2766 h2c->flags |= H2_CF_GOAWAY_FAILED;
2767 }
2768
2769 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002770 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002771 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2772 h2c->flags |= H2_CF_GOAWAY_FAILED;
2773
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002774 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2775 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002776 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002777 b_del(&h2c->mbuf, ret);
2778 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002779 }
2780 }
Willy Tarreauea392822017-10-31 10:02:25 +01002781
Willy Tarreau0975f112018-03-29 15:22:59 +02002782 /* either we can release everything now or it will be done later once
2783 * the last stream closes.
2784 */
2785 if (eb_is_empty(&h2c->streams_by_id))
2786 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002787
Willy Tarreauea392822017-10-31 10:02:25 +01002788 return NULL;
2789}
2790
2791
Willy Tarreau62f52692017-10-08 23:01:42 +02002792/*******************************************/
2793/* functions below are used by the streams */
2794/*******************************************/
2795
2796/*
2797 * Attach a new stream to a connection
2798 * (Used for outgoing connections)
2799 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002800static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002801{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002802 struct conn_stream *cs;
2803 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002804 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002805
2806 cs = cs_new(conn);
2807 if (!cs)
2808 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002809 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002810 if (!h2s) {
2811 cs_free(cs);
2812 return NULL;
2813 }
2814 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002815}
2816
Willy Tarreaufafd3982018-11-18 21:29:20 +01002817/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2818 * We have to scan because we may have some orphan streams. It might be
2819 * beneficial to scan backwards from the end to reduce the likeliness to find
2820 * orphans.
2821 */
2822static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2823{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002824 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002825 struct h2s *h2s;
2826 struct eb32_node *node;
2827
2828 node = eb32_first(&h2c->streams_by_id);
2829 while (node) {
2830 h2s = container_of(node, struct h2s, by_id);
2831 if (h2s->cs)
2832 return h2s->cs;
2833 node = eb32_next(node);
2834 }
2835 return NULL;
2836}
2837
Willy Tarreau62f52692017-10-08 23:01:42 +02002838/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002839 * Destroy the mux and the associated connection, if it is no longer used
2840 */
2841static void h2_destroy(struct connection *conn)
2842{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002843 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002844
2845 if (eb_is_empty(&h2c->streams_by_id))
2846 h2_release(h2c->conn);
2847}
2848
2849/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002850 * Detach the stream from the connection and possibly release the connection.
2851 */
2852static void h2_detach(struct conn_stream *cs)
2853{
Willy Tarreau60935142017-10-16 18:11:19 +02002854 struct h2s *h2s = cs->ctx;
2855 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002856 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002857
2858 cs->ctx = NULL;
2859 if (!h2s)
2860 return;
2861
Olivier Houchardf502aca2018-12-14 19:42:40 +01002862 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002863 h2c = h2s->h2c;
2864 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002865 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002866 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2867 !h2_has_too_many_cs(h2c)) {
2868 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002869 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002870 }
Willy Tarreau60935142017-10-16 18:11:19 +02002871
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002872 /* this stream may be blocked waiting for some data to leave (possibly
2873 * an ES or RST frame), so orphan it in this case.
2874 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002875 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002876 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002877 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002878 return;
2879
Willy Tarreau45f752e2017-10-30 15:44:59 +01002880 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2881 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2882 /* unblock the connection if it was blocked on this
2883 * stream.
2884 */
2885 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2886 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002887 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002888 }
2889
Willy Tarreau71049cc2018-03-28 13:56:39 +02002890 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002891
Olivier Houchard8a786902018-12-15 16:05:40 +01002892 if (h2c->flags & H2_CF_IS_BACK &&
2893 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002894 if (!(h2c->conn->flags &
2895 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2896 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002897 h2c->conn->owner = sess;
2898 session_add_conn(sess, h2c->conn, h2c->conn->target);
Olivier Houchard8a786902018-12-15 16:05:40 +01002899 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002900 if (eb_is_empty(&h2c->streams_by_id)) {
2901 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2902 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2903 return;
2904 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002905 /* Never ever allow to reuse a connection from a non-reuse backend */
2906 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2907 h2c->conn->flags |= CO_FL_PRIVATE;
2908 if (LIST_ISEMPTY(&h2c->conn->list)) {
2909 struct server *srv = objt_server(h2c->conn->target);
2910
2911 if (srv) {
2912 if (h2c->conn->flags & CO_FL_PRIVATE)
2913 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2914 else
2915 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2916 }
2917
2918 }
2919 }
2920 }
2921
Willy Tarreaue323f342018-03-28 13:51:45 +02002922 /* We don't want to close right now unless we're removing the
2923 * last stream, and either the connection is in error, or it
2924 * reached the ID already specified in a GOAWAY frame received
2925 * or sent (as seen by last_sid >= 0).
2926 */
2927 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2928 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002929 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002930 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002931 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002932 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002933 (conn_xprt_read0_pending(h2c->conn) ||
2934 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2935 /* no more stream will come, kill it now */
2936 h2_release(h2c->conn);
2937 }
2938 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002940 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2941 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002942 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002943 else
2944 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002945 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002946}
2947
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002948static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002949{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002950 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002951 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002952
Willy Tarreau721c9742017-11-07 11:05:42 +01002953 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002954 return;
2955
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002956 /* if no outgoing data was seen on this stream, it means it was
2957 * closed with a "tcp-request content" rule that is normally
2958 * used to kill the connection ASAP (eg: limit abuse). In this
2959 * case we send a goaway to close the connection.
2960 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002961 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002962 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002963 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002964
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002965 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2966 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002967 h2c_send_goaway_error(h2c, h2s) <= 0)
2968 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002969
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002970 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002971 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002972 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002973
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002974 return;
2975add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002976 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002977 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002978 if (h2s->flags & H2_SF_BLK_MFCTL) {
2979 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2980 h2s->send_wait = sw;
2981 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2982 h2s->send_wait = sw;
2983 LIST_ADDQ(&h2c->send_list, &h2s->list);
2984 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002985 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002986 /* Let the handler know we want shutr */
2987 sw->handle = (void *)((long)sw->handle | 1);
2988
Willy Tarreau62f52692017-10-08 23:01:42 +02002989}
2990
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002991static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002992{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002993 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002994 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002995
Willy Tarreau721c9742017-11-07 11:05:42 +01002996 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002997 return;
2998
Willy Tarreau67434202017-11-06 20:20:51 +01002999 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003000 /* we can cleanly close using an empty data frame only after headers */
3001
3002 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3003 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003004 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003005
3006 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003007 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003008 else
3009 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003010 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003011 /* if no outgoing data was seen on this stream, it means it was
3012 * closed with a "tcp-request content" rule that is normally
3013 * used to kill the connection ASAP (eg: limit abuse). In this
3014 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003015 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003016 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003017 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003018 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003019
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003020 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3021 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003022 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003023 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003024
Willy Tarreau00dd0782018-03-01 16:31:34 +01003025 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003026 }
3027
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003028 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003029 tasklet_wakeup(h2c->wait_event.task);
3030 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003031
3032 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003033 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003034 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003035 if (h2s->flags & H2_SF_BLK_MFCTL) {
3036 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3037 h2s->send_wait = sw;
3038 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3039 h2s->send_wait = sw;
3040 LIST_ADDQ(&h2c->send_list, &h2s->list);
3041 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003042 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003043 /* let the handler know we want to shutw */
3044 sw->handle = (void *)((long)(sw->handle) | 2);
3045
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003046}
3047
3048static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3049{
3050 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003051 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003052
Olivier Houchard2c68a462018-12-15 22:42:20 +01003053 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003054 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003055 h2s->send_wait = NULL;
3056 LIST_DEL(&h2s->list);
3057 LIST_INIT(&h2s->list);
3058 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003059 if (reason & 2)
3060 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003061 if (reason & 1)
3062 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003063
Olivier Houchard2c68a462018-12-15 22:42:20 +01003064 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003065 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003066 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003067 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003068}
3069
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003070static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3071{
3072 struct h2s *h2s = cs->ctx;
3073
3074 if (!mode)
3075 return;
3076
3077 h2_do_shutr(h2s);
3078}
3079
3080static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3081{
3082 struct h2s *h2s = cs->ctx;
3083
3084 h2_do_shutw(h2s);
3085}
3086
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003087/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003088 * HTX request or response depending on the connection's side. Returns the
3089 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3090 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003091 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003092static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003093{
3094 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003095 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003096 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003097 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003098 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003099 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003100 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003101 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003102 int flen = h2c->dfl;
3103 int outlen = 0;
3104 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003105 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003106
3107 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01003108 /* RFC7540#4.2 */
3109 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); // empty headers frame!
3110 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003111 }
3112
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003113 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003114 return 0; // incomplete input frame
3115
Willy Tarreau13278b42017-10-13 19:23:14 +02003116 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003117 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003118 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003119 copy = alloc_trash_chunk();
3120 if (!copy) {
3121 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3122 goto fail;
3123 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003124 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3125 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3126 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003127 }
3128
3129 /* The padlen is the first byte before data, and the padding appears
3130 * after data. padlen+data+padding are included in flen.
3131 */
3132 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003133 h2c->dpl = *hdrs;
3134 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003135 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3136 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003137 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003138 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003139 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003140 hdrs += 1; // skip Pad Length
3141 }
3142
3143 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3144 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003145 if (read_n32(hdrs) == h2s->id) {
3146 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3147 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003148 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003149 }
3150
Willy Tarreau13278b42017-10-13 19:23:14 +02003151 hdrs += 5; // stream dep = 4, weight = 1
3152 flen -= 5;
3153 }
3154
3155 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3156 * don't support this for now and can't even decompress so we have to
3157 * break the connection.
3158 */
3159 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3160 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003161 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003162 }
3163
Olivier Houchard638b7992018-08-16 15:41:52 +02003164 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003165 if (!csbuf) {
3166 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003167 goto fail;
3168 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003169
Willy Tarreau937f7602018-02-26 15:22:17 +01003170 /* we can't retry a failed decompression operation so we must be very
3171 * careful not to take any risks. In practice the output buffer is
3172 * always empty except maybe for trailers, in which case we simply have
3173 * to wait for the upper layer to finish consuming what is available.
3174 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003175
3176 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3177 htx = htx_from_buf(&h2s->rxbuf);
3178 if (!htx_is_empty(htx))
3179 goto fail;
3180 } else {
3181 if (b_data(csbuf))
3182 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003183
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003184 csbuf->head = 0;
3185 try = b_size(csbuf);
3186 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003187
3188 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3189 sizeof(list)/sizeof(list[0]), tmp);
3190 if (outlen < 0) {
3191 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3192 goto fail;
3193 }
3194
3195 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003196 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003197
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003198 if (htx) {
3199 /* HTX mode */
3200 if (h2c->flags & H2_CF_IS_BACK)
3201 outlen = h2_make_htx_response(list, htx, &msgf);
3202 else
3203 outlen = h2_make_htx_request(list, htx, &msgf);
3204 } else {
3205 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003206 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003207 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003208
3209 if (outlen < 0) {
3210 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3211 goto fail;
3212 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003213
Willy Tarreau174b06a2018-04-25 18:13:58 +02003214 if (msgf & H2_MSGF_BODY) {
3215 /* a payload is present */
3216 if (msgf & H2_MSGF_BODY_CL)
3217 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003218 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003219 h2s->flags |= H2_SF_DATA_CHNK;
3220 }
3221
Willy Tarreau13278b42017-10-13 19:23:14 +02003222 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003223 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003224 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003225 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003226
Willy Tarreau39d68502018-03-02 12:26:37 +01003227 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003228 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003229 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003230 if (htx)
3231 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003232 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003233
Willy Tarreau68dd9852017-07-03 14:44:26 +02003234 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003235 if (htx)
3236 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003237 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003238 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003239 fail:
3240 outlen = 0;
3241 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003242}
3243
Willy Tarreau454f9052017-10-26 19:40:35 +02003244/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3245 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3246 * in use, a new chunk is emitted for each frame. This is supposed to fit
3247 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3248 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3249 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003250 * parser state is automatically updated. Returns > 0 if it could completely
3251 * send the current frame, 0 if it couldn't complete, in which case
3252 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3253 * DATA frame can return 0 as a valid result). Stream errors are reported in
3254 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3255 * have checked the frame header and ensured that the frame was complete or the
3256 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003257 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003258static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003259{
3260 struct h2c *h2c = h2s->h2c;
3261 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003262 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003263 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003264 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003265 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003266
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003267 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003268
3269 /* The padlen is the first byte before data, and the padding appears
3270 * after data. padlen+data+padding are included in flen.
3271 */
Willy Tarreau79127812017-12-03 21:06:59 +01003272 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003273 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003274 return 0;
3275
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003276 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003277 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003278 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3279 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003280 return 0;
3281 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003282
3283 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003284 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003285 h2c->dfl--;
3286 h2c->rcvd_c++; h2c->rcvd_s++;
3287 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003288 }
3289
Olivier Houchard638b7992018-08-16 15:41:52 +02003290 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003291 if (!csbuf) {
3292 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003293 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003294 }
3295
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003296try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003297 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003298 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3299 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003300 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003301 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003302
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003303 if (flen > b_data(&h2c->dbuf)) {
3304 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003305 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003306 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003307 }
3308
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003309 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003310 block1 = htx_free_data_space(htx);
3311 if (!block1) {
3312 h2c->flags |= H2_CF_DEM_SFULL;
3313 goto fail;
3314 }
3315 if (flen > block1)
3316 flen = block1;
3317
3318 /* here, flen is the max we can copy into the output buffer */
3319 block1 = b_contig_data(&h2c->dbuf, 0);
3320 if (flen > block1)
3321 flen = block1;
3322
3323 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3324 h2c->flags |= H2_CF_DEM_SFULL;
3325 goto fail;
3326 }
3327
3328 b_del(&h2c->dbuf, flen);
3329 h2c->dfl -= flen;
3330 h2c->rcvd_c += flen;
3331 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3332 goto try_again;
3333 }
3334 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003335 /* it doesn't fit and the buffer is fragmented,
3336 * so let's defragment it and try again.
3337 */
3338 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003339 }
3340
Willy Tarreaueba10f22018-04-25 20:44:22 +02003341 /* chunked-encoding requires more room */
3342 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003343 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003344 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3345 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3346 (chklen < 1048576) ? 4 : 8;
3347 chklen += 4; // CRLF, CRLF
3348 }
3349
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003350 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003351 if (flen + chklen > b_room(csbuf)) {
3352 if (chklen >= b_room(csbuf)) {
3353 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003354 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003355 }
3356 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003357 }
3358
3359 if (h2s->flags & H2_SF_DATA_CHNK) {
3360 /* emit the chunk size */
3361 unsigned int chksz = flen;
3362 char str[10];
3363 char *beg;
3364
3365 beg = str + sizeof(str);
3366 *--beg = '\n';
3367 *--beg = '\r';
3368 do {
3369 *--beg = hextab[chksz & 0xF];
3370 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003371 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003372 }
3373
Willy Tarreau454f9052017-10-26 19:40:35 +02003374 /* Block1 is the length of the first block before the buffer wraps,
3375 * block2 is the optional second block to reach the end of the frame.
3376 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003377 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003378 if (block1 > flen)
3379 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003380 block2 = flen - block1;
3381
3382 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003383 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003384
3385 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003386 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003387
Willy Tarreaueba10f22018-04-25 20:44:22 +02003388 if (h2s->flags & H2_SF_DATA_CHNK) {
3389 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003390 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003391 }
3392
Willy Tarreau454f9052017-10-26 19:40:35 +02003393 /* now mark the input data as consumed (will be deleted from the buffer
3394 * by the caller when seeing FRAME_A after sending the window update).
3395 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003396 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003397 h2c->dfl -= flen;
3398 h2c->rcvd_c += flen;
3399 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3400
3401 if (h2c->dfl > h2c->dpl) {
3402 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003403 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003404 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003405 }
3406
Willy Tarreau4a28da12018-01-04 14:41:00 +01003407 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003408 /* here we're done with the frame, all the payload (except padding) was
3409 * transferred.
3410 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003411
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003412 if (h2c->dff & H2_F_DATA_END_STREAM) {
3413 if (htx) {
3414 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3415 h2c->flags |= H2_CF_DEM_SFULL;
3416 goto fail;
3417 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003418 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003419 else if (h2s->flags & H2_SF_DATA_CHNK) {
3420 /* emit the trailing 0 CRLF CRLF */
3421 if (b_room(csbuf) < 5) {
3422 h2c->flags |= H2_CF_DEM_SFULL;
3423 goto fail;
3424 }
3425 chklen += 5;
3426 b_putblk(csbuf, "0\r\n\r\n", 5);
3427 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003428 }
3429
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003430 h2c->rcvd_c += h2c->dpl;
3431 h2c->rcvd_s += h2c->dpl;
3432 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003433 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3434
Willy Tarreau39d68502018-03-02 12:26:37 +01003435 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003436 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003437 h2s->cs->flags |= CS_FL_REOS;
3438 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003439 if (htx)
3440 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003441 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003442 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003443 if (htx)
3444 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003445 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003446}
3447
Willy Tarreau5dd17352018-06-14 13:33:30 +02003448/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3449 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3450 * number of bytes sent. The caller must check the stream's status to detect
3451 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003452 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003453static 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 +02003454{
3455 struct http_hdr list[MAX_HTTP_HDR];
3456 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003457 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003458 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003459 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003460 int es_now = 0;
3461 int ret = 0;
3462 int hdr;
3463
3464 if (h2c_mux_busy(h2c, h2s)) {
3465 h2s->flags |= H2_SF_BLK_MBUSY;
3466 return 0;
3467 }
3468
Willy Tarreau44e973f2018-03-01 17:49:30 +01003469 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003470 h2c->flags |= H2_CF_MUX_MALLOC;
3471 h2s->flags |= H2_SF_BLK_MROOM;
3472 return 0;
3473 }
3474
3475 /* First, try to parse the H1 response and index it into <list>.
3476 * NOTE! Since it comes from haproxy, we *know* that a response header
3477 * block does not wrap and we can safely read it this way without
3478 * having to realign the buffer.
3479 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003480 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003481 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003482 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003483 /* incomplete or invalid response, this is abnormal coming from
3484 * haproxy and may only result in a bad errorfile or bad Lua code
3485 * so that won't be fixed, raise an error now.
3486 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003487 * FIXME: we should instead add the ability to only return a
3488 * 502 bad gateway. But in theory this is not supposed to
3489 * happen.
3490 */
3491 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3492 ret = 0;
3493 goto end;
3494 }
3495
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003496 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003497
3498 /* certain statuses have no body or an empty one, regardless of
3499 * what the headers say.
3500 */
3501 if (sl.st.status >= 100 && sl.st.status < 200) {
3502 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3503 h1m->curr_len = h1m->body_len = 0;
3504 }
3505 else if (sl.st.status == 204 || sl.st.status == 304) {
3506 /* no contents, claim c-len is present and set to zero */
3507 h1m->flags &= ~H1_MF_CHNK;
3508 h1m->flags |= H1_MF_CLEN;
3509 h1m->curr_len = h1m->body_len = 0;
3510 }
3511
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003512 chunk_reset(&outbuf);
3513
3514 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003515 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003516 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003517 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003518
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003519 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003520 break;
3521 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003522 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003523 }
3524
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003525 if (outbuf.size < 9)
3526 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003527
3528 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003529 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3530 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3531 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003532
3533 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003534 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003535 /* this is an unparsable response */
3536 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3537 ret = 0;
3538 goto end;
3539 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003540
3541 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003542 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003543 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003544 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003545 }
3546
3547 /* encode all headers, stop at empty name */
3548 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003549 /* these ones do not exist in H2 and must be dropped. */
3550 if (isteq(list[hdr].n, ist("connection")) ||
3551 isteq(list[hdr].n, ist("proxy-connection")) ||
3552 isteq(list[hdr].n, ist("keep-alive")) ||
3553 isteq(list[hdr].n, ist("upgrade")) ||
3554 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003555 continue;
3556
3557 if (isteq(list[hdr].n, ist("")))
3558 break; // end
3559
3560 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3561 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003562 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003563 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003564 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003565 }
3566 }
3567
3568 /* we may need to add END_STREAM */
3569 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3570 es_now = 1;
3571
3572 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003573 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003574
3575 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003576 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003577
3578 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003579 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003580
3581 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003582 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003583 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003584
3585 /* for now we don't implemented CONTINUATION, so we wait for a
3586 * body or directly end in TRL2.
3587 */
3588 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003589 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003590 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003591
Willy Tarreau801250e2018-09-11 11:45:04 +02003592 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003593 h2s->flags |= H2_SF_ES_SENT;
3594 if (h2s->st == H2_SS_OPEN)
3595 h2s->st = H2_SS_HLOC;
3596 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003597 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003598 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003599 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003600 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003601 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003602 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003603 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003604 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003605 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003606
3607 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003608
3609 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003610 //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 +02003611 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003612 full:
3613 h1m_init_res(h1m);
3614 h1m->err_pos = -1; // don't care about errors on the response path
3615 h2c->flags |= H2_CF_MUX_MFULL;
3616 h2s->flags |= H2_SF_BLK_MROOM;
3617 ret = 0;
3618 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003619}
3620
Willy Tarreau5dd17352018-06-14 13:33:30 +02003621/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3622 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3623 * the number of bytes sent. The caller must check the stream's status to
3624 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003625 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003626static 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 +02003627{
3628 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003629 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003630 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003631 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003632 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003633 int es_now = 0;
3634 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003635 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003636 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003637
3638 if (h2c_mux_busy(h2c, h2s)) {
3639 h2s->flags |= H2_SF_BLK_MBUSY;
3640 goto end;
3641 }
3642
Willy Tarreau44e973f2018-03-01 17:49:30 +01003643 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003644 h2c->flags |= H2_CF_MUX_MALLOC;
3645 h2s->flags |= H2_SF_BLK_MROOM;
3646 goto end;
3647 }
3648
3649 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003650 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003651 goto end;
3652
3653 chunk_reset(&outbuf);
3654
3655 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003656 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003657 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003658 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003659
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003660 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003661 break;
3662 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003663 /* If there are pending data in the output buffer, and we have
3664 * less than 1/4 of the mbuf's size and everything fits, we'll
3665 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3666 * is full and wait, to save some slow realign calls.
3667 */
3668 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3669 h2c->flags |= H2_CF_MUX_MFULL;
3670 h2s->flags |= H2_SF_BLK_MROOM;
3671 goto end;
3672 }
3673
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003674 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003675 }
3676
3677 if (outbuf.size < 9) {
3678 h2c->flags |= H2_CF_MUX_MFULL;
3679 h2s->flags |= H2_SF_BLK_MROOM;
3680 goto end;
3681 }
3682
3683 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003684 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3685 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3686 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003687
3688 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3689 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003690 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003691 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003692 break;
3693 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003694 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003695 if ((long long)size > h1m->curr_len)
3696 size = h1m->curr_len;
3697 break;
3698 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003699 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003700 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003701 if (!ret)
3702 goto end;
3703
3704 if (ret < 0) {
3705 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003706 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003707 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3708 goto end;
3709 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003710 max -= ret;
3711 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003712 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003713 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003714 }
3715
Willy Tarreau801250e2018-09-11 11:45:04 +02003716 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003717 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003718 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003719 if (!ret)
3720 goto end;
3721
3722 if (ret < 0) {
3723 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003724 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003725 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3726 goto end;
3727 }
3728
3729 size = chunk;
3730 h1m->curr_len = chunk;
3731 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003732 max -= ret;
3733 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003734 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003735 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003736 if (!size)
3737 goto send_empty;
3738 }
3739
3740 /* in MSG_DATA state, continue below */
3741 size = h1m->curr_len;
3742 break;
3743 }
3744
3745 /* we have in <size> the exact number of bytes we need to copy from
3746 * the H1 buffer. We need to check this against the connection's and
3747 * the stream's send windows, and to ensure that this fits in the max
3748 * frame size and in the buffer's available space minus 9 bytes (for
3749 * the frame header). The connection's flow control is applied last so
3750 * that we can use a separate list of streams which are immediately
3751 * unblocked on window opening. Note: we don't implement padding.
3752 */
3753
Willy Tarreau5dd17352018-06-14 13:33:30 +02003754 if (size > max)
3755 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003756
3757 if (size > h2s->mws)
3758 size = h2s->mws;
3759
3760 if (size <= 0) {
3761 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003762 if (h2s->send_wait) {
3763 LIST_DEL(&h2s->list);
3764 LIST_INIT(&h2s->list);
3765 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003766 goto end;
3767 }
3768
3769 if (h2c->mfs && size > h2c->mfs)
3770 size = h2c->mfs;
3771
3772 if (size + 9 > outbuf.size) {
3773 /* we have an opportunity for enlarging the too small
3774 * available space, let's try.
3775 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003776 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003777 goto realign_again;
3778 size = outbuf.size - 9;
3779 }
3780
3781 if (size <= 0) {
3782 h2c->flags |= H2_CF_MUX_MFULL;
3783 h2s->flags |= H2_SF_BLK_MROOM;
3784 goto end;
3785 }
3786
3787 if (size > h2c->mws)
3788 size = h2c->mws;
3789
3790 if (size <= 0) {
3791 h2s->flags |= H2_SF_BLK_MFCTL;
3792 goto end;
3793 }
3794
3795 /* copy whatever we can */
3796 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003797 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003798 if (ret == 1)
3799 len2 = 0;
3800
3801 if (!ret || len1 + len2 < size) {
3802 /* FIXME: must normally never happen */
3803 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3804 goto end;
3805 }
3806
3807 /* limit len1/len2 to size */
3808 if (len1 + len2 > size) {
3809 int sub = len1 + len2 - size;
3810
3811 if (len2 > sub)
3812 len2 -= sub;
3813 else {
3814 sub -= len2;
3815 len2 = 0;
3816 len1 -= sub;
3817 }
3818 }
3819
3820 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003821 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003822 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003823 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003824
3825 send_empty:
3826 /* we may need to add END_STREAM */
3827 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3828 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003829 *
3830 * FIXME: what we do here is not correct because we send end_stream
3831 * before knowing if we'll have to send a HEADERS frame for the
3832 * trailers. More importantly we're not consuming the trailing CRLF
3833 * after the end of trailers, so it will be left to the caller to
3834 * eat it. The right way to do it would be to measure trailers here
3835 * and to send ES only if there are no trailers.
3836 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003837 */
3838 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003839 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003840 es_now = 1;
3841
3842 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003843 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003844
3845 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003847
3848 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003849 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003850
3851 /* consume incoming H1 response */
3852 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003853 max -= size;
3854 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003855 total += size;
3856 h1m->curr_len -= size;
3857 h2s->mws -= size;
3858 h2c->mws -= size;
3859
3860 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003861 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003862 goto new_frame;
3863 }
3864 }
3865
3866 if (es_now) {
3867 if (h2s->st == H2_SS_OPEN)
3868 h2s->st = H2_SS_HLOC;
3869 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003870 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003871
Willy Tarreau35a62702018-02-27 15:37:25 +01003872 if (!(h1m->flags & H1_MF_CHNK)) {
3873 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003874 total += max;
3875 ofs += max;
3876 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003877
Willy Tarreau801250e2018-09-11 11:45:04 +02003878 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003879 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003880
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003881 h2s->flags |= H2_SF_ES_SENT;
3882 }
3883
3884 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003885 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 +02003886 return total;
3887}
3888
Willy Tarreau115e83b2018-12-01 19:17:53 +01003889/* Try to send a HEADERS frame matching HTX response present in HTX message
3890 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3891 * must check the stream's status to detect any error which might have happened
3892 * subsequently to a successful send. The htx blocks are automatically removed
3893 * from the message. The htx message is assumed to be valid since produced from
3894 * the internal code, hence it contains a start line, an optional series of
3895 * header blocks and an end of header, otherwise an invalid frame could be
3896 * emitted and the resulting htx message could be left in an inconsistent state.
3897 */
3898static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3899{
3900 struct http_hdr list[MAX_HTTP_HDR];
3901 struct h2c *h2c = h2s->h2c;
3902 struct htx_blk *blk;
3903 struct htx_blk *blk_end;
3904 struct buffer outbuf;
3905 struct htx_sl *sl;
3906 enum htx_blk_type type;
3907 int es_now = 0;
3908 int ret = 0;
3909 int hdr;
3910 int idx;
3911
3912 if (h2c_mux_busy(h2c, h2s)) {
3913 h2s->flags |= H2_SF_BLK_MBUSY;
3914 return 0;
3915 }
3916
3917 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3918 h2c->flags |= H2_CF_MUX_MALLOC;
3919 h2s->flags |= H2_SF_BLK_MROOM;
3920 return 0;
3921 }
3922
3923 /* determine the first block which must not be deleted, blk_end may
3924 * be NULL if all blocks have to be deleted.
3925 */
3926 idx = htx_get_head(htx);
3927 blk_end = NULL;
3928 while (idx != -1) {
3929 type = htx_get_blk_type(htx_get_blk(htx, idx));
3930 idx = htx_get_next(htx, idx);
3931 if (type == HTX_BLK_EOH) {
3932 if (idx != -1)
3933 blk_end = htx_get_blk(htx, idx);
3934 break;
3935 }
3936 }
3937
3938 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003939 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003940 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003941 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003942 if (h2s->status < 100 || h2s->status > 999)
3943 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003944
3945 /* and the rest of the headers, that we dump starting at header 0 */
3946 hdr = 0;
3947
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003948 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003949 while ((idx = htx_get_next(htx, idx)) != -1) {
3950 blk = htx_get_blk(htx, idx);
3951 type = htx_get_blk_type(blk);
3952
3953 if (type == HTX_BLK_UNUSED)
3954 continue;
3955
3956 if (type != HTX_BLK_HDR)
3957 break;
3958
3959 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3960 goto fail;
3961
3962 list[hdr].n = htx_get_blk_name(htx, blk);
3963 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003964 hdr++;
3965 }
3966
3967 /* marker for end of headers */
3968 list[hdr].n = ist("");
3969
3970 if (h2s->status == 204 || h2s->status == 304) {
3971 /* no contents, claim c-len is present and set to zero */
3972 es_now = 1;
3973 }
3974
3975 chunk_reset(&outbuf);
3976
3977 while (1) {
3978 outbuf.area = b_tail(&h2c->mbuf);
3979 outbuf.size = b_contig_space(&h2c->mbuf);
3980 outbuf.data = 0;
3981
3982 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3983 break;
3984 realign_again:
3985 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3986 }
3987
3988 if (outbuf.size < 9)
3989 goto full;
3990
3991 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3992 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3993 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3994 outbuf.data = 9;
3995
3996 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003997 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003998 if (b_space_wraps(&h2c->mbuf))
3999 goto realign_again;
4000 goto full;
4001 }
4002
4003 /* encode all headers, stop at empty name */
4004 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4005 /* these ones do not exist in H2 and must be dropped. */
4006 if (isteq(list[hdr].n, ist("connection")) ||
4007 isteq(list[hdr].n, ist("proxy-connection")) ||
4008 isteq(list[hdr].n, ist("keep-alive")) ||
4009 isteq(list[hdr].n, ist("upgrade")) ||
4010 isteq(list[hdr].n, ist("transfer-encoding")))
4011 continue;
4012
4013 if (isteq(list[hdr].n, ist("")))
4014 break; // end
4015
4016 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4017 /* output full */
4018 if (b_space_wraps(&h2c->mbuf))
4019 goto realign_again;
4020 goto full;
4021 }
4022 }
4023
4024 /* we may need to add END_STREAM.
4025 * FIXME: we should also set it when we know for sure that the
4026 * content-length is zero as well as on 204/304
4027 */
4028 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4029 es_now = 1;
4030
4031 if (h2s->cs->flags & CS_FL_SHW)
4032 es_now = 1;
4033
4034 /* update the frame's size */
4035 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4036
4037 if (es_now)
4038 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4039
4040 /* commit the H2 response */
4041 b_add(&h2c->mbuf, outbuf.data);
4042 h2s->flags |= H2_SF_HEADERS_SENT;
4043
4044 /* for now we don't implemented CONTINUATION, so we wait for a
4045 * body or directly end in TRL2.
4046 */
4047 if (es_now) {
4048 h2s->flags |= H2_SF_ES_SENT;
4049 if (h2s->st == H2_SS_OPEN)
4050 h2s->st = H2_SS_HLOC;
4051 else
4052 h2s_close(h2s);
4053 }
4054
4055 /* OK we could properly deliver the response */
4056
4057 /* remove all header blocks including the EOH and compute the
4058 * corresponding size.
4059 *
4060 * FIXME: We should remove everything when es_now is set.
4061 */
4062 ret = 0;
4063 idx = htx_get_head(htx);
4064 blk = htx_get_blk(htx, idx);
4065 while (blk != blk_end) {
4066 ret += htx_get_blksz(blk);
4067 blk = htx_remove_blk(htx, blk);
4068 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004069
4070 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4071 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004072 end:
4073 return ret;
4074 full:
4075 h2c->flags |= H2_CF_MUX_MFULL;
4076 h2s->flags |= H2_SF_BLK_MROOM;
4077 ret = 0;
4078 goto end;
4079 fail:
4080 /* unparsable HTX messages, too large ones to be produced in the local
4081 * list etc go here (unrecoverable errors).
4082 */
4083 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4084 ret = 0;
4085 goto end;
4086}
4087
Willy Tarreau80739692018-10-05 11:35:57 +02004088/* Try to send a HEADERS frame matching HTX request present in HTX message
4089 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4090 * must check the stream's status to detect any error which might have happened
4091 * subsequently to a successful send. The htx blocks are automatically removed
4092 * from the message. The htx message is assumed to be valid since produced from
4093 * the internal code, hence it contains a start line, an optional series of
4094 * header blocks and an end of header, otherwise an invalid frame could be
4095 * emitted and the resulting htx message could be left in an inconsistent state.
4096 */
4097static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4098{
4099 struct http_hdr list[MAX_HTTP_HDR];
4100 struct h2c *h2c = h2s->h2c;
4101 struct htx_blk *blk;
4102 struct htx_blk *blk_end;
4103 struct buffer outbuf;
4104 struct htx_sl *sl;
4105 struct ist meth, path;
4106 enum htx_blk_type type;
4107 int es_now = 0;
4108 int ret = 0;
4109 int hdr;
4110 int idx;
4111
4112 if (h2c_mux_busy(h2c, h2s)) {
4113 h2s->flags |= H2_SF_BLK_MBUSY;
4114 return 0;
4115 }
4116
4117 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4118 h2c->flags |= H2_CF_MUX_MALLOC;
4119 h2s->flags |= H2_SF_BLK_MROOM;
4120 return 0;
4121 }
4122
4123 /* determine the first block which must not be deleted, blk_end may
4124 * be NULL if all blocks have to be deleted.
4125 */
4126 idx = htx_get_head(htx);
4127 blk_end = NULL;
4128 while (idx != -1) {
4129 type = htx_get_blk_type(htx_get_blk(htx, idx));
4130 idx = htx_get_next(htx, idx);
4131 if (type == HTX_BLK_EOH) {
4132 if (idx != -1)
4133 blk_end = htx_get_blk(htx, idx);
4134 break;
4135 }
4136 }
4137
4138 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004139 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004140 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004141 meth = htx_sl_req_meth(sl);
4142 path = htx_sl_req_uri(sl);
4143
4144 /* and the rest of the headers, that we dump starting at header 0 */
4145 hdr = 0;
4146
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004147 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004148 while ((idx = htx_get_next(htx, idx)) != -1) {
4149 blk = htx_get_blk(htx, idx);
4150 type = htx_get_blk_type(blk);
4151
4152 if (type == HTX_BLK_UNUSED)
4153 continue;
4154
4155 if (type != HTX_BLK_HDR)
4156 break;
4157
4158 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4159 goto fail;
4160
4161 list[hdr].n = htx_get_blk_name(htx, blk);
4162 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004163 hdr++;
4164 }
4165
4166 /* marker for end of headers */
4167 list[hdr].n = ist("");
4168
4169 chunk_reset(&outbuf);
4170
4171 while (1) {
4172 outbuf.area = b_tail(&h2c->mbuf);
4173 outbuf.size = b_contig_space(&h2c->mbuf);
4174 outbuf.data = 0;
4175
4176 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4177 break;
4178 realign_again:
4179 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4180 }
4181
4182 if (outbuf.size < 9)
4183 goto full;
4184
4185 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4186 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4187 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4188 outbuf.data = 9;
4189
4190 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004191 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004192 if (b_space_wraps(&h2c->mbuf))
4193 goto realign_again;
4194 goto full;
4195 }
4196
4197 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004198 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4199 /* output full */
4200 if (b_space_wraps(&h2c->mbuf))
4201 goto realign_again;
4202 goto full;
4203 }
Willy Tarreau80739692018-10-05 11:35:57 +02004204
4205 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004206 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004207 /* output full */
4208 if (b_space_wraps(&h2c->mbuf))
4209 goto realign_again;
4210 goto full;
4211 }
4212
4213 /* encode all headers, stop at empty name */
4214 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4215 /* these ones do not exist in H2 and must be dropped. */
4216 if (isteq(list[hdr].n, ist("connection")) ||
4217 isteq(list[hdr].n, ist("proxy-connection")) ||
4218 isteq(list[hdr].n, ist("keep-alive")) ||
4219 isteq(list[hdr].n, ist("upgrade")) ||
4220 isteq(list[hdr].n, ist("transfer-encoding")))
4221 continue;
4222
4223 if (isteq(list[hdr].n, ist("")))
4224 break; // end
4225
4226 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4227 /* output full */
4228 if (b_space_wraps(&h2c->mbuf))
4229 goto realign_again;
4230 goto full;
4231 }
4232 }
4233
4234 /* we may need to add END_STREAM if we have no body :
4235 * - request already closed, or :
4236 * - no transfer-encoding, and :
4237 * - no content-length or content-length:0
4238 * Fixme: this doesn't take into account CONNECT requests.
4239 */
4240 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4241 es_now = 1;
4242
4243 if (sl->flags & HTX_SL_F_BODYLESS)
4244 es_now = 1;
4245
4246 if (h2s->cs->flags & CS_FL_SHW)
4247 es_now = 1;
4248
4249 /* update the frame's size */
4250 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4251
4252 if (es_now)
4253 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4254
4255 /* commit the H2 response */
4256 b_add(&h2c->mbuf, outbuf.data);
4257 h2s->flags |= H2_SF_HEADERS_SENT;
4258 h2s->st = H2_SS_OPEN;
4259
4260 /* for now we don't implemented CONTINUATION, so we wait for a
4261 * body or directly end in TRL2.
4262 */
4263 if (es_now) {
4264 // trim any possibly pending data (eg: inconsistent content-length)
4265 h2s->flags |= H2_SF_ES_SENT;
4266 h2s->st = H2_SS_HLOC;
4267 }
4268
4269 /* remove all header blocks including the EOH and compute the
4270 * corresponding size.
4271 *
4272 * FIXME: We should remove everything when es_now is set.
4273 */
4274 ret = 0;
4275 idx = htx_get_head(htx);
4276 blk = htx_get_blk(htx, idx);
4277 while (blk != blk_end) {
4278 ret += htx_get_blksz(blk);
4279 blk = htx_remove_blk(htx, blk);
4280 }
4281
4282 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4283 htx_remove_blk(htx, blk_end);
4284
4285 end:
4286 return ret;
4287 full:
4288 h2c->flags |= H2_CF_MUX_MFULL;
4289 h2s->flags |= H2_SF_BLK_MROOM;
4290 ret = 0;
4291 goto end;
4292 fail:
4293 /* unparsable HTX messages, too large ones to be produced in the local
4294 * list etc go here (unrecoverable errors).
4295 */
4296 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4297 ret = 0;
4298 goto end;
4299}
4300
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004301/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004302 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4303 * caller must check the stream's status to detect any error which might have
4304 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004305 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4306 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004307static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004308{
4309 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004310 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004311 struct buffer outbuf;
4312 size_t total = 0;
4313 int es_now = 0;
4314 int bsize; /* htx block size */
4315 int fsize; /* h2 frame size */
4316 struct htx_blk *blk;
4317 enum htx_blk_type type;
4318 int idx;
4319
4320 if (h2c_mux_busy(h2c, h2s)) {
4321 h2s->flags |= H2_SF_BLK_MBUSY;
4322 goto end;
4323 }
4324
4325 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4326 h2c->flags |= H2_CF_MUX_MALLOC;
4327 h2s->flags |= H2_SF_BLK_MROOM;
4328 goto end;
4329 }
4330
Willy Tarreau98de12a2018-12-12 07:03:00 +01004331 htx = htx_from_buf(buf);
4332
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004333 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4334 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4335 * the caller to handle.
4336 */
4337
4338 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004339 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004340 goto end;
4341
4342 idx = htx_get_head(htx);
4343 blk = htx_get_blk(htx, idx);
4344 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4345 bsize = htx_get_blksz(blk);
4346 fsize = bsize;
4347
4348 if (type == HTX_BLK_EOD) {
4349 /* if we have an EOD, we're dealing with chunked data. We may
4350 * have a set of trailers after us that the caller will want to
4351 * deal with. Let's simply remove the EOD and return.
4352 */
4353 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004354 total++; // EOD counts as one byte
4355 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004356 goto end;
4357 }
4358
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004359 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4360 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004361
4362 /* Perform some optimizations to reduce the number of buffer copies.
4363 * First, if the mux's buffer is empty and the htx area contains
4364 * exactly one data block of the same size as the requested count, and
4365 * this count fits within the frame size, the stream's window size, and
4366 * the connection's window size, then it's possible to simply swap the
4367 * caller's buffer with the mux's output buffer and adjust offsets and
4368 * length to match the entire DATA HTX block in the middle. In this
4369 * case we perform a true zero-copy operation from end-to-end. This is
4370 * the situation that happens all the time with large files. Second, if
4371 * this is not possible, but the mux's output buffer is empty, we still
4372 * have an opportunity to avoid the copy to the intermediary buffer, by
4373 * making the intermediary buffer's area point to the output buffer's
4374 * area. In this case we want to skip the HTX header to make sure that
4375 * copies remain aligned and that this operation remains possible all
4376 * the time. This goes for headers, data blocks and any data extracted
4377 * from the HTX blocks.
4378 */
4379 if (unlikely(fsize == count &&
4380 htx->used == 1 && type == HTX_BLK_DATA &&
4381 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4382 void *old_area = h2c->mbuf.area;
4383
4384 if (b_data(&h2c->mbuf)) {
4385 /* too bad there are data left there. If we have less
4386 * than 1/4 of the mbuf's size and everything fits,
4387 * we'll perform a copy anyway. Otherwise we'll pretend
4388 * the mbuf is full and wait.
4389 */
4390 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4391 goto copy;
4392 h2c->flags |= H2_CF_MUX_MFULL;
4393 h2s->flags |= H2_SF_BLK_MROOM;
4394 goto end;
4395 }
4396
4397 /* map an H2 frame to the HTX block so that we can put the
4398 * frame header there.
4399 */
4400 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004401 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004402 h2c->mbuf.data = fsize + 9;
4403 outbuf.area = b_head(&h2c->mbuf);
4404
4405 /* prepend an H2 DATA frame header just before the DATA block */
4406 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4407 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4408 h2_set_frame_size(outbuf.area, fsize);
4409
4410 /* update windows */
4411 h2s->mws -= fsize;
4412 h2c->mws -= fsize;
4413
4414 /* and exchange with our old area */
4415 buf->area = old_area;
4416 buf->data = buf->head = 0;
4417 total += fsize;
4418 goto end;
4419 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004420
Willy Tarreau98de12a2018-12-12 07:03:00 +01004421 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004422 /* for DATA and EOM we'll have to emit a frame, even if empty */
4423
4424 while (1) {
4425 outbuf.area = b_tail(&h2c->mbuf);
4426 outbuf.size = b_contig_space(&h2c->mbuf);
4427 outbuf.data = 0;
4428
4429 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4430 break;
4431 realign_again:
4432 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4433 }
4434
4435 if (outbuf.size < 9) {
4436 h2c->flags |= H2_CF_MUX_MFULL;
4437 h2s->flags |= H2_SF_BLK_MROOM;
4438 goto end;
4439 }
4440
4441 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4442 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4443 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4444 outbuf.data = 9;
4445
4446 /* we have in <fsize> the exact number of bytes we need to copy from
4447 * the HTX buffer. We need to check this against the connection's and
4448 * the stream's send windows, and to ensure that this fits in the max
4449 * frame size and in the buffer's available space minus 9 bytes (for
4450 * the frame header). The connection's flow control is applied last so
4451 * that we can use a separate list of streams which are immediately
4452 * unblocked on window opening. Note: we don't implement padding.
4453 */
4454
4455 /* EOM is presented with bsize==1 but would lead to the emission of an
4456 * empty frame, thus we force it to zero here.
4457 */
4458 if (type == HTX_BLK_EOM)
4459 bsize = fsize = 0;
4460
4461 if (!fsize)
4462 goto send_empty;
4463
4464 if (h2s->mws <= 0) {
4465 h2s->flags |= H2_SF_BLK_SFCTL;
4466 if (h2s->send_wait) {
4467 LIST_DEL(&h2s->list);
4468 LIST_INIT(&h2s->list);
4469 }
4470 goto end;
4471 }
4472
Willy Tarreauee573762018-12-04 15:25:57 +01004473 if (fsize > count)
4474 fsize = count;
4475
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004476 if (fsize > h2s->mws)
4477 fsize = h2s->mws; // >0
4478
4479 if (h2c->mfs && fsize > h2c->mfs)
4480 fsize = h2c->mfs; // >0
4481
4482 if (fsize + 9 > outbuf.size) {
4483 /* we have an opportunity for enlarging the too small
4484 * available space, let's try.
4485 * FIXME: is this really interesting to do? Maybe we'll
4486 * spend lots of time realigning instead of using two
4487 * frames.
4488 */
4489 if (b_space_wraps(&h2c->mbuf))
4490 goto realign_again;
4491 fsize = outbuf.size - 9;
4492
4493 if (fsize <= 0) {
4494 /* no need to send an empty frame here */
4495 h2c->flags |= H2_CF_MUX_MFULL;
4496 h2s->flags |= H2_SF_BLK_MROOM;
4497 goto end;
4498 }
4499 }
4500
4501 if (h2c->mws <= 0) {
4502 h2s->flags |= H2_SF_BLK_MFCTL;
4503 goto end;
4504 }
4505
4506 if (fsize > h2c->mws)
4507 fsize = h2c->mws;
4508
4509 /* now let's copy this this into the output buffer */
4510 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004511 h2s->mws -= fsize;
4512 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004513 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004514
4515 send_empty:
4516 /* update the frame's size */
4517 h2_set_frame_size(outbuf.area, fsize);
4518
4519 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4520 * meeting EOM. We should optimize this later.
4521 */
4522 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004523 total++; // EOM counts as one byte
4524 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004525 es_now = 1;
4526 }
4527
4528 if (es_now)
4529 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4530
4531 /* commit the H2 response */
4532 b_add(&h2c->mbuf, fsize + 9);
4533
4534 /* consume incoming HTX block, including EOM */
4535 total += fsize;
4536 if (fsize == bsize) {
4537 htx_remove_blk(htx, blk);
4538 if (fsize)
4539 goto new_frame;
4540 } else {
4541 /* we've truncated this block */
4542 htx_cut_data_blk(htx, blk, fsize);
4543 }
4544
4545 if (es_now) {
4546 if (h2s->st == H2_SS_OPEN)
4547 h2s->st = H2_SS_HLOC;
4548 else
4549 h2s_close(h2s);
4550
4551 h2s->flags |= H2_SF_ES_SENT;
4552 }
4553
4554 end:
4555 return total;
4556}
4557
Olivier Houchard6ff20392018-07-17 18:46:31 +02004558/* Called from the upper layer, to subscribe to events, such as being able to send */
4559static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4560{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004561 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004562 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004563 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004564
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004565 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004566 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004567 if (!(sw->events & SUB_RETRY_RECV)) {
4568 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004569 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004570 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004571 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004572 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004573 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004574 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004575 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004576 if (!(sw->events & SUB_RETRY_SEND)) {
4577 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004578 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004579 h2s->send_wait = sw;
4580 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4581 if (h2s->flags & H2_SF_BLK_MFCTL)
4582 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4583 else
4584 LIST_ADDQ(&h2c->send_list, &h2s->list);
4585 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004586 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004587 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004588 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004589 if (event_type != 0)
4590 return -1;
4591 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004592
4593
4594}
4595
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004596static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4597{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004598 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004599 struct h2s *h2s = cs->ctx;
4600
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004601 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004602 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004603 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004604 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004605 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004606 }
4607 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004608 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004609 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004610 if (h2s->send_wait == sw) {
4611 LIST_DEL(&h2s->list);
4612 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004613 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004614 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004615 }
4616 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004617 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4618 sw = param;
4619 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004620 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004621 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01004622 LIST_DEL(&h2s->list);
4623 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004624 }
4625 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004626 return 0;
4627}
4628
4629
Olivier Houchard511efea2018-08-16 15:30:32 +02004630/* Called from the upper layer, to receive data */
4631static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4632{
Olivier Houchard638b7992018-08-16 15:41:52 +02004633 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004634 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004635 struct htx *h2s_htx = NULL;
4636 struct htx *buf_htx = NULL;
4637 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004638 size_t ret = 0;
4639
4640 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004641 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4642 /* in HTX mode we ignore the count argument */
4643 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004644 if (htx_is_empty(h2s_htx)) {
4645 if (cs->flags & CS_FL_REOS)
4646 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004647 if (cs->flags & CS_FL_ERR_PENDING)
4648 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004649 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004650 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004651
4652 buf_htx = htx_from_buf(buf);
4653 count = htx_free_space(buf_htx);
4654
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004655 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004656
4657 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004658 htx_to_buf(buf_htx, buf);
4659 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004660 ret = htx_ret.ret;
4661 }
4662 else {
4663 ret = b_xfer(buf, &h2s->rxbuf, count);
4664 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004665
Olivier Houchard638b7992018-08-16 15:41:52 +02004666 if (b_data(&h2s->rxbuf))
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 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004669 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004670 if (cs->flags & CS_FL_REOS)
4671 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004672 if (cs->flags & CS_FL_ERR_PENDING)
4673 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004674 if (b_size(&h2s->rxbuf)) {
4675 b_free(&h2s->rxbuf);
4676 offer_buffers(NULL, tasks_run_queue);
4677 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004678 }
4679
Willy Tarreau082f5592018-11-25 08:03:32 +01004680 if (ret && h2c->dsi == h2s->id) {
4681 /* demux is blocking on this stream's buffer */
4682 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau47b515a2018-12-21 16:09:41 +01004683 if (b_data(&h2c->dbuf) || !(h2c->wait_event.events & SUB_RETRY_RECV))
4684 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01004685 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004686end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004687 return ret;
4688}
4689
Olivier Houchardd846c262018-10-19 17:24:29 +02004690static void h2_stop_senders(struct h2c *h2c)
4691{
4692 struct h2s *h2s, *h2s_back;
4693
4694 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4695 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4696 if (h2c->msi == h2s_id(h2s))
4697 continue;
4698 LIST_DEL(&h2s->list);
4699 LIST_INIT(&h2s->list);
4700 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004701 h2s->send_wait->events |= SUB_RETRY_SEND;
4702 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004703 LIST_ADD(&h2c->send_list, &h2s->list);
4704 }
4705}
4706
Willy Tarreau62f52692017-10-08 23:01:42 +02004707/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004708static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004709{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004710 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004711 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004712 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004713 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004714 struct htx *htx;
4715 struct htx_blk *blk;
4716 enum htx_blk_type btype;
4717 uint32_t bsize;
4718 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004719
Olivier Houchardd846c262018-10-19 17:24:29 +02004720 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004721 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004722 h2s->send_wait = NULL;
4723 LIST_DEL(&h2s->list);
4724 LIST_INIT(&h2s->list);
4725 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004726 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4727 return 0;
4728
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004729 /* htx will be enough to decide if we're using HTX or legacy */
4730 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4731
Willy Tarreau0bad0432018-06-14 16:54:01 +02004732 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004733 h2s->flags |= H2_SF_OUTGOING_DATA;
4734
Willy Tarreau751f2d02018-10-05 09:35:00 +02004735 if (h2s->id == 0) {
4736 int32_t id = h2c_get_next_sid(h2s->h2c);
4737
4738 if (id < 0) {
4739 cs->ctx = NULL;
4740 cs->flags |= CS_FL_ERROR;
4741 h2s_destroy(h2s);
4742 return 0;
4743 }
4744
4745 eb32_delete(&h2s->by_id);
4746 h2s->by_id.key = h2s->id = id;
4747 h2s->h2c->max_id = id;
4748 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4749 }
4750
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004751 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004752 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4753 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004754 idx = htx_get_head(htx);
4755 blk = htx_get_blk(htx, idx);
4756 btype = htx_get_blk_type(blk);
4757 bsize = htx_get_blksz(blk);
4758
4759 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004760 case HTX_BLK_REQ_SL:
4761 /* start-line before headers */
4762 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4763 if (ret > 0) {
4764 total += ret;
4765 count -= ret;
4766 if (ret < bsize)
4767 goto done;
4768 }
4769 break;
4770
Willy Tarreau115e83b2018-12-01 19:17:53 +01004771 case HTX_BLK_RES_SL:
4772 /* start-line before headers */
4773 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4774 if (ret > 0) {
4775 total += ret;
4776 count -= ret;
4777 if (ret < bsize)
4778 goto done;
4779 }
4780 break;
4781
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004782 case HTX_BLK_DATA:
4783 case HTX_BLK_EOD:
4784 case HTX_BLK_EOM:
4785 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004786 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004787 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004788 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004789 total += ret;
4790 count -= ret;
4791 if (ret < bsize)
4792 goto done;
4793 }
4794 break;
4795
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004796 default:
4797 htx_remove_blk(htx, blk);
4798 total += bsize;
4799 count -= bsize;
4800 break;
4801 }
4802 }
4803 goto done;
4804 }
4805
4806 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004807 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004808 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004809 if (h2s->h2c->flags & H2_CF_IS_BACK)
4810 ret = -1;
4811 else
4812 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004813 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004814 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004815 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004816 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004817 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004818 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004819 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004820
Willy Tarreau5dd17352018-06-14 13:33:30 +02004821 if (unlikely((int)ret <= 0)) {
4822 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004823 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4824 break;
4825 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004826 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004827 total += count;
4828 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004829 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004830 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004831 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004832 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004833 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004834 break;
4835 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004836
4837 total += ret;
4838 count -= ret;
4839
4840 if (h2s->st >= H2_SS_ERROR)
4841 break;
4842
4843 if (h2s->flags & H2_SF_BLK_ANY)
4844 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004845 }
4846
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004847 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004848 if (h2s->st >= H2_SS_ERROR) {
4849 /* trim any possibly pending data after we close (extra CR-LF,
4850 * unprocessed trailers, abnormal extra data, ...)
4851 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004852 total += count;
4853 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004854 }
4855
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004856 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004857 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004858 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004859 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004860 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004861 }
4862
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004863 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004864 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004865 } else {
4866 b_del(buf, total);
4867 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004868
4869 /* The mux is full, cancel the pending tasks */
4870 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4871 (h2s->flags & H2_SF_BLK_MBUSY))
4872 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004873
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004874 /* If we're running HTX, and we read the whole buffer, then pretend
4875 * we read exactly what the caller specified, as with HTX the caller
4876 * will always give the buffer size, instead of the amount of data
4877 * available.
4878 */
4879 if (htx && !b_data(buf))
4880 total = orig_count;
4881
Olivier Houchard7505f942018-08-21 18:10:44 +02004882 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004883 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004884 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004885
Olivier Houchard7505f942018-08-21 18:10:44 +02004886 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01004887 /* If we're waiting for flow control, and we got a shutr on the
4888 * connection, we will never be unlocked, so add an error on
4889 * the conn_stream.
4890 */
4891 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
4892 !b_data(&h2s->h2c->dbuf) &&
4893 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
4894 if (cs->flags & CS_FL_EOS)
4895 cs->flags |= CS_FL_ERROR;
4896 else
4897 cs->flags |= CS_FL_ERR_PENDING;
4898 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004899 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004900}
4901
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004902/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004903static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004904{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004905 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01004906 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004907 struct eb32_node *node;
4908 int fctl_cnt = 0;
4909 int send_cnt = 0;
4910 int tree_cnt = 0;
4911 int orph_cnt = 0;
4912
4913 if (!h2c)
4914 return;
4915
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004916 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004917 fctl_cnt++;
4918
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004919 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004920 send_cnt++;
4921
Willy Tarreau3af37712018-12-18 14:34:41 +01004922 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004923 node = eb32_first(&h2c->streams_by_id);
4924 while (node) {
4925 h2s = container_of(node, struct h2s, by_id);
4926 tree_cnt++;
4927 if (!h2s->cs)
4928 orph_cnt++;
4929 node = eb32_next(node);
4930 }
4931
Willy Tarreau987c0632018-12-18 10:32:05 +01004932 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
4933 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
4934 " .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 +02004935 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4936 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004937 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01004938 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
4939 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4940 h2c->msi,
4941 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
4942 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
4943
4944 if (h2s) {
4945 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4946 h2s, h2s->id, h2s->flags,
4947 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
4948 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
4949 h2s->cs);
4950 if (h2s->cs)
4951 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4952 h2s->cs->flags, h2s->cs->data);
4953 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004954}
Willy Tarreau62f52692017-10-08 23:01:42 +02004955
4956/*******************************************************/
4957/* functions below are dedicated to the config parsers */
4958/*******************************************************/
4959
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004960/* config parser for global "tune.h2.header-table-size" */
4961static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4962 struct proxy *defpx, const char *file, int line,
4963 char **err)
4964{
4965 if (too_many_args(1, args, err, NULL))
4966 return -1;
4967
4968 h2_settings_header_table_size = atoi(args[1]);
4969 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4970 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4971 return -1;
4972 }
4973 return 0;
4974}
Willy Tarreau62f52692017-10-08 23:01:42 +02004975
Willy Tarreaue6baec02017-07-27 11:45:11 +02004976/* config parser for global "tune.h2.initial-window-size" */
4977static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4978 struct proxy *defpx, const char *file, int line,
4979 char **err)
4980{
4981 if (too_many_args(1, args, err, NULL))
4982 return -1;
4983
4984 h2_settings_initial_window_size = atoi(args[1]);
4985 if (h2_settings_initial_window_size < 0) {
4986 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4987 return -1;
4988 }
4989 return 0;
4990}
4991
Willy Tarreau5242ef82017-07-27 11:47:28 +02004992/* config parser for global "tune.h2.max-concurrent-streams" */
4993static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4994 struct proxy *defpx, const char *file, int line,
4995 char **err)
4996{
4997 if (too_many_args(1, args, err, NULL))
4998 return -1;
4999
5000 h2_settings_max_concurrent_streams = atoi(args[1]);
5001 if (h2_settings_max_concurrent_streams < 0) {
5002 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5003 return -1;
5004 }
5005 return 0;
5006}
5007
Willy Tarreau62f52692017-10-08 23:01:42 +02005008
5009/****************************************/
5010/* MUX initialization and instanciation */
5011/***************************************/
5012
5013/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005014static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005015 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005016 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005017 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005018 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005019 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005020 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005021 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005022 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005023 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005024 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005025 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005026 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005027 .shutr = h2_shutr,
5028 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005029 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005030 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005031 .name = "H2",
5032};
5033
Christopher Faulet32f61c02018-04-10 14:33:41 +02005034/* PROTO selection : this mux registers PROTO token "h2" */
5035static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005036 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005037
Willy Tarreau0108d902018-11-25 19:14:37 +01005038INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5039
Willy Tarreauf8957272018-10-03 10:25:20 +02005040static struct mux_proto_list mux_proto_h2_htx =
5041 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5042
5043INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5044
Willy Tarreau62f52692017-10-08 23:01:42 +02005045/* config keyword parsers */
5046static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005047 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005048 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005049 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005050 { 0, NULL, NULL }
5051}};
5052
Willy Tarreau0108d902018-11-25 19:14:37 +01005053INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);