blob: eeaa0c1e0eb3aff8aeafb51d8fbee3b4aa71b69f [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 {
2445 max = buf->size - b_data(buf);
2446 if (max)
2447 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2448 else
2449 ret = 0;
2450 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002451
Olivier Houchard53216e72018-10-10 15:46:36 +02002452 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002453 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002454
Olivier Houcharda1411e62018-08-17 18:42:48 +02002455 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002456 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002457 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002458 }
2459
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002460 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002461 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002462 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002463}
2464
Willy Tarreau479998a2018-11-18 06:30:59 +01002465/* Try to send data if possible.
2466 * The function returns 1 if data have been sent, otherwise zero.
2467 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002468static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002469{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002470 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002471 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002472 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002473
2474 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002475 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002476
Olivier Houchard7505f942018-08-21 18:10:44 +02002477
Willy Tarreaua2af5122017-10-09 11:56:46 +02002478 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2479 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002480 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002481 }
2482
Willy Tarreaubc933932017-10-09 16:21:43 +02002483 /* This loop is quite simple : it tries to fill as much as it can from
2484 * pending streams into the existing buffer until it's reportedly full
2485 * or the end of send requests is reached. Then it tries to send this
2486 * buffer's contents out, marks it not full if at least one byte could
2487 * be sent, and tries again.
2488 *
2489 * The snd_buf() function normally takes a "flags" argument which may
2490 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2491 * data immediately comes and CO_SFL_STREAMER to indicate that the
2492 * connection is streaming lots of data (used to increase TLS record
2493 * size at the expense of latency). The former can be sent any time
2494 * there's a buffer full flag, as it indicates at least one stream
2495 * attempted to send and failed so there are pending data. An
2496 * alternative would be to set it as long as there's an active stream
2497 * but that would be problematic for ACKs until we have an absolute
2498 * guarantee that all waiters have at least one byte to send. The
2499 * latter should possibly not be set for now.
2500 */
2501
2502 done = 0;
2503 while (!done) {
2504 unsigned int flags = 0;
2505
2506 /* fill as much as we can into the current buffer */
2507 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2508 done = h2_process_mux(h2c);
2509
2510 if (conn->flags & CO_FL_ERROR)
2511 break;
2512
2513 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2514 flags |= CO_SFL_MSG_MORE;
2515
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002516 if (b_data(&h2c->mbuf)) {
2517 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002518 if (!ret)
2519 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002520 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002521 b_del(&h2c->mbuf, ret);
2522 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002523 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002524
2525 /* wrote at least one byte, the buffer is not full anymore */
2526 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2527 }
2528
Willy Tarreaua2af5122017-10-09 11:56:46 +02002529 if (conn->flags & CO_FL_SOCK_WR_SH) {
2530 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002531 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002532 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002533 /* We're not full anymore, so we can wake any task that are waiting
2534 * for us.
2535 */
2536 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002537 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002538 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2539 struct h2s *, list);
2540 LIST_DEL(&h2s->list);
2541 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002542 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002543 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002544 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002545 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002546 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002547 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002548 /* We're done, no more to send */
2549 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002550 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002551schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002552 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2553 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002554 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002555}
2556
2557static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2558{
2559 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002560 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002561
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002562 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002563 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002564 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002565 ret |= h2_recv(h2c);
2566 if (ret)
2567 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002568 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002569}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002570
Willy Tarreau62f52692017-10-08 23:01:42 +02002571/* callback called on any event by the connection handler.
2572 * It applies changes and returns zero, or < 0 if it wants immediate
2573 * destruction of the connection (which normally doesn not happen in h2).
2574 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002575static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002576{
Olivier Houchard7505f942018-08-21 18:10:44 +02002577 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002578
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002579 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002580 h2_process_demux(h2c);
2581
2582 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002583 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002584
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002585 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002586 h2c->flags &= ~H2_CF_DEM_DFULL;
2587 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002588 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002589
Willy Tarreau0b37d652018-10-03 10:33:02 +02002590 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002591 /* frontend is stopping, reload likely in progress, let's try
2592 * to announce a graceful shutdown if not yet done. We don't
2593 * care if it fails, it will be tried again later.
2594 */
2595 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2596 if (h2c->last_sid < 0)
2597 h2c->last_sid = (1U << 31) - 1;
2598 h2c_send_goaway_error(h2c, NULL);
2599 }
2600 }
2601
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002602 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002603 * If we received early data, and the handshake is done, wake
2604 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002605 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002606 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2607 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2608 struct eb32_node *node;
2609 struct h2s *h2s;
2610
2611 h2c->flags |= H2_CF_WAIT_FOR_HS;
2612 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2613
2614 while (node) {
2615 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002616 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002617 h2s->recv_wait) {
2618 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002619 sw->wait_reason &= ~SUB_CAN_RECV;
2620 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002621 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002622 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002623 node = eb32_next(node);
2624 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002625 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002626
Willy Tarreau26bd7612017-10-09 16:47:04 +02002627 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002628 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2629 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2630 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002631 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002632
2633 if (eb_is_empty(&h2c->streams_by_id)) {
2634 /* no more stream, kill the connection now */
2635 h2_release(conn);
2636 return -1;
2637 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002638 }
2639
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002640 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002641 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002642
Olivier Houchard53216e72018-10-10 15:46:36 +02002643 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2644 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2645 (h2c->st0 != H2_CS_ERROR &&
2646 !b_data(&h2c->mbuf) &&
2647 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2648 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002649 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002650
Willy Tarreau3f133572017-10-31 19:21:06 +01002651 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002652 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002653 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002654 task_queue(h2c->task);
2655 }
2656 else
2657 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002658 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002659
Olivier Houchard7505f942018-08-21 18:10:44 +02002660 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002661 return 0;
2662}
2663
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002664static int h2_wake(struct connection *conn)
2665{
2666 struct h2c *h2c = conn->mux_ctx;
2667
2668 return (h2_process(h2c));
2669}
2670
Willy Tarreauea392822017-10-31 10:02:25 +01002671/* Connection timeout management. The principle is that if there's no receipt
2672 * nor sending for a certain amount of time, the connection is closed. If the
2673 * MUX buffer still has lying data or is not allocatable, the connection is
2674 * immediately killed. If it's allocatable and empty, we attempt to send a
2675 * GOAWAY frame.
2676 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002677static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002678{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002679 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002680 int expired = tick_is_expired(t->expire, now_ms);
2681
Willy Tarreau0975f112018-03-29 15:22:59 +02002682 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002683 return t;
2684
Willy Tarreau0975f112018-03-29 15:22:59 +02002685 task_delete(t);
2686 task_free(t);
2687
2688 if (!h2c) {
2689 /* resources were already deleted */
2690 return NULL;
2691 }
2692
2693 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002694 h2c_error(h2c, H2_ERR_NO_ERROR);
2695 h2_wake_some_streams(h2c, 0, 0);
2696
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002697 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002698 /* don't even try to send a GOAWAY, the buffer is stuck */
2699 h2c->flags |= H2_CF_GOAWAY_FAILED;
2700 }
2701
2702 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002703 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002704 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2705 h2c->flags |= H2_CF_GOAWAY_FAILED;
2706
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002707 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2708 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002709 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002710 b_del(&h2c->mbuf, ret);
2711 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002712 }
2713 }
Willy Tarreauea392822017-10-31 10:02:25 +01002714
Willy Tarreau0975f112018-03-29 15:22:59 +02002715 /* either we can release everything now or it will be done later once
2716 * the last stream closes.
2717 */
2718 if (eb_is_empty(&h2c->streams_by_id))
2719 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002720
Willy Tarreauea392822017-10-31 10:02:25 +01002721 return NULL;
2722}
2723
2724
Willy Tarreau62f52692017-10-08 23:01:42 +02002725/*******************************************/
2726/* functions below are used by the streams */
2727/*******************************************/
2728
2729/*
2730 * Attach a new stream to a connection
2731 * (Used for outgoing connections)
2732 */
2733static struct conn_stream *h2_attach(struct connection *conn)
2734{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002735 struct conn_stream *cs;
2736 struct h2s *h2s;
2737 struct h2c *h2c = conn->mux_ctx;
2738
2739 cs = cs_new(conn);
2740 if (!cs)
2741 return NULL;
2742 h2s = h2c_bck_stream_new(h2c, cs);
2743 if (!h2s) {
2744 cs_free(cs);
2745 return NULL;
2746 }
2747 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002748}
2749
Willy Tarreaufafd3982018-11-18 21:29:20 +01002750/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2751 * We have to scan because we may have some orphan streams. It might be
2752 * beneficial to scan backwards from the end to reduce the likeliness to find
2753 * orphans.
2754 */
2755static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2756{
2757 struct h2c *h2c = conn->mux_ctx;
2758 struct h2s *h2s;
2759 struct eb32_node *node;
2760
2761 node = eb32_first(&h2c->streams_by_id);
2762 while (node) {
2763 h2s = container_of(node, struct h2s, by_id);
2764 if (h2s->cs)
2765 return h2s->cs;
2766 node = eb32_next(node);
2767 }
2768 return NULL;
2769}
2770
Willy Tarreau62f52692017-10-08 23:01:42 +02002771/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002772 * Destroy the mux and the associated connection, if it is no longer used
2773 */
2774static void h2_destroy(struct connection *conn)
2775{
2776 struct h2c *h2c = conn->mux_ctx;
2777
2778 if (eb_is_empty(&h2c->streams_by_id))
2779 h2_release(h2c->conn);
2780}
2781
2782/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002783 * Detach the stream from the connection and possibly release the connection.
2784 */
2785static void h2_detach(struct conn_stream *cs)
2786{
Willy Tarreau60935142017-10-16 18:11:19 +02002787 struct h2s *h2s = cs->ctx;
2788 struct h2c *h2c;
2789
2790 cs->ctx = NULL;
2791 if (!h2s)
2792 return;
2793
2794 h2c = h2s->h2c;
2795 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002796 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002797 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2798 !h2_has_too_many_cs(h2c)) {
2799 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002800 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002801 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002802 }
Willy Tarreau60935142017-10-16 18:11:19 +02002803
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002804 /* this stream may be blocked waiting for some data to leave (possibly
2805 * an ES or RST frame), so orphan it in this case.
2806 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002807 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002808 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002809 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002810 return;
2811
Willy Tarreau45f752e2017-10-30 15:44:59 +01002812 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2813 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2814 /* unblock the connection if it was blocked on this
2815 * stream.
2816 */
2817 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2818 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002819 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002820 }
2821
Willy Tarreau71049cc2018-03-28 13:56:39 +02002822 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002823
Willy Tarreaue323f342018-03-28 13:51:45 +02002824 /* We don't want to close right now unless we're removing the
2825 * last stream, and either the connection is in error, or it
2826 * reached the ID already specified in a GOAWAY frame received
2827 * or sent (as seen by last_sid >= 0).
2828 */
2829 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2830 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002831 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002832 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002833 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002835 (conn_xprt_read0_pending(h2c->conn) ||
2836 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2837 /* no more stream will come, kill it now */
2838 h2_release(h2c->conn);
2839 }
2840 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002841 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002842 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2843 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002844 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002845 else
2846 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002847 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002848}
2849
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002850static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002851{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002852 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002853 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002854
Willy Tarreau721c9742017-11-07 11:05:42 +01002855 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002856 return;
2857
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002858 /* if no outgoing data was seen on this stream, it means it was
2859 * closed with a "tcp-request content" rule that is normally
2860 * used to kill the connection ASAP (eg: limit abuse). In this
2861 * case we send a goaway to close the connection.
2862 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002863 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002864 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002865 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002866
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002867 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2868 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002869 h2c_send_goaway_error(h2c, h2s) <= 0)
2870 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002871
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002872 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2873 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002874 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002875
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002876 return;
2877add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002878 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002879 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002880 if (h2s->flags & H2_SF_BLK_MFCTL) {
2881 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2882 h2s->send_wait = sw;
2883 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2884 h2s->send_wait = sw;
2885 LIST_ADDQ(&h2c->send_list, &h2s->list);
2886 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002887 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002888 /* Let the handler know we want shutr */
2889 sw->handle = (void *)((long)sw->handle | 1);
2890
Willy Tarreau62f52692017-10-08 23:01:42 +02002891}
2892
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002893static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002894{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002895 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002896 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002897
Willy Tarreau721c9742017-11-07 11:05:42 +01002898 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002899 return;
2900
Willy Tarreau67434202017-11-06 20:20:51 +01002901 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002902 /* we can cleanly close using an empty data frame only after headers */
2903
2904 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2905 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002906 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002907
2908 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002909 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002910 else
2911 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002912 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002913 /* if no outgoing data was seen on this stream, it means it was
2914 * closed with a "tcp-request content" rule that is normally
2915 * used to kill the connection ASAP (eg: limit abuse). In this
2916 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002917 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002918 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002919 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002920 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002921
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002922 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2923 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002924 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002925 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002926
Willy Tarreau00dd0782018-03-01 16:31:34 +01002927 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002928 }
2929
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002930 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2931 tasklet_wakeup(h2c->wait_event.task);
2932 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002933
2934 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002935 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002936 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002937 if (h2s->flags & H2_SF_BLK_MFCTL) {
2938 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2939 h2s->send_wait = sw;
2940 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2941 h2s->send_wait = sw;
2942 LIST_ADDQ(&h2c->send_list, &h2s->list);
2943 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002944 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002945 /* let the handler know we want to shutw */
2946 sw->handle = (void *)((long)(sw->handle) | 2);
2947
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002948}
2949
2950static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2951{
2952 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002953 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002954
2955 if (reason & 1)
2956 h2_do_shutr(h2s);
2957 if (reason & 2)
2958 h2_do_shutw(h2s);
2959
2960 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002961}
2962
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002963static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2964{
2965 struct h2s *h2s = cs->ctx;
2966
2967 if (!mode)
2968 return;
2969
2970 h2_do_shutr(h2s);
2971}
2972
2973static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2974{
2975 struct h2s *h2s = cs->ctx;
2976
2977 h2_do_shutw(h2s);
2978}
2979
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002980/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002981 * HTX request or response depending on the connection's side. Returns the
2982 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
2983 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002984 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002985static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002986{
2987 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002989 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002990 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002991 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002992 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002993 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002994 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002995 int flen = h2c->dfl;
2996 int outlen = 0;
2997 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002998 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002999
3000 if (!h2c->dfl) {
3001 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003002 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003003 return 0;
3004 }
3005
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003006 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003007 return 0; // incomplete input frame
3008
Willy Tarreau13278b42017-10-13 19:23:14 +02003009 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003010 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003011 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003012 copy = alloc_trash_chunk();
3013 if (!copy) {
3014 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3015 goto fail;
3016 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003017 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3018 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3019 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003020 }
3021
3022 /* The padlen is the first byte before data, and the padding appears
3023 * after data. padlen+data+padding are included in flen.
3024 */
3025 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003026 h2c->dpl = *hdrs;
3027 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003028 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3029 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003030 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003031 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003032 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003033 hdrs += 1; // skip Pad Length
3034 }
3035
3036 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3037 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003038 if (read_n32(hdrs) == h2s->id) {
3039 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3040 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003041 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003042 }
3043
Willy Tarreau13278b42017-10-13 19:23:14 +02003044 hdrs += 5; // stream dep = 4, weight = 1
3045 flen -= 5;
3046 }
3047
3048 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3049 * don't support this for now and can't even decompress so we have to
3050 * break the connection.
3051 */
3052 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3053 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003054 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003055 }
3056
Olivier Houchard638b7992018-08-16 15:41:52 +02003057 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003058 if (!csbuf) {
3059 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003060 goto fail;
3061 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003062
Willy Tarreau937f7602018-02-26 15:22:17 +01003063 /* we can't retry a failed decompression operation so we must be very
3064 * careful not to take any risks. In practice the output buffer is
3065 * always empty except maybe for trailers, in which case we simply have
3066 * to wait for the upper layer to finish consuming what is available.
3067 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003068
3069 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3070 htx = htx_from_buf(&h2s->rxbuf);
3071 if (!htx_is_empty(htx))
3072 goto fail;
3073 } else {
3074 if (b_data(csbuf))
3075 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003076
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003077 csbuf->head = 0;
3078 try = b_size(csbuf);
3079 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003080
3081 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3082 sizeof(list)/sizeof(list[0]), tmp);
3083 if (outlen < 0) {
3084 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3085 goto fail;
3086 }
3087
3088 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003089 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003090
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003091 if (htx) {
3092 /* HTX mode */
3093 if (h2c->flags & H2_CF_IS_BACK)
3094 outlen = h2_make_htx_response(list, htx, &msgf);
3095 else
3096 outlen = h2_make_htx_request(list, htx, &msgf);
3097 } else {
3098 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003099 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003100 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003101
3102 if (outlen < 0) {
3103 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3104 goto fail;
3105 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003106
Willy Tarreau174b06a2018-04-25 18:13:58 +02003107 if (msgf & H2_MSGF_BODY) {
3108 /* a payload is present */
3109 if (msgf & H2_MSGF_BODY_CL)
3110 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003111 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003112 h2s->flags |= H2_SF_DATA_CHNK;
3113 }
3114
Willy Tarreau13278b42017-10-13 19:23:14 +02003115 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003116 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003117 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003118 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003119
Willy Tarreau39d68502018-03-02 12:26:37 +01003120 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003121 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003122 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003123 if (htx)
3124 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003125 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003126
Willy Tarreau68dd9852017-07-03 14:44:26 +02003127 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003128 if (htx)
3129 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003130 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003131 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003132 fail:
3133 outlen = 0;
3134 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003135}
3136
Willy Tarreau454f9052017-10-26 19:40:35 +02003137/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3138 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3139 * in use, a new chunk is emitted for each frame. This is supposed to fit
3140 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3141 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3142 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003143 * parser state is automatically updated. Returns > 0 if it could completely
3144 * send the current frame, 0 if it couldn't complete, in which case
3145 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3146 * DATA frame can return 0 as a valid result). Stream errors are reported in
3147 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3148 * have checked the frame header and ensured that the frame was complete or the
3149 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003150 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003151static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003152{
3153 struct h2c *h2c = h2s->h2c;
3154 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003155 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003156 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003157 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003158 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003159
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003160 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003161
3162 /* The padlen is the first byte before data, and the padding appears
3163 * after data. padlen+data+padding are included in flen.
3164 */
Willy Tarreau79127812017-12-03 21:06:59 +01003165 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003166 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003167 return 0;
3168
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003169 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003170 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003171 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3172 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003173 return 0;
3174 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003175
3176 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003177 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003178 h2c->dfl--;
3179 h2c->rcvd_c++; h2c->rcvd_s++;
3180 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003181 }
3182
Olivier Houchard638b7992018-08-16 15:41:52 +02003183 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003184 if (!csbuf) {
3185 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003186 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003187 }
3188
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003189try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003190 flen = h2c->dfl - h2c->dpl;
3191 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003192 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003193
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003194 if (flen > b_data(&h2c->dbuf)) {
3195 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003196 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003197 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003198 }
3199
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003200 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3201 htx = htx_from_buf(csbuf);
3202 block1 = htx_free_data_space(htx);
3203 if (!block1) {
3204 h2c->flags |= H2_CF_DEM_SFULL;
3205 goto fail;
3206 }
3207 if (flen > block1)
3208 flen = block1;
3209
3210 /* here, flen is the max we can copy into the output buffer */
3211 block1 = b_contig_data(&h2c->dbuf, 0);
3212 if (flen > block1)
3213 flen = block1;
3214
3215 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3216 h2c->flags |= H2_CF_DEM_SFULL;
3217 goto fail;
3218 }
3219
3220 b_del(&h2c->dbuf, flen);
3221 h2c->dfl -= flen;
3222 h2c->rcvd_c += flen;
3223 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3224 goto try_again;
3225 }
3226 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003227 /* it doesn't fit and the buffer is fragmented,
3228 * so let's defragment it and try again.
3229 */
3230 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003231 }
3232
Willy Tarreaueba10f22018-04-25 20:44:22 +02003233 /* chunked-encoding requires more room */
3234 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003235 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003236 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3237 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3238 (chklen < 1048576) ? 4 : 8;
3239 chklen += 4; // CRLF, CRLF
3240 }
3241
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003242 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003243 if (flen + chklen > b_room(csbuf)) {
3244 if (chklen >= b_room(csbuf)) {
3245 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003246 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003247 }
3248 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003249 }
3250
3251 if (h2s->flags & H2_SF_DATA_CHNK) {
3252 /* emit the chunk size */
3253 unsigned int chksz = flen;
3254 char str[10];
3255 char *beg;
3256
3257 beg = str + sizeof(str);
3258 *--beg = '\n';
3259 *--beg = '\r';
3260 do {
3261 *--beg = hextab[chksz & 0xF];
3262 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003263 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003264 }
3265
Willy Tarreau454f9052017-10-26 19:40:35 +02003266 /* Block1 is the length of the first block before the buffer wraps,
3267 * block2 is the optional second block to reach the end of the frame.
3268 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003269 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003270 if (block1 > flen)
3271 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003272 block2 = flen - block1;
3273
3274 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003275 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003276
3277 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003278 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003279
Willy Tarreaueba10f22018-04-25 20:44:22 +02003280 if (h2s->flags & H2_SF_DATA_CHNK) {
3281 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003282 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003283 }
3284
Willy Tarreau454f9052017-10-26 19:40:35 +02003285 /* now mark the input data as consumed (will be deleted from the buffer
3286 * by the caller when seeing FRAME_A after sending the window update).
3287 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003288 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003289 h2c->dfl -= flen;
3290 h2c->rcvd_c += flen;
3291 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3292
3293 if (h2c->dfl > h2c->dpl) {
3294 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003295 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003296 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003297 }
3298
Willy Tarreau4a28da12018-01-04 14:41:00 +01003299 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003300 /* here we're done with the frame, all the payload (except padding) was
3301 * transferred.
3302 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003303
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003304 if (h2c->dff & H2_F_DATA_END_STREAM) {
3305 if (htx) {
3306 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3307 h2c->flags |= H2_CF_DEM_SFULL;
3308 goto fail;
3309 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003310 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003311 else if (h2s->flags & H2_SF_DATA_CHNK) {
3312 /* emit the trailing 0 CRLF CRLF */
3313 if (b_room(csbuf) < 5) {
3314 h2c->flags |= H2_CF_DEM_SFULL;
3315 goto fail;
3316 }
3317 chklen += 5;
3318 b_putblk(csbuf, "0\r\n\r\n", 5);
3319 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003320 }
3321
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003322 h2c->rcvd_c += h2c->dpl;
3323 h2c->rcvd_s += h2c->dpl;
3324 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003325 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3326
Willy Tarreau39d68502018-03-02 12:26:37 +01003327 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003328 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003329 h2s->cs->flags |= CS_FL_REOS;
3330 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003331 if (htx)
3332 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003333 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003334 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003335 if (htx)
3336 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003337 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003338}
3339
Willy Tarreau5dd17352018-06-14 13:33:30 +02003340/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3341 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3342 * number of bytes sent. The caller must check the stream's status to detect
3343 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003344 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003345static 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 +02003346{
3347 struct http_hdr list[MAX_HTTP_HDR];
3348 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003349 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003350 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003351 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003352 int es_now = 0;
3353 int ret = 0;
3354 int hdr;
3355
3356 if (h2c_mux_busy(h2c, h2s)) {
3357 h2s->flags |= H2_SF_BLK_MBUSY;
3358 return 0;
3359 }
3360
Willy Tarreau44e973f2018-03-01 17:49:30 +01003361 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003362 h2c->flags |= H2_CF_MUX_MALLOC;
3363 h2s->flags |= H2_SF_BLK_MROOM;
3364 return 0;
3365 }
3366
3367 /* First, try to parse the H1 response and index it into <list>.
3368 * NOTE! Since it comes from haproxy, we *know* that a response header
3369 * block does not wrap and we can safely read it this way without
3370 * having to realign the buffer.
3371 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003372 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003373 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003374 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003375 /* incomplete or invalid response, this is abnormal coming from
3376 * haproxy and may only result in a bad errorfile or bad Lua code
3377 * so that won't be fixed, raise an error now.
3378 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003379 * FIXME: we should instead add the ability to only return a
3380 * 502 bad gateway. But in theory this is not supposed to
3381 * happen.
3382 */
3383 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3384 ret = 0;
3385 goto end;
3386 }
3387
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003388 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003389
3390 /* certain statuses have no body or an empty one, regardless of
3391 * what the headers say.
3392 */
3393 if (sl.st.status >= 100 && sl.st.status < 200) {
3394 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3395 h1m->curr_len = h1m->body_len = 0;
3396 }
3397 else if (sl.st.status == 204 || sl.st.status == 304) {
3398 /* no contents, claim c-len is present and set to zero */
3399 h1m->flags &= ~H1_MF_CHNK;
3400 h1m->flags |= H1_MF_CLEN;
3401 h1m->curr_len = h1m->body_len = 0;
3402 }
3403
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003404 chunk_reset(&outbuf);
3405
3406 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003407 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003408 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003409 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003410
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003411 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003412 break;
3413 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003414 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003415 }
3416
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003417 if (outbuf.size < 9)
3418 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003419
3420 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003421 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3422 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3423 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003424
3425 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003426 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003427 /* this is an unparsable response */
3428 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3429 ret = 0;
3430 goto end;
3431 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003432
3433 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003434 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003435 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003436 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003437 }
3438
3439 /* encode all headers, stop at empty name */
3440 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003441 /* these ones do not exist in H2 and must be dropped. */
3442 if (isteq(list[hdr].n, ist("connection")) ||
3443 isteq(list[hdr].n, ist("proxy-connection")) ||
3444 isteq(list[hdr].n, ist("keep-alive")) ||
3445 isteq(list[hdr].n, ist("upgrade")) ||
3446 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003447 continue;
3448
3449 if (isteq(list[hdr].n, ist("")))
3450 break; // end
3451
3452 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3453 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003454 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003455 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003456 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003457 }
3458 }
3459
3460 /* we may need to add END_STREAM */
3461 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3462 es_now = 1;
3463
3464 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003465 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003466
3467 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003468 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003469
3470 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003471 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003472
3473 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003474 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003475 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003476
3477 /* for now we don't implemented CONTINUATION, so we wait for a
3478 * body or directly end in TRL2.
3479 */
3480 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003481 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003482 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003483
Willy Tarreau801250e2018-09-11 11:45:04 +02003484 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003485 h2s->flags |= H2_SF_ES_SENT;
3486 if (h2s->st == H2_SS_OPEN)
3487 h2s->st = H2_SS_HLOC;
3488 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003489 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003490 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003491 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003492 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003493 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003494 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003495 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003496 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003497 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003498
3499 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003500
3501 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003502 //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 +02003503 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003504 full:
3505 h1m_init_res(h1m);
3506 h1m->err_pos = -1; // don't care about errors on the response path
3507 h2c->flags |= H2_CF_MUX_MFULL;
3508 h2s->flags |= H2_SF_BLK_MROOM;
3509 ret = 0;
3510 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003511}
3512
Willy Tarreau5dd17352018-06-14 13:33:30 +02003513/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3514 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3515 * the number of bytes sent. The caller must check the stream's status to
3516 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003517 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003518static 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 +02003519{
3520 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003521 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003522 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003524 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003525 int es_now = 0;
3526 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003527 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003528 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003529
3530 if (h2c_mux_busy(h2c, h2s)) {
3531 h2s->flags |= H2_SF_BLK_MBUSY;
3532 goto end;
3533 }
3534
Willy Tarreau44e973f2018-03-01 17:49:30 +01003535 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003536 h2c->flags |= H2_CF_MUX_MALLOC;
3537 h2s->flags |= H2_SF_BLK_MROOM;
3538 goto end;
3539 }
3540
3541 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003542 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003543 goto end;
3544
3545 chunk_reset(&outbuf);
3546
3547 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003548 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003549 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003550 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003551
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003552 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003553 break;
3554 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003555 /* If there are pending data in the output buffer, and we have
3556 * less than 1/4 of the mbuf's size and everything fits, we'll
3557 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3558 * is full and wait, to save some slow realign calls.
3559 */
3560 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3561 h2c->flags |= H2_CF_MUX_MFULL;
3562 h2s->flags |= H2_SF_BLK_MROOM;
3563 goto end;
3564 }
3565
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003566 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003567 }
3568
3569 if (outbuf.size < 9) {
3570 h2c->flags |= H2_CF_MUX_MFULL;
3571 h2s->flags |= H2_SF_BLK_MROOM;
3572 goto end;
3573 }
3574
3575 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003576 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3577 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3578 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003579
3580 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3581 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003582 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003583 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003584 break;
3585 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003586 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003587 if ((long long)size > h1m->curr_len)
3588 size = h1m->curr_len;
3589 break;
3590 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003591 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003592 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003593 if (!ret)
3594 goto end;
3595
3596 if (ret < 0) {
3597 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003598 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003599 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3600 goto end;
3601 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003602 max -= ret;
3603 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003604 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003605 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003606 }
3607
Willy Tarreau801250e2018-09-11 11:45:04 +02003608 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003609 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003610 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003611 if (!ret)
3612 goto end;
3613
3614 if (ret < 0) {
3615 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003616 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003617 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3618 goto end;
3619 }
3620
3621 size = chunk;
3622 h1m->curr_len = chunk;
3623 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003624 max -= ret;
3625 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003626 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003627 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003628 if (!size)
3629 goto send_empty;
3630 }
3631
3632 /* in MSG_DATA state, continue below */
3633 size = h1m->curr_len;
3634 break;
3635 }
3636
3637 /* we have in <size> the exact number of bytes we need to copy from
3638 * the H1 buffer. We need to check this against the connection's and
3639 * the stream's send windows, and to ensure that this fits in the max
3640 * frame size and in the buffer's available space minus 9 bytes (for
3641 * the frame header). The connection's flow control is applied last so
3642 * that we can use a separate list of streams which are immediately
3643 * unblocked on window opening. Note: we don't implement padding.
3644 */
3645
Willy Tarreau5dd17352018-06-14 13:33:30 +02003646 if (size > max)
3647 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003648
3649 if (size > h2s->mws)
3650 size = h2s->mws;
3651
3652 if (size <= 0) {
3653 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003654 if (h2s->send_wait) {
3655 LIST_DEL(&h2s->list);
3656 LIST_INIT(&h2s->list);
3657 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003658 goto end;
3659 }
3660
3661 if (h2c->mfs && size > h2c->mfs)
3662 size = h2c->mfs;
3663
3664 if (size + 9 > outbuf.size) {
3665 /* we have an opportunity for enlarging the too small
3666 * available space, let's try.
3667 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003668 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003669 goto realign_again;
3670 size = outbuf.size - 9;
3671 }
3672
3673 if (size <= 0) {
3674 h2c->flags |= H2_CF_MUX_MFULL;
3675 h2s->flags |= H2_SF_BLK_MROOM;
3676 goto end;
3677 }
3678
3679 if (size > h2c->mws)
3680 size = h2c->mws;
3681
3682 if (size <= 0) {
3683 h2s->flags |= H2_SF_BLK_MFCTL;
3684 goto end;
3685 }
3686
3687 /* copy whatever we can */
3688 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003689 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003690 if (ret == 1)
3691 len2 = 0;
3692
3693 if (!ret || len1 + len2 < size) {
3694 /* FIXME: must normally never happen */
3695 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3696 goto end;
3697 }
3698
3699 /* limit len1/len2 to size */
3700 if (len1 + len2 > size) {
3701 int sub = len1 + len2 - size;
3702
3703 if (len2 > sub)
3704 len2 -= sub;
3705 else {
3706 sub -= len2;
3707 len2 = 0;
3708 len1 -= sub;
3709 }
3710 }
3711
3712 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003713 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003714 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003715 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003716
3717 send_empty:
3718 /* we may need to add END_STREAM */
3719 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3720 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003721 *
3722 * FIXME: what we do here is not correct because we send end_stream
3723 * before knowing if we'll have to send a HEADERS frame for the
3724 * trailers. More importantly we're not consuming the trailing CRLF
3725 * after the end of trailers, so it will be left to the caller to
3726 * eat it. The right way to do it would be to measure trailers here
3727 * and to send ES only if there are no trailers.
3728 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003729 */
3730 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003731 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003732 es_now = 1;
3733
3734 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003735 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003736
3737 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003738 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003739
3740 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003741 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003742
3743 /* consume incoming H1 response */
3744 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003745 max -= size;
3746 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003747 total += size;
3748 h1m->curr_len -= size;
3749 h2s->mws -= size;
3750 h2c->mws -= size;
3751
3752 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003753 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003754 goto new_frame;
3755 }
3756 }
3757
3758 if (es_now) {
3759 if (h2s->st == H2_SS_OPEN)
3760 h2s->st = H2_SS_HLOC;
3761 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003762 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003763
Willy Tarreau35a62702018-02-27 15:37:25 +01003764 if (!(h1m->flags & H1_MF_CHNK)) {
3765 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003766 total += max;
3767 ofs += max;
3768 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003769
Willy Tarreau801250e2018-09-11 11:45:04 +02003770 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003771 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003772
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003773 h2s->flags |= H2_SF_ES_SENT;
3774 }
3775
3776 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003777 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 +02003778 return total;
3779}
3780
Willy Tarreau115e83b2018-12-01 19:17:53 +01003781/* Try to send a HEADERS frame matching HTX response present in HTX message
3782 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3783 * must check the stream's status to detect any error which might have happened
3784 * subsequently to a successful send. The htx blocks are automatically removed
3785 * from the message. The htx message is assumed to be valid since produced from
3786 * the internal code, hence it contains a start line, an optional series of
3787 * header blocks and an end of header, otherwise an invalid frame could be
3788 * emitted and the resulting htx message could be left in an inconsistent state.
3789 */
3790static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3791{
3792 struct http_hdr list[MAX_HTTP_HDR];
3793 struct h2c *h2c = h2s->h2c;
3794 struct htx_blk *blk;
3795 struct htx_blk *blk_end;
3796 struct buffer outbuf;
3797 struct htx_sl *sl;
3798 enum htx_blk_type type;
3799 int es_now = 0;
3800 int ret = 0;
3801 int hdr;
3802 int idx;
3803
3804 if (h2c_mux_busy(h2c, h2s)) {
3805 h2s->flags |= H2_SF_BLK_MBUSY;
3806 return 0;
3807 }
3808
3809 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3810 h2c->flags |= H2_CF_MUX_MALLOC;
3811 h2s->flags |= H2_SF_BLK_MROOM;
3812 return 0;
3813 }
3814
3815 /* determine the first block which must not be deleted, blk_end may
3816 * be NULL if all blocks have to be deleted.
3817 */
3818 idx = htx_get_head(htx);
3819 blk_end = NULL;
3820 while (idx != -1) {
3821 type = htx_get_blk_type(htx_get_blk(htx, idx));
3822 idx = htx_get_next(htx, idx);
3823 if (type == HTX_BLK_EOH) {
3824 if (idx != -1)
3825 blk_end = htx_get_blk(htx, idx);
3826 break;
3827 }
3828 }
3829
3830 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003831 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003832 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003833 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003834 if (h2s->status < 100 || h2s->status > 999)
3835 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003836
3837 /* and the rest of the headers, that we dump starting at header 0 */
3838 hdr = 0;
3839
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003840 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003841 while ((idx = htx_get_next(htx, idx)) != -1) {
3842 blk = htx_get_blk(htx, idx);
3843 type = htx_get_blk_type(blk);
3844
3845 if (type == HTX_BLK_UNUSED)
3846 continue;
3847
3848 if (type != HTX_BLK_HDR)
3849 break;
3850
3851 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3852 goto fail;
3853
3854 list[hdr].n = htx_get_blk_name(htx, blk);
3855 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003856 hdr++;
3857 }
3858
3859 /* marker for end of headers */
3860 list[hdr].n = ist("");
3861
3862 if (h2s->status == 204 || h2s->status == 304) {
3863 /* no contents, claim c-len is present and set to zero */
3864 es_now = 1;
3865 }
3866
3867 chunk_reset(&outbuf);
3868
3869 while (1) {
3870 outbuf.area = b_tail(&h2c->mbuf);
3871 outbuf.size = b_contig_space(&h2c->mbuf);
3872 outbuf.data = 0;
3873
3874 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3875 break;
3876 realign_again:
3877 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3878 }
3879
3880 if (outbuf.size < 9)
3881 goto full;
3882
3883 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3884 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3885 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3886 outbuf.data = 9;
3887
3888 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003889 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003890 if (b_space_wraps(&h2c->mbuf))
3891 goto realign_again;
3892 goto full;
3893 }
3894
3895 /* encode all headers, stop at empty name */
3896 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3897 /* these ones do not exist in H2 and must be dropped. */
3898 if (isteq(list[hdr].n, ist("connection")) ||
3899 isteq(list[hdr].n, ist("proxy-connection")) ||
3900 isteq(list[hdr].n, ist("keep-alive")) ||
3901 isteq(list[hdr].n, ist("upgrade")) ||
3902 isteq(list[hdr].n, ist("transfer-encoding")))
3903 continue;
3904
3905 if (isteq(list[hdr].n, ist("")))
3906 break; // end
3907
3908 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3909 /* output full */
3910 if (b_space_wraps(&h2c->mbuf))
3911 goto realign_again;
3912 goto full;
3913 }
3914 }
3915
3916 /* we may need to add END_STREAM.
3917 * FIXME: we should also set it when we know for sure that the
3918 * content-length is zero as well as on 204/304
3919 */
3920 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3921 es_now = 1;
3922
3923 if (h2s->cs->flags & CS_FL_SHW)
3924 es_now = 1;
3925
3926 /* update the frame's size */
3927 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3928
3929 if (es_now)
3930 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3931
3932 /* commit the H2 response */
3933 b_add(&h2c->mbuf, outbuf.data);
3934 h2s->flags |= H2_SF_HEADERS_SENT;
3935
3936 /* for now we don't implemented CONTINUATION, so we wait for a
3937 * body or directly end in TRL2.
3938 */
3939 if (es_now) {
3940 h2s->flags |= H2_SF_ES_SENT;
3941 if (h2s->st == H2_SS_OPEN)
3942 h2s->st = H2_SS_HLOC;
3943 else
3944 h2s_close(h2s);
3945 }
3946
3947 /* OK we could properly deliver the response */
3948
3949 /* remove all header blocks including the EOH and compute the
3950 * corresponding size.
3951 *
3952 * FIXME: We should remove everything when es_now is set.
3953 */
3954 ret = 0;
3955 idx = htx_get_head(htx);
3956 blk = htx_get_blk(htx, idx);
3957 while (blk != blk_end) {
3958 ret += htx_get_blksz(blk);
3959 blk = htx_remove_blk(htx, blk);
3960 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003961
3962 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3963 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003964 end:
3965 return ret;
3966 full:
3967 h2c->flags |= H2_CF_MUX_MFULL;
3968 h2s->flags |= H2_SF_BLK_MROOM;
3969 ret = 0;
3970 goto end;
3971 fail:
3972 /* unparsable HTX messages, too large ones to be produced in the local
3973 * list etc go here (unrecoverable errors).
3974 */
3975 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3976 ret = 0;
3977 goto end;
3978}
3979
Willy Tarreau80739692018-10-05 11:35:57 +02003980/* Try to send a HEADERS frame matching HTX request present in HTX message
3981 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3982 * must check the stream's status to detect any error which might have happened
3983 * subsequently to a successful send. The htx blocks are automatically removed
3984 * from the message. The htx message is assumed to be valid since produced from
3985 * the internal code, hence it contains a start line, an optional series of
3986 * header blocks and an end of header, otherwise an invalid frame could be
3987 * emitted and the resulting htx message could be left in an inconsistent state.
3988 */
3989static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
3990{
3991 struct http_hdr list[MAX_HTTP_HDR];
3992 struct h2c *h2c = h2s->h2c;
3993 struct htx_blk *blk;
3994 struct htx_blk *blk_end;
3995 struct buffer outbuf;
3996 struct htx_sl *sl;
3997 struct ist meth, path;
3998 enum htx_blk_type type;
3999 int es_now = 0;
4000 int ret = 0;
4001 int hdr;
4002 int idx;
4003
4004 if (h2c_mux_busy(h2c, h2s)) {
4005 h2s->flags |= H2_SF_BLK_MBUSY;
4006 return 0;
4007 }
4008
4009 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4010 h2c->flags |= H2_CF_MUX_MALLOC;
4011 h2s->flags |= H2_SF_BLK_MROOM;
4012 return 0;
4013 }
4014
4015 /* determine the first block which must not be deleted, blk_end may
4016 * be NULL if all blocks have to be deleted.
4017 */
4018 idx = htx_get_head(htx);
4019 blk_end = NULL;
4020 while (idx != -1) {
4021 type = htx_get_blk_type(htx_get_blk(htx, idx));
4022 idx = htx_get_next(htx, idx);
4023 if (type == HTX_BLK_EOH) {
4024 if (idx != -1)
4025 blk_end = htx_get_blk(htx, idx);
4026 break;
4027 }
4028 }
4029
4030 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004031 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004032 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004033 meth = htx_sl_req_meth(sl);
4034 path = htx_sl_req_uri(sl);
4035
4036 /* and the rest of the headers, that we dump starting at header 0 */
4037 hdr = 0;
4038
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004039 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004040 while ((idx = htx_get_next(htx, idx)) != -1) {
4041 blk = htx_get_blk(htx, idx);
4042 type = htx_get_blk_type(blk);
4043
4044 if (type == HTX_BLK_UNUSED)
4045 continue;
4046
4047 if (type != HTX_BLK_HDR)
4048 break;
4049
4050 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4051 goto fail;
4052
4053 list[hdr].n = htx_get_blk_name(htx, blk);
4054 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004055 hdr++;
4056 }
4057
4058 /* marker for end of headers */
4059 list[hdr].n = ist("");
4060
4061 chunk_reset(&outbuf);
4062
4063 while (1) {
4064 outbuf.area = b_tail(&h2c->mbuf);
4065 outbuf.size = b_contig_space(&h2c->mbuf);
4066 outbuf.data = 0;
4067
4068 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4069 break;
4070 realign_again:
4071 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4072 }
4073
4074 if (outbuf.size < 9)
4075 goto full;
4076
4077 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4078 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4079 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4080 outbuf.data = 9;
4081
4082 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004083 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004084 if (b_space_wraps(&h2c->mbuf))
4085 goto realign_again;
4086 goto full;
4087 }
4088
4089 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004090 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4091 /* output full */
4092 if (b_space_wraps(&h2c->mbuf))
4093 goto realign_again;
4094 goto full;
4095 }
Willy Tarreau80739692018-10-05 11:35:57 +02004096
4097 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004098 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004099 /* output full */
4100 if (b_space_wraps(&h2c->mbuf))
4101 goto realign_again;
4102 goto full;
4103 }
4104
4105 /* encode all headers, stop at empty name */
4106 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4107 /* these ones do not exist in H2 and must be dropped. */
4108 if (isteq(list[hdr].n, ist("connection")) ||
4109 isteq(list[hdr].n, ist("proxy-connection")) ||
4110 isteq(list[hdr].n, ist("keep-alive")) ||
4111 isteq(list[hdr].n, ist("upgrade")) ||
4112 isteq(list[hdr].n, ist("transfer-encoding")))
4113 continue;
4114
4115 if (isteq(list[hdr].n, ist("")))
4116 break; // end
4117
4118 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4119 /* output full */
4120 if (b_space_wraps(&h2c->mbuf))
4121 goto realign_again;
4122 goto full;
4123 }
4124 }
4125
4126 /* we may need to add END_STREAM if we have no body :
4127 * - request already closed, or :
4128 * - no transfer-encoding, and :
4129 * - no content-length or content-length:0
4130 * Fixme: this doesn't take into account CONNECT requests.
4131 */
4132 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4133 es_now = 1;
4134
4135 if (sl->flags & HTX_SL_F_BODYLESS)
4136 es_now = 1;
4137
4138 if (h2s->cs->flags & CS_FL_SHW)
4139 es_now = 1;
4140
4141 /* update the frame's size */
4142 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4143
4144 if (es_now)
4145 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4146
4147 /* commit the H2 response */
4148 b_add(&h2c->mbuf, outbuf.data);
4149 h2s->flags |= H2_SF_HEADERS_SENT;
4150 h2s->st = H2_SS_OPEN;
4151
4152 /* for now we don't implemented CONTINUATION, so we wait for a
4153 * body or directly end in TRL2.
4154 */
4155 if (es_now) {
4156 // trim any possibly pending data (eg: inconsistent content-length)
4157 h2s->flags |= H2_SF_ES_SENT;
4158 h2s->st = H2_SS_HLOC;
4159 }
4160
4161 /* remove all header blocks including the EOH and compute the
4162 * corresponding size.
4163 *
4164 * FIXME: We should remove everything when es_now is set.
4165 */
4166 ret = 0;
4167 idx = htx_get_head(htx);
4168 blk = htx_get_blk(htx, idx);
4169 while (blk != blk_end) {
4170 ret += htx_get_blksz(blk);
4171 blk = htx_remove_blk(htx, blk);
4172 }
4173
4174 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4175 htx_remove_blk(htx, blk_end);
4176
4177 end:
4178 return ret;
4179 full:
4180 h2c->flags |= H2_CF_MUX_MFULL;
4181 h2s->flags |= H2_SF_BLK_MROOM;
4182 ret = 0;
4183 goto end;
4184 fail:
4185 /* unparsable HTX messages, too large ones to be produced in the local
4186 * list etc go here (unrecoverable errors).
4187 */
4188 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4189 ret = 0;
4190 goto end;
4191}
4192
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004193/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004194 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4195 * caller must check the stream's status to detect any error which might have
4196 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004197 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4198 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004199static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004200{
4201 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004202 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004203 struct buffer outbuf;
4204 size_t total = 0;
4205 int es_now = 0;
4206 int bsize; /* htx block size */
4207 int fsize; /* h2 frame size */
4208 struct htx_blk *blk;
4209 enum htx_blk_type type;
4210 int idx;
4211
4212 if (h2c_mux_busy(h2c, h2s)) {
4213 h2s->flags |= H2_SF_BLK_MBUSY;
4214 goto end;
4215 }
4216
4217 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4218 h2c->flags |= H2_CF_MUX_MALLOC;
4219 h2s->flags |= H2_SF_BLK_MROOM;
4220 goto end;
4221 }
4222
Willy Tarreau98de12a2018-12-12 07:03:00 +01004223 htx = htx_from_buf(buf);
4224
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004225 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4226 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4227 * the caller to handle.
4228 */
4229
4230 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004231 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004232 goto end;
4233
4234 idx = htx_get_head(htx);
4235 blk = htx_get_blk(htx, idx);
4236 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4237 bsize = htx_get_blksz(blk);
4238 fsize = bsize;
4239
4240 if (type == HTX_BLK_EOD) {
4241 /* if we have an EOD, we're dealing with chunked data. We may
4242 * have a set of trailers after us that the caller will want to
4243 * deal with. Let's simply remove the EOD and return.
4244 */
4245 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004246 total++; // EOD counts as one byte
4247 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004248 goto end;
4249 }
4250
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004251 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4252 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004253
4254 /* Perform some optimizations to reduce the number of buffer copies.
4255 * First, if the mux's buffer is empty and the htx area contains
4256 * exactly one data block of the same size as the requested count, and
4257 * this count fits within the frame size, the stream's window size, and
4258 * the connection's window size, then it's possible to simply swap the
4259 * caller's buffer with the mux's output buffer and adjust offsets and
4260 * length to match the entire DATA HTX block in the middle. In this
4261 * case we perform a true zero-copy operation from end-to-end. This is
4262 * the situation that happens all the time with large files. Second, if
4263 * this is not possible, but the mux's output buffer is empty, we still
4264 * have an opportunity to avoid the copy to the intermediary buffer, by
4265 * making the intermediary buffer's area point to the output buffer's
4266 * area. In this case we want to skip the HTX header to make sure that
4267 * copies remain aligned and that this operation remains possible all
4268 * the time. This goes for headers, data blocks and any data extracted
4269 * from the HTX blocks.
4270 */
4271 if (unlikely(fsize == count &&
4272 htx->used == 1 && type == HTX_BLK_DATA &&
4273 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4274 void *old_area = h2c->mbuf.area;
4275
4276 if (b_data(&h2c->mbuf)) {
4277 /* too bad there are data left there. If we have less
4278 * than 1/4 of the mbuf's size and everything fits,
4279 * we'll perform a copy anyway. Otherwise we'll pretend
4280 * the mbuf is full and wait.
4281 */
4282 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4283 goto copy;
4284 h2c->flags |= H2_CF_MUX_MFULL;
4285 h2s->flags |= H2_SF_BLK_MROOM;
4286 goto end;
4287 }
4288
4289 /* map an H2 frame to the HTX block so that we can put the
4290 * frame header there.
4291 */
4292 h2c->mbuf.area = buf->area;
4293 h2c->mbuf.head = sizeof(struct htx) - 9;
4294 h2c->mbuf.data = fsize + 9;
4295 outbuf.area = b_head(&h2c->mbuf);
4296
4297 /* prepend an H2 DATA frame header just before the DATA block */
4298 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4299 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4300 h2_set_frame_size(outbuf.area, fsize);
4301
4302 /* update windows */
4303 h2s->mws -= fsize;
4304 h2c->mws -= fsize;
4305
4306 /* and exchange with our old area */
4307 buf->area = old_area;
4308 buf->data = buf->head = 0;
4309 total += fsize;
4310 goto end;
4311 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004312
Willy Tarreau98de12a2018-12-12 07:03:00 +01004313 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004314 /* for DATA and EOM we'll have to emit a frame, even if empty */
4315
4316 while (1) {
4317 outbuf.area = b_tail(&h2c->mbuf);
4318 outbuf.size = b_contig_space(&h2c->mbuf);
4319 outbuf.data = 0;
4320
4321 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4322 break;
4323 realign_again:
4324 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4325 }
4326
4327 if (outbuf.size < 9) {
4328 h2c->flags |= H2_CF_MUX_MFULL;
4329 h2s->flags |= H2_SF_BLK_MROOM;
4330 goto end;
4331 }
4332
4333 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4334 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4335 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4336 outbuf.data = 9;
4337
4338 /* we have in <fsize> the exact number of bytes we need to copy from
4339 * the HTX buffer. We need to check this against the connection's and
4340 * the stream's send windows, and to ensure that this fits in the max
4341 * frame size and in the buffer's available space minus 9 bytes (for
4342 * the frame header). The connection's flow control is applied last so
4343 * that we can use a separate list of streams which are immediately
4344 * unblocked on window opening. Note: we don't implement padding.
4345 */
4346
4347 /* EOM is presented with bsize==1 but would lead to the emission of an
4348 * empty frame, thus we force it to zero here.
4349 */
4350 if (type == HTX_BLK_EOM)
4351 bsize = fsize = 0;
4352
4353 if (!fsize)
4354 goto send_empty;
4355
4356 if (h2s->mws <= 0) {
4357 h2s->flags |= H2_SF_BLK_SFCTL;
4358 if (h2s->send_wait) {
4359 LIST_DEL(&h2s->list);
4360 LIST_INIT(&h2s->list);
4361 }
4362 goto end;
4363 }
4364
Willy Tarreauee573762018-12-04 15:25:57 +01004365 if (fsize > count)
4366 fsize = count;
4367
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004368 if (fsize > h2s->mws)
4369 fsize = h2s->mws; // >0
4370
4371 if (h2c->mfs && fsize > h2c->mfs)
4372 fsize = h2c->mfs; // >0
4373
4374 if (fsize + 9 > outbuf.size) {
4375 /* we have an opportunity for enlarging the too small
4376 * available space, let's try.
4377 * FIXME: is this really interesting to do? Maybe we'll
4378 * spend lots of time realigning instead of using two
4379 * frames.
4380 */
4381 if (b_space_wraps(&h2c->mbuf))
4382 goto realign_again;
4383 fsize = outbuf.size - 9;
4384
4385 if (fsize <= 0) {
4386 /* no need to send an empty frame here */
4387 h2c->flags |= H2_CF_MUX_MFULL;
4388 h2s->flags |= H2_SF_BLK_MROOM;
4389 goto end;
4390 }
4391 }
4392
4393 if (h2c->mws <= 0) {
4394 h2s->flags |= H2_SF_BLK_MFCTL;
4395 goto end;
4396 }
4397
4398 if (fsize > h2c->mws)
4399 fsize = h2c->mws;
4400
4401 /* now let's copy this this into the output buffer */
4402 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004403 h2s->mws -= fsize;
4404 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004405 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004406
4407 send_empty:
4408 /* update the frame's size */
4409 h2_set_frame_size(outbuf.area, fsize);
4410
4411 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4412 * meeting EOM. We should optimize this later.
4413 */
4414 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004415 total++; // EOM counts as one byte
4416 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004417 es_now = 1;
4418 }
4419
4420 if (es_now)
4421 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4422
4423 /* commit the H2 response */
4424 b_add(&h2c->mbuf, fsize + 9);
4425
4426 /* consume incoming HTX block, including EOM */
4427 total += fsize;
4428 if (fsize == bsize) {
4429 htx_remove_blk(htx, blk);
4430 if (fsize)
4431 goto new_frame;
4432 } else {
4433 /* we've truncated this block */
4434 htx_cut_data_blk(htx, blk, fsize);
4435 }
4436
4437 if (es_now) {
4438 if (h2s->st == H2_SS_OPEN)
4439 h2s->st = H2_SS_HLOC;
4440 else
4441 h2s_close(h2s);
4442
4443 h2s->flags |= H2_SF_ES_SENT;
4444 }
4445
4446 end:
4447 return total;
4448}
4449
Olivier Houchard6ff20392018-07-17 18:46:31 +02004450/* Called from the upper layer, to subscribe to events, such as being able to send */
4451static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4452{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004453 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004454 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004455 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004456
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004457 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004458 sw = param;
4459 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4460 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004461 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004462 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004463 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004464 event_type &= ~SUB_CAN_RECV;
4465 }
4466 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004467 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004468 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004469 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004470 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004471 h2s->send_wait = sw;
4472 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4473 if (h2s->flags & H2_SF_BLK_MFCTL)
4474 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4475 else
4476 LIST_ADDQ(&h2c->send_list, &h2s->list);
4477 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004478 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004479 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004480 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004481 if (event_type != 0)
4482 return -1;
4483 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004484
4485
4486}
4487
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004488static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4489{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004490 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004491 struct h2s *h2s = cs->ctx;
4492
4493 if (event_type & SUB_CAN_RECV) {
4494 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004495 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004496 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004497 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004498 }
4499 }
4500 if (event_type & SUB_CAN_SEND) {
4501 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004502 if (h2s->send_wait == sw) {
4503 LIST_DEL(&h2s->list);
4504 LIST_INIT(&h2s->list);
4505 sw->wait_reason &= ~SUB_CAN_SEND;
4506 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004507 }
4508 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004509 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4510 sw = param;
4511 if (h2s->send_wait == sw) {
4512 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4513 h2s->send_wait = NULL;
4514 }
4515 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004516 return 0;
4517}
4518
4519
Olivier Houchard511efea2018-08-16 15:30:32 +02004520/* Called from the upper layer, to receive data */
4521static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4522{
Olivier Houchard638b7992018-08-16 15:41:52 +02004523 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004524 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004525 struct htx *h2s_htx = NULL;
4526 struct htx *buf_htx = NULL;
4527 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004528 size_t ret = 0;
4529
4530 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004531 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4532 /* in HTX mode we ignore the count argument */
4533 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004534 if (htx_is_empty(h2s_htx)) {
4535 if (cs->flags & CS_FL_REOS)
4536 cs->flags |= CS_FL_EOS;
Willy Tarreau86724e22018-12-01 23:19:43 +01004537 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004538 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004539
4540 buf_htx = htx_from_buf(buf);
4541 count = htx_free_space(buf_htx);
4542
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004543 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004544
4545 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004546 htx_to_buf(buf_htx, buf);
4547 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004548 ret = htx_ret.ret;
4549 }
4550 else {
4551 ret = b_xfer(buf, &h2s->rxbuf, count);
4552 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004553
Olivier Houchard638b7992018-08-16 15:41:52 +02004554 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004555 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004556 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004557 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004558 if (cs->flags & CS_FL_REOS)
4559 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004560 if (b_size(&h2s->rxbuf)) {
4561 b_free(&h2s->rxbuf);
4562 offer_buffers(NULL, tasks_run_queue);
4563 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004564 }
4565
Willy Tarreau082f5592018-11-25 08:03:32 +01004566 if (ret && h2c->dsi == h2s->id) {
4567 /* demux is blocking on this stream's buffer */
4568 h2c->flags &= ~H2_CF_DEM_SFULL;
4569 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4570 if (h2_recv_allowed(h2c))
4571 tasklet_wakeup(h2c->wait_event.task);
4572 }
4573 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004574end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004575 return ret;
4576}
4577
Olivier Houchardd846c262018-10-19 17:24:29 +02004578static void h2_stop_senders(struct h2c *h2c)
4579{
4580 struct h2s *h2s, *h2s_back;
4581
4582 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4583 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4584 if (h2c->msi == h2s_id(h2s))
4585 continue;
4586 LIST_DEL(&h2s->list);
4587 LIST_INIT(&h2s->list);
4588 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4589 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4590 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4591 LIST_ADD(&h2c->send_list, &h2s->list);
4592 }
4593}
4594
Willy Tarreau62f52692017-10-08 23:01:42 +02004595/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004596static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004597{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004598 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004599 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004600 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004601 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004602 struct htx *htx;
4603 struct htx_blk *blk;
4604 enum htx_blk_type btype;
4605 uint32_t bsize;
4606 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004607
Olivier Houchardd846c262018-10-19 17:24:29 +02004608 if (h2s->send_wait) {
4609 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4610 h2s->send_wait = NULL;
4611 LIST_DEL(&h2s->list);
4612 LIST_INIT(&h2s->list);
4613 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004614 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4615 return 0;
4616
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004617 /* htx will be enough to decide if we're using HTX or legacy */
4618 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4619
Willy Tarreau0bad0432018-06-14 16:54:01 +02004620 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004621 h2s->flags |= H2_SF_OUTGOING_DATA;
4622
Willy Tarreau751f2d02018-10-05 09:35:00 +02004623 if (h2s->id == 0) {
4624 int32_t id = h2c_get_next_sid(h2s->h2c);
4625
4626 if (id < 0) {
4627 cs->ctx = NULL;
4628 cs->flags |= CS_FL_ERROR;
4629 h2s_destroy(h2s);
4630 return 0;
4631 }
4632
4633 eb32_delete(&h2s->by_id);
4634 h2s->by_id.key = h2s->id = id;
4635 h2s->h2c->max_id = id;
4636 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4637 }
4638
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004639 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004640 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4641 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004642 idx = htx_get_head(htx);
4643 blk = htx_get_blk(htx, idx);
4644 btype = htx_get_blk_type(blk);
4645 bsize = htx_get_blksz(blk);
4646
4647 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004648 case HTX_BLK_REQ_SL:
4649 /* start-line before headers */
4650 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4651 if (ret > 0) {
4652 total += ret;
4653 count -= ret;
4654 if (ret < bsize)
4655 goto done;
4656 }
4657 break;
4658
Willy Tarreau115e83b2018-12-01 19:17:53 +01004659 case HTX_BLK_RES_SL:
4660 /* start-line before headers */
4661 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4662 if (ret > 0) {
4663 total += ret;
4664 count -= ret;
4665 if (ret < bsize)
4666 goto done;
4667 }
4668 break;
4669
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004670 case HTX_BLK_DATA:
4671 case HTX_BLK_EOD:
4672 case HTX_BLK_EOM:
4673 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004674 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004675 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004676 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004677 total += ret;
4678 count -= ret;
4679 if (ret < bsize)
4680 goto done;
4681 }
4682 break;
4683
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004684 default:
4685 htx_remove_blk(htx, blk);
4686 total += bsize;
4687 count -= bsize;
4688 break;
4689 }
4690 }
4691 goto done;
4692 }
4693
4694 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004695 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004696 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004697 if (h2s->h2c->flags & H2_CF_IS_BACK)
4698 ret = -1;
4699 else
4700 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004701 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004702 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004703 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004704 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004705 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004706 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004707 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004708
Willy Tarreau5dd17352018-06-14 13:33:30 +02004709 if (unlikely((int)ret <= 0)) {
4710 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004711 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4712 break;
4713 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004714 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004715 total += count;
4716 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004717 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004718 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004719 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004720 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004721 cs->flags |= CS_FL_ERROR;
4722 break;
4723 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004724
4725 total += ret;
4726 count -= ret;
4727
4728 if (h2s->st >= H2_SS_ERROR)
4729 break;
4730
4731 if (h2s->flags & H2_SF_BLK_ANY)
4732 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004733 }
4734
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004735 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004736 if (h2s->st >= H2_SS_ERROR) {
4737 /* trim any possibly pending data after we close (extra CR-LF,
4738 * unprocessed trailers, abnormal extra data, ...)
4739 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004740 total += count;
4741 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004742 }
4743
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004744 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004745 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004746 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004747 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004748 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004749 }
4750
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004751 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004752 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004753 } else {
4754 b_del(buf, total);
4755 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004756
4757 /* The mux is full, cancel the pending tasks */
4758 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4759 (h2s->flags & H2_SF_BLK_MBUSY))
4760 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004761
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004762 /* If we're running HTX, and we read the whole buffer, then pretend
4763 * we read exactly what the caller specified, as with HTX the caller
4764 * will always give the buffer size, instead of the amount of data
4765 * available.
4766 */
4767 if (htx && !b_data(buf))
4768 total = orig_count;
4769
Olivier Houchard7505f942018-08-21 18:10:44 +02004770 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004771 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4772 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004773
Olivier Houchard7505f942018-08-21 18:10:44 +02004774 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004775 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004776}
4777
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004778/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004779static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004780{
4781 struct h2c *h2c = conn->mux_ctx;
4782 struct h2s *h2s;
4783 struct eb32_node *node;
4784 int fctl_cnt = 0;
4785 int send_cnt = 0;
4786 int tree_cnt = 0;
4787 int orph_cnt = 0;
4788
4789 if (!h2c)
4790 return;
4791
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004792 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004793 fctl_cnt++;
4794
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004795 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004796 send_cnt++;
4797
4798 node = eb32_first(&h2c->streams_by_id);
4799 while (node) {
4800 h2s = container_of(node, struct h2s, by_id);
4801 tree_cnt++;
4802 if (!h2s->cs)
4803 orph_cnt++;
4804 node = eb32_next(node);
4805 }
4806
Willy Tarreau616ac812018-07-24 14:12:42 +02004807 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4808 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4809 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4810 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4811 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4812 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004813}
Willy Tarreau62f52692017-10-08 23:01:42 +02004814
4815/*******************************************************/
4816/* functions below are dedicated to the config parsers */
4817/*******************************************************/
4818
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004819/* config parser for global "tune.h2.header-table-size" */
4820static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4821 struct proxy *defpx, const char *file, int line,
4822 char **err)
4823{
4824 if (too_many_args(1, args, err, NULL))
4825 return -1;
4826
4827 h2_settings_header_table_size = atoi(args[1]);
4828 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4829 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4830 return -1;
4831 }
4832 return 0;
4833}
Willy Tarreau62f52692017-10-08 23:01:42 +02004834
Willy Tarreaue6baec02017-07-27 11:45:11 +02004835/* config parser for global "tune.h2.initial-window-size" */
4836static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4837 struct proxy *defpx, const char *file, int line,
4838 char **err)
4839{
4840 if (too_many_args(1, args, err, NULL))
4841 return -1;
4842
4843 h2_settings_initial_window_size = atoi(args[1]);
4844 if (h2_settings_initial_window_size < 0) {
4845 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4846 return -1;
4847 }
4848 return 0;
4849}
4850
Willy Tarreau5242ef82017-07-27 11:47:28 +02004851/* config parser for global "tune.h2.max-concurrent-streams" */
4852static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4853 struct proxy *defpx, const char *file, int line,
4854 char **err)
4855{
4856 if (too_many_args(1, args, err, NULL))
4857 return -1;
4858
4859 h2_settings_max_concurrent_streams = atoi(args[1]);
4860 if (h2_settings_max_concurrent_streams < 0) {
4861 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4862 return -1;
4863 }
4864 return 0;
4865}
4866
Willy Tarreau62f52692017-10-08 23:01:42 +02004867
4868/****************************************/
4869/* MUX initialization and instanciation */
4870/***************************************/
4871
4872/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004873static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004874 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004875 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004876 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004877 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004878 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004879 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004880 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004881 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004882 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004883 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004884 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004885 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004886 .shutr = h2_shutr,
4887 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004888 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004889 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004890 .name = "H2",
4891};
4892
Christopher Faulet32f61c02018-04-10 14:33:41 +02004893/* PROTO selection : this mux registers PROTO token "h2" */
4894static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004895 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004896
Willy Tarreau0108d902018-11-25 19:14:37 +01004897INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4898
Willy Tarreauf8957272018-10-03 10:25:20 +02004899static struct mux_proto_list mux_proto_h2_htx =
4900 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4901
4902INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4903
Willy Tarreau62f52692017-10-08 23:01:42 +02004904/* config keyword parsers */
4905static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004906 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004907 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004908 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004909 { 0, NULL, NULL }
4910}};
4911
Willy Tarreau0108d902018-11-25 19:14:37 +01004912INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);