blob: 806c817bd3ed6f7d93fd443266b055ff40175b7e [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>
Willy Tarreau62f52692017-10-08 23:01:42 +020025#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010026#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020027#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020028
29
Willy Tarreau2a856182017-05-16 15:20:39 +020030/* dummy streams returned for idle and closed states */
31static const struct h2s *h2_closed_stream;
32static const struct h2s *h2_idle_stream;
33
Willy Tarreau5ab6b572017-09-22 08:05:00 +020034/* Connection flags (32 bit), in h2c->flags */
35#define H2_CF_NONE 0x00000000
36
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020037/* Flags indicating why writing to the mux is blocked. */
38#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
39#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
40#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
41
Willy Tarreau315d8072017-12-10 22:17:57 +010042/* Flags indicating why writing to the demux is blocked.
43 * The first two ones directly affect the ability for the mux to receive data
44 * from the connection. The other ones affect the mux's ability to demux
45 * received data.
46 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020047#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
48#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010049
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
51#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
52#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
53#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020054#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
55#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020056
Willy Tarreau081d4722017-05-16 21:51:05 +020057/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
59#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
60#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 +020061#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020062
Willy Tarreau5ab6b572017-09-22 08:05:00 +020063/* H2 connection state, in h2c->st0 */
64enum h2_cs {
65 H2_CS_PREFACE, // init done, waiting for connection preface
66 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
67 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
68 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010069 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
70 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020071 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
72 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
73 H2_CS_ENTRIES // must be last
74} __attribute__((packed));
75
76/* H2 connection descriptor */
77struct h2c {
78 struct connection *conn;
79
80 enum h2_cs st0; /* mux state */
81 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
82
83 /* 16 bit hole here */
84 uint32_t flags; /* connection flags: H2_CF_* */
85 int32_t max_id; /* highest ID known on this connection, <0 before preface */
86 uint32_t rcvd_c; /* newly received data to ACK for the connection */
87 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
88
89 /* states for the demux direction */
90 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020091 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020092
93 int32_t dsi; /* demux stream ID (<0 = idle) */
94 int32_t dfl; /* demux frame length (if dsi >= 0) */
95 int8_t dft; /* demux frame type (if dsi >= 0) */
96 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010097 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
98 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020099 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
100
101 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103 int32_t msi; /* mux stream ID (<0 = idle) */
104 int32_t mfl; /* mux frame length (if dsi >= 0) */
105 int8_t mft; /* mux frame type (if dsi >= 0) */
106 int8_t mff; /* mux frame flags (if dsi >= 0) */
107 /* 16 bit hole here */
108 int32_t miw; /* mux initial window size for all new streams */
109 int32_t mws; /* mux window size. Can be negative. */
110 int32_t mfs; /* mux's max frame size */
111
Willy Tarreauea392822017-10-31 10:02:25 +0100112 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100113 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100114 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200115 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200116 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100117 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200118 struct eb_root streams_by_id; /* all active streams by their ID */
119 struct list send_list; /* list of blocked streams requesting to send */
120 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200121 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200123 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200177 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200178 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200184 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200185 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200186 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
188 struct wait_event *send_wait; /* The streeam is waiting for flow control */
189 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200190};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200191
Willy Tarreauc6405142017-09-21 20:23:50 +0200192/* descriptor for an h2 frame header */
193struct h2_fh {
194 uint32_t len; /* length, host order, 24 bits */
195 uint32_t sid; /* stream id, host order, 31 bits */
196 uint8_t ft; /* frame type */
197 uint8_t ff; /* frame flags */
198};
199
Willy Tarreau8ceae722018-11-26 11:58:30 +0100200/* the h2c connection pool */
201DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
202
203/* the h2s stream pool */
204DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
205
Willy Tarreaudc572362018-12-12 08:08:05 +0100206/* The default connection window size is 65535, it may only be enlarged using
207 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
208 * we'll pretend we already received the difference between the two to send
209 * an equivalent window update to enlarge it to 2G-1.
210 */
211#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
212
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200213/* a few settings from the global section */
214static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200215static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200216static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200217
Willy Tarreau2a856182017-05-16 15:20:39 +0200218/* a dmumy closed stream */
219static const struct h2s *h2_closed_stream = &(const struct h2s){
220 .cs = NULL,
221 .h2c = NULL,
222 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100223 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100224 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200225 .id = 0,
226};
227
228/* and a dummy idle stream for use with any unannounced stream */
229static const struct h2s *h2_idle_stream = &(const struct h2s){
230 .cs = NULL,
231 .h2c = NULL,
232 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100233 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200234 .id = 0,
235};
236
Olivier Houchard9f6af332018-05-25 14:04:04 +0200237static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200238static int h2_send(struct h2c *h2c);
239static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200240static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200241static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100242static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200243static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100244static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200245static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreau751f2d02018-10-05 09:35:00 +0200246static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200247
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200248/*****************************************************/
249/* functions below are for dynamic buffer management */
250/*****************************************************/
251
Willy Tarreau315d8072017-12-10 22:17:57 +0100252/* indicates whether or not the we may call the h2_recv() function to attempt
253 * to receive data into the buffer and/or demux pending data. The condition is
254 * a bit complex due to some API limits for now. The rules are the following :
255 * - if an error or a shutdown was detected on the connection and the buffer
256 * is empty, we must not attempt to receive
257 * - if the demux buf failed to be allocated, we must not try to receive and
258 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100259 * - if no flag indicates a blocking condition, we may attempt to receive,
260 * regardless of whether the demux buffer is full or not, so that only
261 * de demux part decides whether or not to block. This is needed because
262 * the connection API indeed prevents us from re-enabling receipt that is
263 * already enabled in a polled state, so we must always immediately stop
264 * as soon as the demux can't proceed so as never to hit an end of read
265 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100266 * - otherwise must may not attempt
267 */
268static inline int h2_recv_allowed(const struct h2c *h2c)
269{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200270 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100271 (h2c->st0 >= H2_CS_ERROR ||
272 h2c->conn->flags & CO_FL_ERROR ||
273 conn_xprt_read0_pending(h2c->conn)))
274 return 0;
275
276 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100277 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100278 return 1;
279
280 return 0;
281}
282
Willy Tarreauf2101912018-07-19 10:11:38 +0200283/* returns true if the connection has too many conn_streams attached */
284static inline int h2_has_too_many_cs(const struct h2c *h2c)
285{
286 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
287}
288
Willy Tarreau44e973f2018-03-01 17:49:30 +0100289/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
290 * flags are used to figure what buffer was requested. It returns 1 if the
291 * allocation succeeds, in which case the connection is woken up, or 0 if it's
292 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200293 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100294static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200295{
296 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100297 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200298
Willy Tarreau44e973f2018-03-01 17:49:30 +0100299 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200300 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200301 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200302 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200303 return 1;
304 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200305
Willy Tarreau44e973f2018-03-01 17:49:30 +0100306 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
307 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200308
309 if (h2c->flags & H2_CF_DEM_MROOM) {
310 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200311 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200312 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200313 }
Willy Tarreau14398122017-09-22 14:26:04 +0200314 return 1;
315 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100316
317 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
318 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200319 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100320 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200321 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200322 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100323 return 1;
324 }
325
Willy Tarreau14398122017-09-22 14:26:04 +0200326 return 0;
327}
328
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200329static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200330{
331 struct buffer *buf = NULL;
332
Willy Tarreau44e973f2018-03-01 17:49:30 +0100333 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
334 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
335 h2c->buf_wait.target = h2c;
336 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100338 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100339 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200340 __conn_xprt_stop_recv(h2c->conn);
341 }
342 return buf;
343}
344
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200345static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200346{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200347 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100348 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200349 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200350 }
351}
352
Olivier Houchardd540b362018-11-05 18:37:53 +0100353static int h2_avail_streams(struct connection *conn)
354{
355 struct h2c *h2c = conn->mux_ctx;
356
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100357 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100358 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
359}
360
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100361static int h2_max_streams(struct connection *conn)
362{
363 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
364 return h2_settings_max_concurrent_streams;
365}
366
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau62f52692017-10-08 23:01:42 +0200368/*****************************************************************/
369/* functions below are dedicated to the mux setup and management */
370/*****************************************************************/
371
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200372/* Initialize the mux once it's attached. For outgoing connections, the context
373 * is already initialized before installing the mux, so we detect incoming
374 * connections from the fact that the context is still NULL. Returns < 0 on
375 * error.
376 */
377static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200378{
379 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100380 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381
Willy Tarreaubafbe012017-11-24 17:34:44 +0100382 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200384 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200385
Willy Tarreau01b44822018-10-03 14:26:37 +0200386 if (conn->mux_ctx) {
387 h2c->flags = H2_CF_IS_BACK;
388 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
389 if (tick_isset(prx->timeout.serverfin))
390 h2c->shut_timeout = prx->timeout.serverfin;
391 } else {
392 h2c->flags = H2_CF_NONE;
393 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
394 if (tick_isset(prx->timeout.clientfin))
395 h2c->shut_timeout = prx->timeout.clientfin;
396 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100397
Willy Tarreau0b37d652018-10-03 10:33:02 +0200398 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100399 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100400 if (tick_isset(h2c->timeout)) {
401 t = task_new(tid_bit);
402 if (!t)
403 goto fail;
404
405 h2c->task = t;
406 t->process = h2_timeout_task;
407 t->context = h2c;
408 t->expire = tick_add(now_ms, h2c->timeout);
409 }
Willy Tarreauea392822017-10-31 10:02:25 +0100410
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200411 h2c->wait_event.task = tasklet_new();
412 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200413 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200414 h2c->wait_event.task->process = h2_io_cb;
415 h2c->wait_event.task->context = h2c;
416 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200417
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
419 if (!h2c->ddht)
420 goto fail;
421
422 /* Initialise the context. */
423 h2c->st0 = H2_CS_PREFACE;
424 h2c->conn = conn;
425 h2c->max_id = -1;
426 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreaudc572362018-12-12 08:08:05 +0100427 h2c->rcvd_c = H2_INITIAL_WINDOW_INCREMENT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200428 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100429 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200430 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200432 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 h2c->dsi = -1;
434 h2c->msi = -1;
435 h2c->last_sid = -1;
436
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200437 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200438 h2c->miw = 65535; /* mux initial window size */
439 h2c->mws = 65535; /* mux window size */
440 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200441 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200442 LIST_INIT(&h2c->send_list);
443 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200444 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100445 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200446
Willy Tarreau3f133572017-10-31 19:21:06 +0100447 if (t)
448 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100449
Willy Tarreau01b44822018-10-03 14:26:37 +0200450 if (h2c->flags & H2_CF_IS_BACK) {
451 /* FIXME: this is temporary, for outgoing connections we need
452 * to immediately allocate a stream until the code is modified
453 * so that the caller calls ->attach(). For now the outgoing cs
454 * is stored as conn->mux_ctx by the caller.
455 */
456 struct h2s *h2s;
457
458 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx);
459 if (!h2s)
460 goto fail_stream;
461 }
462
463 conn->mux_ctx = h2c;
464
Willy Tarreau0f383582018-10-03 14:22:21 +0200465 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200466 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200467 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200468 fail_stream:
469 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200470 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100471 if (t)
472 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200473 if (h2c->wait_event.task)
474 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100475 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200476 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200477 return -1;
478}
479
Willy Tarreau751f2d02018-10-05 09:35:00 +0200480/* returns the next allocatable outgoing stream ID for the H2 connection, or
481 * -1 if no more is allocatable.
482 */
483static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
484{
485 int32_t id = (h2c->max_id + 1) | 1;
486 if (id & 0x80000000U)
487 id = -1;
488 return id;
489}
490
Willy Tarreau2373acc2017-10-12 17:35:14 +0200491/* returns the stream associated with id <id> or NULL if not found */
492static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
493{
494 struct eb32_node *node;
495
Willy Tarreau751f2d02018-10-05 09:35:00 +0200496 if (id == 0)
497 return (struct h2s *)h2_closed_stream;
498
Willy Tarreau2a856182017-05-16 15:20:39 +0200499 if (id > h2c->max_id)
500 return (struct h2s *)h2_idle_stream;
501
Willy Tarreau2373acc2017-10-12 17:35:14 +0200502 node = eb32_lookup(&h2c->streams_by_id, id);
503 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200504 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200505
506 return container_of(node, struct h2s, by_id);
507}
508
Willy Tarreau62f52692017-10-08 23:01:42 +0200509/* release function for a connection. This one should be called to free all
510 * resources allocated to the mux.
511 */
512static void h2_release(struct connection *conn)
513{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200514 struct h2c *h2c = conn->mux_ctx;
515
516 LIST_DEL(&conn->list);
517
518 if (h2c) {
519 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200520
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100521 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100522 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100523 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200524
Willy Tarreau44e973f2018-03-01 17:49:30 +0100525 h2_release_buf(h2c, &h2c->dbuf);
526 h2_release_buf(h2c, &h2c->mbuf);
527
Willy Tarreauea392822017-10-31 10:02:25 +0100528 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200529 h2c->task->context = NULL;
530 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100531 h2c->task = NULL;
532 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200533 if (h2c->wait_event.task)
534 tasklet_free(h2c->wait_event.task);
535 if (h2c->wait_event.wait_reason != 0)
536 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
537 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100538
Willy Tarreaubafbe012017-11-24 17:34:44 +0100539 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200540 }
541
542 conn->mux = NULL;
543 conn->mux_ctx = NULL;
544
545 conn_stop_tracking(conn);
546 conn_full_close(conn);
547 if (conn->destroy_cb)
548 conn->destroy_cb(conn);
549 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200550}
551
552
Willy Tarreau71681172017-10-23 14:39:06 +0200553/******************************************************/
554/* functions below are for the H2 protocol processing */
555/******************************************************/
556
557/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100558static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200559{
560 return h2s ? h2s->id : 0;
561}
562
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200563/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100564static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200565{
566 if (h2c->msi < 0)
567 return 0;
568
569 if (h2c->msi == h2s_id(h2s))
570 return 0;
571
572 return 1;
573}
574
Willy Tarreau741d6df2017-10-17 08:00:59 +0200575/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200577{
578 h2c->errcode = err;
579 h2c->st0 = H2_CS_ERROR;
580}
581
Willy Tarreau2e43f082017-10-17 08:03:59 +0200582/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100583static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200584{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200585 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200586 h2s->errcode = err;
587 h2s->st = H2_SS_ERROR;
588 if (h2s->cs)
589 h2s->cs->flags |= CS_FL_ERROR;
590 }
591}
592
Willy Tarreaue4820742017-07-27 13:37:23 +0200593/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100594static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200595{
596 uint8_t *out = frame;
597
598 *out = len >> 16;
599 write_n16(out + 1, len);
600}
601
Willy Tarreau54c15062017-10-10 17:10:03 +0200602/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
603 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
604 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200605 * available in the buffer's input prior to calling this function. The buffer
606 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200607 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100608static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200609 const struct buffer *b, int o)
610{
Willy Tarreau591d4452018-06-15 17:21:00 +0200611 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 +0200612}
613
Willy Tarreau1f094672017-11-20 21:27:45 +0100614static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200615{
Willy Tarreau591d4452018-06-15 17:21:00 +0200616 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200617}
618
Willy Tarreau1f094672017-11-20 21:27:45 +0100619static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200620{
Willy Tarreau591d4452018-06-15 17:21:00 +0200621 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200622}
623
Willy Tarreau1f094672017-11-20 21:27:45 +0100624static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200625{
Willy Tarreau591d4452018-06-15 17:21:00 +0200626 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200627}
628
629
Willy Tarreau715d5312017-07-11 15:20:24 +0200630/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
631 * is not obvious. It turns out that H2 headers are neither aligned nor do they
632 * use regular sizes. And to add to the trouble, the buffer may wrap so each
633 * byte read must be checked. The header is formed like this :
634 *
635 * b0 b1 b2 b3 b4 b5..b8
636 * +----------+---------+--------+----+----+----------------------+
637 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
638 * +----------+---------+--------+----+----+----------------------+
639 *
640 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
641 * we get the sid properly aligned and ordered, and 16 bits of len properly
642 * ordered as well. The type and flags can be extracted using bit shifts from
643 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200644 * Returns zero if some bytes are missing, otherwise non-zero on success. The
645 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200646 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100647static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200648{
649 uint64_t w;
650
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200651 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200652 return 0;
653
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200654 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200655 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200656 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
657 h->ff = w >> 32;
658 h->ft = w >> 40;
659 h->len += w >> 48;
660 return 1;
661}
662
663/* skip the next 9 bytes corresponding to the frame header possibly parsed by
664 * h2_peek_frame_hdr() above.
665 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100666static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200667{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200668 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200669}
670
671/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100672static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200673{
674 int ret;
675
676 ret = h2_peek_frame_hdr(b, h);
677 if (ret > 0)
678 h2_skip_frame_hdr(b);
679 return ret;
680}
681
Willy Tarreau00dd0782018-03-01 16:31:34 +0100682/* marks stream <h2s> as CLOSED and decrement the number of active streams for
683 * its connection if the stream was not yet closed. Please use this exclusively
684 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100685 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100686static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100687{
688 if (h2s->st != H2_SS_CLOSED)
689 h2s->h2c->nb_streams--;
690 h2s->st = H2_SS_CLOSED;
691}
692
Willy Tarreau71049cc2018-03-28 13:56:39 +0200693/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
694static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100695{
696 h2s_close(h2s);
697 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200698 if (b_size(&h2s->rxbuf)) {
699 b_free(&h2s->rxbuf);
700 offer_buffers(NULL, tasks_run_queue);
701 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200702 if (h2s->send_wait != NULL)
703 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
704 if (h2s->recv_wait != NULL)
705 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800706 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200707 * reference left would be in the h2c send_list/fctl_list, and if
708 * we're in it, we're getting out anyway
709 */
710 LIST_DEL(&h2s->list);
711 LIST_INIT(&h2s->list);
712 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100713 pool_free(pool_head_h2s, h2s);
714}
715
Willy Tarreaua8e49542018-10-03 18:53:55 +0200716/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
717 * stream tree. In case of error, nothing is added and NULL is returned. The
718 * causes of errors can be any failed memory allocation. The caller is
719 * responsible for checking if the connection may support an extra stream
720 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200721 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200722static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200723{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200724 struct h2s *h2s;
725
Willy Tarreaubafbe012017-11-24 17:34:44 +0100726 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200727 if (!h2s)
728 goto out;
729
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200730 h2s->wait_event.task = tasklet_new();
731 if (!h2s->wait_event.task) {
732 pool_free(pool_head_h2s, h2s);
733 goto out;
734 }
735 h2s->send_wait = NULL;
736 h2s->recv_wait = NULL;
737 h2s->wait_event.task->process = h2_deferred_shut;
738 h2s->wait_event.task->context = h2s;
739 h2s->wait_event.handle = NULL;
740 h2s->wait_event.wait_reason = 0;
741 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200742 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200743 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200744 h2s->mws = h2c->miw;
745 h2s->flags = H2_SF_NONE;
746 h2s->errcode = H2_ERR_NO_ERROR;
747 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200748 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200749 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200750
751 if (h2c->flags & H2_CF_IS_BACK) {
752 h1m_init_req(&h2s->h1m);
753 h2s->h1m.err_pos = -1; // don't care about errors on the request path
754 h2s->h1m.flags |= H1_MF_TOLOWER;
755 } else {
756 h1m_init_res(&h2s->h1m);
757 h2s->h1m.err_pos = -1; // don't care about errors on the response path
758 h2s->h1m.flags |= H1_MF_TOLOWER;
759 }
760
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200761 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200762 if (id > 0)
763 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200764
765 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100766 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200767
768 return h2s;
769
770 out_free_h2s:
771 pool_free(pool_head_h2s, h2s);
772 out:
773 return NULL;
774}
775
776/* creates a new stream <id> on the h2c connection and returns it, or NULL in
777 * case of memory allocation error.
778 */
779static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
780{
781 struct session *sess = h2c->conn->owner;
782 struct conn_stream *cs;
783 struct h2s *h2s;
784
785 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
786 goto out;
787
788 h2s = h2s_new(h2c, id);
789 if (!h2s)
790 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200791
792 cs = cs_new(h2c->conn);
793 if (!cs)
794 goto out_close;
795
796 h2s->cs = cs;
797 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200798 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200799
800 if (stream_create_from_cs(cs) < 0)
801 goto out_free_cs;
802
Willy Tarreau590a0512018-09-05 11:56:48 +0200803 /* We want the accept date presented to the next stream to be the one
804 * we have now, the handshake time to be null (since the next stream
805 * is not delayed by a handshake), and the idle time to count since
806 * right now.
807 */
808 sess->accept_date = date;
809 sess->tv_accept = now;
810 sess->t_handshake = 0;
811
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200812 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200813 if (h2_has_too_many_cs(h2c))
814 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200815 return h2s;
816
817 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200818 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200819 cs_free(cs);
820 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200821 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200822 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200823 sess_log(sess);
824 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200825}
826
Willy Tarreau751f2d02018-10-05 09:35:00 +0200827/* allocates a new stream associated to conn_stream <cs> on the h2c connection
828 * and returns it, or NULL in case of memory allocation error or if the highest
829 * possible stream ID was reached.
830 */
831static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs)
832{
833 struct h2s *h2s = NULL;
834
835 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
836 goto out;
837
838 /* Defer choosing the ID until we send the first message to create the stream */
839 h2s = h2s_new(h2c, 0);
840 if (!h2s)
841 goto out;
842
843 h2s->cs = cs;
844 cs->ctx = h2s;
845 h2c->nb_cs++;
846
Willy Tarreau751f2d02018-10-05 09:35:00 +0200847 out:
848 return h2s;
849}
850
Willy Tarreaube5b7152017-09-25 16:25:39 +0200851/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
852 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
853 * the various settings codes.
854 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200855static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200856{
857 struct buffer *res;
858 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200859 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200860 int ret;
861
862 if (h2c_mux_busy(h2c, NULL)) {
863 h2c->flags |= H2_CF_DEM_MBUSY;
864 return 0;
865 }
866
Willy Tarreau44e973f2018-03-01 17:49:30 +0100867 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200868 if (!res) {
869 h2c->flags |= H2_CF_MUX_MALLOC;
870 h2c->flags |= H2_CF_DEM_MROOM;
871 return 0;
872 }
873
874 chunk_init(&buf, buf_data, sizeof(buf_data));
875 chunk_memcpy(&buf,
876 "\x00\x00\x00" /* length : 0 for now */
877 "\x04\x00" /* type : 4 (settings), flags : 0 */
878 "\x00\x00\x00\x00", /* stream ID : 0 */
879 9);
880
881 if (h2_settings_header_table_size != 4096) {
882 char str[6] = "\x00\x01"; /* header_table_size */
883
884 write_n32(str + 2, h2_settings_header_table_size);
885 chunk_memcat(&buf, str, 6);
886 }
887
888 if (h2_settings_initial_window_size != 65535) {
889 char str[6] = "\x00\x04"; /* initial_window_size */
890
891 write_n32(str + 2, h2_settings_initial_window_size);
892 chunk_memcat(&buf, str, 6);
893 }
894
895 if (h2_settings_max_concurrent_streams != 0) {
896 char str[6] = "\x00\x03"; /* max_concurrent_streams */
897
898 /* Note: 0 means "unlimited" for haproxy's config but not for
899 * the protocol, so never send this value!
900 */
901 write_n32(str + 2, h2_settings_max_concurrent_streams);
902 chunk_memcat(&buf, str, 6);
903 }
904
905 if (global.tune.bufsize != 16384) {
906 char str[6] = "\x00\x05"; /* max_frame_size */
907
908 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
909 * match bufsize - rewrite size, but at the moment it seems
910 * that clients don't take care of it.
911 */
912 write_n32(str + 2, global.tune.bufsize);
913 chunk_memcat(&buf, str, 6);
914 }
915
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200916 h2_set_frame_size(buf.area, buf.data - 9);
917 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200918 if (unlikely(ret <= 0)) {
919 if (!ret) {
920 h2c->flags |= H2_CF_MUX_MFULL;
921 h2c->flags |= H2_CF_DEM_MROOM;
922 return 0;
923 }
924 else {
925 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
926 return 0;
927 }
928 }
929 return ret;
930}
931
Willy Tarreau52eed752017-09-22 15:05:09 +0200932/* Try to receive a connection preface, then upon success try to send our
933 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
934 * missing data. It may return an error in h2c.
935 */
936static int h2c_frt_recv_preface(struct h2c *h2c)
937{
938 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200939 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200940
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200941 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200942
943 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200944 if (ret1 < 0)
945 sess_log(h2c->conn->owner);
946
Willy Tarreau52eed752017-09-22 15:05:09 +0200947 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
948 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
949 return 0;
950 }
951
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200952 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200953 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200954 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200955
Willy Tarreaube5b7152017-09-25 16:25:39 +0200956 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200957}
958
Willy Tarreau01b44822018-10-03 14:26:37 +0200959/* Try to send a connection preface, then upon success try to send our
960 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
961 * missing data. It may return an error in h2c.
962 */
963static int h2c_bck_send_preface(struct h2c *h2c)
964{
965 struct buffer *res;
966
967 if (h2c_mux_busy(h2c, NULL)) {
968 h2c->flags |= H2_CF_DEM_MBUSY;
969 return 0;
970 }
971
972 res = h2_get_buf(h2c, &h2c->mbuf);
973 if (!res) {
974 h2c->flags |= H2_CF_MUX_MALLOC;
975 h2c->flags |= H2_CF_DEM_MROOM;
976 return 0;
977 }
978
979 if (!b_data(res)) {
980 /* preface not yet sent */
981 b_istput(res, ist(H2_CONN_PREFACE));
982 }
983
984 return h2c_send_settings(h2c);
985}
986
Willy Tarreau081d4722017-05-16 21:51:05 +0200987/* try to send a GOAWAY frame on the connection to report an error or a graceful
988 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
989 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
990 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
991 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
992 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
993 * on unrecoverable failure. It will not attempt to send one again in this last
994 * case so that it is safe to use h2c_error() to report such errors.
995 */
996static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
997{
998 struct buffer *res;
999 char str[17];
1000 int ret;
1001
1002 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1003 return 1; // claim that it worked
1004
1005 if (h2c_mux_busy(h2c, h2s)) {
1006 if (h2s)
1007 h2s->flags |= H2_SF_BLK_MBUSY;
1008 else
1009 h2c->flags |= H2_CF_DEM_MBUSY;
1010 return 0;
1011 }
1012
Willy Tarreau44e973f2018-03-01 17:49:30 +01001013 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001014 if (!res) {
1015 h2c->flags |= H2_CF_MUX_MALLOC;
1016 if (h2s)
1017 h2s->flags |= H2_SF_BLK_MROOM;
1018 else
1019 h2c->flags |= H2_CF_DEM_MROOM;
1020 return 0;
1021 }
1022
1023 /* len: 8, type: 7, flags: none, sid: 0 */
1024 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1025
1026 if (h2c->last_sid < 0)
1027 h2c->last_sid = h2c->max_id;
1028
1029 write_n32(str + 9, h2c->last_sid);
1030 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001031 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001032 if (unlikely(ret <= 0)) {
1033 if (!ret) {
1034 h2c->flags |= H2_CF_MUX_MFULL;
1035 if (h2s)
1036 h2s->flags |= H2_SF_BLK_MROOM;
1037 else
1038 h2c->flags |= H2_CF_DEM_MROOM;
1039 return 0;
1040 }
1041 else {
1042 /* we cannot report this error using GOAWAY, so we mark
1043 * it and claim a success.
1044 */
1045 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1046 h2c->flags |= H2_CF_GOAWAY_FAILED;
1047 return 1;
1048 }
1049 }
1050 h2c->flags |= H2_CF_GOAWAY_SENT;
1051 return ret;
1052}
1053
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001054/* Try to send an RST_STREAM frame on the connection for the indicated stream
1055 * during mux operations. This stream must be valid and cannot be closed
1056 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1057 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1058 * not yet.
1059 *
1060 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1061 * to write the message, it subscribes the stream to future notifications.
1062 */
1063static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1064{
1065 struct buffer *res;
1066 char str[13];
1067 int ret;
1068
1069 if (!h2s || h2s->st == H2_SS_CLOSED)
1070 return 1;
1071
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001072 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1073 * RST_STREAM in response to a RST_STREAM frame.
1074 */
1075 if (h2c->dft == H2_FT_RST_STREAM) {
1076 ret = 1;
1077 goto ignore;
1078 }
1079
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001080 if (h2c_mux_busy(h2c, h2s)) {
1081 h2s->flags |= H2_SF_BLK_MBUSY;
1082 return 0;
1083 }
1084
Willy Tarreau44e973f2018-03-01 17:49:30 +01001085 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001086 if (!res) {
1087 h2c->flags |= H2_CF_MUX_MALLOC;
1088 h2s->flags |= H2_SF_BLK_MROOM;
1089 return 0;
1090 }
1091
1092 /* len: 4, type: 3, flags: none */
1093 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1094 write_n32(str + 5, h2s->id);
1095 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001096 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001097
1098 if (unlikely(ret <= 0)) {
1099 if (!ret) {
1100 h2c->flags |= H2_CF_MUX_MFULL;
1101 h2s->flags |= H2_SF_BLK_MROOM;
1102 return 0;
1103 }
1104 else {
1105 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1106 return 0;
1107 }
1108 }
1109
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001110 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001111 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001112 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001113 return ret;
1114}
1115
1116/* Try to send an RST_STREAM frame on the connection for the stream being
1117 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1118 * error code unless the stream's state already is IDLE or CLOSED in which
1119 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1120 * it was not yet.
1121 *
1122 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1123 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001124 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001125 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001126 */
1127static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1128{
1129 struct buffer *res;
1130 char str[13];
1131 int ret;
1132
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001133 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1134 * RST_STREAM in response to a RST_STREAM frame.
1135 */
1136 if (h2c->dft == H2_FT_RST_STREAM) {
1137 ret = 1;
1138 goto ignore;
1139 }
1140
Willy Tarreau27a84c92017-10-17 08:10:17 +02001141 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001142 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001143 return 0;
1144 }
1145
Willy Tarreau44e973f2018-03-01 17:49:30 +01001146 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001147 if (!res) {
1148 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001149 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001150 return 0;
1151 }
1152
1153 /* len: 4, type: 3, flags: none */
1154 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001155
Willy Tarreau27a84c92017-10-17 08:10:17 +02001156 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001157 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001158 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001159
Willy Tarreau27a84c92017-10-17 08:10:17 +02001160 if (unlikely(ret <= 0)) {
1161 if (!ret) {
1162 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001163 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001164 return 0;
1165 }
1166 else {
1167 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1168 return 0;
1169 }
1170 }
1171
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001172 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001173 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001174 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001175 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001176 }
1177
Willy Tarreau27a84c92017-10-17 08:10:17 +02001178 return ret;
1179}
1180
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001181/* try to send an empty DATA frame with the ES flag set to notify about the
1182 * end of stream and match a shutdown(write). If an ES was already sent as
1183 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1184 * on success or zero if nothing was done. In case of lack of room to write the
1185 * message, it subscribes the requesting stream to future notifications.
1186 */
1187static int h2_send_empty_data_es(struct h2s *h2s)
1188{
1189 struct h2c *h2c = h2s->h2c;
1190 struct buffer *res;
1191 char str[9];
1192 int ret;
1193
Willy Tarreau721c9742017-11-07 11:05:42 +01001194 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001195 return 1;
1196
1197 if (h2c_mux_busy(h2c, h2s)) {
1198 h2s->flags |= H2_SF_BLK_MBUSY;
1199 return 0;
1200 }
1201
Willy Tarreau44e973f2018-03-01 17:49:30 +01001202 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001203 if (!res) {
1204 h2c->flags |= H2_CF_MUX_MALLOC;
1205 h2s->flags |= H2_SF_BLK_MROOM;
1206 return 0;
1207 }
1208
1209 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1210 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1211 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001212 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001213 if (likely(ret > 0)) {
1214 h2s->flags |= H2_SF_ES_SENT;
1215 }
1216 else if (!ret) {
1217 h2c->flags |= H2_CF_MUX_MFULL;
1218 h2s->flags |= H2_SF_BLK_MROOM;
1219 return 0;
1220 }
1221 else {
1222 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1223 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001224 }
1225 return ret;
1226}
1227
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001228/* wake the streams attached to the connection, whose id is greater than <last>,
1229 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001230 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1231 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001232 */
1233static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1234{
1235 struct eb32_node *node;
1236 struct h2s *h2s;
1237
1238 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1239 flags |= CS_FL_ERROR;
1240
1241 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001242 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001243
1244 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1245 while (node) {
1246 h2s = container_of(node, struct h2s, by_id);
1247 if (h2s->id <= last)
1248 break;
1249 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001250
1251 if (!h2s->cs) {
1252 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001253 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001254 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001255 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001256
1257 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001258 if (h2s->recv_wait) {
1259 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001260 sw->wait_reason &= ~SUB_CAN_RECV;
1261 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001262 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001263 } else if (h2s->cs->data_cb->wake != NULL)
1264 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001265
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001266 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1267 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001268 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001269 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001270 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001271 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001272 }
1273}
1274
Willy Tarreau3421aba2017-07-27 15:41:03 +02001275/* Increase all streams' outgoing window size by the difference passed in
1276 * argument. This is needed upon receipt of the settings frame if the initial
1277 * window size is different. The difference may be negative and the resulting
1278 * window size as well, for the time it takes to receive some window updates.
1279 */
1280static void h2c_update_all_ws(struct h2c *h2c, int diff)
1281{
1282 struct h2s *h2s;
1283 struct eb32_node *node;
1284
1285 if (!diff)
1286 return;
1287
1288 node = eb32_first(&h2c->streams_by_id);
1289 while (node) {
1290 h2s = container_of(node, struct h2s, by_id);
1291 h2s->mws += diff;
1292 node = eb32_next(node);
1293 }
1294}
1295
1296/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1297 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1298 * return an error in h2c. Described in RFC7540#6.5.
1299 */
1300static int h2c_handle_settings(struct h2c *h2c)
1301{
1302 unsigned int offset;
1303 int error;
1304
1305 if (h2c->dff & H2_F_SETTINGS_ACK) {
1306 if (h2c->dfl) {
1307 error = H2_ERR_FRAME_SIZE_ERROR;
1308 goto fail;
1309 }
1310 return 1;
1311 }
1312
1313 if (h2c->dsi != 0) {
1314 error = H2_ERR_PROTOCOL_ERROR;
1315 goto fail;
1316 }
1317
1318 if (h2c->dfl % 6) {
1319 error = H2_ERR_FRAME_SIZE_ERROR;
1320 goto fail;
1321 }
1322
1323 /* that's the limit we can process */
1324 if (h2c->dfl > global.tune.bufsize) {
1325 error = H2_ERR_FRAME_SIZE_ERROR;
1326 goto fail;
1327 }
1328
1329 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001330 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001331 return 0;
1332
1333 /* parse the frame */
1334 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001335 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1336 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001337
1338 switch (type) {
1339 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1340 /* we need to update all existing streams with the
1341 * difference from the previous iws.
1342 */
1343 if (arg < 0) { // RFC7540#6.5.2
1344 error = H2_ERR_FLOW_CONTROL_ERROR;
1345 goto fail;
1346 }
1347 h2c_update_all_ws(h2c, arg - h2c->miw);
1348 h2c->miw = arg;
1349 break;
1350 case H2_SETTINGS_MAX_FRAME_SIZE:
1351 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1352 error = H2_ERR_PROTOCOL_ERROR;
1353 goto fail;
1354 }
1355 h2c->mfs = arg;
1356 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001357 case H2_SETTINGS_ENABLE_PUSH:
1358 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1359 error = H2_ERR_PROTOCOL_ERROR;
1360 goto fail;
1361 }
1362 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001363 }
1364 }
1365
1366 /* need to ACK this frame now */
1367 h2c->st0 = H2_CS_FRAME_A;
1368 return 1;
1369 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001370 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001371 h2c_error(h2c, error);
1372 return 0;
1373}
1374
1375/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1376 * success or one of the h2_status values.
1377 */
1378static int h2c_ack_settings(struct h2c *h2c)
1379{
1380 struct buffer *res;
1381 char str[9];
1382 int ret = -1;
1383
1384 if (h2c_mux_busy(h2c, NULL)) {
1385 h2c->flags |= H2_CF_DEM_MBUSY;
1386 return 0;
1387 }
1388
Willy Tarreau44e973f2018-03-01 17:49:30 +01001389 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001390 if (!res) {
1391 h2c->flags |= H2_CF_MUX_MALLOC;
1392 h2c->flags |= H2_CF_DEM_MROOM;
1393 return 0;
1394 }
1395
1396 memcpy(str,
1397 "\x00\x00\x00" /* length : 0 (no data) */
1398 "\x04" "\x01" /* type : 4, flags : ACK */
1399 "\x00\x00\x00\x00" /* stream ID */, 9);
1400
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001401 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001402 if (unlikely(ret <= 0)) {
1403 if (!ret) {
1404 h2c->flags |= H2_CF_MUX_MFULL;
1405 h2c->flags |= H2_CF_DEM_MROOM;
1406 return 0;
1407 }
1408 else {
1409 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1410 return 0;
1411 }
1412 }
1413 return ret;
1414}
1415
Willy Tarreaucf68c782017-10-10 17:11:41 +02001416/* processes a PING frame and schedules an ACK if needed. The caller must pass
1417 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1418 * missing data. It may return an error in h2c.
1419 */
1420static int h2c_handle_ping(struct h2c *h2c)
1421{
1422 /* frame length must be exactly 8 */
1423 if (h2c->dfl != 8) {
1424 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1425 return 0;
1426 }
1427
1428 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001429 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001430 h2c->st0 = H2_CS_FRAME_A;
1431 return 1;
1432}
1433
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001434/* Try to send a window update for stream id <sid> and value <increment>.
1435 * Returns > 0 on success or zero on missing room or failure. It may return an
1436 * error in h2c.
1437 */
1438static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1439{
1440 struct buffer *res;
1441 char str[13];
1442 int ret = -1;
1443
1444 if (h2c_mux_busy(h2c, NULL)) {
1445 h2c->flags |= H2_CF_DEM_MBUSY;
1446 return 0;
1447 }
1448
Willy Tarreau44e973f2018-03-01 17:49:30 +01001449 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001450 if (!res) {
1451 h2c->flags |= H2_CF_MUX_MALLOC;
1452 h2c->flags |= H2_CF_DEM_MROOM;
1453 return 0;
1454 }
1455
1456 /* length: 4, type: 8, flags: none */
1457 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1458 write_n32(str + 5, sid);
1459 write_n32(str + 9, increment);
1460
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001461 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001462
1463 if (unlikely(ret <= 0)) {
1464 if (!ret) {
1465 h2c->flags |= H2_CF_MUX_MFULL;
1466 h2c->flags |= H2_CF_DEM_MROOM;
1467 return 0;
1468 }
1469 else {
1470 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1471 return 0;
1472 }
1473 }
1474 return ret;
1475}
1476
1477/* try to send pending window update for the connection. It's safe to call it
1478 * with no pending updates. Returns > 0 on success or zero on missing room or
1479 * failure. It may return an error in h2c.
1480 */
1481static int h2c_send_conn_wu(struct h2c *h2c)
1482{
1483 int ret = 1;
1484
1485 if (h2c->rcvd_c <= 0)
1486 return 1;
1487
1488 /* send WU for the connection */
1489 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1490 if (ret > 0)
1491 h2c->rcvd_c = 0;
1492
1493 return ret;
1494}
1495
1496/* try to send pending window update for the current dmux stream. It's safe to
1497 * call it with no pending updates. Returns > 0 on success or zero on missing
1498 * room or failure. It may return an error in h2c.
1499 */
1500static int h2c_send_strm_wu(struct h2c *h2c)
1501{
1502 int ret = 1;
1503
1504 if (h2c->rcvd_s <= 0)
1505 return 1;
1506
1507 /* send WU for the stream */
1508 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1509 if (ret > 0)
1510 h2c->rcvd_s = 0;
1511
1512 return ret;
1513}
1514
Willy Tarreaucf68c782017-10-10 17:11:41 +02001515/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1516 * success, 0 on missing data or one of the h2_status values.
1517 */
1518static int h2c_ack_ping(struct h2c *h2c)
1519{
1520 struct buffer *res;
1521 char str[17];
1522 int ret = -1;
1523
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001524 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001525 return 0;
1526
1527 if (h2c_mux_busy(h2c, NULL)) {
1528 h2c->flags |= H2_CF_DEM_MBUSY;
1529 return 0;
1530 }
1531
Willy Tarreau44e973f2018-03-01 17:49:30 +01001532 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001533 if (!res) {
1534 h2c->flags |= H2_CF_MUX_MALLOC;
1535 h2c->flags |= H2_CF_DEM_MROOM;
1536 return 0;
1537 }
1538
1539 memcpy(str,
1540 "\x00\x00\x08" /* length : 8 (same payload) */
1541 "\x06" "\x01" /* type : 6, flags : ACK */
1542 "\x00\x00\x00\x00" /* stream ID */, 9);
1543
1544 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001545 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001546
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001547 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001548 if (unlikely(ret <= 0)) {
1549 if (!ret) {
1550 h2c->flags |= H2_CF_MUX_MFULL;
1551 h2c->flags |= H2_CF_DEM_MROOM;
1552 return 0;
1553 }
1554 else {
1555 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1556 return 0;
1557 }
1558 }
1559 return ret;
1560}
1561
Willy Tarreau26f95952017-07-27 17:18:30 +02001562/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1563 * Returns > 0 on success or zero on missing data. It may return an error in
1564 * h2c or h2s. Described in RFC7540#6.9.
1565 */
1566static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1567{
1568 int32_t inc;
1569 int error;
1570
1571 if (h2c->dfl != 4) {
1572 error = H2_ERR_FRAME_SIZE_ERROR;
1573 goto conn_err;
1574 }
1575
1576 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001577 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001578 return 0;
1579
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001580 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001581
1582 if (h2c->dsi != 0) {
1583 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001584
1585 /* it's not an error to receive WU on a closed stream */
1586 if (h2s->st == H2_SS_CLOSED)
1587 return 1;
1588
1589 if (!inc) {
1590 error = H2_ERR_PROTOCOL_ERROR;
1591 goto strm_err;
1592 }
1593
1594 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1595 error = H2_ERR_FLOW_CONTROL_ERROR;
1596 goto strm_err;
1597 }
1598
1599 h2s->mws += inc;
1600 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1601 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001602 if (h2s->send_wait)
1603 LIST_ADDQ(&h2c->send_list, &h2s->list);
1604
Willy Tarreau26f95952017-07-27 17:18:30 +02001605 }
1606 }
1607 else {
1608 /* connection window update */
1609 if (!inc) {
1610 error = H2_ERR_PROTOCOL_ERROR;
1611 goto conn_err;
1612 }
1613
1614 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1615 error = H2_ERR_FLOW_CONTROL_ERROR;
1616 goto conn_err;
1617 }
1618
1619 h2c->mws += inc;
1620 }
1621
1622 return 1;
1623
1624 conn_err:
1625 h2c_error(h2c, error);
1626 return 0;
1627
1628 strm_err:
1629 if (h2s) {
1630 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001631 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001632 }
1633 else
1634 h2c_error(h2c, error);
1635 return 0;
1636}
1637
Willy Tarreaue96b0922017-10-30 00:28:29 +01001638/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1639 * the last ID. Returns > 0 on success or zero on missing data. It may return
1640 * an error in h2c. Described in RFC7540#6.8.
1641 */
1642static int h2c_handle_goaway(struct h2c *h2c)
1643{
1644 int error;
1645 int last;
1646
1647 if (h2c->dsi != 0) {
1648 error = H2_ERR_PROTOCOL_ERROR;
1649 goto conn_err;
1650 }
1651
1652 if (h2c->dfl < 8) {
1653 error = H2_ERR_FRAME_SIZE_ERROR;
1654 goto conn_err;
1655 }
1656
1657 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001658 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001659 return 0;
1660
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001661 last = h2_get_n32(&h2c->dbuf, 0);
1662 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001663 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001664 if (h2c->last_sid < 0)
1665 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001666 return 1;
1667
1668 conn_err:
1669 h2c_error(h2c, error);
1670 return 0;
1671}
1672
Willy Tarreau92153fc2017-12-03 19:46:19 +01001673/* processes a PRIORITY frame, and either skips it or rejects if it is
1674 * invalid. Returns > 0 on success or zero on missing data. It may return
1675 * an error in h2c. Described in RFC7540#6.3.
1676 */
1677static int h2c_handle_priority(struct h2c *h2c)
1678{
1679 int error;
1680
1681 if (h2c->dsi == 0) {
1682 error = H2_ERR_PROTOCOL_ERROR;
1683 goto conn_err;
1684 }
1685
1686 if (h2c->dfl != 5) {
1687 error = H2_ERR_FRAME_SIZE_ERROR;
1688 goto conn_err;
1689 }
1690
1691 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001692 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001693 return 0;
1694
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001695 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001696 /* 7540#5.3 : can't depend on itself */
1697 error = H2_ERR_PROTOCOL_ERROR;
1698 goto conn_err;
1699 }
1700 return 1;
1701
1702 conn_err:
1703 h2c_error(h2c, error);
1704 return 0;
1705}
1706
Willy Tarreaucd234e92017-08-18 10:59:39 +02001707/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1708 * Returns > 0 on success or zero on missing data. It may return an error in
1709 * h2c. Described in RFC7540#6.4.
1710 */
1711static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1712{
1713 int error;
1714
1715 if (h2c->dsi == 0) {
1716 error = H2_ERR_PROTOCOL_ERROR;
1717 goto conn_err;
1718 }
1719
Willy Tarreaucd234e92017-08-18 10:59:39 +02001720 if (h2c->dfl != 4) {
1721 error = H2_ERR_FRAME_SIZE_ERROR;
1722 goto conn_err;
1723 }
1724
1725 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001726 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001727 return 0;
1728
1729 /* late RST, already handled */
1730 if (h2s->st == H2_SS_CLOSED)
1731 return 1;
1732
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001734 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001735
1736 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001737 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001738 if (h2s->recv_wait) {
1739 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001740
1741 sw->wait_reason &= ~SUB_CAN_RECV;
1742 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001743 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001744 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001745 }
1746
1747 h2s->flags |= H2_SF_RST_RCVD;
1748 return 1;
1749
1750 conn_err:
1751 h2c_error(h2c, error);
1752 return 0;
1753}
1754
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001755/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1756 * It may return an error in h2c or h2s. The caller must consider that the
1757 * return value is the new h2s in case one was allocated (most common case).
1758 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001759 * errors here are reported as connection errors since it's impossible to
1760 * recover from such errors after the compression context has been altered.
1761 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001762static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001763{
1764 int error;
1765
1766 if (!h2c->dfl) {
1767 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001768 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001769 goto strm_err;
1770 }
1771
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001772 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001773 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001774
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001775 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001776 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001777
Willy Tarreauf2101912018-07-19 10:11:38 +02001778 if (h2c->flags & H2_CF_DEM_TOOMANY)
1779 return 0; // too many cs still present
1780
Willy Tarreau13278b42017-10-13 19:23:14 +02001781 /* now either the frame is complete or the buffer is complete */
1782 if (h2s->st != H2_SS_IDLE) {
1783 /* FIXME: stream already exists, this is only allowed for
1784 * trailers (not supported for now).
1785 */
1786 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001787 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001788 goto conn_err;
1789 }
1790 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1791 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1792 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001793 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001794 goto conn_err;
1795 }
1796
Willy Tarreau22de8d32018-09-05 19:55:58 +02001797 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001798 * positively from h2c_frt_stream_new(), the stream will report the error,
1799 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001800 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001801 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001802 if (!h2s) {
1803 error = H2_ERR_INTERNAL_ERROR;
1804 goto conn_err;
1805 }
1806
1807 h2s->st = H2_SS_OPEN;
1808 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1809 h2s->st = H2_SS_HREM;
1810 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001811 /* note: cs cannot be null for now (just created above) */
1812 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001813 }
1814
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001815 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001816 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001817
Willy Tarreau8f650c32017-11-21 19:36:21 +01001818 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001819 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001820
Willy Tarreau721c9742017-11-07 11:05:42 +01001821 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001822 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001823 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001824 }
1825 else {
1826 /* update the max stream ID if the request is being processed */
1827 if (h2s->id > h2c->max_id)
1828 h2c->max_id = h2s->id;
1829 }
1830
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001831 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001832
1833 conn_err:
1834 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001835 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001836
1837 strm_err:
1838 if (h2s) {
1839 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001840 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001841 }
1842 else
1843 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001844 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001845}
1846
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001847/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1848 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1849 * errors here are reported as connection errors since it's impossible to
1850 * recover from such errors after the compression context has been altered.
1851 */
1852static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1853{
1854 int error;
1855
1856 if (!h2c->dfl) {
1857 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1858 sess_log(h2c->conn->owner);
1859 goto strm_err;
1860 }
1861
1862 if (!b_size(&h2c->dbuf))
1863 return NULL; // empty buffer
1864
1865 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1866 return NULL; // incomplete frame
1867
1868 if (h2c->flags & H2_CF_DEM_TOOMANY)
1869 return 0; // too many cs still present
1870
1871 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1872 h2s->flags |= H2_SF_ES_RCVD;
1873 h2s->cs->flags |= CS_FL_REOS;
1874 }
1875
1876 if (!h2s_decode_headers(h2s))
1877 return NULL;
1878
1879 if (h2c->st0 >= H2_CS_ERROR)
1880 return NULL;
1881
1882 if (h2s->st >= H2_SS_ERROR) {
1883 /* stream error : send RST_STREAM */
1884 h2c->st0 = H2_CS_FRAME_E;
1885 }
1886
1887 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1888 h2s->st = H2_SS_ERROR;
1889 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1890 h2s->st = H2_SS_HREM;
1891 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1892 h2s_close(h2s);
1893
1894 return h2s;
1895
1896 conn_err:
1897 h2c_error(h2c, error);
1898 return NULL;
1899
1900 strm_err:
1901 if (h2s) {
1902 h2s_error(h2s, error);
1903 h2c->st0 = H2_CS_FRAME_E;
1904 }
1905 else
1906 h2c_error(h2c, error);
1907 return NULL;
1908}
1909
Willy Tarreau454f9052017-10-26 19:40:35 +02001910/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1911 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1912 */
1913static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1914{
1915 int error;
1916
1917 /* note that empty DATA frames are perfectly valid and sometimes used
1918 * to signal an end of stream (with the ES flag).
1919 */
1920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001922 return 0; // empty buffer
1923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001924 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001925 return 0; // incomplete frame
1926
1927 /* now either the frame is complete or the buffer is complete */
1928
1929 if (!h2c->dsi) {
1930 /* RFC7540#6.1 */
1931 error = H2_ERR_PROTOCOL_ERROR;
1932 goto conn_err;
1933 }
1934
1935 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1936 /* RFC7540#6.1 */
1937 error = H2_ERR_STREAM_CLOSED;
1938 goto strm_err;
1939 }
1940
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001941 if (!h2_frt_transfer_data(h2s))
1942 return 0;
1943
Willy Tarreau454f9052017-10-26 19:40:35 +02001944 /* call the upper layers to process the frame, then let the upper layer
1945 * notify the stream about any change.
1946 */
1947 if (!h2s->cs) {
1948 error = H2_ERR_STREAM_CLOSED;
1949 goto strm_err;
1950 }
1951
Willy Tarreau8f650c32017-11-21 19:36:21 +01001952 if (h2c->st0 >= H2_CS_ERROR)
1953 return 0;
1954
Willy Tarreau721c9742017-11-07 11:05:42 +01001955 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001956 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001957 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001958 }
1959
1960 /* check for completion : the callee will change this to FRAME_A or
1961 * FRAME_H once done.
1962 */
1963 if (h2c->st0 == H2_CS_FRAME_P)
1964 return 0;
1965
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001966
1967 /* last frame */
1968 if (h2c->dff & H2_F_DATA_END_STREAM) {
1969 h2s->st = H2_SS_HREM;
1970 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001971 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001972 }
1973
Willy Tarreau454f9052017-10-26 19:40:35 +02001974 return 1;
1975
1976 conn_err:
1977 h2c_error(h2c, error);
1978 return 0;
1979
1980 strm_err:
1981 if (h2s) {
1982 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001983 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001984 }
1985 else
1986 h2c_error(h2c, error);
1987 return 0;
1988}
1989
Willy Tarreaubc933932017-10-09 16:21:43 +02001990/* process Rx frames to be demultiplexed */
1991static void h2_process_demux(struct h2c *h2c)
1992{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001993 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001994
Willy Tarreau081d4722017-05-16 21:51:05 +02001995 if (h2c->st0 >= H2_CS_ERROR)
1996 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001997
1998 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1999 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002000 if (h2c->flags & H2_CF_IS_BACK)
2001 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002002 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2003 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002004 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002005 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002006 sess_log(h2c->conn->owner);
2007 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002008 goto fail;
2009 }
2010
2011 h2c->max_id = 0;
2012 h2c->st0 = H2_CS_SETTINGS1;
2013 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002014
2015 if (h2c->st0 == H2_CS_SETTINGS1) {
2016 struct h2_fh hdr;
2017
2018 /* ensure that what is pending is a valid SETTINGS frame
2019 * without an ACK.
2020 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002021 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002022 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002023 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002024 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002025 sess_log(h2c->conn->owner);
2026 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002027 goto fail;
2028 }
2029
2030 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2031 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2032 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2033 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002034 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002035 goto fail;
2036 }
2037
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002038 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002039 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2040 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2041 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002042 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002043 goto fail;
2044 }
2045
2046 /* that's OK, switch to FRAME_P to process it */
2047 h2c->dfl = hdr.len;
2048 h2c->dsi = hdr.sid;
2049 h2c->dft = hdr.ft;
2050 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002051 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002052 h2c->st0 = H2_CS_FRAME_P;
2053 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002054 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002055
2056 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002057 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002058 int ret = 0;
2059
2060 if (h2c->st0 >= H2_CS_ERROR)
2061 break;
2062
2063 if (h2c->st0 == H2_CS_FRAME_H) {
2064 struct h2_fh hdr;
2065
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002066 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002067 break;
2068
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002069 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002070 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2071 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002072 if (!h2c->nb_streams) {
2073 /* only log if no other stream can report the error */
2074 sess_log(h2c->conn->owner);
2075 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002076 break;
2077 }
2078
2079 h2c->dfl = hdr.len;
2080 h2c->dsi = hdr.sid;
2081 h2c->dft = hdr.ft;
2082 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002083 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002084 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002085 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002086 }
2087
2088 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002089 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2090
Olivier Houchard638b7992018-08-16 15:41:52 +02002091 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002092 /* we may have to signal the upper layers */
2093 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002094 if (h2s->recv_wait) {
2095 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2096 tasklet_wakeup(h2s->recv_wait->task);
2097 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002098 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002099 }
2100 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002101
Willy Tarreaud7901432017-12-29 11:34:40 +01002102 if (h2c->st0 == H2_CS_FRAME_E)
2103 goto strm_err;
2104
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002105 if (h2s->st == H2_SS_IDLE &&
2106 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2107 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2108 * this state MUST be treated as a connection error
2109 */
2110 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2111 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002112 if (!h2c->nb_streams) {
2113 /* only log if no other stream can report the error */
2114 sess_log(h2c->conn->owner);
2115 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002116 break;
2117 }
2118
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002119 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2120 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2121 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2122 * this state MUST be treated as a stream error
2123 */
2124 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2125 goto strm_err;
2126 }
2127
Willy Tarreauab837502017-12-27 15:07:30 +01002128 /* Below the management of frames received in closed state is a
2129 * bit hackish because the spec makes strong differences between
2130 * streams closed by receiving RST, sending RST, and seeing ES
2131 * in both directions. In addition to this, the creation of a
2132 * new stream reusing the identifier of a closed one will be
2133 * detected here. Given that we cannot keep track of all closed
2134 * streams forever, we consider that unknown closed streams were
2135 * closed on RST received, which allows us to respond with an
2136 * RST without breaking the connection (eg: to abort a transfer).
2137 * Some frames have to be silently ignored as well.
2138 */
2139 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2140 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2141 /* #5.1.1: The identifier of a newly
2142 * established stream MUST be numerically
2143 * greater than all streams that the initiating
2144 * endpoint has opened or reserved. This
2145 * governs streams that are opened using a
2146 * HEADERS frame and streams that are reserved
2147 * using PUSH_PROMISE. An endpoint that
2148 * receives an unexpected stream identifier
2149 * MUST respond with a connection error.
2150 */
2151 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2152 goto strm_err;
2153 }
2154
2155 if (h2s->flags & H2_SF_RST_RCVD) {
2156 /* RFC7540#5.1:closed: an endpoint that
2157 * receives any frame other than PRIORITY after
2158 * receiving a RST_STREAM MUST treat that as a
2159 * stream error of type STREAM_CLOSED.
2160 *
2161 * Note that old streams fall into this category
2162 * and will lead to an RST being sent.
2163 */
2164 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2165 h2c->st0 = H2_CS_FRAME_E;
2166 goto strm_err;
2167 }
2168
2169 /* RFC7540#5.1:closed: if this state is reached as a
2170 * result of sending a RST_STREAM frame, the peer that
2171 * receives the RST_STREAM might have already sent
2172 * frames on the stream that cannot be withdrawn. An
2173 * endpoint MUST ignore frames that it receives on
2174 * closed streams after it has sent a RST_STREAM
2175 * frame. An endpoint MAY choose to limit the period
2176 * over which it ignores frames and treat frames that
2177 * arrive after this time as being in error.
2178 */
2179 if (!(h2s->flags & H2_SF_RST_SENT)) {
2180 /* RFC7540#5.1:closed: any frame other than
2181 * PRIO/WU/RST in this state MUST be treated as
2182 * a connection error
2183 */
2184 if (h2c->dft != H2_FT_RST_STREAM &&
2185 h2c->dft != H2_FT_PRIORITY &&
2186 h2c->dft != H2_FT_WINDOW_UPDATE) {
2187 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2188 goto strm_err;
2189 }
2190 }
2191 }
2192
Willy Tarreauc0da1962017-10-30 18:38:00 +01002193#if 0
2194 // problem below: it is not possible to completely ignore such
2195 // streams as we need to maintain the compression state as well
2196 // and for this we need to completely process these frames (eg:
2197 // HEADERS frames) as well as counting DATA frames to emit
2198 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2199 // This is a typical case of layer violation where the
2200 // transported contents are critical to the connection's
2201 // validity and must be ignored at the same time :-(
2202
2203 /* graceful shutdown, ignore streams whose ID is higher than
2204 * the one advertised in GOAWAY. RFC7540#6.8.
2205 */
2206 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002207 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2208 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002209 h2c->dfl -= ret;
2210 ret = h2c->dfl == 0;
2211 goto strm_err;
2212 }
2213#endif
2214
Willy Tarreau7e98c052017-10-10 15:56:59 +02002215 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002216 case H2_FT_SETTINGS:
2217 if (h2c->st0 == H2_CS_FRAME_P)
2218 ret = h2c_handle_settings(h2c);
2219
2220 if (h2c->st0 == H2_CS_FRAME_A)
2221 ret = h2c_ack_settings(h2c);
2222 break;
2223
Willy Tarreaucf68c782017-10-10 17:11:41 +02002224 case H2_FT_PING:
2225 if (h2c->st0 == H2_CS_FRAME_P)
2226 ret = h2c_handle_ping(h2c);
2227
2228 if (h2c->st0 == H2_CS_FRAME_A)
2229 ret = h2c_ack_ping(h2c);
2230 break;
2231
Willy Tarreau26f95952017-07-27 17:18:30 +02002232 case H2_FT_WINDOW_UPDATE:
2233 if (h2c->st0 == H2_CS_FRAME_P)
2234 ret = h2c_handle_window_update(h2c, h2s);
2235 break;
2236
Willy Tarreau61290ec2017-10-17 08:19:21 +02002237 case H2_FT_CONTINUATION:
2238 /* we currently don't support CONTINUATION frames since
2239 * we have nowhere to store the partial HEADERS frame.
2240 * Let's abort the stream on an INTERNAL_ERROR here.
2241 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002242 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002243 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002244 h2c->st0 = H2_CS_FRAME_E;
2245 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002246 break;
2247
Willy Tarreau13278b42017-10-13 19:23:14 +02002248 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002249 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002250 if (h2c->flags & H2_CF_IS_BACK)
2251 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2252 else
2253 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002254 if (tmp_h2s) {
2255 h2s = tmp_h2s;
2256 ret = 1;
2257 }
2258 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002259 break;
2260
Willy Tarreau454f9052017-10-26 19:40:35 +02002261 case H2_FT_DATA:
2262 if (h2c->st0 == H2_CS_FRAME_P)
2263 ret = h2c_frt_handle_data(h2c, h2s);
2264
2265 if (h2c->st0 == H2_CS_FRAME_A)
2266 ret = h2c_send_strm_wu(h2c);
2267 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002268
Willy Tarreau92153fc2017-12-03 19:46:19 +01002269 case H2_FT_PRIORITY:
2270 if (h2c->st0 == H2_CS_FRAME_P)
2271 ret = h2c_handle_priority(h2c);
2272 break;
2273
Willy Tarreaucd234e92017-08-18 10:59:39 +02002274 case H2_FT_RST_STREAM:
2275 if (h2c->st0 == H2_CS_FRAME_P)
2276 ret = h2c_handle_rst_stream(h2c, h2s);
2277 break;
2278
Willy Tarreaue96b0922017-10-30 00:28:29 +01002279 case H2_FT_GOAWAY:
2280 if (h2c->st0 == H2_CS_FRAME_P)
2281 ret = h2c_handle_goaway(h2c);
2282 break;
2283
Willy Tarreau1c661982017-10-30 13:52:01 +01002284 case H2_FT_PUSH_PROMISE:
2285 /* not permitted here, RFC7540#5.1 */
2286 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002287 if (!h2c->nb_streams) {
2288 /* only log if no other stream can report the error */
2289 sess_log(h2c->conn->owner);
2290 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002291 break;
2292
2293 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002294 default:
2295 /* drop frames that we ignore. They may be larger than
2296 * the buffer so we drain all of their contents until
2297 * we reach the end.
2298 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002299 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2300 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002301 h2c->dfl -= ret;
2302 ret = h2c->dfl == 0;
2303 }
2304
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002305 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002306 /* We may have to send an RST if not done yet */
2307 if (h2s->st == H2_SS_ERROR)
2308 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002309
Willy Tarreaua20a5192017-12-27 11:02:06 +01002310 if (h2c->st0 == H2_CS_FRAME_E)
2311 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002312
Willy Tarreau7e98c052017-10-10 15:56:59 +02002313 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002314 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002315 break;
2316
2317 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002318 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002319 h2c->st0 = H2_CS_FRAME_H;
2320 }
2321 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002322
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002323 if (h2c->rcvd_c > 0 &&
2324 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2325 h2c_send_conn_wu(h2c);
2326
Willy Tarreau52eed752017-09-22 15:05:09 +02002327 fail:
2328 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002329 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002330 /* we may have to signal the upper layers */
2331 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002332 if (h2s->recv_wait) {
2333 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2334 tasklet_wakeup(h2s->recv_wait->task);
2335 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002336 }
2337 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002338
2339 if (h2_recv_allowed(h2c))
2340 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002341}
2342
2343/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2344 * the end.
2345 */
2346static int h2_process_mux(struct h2c *h2c)
2347{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002348 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002349
Willy Tarreau01b44822018-10-03 14:26:37 +02002350 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2351 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2352 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2353 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2354 if (h2c->st0 == H2_CS_ERROR) {
2355 h2c->st0 = H2_CS_ERROR2;
2356 sess_log(h2c->conn->owner);
2357 }
2358 goto fail;
2359 }
2360 h2c->st0 = H2_CS_SETTINGS1;
2361 }
2362 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002363 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002364 return 1;
2365 }
2366
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002367 /* start by sending possibly pending window updates */
2368 if (h2c->rcvd_c > 0 &&
2369 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2370 h2c_send_conn_wu(h2c) < 0)
2371 goto fail;
2372
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002373 /* First we always process the flow control list because the streams
2374 * waiting there were already elected for immediate emission but were
2375 * blocked just on this.
2376 */
2377
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002378 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002379 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2380 h2c->st0 >= H2_CS_ERROR)
2381 break;
2382
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002383 h2s->flags &= ~H2_SF_BLK_ANY;
2384 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002385 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002386 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002387 LIST_DEL(&h2s->list);
2388 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002389 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002390 }
2391
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002392 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002393 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2394 break;
2395
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002396 h2s->flags &= ~H2_SF_BLK_ANY;
2397 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002398 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002399 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002400 LIST_DEL(&h2s->list);
2401 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002402 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002403 }
2404
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002405 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002406 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002407 if (h2c->st0 == H2_CS_ERROR) {
2408 if (h2c->max_id >= 0) {
2409 h2c_send_goaway_error(h2c, NULL);
2410 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2411 return 0;
2412 }
2413
2414 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2415 }
2416 return 1;
2417 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002418 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002419}
2420
Willy Tarreau62f52692017-10-08 23:01:42 +02002421
Willy Tarreau479998a2018-11-18 06:30:59 +01002422/* Attempt to read data, and subscribe if none available.
2423 * The function returns 1 if data has been received, otherwise zero.
2424 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002425static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002426{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002427 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002428 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002429 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002430 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002431
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002432 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002433 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002434
Willy Tarreau315d8072017-12-10 22:17:57 +01002435 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002436 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002437
Willy Tarreau44e973f2018-03-01 17:49:30 +01002438 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002439 if (!buf) {
2440 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002441 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002442 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002443
Olivier Houchard7505f942018-08-21 18:10:44 +02002444 do {
Willy Tarreau2a59e872018-12-12 08:23:47 +01002445 int aligned = 0;
2446
2447 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2448 /* HTX in use : try to pre-align the buffer like the
2449 * rxbufs will be to optimize memory copies. We'll make
2450 * sure that the frame header lands at the end of the
2451 * HTX block to alias it upon recv. We cannot use the
2452 * head because rcv_buf() will realign the buffer if
2453 * it's empty. Thus we cheat and pretend we already
2454 * have a few bytes there.
2455 */
2456 max = buf_room_for_htx_data(buf) + 9;
2457 buf->head = 0;
2458 buf->data = sizeof(struct htx) - 9;
2459 aligned = 1;
2460 }
2461 else
2462 max = b_room(buf);
2463
Olivier Houchard7505f942018-08-21 18:10:44 +02002464 if (max)
2465 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2466 else
2467 ret = 0;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002468
2469 if (aligned) {
2470 buf->data -= sizeof(struct htx) - 9;
2471 buf->head = sizeof(struct htx) - 9;
2472 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002473 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002474
Olivier Houchard53216e72018-10-10 15:46:36 +02002475 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002476 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002477
Olivier Houcharda1411e62018-08-17 18:42:48 +02002478 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002479 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002480 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002481 }
2482
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002483 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002484 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002485 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002486}
2487
Willy Tarreau479998a2018-11-18 06:30:59 +01002488/* Try to send data if possible.
2489 * The function returns 1 if data have been sent, otherwise zero.
2490 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002491static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002492{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002493 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002494 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002495 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002496
2497 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002498 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002499
Olivier Houchard7505f942018-08-21 18:10:44 +02002500
Willy Tarreaua2af5122017-10-09 11:56:46 +02002501 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2502 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002503 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002504 }
2505
Willy Tarreaubc933932017-10-09 16:21:43 +02002506 /* This loop is quite simple : it tries to fill as much as it can from
2507 * pending streams into the existing buffer until it's reportedly full
2508 * or the end of send requests is reached. Then it tries to send this
2509 * buffer's contents out, marks it not full if at least one byte could
2510 * be sent, and tries again.
2511 *
2512 * The snd_buf() function normally takes a "flags" argument which may
2513 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2514 * data immediately comes and CO_SFL_STREAMER to indicate that the
2515 * connection is streaming lots of data (used to increase TLS record
2516 * size at the expense of latency). The former can be sent any time
2517 * there's a buffer full flag, as it indicates at least one stream
2518 * attempted to send and failed so there are pending data. An
2519 * alternative would be to set it as long as there's an active stream
2520 * but that would be problematic for ACKs until we have an absolute
2521 * guarantee that all waiters have at least one byte to send. The
2522 * latter should possibly not be set for now.
2523 */
2524
2525 done = 0;
2526 while (!done) {
2527 unsigned int flags = 0;
2528
2529 /* fill as much as we can into the current buffer */
2530 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2531 done = h2_process_mux(h2c);
2532
2533 if (conn->flags & CO_FL_ERROR)
2534 break;
2535
2536 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2537 flags |= CO_SFL_MSG_MORE;
2538
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002539 if (b_data(&h2c->mbuf)) {
2540 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002541 if (!ret)
2542 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002543 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002544 b_del(&h2c->mbuf, ret);
2545 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002546 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002547
2548 /* wrote at least one byte, the buffer is not full anymore */
2549 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2550 }
2551
Willy Tarreaua2af5122017-10-09 11:56:46 +02002552 if (conn->flags & CO_FL_SOCK_WR_SH) {
2553 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002554 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002555 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002556 /* We're not full anymore, so we can wake any task that are waiting
2557 * for us.
2558 */
2559 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002560 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002561 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2562 struct h2s *, list);
2563 LIST_DEL(&h2s->list);
2564 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002565 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002566 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002567 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002568 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002569 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002570 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002571 /* We're done, no more to send */
2572 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002573 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002574schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002575 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2576 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002577 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002578}
2579
2580static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2581{
2582 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002583 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002584
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002585 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002586 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002587 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002588 ret |= h2_recv(h2c);
2589 if (ret)
2590 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002591 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002592}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002593
Willy Tarreau62f52692017-10-08 23:01:42 +02002594/* callback called on any event by the connection handler.
2595 * It applies changes and returns zero, or < 0 if it wants immediate
2596 * destruction of the connection (which normally doesn not happen in h2).
2597 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002598static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002599{
Olivier Houchard7505f942018-08-21 18:10:44 +02002600 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002601
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002602 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002603 h2_process_demux(h2c);
2604
2605 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002606 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002607
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002608 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002609 h2c->flags &= ~H2_CF_DEM_DFULL;
2610 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002611 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002612
Willy Tarreau0b37d652018-10-03 10:33:02 +02002613 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002614 /* frontend is stopping, reload likely in progress, let's try
2615 * to announce a graceful shutdown if not yet done. We don't
2616 * care if it fails, it will be tried again later.
2617 */
2618 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2619 if (h2c->last_sid < 0)
2620 h2c->last_sid = (1U << 31) - 1;
2621 h2c_send_goaway_error(h2c, NULL);
2622 }
2623 }
2624
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002625 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002626 * If we received early data, and the handshake is done, wake
2627 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002628 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002629 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2630 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2631 struct eb32_node *node;
2632 struct h2s *h2s;
2633
2634 h2c->flags |= H2_CF_WAIT_FOR_HS;
2635 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2636
2637 while (node) {
2638 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002639 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002640 h2s->recv_wait) {
2641 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002642 sw->wait_reason &= ~SUB_CAN_RECV;
2643 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002644 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002645 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002646 node = eb32_next(node);
2647 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002648 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002649
Willy Tarreau26bd7612017-10-09 16:47:04 +02002650 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002651 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2652 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2653 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002654 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002655
2656 if (eb_is_empty(&h2c->streams_by_id)) {
2657 /* no more stream, kill the connection now */
2658 h2_release(conn);
2659 return -1;
2660 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002661 }
2662
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002663 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002664 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002665
Olivier Houchard53216e72018-10-10 15:46:36 +02002666 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2667 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2668 (h2c->st0 != H2_CS_ERROR &&
2669 !b_data(&h2c->mbuf) &&
2670 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2671 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002672 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002673
Willy Tarreau3f133572017-10-31 19:21:06 +01002674 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002675 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002676 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002677 task_queue(h2c->task);
2678 }
2679 else
2680 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002681 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002682
Olivier Houchard7505f942018-08-21 18:10:44 +02002683 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002684 return 0;
2685}
2686
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002687static int h2_wake(struct connection *conn)
2688{
2689 struct h2c *h2c = conn->mux_ctx;
2690
2691 return (h2_process(h2c));
2692}
2693
Willy Tarreauea392822017-10-31 10:02:25 +01002694/* Connection timeout management. The principle is that if there's no receipt
2695 * nor sending for a certain amount of time, the connection is closed. If the
2696 * MUX buffer still has lying data or is not allocatable, the connection is
2697 * immediately killed. If it's allocatable and empty, we attempt to send a
2698 * GOAWAY frame.
2699 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002700static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002701{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002702 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002703 int expired = tick_is_expired(t->expire, now_ms);
2704
Willy Tarreau0975f112018-03-29 15:22:59 +02002705 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002706 return t;
2707
Willy Tarreau0975f112018-03-29 15:22:59 +02002708 task_delete(t);
2709 task_free(t);
2710
2711 if (!h2c) {
2712 /* resources were already deleted */
2713 return NULL;
2714 }
2715
2716 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002717 h2c_error(h2c, H2_ERR_NO_ERROR);
2718 h2_wake_some_streams(h2c, 0, 0);
2719
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002720 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002721 /* don't even try to send a GOAWAY, the buffer is stuck */
2722 h2c->flags |= H2_CF_GOAWAY_FAILED;
2723 }
2724
2725 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002726 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002727 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2728 h2c->flags |= H2_CF_GOAWAY_FAILED;
2729
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002730 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2731 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002732 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002733 b_del(&h2c->mbuf, ret);
2734 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002735 }
2736 }
Willy Tarreauea392822017-10-31 10:02:25 +01002737
Willy Tarreau0975f112018-03-29 15:22:59 +02002738 /* either we can release everything now or it will be done later once
2739 * the last stream closes.
2740 */
2741 if (eb_is_empty(&h2c->streams_by_id))
2742 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002743
Willy Tarreauea392822017-10-31 10:02:25 +01002744 return NULL;
2745}
2746
2747
Willy Tarreau62f52692017-10-08 23:01:42 +02002748/*******************************************/
2749/* functions below are used by the streams */
2750/*******************************************/
2751
2752/*
2753 * Attach a new stream to a connection
2754 * (Used for outgoing connections)
2755 */
2756static struct conn_stream *h2_attach(struct connection *conn)
2757{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002758 struct conn_stream *cs;
2759 struct h2s *h2s;
2760 struct h2c *h2c = conn->mux_ctx;
2761
2762 cs = cs_new(conn);
2763 if (!cs)
2764 return NULL;
2765 h2s = h2c_bck_stream_new(h2c, cs);
2766 if (!h2s) {
2767 cs_free(cs);
2768 return NULL;
2769 }
2770 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002771}
2772
Willy Tarreaufafd3982018-11-18 21:29:20 +01002773/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2774 * We have to scan because we may have some orphan streams. It might be
2775 * beneficial to scan backwards from the end to reduce the likeliness to find
2776 * orphans.
2777 */
2778static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2779{
2780 struct h2c *h2c = conn->mux_ctx;
2781 struct h2s *h2s;
2782 struct eb32_node *node;
2783
2784 node = eb32_first(&h2c->streams_by_id);
2785 while (node) {
2786 h2s = container_of(node, struct h2s, by_id);
2787 if (h2s->cs)
2788 return h2s->cs;
2789 node = eb32_next(node);
2790 }
2791 return NULL;
2792}
2793
Willy Tarreau62f52692017-10-08 23:01:42 +02002794/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002795 * Destroy the mux and the associated connection, if it is no longer used
2796 */
2797static void h2_destroy(struct connection *conn)
2798{
2799 struct h2c *h2c = conn->mux_ctx;
2800
2801 if (eb_is_empty(&h2c->streams_by_id))
2802 h2_release(h2c->conn);
2803}
2804
2805/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002806 * Detach the stream from the connection and possibly release the connection.
2807 */
2808static void h2_detach(struct conn_stream *cs)
2809{
Willy Tarreau60935142017-10-16 18:11:19 +02002810 struct h2s *h2s = cs->ctx;
2811 struct h2c *h2c;
2812
2813 cs->ctx = NULL;
2814 if (!h2s)
2815 return;
2816
2817 h2c = h2s->h2c;
2818 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002819 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002820 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2821 !h2_has_too_many_cs(h2c)) {
2822 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002823 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002824 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002825 }
Willy Tarreau60935142017-10-16 18:11:19 +02002826
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002827 /* this stream may be blocked waiting for some data to leave (possibly
2828 * an ES or RST frame), so orphan it in this case.
2829 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002830 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002831 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002832 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002833 return;
2834
Willy Tarreau45f752e2017-10-30 15:44:59 +01002835 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2836 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2837 /* unblock the connection if it was blocked on this
2838 * stream.
2839 */
2840 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2841 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002842 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002843 }
2844
Willy Tarreau71049cc2018-03-28 13:56:39 +02002845 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002846
Willy Tarreaue323f342018-03-28 13:51:45 +02002847 /* We don't want to close right now unless we're removing the
2848 * last stream, and either the connection is in error, or it
2849 * reached the ID already specified in a GOAWAY frame received
2850 * or sent (as seen by last_sid >= 0).
2851 */
2852 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2853 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002854 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002855 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002856 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002857 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002858 (conn_xprt_read0_pending(h2c->conn) ||
2859 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2860 /* no more stream will come, kill it now */
2861 h2_release(h2c->conn);
2862 }
2863 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002864 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002865 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2866 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002867 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002868 else
2869 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002870 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002871}
2872
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002873static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002874{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002875 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002876 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002877
Willy Tarreau721c9742017-11-07 11:05:42 +01002878 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002879 return;
2880
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002881 /* if no outgoing data was seen on this stream, it means it was
2882 * closed with a "tcp-request content" rule that is normally
2883 * used to kill the connection ASAP (eg: limit abuse). In this
2884 * case we send a goaway to close the connection.
2885 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002886 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002887 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002888 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002889
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002890 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2891 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002892 h2c_send_goaway_error(h2c, h2s) <= 0)
2893 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002894
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002895 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2896 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002897 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002898
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002899 return;
2900add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002901 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002902 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002903 if (h2s->flags & H2_SF_BLK_MFCTL) {
2904 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2905 h2s->send_wait = sw;
2906 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2907 h2s->send_wait = sw;
2908 LIST_ADDQ(&h2c->send_list, &h2s->list);
2909 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002910 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002911 /* Let the handler know we want shutr */
2912 sw->handle = (void *)((long)sw->handle | 1);
2913
Willy Tarreau62f52692017-10-08 23:01:42 +02002914}
2915
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002916static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002917{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002918 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002919 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002920
Willy Tarreau721c9742017-11-07 11:05:42 +01002921 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002922 return;
2923
Willy Tarreau67434202017-11-06 20:20:51 +01002924 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002925 /* we can cleanly close using an empty data frame only after headers */
2926
2927 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2928 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002929 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002930
2931 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002932 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002933 else
2934 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002935 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002936 /* if no outgoing data was seen on this stream, it means it was
2937 * closed with a "tcp-request content" rule that is normally
2938 * used to kill the connection ASAP (eg: limit abuse). In this
2939 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002940 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002941 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002942 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002943 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002944
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002945 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2946 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002947 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002948 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002949
Willy Tarreau00dd0782018-03-01 16:31:34 +01002950 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002951 }
2952
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002953 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2954 tasklet_wakeup(h2c->wait_event.task);
2955 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002956
2957 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002958 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002959 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002960 if (h2s->flags & H2_SF_BLK_MFCTL) {
2961 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2962 h2s->send_wait = sw;
2963 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2964 h2s->send_wait = sw;
2965 LIST_ADDQ(&h2c->send_list, &h2s->list);
2966 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002967 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002968 /* let the handler know we want to shutw */
2969 sw->handle = (void *)((long)(sw->handle) | 2);
2970
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002971}
2972
2973static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2974{
2975 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002976 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002977
2978 if (reason & 1)
2979 h2_do_shutr(h2s);
2980 if (reason & 2)
2981 h2_do_shutw(h2s);
2982
2983 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002984}
2985
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002986static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2987{
2988 struct h2s *h2s = cs->ctx;
2989
2990 if (!mode)
2991 return;
2992
2993 h2_do_shutr(h2s);
2994}
2995
2996static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2997{
2998 struct h2s *h2s = cs->ctx;
2999
3000 h2_do_shutw(h2s);
3001}
3002
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003003/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003004 * HTX request or response depending on the connection's side. Returns the
3005 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3006 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003007 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003008static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003009{
3010 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003011 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003012 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003013 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003014 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003015 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003016 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003017 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003018 int flen = h2c->dfl;
3019 int outlen = 0;
3020 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003021 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003022
3023 if (!h2c->dfl) {
3024 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003025 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003026 return 0;
3027 }
3028
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003029 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003030 return 0; // incomplete input frame
3031
Willy Tarreau13278b42017-10-13 19:23:14 +02003032 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003033 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003034 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003035 copy = alloc_trash_chunk();
3036 if (!copy) {
3037 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3038 goto fail;
3039 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003040 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3041 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3042 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003043 }
3044
3045 /* The padlen is the first byte before data, and the padding appears
3046 * after data. padlen+data+padding are included in flen.
3047 */
3048 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003049 h2c->dpl = *hdrs;
3050 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003051 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3052 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003053 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003054 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003055 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003056 hdrs += 1; // skip Pad Length
3057 }
3058
3059 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3060 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003061 if (read_n32(hdrs) == h2s->id) {
3062 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3063 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003064 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003065 }
3066
Willy Tarreau13278b42017-10-13 19:23:14 +02003067 hdrs += 5; // stream dep = 4, weight = 1
3068 flen -= 5;
3069 }
3070
3071 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3072 * don't support this for now and can't even decompress so we have to
3073 * break the connection.
3074 */
3075 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3076 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003077 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003078 }
3079
Olivier Houchard638b7992018-08-16 15:41:52 +02003080 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003081 if (!csbuf) {
3082 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003083 goto fail;
3084 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003085
Willy Tarreau937f7602018-02-26 15:22:17 +01003086 /* we can't retry a failed decompression operation so we must be very
3087 * careful not to take any risks. In practice the output buffer is
3088 * always empty except maybe for trailers, in which case we simply have
3089 * to wait for the upper layer to finish consuming what is available.
3090 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003091
3092 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3093 htx = htx_from_buf(&h2s->rxbuf);
3094 if (!htx_is_empty(htx))
3095 goto fail;
3096 } else {
3097 if (b_data(csbuf))
3098 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003099
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003100 csbuf->head = 0;
3101 try = b_size(csbuf);
3102 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003103
3104 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3105 sizeof(list)/sizeof(list[0]), tmp);
3106 if (outlen < 0) {
3107 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3108 goto fail;
3109 }
3110
3111 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003112 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003113
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003114 if (htx) {
3115 /* HTX mode */
3116 if (h2c->flags & H2_CF_IS_BACK)
3117 outlen = h2_make_htx_response(list, htx, &msgf);
3118 else
3119 outlen = h2_make_htx_request(list, htx, &msgf);
3120 } else {
3121 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003122 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003123 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003124
3125 if (outlen < 0) {
3126 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3127 goto fail;
3128 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003129
Willy Tarreau174b06a2018-04-25 18:13:58 +02003130 if (msgf & H2_MSGF_BODY) {
3131 /* a payload is present */
3132 if (msgf & H2_MSGF_BODY_CL)
3133 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003134 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003135 h2s->flags |= H2_SF_DATA_CHNK;
3136 }
3137
Willy Tarreau13278b42017-10-13 19:23:14 +02003138 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003139 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003140 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003141 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003142
Willy Tarreau39d68502018-03-02 12:26:37 +01003143 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003144 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003145 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003146 if (htx)
3147 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003148 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003149
Willy Tarreau68dd9852017-07-03 14:44:26 +02003150 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003151 if (htx)
3152 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003153 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003154 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003155 fail:
3156 outlen = 0;
3157 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003158}
3159
Willy Tarreau454f9052017-10-26 19:40:35 +02003160/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3161 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3162 * in use, a new chunk is emitted for each frame. This is supposed to fit
3163 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3164 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3165 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003166 * parser state is automatically updated. Returns > 0 if it could completely
3167 * send the current frame, 0 if it couldn't complete, in which case
3168 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3169 * DATA frame can return 0 as a valid result). Stream errors are reported in
3170 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3171 * have checked the frame header and ensured that the frame was complete or the
3172 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003173 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003174static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003175{
3176 struct h2c *h2c = h2s->h2c;
3177 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003178 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003179 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003180 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003181 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003182
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003183 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003184
3185 /* The padlen is the first byte before data, and the padding appears
3186 * after data. padlen+data+padding are included in flen.
3187 */
Willy Tarreau79127812017-12-03 21:06:59 +01003188 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003189 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003190 return 0;
3191
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003192 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003193 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003194 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3195 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003196 return 0;
3197 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003198
3199 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003200 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003201 h2c->dfl--;
3202 h2c->rcvd_c++; h2c->rcvd_s++;
3203 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003204 }
3205
Olivier Houchard638b7992018-08-16 15:41:52 +02003206 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003207 if (!csbuf) {
3208 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003209 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003210 }
3211
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003212try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003213 flen = h2c->dfl - h2c->dpl;
3214 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003215 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003216
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003217 if (flen > b_data(&h2c->dbuf)) {
3218 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003219 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003220 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003221 }
3222
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003223 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3224 htx = htx_from_buf(csbuf);
3225 block1 = htx_free_data_space(htx);
3226 if (!block1) {
3227 h2c->flags |= H2_CF_DEM_SFULL;
3228 goto fail;
3229 }
3230 if (flen > block1)
3231 flen = block1;
3232
3233 /* here, flen is the max we can copy into the output buffer */
3234 block1 = b_contig_data(&h2c->dbuf, 0);
3235 if (flen > block1)
3236 flen = block1;
3237
3238 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3239 h2c->flags |= H2_CF_DEM_SFULL;
3240 goto fail;
3241 }
3242
3243 b_del(&h2c->dbuf, flen);
3244 h2c->dfl -= flen;
3245 h2c->rcvd_c += flen;
3246 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3247 goto try_again;
3248 }
3249 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003250 /* it doesn't fit and the buffer is fragmented,
3251 * so let's defragment it and try again.
3252 */
3253 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003254 }
3255
Willy Tarreaueba10f22018-04-25 20:44:22 +02003256 /* chunked-encoding requires more room */
3257 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003258 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003259 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3260 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3261 (chklen < 1048576) ? 4 : 8;
3262 chklen += 4; // CRLF, CRLF
3263 }
3264
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003265 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003266 if (flen + chklen > b_room(csbuf)) {
3267 if (chklen >= b_room(csbuf)) {
3268 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003269 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003270 }
3271 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003272 }
3273
3274 if (h2s->flags & H2_SF_DATA_CHNK) {
3275 /* emit the chunk size */
3276 unsigned int chksz = flen;
3277 char str[10];
3278 char *beg;
3279
3280 beg = str + sizeof(str);
3281 *--beg = '\n';
3282 *--beg = '\r';
3283 do {
3284 *--beg = hextab[chksz & 0xF];
3285 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003286 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003287 }
3288
Willy Tarreau454f9052017-10-26 19:40:35 +02003289 /* Block1 is the length of the first block before the buffer wraps,
3290 * block2 is the optional second block to reach the end of the frame.
3291 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003292 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003293 if (block1 > flen)
3294 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003295 block2 = flen - block1;
3296
3297 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003298 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003299
3300 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003301 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003302
Willy Tarreaueba10f22018-04-25 20:44:22 +02003303 if (h2s->flags & H2_SF_DATA_CHNK) {
3304 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003305 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003306 }
3307
Willy Tarreau454f9052017-10-26 19:40:35 +02003308 /* now mark the input data as consumed (will be deleted from the buffer
3309 * by the caller when seeing FRAME_A after sending the window update).
3310 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003311 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003312 h2c->dfl -= flen;
3313 h2c->rcvd_c += flen;
3314 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3315
3316 if (h2c->dfl > h2c->dpl) {
3317 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003318 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003319 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003320 }
3321
Willy Tarreau4a28da12018-01-04 14:41:00 +01003322 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003323 /* here we're done with the frame, all the payload (except padding) was
3324 * transferred.
3325 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003326
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003327 if (h2c->dff & H2_F_DATA_END_STREAM) {
3328 if (htx) {
3329 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3330 h2c->flags |= H2_CF_DEM_SFULL;
3331 goto fail;
3332 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003333 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003334 else if (h2s->flags & H2_SF_DATA_CHNK) {
3335 /* emit the trailing 0 CRLF CRLF */
3336 if (b_room(csbuf) < 5) {
3337 h2c->flags |= H2_CF_DEM_SFULL;
3338 goto fail;
3339 }
3340 chklen += 5;
3341 b_putblk(csbuf, "0\r\n\r\n", 5);
3342 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003343 }
3344
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003345 h2c->rcvd_c += h2c->dpl;
3346 h2c->rcvd_s += h2c->dpl;
3347 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003348 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3349
Willy Tarreau39d68502018-03-02 12:26:37 +01003350 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003351 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003352 h2s->cs->flags |= CS_FL_REOS;
3353 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003354 if (htx)
3355 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003356 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003357 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003358 if (htx)
3359 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003360 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003361}
3362
Willy Tarreau5dd17352018-06-14 13:33:30 +02003363/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3364 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3365 * number of bytes sent. The caller must check the stream's status to detect
3366 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003367 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003368static 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 +02003369{
3370 struct http_hdr list[MAX_HTTP_HDR];
3371 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003372 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003373 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003374 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003375 int es_now = 0;
3376 int ret = 0;
3377 int hdr;
3378
3379 if (h2c_mux_busy(h2c, h2s)) {
3380 h2s->flags |= H2_SF_BLK_MBUSY;
3381 return 0;
3382 }
3383
Willy Tarreau44e973f2018-03-01 17:49:30 +01003384 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003385 h2c->flags |= H2_CF_MUX_MALLOC;
3386 h2s->flags |= H2_SF_BLK_MROOM;
3387 return 0;
3388 }
3389
3390 /* First, try to parse the H1 response and index it into <list>.
3391 * NOTE! Since it comes from haproxy, we *know* that a response header
3392 * block does not wrap and we can safely read it this way without
3393 * having to realign the buffer.
3394 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003395 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003396 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003397 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003398 /* incomplete or invalid response, this is abnormal coming from
3399 * haproxy and may only result in a bad errorfile or bad Lua code
3400 * so that won't be fixed, raise an error now.
3401 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003402 * FIXME: we should instead add the ability to only return a
3403 * 502 bad gateway. But in theory this is not supposed to
3404 * happen.
3405 */
3406 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3407 ret = 0;
3408 goto end;
3409 }
3410
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003411 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003412
3413 /* certain statuses have no body or an empty one, regardless of
3414 * what the headers say.
3415 */
3416 if (sl.st.status >= 100 && sl.st.status < 200) {
3417 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3418 h1m->curr_len = h1m->body_len = 0;
3419 }
3420 else if (sl.st.status == 204 || sl.st.status == 304) {
3421 /* no contents, claim c-len is present and set to zero */
3422 h1m->flags &= ~H1_MF_CHNK;
3423 h1m->flags |= H1_MF_CLEN;
3424 h1m->curr_len = h1m->body_len = 0;
3425 }
3426
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003427 chunk_reset(&outbuf);
3428
3429 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003430 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003431 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003432 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003433
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003434 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003435 break;
3436 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003437 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003438 }
3439
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003440 if (outbuf.size < 9)
3441 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003442
3443 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003444 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3445 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3446 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003447
3448 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003449 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003450 /* this is an unparsable response */
3451 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3452 ret = 0;
3453 goto end;
3454 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003455
3456 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003457 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003458 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003459 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003460 }
3461
3462 /* encode all headers, stop at empty name */
3463 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003464 /* these ones do not exist in H2 and must be dropped. */
3465 if (isteq(list[hdr].n, ist("connection")) ||
3466 isteq(list[hdr].n, ist("proxy-connection")) ||
3467 isteq(list[hdr].n, ist("keep-alive")) ||
3468 isteq(list[hdr].n, ist("upgrade")) ||
3469 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003470 continue;
3471
3472 if (isteq(list[hdr].n, ist("")))
3473 break; // end
3474
3475 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3476 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003477 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003478 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003479 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003480 }
3481 }
3482
3483 /* we may need to add END_STREAM */
3484 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3485 es_now = 1;
3486
3487 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003488 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003489
3490 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003491 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003492
3493 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003494 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003495
3496 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003497 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003498 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003499
3500 /* for now we don't implemented CONTINUATION, so we wait for a
3501 * body or directly end in TRL2.
3502 */
3503 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003504 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003505 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003506
Willy Tarreau801250e2018-09-11 11:45:04 +02003507 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003508 h2s->flags |= H2_SF_ES_SENT;
3509 if (h2s->st == H2_SS_OPEN)
3510 h2s->st = H2_SS_HLOC;
3511 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003512 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003513 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003514 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003515 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003516 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003517 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003518 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003519 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003520 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003521
3522 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003523
3524 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003525 //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 +02003526 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003527 full:
3528 h1m_init_res(h1m);
3529 h1m->err_pos = -1; // don't care about errors on the response path
3530 h2c->flags |= H2_CF_MUX_MFULL;
3531 h2s->flags |= H2_SF_BLK_MROOM;
3532 ret = 0;
3533 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003534}
3535
Willy Tarreau5dd17352018-06-14 13:33:30 +02003536/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3537 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3538 * the number of bytes sent. The caller must check the stream's status to
3539 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003540 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003541static 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 +02003542{
3543 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003544 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003545 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003546 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003547 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003548 int es_now = 0;
3549 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003550 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003551 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003552
3553 if (h2c_mux_busy(h2c, h2s)) {
3554 h2s->flags |= H2_SF_BLK_MBUSY;
3555 goto end;
3556 }
3557
Willy Tarreau44e973f2018-03-01 17:49:30 +01003558 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003559 h2c->flags |= H2_CF_MUX_MALLOC;
3560 h2s->flags |= H2_SF_BLK_MROOM;
3561 goto end;
3562 }
3563
3564 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003565 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003566 goto end;
3567
3568 chunk_reset(&outbuf);
3569
3570 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003571 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003572 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003573 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003574
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003575 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003576 break;
3577 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003578 /* If there are pending data in the output buffer, and we have
3579 * less than 1/4 of the mbuf's size and everything fits, we'll
3580 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3581 * is full and wait, to save some slow realign calls.
3582 */
3583 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3584 h2c->flags |= H2_CF_MUX_MFULL;
3585 h2s->flags |= H2_SF_BLK_MROOM;
3586 goto end;
3587 }
3588
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003589 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003590 }
3591
3592 if (outbuf.size < 9) {
3593 h2c->flags |= H2_CF_MUX_MFULL;
3594 h2s->flags |= H2_SF_BLK_MROOM;
3595 goto end;
3596 }
3597
3598 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003599 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3600 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3601 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003602
3603 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3604 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003605 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003606 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003607 break;
3608 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003609 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003610 if ((long long)size > h1m->curr_len)
3611 size = h1m->curr_len;
3612 break;
3613 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003614 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003615 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003616 if (!ret)
3617 goto end;
3618
3619 if (ret < 0) {
3620 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003621 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003622 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3623 goto end;
3624 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003625 max -= ret;
3626 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003627 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003628 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003629 }
3630
Willy Tarreau801250e2018-09-11 11:45:04 +02003631 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003632 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003633 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003634 if (!ret)
3635 goto end;
3636
3637 if (ret < 0) {
3638 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003639 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003640 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3641 goto end;
3642 }
3643
3644 size = chunk;
3645 h1m->curr_len = chunk;
3646 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003647 max -= ret;
3648 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003649 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003650 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003651 if (!size)
3652 goto send_empty;
3653 }
3654
3655 /* in MSG_DATA state, continue below */
3656 size = h1m->curr_len;
3657 break;
3658 }
3659
3660 /* we have in <size> the exact number of bytes we need to copy from
3661 * the H1 buffer. We need to check this against the connection's and
3662 * the stream's send windows, and to ensure that this fits in the max
3663 * frame size and in the buffer's available space minus 9 bytes (for
3664 * the frame header). The connection's flow control is applied last so
3665 * that we can use a separate list of streams which are immediately
3666 * unblocked on window opening. Note: we don't implement padding.
3667 */
3668
Willy Tarreau5dd17352018-06-14 13:33:30 +02003669 if (size > max)
3670 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003671
3672 if (size > h2s->mws)
3673 size = h2s->mws;
3674
3675 if (size <= 0) {
3676 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003677 if (h2s->send_wait) {
3678 LIST_DEL(&h2s->list);
3679 LIST_INIT(&h2s->list);
3680 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003681 goto end;
3682 }
3683
3684 if (h2c->mfs && size > h2c->mfs)
3685 size = h2c->mfs;
3686
3687 if (size + 9 > outbuf.size) {
3688 /* we have an opportunity for enlarging the too small
3689 * available space, let's try.
3690 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003691 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003692 goto realign_again;
3693 size = outbuf.size - 9;
3694 }
3695
3696 if (size <= 0) {
3697 h2c->flags |= H2_CF_MUX_MFULL;
3698 h2s->flags |= H2_SF_BLK_MROOM;
3699 goto end;
3700 }
3701
3702 if (size > h2c->mws)
3703 size = h2c->mws;
3704
3705 if (size <= 0) {
3706 h2s->flags |= H2_SF_BLK_MFCTL;
3707 goto end;
3708 }
3709
3710 /* copy whatever we can */
3711 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003712 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003713 if (ret == 1)
3714 len2 = 0;
3715
3716 if (!ret || len1 + len2 < size) {
3717 /* FIXME: must normally never happen */
3718 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3719 goto end;
3720 }
3721
3722 /* limit len1/len2 to size */
3723 if (len1 + len2 > size) {
3724 int sub = len1 + len2 - size;
3725
3726 if (len2 > sub)
3727 len2 -= sub;
3728 else {
3729 sub -= len2;
3730 len2 = 0;
3731 len1 -= sub;
3732 }
3733 }
3734
3735 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003736 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003737 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003738 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003739
3740 send_empty:
3741 /* we may need to add END_STREAM */
3742 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3743 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003744 *
3745 * FIXME: what we do here is not correct because we send end_stream
3746 * before knowing if we'll have to send a HEADERS frame for the
3747 * trailers. More importantly we're not consuming the trailing CRLF
3748 * after the end of trailers, so it will be left to the caller to
3749 * eat it. The right way to do it would be to measure trailers here
3750 * and to send ES only if there are no trailers.
3751 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003752 */
3753 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003754 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003755 es_now = 1;
3756
3757 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003758 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003759
3760 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003761 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003762
3763 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003764 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003765
3766 /* consume incoming H1 response */
3767 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003768 max -= size;
3769 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003770 total += size;
3771 h1m->curr_len -= size;
3772 h2s->mws -= size;
3773 h2c->mws -= size;
3774
3775 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003776 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003777 goto new_frame;
3778 }
3779 }
3780
3781 if (es_now) {
3782 if (h2s->st == H2_SS_OPEN)
3783 h2s->st = H2_SS_HLOC;
3784 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003785 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003786
Willy Tarreau35a62702018-02-27 15:37:25 +01003787 if (!(h1m->flags & H1_MF_CHNK)) {
3788 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003789 total += max;
3790 ofs += max;
3791 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003792
Willy Tarreau801250e2018-09-11 11:45:04 +02003793 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003794 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003795
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003796 h2s->flags |= H2_SF_ES_SENT;
3797 }
3798
3799 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003800 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 +02003801 return total;
3802}
3803
Willy Tarreau115e83b2018-12-01 19:17:53 +01003804/* Try to send a HEADERS frame matching HTX response present in HTX message
3805 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3806 * must check the stream's status to detect any error which might have happened
3807 * subsequently to a successful send. The htx blocks are automatically removed
3808 * from the message. The htx message is assumed to be valid since produced from
3809 * the internal code, hence it contains a start line, an optional series of
3810 * header blocks and an end of header, otherwise an invalid frame could be
3811 * emitted and the resulting htx message could be left in an inconsistent state.
3812 */
3813static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3814{
3815 struct http_hdr list[MAX_HTTP_HDR];
3816 struct h2c *h2c = h2s->h2c;
3817 struct htx_blk *blk;
3818 struct htx_blk *blk_end;
3819 struct buffer outbuf;
3820 struct htx_sl *sl;
3821 enum htx_blk_type type;
3822 int es_now = 0;
3823 int ret = 0;
3824 int hdr;
3825 int idx;
3826
3827 if (h2c_mux_busy(h2c, h2s)) {
3828 h2s->flags |= H2_SF_BLK_MBUSY;
3829 return 0;
3830 }
3831
3832 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3833 h2c->flags |= H2_CF_MUX_MALLOC;
3834 h2s->flags |= H2_SF_BLK_MROOM;
3835 return 0;
3836 }
3837
3838 /* determine the first block which must not be deleted, blk_end may
3839 * be NULL if all blocks have to be deleted.
3840 */
3841 idx = htx_get_head(htx);
3842 blk_end = NULL;
3843 while (idx != -1) {
3844 type = htx_get_blk_type(htx_get_blk(htx, idx));
3845 idx = htx_get_next(htx, idx);
3846 if (type == HTX_BLK_EOH) {
3847 if (idx != -1)
3848 blk_end = htx_get_blk(htx, idx);
3849 break;
3850 }
3851 }
3852
3853 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003854 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003855 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003856 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003857 if (h2s->status < 100 || h2s->status > 999)
3858 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003859
3860 /* and the rest of the headers, that we dump starting at header 0 */
3861 hdr = 0;
3862
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003863 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003864 while ((idx = htx_get_next(htx, idx)) != -1) {
3865 blk = htx_get_blk(htx, idx);
3866 type = htx_get_blk_type(blk);
3867
3868 if (type == HTX_BLK_UNUSED)
3869 continue;
3870
3871 if (type != HTX_BLK_HDR)
3872 break;
3873
3874 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3875 goto fail;
3876
3877 list[hdr].n = htx_get_blk_name(htx, blk);
3878 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003879 hdr++;
3880 }
3881
3882 /* marker for end of headers */
3883 list[hdr].n = ist("");
3884
3885 if (h2s->status == 204 || h2s->status == 304) {
3886 /* no contents, claim c-len is present and set to zero */
3887 es_now = 1;
3888 }
3889
3890 chunk_reset(&outbuf);
3891
3892 while (1) {
3893 outbuf.area = b_tail(&h2c->mbuf);
3894 outbuf.size = b_contig_space(&h2c->mbuf);
3895 outbuf.data = 0;
3896
3897 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3898 break;
3899 realign_again:
3900 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3901 }
3902
3903 if (outbuf.size < 9)
3904 goto full;
3905
3906 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3907 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3908 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3909 outbuf.data = 9;
3910
3911 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003912 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003913 if (b_space_wraps(&h2c->mbuf))
3914 goto realign_again;
3915 goto full;
3916 }
3917
3918 /* encode all headers, stop at empty name */
3919 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3920 /* these ones do not exist in H2 and must be dropped. */
3921 if (isteq(list[hdr].n, ist("connection")) ||
3922 isteq(list[hdr].n, ist("proxy-connection")) ||
3923 isteq(list[hdr].n, ist("keep-alive")) ||
3924 isteq(list[hdr].n, ist("upgrade")) ||
3925 isteq(list[hdr].n, ist("transfer-encoding")))
3926 continue;
3927
3928 if (isteq(list[hdr].n, ist("")))
3929 break; // end
3930
3931 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3932 /* output full */
3933 if (b_space_wraps(&h2c->mbuf))
3934 goto realign_again;
3935 goto full;
3936 }
3937 }
3938
3939 /* we may need to add END_STREAM.
3940 * FIXME: we should also set it when we know for sure that the
3941 * content-length is zero as well as on 204/304
3942 */
3943 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3944 es_now = 1;
3945
3946 if (h2s->cs->flags & CS_FL_SHW)
3947 es_now = 1;
3948
3949 /* update the frame's size */
3950 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3951
3952 if (es_now)
3953 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3954
3955 /* commit the H2 response */
3956 b_add(&h2c->mbuf, outbuf.data);
3957 h2s->flags |= H2_SF_HEADERS_SENT;
3958
3959 /* for now we don't implemented CONTINUATION, so we wait for a
3960 * body or directly end in TRL2.
3961 */
3962 if (es_now) {
3963 h2s->flags |= H2_SF_ES_SENT;
3964 if (h2s->st == H2_SS_OPEN)
3965 h2s->st = H2_SS_HLOC;
3966 else
3967 h2s_close(h2s);
3968 }
3969
3970 /* OK we could properly deliver the response */
3971
3972 /* remove all header blocks including the EOH and compute the
3973 * corresponding size.
3974 *
3975 * FIXME: We should remove everything when es_now is set.
3976 */
3977 ret = 0;
3978 idx = htx_get_head(htx);
3979 blk = htx_get_blk(htx, idx);
3980 while (blk != blk_end) {
3981 ret += htx_get_blksz(blk);
3982 blk = htx_remove_blk(htx, blk);
3983 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003984
3985 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3986 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003987 end:
3988 return ret;
3989 full:
3990 h2c->flags |= H2_CF_MUX_MFULL;
3991 h2s->flags |= H2_SF_BLK_MROOM;
3992 ret = 0;
3993 goto end;
3994 fail:
3995 /* unparsable HTX messages, too large ones to be produced in the local
3996 * list etc go here (unrecoverable errors).
3997 */
3998 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3999 ret = 0;
4000 goto end;
4001}
4002
Willy Tarreau80739692018-10-05 11:35:57 +02004003/* Try to send a HEADERS frame matching HTX request present in HTX message
4004 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4005 * must check the stream's status to detect any error which might have happened
4006 * subsequently to a successful send. The htx blocks are automatically removed
4007 * from the message. The htx message is assumed to be valid since produced from
4008 * the internal code, hence it contains a start line, an optional series of
4009 * header blocks and an end of header, otherwise an invalid frame could be
4010 * emitted and the resulting htx message could be left in an inconsistent state.
4011 */
4012static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4013{
4014 struct http_hdr list[MAX_HTTP_HDR];
4015 struct h2c *h2c = h2s->h2c;
4016 struct htx_blk *blk;
4017 struct htx_blk *blk_end;
4018 struct buffer outbuf;
4019 struct htx_sl *sl;
4020 struct ist meth, path;
4021 enum htx_blk_type type;
4022 int es_now = 0;
4023 int ret = 0;
4024 int hdr;
4025 int idx;
4026
4027 if (h2c_mux_busy(h2c, h2s)) {
4028 h2s->flags |= H2_SF_BLK_MBUSY;
4029 return 0;
4030 }
4031
4032 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4033 h2c->flags |= H2_CF_MUX_MALLOC;
4034 h2s->flags |= H2_SF_BLK_MROOM;
4035 return 0;
4036 }
4037
4038 /* determine the first block which must not be deleted, blk_end may
4039 * be NULL if all blocks have to be deleted.
4040 */
4041 idx = htx_get_head(htx);
4042 blk_end = NULL;
4043 while (idx != -1) {
4044 type = htx_get_blk_type(htx_get_blk(htx, idx));
4045 idx = htx_get_next(htx, idx);
4046 if (type == HTX_BLK_EOH) {
4047 if (idx != -1)
4048 blk_end = htx_get_blk(htx, idx);
4049 break;
4050 }
4051 }
4052
4053 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004054 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004055 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004056 meth = htx_sl_req_meth(sl);
4057 path = htx_sl_req_uri(sl);
4058
4059 /* and the rest of the headers, that we dump starting at header 0 */
4060 hdr = 0;
4061
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004062 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004063 while ((idx = htx_get_next(htx, idx)) != -1) {
4064 blk = htx_get_blk(htx, idx);
4065 type = htx_get_blk_type(blk);
4066
4067 if (type == HTX_BLK_UNUSED)
4068 continue;
4069
4070 if (type != HTX_BLK_HDR)
4071 break;
4072
4073 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4074 goto fail;
4075
4076 list[hdr].n = htx_get_blk_name(htx, blk);
4077 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004078 hdr++;
4079 }
4080
4081 /* marker for end of headers */
4082 list[hdr].n = ist("");
4083
4084 chunk_reset(&outbuf);
4085
4086 while (1) {
4087 outbuf.area = b_tail(&h2c->mbuf);
4088 outbuf.size = b_contig_space(&h2c->mbuf);
4089 outbuf.data = 0;
4090
4091 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4092 break;
4093 realign_again:
4094 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4095 }
4096
4097 if (outbuf.size < 9)
4098 goto full;
4099
4100 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4101 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4102 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4103 outbuf.data = 9;
4104
4105 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004106 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004107 if (b_space_wraps(&h2c->mbuf))
4108 goto realign_again;
4109 goto full;
4110 }
4111
4112 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004113 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4114 /* output full */
4115 if (b_space_wraps(&h2c->mbuf))
4116 goto realign_again;
4117 goto full;
4118 }
Willy Tarreau80739692018-10-05 11:35:57 +02004119
4120 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004121 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004122 /* output full */
4123 if (b_space_wraps(&h2c->mbuf))
4124 goto realign_again;
4125 goto full;
4126 }
4127
4128 /* encode all headers, stop at empty name */
4129 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4130 /* these ones do not exist in H2 and must be dropped. */
4131 if (isteq(list[hdr].n, ist("connection")) ||
4132 isteq(list[hdr].n, ist("proxy-connection")) ||
4133 isteq(list[hdr].n, ist("keep-alive")) ||
4134 isteq(list[hdr].n, ist("upgrade")) ||
4135 isteq(list[hdr].n, ist("transfer-encoding")))
4136 continue;
4137
4138 if (isteq(list[hdr].n, ist("")))
4139 break; // end
4140
4141 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4142 /* output full */
4143 if (b_space_wraps(&h2c->mbuf))
4144 goto realign_again;
4145 goto full;
4146 }
4147 }
4148
4149 /* we may need to add END_STREAM if we have no body :
4150 * - request already closed, or :
4151 * - no transfer-encoding, and :
4152 * - no content-length or content-length:0
4153 * Fixme: this doesn't take into account CONNECT requests.
4154 */
4155 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4156 es_now = 1;
4157
4158 if (sl->flags & HTX_SL_F_BODYLESS)
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 h2s->st = H2_SS_OPEN;
4174
4175 /* for now we don't implemented CONTINUATION, so we wait for a
4176 * body or directly end in TRL2.
4177 */
4178 if (es_now) {
4179 // trim any possibly pending data (eg: inconsistent content-length)
4180 h2s->flags |= H2_SF_ES_SENT;
4181 h2s->st = H2_SS_HLOC;
4182 }
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 }
4196
4197 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4198 htx_remove_blk(htx, blk_end);
4199
4200 end:
4201 return ret;
4202 full:
4203 h2c->flags |= H2_CF_MUX_MFULL;
4204 h2s->flags |= H2_SF_BLK_MROOM;
4205 ret = 0;
4206 goto end;
4207 fail:
4208 /* unparsable HTX messages, too large ones to be produced in the local
4209 * list etc go here (unrecoverable errors).
4210 */
4211 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4212 ret = 0;
4213 goto end;
4214}
4215
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004216/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004217 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4218 * caller must check the stream's status to detect any error which might have
4219 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004220 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4221 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004222static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004223{
4224 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004225 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004226 struct buffer outbuf;
4227 size_t total = 0;
4228 int es_now = 0;
4229 int bsize; /* htx block size */
4230 int fsize; /* h2 frame size */
4231 struct htx_blk *blk;
4232 enum htx_blk_type type;
4233 int idx;
4234
4235 if (h2c_mux_busy(h2c, h2s)) {
4236 h2s->flags |= H2_SF_BLK_MBUSY;
4237 goto end;
4238 }
4239
4240 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4241 h2c->flags |= H2_CF_MUX_MALLOC;
4242 h2s->flags |= H2_SF_BLK_MROOM;
4243 goto end;
4244 }
4245
Willy Tarreau98de12a2018-12-12 07:03:00 +01004246 htx = htx_from_buf(buf);
4247
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004248 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4249 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4250 * the caller to handle.
4251 */
4252
4253 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004254 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004255 goto end;
4256
4257 idx = htx_get_head(htx);
4258 blk = htx_get_blk(htx, idx);
4259 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4260 bsize = htx_get_blksz(blk);
4261 fsize = bsize;
4262
4263 if (type == HTX_BLK_EOD) {
4264 /* if we have an EOD, we're dealing with chunked data. We may
4265 * have a set of trailers after us that the caller will want to
4266 * deal with. Let's simply remove the EOD and return.
4267 */
4268 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004269 total++; // EOD counts as one byte
4270 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004271 goto end;
4272 }
4273
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004274 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4275 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004276
4277 /* Perform some optimizations to reduce the number of buffer copies.
4278 * First, if the mux's buffer is empty and the htx area contains
4279 * exactly one data block of the same size as the requested count, and
4280 * this count fits within the frame size, the stream's window size, and
4281 * the connection's window size, then it's possible to simply swap the
4282 * caller's buffer with the mux's output buffer and adjust offsets and
4283 * length to match the entire DATA HTX block in the middle. In this
4284 * case we perform a true zero-copy operation from end-to-end. This is
4285 * the situation that happens all the time with large files. Second, if
4286 * this is not possible, but the mux's output buffer is empty, we still
4287 * have an opportunity to avoid the copy to the intermediary buffer, by
4288 * making the intermediary buffer's area point to the output buffer's
4289 * area. In this case we want to skip the HTX header to make sure that
4290 * copies remain aligned and that this operation remains possible all
4291 * the time. This goes for headers, data blocks and any data extracted
4292 * from the HTX blocks.
4293 */
4294 if (unlikely(fsize == count &&
4295 htx->used == 1 && type == HTX_BLK_DATA &&
4296 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4297 void *old_area = h2c->mbuf.area;
4298
4299 if (b_data(&h2c->mbuf)) {
4300 /* too bad there are data left there. If we have less
4301 * than 1/4 of the mbuf's size and everything fits,
4302 * we'll perform a copy anyway. Otherwise we'll pretend
4303 * the mbuf is full and wait.
4304 */
4305 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4306 goto copy;
4307 h2c->flags |= H2_CF_MUX_MFULL;
4308 h2s->flags |= H2_SF_BLK_MROOM;
4309 goto end;
4310 }
4311
4312 /* map an H2 frame to the HTX block so that we can put the
4313 * frame header there.
4314 */
4315 h2c->mbuf.area = buf->area;
4316 h2c->mbuf.head = sizeof(struct htx) - 9;
4317 h2c->mbuf.data = fsize + 9;
4318 outbuf.area = b_head(&h2c->mbuf);
4319
4320 /* prepend an H2 DATA frame header just before the DATA block */
4321 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4322 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4323 h2_set_frame_size(outbuf.area, fsize);
4324
4325 /* update windows */
4326 h2s->mws -= fsize;
4327 h2c->mws -= fsize;
4328
4329 /* and exchange with our old area */
4330 buf->area = old_area;
4331 buf->data = buf->head = 0;
4332 total += fsize;
4333 goto end;
4334 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004335
Willy Tarreau98de12a2018-12-12 07:03:00 +01004336 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004337 /* for DATA and EOM we'll have to emit a frame, even if empty */
4338
4339 while (1) {
4340 outbuf.area = b_tail(&h2c->mbuf);
4341 outbuf.size = b_contig_space(&h2c->mbuf);
4342 outbuf.data = 0;
4343
4344 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4345 break;
4346 realign_again:
4347 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4348 }
4349
4350 if (outbuf.size < 9) {
4351 h2c->flags |= H2_CF_MUX_MFULL;
4352 h2s->flags |= H2_SF_BLK_MROOM;
4353 goto end;
4354 }
4355
4356 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4357 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4358 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4359 outbuf.data = 9;
4360
4361 /* we have in <fsize> the exact number of bytes we need to copy from
4362 * the HTX buffer. We need to check this against the connection's and
4363 * the stream's send windows, and to ensure that this fits in the max
4364 * frame size and in the buffer's available space minus 9 bytes (for
4365 * the frame header). The connection's flow control is applied last so
4366 * that we can use a separate list of streams which are immediately
4367 * unblocked on window opening. Note: we don't implement padding.
4368 */
4369
4370 /* EOM is presented with bsize==1 but would lead to the emission of an
4371 * empty frame, thus we force it to zero here.
4372 */
4373 if (type == HTX_BLK_EOM)
4374 bsize = fsize = 0;
4375
4376 if (!fsize)
4377 goto send_empty;
4378
4379 if (h2s->mws <= 0) {
4380 h2s->flags |= H2_SF_BLK_SFCTL;
4381 if (h2s->send_wait) {
4382 LIST_DEL(&h2s->list);
4383 LIST_INIT(&h2s->list);
4384 }
4385 goto end;
4386 }
4387
Willy Tarreauee573762018-12-04 15:25:57 +01004388 if (fsize > count)
4389 fsize = count;
4390
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004391 if (fsize > h2s->mws)
4392 fsize = h2s->mws; // >0
4393
4394 if (h2c->mfs && fsize > h2c->mfs)
4395 fsize = h2c->mfs; // >0
4396
4397 if (fsize + 9 > outbuf.size) {
4398 /* we have an opportunity for enlarging the too small
4399 * available space, let's try.
4400 * FIXME: is this really interesting to do? Maybe we'll
4401 * spend lots of time realigning instead of using two
4402 * frames.
4403 */
4404 if (b_space_wraps(&h2c->mbuf))
4405 goto realign_again;
4406 fsize = outbuf.size - 9;
4407
4408 if (fsize <= 0) {
4409 /* no need to send an empty frame here */
4410 h2c->flags |= H2_CF_MUX_MFULL;
4411 h2s->flags |= H2_SF_BLK_MROOM;
4412 goto end;
4413 }
4414 }
4415
4416 if (h2c->mws <= 0) {
4417 h2s->flags |= H2_SF_BLK_MFCTL;
4418 goto end;
4419 }
4420
4421 if (fsize > h2c->mws)
4422 fsize = h2c->mws;
4423
4424 /* now let's copy this this into the output buffer */
4425 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004426 h2s->mws -= fsize;
4427 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004428 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004429
4430 send_empty:
4431 /* update the frame's size */
4432 h2_set_frame_size(outbuf.area, fsize);
4433
4434 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4435 * meeting EOM. We should optimize this later.
4436 */
4437 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004438 total++; // EOM counts as one byte
4439 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004440 es_now = 1;
4441 }
4442
4443 if (es_now)
4444 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4445
4446 /* commit the H2 response */
4447 b_add(&h2c->mbuf, fsize + 9);
4448
4449 /* consume incoming HTX block, including EOM */
4450 total += fsize;
4451 if (fsize == bsize) {
4452 htx_remove_blk(htx, blk);
4453 if (fsize)
4454 goto new_frame;
4455 } else {
4456 /* we've truncated this block */
4457 htx_cut_data_blk(htx, blk, fsize);
4458 }
4459
4460 if (es_now) {
4461 if (h2s->st == H2_SS_OPEN)
4462 h2s->st = H2_SS_HLOC;
4463 else
4464 h2s_close(h2s);
4465
4466 h2s->flags |= H2_SF_ES_SENT;
4467 }
4468
4469 end:
4470 return total;
4471}
4472
Olivier Houchard6ff20392018-07-17 18:46:31 +02004473/* Called from the upper layer, to subscribe to events, such as being able to send */
4474static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4475{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004476 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004477 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004478 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004479
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004480 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004481 sw = param;
4482 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4483 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004484 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004485 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004486 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004487 event_type &= ~SUB_CAN_RECV;
4488 }
4489 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004490 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004491 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004492 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004493 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004494 h2s->send_wait = sw;
4495 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4496 if (h2s->flags & H2_SF_BLK_MFCTL)
4497 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4498 else
4499 LIST_ADDQ(&h2c->send_list, &h2s->list);
4500 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004501 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004502 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004503 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004504 if (event_type != 0)
4505 return -1;
4506 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004507
4508
4509}
4510
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004511static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4512{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004513 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004514 struct h2s *h2s = cs->ctx;
4515
4516 if (event_type & SUB_CAN_RECV) {
4517 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004518 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004519 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004520 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004521 }
4522 }
4523 if (event_type & SUB_CAN_SEND) {
4524 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004525 if (h2s->send_wait == sw) {
4526 LIST_DEL(&h2s->list);
4527 LIST_INIT(&h2s->list);
4528 sw->wait_reason &= ~SUB_CAN_SEND;
4529 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004530 }
4531 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004532 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4533 sw = param;
4534 if (h2s->send_wait == sw) {
4535 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4536 h2s->send_wait = NULL;
4537 }
4538 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004539 return 0;
4540}
4541
4542
Olivier Houchard511efea2018-08-16 15:30:32 +02004543/* Called from the upper layer, to receive data */
4544static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4545{
Olivier Houchard638b7992018-08-16 15:41:52 +02004546 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004547 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004548 struct htx *h2s_htx = NULL;
4549 struct htx *buf_htx = NULL;
4550 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004551 size_t ret = 0;
4552
4553 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004554 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4555 /* in HTX mode we ignore the count argument */
4556 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004557 if (htx_is_empty(h2s_htx)) {
4558 if (cs->flags & CS_FL_REOS)
4559 cs->flags |= CS_FL_EOS;
Willy Tarreau86724e22018-12-01 23:19:43 +01004560 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004561 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004562
4563 buf_htx = htx_from_buf(buf);
4564 count = htx_free_space(buf_htx);
4565
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004566 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004567
4568 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004569 htx_to_buf(buf_htx, buf);
4570 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004571 ret = htx_ret.ret;
4572 }
4573 else {
4574 ret = b_xfer(buf, &h2s->rxbuf, count);
4575 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004576
Olivier Houchard638b7992018-08-16 15:41:52 +02004577 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004578 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004579 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004580 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004581 if (cs->flags & CS_FL_REOS)
4582 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004583 if (b_size(&h2s->rxbuf)) {
4584 b_free(&h2s->rxbuf);
4585 offer_buffers(NULL, tasks_run_queue);
4586 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004587 }
4588
Willy Tarreau082f5592018-11-25 08:03:32 +01004589 if (ret && h2c->dsi == h2s->id) {
4590 /* demux is blocking on this stream's buffer */
4591 h2c->flags &= ~H2_CF_DEM_SFULL;
4592 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4593 if (h2_recv_allowed(h2c))
4594 tasklet_wakeup(h2c->wait_event.task);
4595 }
4596 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004597end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004598 return ret;
4599}
4600
Olivier Houchardd846c262018-10-19 17:24:29 +02004601static void h2_stop_senders(struct h2c *h2c)
4602{
4603 struct h2s *h2s, *h2s_back;
4604
4605 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4606 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4607 if (h2c->msi == h2s_id(h2s))
4608 continue;
4609 LIST_DEL(&h2s->list);
4610 LIST_INIT(&h2s->list);
4611 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4612 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4613 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4614 LIST_ADD(&h2c->send_list, &h2s->list);
4615 }
4616}
4617
Willy Tarreau62f52692017-10-08 23:01:42 +02004618/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004619static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004620{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004621 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004622 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004623 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004624 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004625 struct htx *htx;
4626 struct htx_blk *blk;
4627 enum htx_blk_type btype;
4628 uint32_t bsize;
4629 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004630
Olivier Houchardd846c262018-10-19 17:24:29 +02004631 if (h2s->send_wait) {
4632 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4633 h2s->send_wait = NULL;
4634 LIST_DEL(&h2s->list);
4635 LIST_INIT(&h2s->list);
4636 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004637 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4638 return 0;
4639
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004640 /* htx will be enough to decide if we're using HTX or legacy */
4641 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4642
Willy Tarreau0bad0432018-06-14 16:54:01 +02004643 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004644 h2s->flags |= H2_SF_OUTGOING_DATA;
4645
Willy Tarreau751f2d02018-10-05 09:35:00 +02004646 if (h2s->id == 0) {
4647 int32_t id = h2c_get_next_sid(h2s->h2c);
4648
4649 if (id < 0) {
4650 cs->ctx = NULL;
4651 cs->flags |= CS_FL_ERROR;
4652 h2s_destroy(h2s);
4653 return 0;
4654 }
4655
4656 eb32_delete(&h2s->by_id);
4657 h2s->by_id.key = h2s->id = id;
4658 h2s->h2c->max_id = id;
4659 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4660 }
4661
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004662 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004663 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4664 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004665 idx = htx_get_head(htx);
4666 blk = htx_get_blk(htx, idx);
4667 btype = htx_get_blk_type(blk);
4668 bsize = htx_get_blksz(blk);
4669
4670 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004671 case HTX_BLK_REQ_SL:
4672 /* start-line before headers */
4673 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4674 if (ret > 0) {
4675 total += ret;
4676 count -= ret;
4677 if (ret < bsize)
4678 goto done;
4679 }
4680 break;
4681
Willy Tarreau115e83b2018-12-01 19:17:53 +01004682 case HTX_BLK_RES_SL:
4683 /* start-line before headers */
4684 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4685 if (ret > 0) {
4686 total += ret;
4687 count -= ret;
4688 if (ret < bsize)
4689 goto done;
4690 }
4691 break;
4692
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004693 case HTX_BLK_DATA:
4694 case HTX_BLK_EOD:
4695 case HTX_BLK_EOM:
4696 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004697 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004698 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004699 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004700 total += ret;
4701 count -= ret;
4702 if (ret < bsize)
4703 goto done;
4704 }
4705 break;
4706
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004707 default:
4708 htx_remove_blk(htx, blk);
4709 total += bsize;
4710 count -= bsize;
4711 break;
4712 }
4713 }
4714 goto done;
4715 }
4716
4717 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004718 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004719 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004720 if (h2s->h2c->flags & H2_CF_IS_BACK)
4721 ret = -1;
4722 else
4723 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004724 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004725 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004726 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004727 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004728 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004729 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004730 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004731
Willy Tarreau5dd17352018-06-14 13:33:30 +02004732 if (unlikely((int)ret <= 0)) {
4733 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004734 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4735 break;
4736 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004737 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004738 total += count;
4739 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004740 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004741 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004742 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004743 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004744 cs->flags |= CS_FL_ERROR;
4745 break;
4746 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004747
4748 total += ret;
4749 count -= ret;
4750
4751 if (h2s->st >= H2_SS_ERROR)
4752 break;
4753
4754 if (h2s->flags & H2_SF_BLK_ANY)
4755 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004756 }
4757
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004758 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004759 if (h2s->st >= H2_SS_ERROR) {
4760 /* trim any possibly pending data after we close (extra CR-LF,
4761 * unprocessed trailers, abnormal extra data, ...)
4762 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004763 total += count;
4764 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004765 }
4766
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004767 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004768 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004769 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004770 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004771 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004772 }
4773
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004774 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004775 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004776 } else {
4777 b_del(buf, total);
4778 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004779
4780 /* The mux is full, cancel the pending tasks */
4781 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4782 (h2s->flags & H2_SF_BLK_MBUSY))
4783 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004784
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004785 /* If we're running HTX, and we read the whole buffer, then pretend
4786 * we read exactly what the caller specified, as with HTX the caller
4787 * will always give the buffer size, instead of the amount of data
4788 * available.
4789 */
4790 if (htx && !b_data(buf))
4791 total = orig_count;
4792
Olivier Houchard7505f942018-08-21 18:10:44 +02004793 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004794 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4795 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004796
Olivier Houchard7505f942018-08-21 18:10:44 +02004797 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004798 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004799}
4800
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004801/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004802static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004803{
4804 struct h2c *h2c = conn->mux_ctx;
4805 struct h2s *h2s;
4806 struct eb32_node *node;
4807 int fctl_cnt = 0;
4808 int send_cnt = 0;
4809 int tree_cnt = 0;
4810 int orph_cnt = 0;
4811
4812 if (!h2c)
4813 return;
4814
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004815 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004816 fctl_cnt++;
4817
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004818 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004819 send_cnt++;
4820
4821 node = eb32_first(&h2c->streams_by_id);
4822 while (node) {
4823 h2s = container_of(node, struct h2s, by_id);
4824 tree_cnt++;
4825 if (!h2s->cs)
4826 orph_cnt++;
4827 node = eb32_next(node);
4828 }
4829
Willy Tarreau616ac812018-07-24 14:12:42 +02004830 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4831 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4832 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4833 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4834 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4835 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004836}
Willy Tarreau62f52692017-10-08 23:01:42 +02004837
4838/*******************************************************/
4839/* functions below are dedicated to the config parsers */
4840/*******************************************************/
4841
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004842/* config parser for global "tune.h2.header-table-size" */
4843static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4844 struct proxy *defpx, const char *file, int line,
4845 char **err)
4846{
4847 if (too_many_args(1, args, err, NULL))
4848 return -1;
4849
4850 h2_settings_header_table_size = atoi(args[1]);
4851 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4852 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4853 return -1;
4854 }
4855 return 0;
4856}
Willy Tarreau62f52692017-10-08 23:01:42 +02004857
Willy Tarreaue6baec02017-07-27 11:45:11 +02004858/* config parser for global "tune.h2.initial-window-size" */
4859static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4860 struct proxy *defpx, const char *file, int line,
4861 char **err)
4862{
4863 if (too_many_args(1, args, err, NULL))
4864 return -1;
4865
4866 h2_settings_initial_window_size = atoi(args[1]);
4867 if (h2_settings_initial_window_size < 0) {
4868 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4869 return -1;
4870 }
4871 return 0;
4872}
4873
Willy Tarreau5242ef82017-07-27 11:47:28 +02004874/* config parser for global "tune.h2.max-concurrent-streams" */
4875static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4876 struct proxy *defpx, const char *file, int line,
4877 char **err)
4878{
4879 if (too_many_args(1, args, err, NULL))
4880 return -1;
4881
4882 h2_settings_max_concurrent_streams = atoi(args[1]);
4883 if (h2_settings_max_concurrent_streams < 0) {
4884 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4885 return -1;
4886 }
4887 return 0;
4888}
4889
Willy Tarreau62f52692017-10-08 23:01:42 +02004890
4891/****************************************/
4892/* MUX initialization and instanciation */
4893/***************************************/
4894
4895/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004896static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004897 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004898 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004899 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004900 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004901 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004902 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004903 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004904 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004905 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004906 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004907 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004908 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004909 .shutr = h2_shutr,
4910 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004911 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004912 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004913 .name = "H2",
4914};
4915
Christopher Faulet32f61c02018-04-10 14:33:41 +02004916/* PROTO selection : this mux registers PROTO token "h2" */
4917static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004918 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004919
Willy Tarreau0108d902018-11-25 19:14:37 +01004920INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4921
Willy Tarreauf8957272018-10-03 10:25:20 +02004922static struct mux_proto_list mux_proto_h2_htx =
4923 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4924
4925INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4926
Willy Tarreau62f52692017-10-08 23:01:42 +02004927/* config keyword parsers */
4928static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004929 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004930 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004931 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004932 { 0, NULL, NULL }
4933}};
4934
Willy Tarreau0108d902018-11-25 19:14:37 +01004935INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);