blob: af7af061ffc2e10b736274396c54937efc0d420d [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 Tarreau8d0d58b2018-12-23 18:29:12 +010032/* dummy streams returned for closed, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010034static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020035static const struct h2s *h2_idle_stream;
36
Willy Tarreau5ab6b572017-09-22 08:05:00 +020037/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
Willy Tarreau315d8072017-12-10 22:17:57 +010045/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
51#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010052
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020053#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
54#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
55#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
56#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020057#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
58#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020059
Willy Tarreau081d4722017-05-16 21:51:05 +020060/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020061#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
62#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
63#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 +020064#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010065#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020066
Willy Tarreau5ab6b572017-09-22 08:05:00 +020067/* H2 connection state, in h2c->st0 */
68enum h2_cs {
69 H2_CS_PREFACE, // init done, waiting for connection preface
70 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
71 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
72 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010073 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
74 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020075 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
76 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
77 H2_CS_ENTRIES // must be last
78} __attribute__((packed));
79
80/* H2 connection descriptor */
81struct h2c {
82 struct connection *conn;
83
84 enum h2_cs st0; /* mux state */
85 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
86
87 /* 16 bit hole here */
88 uint32_t flags; /* connection flags: H2_CF_* */
89 int32_t max_id; /* highest ID known on this connection, <0 before preface */
90 uint32_t rcvd_c; /* newly received data to ACK for the connection */
91 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
92
93 /* states for the demux direction */
94 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020095 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020096
97 int32_t dsi; /* demux stream ID (<0 = idle) */
98 int32_t dfl; /* demux frame length (if dsi >= 0) */
99 int8_t dft; /* demux frame type (if dsi >= 0) */
100 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100101 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
102 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
104
105 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200106 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200107 int32_t msi; /* mux stream ID (<0 = idle) */
108 int32_t mfl; /* mux frame length (if dsi >= 0) */
109 int8_t mft; /* mux frame type (if dsi >= 0) */
110 int8_t mff; /* mux frame flags (if dsi >= 0) */
111 /* 16 bit hole here */
112 int32_t miw; /* mux initial window size for all new streams */
113 int32_t mws; /* mux window size. Can be negative. */
114 int32_t mfs; /* mux's max frame size */
115
Willy Tarreauea392822017-10-31 10:02:25 +0100116 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100117 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100118 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200119 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200120 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100121 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200122 struct eb_root streams_by_id; /* all active streams by their ID */
123 struct list send_list; /* list of blocked streams requesting to send */
124 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200125 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100126 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200127 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200128};
129
Willy Tarreau18312642017-10-11 07:57:07 +0200130/* H2 stream state, in h2s->st */
131enum h2_ss {
132 H2_SS_IDLE = 0, // idle
133 H2_SS_RLOC, // reserved(local)
134 H2_SS_RREM, // reserved(remote)
135 H2_SS_OPEN, // open
136 H2_SS_HREM, // half-closed(remote)
137 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200138 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200139 H2_SS_CLOSED, // closed
140 H2_SS_ENTRIES // must be last
141} __attribute__((packed));
142
143/* HTTP/2 stream flags (32 bit), in h2s->flags */
144#define H2_SF_NONE 0x00000000
145#define H2_SF_ES_RCVD 0x00000001
146#define H2_SF_ES_SENT 0x00000002
147
148#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
149#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
150
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200151/* stream flags indicating the reason the stream is blocked */
152#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
153#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
154#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
155#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
156#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
157
Willy Tarreau454f9052017-10-26 19:40:35 +0200158/* stream flags indicating how data is supposed to be sent */
159#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
160#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
161
162/* step we're currently in when sending chunks. This is needed because we may
163 * have to transfer chunks as large as a full buffer so there's no room left
164 * for size nor crlf around.
165 */
166#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
167#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
168#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
169
170#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
171
Willy Tarreau67434202017-11-06 20:20:51 +0100172#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100173#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100174
Willy Tarreau18312642017-10-11 07:57:07 +0200175/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
176 * it is being processed in the internal HTTP representation (H1 for now).
177 */
178struct h2s {
179 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100180 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200181 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200182 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200183 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200184 int32_t id; /* stream ID */
185 uint32_t flags; /* H2_SF_* */
186 int mws; /* mux window size for this stream */
187 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
188 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200189 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200190 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200191 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
192 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
193 struct wait_event *send_wait; /* The streeam is waiting for flow control */
194 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200195};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200196
Willy Tarreauc6405142017-09-21 20:23:50 +0200197/* descriptor for an h2 frame header */
198struct h2_fh {
199 uint32_t len; /* length, host order, 24 bits */
200 uint32_t sid; /* stream id, host order, 31 bits */
201 uint8_t ft; /* frame type */
202 uint8_t ff; /* frame flags */
203};
204
Willy Tarreau8ceae722018-11-26 11:58:30 +0100205/* the h2c connection pool */
206DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
207
208/* the h2s stream pool */
209DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
210
Willy Tarreaudc572362018-12-12 08:08:05 +0100211/* The default connection window size is 65535, it may only be enlarged using
212 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
213 * we'll pretend we already received the difference between the two to send
214 * an equivalent window update to enlarge it to 2G-1.
215 */
216#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
217
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200218/* a few settings from the global section */
219static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200220static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200221static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200222
Willy Tarreau2a856182017-05-16 15:20:39 +0200223/* a dmumy closed stream */
224static const struct h2s *h2_closed_stream = &(const struct h2s){
225 .cs = NULL,
226 .h2c = NULL,
227 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100228 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100229 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200230 .id = 0,
231};
232
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100233/* a dmumy closed stream returning a REFUSED_STREAM error */
234static const struct h2s *h2_refused_stream = &(const struct h2s){
235 .cs = NULL,
236 .h2c = NULL,
237 .st = H2_SS_CLOSED,
238 .errcode = H2_ERR_REFUSED_STREAM,
239 .flags = 0,
240 .id = 0,
241};
242
Willy Tarreau2a856182017-05-16 15:20:39 +0200243/* and a dummy idle stream for use with any unannounced stream */
244static const struct h2s *h2_idle_stream = &(const struct h2s){
245 .cs = NULL,
246 .h2c = NULL,
247 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100248 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200249 .id = 0,
250};
251
Olivier Houchard9f6af332018-05-25 14:04:04 +0200252static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200253static int h2_send(struct h2c *h2c);
254static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200255static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200256static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100257static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +0100258static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100259static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200260static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100261static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100262static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200263
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200264/*****************************************************/
265/* functions below are for dynamic buffer management */
266/*****************************************************/
267
Willy Tarreau315d8072017-12-10 22:17:57 +0100268/* indicates whether or not the we may call the h2_recv() function to attempt
269 * to receive data into the buffer and/or demux pending data. The condition is
270 * a bit complex due to some API limits for now. The rules are the following :
271 * - if an error or a shutdown was detected on the connection and the buffer
272 * is empty, we must not attempt to receive
273 * - if the demux buf failed to be allocated, we must not try to receive and
274 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100275 * - if no flag indicates a blocking condition, we may attempt to receive,
276 * regardless of whether the demux buffer is full or not, so that only
277 * de demux part decides whether or not to block. This is needed because
278 * the connection API indeed prevents us from re-enabling receipt that is
279 * already enabled in a polled state, so we must always immediately stop
280 * as soon as the demux can't proceed so as never to hit an end of read
281 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100282 * - otherwise must may not attempt
283 */
284static inline int h2_recv_allowed(const struct h2c *h2c)
285{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200286 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100287 (h2c->st0 >= H2_CS_ERROR ||
288 h2c->conn->flags & CO_FL_ERROR ||
289 conn_xprt_read0_pending(h2c->conn)))
290 return 0;
291
292 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100293 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100294 return 1;
295
296 return 0;
297}
298
Willy Tarreau47b515a2018-12-21 16:09:41 +0100299/* restarts reading on the connection if it was not enabled */
300static inline void h2c_restart_reading(const struct h2c *h2c)
301{
302 if (!h2_recv_allowed(h2c))
303 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100304 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100305 return;
306 tasklet_wakeup(h2c->wait_event.task);
307}
308
309
Willy Tarreauf2101912018-07-19 10:11:38 +0200310/* returns true if the connection has too many conn_streams attached */
311static inline int h2_has_too_many_cs(const struct h2c *h2c)
312{
Willy Tarreaua8754662018-12-23 20:43:58 +0100313 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200314}
315
Willy Tarreau44e973f2018-03-01 17:49:30 +0100316/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
317 * flags are used to figure what buffer was requested. It returns 1 if the
318 * allocation succeeds, in which case the connection is woken up, or 0 if it's
319 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200320 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100321static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200322{
323 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100324 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200325
Willy Tarreau44e973f2018-03-01 17:49:30 +0100326 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200327 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100328 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200329 return 1;
330 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200331
Willy Tarreau44e973f2018-03-01 17:49:30 +0100332 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
333 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200334
335 if (h2c->flags & H2_CF_DEM_MROOM) {
336 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100337 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200338 }
Willy Tarreau14398122017-09-22 14:26:04 +0200339 return 1;
340 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100341
342 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
343 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200344 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100345 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100346 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100347 return 1;
348 }
349
Willy Tarreau14398122017-09-22 14:26:04 +0200350 return 0;
351}
352
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200353static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200354{
355 struct buffer *buf = NULL;
356
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
358 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
359 h2c->buf_wait.target = h2c;
360 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100361 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100363 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200364 __conn_xprt_stop_recv(h2c->conn);
365 }
366 return buf;
367}
368
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200369static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200370{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200371 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100372 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200373 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200374 }
375}
376
Olivier Houchardd540b362018-11-05 18:37:53 +0100377static int h2_avail_streams(struct connection *conn)
378{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100379 struct h2c *h2c = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100380
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100381 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100382 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
383}
384
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100385static int h2_max_streams(struct connection *conn)
386{
387 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
388 return h2_settings_max_concurrent_streams;
389}
390
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200391
Willy Tarreau62f52692017-10-08 23:01:42 +0200392/*****************************************************************/
393/* functions below are dedicated to the mux setup and management */
394/*****************************************************************/
395
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200396/* Initialize the mux once it's attached. For outgoing connections, the context
397 * is already initialized before installing the mux, so we detect incoming
398 * connections from the fact that the context is still NULL. Returns < 0 on
399 * error.
400 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100401static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402{
403 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100404 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200405
Willy Tarreaubafbe012017-11-24 17:34:44 +0100406 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200407 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200408 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100410 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200411 h2c->flags = H2_CF_IS_BACK;
412 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
413 if (tick_isset(prx->timeout.serverfin))
414 h2c->shut_timeout = prx->timeout.serverfin;
415 } else {
416 h2c->flags = H2_CF_NONE;
417 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
418 if (tick_isset(prx->timeout.clientfin))
419 h2c->shut_timeout = prx->timeout.clientfin;
420 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100421
Willy Tarreau0b37d652018-10-03 10:33:02 +0200422 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100423 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100424 if (tick_isset(h2c->timeout)) {
425 t = task_new(tid_bit);
426 if (!t)
427 goto fail;
428
429 h2c->task = t;
430 t->process = h2_timeout_task;
431 t->context = h2c;
432 t->expire = tick_add(now_ms, h2c->timeout);
433 }
Willy Tarreauea392822017-10-31 10:02:25 +0100434
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200435 h2c->wait_event.task = tasklet_new();
436 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200437 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200438 h2c->wait_event.task->process = h2_io_cb;
439 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100440 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200441
Willy Tarreau32218eb2017-09-22 08:07:25 +0200442 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
443 if (!h2c->ddht)
444 goto fail;
445
446 /* Initialise the context. */
447 h2c->st0 = H2_CS_PREFACE;
448 h2c->conn = conn;
449 h2c->max_id = -1;
450 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100451 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200452 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100453 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200454 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200456 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200457 h2c->dsi = -1;
458 h2c->msi = -1;
459 h2c->last_sid = -1;
460
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200461 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200462 h2c->miw = 65535; /* mux initial window size */
463 h2c->mws = 65535; /* mux window size */
464 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200465 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466 LIST_INIT(&h2c->send_list);
467 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200468 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100469 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470
Willy Tarreau3f133572017-10-31 19:21:06 +0100471 if (t)
472 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100473
Willy Tarreau01b44822018-10-03 14:26:37 +0200474 if (h2c->flags & H2_CF_IS_BACK) {
475 /* FIXME: this is temporary, for outgoing connections we need
476 * to immediately allocate a stream until the code is modified
477 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100478 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200479 */
480 struct h2s *h2s;
481
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100482 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200483 if (!h2s)
484 goto fail_stream;
485 }
486
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100487 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200488
Willy Tarreau0f383582018-10-03 14:22:21 +0200489 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100490 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200491 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200492 fail_stream:
493 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200494 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100495 if (t)
496 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200497 if (h2c->wait_event.task)
498 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100499 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200500 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200501 return -1;
502}
503
Willy Tarreau751f2d02018-10-05 09:35:00 +0200504/* returns the next allocatable outgoing stream ID for the H2 connection, or
505 * -1 if no more is allocatable.
506 */
507static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
508{
509 int32_t id = (h2c->max_id + 1) | 1;
510 if (id & 0x80000000U)
511 id = -1;
512 return id;
513}
514
Willy Tarreau2373acc2017-10-12 17:35:14 +0200515/* returns the stream associated with id <id> or NULL if not found */
516static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
517{
518 struct eb32_node *node;
519
Willy Tarreau751f2d02018-10-05 09:35:00 +0200520 if (id == 0)
521 return (struct h2s *)h2_closed_stream;
522
Willy Tarreau2a856182017-05-16 15:20:39 +0200523 if (id > h2c->max_id)
524 return (struct h2s *)h2_idle_stream;
525
Willy Tarreau2373acc2017-10-12 17:35:14 +0200526 node = eb32_lookup(&h2c->streams_by_id, id);
527 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200528 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200529
530 return container_of(node, struct h2s, by_id);
531}
532
Willy Tarreau62f52692017-10-08 23:01:42 +0200533/* release function for a connection. This one should be called to free all
534 * resources allocated to the mux.
535 */
536static void h2_release(struct connection *conn)
537{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100538 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539
540 LIST_DEL(&conn->list);
541
542 if (h2c) {
543 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200544
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100545 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100546 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200548
Willy Tarreau44e973f2018-03-01 17:49:30 +0100549 h2_release_buf(h2c, &h2c->dbuf);
550 h2_release_buf(h2c, &h2c->mbuf);
551
Willy Tarreauea392822017-10-31 10:02:25 +0100552 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200553 h2c->task->context = NULL;
554 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100555 h2c->task = NULL;
556 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200557 if (h2c->wait_event.task)
558 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100559 if (h2c->wait_event.events != 0)
560 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200561 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100562
Willy Tarreaubafbe012017-11-24 17:34:44 +0100563 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200564 }
565
566 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100567 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200568
569 conn_stop_tracking(conn);
570 conn_full_close(conn);
571 if (conn->destroy_cb)
572 conn->destroy_cb(conn);
573 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200574}
575
576
Willy Tarreau71681172017-10-23 14:39:06 +0200577/******************************************************/
578/* functions below are for the H2 protocol processing */
579/******************************************************/
580
581/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100582static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200583{
584 return h2s ? h2s->id : 0;
585}
586
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200587/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100588static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200589{
590 if (h2c->msi < 0)
591 return 0;
592
593 if (h2c->msi == h2s_id(h2s))
594 return 0;
595
596 return 1;
597}
598
Willy Tarreau741d6df2017-10-17 08:00:59 +0200599/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100600static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200601{
602 h2c->errcode = err;
603 h2c->st0 = H2_CS_ERROR;
604}
605
Willy Tarreau2e43f082017-10-17 08:03:59 +0200606/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100607static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200608{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200609 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200610 h2s->errcode = err;
611 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100612 if (h2s->cs)
613 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200614 }
615}
616
Willy Tarreau7e094452018-12-19 18:08:52 +0100617/* attempt to notify the data layer of recv availability */
618static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
619{
620 struct wait_event *sw;
621
622 if (h2s->recv_wait) {
623 sw = h2s->recv_wait;
624 sw->events &= ~SUB_RETRY_RECV;
625 tasklet_wakeup(sw->task);
626 h2s->recv_wait = NULL;
627 }
628}
629
630/* attempt to notify the data layer of send availability */
631static void __maybe_unused h2s_notify_send(struct h2s *h2s)
632{
633 struct wait_event *sw;
634
635 if (h2s->send_wait) {
636 sw = h2s->send_wait;
637 sw->events &= ~SUB_RETRY_SEND;
638 tasklet_wakeup(sw->task);
639 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100640 LIST_DEL(&h2s->list);
641 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100642 }
643}
644
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100645/* alerts the data layer, trying to wake it up by all means, following
646 * this sequence :
647 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
648 * - if its subscribed to send, then it's woken up for send
649 * - if it was subscribed to neither, its ->wake() callback is called
650 * It is safe to call this function with a closed stream which doesn't have a
651 * conn_stream anymore.
652 */
653static void __maybe_unused h2s_alert(struct h2s *h2s)
654{
655 if (h2s->recv_wait || h2s->send_wait) {
656 h2s_notify_recv(h2s);
657 h2s_notify_send(h2s);
658 }
659 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
660 h2s->cs->data_cb->wake(h2s->cs);
661}
662
Willy Tarreaue4820742017-07-27 13:37:23 +0200663/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100664static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200665{
666 uint8_t *out = frame;
667
668 *out = len >> 16;
669 write_n16(out + 1, len);
670}
671
Willy Tarreau54c15062017-10-10 17:10:03 +0200672/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
673 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
674 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200675 * available in the buffer's input prior to calling this function. The buffer
676 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200677 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100678static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200679 const struct buffer *b, int o)
680{
Willy Tarreau591d4452018-06-15 17:21:00 +0200681 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 +0200682}
683
Willy Tarreau1f094672017-11-20 21:27:45 +0100684static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200685{
Willy Tarreau591d4452018-06-15 17:21:00 +0200686 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200687}
688
Willy Tarreau1f094672017-11-20 21:27:45 +0100689static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200690{
Willy Tarreau591d4452018-06-15 17:21:00 +0200691 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200692}
693
Willy Tarreau1f094672017-11-20 21:27:45 +0100694static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200695{
Willy Tarreau591d4452018-06-15 17:21:00 +0200696 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200697}
698
699
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100700/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
701 * The algorithm is not obvious. It turns out that H2 headers are neither
702 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
703 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200704 *
705 * b0 b1 b2 b3 b4 b5..b8
706 * +----------+---------+--------+----+----+----------------------+
707 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
708 * +----------+---------+--------+----+----+----------------------+
709 *
710 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
711 * we get the sid properly aligned and ordered, and 16 bits of len properly
712 * ordered as well. The type and flags can be extracted using bit shifts from
713 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200714 * Returns zero if some bytes are missing, otherwise non-zero on success. The
715 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200716 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100717static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200718{
719 uint64_t w;
720
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100721 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200722 return 0;
723
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100724 w = h2_get_n64(b, o + 1);
725 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200726 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
727 h->ff = w >> 32;
728 h->ft = w >> 40;
729 h->len += w >> 48;
730 return 1;
731}
732
733/* skip the next 9 bytes corresponding to the frame header possibly parsed by
734 * h2_peek_frame_hdr() above.
735 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100736static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200737{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200738 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200739}
740
741/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100742static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200743{
744 int ret;
745
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100746 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200747 if (ret > 0)
748 h2_skip_frame_hdr(b);
749 return ret;
750}
751
Willy Tarreau00dd0782018-03-01 16:31:34 +0100752/* marks stream <h2s> as CLOSED and decrement the number of active streams for
753 * its connection if the stream was not yet closed. Please use this exclusively
754 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100755 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100756static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100757{
758 if (h2s->st != H2_SS_CLOSED)
759 h2s->h2c->nb_streams--;
760 h2s->st = H2_SS_CLOSED;
761}
762
Willy Tarreau71049cc2018-03-28 13:56:39 +0200763/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
764static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100765{
766 h2s_close(h2s);
767 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200768 if (b_size(&h2s->rxbuf)) {
769 b_free(&h2s->rxbuf);
770 offer_buffers(NULL, tasks_run_queue);
771 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200772 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100773 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200774 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100775 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800776 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200777 * reference left would be in the h2c send_list/fctl_list, and if
778 * we're in it, we're getting out anyway
779 */
780 LIST_DEL(&h2s->list);
781 LIST_INIT(&h2s->list);
782 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100783 pool_free(pool_head_h2s, h2s);
784}
785
Willy Tarreaua8e49542018-10-03 18:53:55 +0200786/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
787 * stream tree. In case of error, nothing is added and NULL is returned. The
788 * causes of errors can be any failed memory allocation. The caller is
789 * responsible for checking if the connection may support an extra stream
790 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200791 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200792static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200793{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200794 struct h2s *h2s;
795
Willy Tarreaubafbe012017-11-24 17:34:44 +0100796 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200797 if (!h2s)
798 goto out;
799
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200800 h2s->wait_event.task = tasklet_new();
801 if (!h2s->wait_event.task) {
802 pool_free(pool_head_h2s, h2s);
803 goto out;
804 }
805 h2s->send_wait = NULL;
806 h2s->recv_wait = NULL;
807 h2s->wait_event.task->process = h2_deferred_shut;
808 h2s->wait_event.task->context = h2s;
809 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100810 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200811 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200812 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200813 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200814 h2s->mws = h2c->miw;
815 h2s->flags = H2_SF_NONE;
816 h2s->errcode = H2_ERR_NO_ERROR;
817 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200818 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200819 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200820
821 if (h2c->flags & H2_CF_IS_BACK) {
822 h1m_init_req(&h2s->h1m);
823 h2s->h1m.err_pos = -1; // don't care about errors on the request path
824 h2s->h1m.flags |= H1_MF_TOLOWER;
825 } else {
826 h1m_init_res(&h2s->h1m);
827 h2s->h1m.err_pos = -1; // don't care about errors on the response path
828 h2s->h1m.flags |= H1_MF_TOLOWER;
829 }
830
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200831 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200832 if (id > 0)
833 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200834
835 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100836 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200837
838 return h2s;
839
840 out_free_h2s:
841 pool_free(pool_head_h2s, h2s);
842 out:
843 return NULL;
844}
845
846/* creates a new stream <id> on the h2c connection and returns it, or NULL in
847 * case of memory allocation error.
848 */
849static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
850{
851 struct session *sess = h2c->conn->owner;
852 struct conn_stream *cs;
853 struct h2s *h2s;
854
855 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
856 goto out;
857
858 h2s = h2s_new(h2c, id);
859 if (!h2s)
860 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200861
862 cs = cs_new(h2c->conn);
863 if (!cs)
864 goto out_close;
865
Olivier Houchard746fb772018-12-15 19:42:00 +0100866 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200867 h2s->cs = cs;
868 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200869 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200870
871 if (stream_create_from_cs(cs) < 0)
872 goto out_free_cs;
873
Willy Tarreau590a0512018-09-05 11:56:48 +0200874 /* We want the accept date presented to the next stream to be the one
875 * we have now, the handshake time to be null (since the next stream
876 * is not delayed by a handshake), and the idle time to count since
877 * right now.
878 */
879 sess->accept_date = date;
880 sess->tv_accept = now;
881 sess->t_handshake = 0;
882
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200884 if (h2_has_too_many_cs(h2c))
885 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200886 return h2s;
887
888 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200889 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200890 cs_free(cs);
891 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200892 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200894 sess_log(sess);
895 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200896}
897
Willy Tarreau751f2d02018-10-05 09:35:00 +0200898/* allocates a new stream associated to conn_stream <cs> on the h2c connection
899 * and returns it, or NULL in case of memory allocation error or if the highest
900 * possible stream ID was reached.
901 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100902static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200903{
904 struct h2s *h2s = NULL;
905
906 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
907 goto out;
908
909 /* Defer choosing the ID until we send the first message to create the stream */
910 h2s = h2s_new(h2c, 0);
911 if (!h2s)
912 goto out;
913
914 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100915 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200916 cs->ctx = h2s;
917 h2c->nb_cs++;
918
Willy Tarreau751f2d02018-10-05 09:35:00 +0200919 out:
920 return h2s;
921}
922
Willy Tarreaube5b7152017-09-25 16:25:39 +0200923/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
924 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
925 * the various settings codes.
926 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200927static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200928{
929 struct buffer *res;
930 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200931 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200932 int ret;
933
934 if (h2c_mux_busy(h2c, NULL)) {
935 h2c->flags |= H2_CF_DEM_MBUSY;
936 return 0;
937 }
938
Willy Tarreau44e973f2018-03-01 17:49:30 +0100939 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200940 if (!res) {
941 h2c->flags |= H2_CF_MUX_MALLOC;
942 h2c->flags |= H2_CF_DEM_MROOM;
943 return 0;
944 }
945
946 chunk_init(&buf, buf_data, sizeof(buf_data));
947 chunk_memcpy(&buf,
948 "\x00\x00\x00" /* length : 0 for now */
949 "\x04\x00" /* type : 4 (settings), flags : 0 */
950 "\x00\x00\x00\x00", /* stream ID : 0 */
951 9);
952
953 if (h2_settings_header_table_size != 4096) {
954 char str[6] = "\x00\x01"; /* header_table_size */
955
956 write_n32(str + 2, h2_settings_header_table_size);
957 chunk_memcat(&buf, str, 6);
958 }
959
960 if (h2_settings_initial_window_size != 65535) {
961 char str[6] = "\x00\x04"; /* initial_window_size */
962
963 write_n32(str + 2, h2_settings_initial_window_size);
964 chunk_memcat(&buf, str, 6);
965 }
966
967 if (h2_settings_max_concurrent_streams != 0) {
968 char str[6] = "\x00\x03"; /* max_concurrent_streams */
969
970 /* Note: 0 means "unlimited" for haproxy's config but not for
971 * the protocol, so never send this value!
972 */
973 write_n32(str + 2, h2_settings_max_concurrent_streams);
974 chunk_memcat(&buf, str, 6);
975 }
976
977 if (global.tune.bufsize != 16384) {
978 char str[6] = "\x00\x05"; /* max_frame_size */
979
980 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
981 * match bufsize - rewrite size, but at the moment it seems
982 * that clients don't take care of it.
983 */
984 write_n32(str + 2, global.tune.bufsize);
985 chunk_memcat(&buf, str, 6);
986 }
987
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200988 h2_set_frame_size(buf.area, buf.data - 9);
989 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200990 if (unlikely(ret <= 0)) {
991 if (!ret) {
992 h2c->flags |= H2_CF_MUX_MFULL;
993 h2c->flags |= H2_CF_DEM_MROOM;
994 return 0;
995 }
996 else {
997 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
998 return 0;
999 }
1000 }
1001 return ret;
1002}
1003
Willy Tarreau52eed752017-09-22 15:05:09 +02001004/* Try to receive a connection preface, then upon success try to send our
1005 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1006 * missing data. It may return an error in h2c.
1007 */
1008static int h2c_frt_recv_preface(struct h2c *h2c)
1009{
1010 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001011 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001012
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001013 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001014
1015 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001016 if (ret1 < 0)
1017 sess_log(h2c->conn->owner);
1018
Willy Tarreau52eed752017-09-22 15:05:09 +02001019 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1020 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1021 return 0;
1022 }
1023
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001024 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001025 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001026 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001027
Willy Tarreaube5b7152017-09-25 16:25:39 +02001028 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001029}
1030
Willy Tarreau01b44822018-10-03 14:26:37 +02001031/* Try to send a connection preface, then upon success try to send our
1032 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1033 * missing data. It may return an error in h2c.
1034 */
1035static int h2c_bck_send_preface(struct h2c *h2c)
1036{
1037 struct buffer *res;
1038
1039 if (h2c_mux_busy(h2c, NULL)) {
1040 h2c->flags |= H2_CF_DEM_MBUSY;
1041 return 0;
1042 }
1043
1044 res = h2_get_buf(h2c, &h2c->mbuf);
1045 if (!res) {
1046 h2c->flags |= H2_CF_MUX_MALLOC;
1047 h2c->flags |= H2_CF_DEM_MROOM;
1048 return 0;
1049 }
1050
1051 if (!b_data(res)) {
1052 /* preface not yet sent */
1053 b_istput(res, ist(H2_CONN_PREFACE));
1054 }
1055
1056 return h2c_send_settings(h2c);
1057}
1058
Willy Tarreau081d4722017-05-16 21:51:05 +02001059/* try to send a GOAWAY frame on the connection to report an error or a graceful
1060 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1061 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1062 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1063 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1064 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1065 * on unrecoverable failure. It will not attempt to send one again in this last
1066 * case so that it is safe to use h2c_error() to report such errors.
1067 */
1068static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1069{
1070 struct buffer *res;
1071 char str[17];
1072 int ret;
1073
1074 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1075 return 1; // claim that it worked
1076
1077 if (h2c_mux_busy(h2c, h2s)) {
1078 if (h2s)
1079 h2s->flags |= H2_SF_BLK_MBUSY;
1080 else
1081 h2c->flags |= H2_CF_DEM_MBUSY;
1082 return 0;
1083 }
1084
Willy Tarreau44e973f2018-03-01 17:49:30 +01001085 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001086 if (!res) {
1087 h2c->flags |= H2_CF_MUX_MALLOC;
1088 if (h2s)
1089 h2s->flags |= H2_SF_BLK_MROOM;
1090 else
1091 h2c->flags |= H2_CF_DEM_MROOM;
1092 return 0;
1093 }
1094
1095 /* len: 8, type: 7, flags: none, sid: 0 */
1096 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1097
1098 if (h2c->last_sid < 0)
1099 h2c->last_sid = h2c->max_id;
1100
1101 write_n32(str + 9, h2c->last_sid);
1102 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001103 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001104 if (unlikely(ret <= 0)) {
1105 if (!ret) {
1106 h2c->flags |= H2_CF_MUX_MFULL;
1107 if (h2s)
1108 h2s->flags |= H2_SF_BLK_MROOM;
1109 else
1110 h2c->flags |= H2_CF_DEM_MROOM;
1111 return 0;
1112 }
1113 else {
1114 /* we cannot report this error using GOAWAY, so we mark
1115 * it and claim a success.
1116 */
1117 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1118 h2c->flags |= H2_CF_GOAWAY_FAILED;
1119 return 1;
1120 }
1121 }
1122 h2c->flags |= H2_CF_GOAWAY_SENT;
1123 return ret;
1124}
1125
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001126/* Try to send an RST_STREAM frame on the connection for the indicated stream
1127 * during mux operations. This stream must be valid and cannot be closed
1128 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1129 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1130 * not yet.
1131 *
1132 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1133 * to write the message, it subscribes the stream to future notifications.
1134 */
1135static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1136{
1137 struct buffer *res;
1138 char str[13];
1139 int ret;
1140
1141 if (!h2s || h2s->st == H2_SS_CLOSED)
1142 return 1;
1143
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001144 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1145 * RST_STREAM in response to a RST_STREAM frame.
1146 */
1147 if (h2c->dft == H2_FT_RST_STREAM) {
1148 ret = 1;
1149 goto ignore;
1150 }
1151
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001152 if (h2c_mux_busy(h2c, h2s)) {
1153 h2s->flags |= H2_SF_BLK_MBUSY;
1154 return 0;
1155 }
1156
Willy Tarreau44e973f2018-03-01 17:49:30 +01001157 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001158 if (!res) {
1159 h2c->flags |= H2_CF_MUX_MALLOC;
1160 h2s->flags |= H2_SF_BLK_MROOM;
1161 return 0;
1162 }
1163
1164 /* len: 4, type: 3, flags: none */
1165 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1166 write_n32(str + 5, h2s->id);
1167 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001168 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001169
1170 if (unlikely(ret <= 0)) {
1171 if (!ret) {
1172 h2c->flags |= H2_CF_MUX_MFULL;
1173 h2s->flags |= H2_SF_BLK_MROOM;
1174 return 0;
1175 }
1176 else {
1177 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1178 return 0;
1179 }
1180 }
1181
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001182 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001183 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001184 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001185 return ret;
1186}
1187
1188/* Try to send an RST_STREAM frame on the connection for the stream being
1189 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001190 * error code, even if the stream is one of the dummy ones, and will update
1191 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001192 *
1193 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1194 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001195 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001196 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001197 */
1198static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1199{
1200 struct buffer *res;
1201 char str[13];
1202 int ret;
1203
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001204 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1205 * RST_STREAM in response to a RST_STREAM frame.
1206 */
1207 if (h2c->dft == H2_FT_RST_STREAM) {
1208 ret = 1;
1209 goto ignore;
1210 }
1211
Willy Tarreau27a84c92017-10-17 08:10:17 +02001212 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001213 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001214 return 0;
1215 }
1216
Willy Tarreau44e973f2018-03-01 17:49:30 +01001217 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001218 if (!res) {
1219 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001220 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001221 return 0;
1222 }
1223
1224 /* len: 4, type: 3, flags: none */
1225 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001226
Willy Tarreau27a84c92017-10-17 08:10:17 +02001227 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001228 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001229 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001230
Willy Tarreau27a84c92017-10-17 08:10:17 +02001231 if (unlikely(ret <= 0)) {
1232 if (!ret) {
1233 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001234 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001235 return 0;
1236 }
1237 else {
1238 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1239 return 0;
1240 }
1241 }
1242
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001243 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001244 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001245 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001246 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001247 }
1248
Willy Tarreau27a84c92017-10-17 08:10:17 +02001249 return ret;
1250}
1251
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001252/* try to send an empty DATA frame with the ES flag set to notify about the
1253 * end of stream and match a shutdown(write). If an ES was already sent as
1254 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1255 * on success or zero if nothing was done. In case of lack of room to write the
1256 * message, it subscribes the requesting stream to future notifications.
1257 */
1258static int h2_send_empty_data_es(struct h2s *h2s)
1259{
1260 struct h2c *h2c = h2s->h2c;
1261 struct buffer *res;
1262 char str[9];
1263 int ret;
1264
Willy Tarreau721c9742017-11-07 11:05:42 +01001265 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001266 return 1;
1267
1268 if (h2c_mux_busy(h2c, h2s)) {
1269 h2s->flags |= H2_SF_BLK_MBUSY;
1270 return 0;
1271 }
1272
Willy Tarreau44e973f2018-03-01 17:49:30 +01001273 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001274 if (!res) {
1275 h2c->flags |= H2_CF_MUX_MALLOC;
1276 h2s->flags |= H2_SF_BLK_MROOM;
1277 return 0;
1278 }
1279
1280 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1281 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1282 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001283 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001284 if (likely(ret > 0)) {
1285 h2s->flags |= H2_SF_ES_SENT;
1286 }
1287 else if (!ret) {
1288 h2c->flags |= H2_CF_MUX_MFULL;
1289 h2s->flags |= H2_SF_BLK_MROOM;
1290 return 0;
1291 }
1292 else {
1293 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1294 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001295 }
1296 return ret;
1297}
1298
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001299/* wake the streams attached to the connection, whose id is greater than <last>,
1300 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001301 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1302 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001303 */
1304static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1305{
1306 struct eb32_node *node;
1307 struct h2s *h2s;
1308
1309 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001310 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001311
1312 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001313 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001314
1315 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1316 while (node) {
1317 h2s = container_of(node, struct h2s, by_id);
1318 if (h2s->id <= last)
1319 break;
1320 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001321
1322 if (!h2s->cs) {
1323 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001324 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001325 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001326 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001327
1328 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001329 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1330 h2s->cs->flags |= CS_FL_ERROR;
1331
Willy Tarreauf830f012018-12-19 17:44:55 +01001332 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001333
Willy Tarreaua8519352018-12-18 16:44:28 +01001334 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001335 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001336 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001337 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001338 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001339 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001340 }
1341}
1342
Willy Tarreau3421aba2017-07-27 15:41:03 +02001343/* Increase all streams' outgoing window size by the difference passed in
1344 * argument. This is needed upon receipt of the settings frame if the initial
1345 * window size is different. The difference may be negative and the resulting
1346 * window size as well, for the time it takes to receive some window updates.
1347 */
1348static void h2c_update_all_ws(struct h2c *h2c, int diff)
1349{
1350 struct h2s *h2s;
1351 struct eb32_node *node;
1352
1353 if (!diff)
1354 return;
1355
1356 node = eb32_first(&h2c->streams_by_id);
1357 while (node) {
1358 h2s = container_of(node, struct h2s, by_id);
1359 h2s->mws += diff;
1360 node = eb32_next(node);
1361 }
1362}
1363
1364/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1365 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1366 * return an error in h2c. Described in RFC7540#6.5.
1367 */
1368static int h2c_handle_settings(struct h2c *h2c)
1369{
1370 unsigned int offset;
1371 int error;
1372
1373 if (h2c->dff & H2_F_SETTINGS_ACK) {
1374 if (h2c->dfl) {
1375 error = H2_ERR_FRAME_SIZE_ERROR;
1376 goto fail;
1377 }
1378 return 1;
1379 }
1380
1381 if (h2c->dsi != 0) {
1382 error = H2_ERR_PROTOCOL_ERROR;
1383 goto fail;
1384 }
1385
1386 if (h2c->dfl % 6) {
1387 error = H2_ERR_FRAME_SIZE_ERROR;
1388 goto fail;
1389 }
1390
1391 /* that's the limit we can process */
1392 if (h2c->dfl > global.tune.bufsize) {
1393 error = H2_ERR_FRAME_SIZE_ERROR;
1394 goto fail;
1395 }
1396
1397 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001398 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001399 return 0;
1400
1401 /* parse the frame */
1402 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001403 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1404 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001405
1406 switch (type) {
1407 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1408 /* we need to update all existing streams with the
1409 * difference from the previous iws.
1410 */
1411 if (arg < 0) { // RFC7540#6.5.2
1412 error = H2_ERR_FLOW_CONTROL_ERROR;
1413 goto fail;
1414 }
1415 h2c_update_all_ws(h2c, arg - h2c->miw);
1416 h2c->miw = arg;
1417 break;
1418 case H2_SETTINGS_MAX_FRAME_SIZE:
1419 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1420 error = H2_ERR_PROTOCOL_ERROR;
1421 goto fail;
1422 }
1423 h2c->mfs = arg;
1424 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001425 case H2_SETTINGS_ENABLE_PUSH:
1426 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1427 error = H2_ERR_PROTOCOL_ERROR;
1428 goto fail;
1429 }
1430 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001431 }
1432 }
1433
1434 /* need to ACK this frame now */
1435 h2c->st0 = H2_CS_FRAME_A;
1436 return 1;
1437 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001438 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001439 h2c_error(h2c, error);
1440 return 0;
1441}
1442
1443/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1444 * success or one of the h2_status values.
1445 */
1446static int h2c_ack_settings(struct h2c *h2c)
1447{
1448 struct buffer *res;
1449 char str[9];
1450 int ret = -1;
1451
1452 if (h2c_mux_busy(h2c, NULL)) {
1453 h2c->flags |= H2_CF_DEM_MBUSY;
1454 return 0;
1455 }
1456
Willy Tarreau44e973f2018-03-01 17:49:30 +01001457 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001458 if (!res) {
1459 h2c->flags |= H2_CF_MUX_MALLOC;
1460 h2c->flags |= H2_CF_DEM_MROOM;
1461 return 0;
1462 }
1463
1464 memcpy(str,
1465 "\x00\x00\x00" /* length : 0 (no data) */
1466 "\x04" "\x01" /* type : 4, flags : ACK */
1467 "\x00\x00\x00\x00" /* stream ID */, 9);
1468
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001469 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001470 if (unlikely(ret <= 0)) {
1471 if (!ret) {
1472 h2c->flags |= H2_CF_MUX_MFULL;
1473 h2c->flags |= H2_CF_DEM_MROOM;
1474 return 0;
1475 }
1476 else {
1477 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1478 return 0;
1479 }
1480 }
1481 return ret;
1482}
1483
Willy Tarreaucf68c782017-10-10 17:11:41 +02001484/* processes a PING frame and schedules an ACK if needed. The caller must pass
1485 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1486 * missing data. It may return an error in h2c.
1487 */
1488static int h2c_handle_ping(struct h2c *h2c)
1489{
1490 /* frame length must be exactly 8 */
1491 if (h2c->dfl != 8) {
1492 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1493 return 0;
1494 }
1495
1496 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001497 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001498 h2c->st0 = H2_CS_FRAME_A;
1499 return 1;
1500}
1501
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001502/* Try to send a window update for stream id <sid> and value <increment>.
1503 * Returns > 0 on success or zero on missing room or failure. It may return an
1504 * error in h2c.
1505 */
1506static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1507{
1508 struct buffer *res;
1509 char str[13];
1510 int ret = -1;
1511
1512 if (h2c_mux_busy(h2c, NULL)) {
1513 h2c->flags |= H2_CF_DEM_MBUSY;
1514 return 0;
1515 }
1516
Willy Tarreau44e973f2018-03-01 17:49:30 +01001517 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001518 if (!res) {
1519 h2c->flags |= H2_CF_MUX_MALLOC;
1520 h2c->flags |= H2_CF_DEM_MROOM;
1521 return 0;
1522 }
1523
1524 /* length: 4, type: 8, flags: none */
1525 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1526 write_n32(str + 5, sid);
1527 write_n32(str + 9, increment);
1528
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001529 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001530
1531 if (unlikely(ret <= 0)) {
1532 if (!ret) {
1533 h2c->flags |= H2_CF_MUX_MFULL;
1534 h2c->flags |= H2_CF_DEM_MROOM;
1535 return 0;
1536 }
1537 else {
1538 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1539 return 0;
1540 }
1541 }
1542 return ret;
1543}
1544
1545/* try to send pending window update for the connection. It's safe to call it
1546 * with no pending updates. Returns > 0 on success or zero on missing room or
1547 * failure. It may return an error in h2c.
1548 */
1549static int h2c_send_conn_wu(struct h2c *h2c)
1550{
1551 int ret = 1;
1552
1553 if (h2c->rcvd_c <= 0)
1554 return 1;
1555
Willy Tarreau97aaa672018-12-23 09:49:04 +01001556 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1557 /* increase the advertised connection window to 2G on
1558 * first update.
1559 */
1560 h2c->flags |= H2_CF_WINDOW_OPENED;
1561 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1562 }
1563
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001564 /* send WU for the connection */
1565 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1566 if (ret > 0)
1567 h2c->rcvd_c = 0;
1568
1569 return ret;
1570}
1571
1572/* try to send pending window update for the current dmux stream. It's safe to
1573 * call it with no pending updates. Returns > 0 on success or zero on missing
1574 * room or failure. It may return an error in h2c.
1575 */
1576static int h2c_send_strm_wu(struct h2c *h2c)
1577{
1578 int ret = 1;
1579
1580 if (h2c->rcvd_s <= 0)
1581 return 1;
1582
1583 /* send WU for the stream */
1584 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1585 if (ret > 0)
1586 h2c->rcvd_s = 0;
1587
1588 return ret;
1589}
1590
Willy Tarreaucf68c782017-10-10 17:11:41 +02001591/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1592 * success, 0 on missing data or one of the h2_status values.
1593 */
1594static int h2c_ack_ping(struct h2c *h2c)
1595{
1596 struct buffer *res;
1597 char str[17];
1598 int ret = -1;
1599
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001600 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001601 return 0;
1602
1603 if (h2c_mux_busy(h2c, NULL)) {
1604 h2c->flags |= H2_CF_DEM_MBUSY;
1605 return 0;
1606 }
1607
Willy Tarreau44e973f2018-03-01 17:49:30 +01001608 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001609 if (!res) {
1610 h2c->flags |= H2_CF_MUX_MALLOC;
1611 h2c->flags |= H2_CF_DEM_MROOM;
1612 return 0;
1613 }
1614
1615 memcpy(str,
1616 "\x00\x00\x08" /* length : 8 (same payload) */
1617 "\x06" "\x01" /* type : 6, flags : ACK */
1618 "\x00\x00\x00\x00" /* stream ID */, 9);
1619
1620 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001621 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001622
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001623 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001624 if (unlikely(ret <= 0)) {
1625 if (!ret) {
1626 h2c->flags |= H2_CF_MUX_MFULL;
1627 h2c->flags |= H2_CF_DEM_MROOM;
1628 return 0;
1629 }
1630 else {
1631 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1632 return 0;
1633 }
1634 }
1635 return ret;
1636}
1637
Willy Tarreau26f95952017-07-27 17:18:30 +02001638/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1639 * Returns > 0 on success or zero on missing data. It may return an error in
1640 * h2c or h2s. Described in RFC7540#6.9.
1641 */
1642static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1643{
1644 int32_t inc;
1645 int error;
1646
1647 if (h2c->dfl != 4) {
1648 error = H2_ERR_FRAME_SIZE_ERROR;
1649 goto conn_err;
1650 }
1651
1652 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001653 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001654 return 0;
1655
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001656 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001657
1658 if (h2c->dsi != 0) {
1659 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001660
1661 /* it's not an error to receive WU on a closed stream */
1662 if (h2s->st == H2_SS_CLOSED)
1663 return 1;
1664
1665 if (!inc) {
1666 error = H2_ERR_PROTOCOL_ERROR;
1667 goto strm_err;
1668 }
1669
1670 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1671 error = H2_ERR_FLOW_CONTROL_ERROR;
1672 goto strm_err;
1673 }
1674
1675 h2s->mws += inc;
1676 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1677 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001678 if (h2s->send_wait)
1679 LIST_ADDQ(&h2c->send_list, &h2s->list);
1680
Willy Tarreau26f95952017-07-27 17:18:30 +02001681 }
1682 }
1683 else {
1684 /* connection window update */
1685 if (!inc) {
1686 error = H2_ERR_PROTOCOL_ERROR;
1687 goto conn_err;
1688 }
1689
1690 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1691 error = H2_ERR_FLOW_CONTROL_ERROR;
1692 goto conn_err;
1693 }
1694
1695 h2c->mws += inc;
1696 }
1697
1698 return 1;
1699
1700 conn_err:
1701 h2c_error(h2c, error);
1702 return 0;
1703
1704 strm_err:
1705 if (h2s) {
1706 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001707 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001708 }
1709 else
1710 h2c_error(h2c, error);
1711 return 0;
1712}
1713
Willy Tarreaue96b0922017-10-30 00:28:29 +01001714/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1715 * the last ID. Returns > 0 on success or zero on missing data. It may return
1716 * an error in h2c. Described in RFC7540#6.8.
1717 */
1718static int h2c_handle_goaway(struct h2c *h2c)
1719{
1720 int error;
1721 int last;
1722
1723 if (h2c->dsi != 0) {
1724 error = H2_ERR_PROTOCOL_ERROR;
1725 goto conn_err;
1726 }
1727
1728 if (h2c->dfl < 8) {
1729 error = H2_ERR_FRAME_SIZE_ERROR;
1730 goto conn_err;
1731 }
1732
1733 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001734 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001735 return 0;
1736
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001737 last = h2_get_n32(&h2c->dbuf, 0);
1738 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001739 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001740 if (h2c->last_sid < 0)
1741 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001742 return 1;
1743
1744 conn_err:
1745 h2c_error(h2c, error);
1746 return 0;
1747}
1748
Willy Tarreau92153fc2017-12-03 19:46:19 +01001749/* processes a PRIORITY frame, and either skips it or rejects if it is
1750 * invalid. Returns > 0 on success or zero on missing data. It may return
1751 * an error in h2c. Described in RFC7540#6.3.
1752 */
1753static int h2c_handle_priority(struct h2c *h2c)
1754{
1755 int error;
1756
1757 if (h2c->dsi == 0) {
1758 error = H2_ERR_PROTOCOL_ERROR;
1759 goto conn_err;
1760 }
1761
1762 if (h2c->dfl != 5) {
1763 error = H2_ERR_FRAME_SIZE_ERROR;
1764 goto conn_err;
1765 }
1766
1767 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001768 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001769 return 0;
1770
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001771 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001772 /* 7540#5.3 : can't depend on itself */
1773 error = H2_ERR_PROTOCOL_ERROR;
1774 goto conn_err;
1775 }
1776 return 1;
1777
1778 conn_err:
1779 h2c_error(h2c, error);
1780 return 0;
1781}
1782
Willy Tarreaucd234e92017-08-18 10:59:39 +02001783/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1784 * Returns > 0 on success or zero on missing data. It may return an error in
1785 * h2c. Described in RFC7540#6.4.
1786 */
1787static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1788{
1789 int error;
1790
1791 if (h2c->dsi == 0) {
1792 error = H2_ERR_PROTOCOL_ERROR;
1793 goto conn_err;
1794 }
1795
Willy Tarreaucd234e92017-08-18 10:59:39 +02001796 if (h2c->dfl != 4) {
1797 error = H2_ERR_FRAME_SIZE_ERROR;
1798 goto conn_err;
1799 }
1800
1801 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001802 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001803 return 0;
1804
1805 /* late RST, already handled */
1806 if (h2s->st == H2_SS_CLOSED)
1807 return 1;
1808
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001809 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001810 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001811
1812 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001813 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001814 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001815 }
1816
1817 h2s->flags |= H2_SF_RST_RCVD;
1818 return 1;
1819
1820 conn_err:
1821 h2c_error(h2c, error);
1822 return 0;
1823}
1824
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001825/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1826 * It may return an error in h2c or h2s. The caller must consider that the
1827 * return value is the new h2s in case one was allocated (most common case).
1828 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001829 * errors here are reported as connection errors since it's impossible to
1830 * recover from such errors after the compression context has been altered.
1831 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001832static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001833{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001834 struct buffer rxbuf = BUF_NULL;
1835 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001836 int error;
1837
1838 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001839 /* RFC7540#4.2 */
1840 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001841 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001842 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001843 }
1844
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001845 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001846 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001847
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001848 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001849 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001850
Willy Tarreauf2101912018-07-19 10:11:38 +02001851 if (h2c->flags & H2_CF_DEM_TOOMANY)
1852 return 0; // too many cs still present
1853
Willy Tarreau13278b42017-10-13 19:23:14 +02001854 /* now either the frame is complete or the buffer is complete */
1855 if (h2s->st != H2_SS_IDLE) {
1856 /* FIXME: stream already exists, this is only allowed for
1857 * trailers (not supported for now).
1858 */
1859 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001860 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001861 goto conn_err;
1862 }
1863 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1864 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1865 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001866 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001867 goto conn_err;
1868 }
1869
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001870 if (!h2c_decode_headers(h2c, &rxbuf, &flags))
1871 goto out;
1872
1873 if (h2c->st0 >= H2_CS_ERROR)
1874 goto out;
1875
Willy Tarreau22de8d32018-09-05 19:55:58 +02001876 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001877 * positively from h2c_frt_stream_new(), the stream will report the error,
1878 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001879 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001880 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001881 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001882 h2s = (struct h2s*)h2_refused_stream;
1883 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001884 }
1885
1886 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001887 h2s->rxbuf = rxbuf;
1888 h2s->flags |= flags;
1889
1890 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001891 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001892
1893 if (h2s->flags & H2_SF_ES_RCVD) {
1894 h2s->st = H2_SS_HREM;
Willy Tarreau39d68502018-03-02 12:26:37 +01001895 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001896 }
1897
Willy Tarreau721c9742017-11-07 11:05:42 +01001898 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001899 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001900 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001901 }
1902 else {
1903 /* update the max stream ID if the request is being processed */
1904 if (h2s->id > h2c->max_id)
1905 h2c->max_id = h2s->id;
1906 }
1907
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001908 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001909
1910 conn_err:
1911 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001912 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001913
1914 strm_err:
1915 if (h2s) {
1916 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001917 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001918 }
1919 else
1920 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001921 out:
1922 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001923 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001924
1925 send_rst:
1926 /* make the demux send an RST for the current stream. We may only
1927 * do this if we're certain that the HEADERS frame was properly
1928 * decompressed so that the HPACK decoder is still kept up to date.
1929 */
1930 h2_release_buf(h2c, &rxbuf);
1931 h2c->st0 = H2_CS_FRAME_E;
1932 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001933}
1934
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001935/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1936 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1937 * errors here are reported as connection errors since it's impossible to
1938 * recover from such errors after the compression context has been altered.
1939 */
1940static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1941{
1942 int error;
1943
1944 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001945 /* RFC7540#4.2 */
1946 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001947 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001948 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001949 }
1950
1951 if (!b_size(&h2c->dbuf))
1952 return NULL; // empty buffer
1953
1954 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1955 return NULL; // incomplete frame
1956
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001957 if (!h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags))
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001958 return NULL;
1959
1960 if (h2c->st0 >= H2_CS_ERROR)
1961 return NULL;
1962
1963 if (h2s->st >= H2_SS_ERROR) {
1964 /* stream error : send RST_STREAM */
1965 h2c->st0 = H2_CS_FRAME_E;
1966 }
1967
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01001968 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1969 h2s->flags |= H2_SF_ES_RCVD;
1970 h2s->cs->flags |= CS_FL_REOS;
1971 }
1972
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001973 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1974 h2s->st = H2_SS_ERROR;
1975 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1976 h2s->st = H2_SS_HREM;
1977 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1978 h2s_close(h2s);
1979
1980 return h2s;
1981
1982 conn_err:
1983 h2c_error(h2c, error);
1984 return NULL;
1985
1986 strm_err:
1987 if (h2s) {
1988 h2s_error(h2s, error);
1989 h2c->st0 = H2_CS_FRAME_E;
1990 }
1991 else
1992 h2c_error(h2c, error);
1993 return NULL;
1994}
1995
Willy Tarreau454f9052017-10-26 19:40:35 +02001996/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1997 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1998 */
1999static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2000{
2001 int error;
2002
2003 /* note that empty DATA frames are perfectly valid and sometimes used
2004 * to signal an end of stream (with the ES flag).
2005 */
2006
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002007 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002008 return 0; // empty buffer
2009
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002010 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002011 return 0; // incomplete frame
2012
2013 /* now either the frame is complete or the buffer is complete */
2014
2015 if (!h2c->dsi) {
2016 /* RFC7540#6.1 */
2017 error = H2_ERR_PROTOCOL_ERROR;
2018 goto conn_err;
2019 }
2020
2021 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2022 /* RFC7540#6.1 */
2023 error = H2_ERR_STREAM_CLOSED;
2024 goto strm_err;
2025 }
2026
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002027 if (!h2_frt_transfer_data(h2s))
2028 return 0;
2029
Willy Tarreau454f9052017-10-26 19:40:35 +02002030 /* call the upper layers to process the frame, then let the upper layer
2031 * notify the stream about any change.
2032 */
2033 if (!h2s->cs) {
2034 error = H2_ERR_STREAM_CLOSED;
2035 goto strm_err;
2036 }
2037
Willy Tarreau8f650c32017-11-21 19:36:21 +01002038 if (h2c->st0 >= H2_CS_ERROR)
2039 return 0;
2040
Willy Tarreau721c9742017-11-07 11:05:42 +01002041 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002042 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002043 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002044 }
2045
2046 /* check for completion : the callee will change this to FRAME_A or
2047 * FRAME_H once done.
2048 */
2049 if (h2c->st0 == H2_CS_FRAME_P)
2050 return 0;
2051
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002052
2053 /* last frame */
2054 if (h2c->dff & H2_F_DATA_END_STREAM) {
2055 h2s->st = H2_SS_HREM;
2056 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002057 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002058 }
2059
Willy Tarreau454f9052017-10-26 19:40:35 +02002060 return 1;
2061
2062 conn_err:
2063 h2c_error(h2c, error);
2064 return 0;
2065
2066 strm_err:
2067 if (h2s) {
2068 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002069 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002070 }
2071 else
2072 h2c_error(h2c, error);
2073 return 0;
2074}
2075
Willy Tarreaubc933932017-10-09 16:21:43 +02002076/* process Rx frames to be demultiplexed */
2077static void h2_process_demux(struct h2c *h2c)
2078{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002079 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002080
Willy Tarreau081d4722017-05-16 21:51:05 +02002081 if (h2c->st0 >= H2_CS_ERROR)
2082 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002083
2084 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2085 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002086 if (h2c->flags & H2_CF_IS_BACK)
2087 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002088 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2089 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002090 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002091 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002092 sess_log(h2c->conn->owner);
2093 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002094 goto fail;
2095 }
2096
2097 h2c->max_id = 0;
2098 h2c->st0 = H2_CS_SETTINGS1;
2099 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002100
2101 if (h2c->st0 == H2_CS_SETTINGS1) {
2102 struct h2_fh hdr;
2103
2104 /* ensure that what is pending is a valid SETTINGS frame
2105 * without an ACK.
2106 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002107 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002108 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002109 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002110 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002111 sess_log(h2c->conn->owner);
2112 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002113 goto fail;
2114 }
2115
2116 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2117 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2118 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2119 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002120 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002121 goto fail;
2122 }
2123
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002124 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002125 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2126 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2127 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002128 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002129 goto fail;
2130 }
2131
Willy Tarreau3bf69182018-12-21 15:34:50 +01002132 /* that's OK, switch to FRAME_P to process it. This is
2133 * a SETTINGS frame whose header has already been
2134 * deleted above.
2135 */
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002136 h2c->dfl = hdr.len;
2137 h2c->dsi = hdr.sid;
2138 h2c->dft = hdr.ft;
2139 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002140 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002141 h2c->st0 = H2_CS_FRAME_P;
2142 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002143 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002144
2145 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002146 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002147 int ret = 0;
2148
2149 if (h2c->st0 >= H2_CS_ERROR)
2150 break;
2151
2152 if (h2c->st0 == H2_CS_FRAME_H) {
2153 struct h2_fh hdr;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002154 unsigned int padlen = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002155
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002156 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002157 break;
2158
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002159 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002160 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2161 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002162 if (!h2c->nb_streams) {
2163 /* only log if no other stream can report the error */
2164 sess_log(h2c->conn->owner);
2165 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002166 break;
2167 }
2168
Willy Tarreau3bf69182018-12-21 15:34:50 +01002169 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2170 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2171 * we read the pad length and drop it from the remaining
2172 * payload (one byte + the 9 remaining ones = 10 total
2173 * removed), so we have a frame payload starting after the
2174 * pad len. Flow controlled frames (DATA) also count the
2175 * padlen in the flow control, so it must be adjusted.
2176 */
2177 if (hdr.len < 1) {
2178 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2179 sess_log(h2c->conn->owner);
2180 goto fail;
2181 }
2182 hdr.len--;
2183
2184 if (b_data(&h2c->dbuf) < 10)
2185 break; // missing padlen
2186
2187 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2188
2189 if (padlen > hdr.len) {
2190 /* RFC7540#6.1 : pad length = length of
2191 * frame payload or greater => error.
2192 */
2193 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2194 sess_log(h2c->conn->owner);
2195 goto fail;
2196 }
2197
2198 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2199 h2c->rcvd_c++;
2200 h2c->rcvd_s++;
2201 }
2202 b_del(&h2c->dbuf, 1);
2203 }
2204 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002205 h2c->dfl = hdr.len;
2206 h2c->dsi = hdr.sid;
2207 h2c->dft = hdr.ft;
2208 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002209 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002210 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002211 }
2212
2213 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002214 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2215
Willy Tarreau567beb82018-12-18 16:52:44 +01002216 if (tmp_h2s != h2s && h2s && h2s->cs &&
2217 (b_data(&h2s->rxbuf) ||
2218 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002219 /* we may have to signal the upper layers */
2220 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002221 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002222 }
2223 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002224
Willy Tarreaud7901432017-12-29 11:34:40 +01002225 if (h2c->st0 == H2_CS_FRAME_E)
2226 goto strm_err;
2227
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002228 if (h2s->st == H2_SS_IDLE &&
2229 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2230 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2231 * this state MUST be treated as a connection error
2232 */
2233 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2234 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002235 if (!h2c->nb_streams) {
2236 /* only log if no other stream can report the error */
2237 sess_log(h2c->conn->owner);
2238 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002239 break;
2240 }
2241
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002242 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2243 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2244 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2245 * this state MUST be treated as a stream error
2246 */
2247 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2248 goto strm_err;
2249 }
2250
Willy Tarreauab837502017-12-27 15:07:30 +01002251 /* Below the management of frames received in closed state is a
2252 * bit hackish because the spec makes strong differences between
2253 * streams closed by receiving RST, sending RST, and seeing ES
2254 * in both directions. In addition to this, the creation of a
2255 * new stream reusing the identifier of a closed one will be
2256 * detected here. Given that we cannot keep track of all closed
2257 * streams forever, we consider that unknown closed streams were
2258 * closed on RST received, which allows us to respond with an
2259 * RST without breaking the connection (eg: to abort a transfer).
2260 * Some frames have to be silently ignored as well.
2261 */
2262 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2263 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2264 /* #5.1.1: The identifier of a newly
2265 * established stream MUST be numerically
2266 * greater than all streams that the initiating
2267 * endpoint has opened or reserved. This
2268 * governs streams that are opened using a
2269 * HEADERS frame and streams that are reserved
2270 * using PUSH_PROMISE. An endpoint that
2271 * receives an unexpected stream identifier
2272 * MUST respond with a connection error.
2273 */
2274 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2275 goto strm_err;
2276 }
2277
2278 if (h2s->flags & H2_SF_RST_RCVD) {
2279 /* RFC7540#5.1:closed: an endpoint that
2280 * receives any frame other than PRIORITY after
2281 * receiving a RST_STREAM MUST treat that as a
2282 * stream error of type STREAM_CLOSED.
2283 *
2284 * Note that old streams fall into this category
2285 * and will lead to an RST being sent.
2286 */
2287 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2288 h2c->st0 = H2_CS_FRAME_E;
2289 goto strm_err;
2290 }
2291
2292 /* RFC7540#5.1:closed: if this state is reached as a
2293 * result of sending a RST_STREAM frame, the peer that
2294 * receives the RST_STREAM might have already sent
2295 * frames on the stream that cannot be withdrawn. An
2296 * endpoint MUST ignore frames that it receives on
2297 * closed streams after it has sent a RST_STREAM
2298 * frame. An endpoint MAY choose to limit the period
2299 * over which it ignores frames and treat frames that
2300 * arrive after this time as being in error.
2301 */
2302 if (!(h2s->flags & H2_SF_RST_SENT)) {
2303 /* RFC7540#5.1:closed: any frame other than
2304 * PRIO/WU/RST in this state MUST be treated as
2305 * a connection error
2306 */
2307 if (h2c->dft != H2_FT_RST_STREAM &&
2308 h2c->dft != H2_FT_PRIORITY &&
2309 h2c->dft != H2_FT_WINDOW_UPDATE) {
2310 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2311 goto strm_err;
2312 }
2313 }
2314 }
2315
Willy Tarreauc0da1962017-10-30 18:38:00 +01002316#if 0
2317 // problem below: it is not possible to completely ignore such
2318 // streams as we need to maintain the compression state as well
2319 // and for this we need to completely process these frames (eg:
2320 // HEADERS frames) as well as counting DATA frames to emit
2321 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2322 // This is a typical case of layer violation where the
2323 // transported contents are critical to the connection's
2324 // validity and must be ignored at the same time :-(
2325
2326 /* graceful shutdown, ignore streams whose ID is higher than
2327 * the one advertised in GOAWAY. RFC7540#6.8.
2328 */
2329 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002330 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2331 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002332 h2c->dfl -= ret;
2333 ret = h2c->dfl == 0;
2334 goto strm_err;
2335 }
2336#endif
2337
Willy Tarreau7e98c052017-10-10 15:56:59 +02002338 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002339 case H2_FT_SETTINGS:
2340 if (h2c->st0 == H2_CS_FRAME_P)
2341 ret = h2c_handle_settings(h2c);
2342
2343 if (h2c->st0 == H2_CS_FRAME_A)
2344 ret = h2c_ack_settings(h2c);
2345 break;
2346
Willy Tarreaucf68c782017-10-10 17:11:41 +02002347 case H2_FT_PING:
2348 if (h2c->st0 == H2_CS_FRAME_P)
2349 ret = h2c_handle_ping(h2c);
2350
2351 if (h2c->st0 == H2_CS_FRAME_A)
2352 ret = h2c_ack_ping(h2c);
2353 break;
2354
Willy Tarreau26f95952017-07-27 17:18:30 +02002355 case H2_FT_WINDOW_UPDATE:
2356 if (h2c->st0 == H2_CS_FRAME_P)
2357 ret = h2c_handle_window_update(h2c, h2s);
2358 break;
2359
Willy Tarreau61290ec2017-10-17 08:19:21 +02002360 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002361 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2362 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2363 * frames' parsers consume all following CONTINUATION
2364 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002365 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002366 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2367 sess_log(h2c->conn->owner);
2368 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002369
Willy Tarreau13278b42017-10-13 19:23:14 +02002370 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002371 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002372 if (h2c->flags & H2_CF_IS_BACK)
2373 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2374 else
2375 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002376 if (tmp_h2s) {
2377 h2s = tmp_h2s;
2378 ret = 1;
2379 }
2380 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002381 break;
2382
Willy Tarreau454f9052017-10-26 19:40:35 +02002383 case H2_FT_DATA:
2384 if (h2c->st0 == H2_CS_FRAME_P)
2385 ret = h2c_frt_handle_data(h2c, h2s);
2386
2387 if (h2c->st0 == H2_CS_FRAME_A)
2388 ret = h2c_send_strm_wu(h2c);
2389 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002390
Willy Tarreau92153fc2017-12-03 19:46:19 +01002391 case H2_FT_PRIORITY:
2392 if (h2c->st0 == H2_CS_FRAME_P)
2393 ret = h2c_handle_priority(h2c);
2394 break;
2395
Willy Tarreaucd234e92017-08-18 10:59:39 +02002396 case H2_FT_RST_STREAM:
2397 if (h2c->st0 == H2_CS_FRAME_P)
2398 ret = h2c_handle_rst_stream(h2c, h2s);
2399 break;
2400
Willy Tarreaue96b0922017-10-30 00:28:29 +01002401 case H2_FT_GOAWAY:
2402 if (h2c->st0 == H2_CS_FRAME_P)
2403 ret = h2c_handle_goaway(h2c);
2404 break;
2405
Willy Tarreau1c661982017-10-30 13:52:01 +01002406 case H2_FT_PUSH_PROMISE:
2407 /* not permitted here, RFC7540#5.1 */
2408 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002409 if (!h2c->nb_streams) {
2410 /* only log if no other stream can report the error */
2411 sess_log(h2c->conn->owner);
2412 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002413 break;
2414
2415 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002416 default:
2417 /* drop frames that we ignore. They may be larger than
2418 * the buffer so we drain all of their contents until
2419 * we reach the end.
2420 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002421 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2422 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002423 h2c->dfl -= ret;
2424 ret = h2c->dfl == 0;
2425 }
2426
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002427 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002428 /* We may have to send an RST if not done yet */
2429 if (h2s->st == H2_SS_ERROR)
2430 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002431
Willy Tarreaua20a5192017-12-27 11:02:06 +01002432 if (h2c->st0 == H2_CS_FRAME_E)
2433 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002434
Willy Tarreau7e98c052017-10-10 15:56:59 +02002435 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002436 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002437 break;
2438
2439 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002440 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002441 h2c->st0 = H2_CS_FRAME_H;
2442 }
2443 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002444
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002445 if (h2c->rcvd_c > 0 &&
2446 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2447 h2c_send_conn_wu(h2c);
2448
Willy Tarreau52eed752017-09-22 15:05:09 +02002449 fail:
2450 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002451 if (h2s && h2s->cs &&
2452 (b_data(&h2s->rxbuf) ||
2453 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002454 /* we may have to signal the upper layers */
2455 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002456 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002457 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002458
Willy Tarreau47b515a2018-12-21 16:09:41 +01002459 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002460}
2461
2462/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2463 * the end.
2464 */
2465static int h2_process_mux(struct h2c *h2c)
2466{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002467 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002468
Willy Tarreau01b44822018-10-03 14:26:37 +02002469 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2470 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2471 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2472 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2473 if (h2c->st0 == H2_CS_ERROR) {
2474 h2c->st0 = H2_CS_ERROR2;
2475 sess_log(h2c->conn->owner);
2476 }
2477 goto fail;
2478 }
2479 h2c->st0 = H2_CS_SETTINGS1;
2480 }
2481 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002482 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002483 return 1;
2484 }
2485
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002486 /* start by sending possibly pending window updates */
2487 if (h2c->rcvd_c > 0 &&
2488 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2489 h2c_send_conn_wu(h2c) < 0)
2490 goto fail;
2491
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002492 /* First we always process the flow control list because the streams
2493 * waiting there were already elected for immediate emission but were
2494 * blocked just on this.
2495 */
2496
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002497 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002498 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2499 h2c->st0 >= H2_CS_ERROR)
2500 break;
2501
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002502 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002503 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2504 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002505 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002506 LIST_DEL(&h2s->list);
2507 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002508 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002509 }
2510
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002511 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002512 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2513 break;
2514
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002515 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002516 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2517 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002518 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002519 LIST_DEL(&h2s->list);
2520 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002521 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002522 }
2523
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002524 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002525 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002526 if (h2c->st0 == H2_CS_ERROR) {
2527 if (h2c->max_id >= 0) {
2528 h2c_send_goaway_error(h2c, NULL);
2529 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2530 return 0;
2531 }
2532
2533 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2534 }
2535 return 1;
2536 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002537 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002538}
2539
Willy Tarreau62f52692017-10-08 23:01:42 +02002540
Willy Tarreau479998a2018-11-18 06:30:59 +01002541/* Attempt to read data, and subscribe if none available.
2542 * The function returns 1 if data has been received, otherwise zero.
2543 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002544static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002545{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002546 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002547 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002548 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002549 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002550
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002551 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002552 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002553
Willy Tarreau315d8072017-12-10 22:17:57 +01002554 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002555 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002556
Willy Tarreau44e973f2018-03-01 17:49:30 +01002557 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002558 if (!buf) {
2559 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002560 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002561 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002562
Olivier Houchard7505f942018-08-21 18:10:44 +02002563 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002564 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002565 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2566 /* HTX in use : try to pre-align the buffer like the
2567 * rxbufs will be to optimize memory copies. We'll make
2568 * sure that the frame header lands at the end of the
2569 * HTX block to alias it upon recv. We cannot use the
2570 * head because rcv_buf() will realign the buffer if
2571 * it's empty. Thus we cheat and pretend we already
2572 * have a few bytes there.
2573 */
2574 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002575 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002576 }
2577 else
2578 max = b_room(buf);
2579
Olivier Houchard7505f942018-08-21 18:10:44 +02002580 if (max)
2581 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2582 else
2583 ret = 0;
2584 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002585
Olivier Houchard53216e72018-10-10 15:46:36 +02002586 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002587 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002588
Olivier Houcharda1411e62018-08-17 18:42:48 +02002589 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002590 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002591 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002592 }
2593
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002594 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002595 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002596 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002597}
2598
Willy Tarreau479998a2018-11-18 06:30:59 +01002599/* Try to send data if possible.
2600 * The function returns 1 if data have been sent, otherwise zero.
2601 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002602static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002603{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002604 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002605 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002606 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002607
2608 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002609 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002610
Olivier Houchard7505f942018-08-21 18:10:44 +02002611
Willy Tarreaua2af5122017-10-09 11:56:46 +02002612 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2613 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002614 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002615 }
2616
Willy Tarreaubc933932017-10-09 16:21:43 +02002617 /* This loop is quite simple : it tries to fill as much as it can from
2618 * pending streams into the existing buffer until it's reportedly full
2619 * or the end of send requests is reached. Then it tries to send this
2620 * buffer's contents out, marks it not full if at least one byte could
2621 * be sent, and tries again.
2622 *
2623 * The snd_buf() function normally takes a "flags" argument which may
2624 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2625 * data immediately comes and CO_SFL_STREAMER to indicate that the
2626 * connection is streaming lots of data (used to increase TLS record
2627 * size at the expense of latency). The former can be sent any time
2628 * there's a buffer full flag, as it indicates at least one stream
2629 * attempted to send and failed so there are pending data. An
2630 * alternative would be to set it as long as there's an active stream
2631 * but that would be problematic for ACKs until we have an absolute
2632 * guarantee that all waiters have at least one byte to send. The
2633 * latter should possibly not be set for now.
2634 */
2635
2636 done = 0;
2637 while (!done) {
2638 unsigned int flags = 0;
2639
2640 /* fill as much as we can into the current buffer */
2641 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2642 done = h2_process_mux(h2c);
2643
2644 if (conn->flags & CO_FL_ERROR)
2645 break;
2646
2647 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2648 flags |= CO_SFL_MSG_MORE;
2649
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002650 if (b_data(&h2c->mbuf)) {
2651 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002652 if (!ret)
2653 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002654 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 b_del(&h2c->mbuf, ret);
2656 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002657 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002658
2659 /* wrote at least one byte, the buffer is not full anymore */
2660 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2661 }
2662
Willy Tarreaua2af5122017-10-09 11:56:46 +02002663 if (conn->flags & CO_FL_SOCK_WR_SH) {
2664 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002665 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002666 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002667 /* We're not full anymore, so we can wake any task that are waiting
2668 * for us.
2669 */
2670 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002672 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2673 struct h2s *, list);
2674 LIST_DEL(&h2s->list);
2675 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002676 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002677 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2678 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002679 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002680 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002681 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002682 /* We're done, no more to send */
2683 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002684 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002685schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002686 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2687 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002688 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002689}
2690
2691static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2692{
2693 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002694 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002695
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002696 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002697 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002698 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002699 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002700 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002701 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002702 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002703}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002704
Willy Tarreau62f52692017-10-08 23:01:42 +02002705/* callback called on any event by the connection handler.
2706 * It applies changes and returns zero, or < 0 if it wants immediate
2707 * destruction of the connection (which normally doesn not happen in h2).
2708 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002709static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002710{
Olivier Houchard7505f942018-08-21 18:10:44 +02002711 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002712
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002713 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002714 h2_process_demux(h2c);
2715
2716 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002717 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002718
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002719 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002720 h2c->flags &= ~H2_CF_DEM_DFULL;
2721 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002722 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002723
Willy Tarreau0b37d652018-10-03 10:33:02 +02002724 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002725 /* frontend is stopping, reload likely in progress, let's try
2726 * to announce a graceful shutdown if not yet done. We don't
2727 * care if it fails, it will be tried again later.
2728 */
2729 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2730 if (h2c->last_sid < 0)
2731 h2c->last_sid = (1U << 31) - 1;
2732 h2c_send_goaway_error(h2c, NULL);
2733 }
2734 }
2735
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002736 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002737 * If we received early data, and the handshake is done, wake
2738 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002739 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002740 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2741 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2742 struct eb32_node *node;
2743 struct h2s *h2s;
2744
2745 h2c->flags |= H2_CF_WAIT_FOR_HS;
2746 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2747
2748 while (node) {
2749 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002750 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002751 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002752 node = eb32_next(node);
2753 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002754 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002755
Willy Tarreau26bd7612017-10-09 16:47:04 +02002756 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002757 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2758 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2759 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002760 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002761
2762 if (eb_is_empty(&h2c->streams_by_id)) {
2763 /* no more stream, kill the connection now */
2764 h2_release(conn);
2765 return -1;
2766 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002767 }
2768
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002769 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002770 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002771
Olivier Houchard53216e72018-10-10 15:46:36 +02002772 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2773 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2774 (h2c->st0 != H2_CS_ERROR &&
2775 !b_data(&h2c->mbuf) &&
2776 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2777 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002778 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002779
Willy Tarreau3f133572017-10-31 19:21:06 +01002780 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002781 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002782 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002783 task_queue(h2c->task);
2784 }
2785 else
2786 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002787 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002788
Olivier Houchard7505f942018-08-21 18:10:44 +02002789 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002790 return 0;
2791}
2792
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002793static int h2_wake(struct connection *conn)
2794{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002795 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002796
2797 return (h2_process(h2c));
2798}
2799
Willy Tarreauea392822017-10-31 10:02:25 +01002800/* Connection timeout management. The principle is that if there's no receipt
2801 * nor sending for a certain amount of time, the connection is closed. If the
2802 * MUX buffer still has lying data or is not allocatable, the connection is
2803 * immediately killed. If it's allocatable and empty, we attempt to send a
2804 * GOAWAY frame.
2805 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002806static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002807{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002808 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002809 int expired = tick_is_expired(t->expire, now_ms);
2810
Willy Tarreau0975f112018-03-29 15:22:59 +02002811 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002812 return t;
2813
Willy Tarreau0975f112018-03-29 15:22:59 +02002814 task_delete(t);
2815 task_free(t);
2816
2817 if (!h2c) {
2818 /* resources were already deleted */
2819 return NULL;
2820 }
2821
2822 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002823 h2c_error(h2c, H2_ERR_NO_ERROR);
2824 h2_wake_some_streams(h2c, 0, 0);
2825
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002827 /* don't even try to send a GOAWAY, the buffer is stuck */
2828 h2c->flags |= H2_CF_GOAWAY_FAILED;
2829 }
2830
2831 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002832 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002833 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2834 h2c->flags |= H2_CF_GOAWAY_FAILED;
2835
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002836 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2837 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002838 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002839 b_del(&h2c->mbuf, ret);
2840 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002841 }
2842 }
Willy Tarreauea392822017-10-31 10:02:25 +01002843
Willy Tarreau0975f112018-03-29 15:22:59 +02002844 /* either we can release everything now or it will be done later once
2845 * the last stream closes.
2846 */
2847 if (eb_is_empty(&h2c->streams_by_id))
2848 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002849
Willy Tarreauea392822017-10-31 10:02:25 +01002850 return NULL;
2851}
2852
2853
Willy Tarreau62f52692017-10-08 23:01:42 +02002854/*******************************************/
2855/* functions below are used by the streams */
2856/*******************************************/
2857
2858/*
2859 * Attach a new stream to a connection
2860 * (Used for outgoing connections)
2861 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002862static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002863{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002864 struct conn_stream *cs;
2865 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002866 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002867
2868 cs = cs_new(conn);
2869 if (!cs)
2870 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002871 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002872 if (!h2s) {
2873 cs_free(cs);
2874 return NULL;
2875 }
2876 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002877}
2878
Willy Tarreaufafd3982018-11-18 21:29:20 +01002879/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2880 * We have to scan because we may have some orphan streams. It might be
2881 * beneficial to scan backwards from the end to reduce the likeliness to find
2882 * orphans.
2883 */
2884static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2885{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002886 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002887 struct h2s *h2s;
2888 struct eb32_node *node;
2889
2890 node = eb32_first(&h2c->streams_by_id);
2891 while (node) {
2892 h2s = container_of(node, struct h2s, by_id);
2893 if (h2s->cs)
2894 return h2s->cs;
2895 node = eb32_next(node);
2896 }
2897 return NULL;
2898}
2899
Willy Tarreau62f52692017-10-08 23:01:42 +02002900/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002901 * Destroy the mux and the associated connection, if it is no longer used
2902 */
2903static void h2_destroy(struct connection *conn)
2904{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002905 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002906
2907 if (eb_is_empty(&h2c->streams_by_id))
2908 h2_release(h2c->conn);
2909}
2910
2911/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002912 * Detach the stream from the connection and possibly release the connection.
2913 */
2914static void h2_detach(struct conn_stream *cs)
2915{
Willy Tarreau60935142017-10-16 18:11:19 +02002916 struct h2s *h2s = cs->ctx;
2917 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002918 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002919
2920 cs->ctx = NULL;
2921 if (!h2s)
2922 return;
2923
Olivier Houchardf502aca2018-12-14 19:42:40 +01002924 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002925 h2c = h2s->h2c;
2926 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002927 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002928 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2929 !h2_has_too_many_cs(h2c)) {
2930 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002931 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002932 }
Willy Tarreau60935142017-10-16 18:11:19 +02002933
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002934 /* this stream may be blocked waiting for some data to leave (possibly
2935 * an ES or RST frame), so orphan it in this case.
2936 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002937 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002938 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002939 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002940 return;
2941
Willy Tarreau45f752e2017-10-30 15:44:59 +01002942 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2943 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2944 /* unblock the connection if it was blocked on this
2945 * stream.
2946 */
2947 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2948 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002949 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002950 }
2951
Willy Tarreau71049cc2018-03-28 13:56:39 +02002952 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002953
Olivier Houchard8a786902018-12-15 16:05:40 +01002954 if (h2c->flags & H2_CF_IS_BACK &&
2955 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002956 if (!(h2c->conn->flags &
2957 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2958 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002959 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002960 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2961 h2c->conn->owner = NULL;
2962 if (eb_is_empty(&h2c->streams_by_id)) {
2963 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2964 /* The server doesn't want it, let's kill the connection right away */
2965 h2c->conn->mux->destroy(h2c->conn);
2966 return;
2967 }
2968 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002969 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002970 if (eb_is_empty(&h2c->streams_by_id)) {
2971 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2972 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2973 return;
2974 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002975 /* Never ever allow to reuse a connection from a non-reuse backend */
2976 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2977 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01002978 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002979 struct server *srv = objt_server(h2c->conn->target);
2980
2981 if (srv) {
2982 if (h2c->conn->flags & CO_FL_PRIVATE)
2983 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2984 else
2985 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2986 }
2987
2988 }
2989 }
2990 }
2991
Willy Tarreaue323f342018-03-28 13:51:45 +02002992 /* We don't want to close right now unless we're removing the
2993 * last stream, and either the connection is in error, or it
2994 * reached the ID already specified in a GOAWAY frame received
2995 * or sent (as seen by last_sid >= 0).
2996 */
2997 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2998 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002999 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003000 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003001 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003002 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003003 (conn_xprt_read0_pending(h2c->conn) ||
3004 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3005 /* no more stream will come, kill it now */
3006 h2_release(h2c->conn);
3007 }
3008 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003009 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003010 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3011 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003012 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003013 else
3014 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003015 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003016}
3017
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003018static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003019{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003020 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003021 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003022
Willy Tarreau721c9742017-11-07 11:05:42 +01003023 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003024 return;
3025
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003026 /* if no outgoing data was seen on this stream, it means it was
3027 * closed with a "tcp-request content" rule that is normally
3028 * used to kill the connection ASAP (eg: limit abuse). In this
3029 * case we send a goaway to close the connection.
3030 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003031 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003032 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003033 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003034
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003035 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3036 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003037 h2c_send_goaway_error(h2c, h2s) <= 0)
3038 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003039
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003040 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003041 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003042 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003043
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003044 return;
3045add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003046 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003047 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003048 if (h2s->flags & H2_SF_BLK_MFCTL) {
3049 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3050 h2s->send_wait = sw;
3051 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3052 h2s->send_wait = sw;
3053 LIST_ADDQ(&h2c->send_list, &h2s->list);
3054 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003055 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003056 /* Let the handler know we want shutr */
3057 sw->handle = (void *)((long)sw->handle | 1);
3058
Willy Tarreau62f52692017-10-08 23:01:42 +02003059}
3060
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003061static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003062{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003063 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003064 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003065
Willy Tarreau721c9742017-11-07 11:05:42 +01003066 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003067 return;
3068
Willy Tarreau67434202017-11-06 20:20:51 +01003069 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003070 /* we can cleanly close using an empty data frame only after headers */
3071
3072 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3073 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003074 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003075
3076 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003077 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003078 else
3079 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003080 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003081 /* if no outgoing data was seen on this stream, it means it was
3082 * closed with a "tcp-request content" rule that is normally
3083 * used to kill the connection ASAP (eg: limit abuse). In this
3084 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003085 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003086 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003087 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003088 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003089
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003090 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3091 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003092 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003093 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003094
Willy Tarreau00dd0782018-03-01 16:31:34 +01003095 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003096 }
3097
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003098 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003099 tasklet_wakeup(h2c->wait_event.task);
3100 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003101
3102 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003103 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003104 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003105 if (h2s->flags & H2_SF_BLK_MFCTL) {
3106 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3107 h2s->send_wait = sw;
3108 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3109 h2s->send_wait = sw;
3110 LIST_ADDQ(&h2c->send_list, &h2s->list);
3111 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003112 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003113 /* let the handler know we want to shutw */
3114 sw->handle = (void *)((long)(sw->handle) | 2);
3115
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003116}
3117
3118static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3119{
3120 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003121 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003122
Olivier Houchard2c68a462018-12-15 22:42:20 +01003123 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003124 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003125 h2s->send_wait = NULL;
3126 LIST_DEL(&h2s->list);
3127 LIST_INIT(&h2s->list);
3128 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003129 if (reason & 2)
3130 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003131 if (reason & 1)
3132 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003133
Olivier Houchard2c68a462018-12-15 22:42:20 +01003134 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003135 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003136 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003137 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003138}
3139
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003140static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3141{
3142 struct h2s *h2s = cs->ctx;
3143
3144 if (!mode)
3145 return;
3146
3147 h2_do_shutr(h2s);
3148}
3149
3150static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3151{
3152 struct h2s *h2s = cs->ctx;
3153
3154 h2_do_shutw(h2s);
3155}
3156
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003157/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003158 * HTX request or response depending on the connection's side. Returns the
Willy Tarreauea18f862018-12-22 20:19:26 +01003159 * number of bytes emitted if > 0, or 0 if it couldn't proceed. May report
3160 * connection errors in h2c->errcode if the frame is non-decodable and not
3161 * recoverable.
3162 *
3163 * The function may fold CONTINUATION frames into the initial HEADERS frame
3164 * by removing padding and next frame header, then moving the CONTINUATION
3165 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3166 * leaving a hole between the main frame and the beginning of the next one.
3167 * The possibly remaining incomplete or next frame at the end may be moved
3168 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3169 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3170 *
3171 * A buffer at the beginning of processing may look like this :
3172 *
3173 * ,---.---------.-----.--------------.--------------.------.---.
3174 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3175 * `---^---------^-----^--------------^--------------^------^---'
3176 * | | <-----> | |
3177 * area | dpl | wrap
3178 * |<--------------> |
3179 * | dfl |
3180 * |<-------------------------------------------------->|
3181 * head data
3182 *
3183 * Padding is automatically overwritten when folding, participating to the
3184 * hole size after dfl :
3185 *
3186 * ,---.------------------------.-----.--------------.------.---.
3187 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3188 * `---^------------------------^-----^--------------^------^---'
3189 * | | <-----> | |
3190 * area | hole | wrap
3191 * |<-----------------------> |
3192 * | dfl |
3193 * |<-------------------------------------------------->|
3194 * head data
3195 *
3196 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3197 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3198 * bit.
Willy Tarreau13278b42017-10-13 19:23:14 +02003199 */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003200static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02003201{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003202 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003203 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003204 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003205 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003206 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003207 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003208 int flen; // header frame len
3209 int hole = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003210 int outlen = 0;
3211 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003212 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003213
Willy Tarreauea18f862018-12-22 20:19:26 +01003214next_frame:
3215 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3216 goto leave; // incomplete input frame
3217
3218 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3219 * this case, we'll try to paste it immediately after the initial
3220 * HEADERS frame payload and kill any possible padding. The initial
3221 * frame's length will be increased to represent the concatenation
3222 * of the two frames. The next frame is read from position <tlen>
3223 * and written at position <flen> (minus padding if some is present).
3224 */
3225 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3226 struct h2_fh hdr;
3227 int clen; // CONTINUATION frame's payload length
3228
3229 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3230 /* no more data, the buffer may be full, either due to
3231 * too large a frame or because of too large a hole that
3232 * we're going to compact at the end.
3233 */
3234 goto leave;
3235 }
3236
3237 if (hdr.ft != H2_FT_CONTINUATION) {
3238 /* RFC7540#6.10: frame of unexpected type */
3239 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3240 goto fail;
3241 }
3242
3243 if (hdr.sid != h2c->dsi) {
3244 /* RFC7540#6.10: frame of different stream */
3245 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3246 goto fail;
3247 }
3248
3249 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3250 /* RFC7540#4.2: invalid frame length */
3251 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3252 goto fail;
3253 }
3254
3255 /* detect when we must stop aggragating frames */
3256 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3257
3258 /* Take as much as we can of the CONTINUATION frame's payload */
3259 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3260 if (clen > hdr.len)
3261 clen = hdr.len;
3262
3263 /* Move the frame's payload over the padding, hole and frame
3264 * header. At least one of hole or dpl is null (see diagrams
3265 * above). The hole moves after the new aggragated frame.
3266 */
3267 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3268 h2c->dfl += clen - h2c->dpl;
3269 hole += h2c->dpl + 9;
3270 h2c->dpl = 0;
3271 goto next_frame;
3272 }
3273
3274 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003275
Willy Tarreau13278b42017-10-13 19:23:14 +02003276 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003277 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003278 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003279 copy = alloc_trash_chunk();
3280 if (!copy) {
3281 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3282 goto fail;
3283 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3285 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3286 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003287 }
3288
Willy Tarreau13278b42017-10-13 19:23:14 +02003289 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3290 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003291 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003292 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3293 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003294 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003295 }
3296
Willy Tarreau13278b42017-10-13 19:23:14 +02003297 hdrs += 5; // stream dep = 4, weight = 1
3298 flen -= 5;
3299 }
3300
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003301 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003302 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003303 goto fail;
3304 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003305
Willy Tarreau937f7602018-02-26 15:22:17 +01003306 /* we can't retry a failed decompression operation so we must be very
3307 * careful not to take any risks. In practice the output buffer is
3308 * always empty except maybe for trailers, in which case we simply have
3309 * to wait for the upper layer to finish consuming what is available.
3310 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003311
3312 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003313 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003314 if (!htx_is_empty(htx)) {
3315 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003316 goto fail;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003317 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003318 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003319 if (b_data(rxbuf)) {
3320 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003321 goto fail;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003322 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003323
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003324 rxbuf->head = 0;
3325 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003326 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003327
3328 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3329 sizeof(list)/sizeof(list[0]), tmp);
3330 if (outlen < 0) {
3331 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3332 goto fail;
3333 }
3334
3335 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003336 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003337
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003338 if (htx) {
3339 /* HTX mode */
3340 if (h2c->flags & H2_CF_IS_BACK)
3341 outlen = h2_make_htx_response(list, htx, &msgf);
3342 else
3343 outlen = h2_make_htx_request(list, htx, &msgf);
3344 } else {
3345 /* HTTP/1 mode */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003346 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf);
Willy Tarreau83195932019-01-03 10:26:23 +01003347 if (outlen > 0)
3348 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003349 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003350
3351 if (outlen < 0) {
3352 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3353 goto fail;
3354 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003355
Willy Tarreau174b06a2018-04-25 18:13:58 +02003356 if (msgf & H2_MSGF_BODY) {
3357 /* a payload is present */
3358 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003359 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003360 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003361 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003362 }
3363
Willy Tarreauea18f862018-12-22 20:19:26 +01003364 /* now consume the input data (length of possibly aggregated frames) */
3365 b_del(&h2c->dbuf, h2c->dfl + hole);
3366 hole = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003367 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau13278b42017-10-13 19:23:14 +02003368
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003369 if (htx && h2c->dff & H2_F_HEADERS_END_STREAM)
3370 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau937f7602018-02-26 15:22:17 +01003371
Willy Tarreau68dd9852017-07-03 14:44:26 +02003372 leave:
Willy Tarreauea18f862018-12-22 20:19:26 +01003373 /* If there is a hole left and it's not a t the end, we are forced to
3374 * move the remaining data over it.
3375 */
3376 if (hole) {
3377 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3378 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3379 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3380 b_sub(&h2c->dbuf, hole);
3381 }
3382
3383 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3384 /* too large frames */
3385 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3386 goto fail;
3387 }
3388
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003389 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003390 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003391 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003392 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003393 fail:
3394 outlen = 0;
3395 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003396}
3397
Willy Tarreau454f9052017-10-26 19:40:35 +02003398/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3399 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3400 * in use, a new chunk is emitted for each frame. This is supposed to fit
3401 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3402 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3403 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003404 * parser state is automatically updated. Returns > 0 if it could completely
3405 * send the current frame, 0 if it couldn't complete, in which case
3406 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3407 * DATA frame can return 0 as a valid result). Stream errors are reported in
3408 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3409 * have checked the frame header and ensured that the frame was complete or the
3410 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003411 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003412static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003413{
3414 struct h2c *h2c = h2s->h2c;
3415 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003416 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003417 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003418 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003419 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003420
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003421 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003422
Olivier Houchard638b7992018-08-16 15:41:52 +02003423 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003424 if (!csbuf) {
3425 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003426 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003427 }
3428
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003429try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003430 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003431 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3432 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003433 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003434 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003435
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003436 if (flen > b_data(&h2c->dbuf)) {
3437 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003438 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003439 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003440 }
3441
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003442 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003443 block1 = htx_free_data_space(htx);
3444 if (!block1) {
3445 h2c->flags |= H2_CF_DEM_SFULL;
3446 goto fail;
3447 }
3448 if (flen > block1)
3449 flen = block1;
3450
3451 /* here, flen is the max we can copy into the output buffer */
3452 block1 = b_contig_data(&h2c->dbuf, 0);
3453 if (flen > block1)
3454 flen = block1;
3455
3456 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3457 h2c->flags |= H2_CF_DEM_SFULL;
3458 goto fail;
3459 }
3460
3461 b_del(&h2c->dbuf, flen);
3462 h2c->dfl -= flen;
3463 h2c->rcvd_c += flen;
3464 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3465 goto try_again;
3466 }
3467 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003468 /* it doesn't fit and the buffer is fragmented,
3469 * so let's defragment it and try again.
3470 */
3471 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003472 }
3473
Willy Tarreaueba10f22018-04-25 20:44:22 +02003474 /* chunked-encoding requires more room */
3475 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003476 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003477 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3478 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3479 (chklen < 1048576) ? 4 : 8;
3480 chklen += 4; // CRLF, CRLF
3481 }
3482
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003483 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003484 if (flen + chklen > b_room(csbuf)) {
3485 if (chklen >= b_room(csbuf)) {
3486 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003487 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003488 }
3489 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003490 }
3491
3492 if (h2s->flags & H2_SF_DATA_CHNK) {
3493 /* emit the chunk size */
3494 unsigned int chksz = flen;
3495 char str[10];
3496 char *beg;
3497
3498 beg = str + sizeof(str);
3499 *--beg = '\n';
3500 *--beg = '\r';
3501 do {
3502 *--beg = hextab[chksz & 0xF];
3503 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003504 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003505 }
3506
Willy Tarreau454f9052017-10-26 19:40:35 +02003507 /* Block1 is the length of the first block before the buffer wraps,
3508 * block2 is the optional second block to reach the end of the frame.
3509 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003510 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003511 if (block1 > flen)
3512 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003513 block2 = flen - block1;
3514
3515 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003516 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003517
3518 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003519 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003520
Willy Tarreaueba10f22018-04-25 20:44:22 +02003521 if (h2s->flags & H2_SF_DATA_CHNK) {
3522 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003523 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003524 }
3525
Willy Tarreau454f9052017-10-26 19:40:35 +02003526 /* now mark the input data as consumed (will be deleted from the buffer
3527 * by the caller when seeing FRAME_A after sending the window update).
3528 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003529 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003530 h2c->dfl -= flen;
3531 h2c->rcvd_c += flen;
3532 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3533
3534 if (h2c->dfl > h2c->dpl) {
3535 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003536 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003537 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003538 }
3539
Willy Tarreau4a28da12018-01-04 14:41:00 +01003540 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003541 /* here we're done with the frame, all the payload (except padding) was
3542 * transferred.
3543 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003544
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003545 if (h2c->dff & H2_F_DATA_END_STREAM) {
3546 if (htx) {
3547 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3548 h2c->flags |= H2_CF_DEM_SFULL;
3549 goto fail;
3550 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003551 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003552 else if (h2s->flags & H2_SF_DATA_CHNK) {
3553 /* emit the trailing 0 CRLF CRLF */
3554 if (b_room(csbuf) < 5) {
3555 h2c->flags |= H2_CF_DEM_SFULL;
3556 goto fail;
3557 }
3558 chklen += 5;
3559 b_putblk(csbuf, "0\r\n\r\n", 5);
3560 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003561 }
3562
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003563 h2c->rcvd_c += h2c->dpl;
3564 h2c->rcvd_s += h2c->dpl;
3565 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003566 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3567
Willy Tarreau39d68502018-03-02 12:26:37 +01003568 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003569 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003570 h2s->cs->flags |= CS_FL_REOS;
3571 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003572 if (htx)
3573 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003574 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003575 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003576 if (htx)
3577 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003578 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003579}
3580
Willy Tarreau5dd17352018-06-14 13:33:30 +02003581/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3582 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3583 * number of bytes sent. The caller must check the stream's status to detect
3584 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003585 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003586static 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 +02003587{
3588 struct http_hdr list[MAX_HTTP_HDR];
3589 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003590 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003591 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003592 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003593 int es_now = 0;
3594 int ret = 0;
3595 int hdr;
3596
3597 if (h2c_mux_busy(h2c, h2s)) {
3598 h2s->flags |= H2_SF_BLK_MBUSY;
3599 return 0;
3600 }
3601
Willy Tarreau44e973f2018-03-01 17:49:30 +01003602 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003603 h2c->flags |= H2_CF_MUX_MALLOC;
3604 h2s->flags |= H2_SF_BLK_MROOM;
3605 return 0;
3606 }
3607
3608 /* First, try to parse the H1 response and index it into <list>.
3609 * NOTE! Since it comes from haproxy, we *know* that a response header
3610 * block does not wrap and we can safely read it this way without
3611 * having to realign the buffer.
3612 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003613 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003614 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003615 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003616 /* incomplete or invalid response, this is abnormal coming from
3617 * haproxy and may only result in a bad errorfile or bad Lua code
3618 * so that won't be fixed, raise an error now.
3619 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003620 * FIXME: we should instead add the ability to only return a
3621 * 502 bad gateway. But in theory this is not supposed to
3622 * happen.
3623 */
3624 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3625 ret = 0;
3626 goto end;
3627 }
3628
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003629 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003630
3631 /* certain statuses have no body or an empty one, regardless of
3632 * what the headers say.
3633 */
3634 if (sl.st.status >= 100 && sl.st.status < 200) {
3635 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3636 h1m->curr_len = h1m->body_len = 0;
3637 }
3638 else if (sl.st.status == 204 || sl.st.status == 304) {
3639 /* no contents, claim c-len is present and set to zero */
3640 h1m->flags &= ~H1_MF_CHNK;
3641 h1m->flags |= H1_MF_CLEN;
3642 h1m->curr_len = h1m->body_len = 0;
3643 }
3644
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003645 chunk_reset(&outbuf);
3646
3647 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003648 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003649 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003650 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003651
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003652 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003653 break;
3654 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003655 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003656 }
3657
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003658 if (outbuf.size < 9)
3659 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003660
3661 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003662 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3663 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3664 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003665
3666 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003667 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003668 /* this is an unparsable response */
3669 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3670 ret = 0;
3671 goto end;
3672 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003673
3674 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003675 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003676 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003677 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003678 }
3679
3680 /* encode all headers, stop at empty name */
3681 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003682 /* these ones do not exist in H2 and must be dropped. */
3683 if (isteq(list[hdr].n, ist("connection")) ||
3684 isteq(list[hdr].n, ist("proxy-connection")) ||
3685 isteq(list[hdr].n, ist("keep-alive")) ||
3686 isteq(list[hdr].n, ist("upgrade")) ||
3687 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003688 continue;
3689
3690 if (isteq(list[hdr].n, ist("")))
3691 break; // end
3692
3693 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3694 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003695 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003696 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003697 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003698 }
3699 }
3700
3701 /* we may need to add END_STREAM */
3702 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3703 es_now = 1;
3704
3705 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003706 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003707
3708 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003709 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003710
3711 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003712 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003713
3714 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003715 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003716 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003717
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003718 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003719 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003720 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003721
Willy Tarreau801250e2018-09-11 11:45:04 +02003722 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003723 h2s->flags |= H2_SF_ES_SENT;
3724 if (h2s->st == H2_SS_OPEN)
3725 h2s->st = H2_SS_HLOC;
3726 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003727 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003728 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003729 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003730 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003731 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003732 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003733 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003734 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003735 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003736
3737 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003738
3739 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003740 //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 +02003741 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003742 full:
3743 h1m_init_res(h1m);
3744 h1m->err_pos = -1; // don't care about errors on the response path
3745 h2c->flags |= H2_CF_MUX_MFULL;
3746 h2s->flags |= H2_SF_BLK_MROOM;
3747 ret = 0;
3748 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003749}
3750
Willy Tarreau5dd17352018-06-14 13:33:30 +02003751/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3752 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3753 * the number of bytes sent. The caller must check the stream's status to
3754 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003755 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003756static 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 +02003757{
3758 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003759 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003760 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003761 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003762 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003763 int es_now = 0;
3764 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003765 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003766 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003767
3768 if (h2c_mux_busy(h2c, h2s)) {
3769 h2s->flags |= H2_SF_BLK_MBUSY;
3770 goto end;
3771 }
3772
Willy Tarreau44e973f2018-03-01 17:49:30 +01003773 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003774 h2c->flags |= H2_CF_MUX_MALLOC;
3775 h2s->flags |= H2_SF_BLK_MROOM;
3776 goto end;
3777 }
3778
3779 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003780 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003781 goto end;
3782
3783 chunk_reset(&outbuf);
3784
3785 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003786 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003787 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003788 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003789
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003790 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003791 break;
3792 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003793 /* If there are pending data in the output buffer, and we have
3794 * less than 1/4 of the mbuf's size and everything fits, we'll
3795 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3796 * is full and wait, to save some slow realign calls.
3797 */
3798 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3799 h2c->flags |= H2_CF_MUX_MFULL;
3800 h2s->flags |= H2_SF_BLK_MROOM;
3801 goto end;
3802 }
3803
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003804 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003805 }
3806
3807 if (outbuf.size < 9) {
3808 h2c->flags |= H2_CF_MUX_MFULL;
3809 h2s->flags |= H2_SF_BLK_MROOM;
3810 goto end;
3811 }
3812
3813 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003814 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3815 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3816 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003817
3818 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3819 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003820 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003821 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003822 break;
3823 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003824 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003825 if ((long long)size > h1m->curr_len)
3826 size = h1m->curr_len;
3827 break;
3828 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003829 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003830 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003831 if (!ret)
3832 goto end;
3833
3834 if (ret < 0) {
3835 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003836 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003837 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3838 goto end;
3839 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003840 max -= ret;
3841 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003842 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003843 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003844 }
3845
Willy Tarreau801250e2018-09-11 11:45:04 +02003846 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003847 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003848 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003849 if (!ret)
3850 goto end;
3851
3852 if (ret < 0) {
3853 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003854 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003855 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3856 goto end;
3857 }
3858
3859 size = chunk;
3860 h1m->curr_len = chunk;
3861 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003862 max -= ret;
3863 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003864 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003865 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003866 if (!size)
3867 goto send_empty;
3868 }
3869
3870 /* in MSG_DATA state, continue below */
3871 size = h1m->curr_len;
3872 break;
3873 }
3874
3875 /* we have in <size> the exact number of bytes we need to copy from
3876 * the H1 buffer. We need to check this against the connection's and
3877 * the stream's send windows, and to ensure that this fits in the max
3878 * frame size and in the buffer's available space minus 9 bytes (for
3879 * the frame header). The connection's flow control is applied last so
3880 * that we can use a separate list of streams which are immediately
3881 * unblocked on window opening. Note: we don't implement padding.
3882 */
3883
Willy Tarreau5dd17352018-06-14 13:33:30 +02003884 if (size > max)
3885 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003886
3887 if (size > h2s->mws)
3888 size = h2s->mws;
3889
3890 if (size <= 0) {
3891 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003892 if (h2s->send_wait) {
3893 LIST_DEL(&h2s->list);
3894 LIST_INIT(&h2s->list);
3895 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003896 goto end;
3897 }
3898
3899 if (h2c->mfs && size > h2c->mfs)
3900 size = h2c->mfs;
3901
3902 if (size + 9 > outbuf.size) {
3903 /* we have an opportunity for enlarging the too small
3904 * available space, let's try.
3905 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003906 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003907 goto realign_again;
3908 size = outbuf.size - 9;
3909 }
3910
3911 if (size <= 0) {
3912 h2c->flags |= H2_CF_MUX_MFULL;
3913 h2s->flags |= H2_SF_BLK_MROOM;
3914 goto end;
3915 }
3916
3917 if (size > h2c->mws)
3918 size = h2c->mws;
3919
3920 if (size <= 0) {
3921 h2s->flags |= H2_SF_BLK_MFCTL;
3922 goto end;
3923 }
3924
3925 /* copy whatever we can */
3926 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003927 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003928 if (ret == 1)
3929 len2 = 0;
3930
3931 if (!ret || len1 + len2 < size) {
3932 /* FIXME: must normally never happen */
3933 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3934 goto end;
3935 }
3936
3937 /* limit len1/len2 to size */
3938 if (len1 + len2 > size) {
3939 int sub = len1 + len2 - size;
3940
3941 if (len2 > sub)
3942 len2 -= sub;
3943 else {
3944 sub -= len2;
3945 len2 = 0;
3946 len1 -= sub;
3947 }
3948 }
3949
3950 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003951 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003952 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003953 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003954
3955 send_empty:
3956 /* we may need to add END_STREAM */
3957 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3958 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003959 *
3960 * FIXME: what we do here is not correct because we send end_stream
3961 * before knowing if we'll have to send a HEADERS frame for the
3962 * trailers. More importantly we're not consuming the trailing CRLF
3963 * after the end of trailers, so it will be left to the caller to
3964 * eat it. The right way to do it would be to measure trailers here
3965 * and to send ES only if there are no trailers.
3966 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003967 */
3968 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003969 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003970 es_now = 1;
3971
3972 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003973 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003974
3975 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003976 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003977
3978 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003979 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980
3981 /* consume incoming H1 response */
3982 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003983 max -= size;
3984 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003985 total += size;
3986 h1m->curr_len -= size;
3987 h2s->mws -= size;
3988 h2c->mws -= size;
3989
3990 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003991 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003992 goto new_frame;
3993 }
3994 }
3995
3996 if (es_now) {
3997 if (h2s->st == H2_SS_OPEN)
3998 h2s->st = H2_SS_HLOC;
3999 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004000 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004001
Willy Tarreau35a62702018-02-27 15:37:25 +01004002 if (!(h1m->flags & H1_MF_CHNK)) {
4003 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004004 total += max;
4005 ofs += max;
4006 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004007
Willy Tarreau801250e2018-09-11 11:45:04 +02004008 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004009 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004010
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004011 h2s->flags |= H2_SF_ES_SENT;
4012 }
4013
4014 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004015 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 +02004016 return total;
4017}
4018
Willy Tarreau115e83b2018-12-01 19:17:53 +01004019/* Try to send a HEADERS frame matching HTX response present in HTX message
4020 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4021 * must check the stream's status to detect any error which might have happened
4022 * subsequently to a successful send. The htx blocks are automatically removed
4023 * from the message. The htx message is assumed to be valid since produced from
4024 * the internal code, hence it contains a start line, an optional series of
4025 * header blocks and an end of header, otherwise an invalid frame could be
4026 * emitted and the resulting htx message could be left in an inconsistent state.
4027 */
4028static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4029{
4030 struct http_hdr list[MAX_HTTP_HDR];
4031 struct h2c *h2c = h2s->h2c;
4032 struct htx_blk *blk;
4033 struct htx_blk *blk_end;
4034 struct buffer outbuf;
4035 struct htx_sl *sl;
4036 enum htx_blk_type type;
4037 int es_now = 0;
4038 int ret = 0;
4039 int hdr;
4040 int idx;
4041
4042 if (h2c_mux_busy(h2c, h2s)) {
4043 h2s->flags |= H2_SF_BLK_MBUSY;
4044 return 0;
4045 }
4046
4047 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4048 h2c->flags |= H2_CF_MUX_MALLOC;
4049 h2s->flags |= H2_SF_BLK_MROOM;
4050 return 0;
4051 }
4052
4053 /* determine the first block which must not be deleted, blk_end may
4054 * be NULL if all blocks have to be deleted.
4055 */
4056 idx = htx_get_head(htx);
4057 blk_end = NULL;
4058 while (idx != -1) {
4059 type = htx_get_blk_type(htx_get_blk(htx, idx));
4060 idx = htx_get_next(htx, idx);
4061 if (type == HTX_BLK_EOH) {
4062 if (idx != -1)
4063 blk_end = htx_get_blk(htx, idx);
4064 break;
4065 }
4066 }
4067
4068 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004069 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004070 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004071 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004072 if (h2s->status < 100 || h2s->status > 999)
4073 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004074
4075 /* and the rest of the headers, that we dump starting at header 0 */
4076 hdr = 0;
4077
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004078 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004079 while ((idx = htx_get_next(htx, idx)) != -1) {
4080 blk = htx_get_blk(htx, idx);
4081 type = htx_get_blk_type(blk);
4082
4083 if (type == HTX_BLK_UNUSED)
4084 continue;
4085
4086 if (type != HTX_BLK_HDR)
4087 break;
4088
4089 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4090 goto fail;
4091
4092 list[hdr].n = htx_get_blk_name(htx, blk);
4093 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004094 hdr++;
4095 }
4096
4097 /* marker for end of headers */
4098 list[hdr].n = ist("");
4099
4100 if (h2s->status == 204 || h2s->status == 304) {
4101 /* no contents, claim c-len is present and set to zero */
4102 es_now = 1;
4103 }
4104
4105 chunk_reset(&outbuf);
4106
4107 while (1) {
4108 outbuf.area = b_tail(&h2c->mbuf);
4109 outbuf.size = b_contig_space(&h2c->mbuf);
4110 outbuf.data = 0;
4111
4112 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4113 break;
4114 realign_again:
4115 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4116 }
4117
4118 if (outbuf.size < 9)
4119 goto full;
4120
4121 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4122 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4123 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4124 outbuf.data = 9;
4125
4126 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004127 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004128 if (b_space_wraps(&h2c->mbuf))
4129 goto realign_again;
4130 goto full;
4131 }
4132
4133 /* encode all headers, stop at empty name */
4134 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4135 /* these ones do not exist in H2 and must be dropped. */
4136 if (isteq(list[hdr].n, ist("connection")) ||
4137 isteq(list[hdr].n, ist("proxy-connection")) ||
4138 isteq(list[hdr].n, ist("keep-alive")) ||
4139 isteq(list[hdr].n, ist("upgrade")) ||
4140 isteq(list[hdr].n, ist("transfer-encoding")))
4141 continue;
4142
4143 if (isteq(list[hdr].n, ist("")))
4144 break; // end
4145
4146 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4147 /* output full */
4148 if (b_space_wraps(&h2c->mbuf))
4149 goto realign_again;
4150 goto full;
4151 }
4152 }
4153
4154 /* we may need to add END_STREAM.
4155 * FIXME: we should also set it when we know for sure that the
4156 * content-length is zero as well as on 204/304
4157 */
4158 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4159 es_now = 1;
4160
4161 if (h2s->cs->flags & CS_FL_SHW)
4162 es_now = 1;
4163
4164 /* update the frame's size */
4165 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4166
4167 if (es_now)
4168 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4169
4170 /* commit the H2 response */
4171 b_add(&h2c->mbuf, outbuf.data);
4172 h2s->flags |= H2_SF_HEADERS_SENT;
4173
Willy Tarreau115e83b2018-12-01 19:17:53 +01004174 if (es_now) {
4175 h2s->flags |= H2_SF_ES_SENT;
4176 if (h2s->st == H2_SS_OPEN)
4177 h2s->st = H2_SS_HLOC;
4178 else
4179 h2s_close(h2s);
4180 }
4181
4182 /* OK we could properly deliver the response */
4183
4184 /* remove all header blocks including the EOH and compute the
4185 * corresponding size.
4186 *
4187 * FIXME: We should remove everything when es_now is set.
4188 */
4189 ret = 0;
4190 idx = htx_get_head(htx);
4191 blk = htx_get_blk(htx, idx);
4192 while (blk != blk_end) {
4193 ret += htx_get_blksz(blk);
4194 blk = htx_remove_blk(htx, blk);
4195 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004196
4197 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4198 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004199 end:
4200 return ret;
4201 full:
4202 h2c->flags |= H2_CF_MUX_MFULL;
4203 h2s->flags |= H2_SF_BLK_MROOM;
4204 ret = 0;
4205 goto end;
4206 fail:
4207 /* unparsable HTX messages, too large ones to be produced in the local
4208 * list etc go here (unrecoverable errors).
4209 */
4210 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4211 ret = 0;
4212 goto end;
4213}
4214
Willy Tarreau80739692018-10-05 11:35:57 +02004215/* Try to send a HEADERS frame matching HTX request present in HTX message
4216 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4217 * must check the stream's status to detect any error which might have happened
4218 * subsequently to a successful send. The htx blocks are automatically removed
4219 * from the message. The htx message is assumed to be valid since produced from
4220 * the internal code, hence it contains a start line, an optional series of
4221 * header blocks and an end of header, otherwise an invalid frame could be
4222 * emitted and the resulting htx message could be left in an inconsistent state.
4223 */
4224static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4225{
4226 struct http_hdr list[MAX_HTTP_HDR];
4227 struct h2c *h2c = h2s->h2c;
4228 struct htx_blk *blk;
4229 struct htx_blk *blk_end;
4230 struct buffer outbuf;
4231 struct htx_sl *sl;
4232 struct ist meth, path;
4233 enum htx_blk_type type;
4234 int es_now = 0;
4235 int ret = 0;
4236 int hdr;
4237 int idx;
4238
4239 if (h2c_mux_busy(h2c, h2s)) {
4240 h2s->flags |= H2_SF_BLK_MBUSY;
4241 return 0;
4242 }
4243
4244 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4245 h2c->flags |= H2_CF_MUX_MALLOC;
4246 h2s->flags |= H2_SF_BLK_MROOM;
4247 return 0;
4248 }
4249
4250 /* determine the first block which must not be deleted, blk_end may
4251 * be NULL if all blocks have to be deleted.
4252 */
4253 idx = htx_get_head(htx);
4254 blk_end = NULL;
4255 while (idx != -1) {
4256 type = htx_get_blk_type(htx_get_blk(htx, idx));
4257 idx = htx_get_next(htx, idx);
4258 if (type == HTX_BLK_EOH) {
4259 if (idx != -1)
4260 blk_end = htx_get_blk(htx, idx);
4261 break;
4262 }
4263 }
4264
4265 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004266 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004267 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004268 meth = htx_sl_req_meth(sl);
4269 path = htx_sl_req_uri(sl);
4270
4271 /* and the rest of the headers, that we dump starting at header 0 */
4272 hdr = 0;
4273
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004274 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004275 while ((idx = htx_get_next(htx, idx)) != -1) {
4276 blk = htx_get_blk(htx, idx);
4277 type = htx_get_blk_type(blk);
4278
4279 if (type == HTX_BLK_UNUSED)
4280 continue;
4281
4282 if (type != HTX_BLK_HDR)
4283 break;
4284
4285 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4286 goto fail;
4287
4288 list[hdr].n = htx_get_blk_name(htx, blk);
4289 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004290 hdr++;
4291 }
4292
4293 /* marker for end of headers */
4294 list[hdr].n = ist("");
4295
4296 chunk_reset(&outbuf);
4297
4298 while (1) {
4299 outbuf.area = b_tail(&h2c->mbuf);
4300 outbuf.size = b_contig_space(&h2c->mbuf);
4301 outbuf.data = 0;
4302
4303 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4304 break;
4305 realign_again:
4306 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4307 }
4308
4309 if (outbuf.size < 9)
4310 goto full;
4311
4312 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4313 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4314 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4315 outbuf.data = 9;
4316
4317 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004318 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004319 if (b_space_wraps(&h2c->mbuf))
4320 goto realign_again;
4321 goto full;
4322 }
4323
4324 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004325 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4326 /* output full */
4327 if (b_space_wraps(&h2c->mbuf))
4328 goto realign_again;
4329 goto full;
4330 }
Willy Tarreau80739692018-10-05 11:35:57 +02004331
4332 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004333 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004334 /* output full */
4335 if (b_space_wraps(&h2c->mbuf))
4336 goto realign_again;
4337 goto full;
4338 }
4339
4340 /* encode all headers, stop at empty name */
4341 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4342 /* these ones do not exist in H2 and must be dropped. */
4343 if (isteq(list[hdr].n, ist("connection")) ||
4344 isteq(list[hdr].n, ist("proxy-connection")) ||
4345 isteq(list[hdr].n, ist("keep-alive")) ||
4346 isteq(list[hdr].n, ist("upgrade")) ||
4347 isteq(list[hdr].n, ist("transfer-encoding")))
4348 continue;
4349
4350 if (isteq(list[hdr].n, ist("")))
4351 break; // end
4352
4353 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4354 /* output full */
4355 if (b_space_wraps(&h2c->mbuf))
4356 goto realign_again;
4357 goto full;
4358 }
4359 }
4360
4361 /* we may need to add END_STREAM if we have no body :
4362 * - request already closed, or :
4363 * - no transfer-encoding, and :
4364 * - no content-length or content-length:0
4365 * Fixme: this doesn't take into account CONNECT requests.
4366 */
4367 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4368 es_now = 1;
4369
4370 if (sl->flags & HTX_SL_F_BODYLESS)
4371 es_now = 1;
4372
4373 if (h2s->cs->flags & CS_FL_SHW)
4374 es_now = 1;
4375
4376 /* update the frame's size */
4377 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4378
4379 if (es_now)
4380 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4381
4382 /* commit the H2 response */
4383 b_add(&h2c->mbuf, outbuf.data);
4384 h2s->flags |= H2_SF_HEADERS_SENT;
4385 h2s->st = H2_SS_OPEN;
4386
Willy Tarreau80739692018-10-05 11:35:57 +02004387 if (es_now) {
4388 // trim any possibly pending data (eg: inconsistent content-length)
4389 h2s->flags |= H2_SF_ES_SENT;
4390 h2s->st = H2_SS_HLOC;
4391 }
4392
4393 /* remove all header blocks including the EOH and compute the
4394 * corresponding size.
4395 *
4396 * FIXME: We should remove everything when es_now is set.
4397 */
4398 ret = 0;
4399 idx = htx_get_head(htx);
4400 blk = htx_get_blk(htx, idx);
4401 while (blk != blk_end) {
4402 ret += htx_get_blksz(blk);
4403 blk = htx_remove_blk(htx, blk);
4404 }
4405
4406 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4407 htx_remove_blk(htx, blk_end);
4408
4409 end:
4410 return ret;
4411 full:
4412 h2c->flags |= H2_CF_MUX_MFULL;
4413 h2s->flags |= H2_SF_BLK_MROOM;
4414 ret = 0;
4415 goto end;
4416 fail:
4417 /* unparsable HTX messages, too large ones to be produced in the local
4418 * list etc go here (unrecoverable errors).
4419 */
4420 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4421 ret = 0;
4422 goto end;
4423}
4424
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004425/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004426 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4427 * caller must check the stream's status to detect any error which might have
4428 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004429 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4430 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004431static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004432{
4433 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004434 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004435 struct buffer outbuf;
4436 size_t total = 0;
4437 int es_now = 0;
4438 int bsize; /* htx block size */
4439 int fsize; /* h2 frame size */
4440 struct htx_blk *blk;
4441 enum htx_blk_type type;
4442 int idx;
4443
4444 if (h2c_mux_busy(h2c, h2s)) {
4445 h2s->flags |= H2_SF_BLK_MBUSY;
4446 goto end;
4447 }
4448
4449 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4450 h2c->flags |= H2_CF_MUX_MALLOC;
4451 h2s->flags |= H2_SF_BLK_MROOM;
4452 goto end;
4453 }
4454
Willy Tarreau98de12a2018-12-12 07:03:00 +01004455 htx = htx_from_buf(buf);
4456
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004457 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4458 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4459 * the caller to handle.
4460 */
4461
4462 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004463 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004464 goto end;
4465
4466 idx = htx_get_head(htx);
4467 blk = htx_get_blk(htx, idx);
4468 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4469 bsize = htx_get_blksz(blk);
4470 fsize = bsize;
4471
4472 if (type == HTX_BLK_EOD) {
4473 /* if we have an EOD, we're dealing with chunked data. We may
4474 * have a set of trailers after us that the caller will want to
4475 * deal with. Let's simply remove the EOD and return.
4476 */
4477 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004478 total++; // EOD counts as one byte
4479 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004480 goto end;
4481 }
4482
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004483 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4484 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004485
4486 /* Perform some optimizations to reduce the number of buffer copies.
4487 * First, if the mux's buffer is empty and the htx area contains
4488 * exactly one data block of the same size as the requested count, and
4489 * this count fits within the frame size, the stream's window size, and
4490 * the connection's window size, then it's possible to simply swap the
4491 * caller's buffer with the mux's output buffer and adjust offsets and
4492 * length to match the entire DATA HTX block in the middle. In this
4493 * case we perform a true zero-copy operation from end-to-end. This is
4494 * the situation that happens all the time with large files. Second, if
4495 * this is not possible, but the mux's output buffer is empty, we still
4496 * have an opportunity to avoid the copy to the intermediary buffer, by
4497 * making the intermediary buffer's area point to the output buffer's
4498 * area. In this case we want to skip the HTX header to make sure that
4499 * copies remain aligned and that this operation remains possible all
4500 * the time. This goes for headers, data blocks and any data extracted
4501 * from the HTX blocks.
4502 */
4503 if (unlikely(fsize == count &&
4504 htx->used == 1 && type == HTX_BLK_DATA &&
4505 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4506 void *old_area = h2c->mbuf.area;
4507
4508 if (b_data(&h2c->mbuf)) {
4509 /* too bad there are data left there. If we have less
4510 * than 1/4 of the mbuf's size and everything fits,
4511 * we'll perform a copy anyway. Otherwise we'll pretend
4512 * the mbuf is full and wait.
4513 */
4514 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4515 goto copy;
4516 h2c->flags |= H2_CF_MUX_MFULL;
4517 h2s->flags |= H2_SF_BLK_MROOM;
4518 goto end;
4519 }
4520
4521 /* map an H2 frame to the HTX block so that we can put the
4522 * frame header there.
4523 */
4524 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004525 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004526 h2c->mbuf.data = fsize + 9;
4527 outbuf.area = b_head(&h2c->mbuf);
4528
4529 /* prepend an H2 DATA frame header just before the DATA block */
4530 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4531 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4532 h2_set_frame_size(outbuf.area, fsize);
4533
4534 /* update windows */
4535 h2s->mws -= fsize;
4536 h2c->mws -= fsize;
4537
4538 /* and exchange with our old area */
4539 buf->area = old_area;
4540 buf->data = buf->head = 0;
4541 total += fsize;
4542 goto end;
4543 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004544
Willy Tarreau98de12a2018-12-12 07:03:00 +01004545 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004546 /* for DATA and EOM we'll have to emit a frame, even if empty */
4547
4548 while (1) {
4549 outbuf.area = b_tail(&h2c->mbuf);
4550 outbuf.size = b_contig_space(&h2c->mbuf);
4551 outbuf.data = 0;
4552
4553 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4554 break;
4555 realign_again:
4556 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4557 }
4558
4559 if (outbuf.size < 9) {
4560 h2c->flags |= H2_CF_MUX_MFULL;
4561 h2s->flags |= H2_SF_BLK_MROOM;
4562 goto end;
4563 }
4564
4565 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4566 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4567 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4568 outbuf.data = 9;
4569
4570 /* we have in <fsize> the exact number of bytes we need to copy from
4571 * the HTX buffer. We need to check this against the connection's and
4572 * the stream's send windows, and to ensure that this fits in the max
4573 * frame size and in the buffer's available space minus 9 bytes (for
4574 * the frame header). The connection's flow control is applied last so
4575 * that we can use a separate list of streams which are immediately
4576 * unblocked on window opening. Note: we don't implement padding.
4577 */
4578
4579 /* EOM is presented with bsize==1 but would lead to the emission of an
4580 * empty frame, thus we force it to zero here.
4581 */
4582 if (type == HTX_BLK_EOM)
4583 bsize = fsize = 0;
4584
4585 if (!fsize)
4586 goto send_empty;
4587
4588 if (h2s->mws <= 0) {
4589 h2s->flags |= H2_SF_BLK_SFCTL;
4590 if (h2s->send_wait) {
4591 LIST_DEL(&h2s->list);
4592 LIST_INIT(&h2s->list);
4593 }
4594 goto end;
4595 }
4596
Willy Tarreauee573762018-12-04 15:25:57 +01004597 if (fsize > count)
4598 fsize = count;
4599
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004600 if (fsize > h2s->mws)
4601 fsize = h2s->mws; // >0
4602
4603 if (h2c->mfs && fsize > h2c->mfs)
4604 fsize = h2c->mfs; // >0
4605
4606 if (fsize + 9 > outbuf.size) {
4607 /* we have an opportunity for enlarging the too small
4608 * available space, let's try.
4609 * FIXME: is this really interesting to do? Maybe we'll
4610 * spend lots of time realigning instead of using two
4611 * frames.
4612 */
4613 if (b_space_wraps(&h2c->mbuf))
4614 goto realign_again;
4615 fsize = outbuf.size - 9;
4616
4617 if (fsize <= 0) {
4618 /* no need to send an empty frame here */
4619 h2c->flags |= H2_CF_MUX_MFULL;
4620 h2s->flags |= H2_SF_BLK_MROOM;
4621 goto end;
4622 }
4623 }
4624
4625 if (h2c->mws <= 0) {
4626 h2s->flags |= H2_SF_BLK_MFCTL;
4627 goto end;
4628 }
4629
4630 if (fsize > h2c->mws)
4631 fsize = h2c->mws;
4632
4633 /* now let's copy this this into the output buffer */
4634 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004635 h2s->mws -= fsize;
4636 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004637 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004638
4639 send_empty:
4640 /* update the frame's size */
4641 h2_set_frame_size(outbuf.area, fsize);
4642
4643 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4644 * meeting EOM. We should optimize this later.
4645 */
4646 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004647 total++; // EOM counts as one byte
4648 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004649 es_now = 1;
4650 }
4651
4652 if (es_now)
4653 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4654
4655 /* commit the H2 response */
4656 b_add(&h2c->mbuf, fsize + 9);
4657
4658 /* consume incoming HTX block, including EOM */
4659 total += fsize;
4660 if (fsize == bsize) {
4661 htx_remove_blk(htx, blk);
4662 if (fsize)
4663 goto new_frame;
4664 } else {
4665 /* we've truncated this block */
4666 htx_cut_data_blk(htx, blk, fsize);
4667 }
4668
4669 if (es_now) {
4670 if (h2s->st == H2_SS_OPEN)
4671 h2s->st = H2_SS_HLOC;
4672 else
4673 h2s_close(h2s);
4674
4675 h2s->flags |= H2_SF_ES_SENT;
4676 }
4677
4678 end:
4679 return total;
4680}
4681
Olivier Houchard6ff20392018-07-17 18:46:31 +02004682/* Called from the upper layer, to subscribe to events, such as being able to send */
4683static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4684{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004685 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004686 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004687 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004688
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004689 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004690 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004691 if (!(sw->events & SUB_RETRY_RECV)) {
4692 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004693 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004694 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004695 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004696 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004697 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004698 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004699 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004700 if (!(sw->events & SUB_RETRY_SEND)) {
4701 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004702 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004703 h2s->send_wait = sw;
4704 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4705 if (h2s->flags & H2_SF_BLK_MFCTL)
4706 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4707 else
4708 LIST_ADDQ(&h2c->send_list, &h2s->list);
4709 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004710 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004711 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004712 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004713 if (event_type != 0)
4714 return -1;
4715 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004716
4717
4718}
4719
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004720static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4721{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004722 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004723 struct h2s *h2s = cs->ctx;
4724
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004725 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004726 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004727 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004728 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004729 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004730 }
4731 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004732 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004733 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004734 if (h2s->send_wait == sw) {
4735 LIST_DEL(&h2s->list);
4736 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004737 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004738 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004739 }
4740 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004741 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4742 sw = param;
4743 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004744 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004745 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01004746 LIST_DEL(&h2s->list);
4747 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004748 }
4749 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004750 return 0;
4751}
4752
4753
Olivier Houchard511efea2018-08-16 15:30:32 +02004754/* Called from the upper layer, to receive data */
4755static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4756{
Olivier Houchard638b7992018-08-16 15:41:52 +02004757 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004758 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004759 struct htx *h2s_htx = NULL;
4760 struct htx *buf_htx = NULL;
4761 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004762 size_t ret = 0;
4763
4764 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004765 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4766 /* in HTX mode we ignore the count argument */
4767 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004768 if (htx_is_empty(h2s_htx)) {
4769 if (cs->flags & CS_FL_REOS)
4770 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004771 if (cs->flags & CS_FL_ERR_PENDING)
4772 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004773 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004774 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004775
4776 buf_htx = htx_from_buf(buf);
4777 count = htx_free_space(buf_htx);
4778
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004779 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004780
4781 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004782 htx_to_buf(buf_htx, buf);
4783 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004784 ret = htx_ret.ret;
4785 }
4786 else {
4787 ret = b_xfer(buf, &h2s->rxbuf, count);
4788 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004789
Olivier Houchard638b7992018-08-16 15:41:52 +02004790 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004791 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004792 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004793 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004794 if (cs->flags & CS_FL_REOS)
4795 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004796 if (cs->flags & CS_FL_ERR_PENDING)
4797 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004798 if (b_size(&h2s->rxbuf)) {
4799 b_free(&h2s->rxbuf);
4800 offer_buffers(NULL, tasks_run_queue);
4801 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004802 }
4803
Willy Tarreau082f5592018-11-25 08:03:32 +01004804 if (ret && h2c->dsi == h2s->id) {
4805 /* demux is blocking on this stream's buffer */
4806 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01004807 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01004808 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004809end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004810 return ret;
4811}
4812
Olivier Houchardd846c262018-10-19 17:24:29 +02004813static void h2_stop_senders(struct h2c *h2c)
4814{
4815 struct h2s *h2s, *h2s_back;
4816
4817 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4818 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4819 if (h2c->msi == h2s_id(h2s))
4820 continue;
4821 LIST_DEL(&h2s->list);
4822 LIST_INIT(&h2s->list);
4823 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004824 h2s->send_wait->events |= SUB_RETRY_SEND;
4825 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004826 LIST_ADD(&h2c->send_list, &h2s->list);
4827 }
4828}
4829
Willy Tarreau62f52692017-10-08 23:01:42 +02004830/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004831static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004832{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004833 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004834 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004835 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004836 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004837 struct htx *htx;
4838 struct htx_blk *blk;
4839 enum htx_blk_type btype;
4840 uint32_t bsize;
4841 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004842
Olivier Houchardd846c262018-10-19 17:24:29 +02004843 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004844 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004845 h2s->send_wait = NULL;
4846 LIST_DEL(&h2s->list);
4847 LIST_INIT(&h2s->list);
4848 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004849 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4850 return 0;
4851
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004852 /* htx will be enough to decide if we're using HTX or legacy */
4853 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4854
Willy Tarreau0bad0432018-06-14 16:54:01 +02004855 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004856 h2s->flags |= H2_SF_OUTGOING_DATA;
4857
Willy Tarreau751f2d02018-10-05 09:35:00 +02004858 if (h2s->id == 0) {
4859 int32_t id = h2c_get_next_sid(h2s->h2c);
4860
4861 if (id < 0) {
4862 cs->ctx = NULL;
4863 cs->flags |= CS_FL_ERROR;
4864 h2s_destroy(h2s);
4865 return 0;
4866 }
4867
4868 eb32_delete(&h2s->by_id);
4869 h2s->by_id.key = h2s->id = id;
4870 h2s->h2c->max_id = id;
4871 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4872 }
4873
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004874 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004875 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4876 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004877 idx = htx_get_head(htx);
4878 blk = htx_get_blk(htx, idx);
4879 btype = htx_get_blk_type(blk);
4880 bsize = htx_get_blksz(blk);
4881
4882 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004883 case HTX_BLK_REQ_SL:
4884 /* start-line before headers */
4885 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4886 if (ret > 0) {
4887 total += ret;
4888 count -= ret;
4889 if (ret < bsize)
4890 goto done;
4891 }
4892 break;
4893
Willy Tarreau115e83b2018-12-01 19:17:53 +01004894 case HTX_BLK_RES_SL:
4895 /* start-line before headers */
4896 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4897 if (ret > 0) {
4898 total += ret;
4899 count -= ret;
4900 if (ret < bsize)
4901 goto done;
4902 }
4903 break;
4904
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004905 case HTX_BLK_DATA:
4906 case HTX_BLK_EOD:
4907 case HTX_BLK_EOM:
4908 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004909 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004910 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004911 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004912 total += ret;
4913 count -= ret;
4914 if (ret < bsize)
4915 goto done;
4916 }
4917 break;
4918
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004919 default:
4920 htx_remove_blk(htx, blk);
4921 total += bsize;
4922 count -= bsize;
4923 break;
4924 }
4925 }
4926 goto done;
4927 }
4928
4929 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004930 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004931 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004932 if (h2s->h2c->flags & H2_CF_IS_BACK)
4933 ret = -1;
4934 else
4935 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004936 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004937 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004938 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004939 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004940 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004941 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004942 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004943
Willy Tarreau5dd17352018-06-14 13:33:30 +02004944 if (unlikely((int)ret <= 0)) {
4945 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004946 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4947 break;
4948 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004949 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004950 total += count;
4951 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004952 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004953 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004954 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004955 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004956 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004957 break;
4958 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004959
4960 total += ret;
4961 count -= ret;
4962
4963 if (h2s->st >= H2_SS_ERROR)
4964 break;
4965
4966 if (h2s->flags & H2_SF_BLK_ANY)
4967 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004968 }
4969
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004970 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004971 if (h2s->st >= H2_SS_ERROR) {
4972 /* trim any possibly pending data after we close (extra CR-LF,
4973 * unprocessed trailers, abnormal extra data, ...)
4974 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004975 total += count;
4976 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004977 }
4978
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004979 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004980 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004981 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004982 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004983 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004984 }
4985
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004986 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004987 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004988 } else {
4989 b_del(buf, total);
4990 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004991
4992 /* The mux is full, cancel the pending tasks */
4993 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4994 (h2s->flags & H2_SF_BLK_MBUSY))
4995 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004996
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004997 /* If we're running HTX, and we read the whole buffer, then pretend
4998 * we read exactly what the caller specified, as with HTX the caller
4999 * will always give the buffer size, instead of the amount of data
5000 * available.
5001 */
5002 if (htx && !b_data(buf))
5003 total = orig_count;
5004
Olivier Houchard7505f942018-08-21 18:10:44 +02005005 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005006 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005007 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005008
Olivier Houchard7505f942018-08-21 18:10:44 +02005009 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005010 /* If we're waiting for flow control, and we got a shutr on the
5011 * connection, we will never be unlocked, so add an error on
5012 * the conn_stream.
5013 */
5014 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5015 !b_data(&h2s->h2c->dbuf) &&
5016 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5017 if (cs->flags & CS_FL_EOS)
5018 cs->flags |= CS_FL_ERROR;
5019 else
5020 cs->flags |= CS_FL_ERR_PENDING;
5021 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005022 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005023}
5024
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005025/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005026static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005027{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005028 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005029 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005030 struct eb32_node *node;
5031 int fctl_cnt = 0;
5032 int send_cnt = 0;
5033 int tree_cnt = 0;
5034 int orph_cnt = 0;
5035
5036 if (!h2c)
5037 return;
5038
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005039 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005040 fctl_cnt++;
5041
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005042 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005043 send_cnt++;
5044
Willy Tarreau3af37712018-12-18 14:34:41 +01005045 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005046 node = eb32_first(&h2c->streams_by_id);
5047 while (node) {
5048 h2s = container_of(node, struct h2s, by_id);
5049 tree_cnt++;
5050 if (!h2s->cs)
5051 orph_cnt++;
5052 node = eb32_next(node);
5053 }
5054
Willy Tarreau987c0632018-12-18 10:32:05 +01005055 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5056 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5057 " .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 +02005058 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5059 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005060 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005061 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5062 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5063 h2c->msi,
5064 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5065 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5066
5067 if (h2s) {
5068 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5069 h2s, h2s->id, h2s->flags,
5070 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5071 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5072 h2s->cs);
5073 if (h2s->cs)
5074 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5075 h2s->cs->flags, h2s->cs->data);
5076 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005077}
Willy Tarreau62f52692017-10-08 23:01:42 +02005078
5079/*******************************************************/
5080/* functions below are dedicated to the config parsers */
5081/*******************************************************/
5082
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005083/* config parser for global "tune.h2.header-table-size" */
5084static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5085 struct proxy *defpx, const char *file, int line,
5086 char **err)
5087{
5088 if (too_many_args(1, args, err, NULL))
5089 return -1;
5090
5091 h2_settings_header_table_size = atoi(args[1]);
5092 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5093 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5094 return -1;
5095 }
5096 return 0;
5097}
Willy Tarreau62f52692017-10-08 23:01:42 +02005098
Willy Tarreaue6baec02017-07-27 11:45:11 +02005099/* config parser for global "tune.h2.initial-window-size" */
5100static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5101 struct proxy *defpx, const char *file, int line,
5102 char **err)
5103{
5104 if (too_many_args(1, args, err, NULL))
5105 return -1;
5106
5107 h2_settings_initial_window_size = atoi(args[1]);
5108 if (h2_settings_initial_window_size < 0) {
5109 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5110 return -1;
5111 }
5112 return 0;
5113}
5114
Willy Tarreau5242ef82017-07-27 11:47:28 +02005115/* config parser for global "tune.h2.max-concurrent-streams" */
5116static int h2_parse_max_concurrent_streams(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_max_concurrent_streams = atoi(args[1]);
5124 if (h2_settings_max_concurrent_streams < 0) {
5125 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5126 return -1;
5127 }
5128 return 0;
5129}
5130
Willy Tarreau62f52692017-10-08 23:01:42 +02005131
5132/****************************************/
5133/* MUX initialization and instanciation */
5134/***************************************/
5135
5136/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005137static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005138 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005139 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005140 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005141 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005142 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005143 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005144 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005145 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005146 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005147 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005148 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005149 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005150 .shutr = h2_shutr,
5151 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005152 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005153 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005154 .name = "H2",
5155};
5156
Christopher Faulet32f61c02018-04-10 14:33:41 +02005157/* PROTO selection : this mux registers PROTO token "h2" */
5158static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005159 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005160
Willy Tarreau0108d902018-11-25 19:14:37 +01005161INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5162
Willy Tarreauf8957272018-10-03 10:25:20 +02005163static struct mux_proto_list mux_proto_h2_htx =
5164 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5165
5166INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5167
Willy Tarreau62f52692017-10-08 23:01:42 +02005168/* config keyword parsers */
5169static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005170 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005171 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005172 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005173 { 0, NULL, NULL }
5174}};
5175
Willy Tarreau0108d902018-11-25 19:14:37 +01005176INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);