blob: d74d51481806f760b6c9079556ef3e9289f35014 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
90 int32_t max_id; /* highest ID known on this connection, <0 before preface */
91 uint32_t rcvd_c; /* newly received data to ACK for the connection */
92 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
93
94 /* states for the demux direction */
95 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020096 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020097
98 int32_t dsi; /* demux stream ID (<0 = idle) */
99 int32_t dfl; /* demux frame length (if dsi >= 0) */
100 int8_t dft; /* demux frame type (if dsi >= 0) */
101 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100102 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
103 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200104 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
105
106 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200108 int32_t msi; /* mux stream ID (<0 = idle) */
109 int32_t mfl; /* mux frame length (if dsi >= 0) */
110 int8_t mft; /* mux frame type (if dsi >= 0) */
111 int8_t mff; /* mux frame flags (if dsi >= 0) */
112 /* 16 bit hole here */
113 int32_t miw; /* mux initial window size for all new streams */
114 int32_t mws; /* mux window size. Can be negative. */
115 int32_t mfs; /* mux's max frame size */
116
Willy Tarreauea392822017-10-31 10:02:25 +0100117 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100118 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100119 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200120 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200121 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100122 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200123 struct eb_root streams_by_id; /* all active streams by their ID */
124 struct list send_list; /* list of blocked streams requesting to send */
125 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200126 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100127 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200128 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200129};
130
Willy Tarreau18312642017-10-11 07:57:07 +0200131/* H2 stream state, in h2s->st */
132enum h2_ss {
133 H2_SS_IDLE = 0, // idle
134 H2_SS_RLOC, // reserved(local)
135 H2_SS_RREM, // reserved(remote)
136 H2_SS_OPEN, // open
137 H2_SS_HREM, // half-closed(remote)
138 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200139 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200140 H2_SS_CLOSED, // closed
141 H2_SS_ENTRIES // must be last
142} __attribute__((packed));
143
144/* HTTP/2 stream flags (32 bit), in h2s->flags */
145#define H2_SF_NONE 0x00000000
146#define H2_SF_ES_RCVD 0x00000001
147#define H2_SF_ES_SENT 0x00000002
148
149#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
150#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
151
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200152/* stream flags indicating the reason the stream is blocked */
153#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
154#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
155#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
156#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
157#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
158
Willy Tarreau454f9052017-10-26 19:40:35 +0200159/* stream flags indicating how data is supposed to be sent */
160#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
161#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
162
163/* step we're currently in when sending chunks. This is needed because we may
164 * have to transfer chunks as large as a full buffer so there's no room left
165 * for size nor crlf around.
166 */
167#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
168#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
169#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
170
171#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
172
Willy Tarreau67434202017-11-06 20:20:51 +0100173#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100174#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100175
Willy Tarreau18312642017-10-11 07:57:07 +0200176/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
177 * it is being processed in the internal HTTP representation (H1 for now).
178 */
179struct h2s {
180 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100181 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200182 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200183 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200184 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200185 int32_t id; /* stream ID */
186 uint32_t flags; /* H2_SF_* */
187 int mws; /* mux window size for this stream */
188 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
189 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200190 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200191 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200192 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
193 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
194 struct wait_event *send_wait; /* The streeam is waiting for flow control */
195 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200196};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200197
Willy Tarreauc6405142017-09-21 20:23:50 +0200198/* descriptor for an h2 frame header */
199struct h2_fh {
200 uint32_t len; /* length, host order, 24 bits */
201 uint32_t sid; /* stream id, host order, 31 bits */
202 uint8_t ft; /* frame type */
203 uint8_t ff; /* frame flags */
204};
205
Willy Tarreau8ceae722018-11-26 11:58:30 +0100206/* the h2c connection pool */
207DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
208
209/* the h2s stream pool */
210DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
211
Willy Tarreaudc572362018-12-12 08:08:05 +0100212/* The default connection window size is 65535, it may only be enlarged using
213 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
214 * we'll pretend we already received the difference between the two to send
215 * an equivalent window update to enlarge it to 2G-1.
216 */
217#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
218
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200219/* a few settings from the global section */
220static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200221static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200222static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200223
Willy Tarreau2a856182017-05-16 15:20:39 +0200224/* a dmumy closed stream */
225static const struct h2s *h2_closed_stream = &(const struct h2s){
226 .cs = NULL,
227 .h2c = NULL,
228 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100229 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100230 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200231 .id = 0,
232};
233
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100234/* a dmumy closed stream returning a PROTOCOL_ERROR error */
235static const struct h2s *h2_error_stream = &(const struct h2s){
236 .cs = NULL,
237 .h2c = NULL,
238 .st = H2_SS_CLOSED,
239 .errcode = H2_ERR_PROTOCOL_ERROR,
240 .flags = 0,
241 .id = 0,
242};
243
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100244/* a dmumy closed stream returning a REFUSED_STREAM error */
245static const struct h2s *h2_refused_stream = &(const struct h2s){
246 .cs = NULL,
247 .h2c = NULL,
248 .st = H2_SS_CLOSED,
249 .errcode = H2_ERR_REFUSED_STREAM,
250 .flags = 0,
251 .id = 0,
252};
253
Willy Tarreau2a856182017-05-16 15:20:39 +0200254/* and a dummy idle stream for use with any unannounced stream */
255static const struct h2s *h2_idle_stream = &(const struct h2s){
256 .cs = NULL,
257 .h2c = NULL,
258 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100259 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200260 .id = 0,
261};
262
Olivier Houchard9f6af332018-05-25 14:04:04 +0200263static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200264static int h2_send(struct h2c *h2c);
265static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200266static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200267static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100268static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +0100269static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100270static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200271static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100272static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100273static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200274
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200275/*****************************************************/
276/* functions below are for dynamic buffer management */
277/*****************************************************/
278
Willy Tarreau315d8072017-12-10 22:17:57 +0100279/* indicates whether or not the we may call the h2_recv() function to attempt
280 * to receive data into the buffer and/or demux pending data. The condition is
281 * a bit complex due to some API limits for now. The rules are the following :
282 * - if an error or a shutdown was detected on the connection and the buffer
283 * is empty, we must not attempt to receive
284 * - if the demux buf failed to be allocated, we must not try to receive and
285 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100286 * - if no flag indicates a blocking condition, we may attempt to receive,
287 * regardless of whether the demux buffer is full or not, so that only
288 * de demux part decides whether or not to block. This is needed because
289 * the connection API indeed prevents us from re-enabling receipt that is
290 * already enabled in a polled state, so we must always immediately stop
291 * as soon as the demux can't proceed so as never to hit an end of read
292 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100293 * - otherwise must may not attempt
294 */
295static inline int h2_recv_allowed(const struct h2c *h2c)
296{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200297 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100298 (h2c->st0 >= H2_CS_ERROR ||
299 h2c->conn->flags & CO_FL_ERROR ||
300 conn_xprt_read0_pending(h2c->conn)))
301 return 0;
302
303 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100304 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100305 return 1;
306
307 return 0;
308}
309
Willy Tarreau47b515a2018-12-21 16:09:41 +0100310/* restarts reading on the connection if it was not enabled */
311static inline void h2c_restart_reading(const struct h2c *h2c)
312{
313 if (!h2_recv_allowed(h2c))
314 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100315 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100316 return;
317 tasklet_wakeup(h2c->wait_event.task);
318}
319
320
Willy Tarreauf2101912018-07-19 10:11:38 +0200321/* returns true if the connection has too many conn_streams attached */
322static inline int h2_has_too_many_cs(const struct h2c *h2c)
323{
Willy Tarreaua8754662018-12-23 20:43:58 +0100324 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200325}
326
Willy Tarreau44e973f2018-03-01 17:49:30 +0100327/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
328 * flags are used to figure what buffer was requested. It returns 1 if the
329 * allocation succeeds, in which case the connection is woken up, or 0 if it's
330 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200331 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100332static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200333{
334 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100335 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200336
Willy Tarreau44e973f2018-03-01 17:49:30 +0100337 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200338 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100339 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200340 return 1;
341 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200342
Willy Tarreau44e973f2018-03-01 17:49:30 +0100343 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
344 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200345
346 if (h2c->flags & H2_CF_DEM_MROOM) {
347 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100348 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200349 }
Willy Tarreau14398122017-09-22 14:26:04 +0200350 return 1;
351 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100352
353 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
354 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200355 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100356 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100357 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100358 return 1;
359 }
360
Willy Tarreau14398122017-09-22 14:26:04 +0200361 return 0;
362}
363
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200364static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200365{
366 struct buffer *buf = NULL;
367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
369 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
370 h2c->buf_wait.target = h2c;
371 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100372 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100373 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100374 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200375 __conn_xprt_stop_recv(h2c->conn);
376 }
377 return buf;
378}
379
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200380static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200381{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200382 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100383 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200384 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200385 }
386}
387
Olivier Houchardd540b362018-11-05 18:37:53 +0100388static int h2_avail_streams(struct connection *conn)
389{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100390 struct h2c *h2c = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100391
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100392 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100393 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
394}
395
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100396static int h2_max_streams(struct connection *conn)
397{
398 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
399 return h2_settings_max_concurrent_streams;
400}
401
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200402
Willy Tarreau62f52692017-10-08 23:01:42 +0200403/*****************************************************************/
404/* functions below are dedicated to the mux setup and management */
405/*****************************************************************/
406
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200407/* Initialize the mux once it's attached. For outgoing connections, the context
408 * is already initialized before installing the mux, so we detect incoming
409 * connections from the fact that the context is still NULL. Returns < 0 on
410 * error.
411 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100412static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413{
414 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100415 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416
Willy Tarreaubafbe012017-11-24 17:34:44 +0100417 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200419 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100421 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200422 h2c->flags = H2_CF_IS_BACK;
423 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
424 if (tick_isset(prx->timeout.serverfin))
425 h2c->shut_timeout = prx->timeout.serverfin;
426 } else {
427 h2c->flags = H2_CF_NONE;
428 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
429 if (tick_isset(prx->timeout.clientfin))
430 h2c->shut_timeout = prx->timeout.clientfin;
431 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100432
Willy Tarreau0b37d652018-10-03 10:33:02 +0200433 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100434 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100435 if (tick_isset(h2c->timeout)) {
436 t = task_new(tid_bit);
437 if (!t)
438 goto fail;
439
440 h2c->task = t;
441 t->process = h2_timeout_task;
442 t->context = h2c;
443 t->expire = tick_add(now_ms, h2c->timeout);
444 }
Willy Tarreauea392822017-10-31 10:02:25 +0100445
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200446 h2c->wait_event.task = tasklet_new();
447 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200448 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200449 h2c->wait_event.task->process = h2_io_cb;
450 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100451 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200452
Willy Tarreau32218eb2017-09-22 08:07:25 +0200453 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
454 if (!h2c->ddht)
455 goto fail;
456
457 /* Initialise the context. */
458 h2c->st0 = H2_CS_PREFACE;
459 h2c->conn = conn;
460 h2c->max_id = -1;
461 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100462 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100464 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200465 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200467 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200468 h2c->dsi = -1;
469 h2c->msi = -1;
470 h2c->last_sid = -1;
471
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200472 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200473 h2c->miw = 65535; /* mux initial window size */
474 h2c->mws = 65535; /* mux window size */
475 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200476 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200477 LIST_INIT(&h2c->send_list);
478 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200479 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100480 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481
Willy Tarreau3f133572017-10-31 19:21:06 +0100482 if (t)
483 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100484
Willy Tarreau01b44822018-10-03 14:26:37 +0200485 if (h2c->flags & H2_CF_IS_BACK) {
486 /* FIXME: this is temporary, for outgoing connections we need
487 * to immediately allocate a stream until the code is modified
488 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100489 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 */
491 struct h2s *h2s;
492
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100493 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200494 if (!h2s)
495 goto fail_stream;
496 }
497
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100498 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200499
Willy Tarreau0f383582018-10-03 14:22:21 +0200500 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100501 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200502 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200503 fail_stream:
504 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200505 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100506 if (t)
507 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200508 if (h2c->wait_event.task)
509 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100510 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200511 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200512 return -1;
513}
514
Willy Tarreau751f2d02018-10-05 09:35:00 +0200515/* returns the next allocatable outgoing stream ID for the H2 connection, or
516 * -1 if no more is allocatable.
517 */
518static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
519{
520 int32_t id = (h2c->max_id + 1) | 1;
521 if (id & 0x80000000U)
522 id = -1;
523 return id;
524}
525
Willy Tarreau2373acc2017-10-12 17:35:14 +0200526/* returns the stream associated with id <id> or NULL if not found */
527static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
528{
529 struct eb32_node *node;
530
Willy Tarreau751f2d02018-10-05 09:35:00 +0200531 if (id == 0)
532 return (struct h2s *)h2_closed_stream;
533
Willy Tarreau2a856182017-05-16 15:20:39 +0200534 if (id > h2c->max_id)
535 return (struct h2s *)h2_idle_stream;
536
Willy Tarreau2373acc2017-10-12 17:35:14 +0200537 node = eb32_lookup(&h2c->streams_by_id, id);
538 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200539 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200540
541 return container_of(node, struct h2s, by_id);
542}
543
Willy Tarreau62f52692017-10-08 23:01:42 +0200544/* release function for a connection. This one should be called to free all
545 * resources allocated to the mux.
546 */
547static void h2_release(struct connection *conn)
548{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100549 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550
551 LIST_DEL(&conn->list);
552
553 if (h2c) {
554 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200555
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100556 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100557 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100558 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200559
Willy Tarreau44e973f2018-03-01 17:49:30 +0100560 h2_release_buf(h2c, &h2c->dbuf);
561 h2_release_buf(h2c, &h2c->mbuf);
562
Willy Tarreauea392822017-10-31 10:02:25 +0100563 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200564 h2c->task->context = NULL;
565 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100566 h2c->task = NULL;
567 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200568 if (h2c->wait_event.task)
569 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100570 if (h2c->wait_event.events != 0)
571 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200572 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100573
Willy Tarreaubafbe012017-11-24 17:34:44 +0100574 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200575 }
576
577 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100578 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200579
580 conn_stop_tracking(conn);
581 conn_full_close(conn);
582 if (conn->destroy_cb)
583 conn->destroy_cb(conn);
584 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200585}
586
587
Willy Tarreau71681172017-10-23 14:39:06 +0200588/******************************************************/
589/* functions below are for the H2 protocol processing */
590/******************************************************/
591
592/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100593static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200594{
595 return h2s ? h2s->id : 0;
596}
597
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200598/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100599static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200600{
601 if (h2c->msi < 0)
602 return 0;
603
604 if (h2c->msi == h2s_id(h2s))
605 return 0;
606
607 return 1;
608}
609
Willy Tarreau741d6df2017-10-17 08:00:59 +0200610/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100611static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200612{
613 h2c->errcode = err;
614 h2c->st0 = H2_CS_ERROR;
615}
616
Willy Tarreau2e43f082017-10-17 08:03:59 +0200617/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100618static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200619{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200620 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200621 h2s->errcode = err;
622 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100623 if (h2s->cs)
624 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200625 }
626}
627
Willy Tarreau7e094452018-12-19 18:08:52 +0100628/* attempt to notify the data layer of recv availability */
629static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
630{
631 struct wait_event *sw;
632
633 if (h2s->recv_wait) {
634 sw = h2s->recv_wait;
635 sw->events &= ~SUB_RETRY_RECV;
636 tasklet_wakeup(sw->task);
637 h2s->recv_wait = NULL;
638 }
639}
640
641/* attempt to notify the data layer of send availability */
642static void __maybe_unused h2s_notify_send(struct h2s *h2s)
643{
644 struct wait_event *sw;
645
646 if (h2s->send_wait) {
647 sw = h2s->send_wait;
648 sw->events &= ~SUB_RETRY_SEND;
649 tasklet_wakeup(sw->task);
650 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100651 LIST_DEL(&h2s->list);
652 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100653 }
654}
655
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100656/* alerts the data layer, trying to wake it up by all means, following
657 * this sequence :
658 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
659 * - if its subscribed to send, then it's woken up for send
660 * - if it was subscribed to neither, its ->wake() callback is called
661 * It is safe to call this function with a closed stream which doesn't have a
662 * conn_stream anymore.
663 */
664static void __maybe_unused h2s_alert(struct h2s *h2s)
665{
666 if (h2s->recv_wait || h2s->send_wait) {
667 h2s_notify_recv(h2s);
668 h2s_notify_send(h2s);
669 }
670 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
671 h2s->cs->data_cb->wake(h2s->cs);
672}
673
Willy Tarreaue4820742017-07-27 13:37:23 +0200674/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200676{
677 uint8_t *out = frame;
678
679 *out = len >> 16;
680 write_n16(out + 1, len);
681}
682
Willy Tarreau54c15062017-10-10 17:10:03 +0200683/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
684 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
685 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200686 * available in the buffer's input prior to calling this function. The buffer
687 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200688 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100689static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200690 const struct buffer *b, int o)
691{
Willy Tarreau591d4452018-06-15 17:21:00 +0200692 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 +0200693}
694
Willy Tarreau1f094672017-11-20 21:27:45 +0100695static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200696{
Willy Tarreau591d4452018-06-15 17:21:00 +0200697 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200698}
699
Willy Tarreau1f094672017-11-20 21:27:45 +0100700static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200701{
Willy Tarreau591d4452018-06-15 17:21:00 +0200702 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200703}
704
Willy Tarreau1f094672017-11-20 21:27:45 +0100705static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200706{
Willy Tarreau591d4452018-06-15 17:21:00 +0200707 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200708}
709
710
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100711/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
712 * The algorithm is not obvious. It turns out that H2 headers are neither
713 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
714 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200715 *
716 * b0 b1 b2 b3 b4 b5..b8
717 * +----------+---------+--------+----+----+----------------------+
718 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
719 * +----------+---------+--------+----+----+----------------------+
720 *
721 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
722 * we get the sid properly aligned and ordered, and 16 bits of len properly
723 * ordered as well. The type and flags can be extracted using bit shifts from
724 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200725 * Returns zero if some bytes are missing, otherwise non-zero on success. The
726 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200727 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100728static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200729{
730 uint64_t w;
731
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100732 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200733 return 0;
734
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100735 w = h2_get_n64(b, o + 1);
736 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200737 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
738 h->ff = w >> 32;
739 h->ft = w >> 40;
740 h->len += w >> 48;
741 return 1;
742}
743
744/* skip the next 9 bytes corresponding to the frame header possibly parsed by
745 * h2_peek_frame_hdr() above.
746 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100747static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200748{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200749 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200750}
751
752/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200754{
755 int ret;
756
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100757 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200758 if (ret > 0)
759 h2_skip_frame_hdr(b);
760 return ret;
761}
762
Willy Tarreau00dd0782018-03-01 16:31:34 +0100763/* marks stream <h2s> as CLOSED and decrement the number of active streams for
764 * its connection if the stream was not yet closed. Please use this exclusively
765 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100766 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100767static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100768{
769 if (h2s->st != H2_SS_CLOSED)
770 h2s->h2c->nb_streams--;
771 h2s->st = H2_SS_CLOSED;
772}
773
Willy Tarreau71049cc2018-03-28 13:56:39 +0200774/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
775static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100776{
777 h2s_close(h2s);
778 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200779 if (b_size(&h2s->rxbuf)) {
780 b_free(&h2s->rxbuf);
781 offer_buffers(NULL, tasks_run_queue);
782 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200783 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100784 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200785 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100786 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800787 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200788 * reference left would be in the h2c send_list/fctl_list, and if
789 * we're in it, we're getting out anyway
790 */
791 LIST_DEL(&h2s->list);
792 LIST_INIT(&h2s->list);
793 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100794 pool_free(pool_head_h2s, h2s);
795}
796
Willy Tarreaua8e49542018-10-03 18:53:55 +0200797/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
798 * stream tree. In case of error, nothing is added and NULL is returned. The
799 * causes of errors can be any failed memory allocation. The caller is
800 * responsible for checking if the connection may support an extra stream
801 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200802 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200803static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200804{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200805 struct h2s *h2s;
806
Willy Tarreaubafbe012017-11-24 17:34:44 +0100807 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200808 if (!h2s)
809 goto out;
810
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200811 h2s->wait_event.task = tasklet_new();
812 if (!h2s->wait_event.task) {
813 pool_free(pool_head_h2s, h2s);
814 goto out;
815 }
816 h2s->send_wait = NULL;
817 h2s->recv_wait = NULL;
818 h2s->wait_event.task->process = h2_deferred_shut;
819 h2s->wait_event.task->context = h2s;
820 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100821 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200822 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200823 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200824 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200825 h2s->mws = h2c->miw;
826 h2s->flags = H2_SF_NONE;
827 h2s->errcode = H2_ERR_NO_ERROR;
828 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200829 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200830 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200831
832 if (h2c->flags & H2_CF_IS_BACK) {
833 h1m_init_req(&h2s->h1m);
834 h2s->h1m.err_pos = -1; // don't care about errors on the request path
835 h2s->h1m.flags |= H1_MF_TOLOWER;
836 } else {
837 h1m_init_res(&h2s->h1m);
838 h2s->h1m.err_pos = -1; // don't care about errors on the response path
839 h2s->h1m.flags |= H1_MF_TOLOWER;
840 }
841
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200842 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200843 if (id > 0)
844 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200845
846 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100847 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200848
849 return h2s;
850
851 out_free_h2s:
852 pool_free(pool_head_h2s, h2s);
853 out:
854 return NULL;
855}
856
857/* creates a new stream <id> on the h2c connection and returns it, or NULL in
858 * case of memory allocation error.
859 */
860static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
861{
862 struct session *sess = h2c->conn->owner;
863 struct conn_stream *cs;
864 struct h2s *h2s;
865
866 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
867 goto out;
868
869 h2s = h2s_new(h2c, id);
870 if (!h2s)
871 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200872
873 cs = cs_new(h2c->conn);
874 if (!cs)
875 goto out_close;
876
Olivier Houchard746fb772018-12-15 19:42:00 +0100877 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200878 h2s->cs = cs;
879 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200880 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200881
882 if (stream_create_from_cs(cs) < 0)
883 goto out_free_cs;
884
Willy Tarreau590a0512018-09-05 11:56:48 +0200885 /* We want the accept date presented to the next stream to be the one
886 * we have now, the handshake time to be null (since the next stream
887 * is not delayed by a handshake), and the idle time to count since
888 * right now.
889 */
890 sess->accept_date = date;
891 sess->tv_accept = now;
892 sess->t_handshake = 0;
893
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200895 if (h2_has_too_many_cs(h2c))
896 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200897 return h2s;
898
899 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200900 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200901 cs_free(cs);
902 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200903 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200904 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200905 sess_log(sess);
906 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200907}
908
Willy Tarreau751f2d02018-10-05 09:35:00 +0200909/* allocates a new stream associated to conn_stream <cs> on the h2c connection
910 * and returns it, or NULL in case of memory allocation error or if the highest
911 * possible stream ID was reached.
912 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100913static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200914{
915 struct h2s *h2s = NULL;
916
917 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
918 goto out;
919
920 /* Defer choosing the ID until we send the first message to create the stream */
921 h2s = h2s_new(h2c, 0);
922 if (!h2s)
923 goto out;
924
925 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100926 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200927 cs->ctx = h2s;
928 h2c->nb_cs++;
929
Willy Tarreau751f2d02018-10-05 09:35:00 +0200930 out:
931 return h2s;
932}
933
Willy Tarreaube5b7152017-09-25 16:25:39 +0200934/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
935 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
936 * the various settings codes.
937 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200938static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200939{
940 struct buffer *res;
941 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200942 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200943 int ret;
944
945 if (h2c_mux_busy(h2c, NULL)) {
946 h2c->flags |= H2_CF_DEM_MBUSY;
947 return 0;
948 }
949
Willy Tarreau44e973f2018-03-01 17:49:30 +0100950 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200951 if (!res) {
952 h2c->flags |= H2_CF_MUX_MALLOC;
953 h2c->flags |= H2_CF_DEM_MROOM;
954 return 0;
955 }
956
957 chunk_init(&buf, buf_data, sizeof(buf_data));
958 chunk_memcpy(&buf,
959 "\x00\x00\x00" /* length : 0 for now */
960 "\x04\x00" /* type : 4 (settings), flags : 0 */
961 "\x00\x00\x00\x00", /* stream ID : 0 */
962 9);
963
964 if (h2_settings_header_table_size != 4096) {
965 char str[6] = "\x00\x01"; /* header_table_size */
966
967 write_n32(str + 2, h2_settings_header_table_size);
968 chunk_memcat(&buf, str, 6);
969 }
970
971 if (h2_settings_initial_window_size != 65535) {
972 char str[6] = "\x00\x04"; /* initial_window_size */
973
974 write_n32(str + 2, h2_settings_initial_window_size);
975 chunk_memcat(&buf, str, 6);
976 }
977
978 if (h2_settings_max_concurrent_streams != 0) {
979 char str[6] = "\x00\x03"; /* max_concurrent_streams */
980
981 /* Note: 0 means "unlimited" for haproxy's config but not for
982 * the protocol, so never send this value!
983 */
984 write_n32(str + 2, h2_settings_max_concurrent_streams);
985 chunk_memcat(&buf, str, 6);
986 }
987
988 if (global.tune.bufsize != 16384) {
989 char str[6] = "\x00\x05"; /* max_frame_size */
990
991 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
992 * match bufsize - rewrite size, but at the moment it seems
993 * that clients don't take care of it.
994 */
995 write_n32(str + 2, global.tune.bufsize);
996 chunk_memcat(&buf, str, 6);
997 }
998
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200999 h2_set_frame_size(buf.area, buf.data - 9);
1000 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001001 if (unlikely(ret <= 0)) {
1002 if (!ret) {
1003 h2c->flags |= H2_CF_MUX_MFULL;
1004 h2c->flags |= H2_CF_DEM_MROOM;
1005 return 0;
1006 }
1007 else {
1008 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1009 return 0;
1010 }
1011 }
1012 return ret;
1013}
1014
Willy Tarreau52eed752017-09-22 15:05:09 +02001015/* Try to receive a connection preface, then upon success try to send our
1016 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1017 * missing data. It may return an error in h2c.
1018 */
1019static int h2c_frt_recv_preface(struct h2c *h2c)
1020{
1021 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001022 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001023
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001024 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001025
1026 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001027 if (ret1 < 0)
1028 sess_log(h2c->conn->owner);
1029
Willy Tarreau52eed752017-09-22 15:05:09 +02001030 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1031 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1032 return 0;
1033 }
1034
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001035 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001036 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001037 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001038
Willy Tarreaube5b7152017-09-25 16:25:39 +02001039 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001040}
1041
Willy Tarreau01b44822018-10-03 14:26:37 +02001042/* Try to send a connection preface, then upon success try to send our
1043 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1044 * missing data. It may return an error in h2c.
1045 */
1046static int h2c_bck_send_preface(struct h2c *h2c)
1047{
1048 struct buffer *res;
1049
1050 if (h2c_mux_busy(h2c, NULL)) {
1051 h2c->flags |= H2_CF_DEM_MBUSY;
1052 return 0;
1053 }
1054
1055 res = h2_get_buf(h2c, &h2c->mbuf);
1056 if (!res) {
1057 h2c->flags |= H2_CF_MUX_MALLOC;
1058 h2c->flags |= H2_CF_DEM_MROOM;
1059 return 0;
1060 }
1061
1062 if (!b_data(res)) {
1063 /* preface not yet sent */
1064 b_istput(res, ist(H2_CONN_PREFACE));
1065 }
1066
1067 return h2c_send_settings(h2c);
1068}
1069
Willy Tarreau081d4722017-05-16 21:51:05 +02001070/* try to send a GOAWAY frame on the connection to report an error or a graceful
1071 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1072 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1073 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1074 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1075 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1076 * on unrecoverable failure. It will not attempt to send one again in this last
1077 * case so that it is safe to use h2c_error() to report such errors.
1078 */
1079static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1080{
1081 struct buffer *res;
1082 char str[17];
1083 int ret;
1084
1085 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1086 return 1; // claim that it worked
1087
1088 if (h2c_mux_busy(h2c, h2s)) {
1089 if (h2s)
1090 h2s->flags |= H2_SF_BLK_MBUSY;
1091 else
1092 h2c->flags |= H2_CF_DEM_MBUSY;
1093 return 0;
1094 }
1095
Willy Tarreau44e973f2018-03-01 17:49:30 +01001096 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001097 if (!res) {
1098 h2c->flags |= H2_CF_MUX_MALLOC;
1099 if (h2s)
1100 h2s->flags |= H2_SF_BLK_MROOM;
1101 else
1102 h2c->flags |= H2_CF_DEM_MROOM;
1103 return 0;
1104 }
1105
1106 /* len: 8, type: 7, flags: none, sid: 0 */
1107 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1108
1109 if (h2c->last_sid < 0)
1110 h2c->last_sid = h2c->max_id;
1111
1112 write_n32(str + 9, h2c->last_sid);
1113 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001114 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001115 if (unlikely(ret <= 0)) {
1116 if (!ret) {
1117 h2c->flags |= H2_CF_MUX_MFULL;
1118 if (h2s)
1119 h2s->flags |= H2_SF_BLK_MROOM;
1120 else
1121 h2c->flags |= H2_CF_DEM_MROOM;
1122 return 0;
1123 }
1124 else {
1125 /* we cannot report this error using GOAWAY, so we mark
1126 * it and claim a success.
1127 */
1128 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1129 h2c->flags |= H2_CF_GOAWAY_FAILED;
1130 return 1;
1131 }
1132 }
1133 h2c->flags |= H2_CF_GOAWAY_SENT;
1134 return ret;
1135}
1136
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001137/* Try to send an RST_STREAM frame on the connection for the indicated stream
1138 * during mux operations. This stream must be valid and cannot be closed
1139 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1140 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1141 * not yet.
1142 *
1143 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1144 * to write the message, it subscribes the stream to future notifications.
1145 */
1146static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1147{
1148 struct buffer *res;
1149 char str[13];
1150 int ret;
1151
1152 if (!h2s || h2s->st == H2_SS_CLOSED)
1153 return 1;
1154
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001155 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1156 * RST_STREAM in response to a RST_STREAM frame.
1157 */
1158 if (h2c->dft == H2_FT_RST_STREAM) {
1159 ret = 1;
1160 goto ignore;
1161 }
1162
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001163 if (h2c_mux_busy(h2c, h2s)) {
1164 h2s->flags |= H2_SF_BLK_MBUSY;
1165 return 0;
1166 }
1167
Willy Tarreau44e973f2018-03-01 17:49:30 +01001168 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001169 if (!res) {
1170 h2c->flags |= H2_CF_MUX_MALLOC;
1171 h2s->flags |= H2_SF_BLK_MROOM;
1172 return 0;
1173 }
1174
1175 /* len: 4, type: 3, flags: none */
1176 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1177 write_n32(str + 5, h2s->id);
1178 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001179 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001180
1181 if (unlikely(ret <= 0)) {
1182 if (!ret) {
1183 h2c->flags |= H2_CF_MUX_MFULL;
1184 h2s->flags |= H2_SF_BLK_MROOM;
1185 return 0;
1186 }
1187 else {
1188 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1189 return 0;
1190 }
1191 }
1192
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001193 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001194 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001195 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001196 return ret;
1197}
1198
1199/* Try to send an RST_STREAM frame on the connection for the stream being
1200 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001201 * error code, even if the stream is one of the dummy ones, and will update
1202 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001203 *
1204 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1205 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001206 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001207 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001208 */
1209static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1210{
1211 struct buffer *res;
1212 char str[13];
1213 int ret;
1214
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001215 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1216 * RST_STREAM in response to a RST_STREAM frame.
1217 */
1218 if (h2c->dft == H2_FT_RST_STREAM) {
1219 ret = 1;
1220 goto ignore;
1221 }
1222
Willy Tarreau27a84c92017-10-17 08:10:17 +02001223 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001224 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001225 return 0;
1226 }
1227
Willy Tarreau44e973f2018-03-01 17:49:30 +01001228 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001229 if (!res) {
1230 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001231 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001232 return 0;
1233 }
1234
1235 /* len: 4, type: 3, flags: none */
1236 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001237
Willy Tarreau27a84c92017-10-17 08:10:17 +02001238 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001239 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001240 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001241
Willy Tarreau27a84c92017-10-17 08:10:17 +02001242 if (unlikely(ret <= 0)) {
1243 if (!ret) {
1244 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001245 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001246 return 0;
1247 }
1248 else {
1249 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1250 return 0;
1251 }
1252 }
1253
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001254 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001255 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001256 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001257 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001258 }
1259
Willy Tarreau27a84c92017-10-17 08:10:17 +02001260 return ret;
1261}
1262
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001263/* try to send an empty DATA frame with the ES flag set to notify about the
1264 * end of stream and match a shutdown(write). If an ES was already sent as
1265 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1266 * on success or zero if nothing was done. In case of lack of room to write the
1267 * message, it subscribes the requesting stream to future notifications.
1268 */
1269static int h2_send_empty_data_es(struct h2s *h2s)
1270{
1271 struct h2c *h2c = h2s->h2c;
1272 struct buffer *res;
1273 char str[9];
1274 int ret;
1275
Willy Tarreau721c9742017-11-07 11:05:42 +01001276 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001277 return 1;
1278
1279 if (h2c_mux_busy(h2c, h2s)) {
1280 h2s->flags |= H2_SF_BLK_MBUSY;
1281 return 0;
1282 }
1283
Willy Tarreau44e973f2018-03-01 17:49:30 +01001284 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001285 if (!res) {
1286 h2c->flags |= H2_CF_MUX_MALLOC;
1287 h2s->flags |= H2_SF_BLK_MROOM;
1288 return 0;
1289 }
1290
1291 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1292 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1293 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001294 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001295 if (likely(ret > 0)) {
1296 h2s->flags |= H2_SF_ES_SENT;
1297 }
1298 else if (!ret) {
1299 h2c->flags |= H2_CF_MUX_MFULL;
1300 h2s->flags |= H2_SF_BLK_MROOM;
1301 return 0;
1302 }
1303 else {
1304 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1305 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001306 }
1307 return ret;
1308}
1309
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001310/* wake the streams attached to the connection, whose id is greater than <last>,
1311 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001312 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1313 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001314 */
1315static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1316{
1317 struct eb32_node *node;
1318 struct h2s *h2s;
1319
1320 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001321 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001322
1323 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001324 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001325
1326 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1327 while (node) {
1328 h2s = container_of(node, struct h2s, by_id);
1329 if (h2s->id <= last)
1330 break;
1331 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001332
1333 if (!h2s->cs) {
1334 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001335 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001336 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001337 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001338
1339 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001340 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1341 h2s->cs->flags |= CS_FL_ERROR;
1342
Willy Tarreauf830f012018-12-19 17:44:55 +01001343 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001344
Willy Tarreaua8519352018-12-18 16:44:28 +01001345 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001346 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001347 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001348 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001349 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001350 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001351 }
1352}
1353
Willy Tarreau3421aba2017-07-27 15:41:03 +02001354/* Increase all streams' outgoing window size by the difference passed in
1355 * argument. This is needed upon receipt of the settings frame if the initial
1356 * window size is different. The difference may be negative and the resulting
1357 * window size as well, for the time it takes to receive some window updates.
1358 */
1359static void h2c_update_all_ws(struct h2c *h2c, int diff)
1360{
1361 struct h2s *h2s;
1362 struct eb32_node *node;
1363
1364 if (!diff)
1365 return;
1366
1367 node = eb32_first(&h2c->streams_by_id);
1368 while (node) {
1369 h2s = container_of(node, struct h2s, by_id);
1370 h2s->mws += diff;
1371 node = eb32_next(node);
1372 }
1373}
1374
1375/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1376 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1377 * return an error in h2c. Described in RFC7540#6.5.
1378 */
1379static int h2c_handle_settings(struct h2c *h2c)
1380{
1381 unsigned int offset;
1382 int error;
1383
1384 if (h2c->dff & H2_F_SETTINGS_ACK) {
1385 if (h2c->dfl) {
1386 error = H2_ERR_FRAME_SIZE_ERROR;
1387 goto fail;
1388 }
1389 return 1;
1390 }
1391
1392 if (h2c->dsi != 0) {
1393 error = H2_ERR_PROTOCOL_ERROR;
1394 goto fail;
1395 }
1396
1397 if (h2c->dfl % 6) {
1398 error = H2_ERR_FRAME_SIZE_ERROR;
1399 goto fail;
1400 }
1401
1402 /* that's the limit we can process */
1403 if (h2c->dfl > global.tune.bufsize) {
1404 error = H2_ERR_FRAME_SIZE_ERROR;
1405 goto fail;
1406 }
1407
1408 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001409 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001410 return 0;
1411
1412 /* parse the frame */
1413 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001414 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1415 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001416
1417 switch (type) {
1418 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1419 /* we need to update all existing streams with the
1420 * difference from the previous iws.
1421 */
1422 if (arg < 0) { // RFC7540#6.5.2
1423 error = H2_ERR_FLOW_CONTROL_ERROR;
1424 goto fail;
1425 }
1426 h2c_update_all_ws(h2c, arg - h2c->miw);
1427 h2c->miw = arg;
1428 break;
1429 case H2_SETTINGS_MAX_FRAME_SIZE:
1430 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1431 error = H2_ERR_PROTOCOL_ERROR;
1432 goto fail;
1433 }
1434 h2c->mfs = arg;
1435 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001436 case H2_SETTINGS_ENABLE_PUSH:
1437 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1438 error = H2_ERR_PROTOCOL_ERROR;
1439 goto fail;
1440 }
1441 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001442 }
1443 }
1444
1445 /* need to ACK this frame now */
1446 h2c->st0 = H2_CS_FRAME_A;
1447 return 1;
1448 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001449 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001450 h2c_error(h2c, error);
1451 return 0;
1452}
1453
1454/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1455 * success or one of the h2_status values.
1456 */
1457static int h2c_ack_settings(struct h2c *h2c)
1458{
1459 struct buffer *res;
1460 char str[9];
1461 int ret = -1;
1462
1463 if (h2c_mux_busy(h2c, NULL)) {
1464 h2c->flags |= H2_CF_DEM_MBUSY;
1465 return 0;
1466 }
1467
Willy Tarreau44e973f2018-03-01 17:49:30 +01001468 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001469 if (!res) {
1470 h2c->flags |= H2_CF_MUX_MALLOC;
1471 h2c->flags |= H2_CF_DEM_MROOM;
1472 return 0;
1473 }
1474
1475 memcpy(str,
1476 "\x00\x00\x00" /* length : 0 (no data) */
1477 "\x04" "\x01" /* type : 4, flags : ACK */
1478 "\x00\x00\x00\x00" /* stream ID */, 9);
1479
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001480 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001481 if (unlikely(ret <= 0)) {
1482 if (!ret) {
1483 h2c->flags |= H2_CF_MUX_MFULL;
1484 h2c->flags |= H2_CF_DEM_MROOM;
1485 return 0;
1486 }
1487 else {
1488 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1489 return 0;
1490 }
1491 }
1492 return ret;
1493}
1494
Willy Tarreaucf68c782017-10-10 17:11:41 +02001495/* processes a PING frame and schedules an ACK if needed. The caller must pass
1496 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1497 * missing data. It may return an error in h2c.
1498 */
1499static int h2c_handle_ping(struct h2c *h2c)
1500{
1501 /* frame length must be exactly 8 */
1502 if (h2c->dfl != 8) {
1503 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1504 return 0;
1505 }
1506
1507 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001508 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001509 h2c->st0 = H2_CS_FRAME_A;
1510 return 1;
1511}
1512
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001513/* Try to send a window update for stream id <sid> and value <increment>.
1514 * Returns > 0 on success or zero on missing room or failure. It may return an
1515 * error in h2c.
1516 */
1517static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1518{
1519 struct buffer *res;
1520 char str[13];
1521 int ret = -1;
1522
1523 if (h2c_mux_busy(h2c, NULL)) {
1524 h2c->flags |= H2_CF_DEM_MBUSY;
1525 return 0;
1526 }
1527
Willy Tarreau44e973f2018-03-01 17:49:30 +01001528 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001529 if (!res) {
1530 h2c->flags |= H2_CF_MUX_MALLOC;
1531 h2c->flags |= H2_CF_DEM_MROOM;
1532 return 0;
1533 }
1534
1535 /* length: 4, type: 8, flags: none */
1536 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1537 write_n32(str + 5, sid);
1538 write_n32(str + 9, increment);
1539
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001540 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001541
1542 if (unlikely(ret <= 0)) {
1543 if (!ret) {
1544 h2c->flags |= H2_CF_MUX_MFULL;
1545 h2c->flags |= H2_CF_DEM_MROOM;
1546 return 0;
1547 }
1548 else {
1549 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1550 return 0;
1551 }
1552 }
1553 return ret;
1554}
1555
1556/* try to send pending window update for the connection. It's safe to call it
1557 * with no pending updates. Returns > 0 on success or zero on missing room or
1558 * failure. It may return an error in h2c.
1559 */
1560static int h2c_send_conn_wu(struct h2c *h2c)
1561{
1562 int ret = 1;
1563
1564 if (h2c->rcvd_c <= 0)
1565 return 1;
1566
Willy Tarreau97aaa672018-12-23 09:49:04 +01001567 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1568 /* increase the advertised connection window to 2G on
1569 * first update.
1570 */
1571 h2c->flags |= H2_CF_WINDOW_OPENED;
1572 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1573 }
1574
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001575 /* send WU for the connection */
1576 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1577 if (ret > 0)
1578 h2c->rcvd_c = 0;
1579
1580 return ret;
1581}
1582
1583/* try to send pending window update for the current dmux stream. It's safe to
1584 * call it with no pending updates. Returns > 0 on success or zero on missing
1585 * room or failure. It may return an error in h2c.
1586 */
1587static int h2c_send_strm_wu(struct h2c *h2c)
1588{
1589 int ret = 1;
1590
1591 if (h2c->rcvd_s <= 0)
1592 return 1;
1593
1594 /* send WU for the stream */
1595 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1596 if (ret > 0)
1597 h2c->rcvd_s = 0;
1598
1599 return ret;
1600}
1601
Willy Tarreaucf68c782017-10-10 17:11:41 +02001602/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1603 * success, 0 on missing data or one of the h2_status values.
1604 */
1605static int h2c_ack_ping(struct h2c *h2c)
1606{
1607 struct buffer *res;
1608 char str[17];
1609 int ret = -1;
1610
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001611 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001612 return 0;
1613
1614 if (h2c_mux_busy(h2c, NULL)) {
1615 h2c->flags |= H2_CF_DEM_MBUSY;
1616 return 0;
1617 }
1618
Willy Tarreau44e973f2018-03-01 17:49:30 +01001619 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001620 if (!res) {
1621 h2c->flags |= H2_CF_MUX_MALLOC;
1622 h2c->flags |= H2_CF_DEM_MROOM;
1623 return 0;
1624 }
1625
1626 memcpy(str,
1627 "\x00\x00\x08" /* length : 8 (same payload) */
1628 "\x06" "\x01" /* type : 6, flags : ACK */
1629 "\x00\x00\x00\x00" /* stream ID */, 9);
1630
1631 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001632 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001633
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001634 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001635 if (unlikely(ret <= 0)) {
1636 if (!ret) {
1637 h2c->flags |= H2_CF_MUX_MFULL;
1638 h2c->flags |= H2_CF_DEM_MROOM;
1639 return 0;
1640 }
1641 else {
1642 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1643 return 0;
1644 }
1645 }
1646 return ret;
1647}
1648
Willy Tarreau26f95952017-07-27 17:18:30 +02001649/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1650 * Returns > 0 on success or zero on missing data. It may return an error in
1651 * h2c or h2s. Described in RFC7540#6.9.
1652 */
1653static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1654{
1655 int32_t inc;
1656 int error;
1657
1658 if (h2c->dfl != 4) {
1659 error = H2_ERR_FRAME_SIZE_ERROR;
1660 goto conn_err;
1661 }
1662
1663 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001664 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001665 return 0;
1666
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001667 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001668
1669 if (h2c->dsi != 0) {
1670 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001671
1672 /* it's not an error to receive WU on a closed stream */
1673 if (h2s->st == H2_SS_CLOSED)
1674 return 1;
1675
1676 if (!inc) {
1677 error = H2_ERR_PROTOCOL_ERROR;
1678 goto strm_err;
1679 }
1680
1681 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1682 error = H2_ERR_FLOW_CONTROL_ERROR;
1683 goto strm_err;
1684 }
1685
1686 h2s->mws += inc;
1687 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1688 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001689 if (h2s->send_wait)
1690 LIST_ADDQ(&h2c->send_list, &h2s->list);
1691
Willy Tarreau26f95952017-07-27 17:18:30 +02001692 }
1693 }
1694 else {
1695 /* connection window update */
1696 if (!inc) {
1697 error = H2_ERR_PROTOCOL_ERROR;
1698 goto conn_err;
1699 }
1700
1701 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1702 error = H2_ERR_FLOW_CONTROL_ERROR;
1703 goto conn_err;
1704 }
1705
1706 h2c->mws += inc;
1707 }
1708
1709 return 1;
1710
1711 conn_err:
1712 h2c_error(h2c, error);
1713 return 0;
1714
1715 strm_err:
1716 if (h2s) {
1717 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001718 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001719 }
1720 else
1721 h2c_error(h2c, error);
1722 return 0;
1723}
1724
Willy Tarreaue96b0922017-10-30 00:28:29 +01001725/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1726 * the last ID. Returns > 0 on success or zero on missing data. It may return
1727 * an error in h2c. Described in RFC7540#6.8.
1728 */
1729static int h2c_handle_goaway(struct h2c *h2c)
1730{
1731 int error;
1732 int last;
1733
1734 if (h2c->dsi != 0) {
1735 error = H2_ERR_PROTOCOL_ERROR;
1736 goto conn_err;
1737 }
1738
1739 if (h2c->dfl < 8) {
1740 error = H2_ERR_FRAME_SIZE_ERROR;
1741 goto conn_err;
1742 }
1743
1744 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001745 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001746 return 0;
1747
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001748 last = h2_get_n32(&h2c->dbuf, 0);
1749 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001750 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001751 if (h2c->last_sid < 0)
1752 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001753 return 1;
1754
1755 conn_err:
1756 h2c_error(h2c, error);
1757 return 0;
1758}
1759
Willy Tarreau92153fc2017-12-03 19:46:19 +01001760/* processes a PRIORITY frame, and either skips it or rejects if it is
1761 * invalid. Returns > 0 on success or zero on missing data. It may return
1762 * an error in h2c. Described in RFC7540#6.3.
1763 */
1764static int h2c_handle_priority(struct h2c *h2c)
1765{
1766 int error;
1767
1768 if (h2c->dsi == 0) {
1769 error = H2_ERR_PROTOCOL_ERROR;
1770 goto conn_err;
1771 }
1772
1773 if (h2c->dfl != 5) {
1774 error = H2_ERR_FRAME_SIZE_ERROR;
1775 goto conn_err;
1776 }
1777
1778 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001779 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001780 return 0;
1781
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001782 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001783 /* 7540#5.3 : can't depend on itself */
1784 error = H2_ERR_PROTOCOL_ERROR;
1785 goto conn_err;
1786 }
1787 return 1;
1788
1789 conn_err:
1790 h2c_error(h2c, error);
1791 return 0;
1792}
1793
Willy Tarreaucd234e92017-08-18 10:59:39 +02001794/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1795 * Returns > 0 on success or zero on missing data. It may return an error in
1796 * h2c. Described in RFC7540#6.4.
1797 */
1798static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1799{
1800 int error;
1801
1802 if (h2c->dsi == 0) {
1803 error = H2_ERR_PROTOCOL_ERROR;
1804 goto conn_err;
1805 }
1806
Willy Tarreaucd234e92017-08-18 10:59:39 +02001807 if (h2c->dfl != 4) {
1808 error = H2_ERR_FRAME_SIZE_ERROR;
1809 goto conn_err;
1810 }
1811
1812 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001813 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001814 return 0;
1815
1816 /* late RST, already handled */
1817 if (h2s->st == H2_SS_CLOSED)
1818 return 1;
1819
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001820 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001821 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001822
1823 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001824 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001825 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001826 }
1827
1828 h2s->flags |= H2_SF_RST_RCVD;
1829 return 1;
1830
1831 conn_err:
1832 h2c_error(h2c, error);
1833 return 0;
1834}
1835
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001836/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1837 * It may return an error in h2c or h2s. The caller must consider that the
1838 * return value is the new h2s in case one was allocated (most common case).
1839 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001840 * errors here are reported as connection errors since it's impossible to
1841 * recover from such errors after the compression context has been altered.
1842 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001843static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001844{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001845 struct buffer rxbuf = BUF_NULL;
1846 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001847 int error;
1848
1849 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001850 /* RFC7540#4.2 */
1851 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001852 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001853 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001854 }
1855
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001857 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001860 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001861
Willy Tarreauf2101912018-07-19 10:11:38 +02001862 if (h2c->flags & H2_CF_DEM_TOOMANY)
1863 return 0; // too many cs still present
1864
Willy Tarreau13278b42017-10-13 19:23:14 +02001865 /* now either the frame is complete or the buffer is complete */
1866 if (h2s->st != H2_SS_IDLE) {
1867 /* FIXME: stream already exists, this is only allowed for
1868 * trailers (not supported for now).
1869 */
1870 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001871 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001872 goto conn_err;
1873 }
1874 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1875 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1876 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001877 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001878 goto conn_err;
1879 }
1880
Willy Tarreau25919232019-01-03 14:48:18 +01001881 error = h2c_decode_headers(h2c, &rxbuf, &flags);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001882
Willy Tarreau25919232019-01-03 14:48:18 +01001883 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001884 if (h2c->st0 >= H2_CS_ERROR)
1885 goto out;
1886
Willy Tarreau25919232019-01-03 14:48:18 +01001887 if (error <= 0) {
1888 if (error == 0)
1889 goto out; // missing data
1890
1891 /* Failed to decode this stream (e.g. too large request)
1892 * but the HPACK decompressor is still synchronized.
1893 */
1894 h2s = (struct h2s*)h2_error_stream;
1895 goto send_rst;
1896 }
1897
Willy Tarreau22de8d32018-09-05 19:55:58 +02001898 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001899 * positively from h2c_frt_stream_new(), the stream will report the error,
1900 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001901 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001902 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001903 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001904 h2s = (struct h2s*)h2_refused_stream;
1905 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001906 }
1907
1908 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001909 h2s->rxbuf = rxbuf;
1910 h2s->flags |= flags;
1911
1912 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001913 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001914
1915 if (h2s->flags & H2_SF_ES_RCVD) {
1916 h2s->st = H2_SS_HREM;
Willy Tarreau39d68502018-03-02 12:26:37 +01001917 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001918 }
1919
Willy Tarreau3a429f02019-01-03 11:41:50 +01001920 /* update the max stream ID if the request is being processed */
1921 if (h2s->id > h2c->max_id)
1922 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001924 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001925
1926 conn_err:
1927 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001928 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001929
1930 strm_err:
1931 if (h2s) {
1932 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001933 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001934 }
1935 else
1936 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001937 out:
1938 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001939 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001940
1941 send_rst:
1942 /* make the demux send an RST for the current stream. We may only
1943 * do this if we're certain that the HEADERS frame was properly
1944 * decompressed so that the HPACK decoder is still kept up to date.
1945 */
1946 h2_release_buf(h2c, &rxbuf);
1947 h2c->st0 = H2_CS_FRAME_E;
1948 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001949}
1950
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001951/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1952 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1953 * errors here are reported as connection errors since it's impossible to
1954 * recover from such errors after the compression context has been altered.
1955 */
1956static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1957{
1958 int error;
1959
1960 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001961 /* RFC7540#4.2 */
1962 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001963 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001964 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001965 }
1966
1967 if (!b_size(&h2c->dbuf))
1968 return NULL; // empty buffer
1969
1970 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1971 return NULL; // incomplete frame
1972
Willy Tarreau25919232019-01-03 14:48:18 +01001973 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001974
Willy Tarreau25919232019-01-03 14:48:18 +01001975 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001976 if (h2c->st0 >= H2_CS_ERROR)
1977 return NULL;
1978
Willy Tarreau25919232019-01-03 14:48:18 +01001979 if (error <= 0) {
1980 if (error == 0)
1981 return NULL; // missing data
1982
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001983 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01001984 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001985 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01001986 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001987 }
1988
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01001989 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1990 h2s->flags |= H2_SF_ES_RCVD;
1991 h2s->cs->flags |= CS_FL_REOS;
1992 }
1993
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001994 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1995 h2s->st = H2_SS_ERROR;
1996 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1997 h2s->st = H2_SS_HREM;
1998 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1999 h2s_close(h2s);
2000
2001 return h2s;
2002
2003 conn_err:
2004 h2c_error(h2c, error);
2005 return NULL;
2006
2007 strm_err:
2008 if (h2s) {
2009 h2s_error(h2s, error);
2010 h2c->st0 = H2_CS_FRAME_E;
2011 }
2012 else
2013 h2c_error(h2c, error);
2014 return NULL;
2015}
2016
Willy Tarreau454f9052017-10-26 19:40:35 +02002017/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2018 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2019 */
2020static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2021{
2022 int error;
2023
2024 /* note that empty DATA frames are perfectly valid and sometimes used
2025 * to signal an end of stream (with the ES flag).
2026 */
2027
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002028 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002029 return 0; // empty buffer
2030
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002031 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002032 return 0; // incomplete frame
2033
2034 /* now either the frame is complete or the buffer is complete */
2035
2036 if (!h2c->dsi) {
2037 /* RFC7540#6.1 */
2038 error = H2_ERR_PROTOCOL_ERROR;
2039 goto conn_err;
2040 }
2041
2042 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2043 /* RFC7540#6.1 */
2044 error = H2_ERR_STREAM_CLOSED;
2045 goto strm_err;
2046 }
2047
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002048 if (!h2_frt_transfer_data(h2s))
2049 return 0;
2050
Willy Tarreau454f9052017-10-26 19:40:35 +02002051 /* call the upper layers to process the frame, then let the upper layer
2052 * notify the stream about any change.
2053 */
2054 if (!h2s->cs) {
2055 error = H2_ERR_STREAM_CLOSED;
2056 goto strm_err;
2057 }
2058
Willy Tarreau8f650c32017-11-21 19:36:21 +01002059 if (h2c->st0 >= H2_CS_ERROR)
2060 return 0;
2061
Willy Tarreau721c9742017-11-07 11:05:42 +01002062 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002063 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002064 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002065 }
2066
2067 /* check for completion : the callee will change this to FRAME_A or
2068 * FRAME_H once done.
2069 */
2070 if (h2c->st0 == H2_CS_FRAME_P)
2071 return 0;
2072
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002073
2074 /* last frame */
2075 if (h2c->dff & H2_F_DATA_END_STREAM) {
2076 h2s->st = H2_SS_HREM;
2077 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002078 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002079 }
2080
Willy Tarreau454f9052017-10-26 19:40:35 +02002081 return 1;
2082
2083 conn_err:
2084 h2c_error(h2c, error);
2085 return 0;
2086
2087 strm_err:
2088 if (h2s) {
2089 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002090 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002091 }
2092 else
2093 h2c_error(h2c, error);
2094 return 0;
2095}
2096
Willy Tarreaubc933932017-10-09 16:21:43 +02002097/* process Rx frames to be demultiplexed */
2098static void h2_process_demux(struct h2c *h2c)
2099{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002100 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002101
Willy Tarreau081d4722017-05-16 21:51:05 +02002102 if (h2c->st0 >= H2_CS_ERROR)
2103 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002104
2105 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2106 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002107 if (h2c->flags & H2_CF_IS_BACK)
2108 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002109 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2110 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002111 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002112 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002113 sess_log(h2c->conn->owner);
2114 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002115 goto fail;
2116 }
2117
2118 h2c->max_id = 0;
2119 h2c->st0 = H2_CS_SETTINGS1;
2120 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002121
2122 if (h2c->st0 == H2_CS_SETTINGS1) {
2123 struct h2_fh hdr;
2124
2125 /* ensure that what is pending is a valid SETTINGS frame
2126 * without an ACK.
2127 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002128 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002129 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002130 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002131 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002132 sess_log(h2c->conn->owner);
2133 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002134 goto fail;
2135 }
2136
2137 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2138 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2139 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2140 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002141 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002142 goto fail;
2143 }
2144
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002145 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002146 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2147 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2148 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002149 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002150 goto fail;
2151 }
2152
Willy Tarreau3bf69182018-12-21 15:34:50 +01002153 /* that's OK, switch to FRAME_P to process it. This is
2154 * a SETTINGS frame whose header has already been
2155 * deleted above.
2156 */
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002157 h2c->dfl = hdr.len;
2158 h2c->dsi = hdr.sid;
2159 h2c->dft = hdr.ft;
2160 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002161 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002162 h2c->st0 = H2_CS_FRAME_P;
2163 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002164 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002165
2166 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002167 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002168 int ret = 0;
2169
2170 if (h2c->st0 >= H2_CS_ERROR)
2171 break;
2172
2173 if (h2c->st0 == H2_CS_FRAME_H) {
2174 struct h2_fh hdr;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002175 unsigned int padlen = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002176
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002177 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002178 break;
2179
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002180 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002181 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2182 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002183 if (!h2c->nb_streams) {
2184 /* only log if no other stream can report the error */
2185 sess_log(h2c->conn->owner);
2186 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002187 break;
2188 }
2189
Willy Tarreau3bf69182018-12-21 15:34:50 +01002190 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2191 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2192 * we read the pad length and drop it from the remaining
2193 * payload (one byte + the 9 remaining ones = 10 total
2194 * removed), so we have a frame payload starting after the
2195 * pad len. Flow controlled frames (DATA) also count the
2196 * padlen in the flow control, so it must be adjusted.
2197 */
2198 if (hdr.len < 1) {
2199 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2200 sess_log(h2c->conn->owner);
2201 goto fail;
2202 }
2203 hdr.len--;
2204
2205 if (b_data(&h2c->dbuf) < 10)
2206 break; // missing padlen
2207
2208 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2209
2210 if (padlen > hdr.len) {
2211 /* RFC7540#6.1 : pad length = length of
2212 * frame payload or greater => error.
2213 */
2214 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2215 sess_log(h2c->conn->owner);
2216 goto fail;
2217 }
2218
2219 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2220 h2c->rcvd_c++;
2221 h2c->rcvd_s++;
2222 }
2223 b_del(&h2c->dbuf, 1);
2224 }
2225 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002226 h2c->dfl = hdr.len;
2227 h2c->dsi = hdr.sid;
2228 h2c->dft = hdr.ft;
2229 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002230 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002231 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002232 }
2233
2234 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002235 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2236
Willy Tarreau567beb82018-12-18 16:52:44 +01002237 if (tmp_h2s != h2s && h2s && h2s->cs &&
2238 (b_data(&h2s->rxbuf) ||
2239 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002240 /* we may have to signal the upper layers */
2241 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002242 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002243 }
2244 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002245
Willy Tarreaud7901432017-12-29 11:34:40 +01002246 if (h2c->st0 == H2_CS_FRAME_E)
2247 goto strm_err;
2248
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002249 if (h2s->st == H2_SS_IDLE &&
2250 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2251 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2252 * this state MUST be treated as a connection error
2253 */
2254 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2255 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002256 if (!h2c->nb_streams) {
2257 /* only log if no other stream can report the error */
2258 sess_log(h2c->conn->owner);
2259 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002260 break;
2261 }
2262
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002263 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2264 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2265 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2266 * this state MUST be treated as a stream error
2267 */
2268 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2269 goto strm_err;
2270 }
2271
Willy Tarreauab837502017-12-27 15:07:30 +01002272 /* Below the management of frames received in closed state is a
2273 * bit hackish because the spec makes strong differences between
2274 * streams closed by receiving RST, sending RST, and seeing ES
2275 * in both directions. In addition to this, the creation of a
2276 * new stream reusing the identifier of a closed one will be
2277 * detected here. Given that we cannot keep track of all closed
2278 * streams forever, we consider that unknown closed streams were
2279 * closed on RST received, which allows us to respond with an
2280 * RST without breaking the connection (eg: to abort a transfer).
2281 * Some frames have to be silently ignored as well.
2282 */
2283 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2284 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2285 /* #5.1.1: The identifier of a newly
2286 * established stream MUST be numerically
2287 * greater than all streams that the initiating
2288 * endpoint has opened or reserved. This
2289 * governs streams that are opened using a
2290 * HEADERS frame and streams that are reserved
2291 * using PUSH_PROMISE. An endpoint that
2292 * receives an unexpected stream identifier
2293 * MUST respond with a connection error.
2294 */
2295 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2296 goto strm_err;
2297 }
2298
2299 if (h2s->flags & H2_SF_RST_RCVD) {
2300 /* RFC7540#5.1:closed: an endpoint that
2301 * receives any frame other than PRIORITY after
2302 * receiving a RST_STREAM MUST treat that as a
2303 * stream error of type STREAM_CLOSED.
2304 *
2305 * Note that old streams fall into this category
2306 * and will lead to an RST being sent.
2307 */
2308 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2309 h2c->st0 = H2_CS_FRAME_E;
2310 goto strm_err;
2311 }
2312
2313 /* RFC7540#5.1:closed: if this state is reached as a
2314 * result of sending a RST_STREAM frame, the peer that
2315 * receives the RST_STREAM might have already sent
2316 * frames on the stream that cannot be withdrawn. An
2317 * endpoint MUST ignore frames that it receives on
2318 * closed streams after it has sent a RST_STREAM
2319 * frame. An endpoint MAY choose to limit the period
2320 * over which it ignores frames and treat frames that
2321 * arrive after this time as being in error.
2322 */
2323 if (!(h2s->flags & H2_SF_RST_SENT)) {
2324 /* RFC7540#5.1:closed: any frame other than
2325 * PRIO/WU/RST in this state MUST be treated as
2326 * a connection error
2327 */
2328 if (h2c->dft != H2_FT_RST_STREAM &&
2329 h2c->dft != H2_FT_PRIORITY &&
2330 h2c->dft != H2_FT_WINDOW_UPDATE) {
2331 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2332 goto strm_err;
2333 }
2334 }
2335 }
2336
Willy Tarreauc0da1962017-10-30 18:38:00 +01002337#if 0
2338 // problem below: it is not possible to completely ignore such
2339 // streams as we need to maintain the compression state as well
2340 // and for this we need to completely process these frames (eg:
2341 // HEADERS frames) as well as counting DATA frames to emit
2342 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2343 // This is a typical case of layer violation where the
2344 // transported contents are critical to the connection's
2345 // validity and must be ignored at the same time :-(
2346
2347 /* graceful shutdown, ignore streams whose ID is higher than
2348 * the one advertised in GOAWAY. RFC7540#6.8.
2349 */
2350 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002351 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2352 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002353 h2c->dfl -= ret;
2354 ret = h2c->dfl == 0;
2355 goto strm_err;
2356 }
2357#endif
2358
Willy Tarreau7e98c052017-10-10 15:56:59 +02002359 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002360 case H2_FT_SETTINGS:
2361 if (h2c->st0 == H2_CS_FRAME_P)
2362 ret = h2c_handle_settings(h2c);
2363
2364 if (h2c->st0 == H2_CS_FRAME_A)
2365 ret = h2c_ack_settings(h2c);
2366 break;
2367
Willy Tarreaucf68c782017-10-10 17:11:41 +02002368 case H2_FT_PING:
2369 if (h2c->st0 == H2_CS_FRAME_P)
2370 ret = h2c_handle_ping(h2c);
2371
2372 if (h2c->st0 == H2_CS_FRAME_A)
2373 ret = h2c_ack_ping(h2c);
2374 break;
2375
Willy Tarreau26f95952017-07-27 17:18:30 +02002376 case H2_FT_WINDOW_UPDATE:
2377 if (h2c->st0 == H2_CS_FRAME_P)
2378 ret = h2c_handle_window_update(h2c, h2s);
2379 break;
2380
Willy Tarreau61290ec2017-10-17 08:19:21 +02002381 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002382 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2383 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2384 * frames' parsers consume all following CONTINUATION
2385 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002386 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002387 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2388 sess_log(h2c->conn->owner);
2389 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002390
Willy Tarreau13278b42017-10-13 19:23:14 +02002391 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002392 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002393 if (h2c->flags & H2_CF_IS_BACK)
2394 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2395 else
2396 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002397 if (tmp_h2s) {
2398 h2s = tmp_h2s;
2399 ret = 1;
2400 }
2401 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002402 break;
2403
Willy Tarreau454f9052017-10-26 19:40:35 +02002404 case H2_FT_DATA:
2405 if (h2c->st0 == H2_CS_FRAME_P)
2406 ret = h2c_frt_handle_data(h2c, h2s);
2407
2408 if (h2c->st0 == H2_CS_FRAME_A)
2409 ret = h2c_send_strm_wu(h2c);
2410 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002411
Willy Tarreau92153fc2017-12-03 19:46:19 +01002412 case H2_FT_PRIORITY:
2413 if (h2c->st0 == H2_CS_FRAME_P)
2414 ret = h2c_handle_priority(h2c);
2415 break;
2416
Willy Tarreaucd234e92017-08-18 10:59:39 +02002417 case H2_FT_RST_STREAM:
2418 if (h2c->st0 == H2_CS_FRAME_P)
2419 ret = h2c_handle_rst_stream(h2c, h2s);
2420 break;
2421
Willy Tarreaue96b0922017-10-30 00:28:29 +01002422 case H2_FT_GOAWAY:
2423 if (h2c->st0 == H2_CS_FRAME_P)
2424 ret = h2c_handle_goaway(h2c);
2425 break;
2426
Willy Tarreau1c661982017-10-30 13:52:01 +01002427 case H2_FT_PUSH_PROMISE:
2428 /* not permitted here, RFC7540#5.1 */
2429 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002430 if (!h2c->nb_streams) {
2431 /* only log if no other stream can report the error */
2432 sess_log(h2c->conn->owner);
2433 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002434 break;
2435
2436 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002437 default:
2438 /* drop frames that we ignore. They may be larger than
2439 * the buffer so we drain all of their contents until
2440 * we reach the end.
2441 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002442 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2443 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002444 h2c->dfl -= ret;
2445 ret = h2c->dfl == 0;
2446 }
2447
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002448 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002449 /* We may have to send an RST if not done yet */
2450 if (h2s->st == H2_SS_ERROR)
2451 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002452
Willy Tarreaua20a5192017-12-27 11:02:06 +01002453 if (h2c->st0 == H2_CS_FRAME_E)
2454 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002455
Willy Tarreau7e98c052017-10-10 15:56:59 +02002456 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002457 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002458 break;
2459
2460 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002461 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002462 h2c->st0 = H2_CS_FRAME_H;
2463 }
2464 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002465
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002466 if (h2c->rcvd_c > 0 &&
2467 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2468 h2c_send_conn_wu(h2c);
2469
Willy Tarreau52eed752017-09-22 15:05:09 +02002470 fail:
2471 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002472 if (h2s && h2s->cs &&
2473 (b_data(&h2s->rxbuf) ||
2474 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002475 /* we may have to signal the upper layers */
2476 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002477 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002478 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002479
Willy Tarreau47b515a2018-12-21 16:09:41 +01002480 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002481}
2482
2483/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2484 * the end.
2485 */
2486static int h2_process_mux(struct h2c *h2c)
2487{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002488 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002489
Willy Tarreau01b44822018-10-03 14:26:37 +02002490 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2491 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2492 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2493 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2494 if (h2c->st0 == H2_CS_ERROR) {
2495 h2c->st0 = H2_CS_ERROR2;
2496 sess_log(h2c->conn->owner);
2497 }
2498 goto fail;
2499 }
2500 h2c->st0 = H2_CS_SETTINGS1;
2501 }
2502 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002503 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002504 return 1;
2505 }
2506
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002507 /* start by sending possibly pending window updates */
2508 if (h2c->rcvd_c > 0 &&
2509 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2510 h2c_send_conn_wu(h2c) < 0)
2511 goto fail;
2512
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002513 /* First we always process the flow control list because the streams
2514 * waiting there were already elected for immediate emission but were
2515 * blocked just on this.
2516 */
2517
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002518 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002519 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2520 h2c->st0 >= H2_CS_ERROR)
2521 break;
2522
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002523 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002524 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2525 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002526 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002527 LIST_DEL(&h2s->list);
2528 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002529 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002530 }
2531
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002532 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002533 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2534 break;
2535
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002536 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002537 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2538 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002539 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002540 LIST_DEL(&h2s->list);
2541 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002542 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002543 }
2544
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002545 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002546 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002547 if (h2c->st0 == H2_CS_ERROR) {
2548 if (h2c->max_id >= 0) {
2549 h2c_send_goaway_error(h2c, NULL);
2550 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2551 return 0;
2552 }
2553
2554 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2555 }
2556 return 1;
2557 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002558 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002559}
2560
Willy Tarreau62f52692017-10-08 23:01:42 +02002561
Willy Tarreau479998a2018-11-18 06:30:59 +01002562/* Attempt to read data, and subscribe if none available.
2563 * The function returns 1 if data has been received, otherwise zero.
2564 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002565static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002566{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002567 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002568 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002569 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002570 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002571
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002572 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002573 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002574
Willy Tarreau315d8072017-12-10 22:17:57 +01002575 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002576 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002577
Willy Tarreau44e973f2018-03-01 17:49:30 +01002578 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002579 if (!buf) {
2580 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002581 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002582 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002583
Olivier Houchard7505f942018-08-21 18:10:44 +02002584 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002585 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002586 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2587 /* HTX in use : try to pre-align the buffer like the
2588 * rxbufs will be to optimize memory copies. We'll make
2589 * sure that the frame header lands at the end of the
2590 * HTX block to alias it upon recv. We cannot use the
2591 * head because rcv_buf() will realign the buffer if
2592 * it's empty. Thus we cheat and pretend we already
2593 * have a few bytes there.
2594 */
2595 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002596 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002597 }
2598 else
2599 max = b_room(buf);
2600
Olivier Houchard7505f942018-08-21 18:10:44 +02002601 if (max)
2602 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2603 else
2604 ret = 0;
2605 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002606
Olivier Houchard53216e72018-10-10 15:46:36 +02002607 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002608 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002609
Olivier Houcharda1411e62018-08-17 18:42:48 +02002610 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002611 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002612 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002613 }
2614
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002615 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002616 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002617 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002618}
2619
Willy Tarreau479998a2018-11-18 06:30:59 +01002620/* Try to send data if possible.
2621 * The function returns 1 if data have been sent, otherwise zero.
2622 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002623static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002624{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002625 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002626 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002627 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002628
2629 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002630 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002631
Olivier Houchard7505f942018-08-21 18:10:44 +02002632
Willy Tarreaua2af5122017-10-09 11:56:46 +02002633 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2634 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002635 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002636 }
2637
Willy Tarreaubc933932017-10-09 16:21:43 +02002638 /* This loop is quite simple : it tries to fill as much as it can from
2639 * pending streams into the existing buffer until it's reportedly full
2640 * or the end of send requests is reached. Then it tries to send this
2641 * buffer's contents out, marks it not full if at least one byte could
2642 * be sent, and tries again.
2643 *
2644 * The snd_buf() function normally takes a "flags" argument which may
2645 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2646 * data immediately comes and CO_SFL_STREAMER to indicate that the
2647 * connection is streaming lots of data (used to increase TLS record
2648 * size at the expense of latency). The former can be sent any time
2649 * there's a buffer full flag, as it indicates at least one stream
2650 * attempted to send and failed so there are pending data. An
2651 * alternative would be to set it as long as there's an active stream
2652 * but that would be problematic for ACKs until we have an absolute
2653 * guarantee that all waiters have at least one byte to send. The
2654 * latter should possibly not be set for now.
2655 */
2656
2657 done = 0;
2658 while (!done) {
2659 unsigned int flags = 0;
2660
2661 /* fill as much as we can into the current buffer */
2662 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2663 done = h2_process_mux(h2c);
2664
2665 if (conn->flags & CO_FL_ERROR)
2666 break;
2667
2668 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2669 flags |= CO_SFL_MSG_MORE;
2670
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002671 if (b_data(&h2c->mbuf)) {
2672 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002673 if (!ret)
2674 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002675 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002676 b_del(&h2c->mbuf, ret);
2677 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002678 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002679
2680 /* wrote at least one byte, the buffer is not full anymore */
2681 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2682 }
2683
Willy Tarreaua2af5122017-10-09 11:56:46 +02002684 if (conn->flags & CO_FL_SOCK_WR_SH) {
2685 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002686 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002687 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002688 /* We're not full anymore, so we can wake any task that are waiting
2689 * for us.
2690 */
2691 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002692 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002693 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2694 struct h2s *, list);
2695 LIST_DEL(&h2s->list);
2696 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002697 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002698 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2699 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002700 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002701 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002702 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002703 /* We're done, no more to send */
2704 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002705 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002706schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002707 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2708 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002709 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002710}
2711
2712static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2713{
2714 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002715 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002716
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002717 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002718 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002719 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002720 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002721 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002722 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002723 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002724}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002725
Willy Tarreau62f52692017-10-08 23:01:42 +02002726/* callback called on any event by the connection handler.
2727 * It applies changes and returns zero, or < 0 if it wants immediate
2728 * destruction of the connection (which normally doesn not happen in h2).
2729 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002730static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002731{
Olivier Houchard7505f942018-08-21 18:10:44 +02002732 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002733
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002734 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002735 h2_process_demux(h2c);
2736
2737 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002738 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002739
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002740 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002741 h2c->flags &= ~H2_CF_DEM_DFULL;
2742 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002743 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002744
Willy Tarreau0b37d652018-10-03 10:33:02 +02002745 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002746 /* frontend is stopping, reload likely in progress, let's try
2747 * to announce a graceful shutdown if not yet done. We don't
2748 * care if it fails, it will be tried again later.
2749 */
2750 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2751 if (h2c->last_sid < 0)
2752 h2c->last_sid = (1U << 31) - 1;
2753 h2c_send_goaway_error(h2c, NULL);
2754 }
2755 }
2756
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002757 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002758 * If we received early data, and the handshake is done, wake
2759 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002760 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002761 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2762 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2763 struct eb32_node *node;
2764 struct h2s *h2s;
2765
2766 h2c->flags |= H2_CF_WAIT_FOR_HS;
2767 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2768
2769 while (node) {
2770 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002771 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002772 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002773 node = eb32_next(node);
2774 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002775 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002776
Willy Tarreau26bd7612017-10-09 16:47:04 +02002777 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002778 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2779 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2780 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002781 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002782
2783 if (eb_is_empty(&h2c->streams_by_id)) {
2784 /* no more stream, kill the connection now */
2785 h2_release(conn);
2786 return -1;
2787 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002788 }
2789
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002790 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002791 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002792
Olivier Houchard53216e72018-10-10 15:46:36 +02002793 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2794 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2795 (h2c->st0 != H2_CS_ERROR &&
2796 !b_data(&h2c->mbuf) &&
2797 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2798 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002799 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002800
Willy Tarreau3f133572017-10-31 19:21:06 +01002801 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002802 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002803 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002804 task_queue(h2c->task);
2805 }
2806 else
2807 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002808 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002809
Olivier Houchard7505f942018-08-21 18:10:44 +02002810 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002811 return 0;
2812}
2813
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002814static int h2_wake(struct connection *conn)
2815{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002816 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002817
2818 return (h2_process(h2c));
2819}
2820
Willy Tarreauea392822017-10-31 10:02:25 +01002821/* Connection timeout management. The principle is that if there's no receipt
2822 * nor sending for a certain amount of time, the connection is closed. If the
2823 * MUX buffer still has lying data or is not allocatable, the connection is
2824 * immediately killed. If it's allocatable and empty, we attempt to send a
2825 * GOAWAY frame.
2826 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002827static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002828{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002829 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002830 int expired = tick_is_expired(t->expire, now_ms);
2831
Willy Tarreau0975f112018-03-29 15:22:59 +02002832 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002833 return t;
2834
Willy Tarreau0975f112018-03-29 15:22:59 +02002835 task_delete(t);
2836 task_free(t);
2837
2838 if (!h2c) {
2839 /* resources were already deleted */
2840 return NULL;
2841 }
2842
2843 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002844 h2c_error(h2c, H2_ERR_NO_ERROR);
2845 h2_wake_some_streams(h2c, 0, 0);
2846
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002848 /* don't even try to send a GOAWAY, the buffer is stuck */
2849 h2c->flags |= H2_CF_GOAWAY_FAILED;
2850 }
2851
2852 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002853 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002854 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2855 h2c->flags |= H2_CF_GOAWAY_FAILED;
2856
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002857 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2858 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002859 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002860 b_del(&h2c->mbuf, ret);
2861 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002862 }
2863 }
Willy Tarreauea392822017-10-31 10:02:25 +01002864
Willy Tarreau0975f112018-03-29 15:22:59 +02002865 /* either we can release everything now or it will be done later once
2866 * the last stream closes.
2867 */
2868 if (eb_is_empty(&h2c->streams_by_id))
2869 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002870
Willy Tarreauea392822017-10-31 10:02:25 +01002871 return NULL;
2872}
2873
2874
Willy Tarreau62f52692017-10-08 23:01:42 +02002875/*******************************************/
2876/* functions below are used by the streams */
2877/*******************************************/
2878
2879/*
2880 * Attach a new stream to a connection
2881 * (Used for outgoing connections)
2882 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002883static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002884{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002885 struct conn_stream *cs;
2886 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002887 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002888
2889 cs = cs_new(conn);
2890 if (!cs)
2891 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002892 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002893 if (!h2s) {
2894 cs_free(cs);
2895 return NULL;
2896 }
2897 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002898}
2899
Willy Tarreaufafd3982018-11-18 21:29:20 +01002900/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2901 * We have to scan because we may have some orphan streams. It might be
2902 * beneficial to scan backwards from the end to reduce the likeliness to find
2903 * orphans.
2904 */
2905static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2906{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002907 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002908 struct h2s *h2s;
2909 struct eb32_node *node;
2910
2911 node = eb32_first(&h2c->streams_by_id);
2912 while (node) {
2913 h2s = container_of(node, struct h2s, by_id);
2914 if (h2s->cs)
2915 return h2s->cs;
2916 node = eb32_next(node);
2917 }
2918 return NULL;
2919}
2920
Willy Tarreau62f52692017-10-08 23:01:42 +02002921/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002922 * Destroy the mux and the associated connection, if it is no longer used
2923 */
2924static void h2_destroy(struct connection *conn)
2925{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002926 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002927
2928 if (eb_is_empty(&h2c->streams_by_id))
2929 h2_release(h2c->conn);
2930}
2931
2932/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002933 * Detach the stream from the connection and possibly release the connection.
2934 */
2935static void h2_detach(struct conn_stream *cs)
2936{
Willy Tarreau60935142017-10-16 18:11:19 +02002937 struct h2s *h2s = cs->ctx;
2938 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002939 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002940
2941 cs->ctx = NULL;
2942 if (!h2s)
2943 return;
2944
Olivier Houchardf502aca2018-12-14 19:42:40 +01002945 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002946 h2c = h2s->h2c;
2947 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002948 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002949 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2950 !h2_has_too_many_cs(h2c)) {
2951 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002952 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002953 }
Willy Tarreau60935142017-10-16 18:11:19 +02002954
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002955 /* this stream may be blocked waiting for some data to leave (possibly
2956 * an ES or RST frame), so orphan it in this case.
2957 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002958 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002959 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002960 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002961 return;
2962
Willy Tarreau45f752e2017-10-30 15:44:59 +01002963 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2964 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2965 /* unblock the connection if it was blocked on this
2966 * stream.
2967 */
2968 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2969 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002970 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002971 }
2972
Willy Tarreau71049cc2018-03-28 13:56:39 +02002973 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002974
Olivier Houchard8a786902018-12-15 16:05:40 +01002975 if (h2c->flags & H2_CF_IS_BACK &&
2976 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002977 if (!(h2c->conn->flags &
2978 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2979 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002980 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002981 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2982 h2c->conn->owner = NULL;
2983 if (eb_is_empty(&h2c->streams_by_id)) {
2984 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2985 /* The server doesn't want it, let's kill the connection right away */
2986 h2c->conn->mux->destroy(h2c->conn);
2987 return;
2988 }
2989 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002990 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002991 if (eb_is_empty(&h2c->streams_by_id)) {
2992 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2993 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2994 return;
2995 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002996 /* Never ever allow to reuse a connection from a non-reuse backend */
2997 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2998 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01002999 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003000 struct server *srv = objt_server(h2c->conn->target);
3001
3002 if (srv) {
3003 if (h2c->conn->flags & CO_FL_PRIVATE)
3004 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3005 else
3006 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3007 }
3008
3009 }
3010 }
3011 }
3012
Willy Tarreaue323f342018-03-28 13:51:45 +02003013 /* We don't want to close right now unless we're removing the
3014 * last stream, and either the connection is in error, or it
3015 * reached the ID already specified in a GOAWAY frame received
3016 * or sent (as seen by last_sid >= 0).
3017 */
3018 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3019 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003020 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003021 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003022 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003023 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003024 (conn_xprt_read0_pending(h2c->conn) ||
3025 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3026 /* no more stream will come, kill it now */
3027 h2_release(h2c->conn);
3028 }
3029 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003030 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003031 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3032 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003033 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003034 else
3035 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003036 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003037}
3038
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003039static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003040{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003041 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003042 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003043
Willy Tarreau721c9742017-11-07 11:05:42 +01003044 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003045 return;
3046
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003047 /* if no outgoing data was seen on this stream, it means it was
3048 * closed with a "tcp-request content" rule that is normally
3049 * used to kill the connection ASAP (eg: limit abuse). In this
3050 * case we send a goaway to close the connection.
3051 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003052 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003053 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003054 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003055
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003056 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3057 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003058 h2c_send_goaway_error(h2c, h2s) <= 0)
3059 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003060
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003061 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003062 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003063 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003064
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003065 return;
3066add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003067 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003068 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003069 if (h2s->flags & H2_SF_BLK_MFCTL) {
3070 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3071 h2s->send_wait = sw;
3072 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3073 h2s->send_wait = sw;
3074 LIST_ADDQ(&h2c->send_list, &h2s->list);
3075 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003076 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003077 /* Let the handler know we want shutr */
3078 sw->handle = (void *)((long)sw->handle | 1);
3079
Willy Tarreau62f52692017-10-08 23:01:42 +02003080}
3081
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003082static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003083{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003084 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003085 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003086
Willy Tarreau721c9742017-11-07 11:05:42 +01003087 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003088 return;
3089
Willy Tarreau67434202017-11-06 20:20:51 +01003090 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003091 /* we can cleanly close using an empty data frame only after headers */
3092
3093 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3094 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003095 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003096
3097 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003098 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003099 else
3100 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003101 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003102 /* if no outgoing data was seen on this stream, it means it was
3103 * closed with a "tcp-request content" rule that is normally
3104 * used to kill the connection ASAP (eg: limit abuse). In this
3105 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003106 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003107 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003108 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003109 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003110
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003111 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3112 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003113 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003114 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003115
Willy Tarreau00dd0782018-03-01 16:31:34 +01003116 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003117 }
3118
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003119 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003120 tasklet_wakeup(h2c->wait_event.task);
3121 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003122
3123 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003124 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003125 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003126 if (h2s->flags & H2_SF_BLK_MFCTL) {
3127 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3128 h2s->send_wait = sw;
3129 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3130 h2s->send_wait = sw;
3131 LIST_ADDQ(&h2c->send_list, &h2s->list);
3132 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003133 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003134 /* let the handler know we want to shutw */
3135 sw->handle = (void *)((long)(sw->handle) | 2);
3136
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003137}
3138
3139static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3140{
3141 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003142 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003143
Olivier Houchard2c68a462018-12-15 22:42:20 +01003144 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003145 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003146 h2s->send_wait = NULL;
3147 LIST_DEL(&h2s->list);
3148 LIST_INIT(&h2s->list);
3149 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003150 if (reason & 2)
3151 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003152 if (reason & 1)
3153 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003154
Olivier Houchard2c68a462018-12-15 22:42:20 +01003155 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003156 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003157 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003158 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003159}
3160
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003161static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3162{
3163 struct h2s *h2s = cs->ctx;
3164
3165 if (!mode)
3166 return;
3167
3168 h2_do_shutr(h2s);
3169}
3170
3171static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3172{
3173 struct h2s *h2s = cs->ctx;
3174
3175 h2_do_shutw(h2s);
3176}
3177
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003178/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003179 * HTX request or response depending on the connection's side. Returns a
3180 * positive value on success, a negative value on failure, or 0 if it couldn't
3181 * proceed. May report connection errors in h2c->errcode if the frame is
3182 * non-decodable and the connection unrecoverable. In absence of connection
3183 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003184 *
3185 * The function may fold CONTINUATION frames into the initial HEADERS frame
3186 * by removing padding and next frame header, then moving the CONTINUATION
3187 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3188 * leaving a hole between the main frame and the beginning of the next one.
3189 * The possibly remaining incomplete or next frame at the end may be moved
3190 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3191 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3192 *
3193 * A buffer at the beginning of processing may look like this :
3194 *
3195 * ,---.---------.-----.--------------.--------------.------.---.
3196 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3197 * `---^---------^-----^--------------^--------------^------^---'
3198 * | | <-----> | |
3199 * area | dpl | wrap
3200 * |<--------------> |
3201 * | dfl |
3202 * |<-------------------------------------------------->|
3203 * head data
3204 *
3205 * Padding is automatically overwritten when folding, participating to the
3206 * hole size after dfl :
3207 *
3208 * ,---.------------------------.-----.--------------.------.---.
3209 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3210 * `---^------------------------^-----^--------------^------^---'
3211 * | | <-----> | |
3212 * area | hole | wrap
3213 * |<-----------------------> |
3214 * | dfl |
3215 * |<-------------------------------------------------->|
3216 * head data
3217 *
3218 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3219 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3220 * bit.
Willy Tarreau13278b42017-10-13 19:23:14 +02003221 */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003222static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02003223{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003224 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003225 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003226 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003227 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003228 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003229 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003230 int flen; // header frame len
3231 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003232 int ret = 0;
3233 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003234 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003235 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003236
Willy Tarreauea18f862018-12-22 20:19:26 +01003237next_frame:
3238 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3239 goto leave; // incomplete input frame
3240
3241 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3242 * this case, we'll try to paste it immediately after the initial
3243 * HEADERS frame payload and kill any possible padding. The initial
3244 * frame's length will be increased to represent the concatenation
3245 * of the two frames. The next frame is read from position <tlen>
3246 * and written at position <flen> (minus padding if some is present).
3247 */
3248 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3249 struct h2_fh hdr;
3250 int clen; // CONTINUATION frame's payload length
3251
3252 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3253 /* no more data, the buffer may be full, either due to
3254 * too large a frame or because of too large a hole that
3255 * we're going to compact at the end.
3256 */
3257 goto leave;
3258 }
3259
3260 if (hdr.ft != H2_FT_CONTINUATION) {
3261 /* RFC7540#6.10: frame of unexpected type */
3262 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3263 goto fail;
3264 }
3265
3266 if (hdr.sid != h2c->dsi) {
3267 /* RFC7540#6.10: frame of different stream */
3268 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3269 goto fail;
3270 }
3271
3272 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3273 /* RFC7540#4.2: invalid frame length */
3274 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3275 goto fail;
3276 }
3277
3278 /* detect when we must stop aggragating frames */
3279 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3280
3281 /* Take as much as we can of the CONTINUATION frame's payload */
3282 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3283 if (clen > hdr.len)
3284 clen = hdr.len;
3285
3286 /* Move the frame's payload over the padding, hole and frame
3287 * header. At least one of hole or dpl is null (see diagrams
3288 * above). The hole moves after the new aggragated frame.
3289 */
3290 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3291 h2c->dfl += clen - h2c->dpl;
3292 hole += h2c->dpl + 9;
3293 h2c->dpl = 0;
3294 goto next_frame;
3295 }
3296
3297 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003298
Willy Tarreau13278b42017-10-13 19:23:14 +02003299 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003300 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003301 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003302 copy = alloc_trash_chunk();
3303 if (!copy) {
3304 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3305 goto fail;
3306 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003307 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3308 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3309 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003310 }
3311
Willy Tarreau13278b42017-10-13 19:23:14 +02003312 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3313 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003314 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003315 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3316 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003317 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003318 }
3319
Willy Tarreau13278b42017-10-13 19:23:14 +02003320 hdrs += 5; // stream dep = 4, weight = 1
3321 flen -= 5;
3322 }
3323
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003324 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003325 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003326 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003327 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003328
Willy Tarreau937f7602018-02-26 15:22:17 +01003329 /* we can't retry a failed decompression operation so we must be very
3330 * careful not to take any risks. In practice the output buffer is
3331 * always empty except maybe for trailers, in which case we simply have
3332 * to wait for the upper layer to finish consuming what is available.
3333 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003334
3335 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003336 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003337 if (!htx_is_empty(htx)) {
3338 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003339 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003340 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003341 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003342 if (b_data(rxbuf)) {
3343 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003344 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003345 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003346
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003347 rxbuf->head = 0;
3348 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003349 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003350
Willy Tarreau25919232019-01-03 14:48:18 +01003351 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003352 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3353 sizeof(list)/sizeof(list[0]), tmp);
3354 if (outlen < 0) {
3355 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3356 goto fail;
3357 }
3358
Willy Tarreau25919232019-01-03 14:48:18 +01003359 /* The PACK decompressor was updated, let's update the input buffer and
3360 * the parser's state to commit these changes and allow us to later
3361 * fail solely on the stream if needed.
3362 */
3363 b_del(&h2c->dbuf, h2c->dfl + hole);
3364 h2c->dfl = hole = 0;
3365 h2c->st0 = H2_CS_FRAME_H;
3366
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003367 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003368 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003369
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003370 if (htx) {
3371 /* HTX mode */
3372 if (h2c->flags & H2_CF_IS_BACK)
3373 outlen = h2_make_htx_response(list, htx, &msgf);
3374 else
3375 outlen = h2_make_htx_request(list, htx, &msgf);
3376 } else {
3377 /* HTTP/1 mode */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003378 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf);
Willy Tarreau83195932019-01-03 10:26:23 +01003379 if (outlen > 0)
3380 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003381 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003382
3383 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003384 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003385 goto fail;
3386 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003387
Willy Tarreau174b06a2018-04-25 18:13:58 +02003388 if (msgf & H2_MSGF_BODY) {
3389 /* a payload is present */
3390 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003391 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003392 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003393 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003394 }
3395
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003396 if (htx && h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreaub30d0f92019-01-03 14:50:36 +01003397 if (!htx_add_endof(htx, HTX_BLK_EOM))
3398 goto fail;
Willy Tarreau937f7602018-02-26 15:22:17 +01003399
Willy Tarreau86277d42019-01-02 15:36:11 +01003400 /* success */
3401 ret = 1;
3402
Willy Tarreau68dd9852017-07-03 14:44:26 +02003403 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003404 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003405 * move the remaining data over it.
3406 */
3407 if (hole) {
3408 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3409 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3410 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3411 b_sub(&h2c->dbuf, hole);
3412 }
3413
3414 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3415 /* too large frames */
3416 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003417 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003418 }
3419
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003420 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003421 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003422 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003423 return ret;
3424
Willy Tarreau68dd9852017-07-03 14:44:26 +02003425 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003426 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003427 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003428}
3429
Willy Tarreau454f9052017-10-26 19:40:35 +02003430/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3431 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3432 * in use, a new chunk is emitted for each frame. This is supposed to fit
3433 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3434 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3435 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003436 * parser state is automatically updated. Returns > 0 if it could completely
3437 * send the current frame, 0 if it couldn't complete, in which case
3438 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3439 * DATA frame can return 0 as a valid result). Stream errors are reported in
3440 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3441 * have checked the frame header and ensured that the frame was complete or the
3442 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003443 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003444static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003445{
3446 struct h2c *h2c = h2s->h2c;
3447 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003448 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003449 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003450 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003451 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003452
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003453 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003454
Olivier Houchard638b7992018-08-16 15:41:52 +02003455 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003456 if (!csbuf) {
3457 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003458 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003459 }
3460
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003461try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003462 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003463 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3464 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003465 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003466 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003467
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003468 if (flen > b_data(&h2c->dbuf)) {
3469 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003470 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003471 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003472 }
3473
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003474 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003475 block1 = htx_free_data_space(htx);
3476 if (!block1) {
3477 h2c->flags |= H2_CF_DEM_SFULL;
3478 goto fail;
3479 }
3480 if (flen > block1)
3481 flen = block1;
3482
3483 /* here, flen is the max we can copy into the output buffer */
3484 block1 = b_contig_data(&h2c->dbuf, 0);
3485 if (flen > block1)
3486 flen = block1;
3487
3488 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3489 h2c->flags |= H2_CF_DEM_SFULL;
3490 goto fail;
3491 }
3492
3493 b_del(&h2c->dbuf, flen);
3494 h2c->dfl -= flen;
3495 h2c->rcvd_c += flen;
3496 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3497 goto try_again;
3498 }
3499 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003500 /* it doesn't fit and the buffer is fragmented,
3501 * so let's defragment it and try again.
3502 */
3503 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003504 }
3505
Willy Tarreaueba10f22018-04-25 20:44:22 +02003506 /* chunked-encoding requires more room */
3507 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003508 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003509 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3510 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3511 (chklen < 1048576) ? 4 : 8;
3512 chklen += 4; // CRLF, CRLF
3513 }
3514
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003515 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003516 if (flen + chklen > b_room(csbuf)) {
3517 if (chklen >= b_room(csbuf)) {
3518 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003519 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003520 }
3521 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003522 }
3523
3524 if (h2s->flags & H2_SF_DATA_CHNK) {
3525 /* emit the chunk size */
3526 unsigned int chksz = flen;
3527 char str[10];
3528 char *beg;
3529
3530 beg = str + sizeof(str);
3531 *--beg = '\n';
3532 *--beg = '\r';
3533 do {
3534 *--beg = hextab[chksz & 0xF];
3535 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003536 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003537 }
3538
Willy Tarreau454f9052017-10-26 19:40:35 +02003539 /* Block1 is the length of the first block before the buffer wraps,
3540 * block2 is the optional second block to reach the end of the frame.
3541 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003542 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003543 if (block1 > flen)
3544 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003545 block2 = flen - block1;
3546
3547 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003548 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003549
3550 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003551 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003552
Willy Tarreaueba10f22018-04-25 20:44:22 +02003553 if (h2s->flags & H2_SF_DATA_CHNK) {
3554 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003555 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003556 }
3557
Willy Tarreau454f9052017-10-26 19:40:35 +02003558 /* now mark the input data as consumed (will be deleted from the buffer
3559 * by the caller when seeing FRAME_A after sending the window update).
3560 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003561 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003562 h2c->dfl -= flen;
3563 h2c->rcvd_c += flen;
3564 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3565
3566 if (h2c->dfl > h2c->dpl) {
3567 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003568 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003569 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003570 }
3571
Willy Tarreau4a28da12018-01-04 14:41:00 +01003572 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003573 /* here we're done with the frame, all the payload (except padding) was
3574 * transferred.
3575 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003576
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003577 if (h2c->dff & H2_F_DATA_END_STREAM) {
3578 if (htx) {
3579 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3580 h2c->flags |= H2_CF_DEM_SFULL;
3581 goto fail;
3582 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003583 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003584 else if (h2s->flags & H2_SF_DATA_CHNK) {
3585 /* emit the trailing 0 CRLF CRLF */
3586 if (b_room(csbuf) < 5) {
3587 h2c->flags |= H2_CF_DEM_SFULL;
3588 goto fail;
3589 }
3590 chklen += 5;
3591 b_putblk(csbuf, "0\r\n\r\n", 5);
3592 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003593 }
3594
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003595 h2c->rcvd_c += h2c->dpl;
3596 h2c->rcvd_s += h2c->dpl;
3597 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003598 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3599
Willy Tarreau39d68502018-03-02 12:26:37 +01003600 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003601 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003602 h2s->cs->flags |= CS_FL_REOS;
3603 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003604 if (htx)
3605 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003606 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003607 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003608 if (htx)
3609 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003610 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003611}
3612
Willy Tarreau5dd17352018-06-14 13:33:30 +02003613/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3614 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3615 * number of bytes sent. The caller must check the stream's status to detect
3616 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003617 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003618static 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 +02003619{
3620 struct http_hdr list[MAX_HTTP_HDR];
3621 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003622 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003623 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003624 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003625 int es_now = 0;
3626 int ret = 0;
3627 int hdr;
3628
3629 if (h2c_mux_busy(h2c, h2s)) {
3630 h2s->flags |= H2_SF_BLK_MBUSY;
3631 return 0;
3632 }
3633
Willy Tarreau44e973f2018-03-01 17:49:30 +01003634 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003635 h2c->flags |= H2_CF_MUX_MALLOC;
3636 h2s->flags |= H2_SF_BLK_MROOM;
3637 return 0;
3638 }
3639
3640 /* First, try to parse the H1 response and index it into <list>.
3641 * NOTE! Since it comes from haproxy, we *know* that a response header
3642 * block does not wrap and we can safely read it this way without
3643 * having to realign the buffer.
3644 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003645 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003646 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003647 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003648 /* incomplete or invalid response, this is abnormal coming from
3649 * haproxy and may only result in a bad errorfile or bad Lua code
3650 * so that won't be fixed, raise an error now.
3651 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003652 * FIXME: we should instead add the ability to only return a
3653 * 502 bad gateway. But in theory this is not supposed to
3654 * happen.
3655 */
3656 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3657 ret = 0;
3658 goto end;
3659 }
3660
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003661 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003662
3663 /* certain statuses have no body or an empty one, regardless of
3664 * what the headers say.
3665 */
3666 if (sl.st.status >= 100 && sl.st.status < 200) {
3667 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3668 h1m->curr_len = h1m->body_len = 0;
3669 }
3670 else if (sl.st.status == 204 || sl.st.status == 304) {
3671 /* no contents, claim c-len is present and set to zero */
3672 h1m->flags &= ~H1_MF_CHNK;
3673 h1m->flags |= H1_MF_CLEN;
3674 h1m->curr_len = h1m->body_len = 0;
3675 }
3676
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003677 chunk_reset(&outbuf);
3678
3679 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003680 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003681 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003682 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003683
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003684 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003685 break;
3686 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003687 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003688 }
3689
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003690 if (outbuf.size < 9)
3691 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003692
3693 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003694 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3695 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3696 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003697
3698 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003699 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003700 /* this is an unparsable response */
3701 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3702 ret = 0;
3703 goto end;
3704 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003705
3706 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003707 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003708 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003709 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003710 }
3711
3712 /* encode all headers, stop at empty name */
3713 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003714 /* these ones do not exist in H2 and must be dropped. */
3715 if (isteq(list[hdr].n, ist("connection")) ||
3716 isteq(list[hdr].n, ist("proxy-connection")) ||
3717 isteq(list[hdr].n, ist("keep-alive")) ||
3718 isteq(list[hdr].n, ist("upgrade")) ||
3719 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003720 continue;
3721
3722 if (isteq(list[hdr].n, ist("")))
3723 break; // end
3724
3725 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3726 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003727 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003728 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003729 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003730 }
3731 }
3732
3733 /* we may need to add END_STREAM */
3734 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3735 es_now = 1;
3736
3737 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003738 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003739
3740 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003741 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003742
3743 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003744 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003745
3746 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003748 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003749
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003750 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003751 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003752 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003753
Willy Tarreau801250e2018-09-11 11:45:04 +02003754 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003755 h2s->flags |= H2_SF_ES_SENT;
3756 if (h2s->st == H2_SS_OPEN)
3757 h2s->st = H2_SS_HLOC;
3758 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003759 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003760 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003761 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003762 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003763 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003764 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003765 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003766 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003767 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003768
3769 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003770
3771 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003772 //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 +02003773 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003774 full:
3775 h1m_init_res(h1m);
3776 h1m->err_pos = -1; // don't care about errors on the response path
3777 h2c->flags |= H2_CF_MUX_MFULL;
3778 h2s->flags |= H2_SF_BLK_MROOM;
3779 ret = 0;
3780 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003781}
3782
Willy Tarreau5dd17352018-06-14 13:33:30 +02003783/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3784 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3785 * the number of bytes sent. The caller must check the stream's status to
3786 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003787 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003788static 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 +02003789{
3790 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003791 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003792 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003793 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003794 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003795 int es_now = 0;
3796 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003797 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003798 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003799
3800 if (h2c_mux_busy(h2c, h2s)) {
3801 h2s->flags |= H2_SF_BLK_MBUSY;
3802 goto end;
3803 }
3804
Willy Tarreau44e973f2018-03-01 17:49:30 +01003805 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003806 h2c->flags |= H2_CF_MUX_MALLOC;
3807 h2s->flags |= H2_SF_BLK_MROOM;
3808 goto end;
3809 }
3810
3811 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003812 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003813 goto end;
3814
3815 chunk_reset(&outbuf);
3816
3817 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003818 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003819 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003820 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003821
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003822 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003823 break;
3824 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003825 /* If there are pending data in the output buffer, and we have
3826 * less than 1/4 of the mbuf's size and everything fits, we'll
3827 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3828 * is full and wait, to save some slow realign calls.
3829 */
3830 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3831 h2c->flags |= H2_CF_MUX_MFULL;
3832 h2s->flags |= H2_SF_BLK_MROOM;
3833 goto end;
3834 }
3835
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003836 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003837 }
3838
3839 if (outbuf.size < 9) {
3840 h2c->flags |= H2_CF_MUX_MFULL;
3841 h2s->flags |= H2_SF_BLK_MROOM;
3842 goto end;
3843 }
3844
3845 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3847 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3848 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003849
3850 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3851 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003852 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003853 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003854 break;
3855 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003856 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003857 if ((long long)size > h1m->curr_len)
3858 size = h1m->curr_len;
3859 break;
3860 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003861 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003862 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003863 if (!ret)
3864 goto end;
3865
3866 if (ret < 0) {
3867 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003868 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003869 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3870 goto end;
3871 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003872 max -= ret;
3873 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003874 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003875 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003876 }
3877
Willy Tarreau801250e2018-09-11 11:45:04 +02003878 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003879 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003880 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003881 if (!ret)
3882 goto end;
3883
3884 if (ret < 0) {
3885 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003886 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003887 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3888 goto end;
3889 }
3890
3891 size = chunk;
3892 h1m->curr_len = chunk;
3893 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003894 max -= ret;
3895 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003896 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003897 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003898 if (!size)
3899 goto send_empty;
3900 }
3901
3902 /* in MSG_DATA state, continue below */
3903 size = h1m->curr_len;
3904 break;
3905 }
3906
3907 /* we have in <size> the exact number of bytes we need to copy from
3908 * the H1 buffer. We need to check this against the connection's and
3909 * the stream's send windows, and to ensure that this fits in the max
3910 * frame size and in the buffer's available space minus 9 bytes (for
3911 * the frame header). The connection's flow control is applied last so
3912 * that we can use a separate list of streams which are immediately
3913 * unblocked on window opening. Note: we don't implement padding.
3914 */
3915
Willy Tarreau5dd17352018-06-14 13:33:30 +02003916 if (size > max)
3917 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003918
3919 if (size > h2s->mws)
3920 size = h2s->mws;
3921
3922 if (size <= 0) {
3923 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003924 if (h2s->send_wait) {
3925 LIST_DEL(&h2s->list);
3926 LIST_INIT(&h2s->list);
3927 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003928 goto end;
3929 }
3930
3931 if (h2c->mfs && size > h2c->mfs)
3932 size = h2c->mfs;
3933
3934 if (size + 9 > outbuf.size) {
3935 /* we have an opportunity for enlarging the too small
3936 * available space, let's try.
3937 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003938 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003939 goto realign_again;
3940 size = outbuf.size - 9;
3941 }
3942
3943 if (size <= 0) {
3944 h2c->flags |= H2_CF_MUX_MFULL;
3945 h2s->flags |= H2_SF_BLK_MROOM;
3946 goto end;
3947 }
3948
3949 if (size > h2c->mws)
3950 size = h2c->mws;
3951
3952 if (size <= 0) {
3953 h2s->flags |= H2_SF_BLK_MFCTL;
3954 goto end;
3955 }
3956
3957 /* copy whatever we can */
3958 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003959 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003960 if (ret == 1)
3961 len2 = 0;
3962
3963 if (!ret || len1 + len2 < size) {
3964 /* FIXME: must normally never happen */
3965 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3966 goto end;
3967 }
3968
3969 /* limit len1/len2 to size */
3970 if (len1 + len2 > size) {
3971 int sub = len1 + len2 - size;
3972
3973 if (len2 > sub)
3974 len2 -= sub;
3975 else {
3976 sub -= len2;
3977 len2 = 0;
3978 len1 -= sub;
3979 }
3980 }
3981
3982 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003983 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003984 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003985 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003986
3987 send_empty:
3988 /* we may need to add END_STREAM */
3989 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3990 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003991 *
3992 * FIXME: what we do here is not correct because we send end_stream
3993 * before knowing if we'll have to send a HEADERS frame for the
3994 * trailers. More importantly we're not consuming the trailing CRLF
3995 * after the end of trailers, so it will be left to the caller to
3996 * eat it. The right way to do it would be to measure trailers here
3997 * and to send ES only if there are no trailers.
3998 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003999 */
4000 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004001 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004002 es_now = 1;
4003
4004 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004005 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004006
4007 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004008 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004009
4010 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004011 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004012
4013 /* consume incoming H1 response */
4014 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004015 max -= size;
4016 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004017 total += size;
4018 h1m->curr_len -= size;
4019 h2s->mws -= size;
4020 h2c->mws -= size;
4021
4022 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004023 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004024 goto new_frame;
4025 }
4026 }
4027
4028 if (es_now) {
4029 if (h2s->st == H2_SS_OPEN)
4030 h2s->st = H2_SS_HLOC;
4031 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004032 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004033
Willy Tarreau35a62702018-02-27 15:37:25 +01004034 if (!(h1m->flags & H1_MF_CHNK)) {
4035 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004036 total += max;
4037 ofs += max;
4038 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004039
Willy Tarreau801250e2018-09-11 11:45:04 +02004040 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004041 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004042
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 h2s->flags |= H2_SF_ES_SENT;
4044 }
4045
4046 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004047 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 +02004048 return total;
4049}
4050
Willy Tarreau115e83b2018-12-01 19:17:53 +01004051/* Try to send a HEADERS frame matching HTX response present in HTX message
4052 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4053 * must check the stream's status to detect any error which might have happened
4054 * subsequently to a successful send. The htx blocks are automatically removed
4055 * from the message. The htx message is assumed to be valid since produced from
4056 * the internal code, hence it contains a start line, an optional series of
4057 * header blocks and an end of header, otherwise an invalid frame could be
4058 * emitted and the resulting htx message could be left in an inconsistent state.
4059 */
4060static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4061{
4062 struct http_hdr list[MAX_HTTP_HDR];
4063 struct h2c *h2c = h2s->h2c;
4064 struct htx_blk *blk;
4065 struct htx_blk *blk_end;
4066 struct buffer outbuf;
4067 struct htx_sl *sl;
4068 enum htx_blk_type type;
4069 int es_now = 0;
4070 int ret = 0;
4071 int hdr;
4072 int idx;
4073
4074 if (h2c_mux_busy(h2c, h2s)) {
4075 h2s->flags |= H2_SF_BLK_MBUSY;
4076 return 0;
4077 }
4078
4079 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4080 h2c->flags |= H2_CF_MUX_MALLOC;
4081 h2s->flags |= H2_SF_BLK_MROOM;
4082 return 0;
4083 }
4084
4085 /* determine the first block which must not be deleted, blk_end may
4086 * be NULL if all blocks have to be deleted.
4087 */
4088 idx = htx_get_head(htx);
4089 blk_end = NULL;
4090 while (idx != -1) {
4091 type = htx_get_blk_type(htx_get_blk(htx, idx));
4092 idx = htx_get_next(htx, idx);
4093 if (type == HTX_BLK_EOH) {
4094 if (idx != -1)
4095 blk_end = htx_get_blk(htx, idx);
4096 break;
4097 }
4098 }
4099
4100 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004101 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004102 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004103 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004104 if (h2s->status < 100 || h2s->status > 999)
4105 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004106
4107 /* and the rest of the headers, that we dump starting at header 0 */
4108 hdr = 0;
4109
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004110 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004111 while ((idx = htx_get_next(htx, idx)) != -1) {
4112 blk = htx_get_blk(htx, idx);
4113 type = htx_get_blk_type(blk);
4114
4115 if (type == HTX_BLK_UNUSED)
4116 continue;
4117
4118 if (type != HTX_BLK_HDR)
4119 break;
4120
4121 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4122 goto fail;
4123
4124 list[hdr].n = htx_get_blk_name(htx, blk);
4125 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004126 hdr++;
4127 }
4128
4129 /* marker for end of headers */
4130 list[hdr].n = ist("");
4131
4132 if (h2s->status == 204 || h2s->status == 304) {
4133 /* no contents, claim c-len is present and set to zero */
4134 es_now = 1;
4135 }
4136
4137 chunk_reset(&outbuf);
4138
4139 while (1) {
4140 outbuf.area = b_tail(&h2c->mbuf);
4141 outbuf.size = b_contig_space(&h2c->mbuf);
4142 outbuf.data = 0;
4143
4144 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4145 break;
4146 realign_again:
4147 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4148 }
4149
4150 if (outbuf.size < 9)
4151 goto full;
4152
4153 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4154 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4155 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4156 outbuf.data = 9;
4157
4158 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004159 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004160 if (b_space_wraps(&h2c->mbuf))
4161 goto realign_again;
4162 goto full;
4163 }
4164
4165 /* encode all headers, stop at empty name */
4166 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4167 /* these ones do not exist in H2 and must be dropped. */
4168 if (isteq(list[hdr].n, ist("connection")) ||
4169 isteq(list[hdr].n, ist("proxy-connection")) ||
4170 isteq(list[hdr].n, ist("keep-alive")) ||
4171 isteq(list[hdr].n, ist("upgrade")) ||
4172 isteq(list[hdr].n, ist("transfer-encoding")))
4173 continue;
4174
4175 if (isteq(list[hdr].n, ist("")))
4176 break; // end
4177
4178 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4179 /* output full */
4180 if (b_space_wraps(&h2c->mbuf))
4181 goto realign_again;
4182 goto full;
4183 }
4184 }
4185
4186 /* we may need to add END_STREAM.
4187 * FIXME: we should also set it when we know for sure that the
4188 * content-length is zero as well as on 204/304
4189 */
4190 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4191 es_now = 1;
4192
4193 if (h2s->cs->flags & CS_FL_SHW)
4194 es_now = 1;
4195
4196 /* update the frame's size */
4197 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4198
4199 if (es_now)
4200 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4201
4202 /* commit the H2 response */
4203 b_add(&h2c->mbuf, outbuf.data);
4204 h2s->flags |= H2_SF_HEADERS_SENT;
4205
Willy Tarreau115e83b2018-12-01 19:17:53 +01004206 if (es_now) {
4207 h2s->flags |= H2_SF_ES_SENT;
4208 if (h2s->st == H2_SS_OPEN)
4209 h2s->st = H2_SS_HLOC;
4210 else
4211 h2s_close(h2s);
4212 }
4213
4214 /* OK we could properly deliver the response */
4215
4216 /* remove all header blocks including the EOH and compute the
4217 * corresponding size.
4218 *
4219 * FIXME: We should remove everything when es_now is set.
4220 */
4221 ret = 0;
4222 idx = htx_get_head(htx);
4223 blk = htx_get_blk(htx, idx);
4224 while (blk != blk_end) {
4225 ret += htx_get_blksz(blk);
4226 blk = htx_remove_blk(htx, blk);
4227 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004228
4229 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4230 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004231 end:
4232 return ret;
4233 full:
4234 h2c->flags |= H2_CF_MUX_MFULL;
4235 h2s->flags |= H2_SF_BLK_MROOM;
4236 ret = 0;
4237 goto end;
4238 fail:
4239 /* unparsable HTX messages, too large ones to be produced in the local
4240 * list etc go here (unrecoverable errors).
4241 */
4242 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4243 ret = 0;
4244 goto end;
4245}
4246
Willy Tarreau80739692018-10-05 11:35:57 +02004247/* Try to send a HEADERS frame matching HTX request present in HTX message
4248 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4249 * must check the stream's status to detect any error which might have happened
4250 * subsequently to a successful send. The htx blocks are automatically removed
4251 * from the message. The htx message is assumed to be valid since produced from
4252 * the internal code, hence it contains a start line, an optional series of
4253 * header blocks and an end of header, otherwise an invalid frame could be
4254 * emitted and the resulting htx message could be left in an inconsistent state.
4255 */
4256static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4257{
4258 struct http_hdr list[MAX_HTTP_HDR];
4259 struct h2c *h2c = h2s->h2c;
4260 struct htx_blk *blk;
4261 struct htx_blk *blk_end;
4262 struct buffer outbuf;
4263 struct htx_sl *sl;
4264 struct ist meth, path;
4265 enum htx_blk_type type;
4266 int es_now = 0;
4267 int ret = 0;
4268 int hdr;
4269 int idx;
4270
4271 if (h2c_mux_busy(h2c, h2s)) {
4272 h2s->flags |= H2_SF_BLK_MBUSY;
4273 return 0;
4274 }
4275
4276 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4277 h2c->flags |= H2_CF_MUX_MALLOC;
4278 h2s->flags |= H2_SF_BLK_MROOM;
4279 return 0;
4280 }
4281
4282 /* determine the first block which must not be deleted, blk_end may
4283 * be NULL if all blocks have to be deleted.
4284 */
4285 idx = htx_get_head(htx);
4286 blk_end = NULL;
4287 while (idx != -1) {
4288 type = htx_get_blk_type(htx_get_blk(htx, idx));
4289 idx = htx_get_next(htx, idx);
4290 if (type == HTX_BLK_EOH) {
4291 if (idx != -1)
4292 blk_end = htx_get_blk(htx, idx);
4293 break;
4294 }
4295 }
4296
4297 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004298 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004299 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004300 meth = htx_sl_req_meth(sl);
4301 path = htx_sl_req_uri(sl);
4302
4303 /* and the rest of the headers, that we dump starting at header 0 */
4304 hdr = 0;
4305
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004306 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004307 while ((idx = htx_get_next(htx, idx)) != -1) {
4308 blk = htx_get_blk(htx, idx);
4309 type = htx_get_blk_type(blk);
4310
4311 if (type == HTX_BLK_UNUSED)
4312 continue;
4313
4314 if (type != HTX_BLK_HDR)
4315 break;
4316
4317 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4318 goto fail;
4319
4320 list[hdr].n = htx_get_blk_name(htx, blk);
4321 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004322 hdr++;
4323 }
4324
4325 /* marker for end of headers */
4326 list[hdr].n = ist("");
4327
4328 chunk_reset(&outbuf);
4329
4330 while (1) {
4331 outbuf.area = b_tail(&h2c->mbuf);
4332 outbuf.size = b_contig_space(&h2c->mbuf);
4333 outbuf.data = 0;
4334
4335 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4336 break;
4337 realign_again:
4338 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4339 }
4340
4341 if (outbuf.size < 9)
4342 goto full;
4343
4344 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4345 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4346 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4347 outbuf.data = 9;
4348
4349 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004350 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004351 if (b_space_wraps(&h2c->mbuf))
4352 goto realign_again;
4353 goto full;
4354 }
4355
4356 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004357 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4358 /* output full */
4359 if (b_space_wraps(&h2c->mbuf))
4360 goto realign_again;
4361 goto full;
4362 }
Willy Tarreau80739692018-10-05 11:35:57 +02004363
4364 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004365 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004366 /* output full */
4367 if (b_space_wraps(&h2c->mbuf))
4368 goto realign_again;
4369 goto full;
4370 }
4371
4372 /* encode all headers, stop at empty name */
4373 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4374 /* these ones do not exist in H2 and must be dropped. */
4375 if (isteq(list[hdr].n, ist("connection")) ||
4376 isteq(list[hdr].n, ist("proxy-connection")) ||
4377 isteq(list[hdr].n, ist("keep-alive")) ||
4378 isteq(list[hdr].n, ist("upgrade")) ||
4379 isteq(list[hdr].n, ist("transfer-encoding")))
4380 continue;
4381
4382 if (isteq(list[hdr].n, ist("")))
4383 break; // end
4384
4385 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4386 /* output full */
4387 if (b_space_wraps(&h2c->mbuf))
4388 goto realign_again;
4389 goto full;
4390 }
4391 }
4392
4393 /* we may need to add END_STREAM if we have no body :
4394 * - request already closed, or :
4395 * - no transfer-encoding, and :
4396 * - no content-length or content-length:0
4397 * Fixme: this doesn't take into account CONNECT requests.
4398 */
4399 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4400 es_now = 1;
4401
4402 if (sl->flags & HTX_SL_F_BODYLESS)
4403 es_now = 1;
4404
4405 if (h2s->cs->flags & CS_FL_SHW)
4406 es_now = 1;
4407
4408 /* update the frame's size */
4409 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4410
4411 if (es_now)
4412 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4413
4414 /* commit the H2 response */
4415 b_add(&h2c->mbuf, outbuf.data);
4416 h2s->flags |= H2_SF_HEADERS_SENT;
4417 h2s->st = H2_SS_OPEN;
4418
Willy Tarreau80739692018-10-05 11:35:57 +02004419 if (es_now) {
4420 // trim any possibly pending data (eg: inconsistent content-length)
4421 h2s->flags |= H2_SF_ES_SENT;
4422 h2s->st = H2_SS_HLOC;
4423 }
4424
4425 /* remove all header blocks including the EOH and compute the
4426 * corresponding size.
4427 *
4428 * FIXME: We should remove everything when es_now is set.
4429 */
4430 ret = 0;
4431 idx = htx_get_head(htx);
4432 blk = htx_get_blk(htx, idx);
4433 while (blk != blk_end) {
4434 ret += htx_get_blksz(blk);
4435 blk = htx_remove_blk(htx, blk);
4436 }
4437
4438 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4439 htx_remove_blk(htx, blk_end);
4440
4441 end:
4442 return ret;
4443 full:
4444 h2c->flags |= H2_CF_MUX_MFULL;
4445 h2s->flags |= H2_SF_BLK_MROOM;
4446 ret = 0;
4447 goto end;
4448 fail:
4449 /* unparsable HTX messages, too large ones to be produced in the local
4450 * list etc go here (unrecoverable errors).
4451 */
4452 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4453 ret = 0;
4454 goto end;
4455}
4456
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004457/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004458 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4459 * caller must check the stream's status to detect any error which might have
4460 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004461 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4462 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004463static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004464{
4465 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004466 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004467 struct buffer outbuf;
4468 size_t total = 0;
4469 int es_now = 0;
4470 int bsize; /* htx block size */
4471 int fsize; /* h2 frame size */
4472 struct htx_blk *blk;
4473 enum htx_blk_type type;
4474 int idx;
4475
4476 if (h2c_mux_busy(h2c, h2s)) {
4477 h2s->flags |= H2_SF_BLK_MBUSY;
4478 goto end;
4479 }
4480
4481 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4482 h2c->flags |= H2_CF_MUX_MALLOC;
4483 h2s->flags |= H2_SF_BLK_MROOM;
4484 goto end;
4485 }
4486
Willy Tarreau98de12a2018-12-12 07:03:00 +01004487 htx = htx_from_buf(buf);
4488
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004489 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4490 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4491 * the caller to handle.
4492 */
4493
4494 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004495 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004496 goto end;
4497
4498 idx = htx_get_head(htx);
4499 blk = htx_get_blk(htx, idx);
4500 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4501 bsize = htx_get_blksz(blk);
4502 fsize = bsize;
4503
4504 if (type == HTX_BLK_EOD) {
4505 /* if we have an EOD, we're dealing with chunked data. We may
4506 * have a set of trailers after us that the caller will want to
4507 * deal with. Let's simply remove the EOD and return.
4508 */
4509 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004510 total++; // EOD counts as one byte
4511 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004512 goto end;
4513 }
4514
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004515 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4516 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004517
4518 /* Perform some optimizations to reduce the number of buffer copies.
4519 * First, if the mux's buffer is empty and the htx area contains
4520 * exactly one data block of the same size as the requested count, and
4521 * this count fits within the frame size, the stream's window size, and
4522 * the connection's window size, then it's possible to simply swap the
4523 * caller's buffer with the mux's output buffer and adjust offsets and
4524 * length to match the entire DATA HTX block in the middle. In this
4525 * case we perform a true zero-copy operation from end-to-end. This is
4526 * the situation that happens all the time with large files. Second, if
4527 * this is not possible, but the mux's output buffer is empty, we still
4528 * have an opportunity to avoid the copy to the intermediary buffer, by
4529 * making the intermediary buffer's area point to the output buffer's
4530 * area. In this case we want to skip the HTX header to make sure that
4531 * copies remain aligned and that this operation remains possible all
4532 * the time. This goes for headers, data blocks and any data extracted
4533 * from the HTX blocks.
4534 */
4535 if (unlikely(fsize == count &&
4536 htx->used == 1 && type == HTX_BLK_DATA &&
4537 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4538 void *old_area = h2c->mbuf.area;
4539
4540 if (b_data(&h2c->mbuf)) {
4541 /* too bad there are data left there. If we have less
4542 * than 1/4 of the mbuf's size and everything fits,
4543 * we'll perform a copy anyway. Otherwise we'll pretend
4544 * the mbuf is full and wait.
4545 */
4546 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4547 goto copy;
4548 h2c->flags |= H2_CF_MUX_MFULL;
4549 h2s->flags |= H2_SF_BLK_MROOM;
4550 goto end;
4551 }
4552
4553 /* map an H2 frame to the HTX block so that we can put the
4554 * frame header there.
4555 */
4556 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004557 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004558 h2c->mbuf.data = fsize + 9;
4559 outbuf.area = b_head(&h2c->mbuf);
4560
4561 /* prepend an H2 DATA frame header just before the DATA block */
4562 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4563 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4564 h2_set_frame_size(outbuf.area, fsize);
4565
4566 /* update windows */
4567 h2s->mws -= fsize;
4568 h2c->mws -= fsize;
4569
4570 /* and exchange with our old area */
4571 buf->area = old_area;
4572 buf->data = buf->head = 0;
4573 total += fsize;
4574 goto end;
4575 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004576
Willy Tarreau98de12a2018-12-12 07:03:00 +01004577 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004578 /* for DATA and EOM we'll have to emit a frame, even if empty */
4579
4580 while (1) {
4581 outbuf.area = b_tail(&h2c->mbuf);
4582 outbuf.size = b_contig_space(&h2c->mbuf);
4583 outbuf.data = 0;
4584
4585 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4586 break;
4587 realign_again:
4588 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4589 }
4590
4591 if (outbuf.size < 9) {
4592 h2c->flags |= H2_CF_MUX_MFULL;
4593 h2s->flags |= H2_SF_BLK_MROOM;
4594 goto end;
4595 }
4596
4597 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4598 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4599 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4600 outbuf.data = 9;
4601
4602 /* we have in <fsize> the exact number of bytes we need to copy from
4603 * the HTX buffer. We need to check this against the connection's and
4604 * the stream's send windows, and to ensure that this fits in the max
4605 * frame size and in the buffer's available space minus 9 bytes (for
4606 * the frame header). The connection's flow control is applied last so
4607 * that we can use a separate list of streams which are immediately
4608 * unblocked on window opening. Note: we don't implement padding.
4609 */
4610
4611 /* EOM is presented with bsize==1 but would lead to the emission of an
4612 * empty frame, thus we force it to zero here.
4613 */
4614 if (type == HTX_BLK_EOM)
4615 bsize = fsize = 0;
4616
4617 if (!fsize)
4618 goto send_empty;
4619
4620 if (h2s->mws <= 0) {
4621 h2s->flags |= H2_SF_BLK_SFCTL;
4622 if (h2s->send_wait) {
4623 LIST_DEL(&h2s->list);
4624 LIST_INIT(&h2s->list);
4625 }
4626 goto end;
4627 }
4628
Willy Tarreauee573762018-12-04 15:25:57 +01004629 if (fsize > count)
4630 fsize = count;
4631
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004632 if (fsize > h2s->mws)
4633 fsize = h2s->mws; // >0
4634
4635 if (h2c->mfs && fsize > h2c->mfs)
4636 fsize = h2c->mfs; // >0
4637
4638 if (fsize + 9 > outbuf.size) {
4639 /* we have an opportunity for enlarging the too small
4640 * available space, let's try.
4641 * FIXME: is this really interesting to do? Maybe we'll
4642 * spend lots of time realigning instead of using two
4643 * frames.
4644 */
4645 if (b_space_wraps(&h2c->mbuf))
4646 goto realign_again;
4647 fsize = outbuf.size - 9;
4648
4649 if (fsize <= 0) {
4650 /* no need to send an empty frame here */
4651 h2c->flags |= H2_CF_MUX_MFULL;
4652 h2s->flags |= H2_SF_BLK_MROOM;
4653 goto end;
4654 }
4655 }
4656
4657 if (h2c->mws <= 0) {
4658 h2s->flags |= H2_SF_BLK_MFCTL;
4659 goto end;
4660 }
4661
4662 if (fsize > h2c->mws)
4663 fsize = h2c->mws;
4664
4665 /* now let's copy this this into the output buffer */
4666 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004667 h2s->mws -= fsize;
4668 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004669 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004670
4671 send_empty:
4672 /* update the frame's size */
4673 h2_set_frame_size(outbuf.area, fsize);
4674
4675 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4676 * meeting EOM. We should optimize this later.
4677 */
4678 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004679 total++; // EOM counts as one byte
4680 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004681 es_now = 1;
4682 }
4683
4684 if (es_now)
4685 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4686
4687 /* commit the H2 response */
4688 b_add(&h2c->mbuf, fsize + 9);
4689
4690 /* consume incoming HTX block, including EOM */
4691 total += fsize;
4692 if (fsize == bsize) {
4693 htx_remove_blk(htx, blk);
4694 if (fsize)
4695 goto new_frame;
4696 } else {
4697 /* we've truncated this block */
4698 htx_cut_data_blk(htx, blk, fsize);
4699 }
4700
4701 if (es_now) {
4702 if (h2s->st == H2_SS_OPEN)
4703 h2s->st = H2_SS_HLOC;
4704 else
4705 h2s_close(h2s);
4706
4707 h2s->flags |= H2_SF_ES_SENT;
4708 }
4709
4710 end:
4711 return total;
4712}
4713
Olivier Houchard6ff20392018-07-17 18:46:31 +02004714/* Called from the upper layer, to subscribe to events, such as being able to send */
4715static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4716{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004717 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004718 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004719 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004720
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004721 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004722 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004723 if (!(sw->events & SUB_RETRY_RECV)) {
4724 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004725 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004726 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004727 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004728 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004729 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004730 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004731 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004732 if (!(sw->events & SUB_RETRY_SEND)) {
4733 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004734 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004735 h2s->send_wait = sw;
4736 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4737 if (h2s->flags & H2_SF_BLK_MFCTL)
4738 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4739 else
4740 LIST_ADDQ(&h2c->send_list, &h2s->list);
4741 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004742 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004743 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004744 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004745 if (event_type != 0)
4746 return -1;
4747 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004748
4749
4750}
4751
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004752static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4753{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004754 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004755 struct h2s *h2s = cs->ctx;
4756
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004757 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004758 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004759 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004760 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004761 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004762 }
4763 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004764 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004765 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004766 if (h2s->send_wait == sw) {
4767 LIST_DEL(&h2s->list);
4768 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004769 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004770 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004771 }
4772 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004773 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4774 sw = param;
4775 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004776 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004777 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01004778 LIST_DEL(&h2s->list);
4779 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004780 }
4781 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004782 return 0;
4783}
4784
4785
Olivier Houchard511efea2018-08-16 15:30:32 +02004786/* Called from the upper layer, to receive data */
4787static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4788{
Olivier Houchard638b7992018-08-16 15:41:52 +02004789 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004790 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004791 struct htx *h2s_htx = NULL;
4792 struct htx *buf_htx = NULL;
4793 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004794 size_t ret = 0;
4795
4796 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004797 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4798 /* in HTX mode we ignore the count argument */
4799 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004800 if (htx_is_empty(h2s_htx)) {
4801 if (cs->flags & CS_FL_REOS)
4802 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004803 if (cs->flags & CS_FL_ERR_PENDING)
4804 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004805 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004806 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004807
4808 buf_htx = htx_from_buf(buf);
4809 count = htx_free_space(buf_htx);
4810
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004811 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004812
4813 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004814 htx_to_buf(buf_htx, buf);
4815 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004816 ret = htx_ret.ret;
4817 }
4818 else {
4819 ret = b_xfer(buf, &h2s->rxbuf, count);
4820 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004821
Olivier Houchard638b7992018-08-16 15:41:52 +02004822 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004823 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004824 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004825 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004826 if (cs->flags & CS_FL_REOS)
4827 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004828 if (cs->flags & CS_FL_ERR_PENDING)
4829 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004830 if (b_size(&h2s->rxbuf)) {
4831 b_free(&h2s->rxbuf);
4832 offer_buffers(NULL, tasks_run_queue);
4833 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004834 }
4835
Willy Tarreau082f5592018-11-25 08:03:32 +01004836 if (ret && h2c->dsi == h2s->id) {
4837 /* demux is blocking on this stream's buffer */
4838 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01004839 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01004840 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004841end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004842 return ret;
4843}
4844
Olivier Houchardd846c262018-10-19 17:24:29 +02004845static void h2_stop_senders(struct h2c *h2c)
4846{
4847 struct h2s *h2s, *h2s_back;
4848
4849 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4850 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4851 if (h2c->msi == h2s_id(h2s))
4852 continue;
4853 LIST_DEL(&h2s->list);
4854 LIST_INIT(&h2s->list);
4855 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004856 h2s->send_wait->events |= SUB_RETRY_SEND;
4857 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004858 LIST_ADD(&h2c->send_list, &h2s->list);
4859 }
4860}
4861
Willy Tarreau62f52692017-10-08 23:01:42 +02004862/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004863static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004864{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004865 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004866 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004867 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004868 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004869 struct htx *htx;
4870 struct htx_blk *blk;
4871 enum htx_blk_type btype;
4872 uint32_t bsize;
4873 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004874
Olivier Houchardd846c262018-10-19 17:24:29 +02004875 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004876 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004877 h2s->send_wait = NULL;
4878 LIST_DEL(&h2s->list);
4879 LIST_INIT(&h2s->list);
4880 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004881 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4882 return 0;
4883
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004884 /* htx will be enough to decide if we're using HTX or legacy */
4885 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4886
Willy Tarreau0bad0432018-06-14 16:54:01 +02004887 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004888 h2s->flags |= H2_SF_OUTGOING_DATA;
4889
Willy Tarreau751f2d02018-10-05 09:35:00 +02004890 if (h2s->id == 0) {
4891 int32_t id = h2c_get_next_sid(h2s->h2c);
4892
4893 if (id < 0) {
4894 cs->ctx = NULL;
4895 cs->flags |= CS_FL_ERROR;
4896 h2s_destroy(h2s);
4897 return 0;
4898 }
4899
4900 eb32_delete(&h2s->by_id);
4901 h2s->by_id.key = h2s->id = id;
4902 h2s->h2c->max_id = id;
4903 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4904 }
4905
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004906 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004907 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4908 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004909 idx = htx_get_head(htx);
4910 blk = htx_get_blk(htx, idx);
4911 btype = htx_get_blk_type(blk);
4912 bsize = htx_get_blksz(blk);
4913
4914 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004915 case HTX_BLK_REQ_SL:
4916 /* start-line before headers */
4917 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4918 if (ret > 0) {
4919 total += ret;
4920 count -= ret;
4921 if (ret < bsize)
4922 goto done;
4923 }
4924 break;
4925
Willy Tarreau115e83b2018-12-01 19:17:53 +01004926 case HTX_BLK_RES_SL:
4927 /* start-line before headers */
4928 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4929 if (ret > 0) {
4930 total += ret;
4931 count -= ret;
4932 if (ret < bsize)
4933 goto done;
4934 }
4935 break;
4936
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004937 case HTX_BLK_DATA:
4938 case HTX_BLK_EOD:
4939 case HTX_BLK_EOM:
4940 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004941 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004942 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004943 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004944 total += ret;
4945 count -= ret;
4946 if (ret < bsize)
4947 goto done;
4948 }
4949 break;
4950
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004951 default:
4952 htx_remove_blk(htx, blk);
4953 total += bsize;
4954 count -= bsize;
4955 break;
4956 }
4957 }
4958 goto done;
4959 }
4960
4961 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004962 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004963 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004964 if (h2s->h2c->flags & H2_CF_IS_BACK)
4965 ret = -1;
4966 else
4967 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004968 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004969 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004970 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004971 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004972 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004973 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004974 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004975
Willy Tarreau5dd17352018-06-14 13:33:30 +02004976 if (unlikely((int)ret <= 0)) {
4977 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004978 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4979 break;
4980 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004981 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004982 total += count;
4983 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004984 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004985 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004986 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004987 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004988 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004989 break;
4990 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004991
4992 total += ret;
4993 count -= ret;
4994
4995 if (h2s->st >= H2_SS_ERROR)
4996 break;
4997
4998 if (h2s->flags & H2_SF_BLK_ANY)
4999 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005000 }
5001
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005002 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005003 if (h2s->st >= H2_SS_ERROR) {
5004 /* trim any possibly pending data after we close (extra CR-LF,
5005 * unprocessed trailers, abnormal extra data, ...)
5006 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005007 total += count;
5008 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005009 }
5010
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005011 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005012 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005013 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005014 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005015 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005016 }
5017
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005018 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005019 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005020 } else {
5021 b_del(buf, total);
5022 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005023
5024 /* The mux is full, cancel the pending tasks */
5025 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5026 (h2s->flags & H2_SF_BLK_MBUSY))
5027 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005028
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005029 /* If we're running HTX, and we read the whole buffer, then pretend
5030 * we read exactly what the caller specified, as with HTX the caller
5031 * will always give the buffer size, instead of the amount of data
5032 * available.
5033 */
5034 if (htx && !b_data(buf))
5035 total = orig_count;
5036
Olivier Houchard7505f942018-08-21 18:10:44 +02005037 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005038 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005039 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005040
Olivier Houchard7505f942018-08-21 18:10:44 +02005041 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005042 /* If we're waiting for flow control, and we got a shutr on the
5043 * connection, we will never be unlocked, so add an error on
5044 * the conn_stream.
5045 */
5046 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5047 !b_data(&h2s->h2c->dbuf) &&
5048 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5049 if (cs->flags & CS_FL_EOS)
5050 cs->flags |= CS_FL_ERROR;
5051 else
5052 cs->flags |= CS_FL_ERR_PENDING;
5053 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005054 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005055}
5056
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005057/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005058static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005059{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005060 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005061 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005062 struct eb32_node *node;
5063 int fctl_cnt = 0;
5064 int send_cnt = 0;
5065 int tree_cnt = 0;
5066 int orph_cnt = 0;
5067
5068 if (!h2c)
5069 return;
5070
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005071 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005072 fctl_cnt++;
5073
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005074 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005075 send_cnt++;
5076
Willy Tarreau3af37712018-12-18 14:34:41 +01005077 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005078 node = eb32_first(&h2c->streams_by_id);
5079 while (node) {
5080 h2s = container_of(node, struct h2s, by_id);
5081 tree_cnt++;
5082 if (!h2s->cs)
5083 orph_cnt++;
5084 node = eb32_next(node);
5085 }
5086
Willy Tarreau987c0632018-12-18 10:32:05 +01005087 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5088 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5089 " .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 +02005090 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5091 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005092 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005093 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5094 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5095 h2c->msi,
5096 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5097 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5098
5099 if (h2s) {
5100 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5101 h2s, h2s->id, h2s->flags,
5102 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5103 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5104 h2s->cs);
5105 if (h2s->cs)
5106 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5107 h2s->cs->flags, h2s->cs->data);
5108 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005109}
Willy Tarreau62f52692017-10-08 23:01:42 +02005110
5111/*******************************************************/
5112/* functions below are dedicated to the config parsers */
5113/*******************************************************/
5114
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005115/* config parser for global "tune.h2.header-table-size" */
5116static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5117 struct proxy *defpx, const char *file, int line,
5118 char **err)
5119{
5120 if (too_many_args(1, args, err, NULL))
5121 return -1;
5122
5123 h2_settings_header_table_size = atoi(args[1]);
5124 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5125 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5126 return -1;
5127 }
5128 return 0;
5129}
Willy Tarreau62f52692017-10-08 23:01:42 +02005130
Willy Tarreaue6baec02017-07-27 11:45:11 +02005131/* config parser for global "tune.h2.initial-window-size" */
5132static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5133 struct proxy *defpx, const char *file, int line,
5134 char **err)
5135{
5136 if (too_many_args(1, args, err, NULL))
5137 return -1;
5138
5139 h2_settings_initial_window_size = atoi(args[1]);
5140 if (h2_settings_initial_window_size < 0) {
5141 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5142 return -1;
5143 }
5144 return 0;
5145}
5146
Willy Tarreau5242ef82017-07-27 11:47:28 +02005147/* config parser for global "tune.h2.max-concurrent-streams" */
5148static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5149 struct proxy *defpx, const char *file, int line,
5150 char **err)
5151{
5152 if (too_many_args(1, args, err, NULL))
5153 return -1;
5154
5155 h2_settings_max_concurrent_streams = atoi(args[1]);
5156 if (h2_settings_max_concurrent_streams < 0) {
5157 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5158 return -1;
5159 }
5160 return 0;
5161}
5162
Willy Tarreau62f52692017-10-08 23:01:42 +02005163
5164/****************************************/
5165/* MUX initialization and instanciation */
5166/***************************************/
5167
5168/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005169static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005170 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005171 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005172 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005173 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005174 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005175 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005176 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005177 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005178 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005179 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005180 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005181 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005182 .shutr = h2_shutr,
5183 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005184 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005185 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005186 .name = "H2",
5187};
5188
Christopher Faulet32f61c02018-04-10 14:33:41 +02005189/* PROTO selection : this mux registers PROTO token "h2" */
5190static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005191 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005192
Willy Tarreau0108d902018-11-25 19:14:37 +01005193INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5194
Willy Tarreauf8957272018-10-03 10:25:20 +02005195static struct mux_proto_list mux_proto_h2_htx =
5196 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5197
5198INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5199
Willy Tarreau62f52692017-10-08 23:01:42 +02005200/* config keyword parsers */
5201static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005202 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005203 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005204 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005205 { 0, NULL, NULL }
5206}};
5207
Willy Tarreau0108d902018-11-25 19:14:37 +01005208INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);