blob: ae872f1c682ed9a17592b558834387ed457f9b00 [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 Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020020#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010023#include <proto/http_htx.h>
24#include <proto/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 Tarreaufe20e5b2017-07-27 11:42:14 +0200206/* a few settings from the global section */
207static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200208static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200209static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200210
Willy Tarreau2a856182017-05-16 15:20:39 +0200211/* a dmumy closed stream */
212static const struct h2s *h2_closed_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100217 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
221/* and a dummy idle stream for use with any unannounced stream */
222static const struct h2s *h2_idle_stream = &(const struct h2s){
223 .cs = NULL,
224 .h2c = NULL,
225 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100226 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200227 .id = 0,
228};
229
Olivier Houchard9f6af332018-05-25 14:04:04 +0200230static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200231static int h2_send(struct h2c *h2c);
232static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200233static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200234static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100235static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200236static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100237static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200238static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreau751f2d02018-10-05 09:35:00 +0200239static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200240
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200241/*****************************************************/
242/* functions below are for dynamic buffer management */
243/*****************************************************/
244
Willy Tarreau315d8072017-12-10 22:17:57 +0100245/* indicates whether or not the we may call the h2_recv() function to attempt
246 * to receive data into the buffer and/or demux pending data. The condition is
247 * a bit complex due to some API limits for now. The rules are the following :
248 * - if an error or a shutdown was detected on the connection and the buffer
249 * is empty, we must not attempt to receive
250 * - if the demux buf failed to be allocated, we must not try to receive and
251 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100252 * - if no flag indicates a blocking condition, we may attempt to receive,
253 * regardless of whether the demux buffer is full or not, so that only
254 * de demux part decides whether or not to block. This is needed because
255 * the connection API indeed prevents us from re-enabling receipt that is
256 * already enabled in a polled state, so we must always immediately stop
257 * as soon as the demux can't proceed so as never to hit an end of read
258 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100259 * - otherwise must may not attempt
260 */
261static inline int h2_recv_allowed(const struct h2c *h2c)
262{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200263 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100264 (h2c->st0 >= H2_CS_ERROR ||
265 h2c->conn->flags & CO_FL_ERROR ||
266 conn_xprt_read0_pending(h2c->conn)))
267 return 0;
268
269 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100270 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100271 return 1;
272
273 return 0;
274}
275
Willy Tarreauf2101912018-07-19 10:11:38 +0200276/* returns true if the connection has too many conn_streams attached */
277static inline int h2_has_too_many_cs(const struct h2c *h2c)
278{
279 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
280}
281
Willy Tarreau44e973f2018-03-01 17:49:30 +0100282/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
283 * flags are used to figure what buffer was requested. It returns 1 if the
284 * allocation succeeds, in which case the connection is woken up, or 0 if it's
285 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200286 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100287static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200288{
289 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100290 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200293 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200294 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200295 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200296 return 1;
297 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200298
Willy Tarreau44e973f2018-03-01 17:49:30 +0100299 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
300 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301
302 if (h2c->flags & H2_CF_DEM_MROOM) {
303 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200304 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200305 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200306 }
Willy Tarreau14398122017-09-22 14:26:04 +0200307 return 1;
308 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100309
310 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
311 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200312 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200314 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200315 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100316 return 1;
317 }
318
Willy Tarreau14398122017-09-22 14:26:04 +0200319 return 0;
320}
321
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200322static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200323{
324 struct buffer *buf = NULL;
325
Willy Tarreau44e973f2018-03-01 17:49:30 +0100326 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
327 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
328 h2c->buf_wait.target = h2c;
329 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100331 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200333 __conn_xprt_stop_recv(h2c->conn);
334 }
335 return buf;
336}
337
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200338static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200339{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200340 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100341 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200342 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200343 }
344}
345
Olivier Houchardd540b362018-11-05 18:37:53 +0100346static int h2_avail_streams(struct connection *conn)
347{
348 struct h2c *h2c = conn->mux_ctx;
349
350 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
351}
352
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200353
Willy Tarreau62f52692017-10-08 23:01:42 +0200354/*****************************************************************/
355/* functions below are dedicated to the mux setup and management */
356/*****************************************************************/
357
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200358/* Initialize the mux once it's attached. For outgoing connections, the context
359 * is already initialized before installing the mux, so we detect incoming
360 * connections from the fact that the context is still NULL. Returns < 0 on
361 * error.
362 */
363static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364{
365 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100366 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200367
Willy Tarreaubafbe012017-11-24 17:34:44 +0100368 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200369 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200370 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371
Willy Tarreau01b44822018-10-03 14:26:37 +0200372 if (conn->mux_ctx) {
373 h2c->flags = H2_CF_IS_BACK;
374 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
375 if (tick_isset(prx->timeout.serverfin))
376 h2c->shut_timeout = prx->timeout.serverfin;
377 } else {
378 h2c->flags = H2_CF_NONE;
379 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
380 if (tick_isset(prx->timeout.clientfin))
381 h2c->shut_timeout = prx->timeout.clientfin;
382 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100383
Willy Tarreau0b37d652018-10-03 10:33:02 +0200384 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100385 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100386 if (tick_isset(h2c->timeout)) {
387 t = task_new(tid_bit);
388 if (!t)
389 goto fail;
390
391 h2c->task = t;
392 t->process = h2_timeout_task;
393 t->context = h2c;
394 t->expire = tick_add(now_ms, h2c->timeout);
395 }
Willy Tarreauea392822017-10-31 10:02:25 +0100396
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200397 h2c->wait_event.task = tasklet_new();
398 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200399 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200400 h2c->wait_event.task->process = h2_io_cb;
401 h2c->wait_event.task->context = h2c;
402 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200403
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
405 if (!h2c->ddht)
406 goto fail;
407
408 /* Initialise the context. */
409 h2c->st0 = H2_CS_PREFACE;
410 h2c->conn = conn;
411 h2c->max_id = -1;
412 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413 h2c->rcvd_c = 0;
414 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100415 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200416 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200417
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200418 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200419 h2c->dsi = -1;
420 h2c->msi = -1;
421 h2c->last_sid = -1;
422
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200423 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200424 h2c->miw = 65535; /* mux initial window size */
425 h2c->mws = 65535; /* mux window size */
426 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200427 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200428 LIST_INIT(&h2c->send_list);
429 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200430 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100431 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432
Willy Tarreau3f133572017-10-31 19:21:06 +0100433 if (t)
434 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100435
Willy Tarreau01b44822018-10-03 14:26:37 +0200436 if (h2c->flags & H2_CF_IS_BACK) {
437 /* FIXME: this is temporary, for outgoing connections we need
438 * to immediately allocate a stream until the code is modified
439 * so that the caller calls ->attach(). For now the outgoing cs
440 * is stored as conn->mux_ctx by the caller.
441 */
442 struct h2s *h2s;
443
444 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx);
445 if (!h2s)
446 goto fail_stream;
447 }
448
449 conn->mux_ctx = h2c;
450
Willy Tarreau0f383582018-10-03 14:22:21 +0200451 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200452 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200453 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200454 fail_stream:
455 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200456 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100457 if (t)
458 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200459 if (h2c->wait_event.task)
460 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100461 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200462 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463 return -1;
464}
465
Willy Tarreau751f2d02018-10-05 09:35:00 +0200466/* returns the next allocatable outgoing stream ID for the H2 connection, or
467 * -1 if no more is allocatable.
468 */
469static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
470{
471 int32_t id = (h2c->max_id + 1) | 1;
472 if (id & 0x80000000U)
473 id = -1;
474 return id;
475}
476
Willy Tarreau2373acc2017-10-12 17:35:14 +0200477/* returns the stream associated with id <id> or NULL if not found */
478static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
479{
480 struct eb32_node *node;
481
Willy Tarreau751f2d02018-10-05 09:35:00 +0200482 if (id == 0)
483 return (struct h2s *)h2_closed_stream;
484
Willy Tarreau2a856182017-05-16 15:20:39 +0200485 if (id > h2c->max_id)
486 return (struct h2s *)h2_idle_stream;
487
Willy Tarreau2373acc2017-10-12 17:35:14 +0200488 node = eb32_lookup(&h2c->streams_by_id, id);
489 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200490 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200491
492 return container_of(node, struct h2s, by_id);
493}
494
Willy Tarreau62f52692017-10-08 23:01:42 +0200495/* release function for a connection. This one should be called to free all
496 * resources allocated to the mux.
497 */
498static void h2_release(struct connection *conn)
499{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200500 struct h2c *h2c = conn->mux_ctx;
501
502 LIST_DEL(&conn->list);
503
504 if (h2c) {
505 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200506
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100507 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100508 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100509 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200510
Willy Tarreau44e973f2018-03-01 17:49:30 +0100511 h2_release_buf(h2c, &h2c->dbuf);
512 h2_release_buf(h2c, &h2c->mbuf);
513
Willy Tarreauea392822017-10-31 10:02:25 +0100514 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200515 h2c->task->context = NULL;
516 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100517 h2c->task = NULL;
518 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200519 if (h2c->wait_event.task)
520 tasklet_free(h2c->wait_event.task);
521 if (h2c->wait_event.wait_reason != 0)
522 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
523 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100524
Willy Tarreaubafbe012017-11-24 17:34:44 +0100525 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200526 }
527
528 conn->mux = NULL;
529 conn->mux_ctx = NULL;
530
531 conn_stop_tracking(conn);
532 conn_full_close(conn);
533 if (conn->destroy_cb)
534 conn->destroy_cb(conn);
535 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200536}
537
538
Willy Tarreau71681172017-10-23 14:39:06 +0200539/******************************************************/
540/* functions below are for the H2 protocol processing */
541/******************************************************/
542
543/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100544static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200545{
546 return h2s ? h2s->id : 0;
547}
548
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200549/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100550static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200551{
552 if (h2c->msi < 0)
553 return 0;
554
555 if (h2c->msi == h2s_id(h2s))
556 return 0;
557
558 return 1;
559}
560
Willy Tarreau741d6df2017-10-17 08:00:59 +0200561/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100562static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200563{
564 h2c->errcode = err;
565 h2c->st0 = H2_CS_ERROR;
566}
567
Willy Tarreau2e43f082017-10-17 08:03:59 +0200568/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100569static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200570{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200571 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200572 h2s->errcode = err;
573 h2s->st = H2_SS_ERROR;
574 if (h2s->cs)
575 h2s->cs->flags |= CS_FL_ERROR;
576 }
577}
578
Willy Tarreaue4820742017-07-27 13:37:23 +0200579/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200581{
582 uint8_t *out = frame;
583
584 *out = len >> 16;
585 write_n16(out + 1, len);
586}
587
Willy Tarreau54c15062017-10-10 17:10:03 +0200588/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
589 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
590 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200591 * available in the buffer's input prior to calling this function. The buffer
592 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200593 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100594static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200595 const struct buffer *b, int o)
596{
Willy Tarreau591d4452018-06-15 17:21:00 +0200597 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 +0200598}
599
Willy Tarreau1f094672017-11-20 21:27:45 +0100600static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200601{
Willy Tarreau591d4452018-06-15 17:21:00 +0200602 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200603}
604
Willy Tarreau1f094672017-11-20 21:27:45 +0100605static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200606{
Willy Tarreau591d4452018-06-15 17:21:00 +0200607 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200608}
609
Willy Tarreau1f094672017-11-20 21:27:45 +0100610static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200611{
Willy Tarreau591d4452018-06-15 17:21:00 +0200612 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200613}
614
615
Willy Tarreau715d5312017-07-11 15:20:24 +0200616/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
617 * is not obvious. It turns out that H2 headers are neither aligned nor do they
618 * use regular sizes. And to add to the trouble, the buffer may wrap so each
619 * byte read must be checked. The header is formed like this :
620 *
621 * b0 b1 b2 b3 b4 b5..b8
622 * +----------+---------+--------+----+----+----------------------+
623 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
624 * +----------+---------+--------+----+----+----------------------+
625 *
626 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
627 * we get the sid properly aligned and ordered, and 16 bits of len properly
628 * ordered as well. The type and flags can be extracted using bit shifts from
629 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200630 * Returns zero if some bytes are missing, otherwise non-zero on success. The
631 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200632 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100633static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200634{
635 uint64_t w;
636
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200637 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200638 return 0;
639
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200640 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200641 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200642 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
643 h->ff = w >> 32;
644 h->ft = w >> 40;
645 h->len += w >> 48;
646 return 1;
647}
648
649/* skip the next 9 bytes corresponding to the frame header possibly parsed by
650 * h2_peek_frame_hdr() above.
651 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100652static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200653{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200654 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200655}
656
657/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100658static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200659{
660 int ret;
661
662 ret = h2_peek_frame_hdr(b, h);
663 if (ret > 0)
664 h2_skip_frame_hdr(b);
665 return ret;
666}
667
Willy Tarreau00dd0782018-03-01 16:31:34 +0100668/* marks stream <h2s> as CLOSED and decrement the number of active streams for
669 * its connection if the stream was not yet closed. Please use this exclusively
670 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100671 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100672static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100673{
674 if (h2s->st != H2_SS_CLOSED)
675 h2s->h2c->nb_streams--;
676 h2s->st = H2_SS_CLOSED;
677}
678
Willy Tarreau71049cc2018-03-28 13:56:39 +0200679/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
680static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100681{
682 h2s_close(h2s);
683 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200684 if (b_size(&h2s->rxbuf)) {
685 b_free(&h2s->rxbuf);
686 offer_buffers(NULL, tasks_run_queue);
687 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200688 if (h2s->send_wait != NULL)
689 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
690 if (h2s->recv_wait != NULL)
691 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
692 /* There's no need to explicitely call unsubscribe here, the only
693 * reference left would be in the h2c send_list/fctl_list, and if
694 * we're in it, we're getting out anyway
695 */
696 LIST_DEL(&h2s->list);
697 LIST_INIT(&h2s->list);
698 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100699 pool_free(pool_head_h2s, h2s);
700}
701
Willy Tarreaua8e49542018-10-03 18:53:55 +0200702/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
703 * stream tree. In case of error, nothing is added and NULL is returned. The
704 * causes of errors can be any failed memory allocation. The caller is
705 * responsible for checking if the connection may support an extra stream
706 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200707 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200708static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200709{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200710 struct h2s *h2s;
711
Willy Tarreaubafbe012017-11-24 17:34:44 +0100712 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200713 if (!h2s)
714 goto out;
715
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200716 h2s->wait_event.task = tasklet_new();
717 if (!h2s->wait_event.task) {
718 pool_free(pool_head_h2s, h2s);
719 goto out;
720 }
721 h2s->send_wait = NULL;
722 h2s->recv_wait = NULL;
723 h2s->wait_event.task->process = h2_deferred_shut;
724 h2s->wait_event.task->context = h2s;
725 h2s->wait_event.handle = NULL;
726 h2s->wait_event.wait_reason = 0;
727 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200728 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200729 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200730 h2s->mws = h2c->miw;
731 h2s->flags = H2_SF_NONE;
732 h2s->errcode = H2_ERR_NO_ERROR;
733 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200734 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200735 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200736
737 if (h2c->flags & H2_CF_IS_BACK) {
738 h1m_init_req(&h2s->h1m);
739 h2s->h1m.err_pos = -1; // don't care about errors on the request path
740 h2s->h1m.flags |= H1_MF_TOLOWER;
741 } else {
742 h1m_init_res(&h2s->h1m);
743 h2s->h1m.err_pos = -1; // don't care about errors on the response path
744 h2s->h1m.flags |= H1_MF_TOLOWER;
745 }
746
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200747 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200748 if (id > 0)
749 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200750
751 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100752 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200753
754 return h2s;
755
756 out_free_h2s:
757 pool_free(pool_head_h2s, h2s);
758 out:
759 return NULL;
760}
761
762/* creates a new stream <id> on the h2c connection and returns it, or NULL in
763 * case of memory allocation error.
764 */
765static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
766{
767 struct session *sess = h2c->conn->owner;
768 struct conn_stream *cs;
769 struct h2s *h2s;
770
771 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
772 goto out;
773
774 h2s = h2s_new(h2c, id);
775 if (!h2s)
776 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200777
778 cs = cs_new(h2c->conn);
779 if (!cs)
780 goto out_close;
781
782 h2s->cs = cs;
783 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200784 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200785
786 if (stream_create_from_cs(cs) < 0)
787 goto out_free_cs;
788
Willy Tarreau590a0512018-09-05 11:56:48 +0200789 /* We want the accept date presented to the next stream to be the one
790 * we have now, the handshake time to be null (since the next stream
791 * is not delayed by a handshake), and the idle time to count since
792 * right now.
793 */
794 sess->accept_date = date;
795 sess->tv_accept = now;
796 sess->t_handshake = 0;
797
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200798 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200799 if (h2_has_too_many_cs(h2c))
800 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200801 return h2s;
802
803 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200804 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200805 cs_free(cs);
806 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200807 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200808 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200809 sess_log(sess);
810 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200811}
812
Willy Tarreau751f2d02018-10-05 09:35:00 +0200813/* allocates a new stream associated to conn_stream <cs> on the h2c connection
814 * and returns it, or NULL in case of memory allocation error or if the highest
815 * possible stream ID was reached.
816 */
817static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs)
818{
819 struct h2s *h2s = NULL;
820
821 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
822 goto out;
823
824 /* Defer choosing the ID until we send the first message to create the stream */
825 h2s = h2s_new(h2c, 0);
826 if (!h2s)
827 goto out;
828
829 h2s->cs = cs;
830 cs->ctx = h2s;
831 h2c->nb_cs++;
832
Willy Tarreau751f2d02018-10-05 09:35:00 +0200833 out:
834 return h2s;
835}
836
Willy Tarreaube5b7152017-09-25 16:25:39 +0200837/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
838 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
839 * the various settings codes.
840 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200841static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200842{
843 struct buffer *res;
844 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200845 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200846 int ret;
847
848 if (h2c_mux_busy(h2c, NULL)) {
849 h2c->flags |= H2_CF_DEM_MBUSY;
850 return 0;
851 }
852
Willy Tarreau44e973f2018-03-01 17:49:30 +0100853 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200854 if (!res) {
855 h2c->flags |= H2_CF_MUX_MALLOC;
856 h2c->flags |= H2_CF_DEM_MROOM;
857 return 0;
858 }
859
860 chunk_init(&buf, buf_data, sizeof(buf_data));
861 chunk_memcpy(&buf,
862 "\x00\x00\x00" /* length : 0 for now */
863 "\x04\x00" /* type : 4 (settings), flags : 0 */
864 "\x00\x00\x00\x00", /* stream ID : 0 */
865 9);
866
867 if (h2_settings_header_table_size != 4096) {
868 char str[6] = "\x00\x01"; /* header_table_size */
869
870 write_n32(str + 2, h2_settings_header_table_size);
871 chunk_memcat(&buf, str, 6);
872 }
873
874 if (h2_settings_initial_window_size != 65535) {
875 char str[6] = "\x00\x04"; /* initial_window_size */
876
877 write_n32(str + 2, h2_settings_initial_window_size);
878 chunk_memcat(&buf, str, 6);
879 }
880
881 if (h2_settings_max_concurrent_streams != 0) {
882 char str[6] = "\x00\x03"; /* max_concurrent_streams */
883
884 /* Note: 0 means "unlimited" for haproxy's config but not for
885 * the protocol, so never send this value!
886 */
887 write_n32(str + 2, h2_settings_max_concurrent_streams);
888 chunk_memcat(&buf, str, 6);
889 }
890
891 if (global.tune.bufsize != 16384) {
892 char str[6] = "\x00\x05"; /* max_frame_size */
893
894 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
895 * match bufsize - rewrite size, but at the moment it seems
896 * that clients don't take care of it.
897 */
898 write_n32(str + 2, global.tune.bufsize);
899 chunk_memcat(&buf, str, 6);
900 }
901
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200902 h2_set_frame_size(buf.area, buf.data - 9);
903 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200904 if (unlikely(ret <= 0)) {
905 if (!ret) {
906 h2c->flags |= H2_CF_MUX_MFULL;
907 h2c->flags |= H2_CF_DEM_MROOM;
908 return 0;
909 }
910 else {
911 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
912 return 0;
913 }
914 }
915 return ret;
916}
917
Willy Tarreau52eed752017-09-22 15:05:09 +0200918/* Try to receive a connection preface, then upon success try to send our
919 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
920 * missing data. It may return an error in h2c.
921 */
922static int h2c_frt_recv_preface(struct h2c *h2c)
923{
924 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200925 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200926
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200927 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200928
929 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200930 if (ret1 < 0)
931 sess_log(h2c->conn->owner);
932
Willy Tarreau52eed752017-09-22 15:05:09 +0200933 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
934 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
935 return 0;
936 }
937
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200938 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200939 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200940 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200941
Willy Tarreaube5b7152017-09-25 16:25:39 +0200942 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200943}
944
Willy Tarreau01b44822018-10-03 14:26:37 +0200945/* Try to send a connection preface, then upon success try to send our
946 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
947 * missing data. It may return an error in h2c.
948 */
949static int h2c_bck_send_preface(struct h2c *h2c)
950{
951 struct buffer *res;
952
953 if (h2c_mux_busy(h2c, NULL)) {
954 h2c->flags |= H2_CF_DEM_MBUSY;
955 return 0;
956 }
957
958 res = h2_get_buf(h2c, &h2c->mbuf);
959 if (!res) {
960 h2c->flags |= H2_CF_MUX_MALLOC;
961 h2c->flags |= H2_CF_DEM_MROOM;
962 return 0;
963 }
964
965 if (!b_data(res)) {
966 /* preface not yet sent */
967 b_istput(res, ist(H2_CONN_PREFACE));
968 }
969
970 return h2c_send_settings(h2c);
971}
972
Willy Tarreau081d4722017-05-16 21:51:05 +0200973/* try to send a GOAWAY frame on the connection to report an error or a graceful
974 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
975 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
976 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
977 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
978 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
979 * on unrecoverable failure. It will not attempt to send one again in this last
980 * case so that it is safe to use h2c_error() to report such errors.
981 */
982static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
983{
984 struct buffer *res;
985 char str[17];
986 int ret;
987
988 if (h2c->flags & H2_CF_GOAWAY_FAILED)
989 return 1; // claim that it worked
990
991 if (h2c_mux_busy(h2c, h2s)) {
992 if (h2s)
993 h2s->flags |= H2_SF_BLK_MBUSY;
994 else
995 h2c->flags |= H2_CF_DEM_MBUSY;
996 return 0;
997 }
998
Willy Tarreau44e973f2018-03-01 17:49:30 +0100999 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001000 if (!res) {
1001 h2c->flags |= H2_CF_MUX_MALLOC;
1002 if (h2s)
1003 h2s->flags |= H2_SF_BLK_MROOM;
1004 else
1005 h2c->flags |= H2_CF_DEM_MROOM;
1006 return 0;
1007 }
1008
1009 /* len: 8, type: 7, flags: none, sid: 0 */
1010 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1011
1012 if (h2c->last_sid < 0)
1013 h2c->last_sid = h2c->max_id;
1014
1015 write_n32(str + 9, h2c->last_sid);
1016 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001017 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001018 if (unlikely(ret <= 0)) {
1019 if (!ret) {
1020 h2c->flags |= H2_CF_MUX_MFULL;
1021 if (h2s)
1022 h2s->flags |= H2_SF_BLK_MROOM;
1023 else
1024 h2c->flags |= H2_CF_DEM_MROOM;
1025 return 0;
1026 }
1027 else {
1028 /* we cannot report this error using GOAWAY, so we mark
1029 * it and claim a success.
1030 */
1031 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1032 h2c->flags |= H2_CF_GOAWAY_FAILED;
1033 return 1;
1034 }
1035 }
1036 h2c->flags |= H2_CF_GOAWAY_SENT;
1037 return ret;
1038}
1039
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001040/* Try to send an RST_STREAM frame on the connection for the indicated stream
1041 * during mux operations. This stream must be valid and cannot be closed
1042 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1043 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1044 * not yet.
1045 *
1046 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1047 * to write the message, it subscribes the stream to future notifications.
1048 */
1049static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1050{
1051 struct buffer *res;
1052 char str[13];
1053 int ret;
1054
1055 if (!h2s || h2s->st == H2_SS_CLOSED)
1056 return 1;
1057
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001058 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1059 * RST_STREAM in response to a RST_STREAM frame.
1060 */
1061 if (h2c->dft == H2_FT_RST_STREAM) {
1062 ret = 1;
1063 goto ignore;
1064 }
1065
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001066 if (h2c_mux_busy(h2c, h2s)) {
1067 h2s->flags |= H2_SF_BLK_MBUSY;
1068 return 0;
1069 }
1070
Willy Tarreau44e973f2018-03-01 17:49:30 +01001071 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001072 if (!res) {
1073 h2c->flags |= H2_CF_MUX_MALLOC;
1074 h2s->flags |= H2_SF_BLK_MROOM;
1075 return 0;
1076 }
1077
1078 /* len: 4, type: 3, flags: none */
1079 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1080 write_n32(str + 5, h2s->id);
1081 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001082 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001083
1084 if (unlikely(ret <= 0)) {
1085 if (!ret) {
1086 h2c->flags |= H2_CF_MUX_MFULL;
1087 h2s->flags |= H2_SF_BLK_MROOM;
1088 return 0;
1089 }
1090 else {
1091 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1092 return 0;
1093 }
1094 }
1095
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001096 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001097 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001098 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001099 return ret;
1100}
1101
1102/* Try to send an RST_STREAM frame on the connection for the stream being
1103 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1104 * error code unless the stream's state already is IDLE or CLOSED in which
1105 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1106 * it was not yet.
1107 *
1108 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1109 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001110 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001111 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001112 */
1113static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1114{
1115 struct buffer *res;
1116 char str[13];
1117 int ret;
1118
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001119 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1120 * RST_STREAM in response to a RST_STREAM frame.
1121 */
1122 if (h2c->dft == H2_FT_RST_STREAM) {
1123 ret = 1;
1124 goto ignore;
1125 }
1126
Willy Tarreau27a84c92017-10-17 08:10:17 +02001127 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001128 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001129 return 0;
1130 }
1131
Willy Tarreau44e973f2018-03-01 17:49:30 +01001132 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001133 if (!res) {
1134 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001135 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001136 return 0;
1137 }
1138
1139 /* len: 4, type: 3, flags: none */
1140 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001141
Willy Tarreau27a84c92017-10-17 08:10:17 +02001142 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001143 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001144 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001145
Willy Tarreau27a84c92017-10-17 08:10:17 +02001146 if (unlikely(ret <= 0)) {
1147 if (!ret) {
1148 h2c->flags |= H2_CF_MUX_MFULL;
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 else {
1153 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1154 return 0;
1155 }
1156 }
1157
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001158 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001159 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001160 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001161 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001162 }
1163
Willy Tarreau27a84c92017-10-17 08:10:17 +02001164 return ret;
1165}
1166
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001167/* try to send an empty DATA frame with the ES flag set to notify about the
1168 * end of stream and match a shutdown(write). If an ES was already sent as
1169 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1170 * on success or zero if nothing was done. In case of lack of room to write the
1171 * message, it subscribes the requesting stream to future notifications.
1172 */
1173static int h2_send_empty_data_es(struct h2s *h2s)
1174{
1175 struct h2c *h2c = h2s->h2c;
1176 struct buffer *res;
1177 char str[9];
1178 int ret;
1179
Willy Tarreau721c9742017-11-07 11:05:42 +01001180 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001181 return 1;
1182
1183 if (h2c_mux_busy(h2c, h2s)) {
1184 h2s->flags |= H2_SF_BLK_MBUSY;
1185 return 0;
1186 }
1187
Willy Tarreau44e973f2018-03-01 17:49:30 +01001188 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001189 if (!res) {
1190 h2c->flags |= H2_CF_MUX_MALLOC;
1191 h2s->flags |= H2_SF_BLK_MROOM;
1192 return 0;
1193 }
1194
1195 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1196 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1197 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001198 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001199 if (likely(ret > 0)) {
1200 h2s->flags |= H2_SF_ES_SENT;
1201 }
1202 else if (!ret) {
1203 h2c->flags |= H2_CF_MUX_MFULL;
1204 h2s->flags |= H2_SF_BLK_MROOM;
1205 return 0;
1206 }
1207 else {
1208 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1209 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001210 }
1211 return ret;
1212}
1213
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001214/* wake the streams attached to the connection, whose id is greater than <last>,
1215 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001216 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1217 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001218 */
1219static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1220{
1221 struct eb32_node *node;
1222 struct h2s *h2s;
1223
1224 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1225 flags |= CS_FL_ERROR;
1226
1227 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001228 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001229
1230 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1231 while (node) {
1232 h2s = container_of(node, struct h2s, by_id);
1233 if (h2s->id <= last)
1234 break;
1235 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001236
1237 if (!h2s->cs) {
1238 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001239 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001240 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001241 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001242
1243 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001244 if (h2s->recv_wait) {
1245 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001246 sw->wait_reason &= ~SUB_CAN_RECV;
1247 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001248 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001249 } else if (h2s->cs->data_cb->wake != NULL)
1250 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001251
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001252 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1253 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001254 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001255 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001256 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001257 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001258 }
1259}
1260
Willy Tarreau3421aba2017-07-27 15:41:03 +02001261/* Increase all streams' outgoing window size by the difference passed in
1262 * argument. This is needed upon receipt of the settings frame if the initial
1263 * window size is different. The difference may be negative and the resulting
1264 * window size as well, for the time it takes to receive some window updates.
1265 */
1266static void h2c_update_all_ws(struct h2c *h2c, int diff)
1267{
1268 struct h2s *h2s;
1269 struct eb32_node *node;
1270
1271 if (!diff)
1272 return;
1273
1274 node = eb32_first(&h2c->streams_by_id);
1275 while (node) {
1276 h2s = container_of(node, struct h2s, by_id);
1277 h2s->mws += diff;
1278 node = eb32_next(node);
1279 }
1280}
1281
1282/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1283 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1284 * return an error in h2c. Described in RFC7540#6.5.
1285 */
1286static int h2c_handle_settings(struct h2c *h2c)
1287{
1288 unsigned int offset;
1289 int error;
1290
1291 if (h2c->dff & H2_F_SETTINGS_ACK) {
1292 if (h2c->dfl) {
1293 error = H2_ERR_FRAME_SIZE_ERROR;
1294 goto fail;
1295 }
1296 return 1;
1297 }
1298
1299 if (h2c->dsi != 0) {
1300 error = H2_ERR_PROTOCOL_ERROR;
1301 goto fail;
1302 }
1303
1304 if (h2c->dfl % 6) {
1305 error = H2_ERR_FRAME_SIZE_ERROR;
1306 goto fail;
1307 }
1308
1309 /* that's the limit we can process */
1310 if (h2c->dfl > global.tune.bufsize) {
1311 error = H2_ERR_FRAME_SIZE_ERROR;
1312 goto fail;
1313 }
1314
1315 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001316 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001317 return 0;
1318
1319 /* parse the frame */
1320 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001321 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1322 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001323
1324 switch (type) {
1325 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1326 /* we need to update all existing streams with the
1327 * difference from the previous iws.
1328 */
1329 if (arg < 0) { // RFC7540#6.5.2
1330 error = H2_ERR_FLOW_CONTROL_ERROR;
1331 goto fail;
1332 }
1333 h2c_update_all_ws(h2c, arg - h2c->miw);
1334 h2c->miw = arg;
1335 break;
1336 case H2_SETTINGS_MAX_FRAME_SIZE:
1337 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1338 error = H2_ERR_PROTOCOL_ERROR;
1339 goto fail;
1340 }
1341 h2c->mfs = arg;
1342 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001343 case H2_SETTINGS_ENABLE_PUSH:
1344 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1345 error = H2_ERR_PROTOCOL_ERROR;
1346 goto fail;
1347 }
1348 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001349 }
1350 }
1351
1352 /* need to ACK this frame now */
1353 h2c->st0 = H2_CS_FRAME_A;
1354 return 1;
1355 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001356 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001357 h2c_error(h2c, error);
1358 return 0;
1359}
1360
1361/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1362 * success or one of the h2_status values.
1363 */
1364static int h2c_ack_settings(struct h2c *h2c)
1365{
1366 struct buffer *res;
1367 char str[9];
1368 int ret = -1;
1369
1370 if (h2c_mux_busy(h2c, NULL)) {
1371 h2c->flags |= H2_CF_DEM_MBUSY;
1372 return 0;
1373 }
1374
Willy Tarreau44e973f2018-03-01 17:49:30 +01001375 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001376 if (!res) {
1377 h2c->flags |= H2_CF_MUX_MALLOC;
1378 h2c->flags |= H2_CF_DEM_MROOM;
1379 return 0;
1380 }
1381
1382 memcpy(str,
1383 "\x00\x00\x00" /* length : 0 (no data) */
1384 "\x04" "\x01" /* type : 4, flags : ACK */
1385 "\x00\x00\x00\x00" /* stream ID */, 9);
1386
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001387 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001388 if (unlikely(ret <= 0)) {
1389 if (!ret) {
1390 h2c->flags |= H2_CF_MUX_MFULL;
1391 h2c->flags |= H2_CF_DEM_MROOM;
1392 return 0;
1393 }
1394 else {
1395 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1396 return 0;
1397 }
1398 }
1399 return ret;
1400}
1401
Willy Tarreaucf68c782017-10-10 17:11:41 +02001402/* processes a PING frame and schedules an ACK if needed. The caller must pass
1403 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1404 * missing data. It may return an error in h2c.
1405 */
1406static int h2c_handle_ping(struct h2c *h2c)
1407{
1408 /* frame length must be exactly 8 */
1409 if (h2c->dfl != 8) {
1410 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1411 return 0;
1412 }
1413
1414 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001415 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001416 h2c->st0 = H2_CS_FRAME_A;
1417 return 1;
1418}
1419
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001420/* Try to send a window update for stream id <sid> and value <increment>.
1421 * Returns > 0 on success or zero on missing room or failure. It may return an
1422 * error in h2c.
1423 */
1424static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1425{
1426 struct buffer *res;
1427 char str[13];
1428 int ret = -1;
1429
1430 if (h2c_mux_busy(h2c, NULL)) {
1431 h2c->flags |= H2_CF_DEM_MBUSY;
1432 return 0;
1433 }
1434
Willy Tarreau44e973f2018-03-01 17:49:30 +01001435 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001436 if (!res) {
1437 h2c->flags |= H2_CF_MUX_MALLOC;
1438 h2c->flags |= H2_CF_DEM_MROOM;
1439 return 0;
1440 }
1441
1442 /* length: 4, type: 8, flags: none */
1443 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1444 write_n32(str + 5, sid);
1445 write_n32(str + 9, increment);
1446
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001447 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001448
1449 if (unlikely(ret <= 0)) {
1450 if (!ret) {
1451 h2c->flags |= H2_CF_MUX_MFULL;
1452 h2c->flags |= H2_CF_DEM_MROOM;
1453 return 0;
1454 }
1455 else {
1456 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1457 return 0;
1458 }
1459 }
1460 return ret;
1461}
1462
1463/* try to send pending window update for the connection. It's safe to call it
1464 * with no pending updates. Returns > 0 on success or zero on missing room or
1465 * failure. It may return an error in h2c.
1466 */
1467static int h2c_send_conn_wu(struct h2c *h2c)
1468{
1469 int ret = 1;
1470
1471 if (h2c->rcvd_c <= 0)
1472 return 1;
1473
1474 /* send WU for the connection */
1475 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1476 if (ret > 0)
1477 h2c->rcvd_c = 0;
1478
1479 return ret;
1480}
1481
1482/* try to send pending window update for the current dmux stream. It's safe to
1483 * call it with no pending updates. Returns > 0 on success or zero on missing
1484 * room or failure. It may return an error in h2c.
1485 */
1486static int h2c_send_strm_wu(struct h2c *h2c)
1487{
1488 int ret = 1;
1489
1490 if (h2c->rcvd_s <= 0)
1491 return 1;
1492
1493 /* send WU for the stream */
1494 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1495 if (ret > 0)
1496 h2c->rcvd_s = 0;
1497
1498 return ret;
1499}
1500
Willy Tarreaucf68c782017-10-10 17:11:41 +02001501/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1502 * success, 0 on missing data or one of the h2_status values.
1503 */
1504static int h2c_ack_ping(struct h2c *h2c)
1505{
1506 struct buffer *res;
1507 char str[17];
1508 int ret = -1;
1509
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001510 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001511 return 0;
1512
1513 if (h2c_mux_busy(h2c, NULL)) {
1514 h2c->flags |= H2_CF_DEM_MBUSY;
1515 return 0;
1516 }
1517
Willy Tarreau44e973f2018-03-01 17:49:30 +01001518 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001519 if (!res) {
1520 h2c->flags |= H2_CF_MUX_MALLOC;
1521 h2c->flags |= H2_CF_DEM_MROOM;
1522 return 0;
1523 }
1524
1525 memcpy(str,
1526 "\x00\x00\x08" /* length : 8 (same payload) */
1527 "\x06" "\x01" /* type : 6, flags : ACK */
1528 "\x00\x00\x00\x00" /* stream ID */, 9);
1529
1530 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001531 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001532
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001533 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001534 if (unlikely(ret <= 0)) {
1535 if (!ret) {
1536 h2c->flags |= H2_CF_MUX_MFULL;
1537 h2c->flags |= H2_CF_DEM_MROOM;
1538 return 0;
1539 }
1540 else {
1541 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1542 return 0;
1543 }
1544 }
1545 return ret;
1546}
1547
Willy Tarreau26f95952017-07-27 17:18:30 +02001548/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1549 * Returns > 0 on success or zero on missing data. It may return an error in
1550 * h2c or h2s. Described in RFC7540#6.9.
1551 */
1552static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1553{
1554 int32_t inc;
1555 int error;
1556
1557 if (h2c->dfl != 4) {
1558 error = H2_ERR_FRAME_SIZE_ERROR;
1559 goto conn_err;
1560 }
1561
1562 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001563 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001564 return 0;
1565
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001566 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001567
1568 if (h2c->dsi != 0) {
1569 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001570
1571 /* it's not an error to receive WU on a closed stream */
1572 if (h2s->st == H2_SS_CLOSED)
1573 return 1;
1574
1575 if (!inc) {
1576 error = H2_ERR_PROTOCOL_ERROR;
1577 goto strm_err;
1578 }
1579
1580 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1581 error = H2_ERR_FLOW_CONTROL_ERROR;
1582 goto strm_err;
1583 }
1584
1585 h2s->mws += inc;
1586 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1587 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001588 if (h2s->send_wait)
1589 LIST_ADDQ(&h2c->send_list, &h2s->list);
1590
Willy Tarreau26f95952017-07-27 17:18:30 +02001591 }
1592 }
1593 else {
1594 /* connection window update */
1595 if (!inc) {
1596 error = H2_ERR_PROTOCOL_ERROR;
1597 goto conn_err;
1598 }
1599
1600 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1601 error = H2_ERR_FLOW_CONTROL_ERROR;
1602 goto conn_err;
1603 }
1604
1605 h2c->mws += inc;
1606 }
1607
1608 return 1;
1609
1610 conn_err:
1611 h2c_error(h2c, error);
1612 return 0;
1613
1614 strm_err:
1615 if (h2s) {
1616 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001617 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001618 }
1619 else
1620 h2c_error(h2c, error);
1621 return 0;
1622}
1623
Willy Tarreaue96b0922017-10-30 00:28:29 +01001624/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1625 * the last ID. Returns > 0 on success or zero on missing data. It may return
1626 * an error in h2c. Described in RFC7540#6.8.
1627 */
1628static int h2c_handle_goaway(struct h2c *h2c)
1629{
1630 int error;
1631 int last;
1632
1633 if (h2c->dsi != 0) {
1634 error = H2_ERR_PROTOCOL_ERROR;
1635 goto conn_err;
1636 }
1637
1638 if (h2c->dfl < 8) {
1639 error = H2_ERR_FRAME_SIZE_ERROR;
1640 goto conn_err;
1641 }
1642
1643 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001644 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001645 return 0;
1646
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001647 last = h2_get_n32(&h2c->dbuf, 0);
1648 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001649 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001650 if (h2c->last_sid < 0)
1651 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001652 return 1;
1653
1654 conn_err:
1655 h2c_error(h2c, error);
1656 return 0;
1657}
1658
Willy Tarreau92153fc2017-12-03 19:46:19 +01001659/* processes a PRIORITY frame, and either skips it or rejects if it is
1660 * invalid. Returns > 0 on success or zero on missing data. It may return
1661 * an error in h2c. Described in RFC7540#6.3.
1662 */
1663static int h2c_handle_priority(struct h2c *h2c)
1664{
1665 int error;
1666
1667 if (h2c->dsi == 0) {
1668 error = H2_ERR_PROTOCOL_ERROR;
1669 goto conn_err;
1670 }
1671
1672 if (h2c->dfl != 5) {
1673 error = H2_ERR_FRAME_SIZE_ERROR;
1674 goto conn_err;
1675 }
1676
1677 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001678 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001679 return 0;
1680
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001681 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001682 /* 7540#5.3 : can't depend on itself */
1683 error = H2_ERR_PROTOCOL_ERROR;
1684 goto conn_err;
1685 }
1686 return 1;
1687
1688 conn_err:
1689 h2c_error(h2c, error);
1690 return 0;
1691}
1692
Willy Tarreaucd234e92017-08-18 10:59:39 +02001693/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1694 * Returns > 0 on success or zero on missing data. It may return an error in
1695 * h2c. Described in RFC7540#6.4.
1696 */
1697static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1698{
1699 int error;
1700
1701 if (h2c->dsi == 0) {
1702 error = H2_ERR_PROTOCOL_ERROR;
1703 goto conn_err;
1704 }
1705
Willy Tarreaucd234e92017-08-18 10:59:39 +02001706 if (h2c->dfl != 4) {
1707 error = H2_ERR_FRAME_SIZE_ERROR;
1708 goto conn_err;
1709 }
1710
1711 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001712 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001713 return 0;
1714
1715 /* late RST, already handled */
1716 if (h2s->st == H2_SS_CLOSED)
1717 return 1;
1718
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001720 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001721
1722 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001723 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001724 if (h2s->recv_wait) {
1725 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001726
1727 sw->wait_reason &= ~SUB_CAN_RECV;
1728 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001729 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001730 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001731 }
1732
1733 h2s->flags |= H2_SF_RST_RCVD;
1734 return 1;
1735
1736 conn_err:
1737 h2c_error(h2c, error);
1738 return 0;
1739}
1740
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001741/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1742 * It may return an error in h2c or h2s. The caller must consider that the
1743 * return value is the new h2s in case one was allocated (most common case).
1744 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001745 * errors here are reported as connection errors since it's impossible to
1746 * recover from such errors after the compression context has been altered.
1747 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001748static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001749{
1750 int error;
1751
1752 if (!h2c->dfl) {
1753 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001754 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001755 goto strm_err;
1756 }
1757
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001758 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001759 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001760
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001761 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001762 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001763
Willy Tarreauf2101912018-07-19 10:11:38 +02001764 if (h2c->flags & H2_CF_DEM_TOOMANY)
1765 return 0; // too many cs still present
1766
Willy Tarreau13278b42017-10-13 19:23:14 +02001767 /* now either the frame is complete or the buffer is complete */
1768 if (h2s->st != H2_SS_IDLE) {
1769 /* FIXME: stream already exists, this is only allowed for
1770 * trailers (not supported for now).
1771 */
1772 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001773 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001774 goto conn_err;
1775 }
1776 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1777 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1778 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001779 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001780 goto conn_err;
1781 }
1782
Willy Tarreau22de8d32018-09-05 19:55:58 +02001783 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001784 * positively from h2c_frt_stream_new(), the stream will report the error,
1785 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001786 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001787 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001788 if (!h2s) {
1789 error = H2_ERR_INTERNAL_ERROR;
1790 goto conn_err;
1791 }
1792
1793 h2s->st = H2_SS_OPEN;
1794 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1795 h2s->st = H2_SS_HREM;
1796 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001797 /* note: cs cannot be null for now (just created above) */
1798 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001799 }
1800
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001801 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001802 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001803
Willy Tarreau8f650c32017-11-21 19:36:21 +01001804 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001805 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001806
Willy Tarreau721c9742017-11-07 11:05:42 +01001807 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001808 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001809 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001810 }
1811 else {
1812 /* update the max stream ID if the request is being processed */
1813 if (h2s->id > h2c->max_id)
1814 h2c->max_id = h2s->id;
1815 }
1816
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001817 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001818
1819 conn_err:
1820 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001821 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001822
1823 strm_err:
1824 if (h2s) {
1825 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001826 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001827 }
1828 else
1829 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001830 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001831}
1832
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001833/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1834 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1835 * errors here are reported as connection errors since it's impossible to
1836 * recover from such errors after the compression context has been altered.
1837 */
1838static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1839{
1840 int error;
1841
1842 if (!h2c->dfl) {
1843 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1844 sess_log(h2c->conn->owner);
1845 goto strm_err;
1846 }
1847
1848 if (!b_size(&h2c->dbuf))
1849 return NULL; // empty buffer
1850
1851 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1852 return NULL; // incomplete frame
1853
1854 if (h2c->flags & H2_CF_DEM_TOOMANY)
1855 return 0; // too many cs still present
1856
1857 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1858 h2s->flags |= H2_SF_ES_RCVD;
1859 h2s->cs->flags |= CS_FL_REOS;
1860 }
1861
1862 if (!h2s_decode_headers(h2s))
1863 return NULL;
1864
1865 if (h2c->st0 >= H2_CS_ERROR)
1866 return NULL;
1867
1868 if (h2s->st >= H2_SS_ERROR) {
1869 /* stream error : send RST_STREAM */
1870 h2c->st0 = H2_CS_FRAME_E;
1871 }
1872
1873 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1874 h2s->st = H2_SS_ERROR;
1875 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1876 h2s->st = H2_SS_HREM;
1877 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1878 h2s_close(h2s);
1879
1880 return h2s;
1881
1882 conn_err:
1883 h2c_error(h2c, error);
1884 return NULL;
1885
1886 strm_err:
1887 if (h2s) {
1888 h2s_error(h2s, error);
1889 h2c->st0 = H2_CS_FRAME_E;
1890 }
1891 else
1892 h2c_error(h2c, error);
1893 return NULL;
1894}
1895
Willy Tarreau454f9052017-10-26 19:40:35 +02001896/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1897 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1898 */
1899static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1900{
1901 int error;
1902
1903 /* note that empty DATA frames are perfectly valid and sometimes used
1904 * to signal an end of stream (with the ES flag).
1905 */
1906
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001907 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001908 return 0; // empty buffer
1909
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001910 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001911 return 0; // incomplete frame
1912
1913 /* now either the frame is complete or the buffer is complete */
1914
1915 if (!h2c->dsi) {
1916 /* RFC7540#6.1 */
1917 error = H2_ERR_PROTOCOL_ERROR;
1918 goto conn_err;
1919 }
1920
1921 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1922 /* RFC7540#6.1 */
1923 error = H2_ERR_STREAM_CLOSED;
1924 goto strm_err;
1925 }
1926
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001927 if (!h2_frt_transfer_data(h2s))
1928 return 0;
1929
Willy Tarreau454f9052017-10-26 19:40:35 +02001930 /* call the upper layers to process the frame, then let the upper layer
1931 * notify the stream about any change.
1932 */
1933 if (!h2s->cs) {
1934 error = H2_ERR_STREAM_CLOSED;
1935 goto strm_err;
1936 }
1937
Willy Tarreau8f650c32017-11-21 19:36:21 +01001938 if (h2c->st0 >= H2_CS_ERROR)
1939 return 0;
1940
Willy Tarreau721c9742017-11-07 11:05:42 +01001941 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001942 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001943 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001944 }
1945
1946 /* check for completion : the callee will change this to FRAME_A or
1947 * FRAME_H once done.
1948 */
1949 if (h2c->st0 == H2_CS_FRAME_P)
1950 return 0;
1951
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001952
1953 /* last frame */
1954 if (h2c->dff & H2_F_DATA_END_STREAM) {
1955 h2s->st = H2_SS_HREM;
1956 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001957 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001958 }
1959
Willy Tarreau454f9052017-10-26 19:40:35 +02001960 return 1;
1961
1962 conn_err:
1963 h2c_error(h2c, error);
1964 return 0;
1965
1966 strm_err:
1967 if (h2s) {
1968 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001969 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001970 }
1971 else
1972 h2c_error(h2c, error);
1973 return 0;
1974}
1975
Willy Tarreaubc933932017-10-09 16:21:43 +02001976/* process Rx frames to be demultiplexed */
1977static void h2_process_demux(struct h2c *h2c)
1978{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001979 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001980
Willy Tarreau081d4722017-05-16 21:51:05 +02001981 if (h2c->st0 >= H2_CS_ERROR)
1982 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001983
1984 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1985 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001986 if (h2c->flags & H2_CF_IS_BACK)
1987 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001988 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1989 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001990 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001991 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001992 sess_log(h2c->conn->owner);
1993 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001994 goto fail;
1995 }
1996
1997 h2c->max_id = 0;
1998 h2c->st0 = H2_CS_SETTINGS1;
1999 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002000
2001 if (h2c->st0 == H2_CS_SETTINGS1) {
2002 struct h2_fh hdr;
2003
2004 /* ensure that what is pending is a valid SETTINGS frame
2005 * without an ACK.
2006 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002007 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002008 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002009 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002010 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002011 sess_log(h2c->conn->owner);
2012 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002013 goto fail;
2014 }
2015
2016 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2017 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2018 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2019 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002020 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002021 goto fail;
2022 }
2023
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002024 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002025 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2026 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2027 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002028 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002029 goto fail;
2030 }
2031
2032 /* that's OK, switch to FRAME_P to process it */
2033 h2c->dfl = hdr.len;
2034 h2c->dsi = hdr.sid;
2035 h2c->dft = hdr.ft;
2036 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002037 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002038 h2c->st0 = H2_CS_FRAME_P;
2039 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002040 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002041
2042 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002043 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002044 int ret = 0;
2045
2046 if (h2c->st0 >= H2_CS_ERROR)
2047 break;
2048
2049 if (h2c->st0 == H2_CS_FRAME_H) {
2050 struct h2_fh hdr;
2051
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002052 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002053 break;
2054
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002055 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002056 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2057 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002058 if (!h2c->nb_streams) {
2059 /* only log if no other stream can report the error */
2060 sess_log(h2c->conn->owner);
2061 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002062 break;
2063 }
2064
2065 h2c->dfl = hdr.len;
2066 h2c->dsi = hdr.sid;
2067 h2c->dft = hdr.ft;
2068 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002069 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002070 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002071 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002072 }
2073
2074 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002075 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2076
Olivier Houchard638b7992018-08-16 15:41:52 +02002077 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002078 /* we may have to signal the upper layers */
2079 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002080 if (h2s->recv_wait) {
2081 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2082 tasklet_wakeup(h2s->recv_wait->task);
2083 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002084 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002085 }
2086 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002087
Willy Tarreaud7901432017-12-29 11:34:40 +01002088 if (h2c->st0 == H2_CS_FRAME_E)
2089 goto strm_err;
2090
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002091 if (h2s->st == H2_SS_IDLE &&
2092 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2093 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2094 * this state MUST be treated as a connection error
2095 */
2096 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2097 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002098 if (!h2c->nb_streams) {
2099 /* only log if no other stream can report the error */
2100 sess_log(h2c->conn->owner);
2101 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002102 break;
2103 }
2104
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002105 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2106 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2107 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2108 * this state MUST be treated as a stream error
2109 */
2110 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2111 goto strm_err;
2112 }
2113
Willy Tarreauab837502017-12-27 15:07:30 +01002114 /* Below the management of frames received in closed state is a
2115 * bit hackish because the spec makes strong differences between
2116 * streams closed by receiving RST, sending RST, and seeing ES
2117 * in both directions. In addition to this, the creation of a
2118 * new stream reusing the identifier of a closed one will be
2119 * detected here. Given that we cannot keep track of all closed
2120 * streams forever, we consider that unknown closed streams were
2121 * closed on RST received, which allows us to respond with an
2122 * RST without breaking the connection (eg: to abort a transfer).
2123 * Some frames have to be silently ignored as well.
2124 */
2125 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2126 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2127 /* #5.1.1: The identifier of a newly
2128 * established stream MUST be numerically
2129 * greater than all streams that the initiating
2130 * endpoint has opened or reserved. This
2131 * governs streams that are opened using a
2132 * HEADERS frame and streams that are reserved
2133 * using PUSH_PROMISE. An endpoint that
2134 * receives an unexpected stream identifier
2135 * MUST respond with a connection error.
2136 */
2137 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2138 goto strm_err;
2139 }
2140
2141 if (h2s->flags & H2_SF_RST_RCVD) {
2142 /* RFC7540#5.1:closed: an endpoint that
2143 * receives any frame other than PRIORITY after
2144 * receiving a RST_STREAM MUST treat that as a
2145 * stream error of type STREAM_CLOSED.
2146 *
2147 * Note that old streams fall into this category
2148 * and will lead to an RST being sent.
2149 */
2150 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2151 h2c->st0 = H2_CS_FRAME_E;
2152 goto strm_err;
2153 }
2154
2155 /* RFC7540#5.1:closed: if this state is reached as a
2156 * result of sending a RST_STREAM frame, the peer that
2157 * receives the RST_STREAM might have already sent
2158 * frames on the stream that cannot be withdrawn. An
2159 * endpoint MUST ignore frames that it receives on
2160 * closed streams after it has sent a RST_STREAM
2161 * frame. An endpoint MAY choose to limit the period
2162 * over which it ignores frames and treat frames that
2163 * arrive after this time as being in error.
2164 */
2165 if (!(h2s->flags & H2_SF_RST_SENT)) {
2166 /* RFC7540#5.1:closed: any frame other than
2167 * PRIO/WU/RST in this state MUST be treated as
2168 * a connection error
2169 */
2170 if (h2c->dft != H2_FT_RST_STREAM &&
2171 h2c->dft != H2_FT_PRIORITY &&
2172 h2c->dft != H2_FT_WINDOW_UPDATE) {
2173 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2174 goto strm_err;
2175 }
2176 }
2177 }
2178
Willy Tarreauc0da1962017-10-30 18:38:00 +01002179#if 0
2180 // problem below: it is not possible to completely ignore such
2181 // streams as we need to maintain the compression state as well
2182 // and for this we need to completely process these frames (eg:
2183 // HEADERS frames) as well as counting DATA frames to emit
2184 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2185 // This is a typical case of layer violation where the
2186 // transported contents are critical to the connection's
2187 // validity and must be ignored at the same time :-(
2188
2189 /* graceful shutdown, ignore streams whose ID is higher than
2190 * the one advertised in GOAWAY. RFC7540#6.8.
2191 */
2192 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002193 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2194 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002195 h2c->dfl -= ret;
2196 ret = h2c->dfl == 0;
2197 goto strm_err;
2198 }
2199#endif
2200
Willy Tarreau7e98c052017-10-10 15:56:59 +02002201 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002202 case H2_FT_SETTINGS:
2203 if (h2c->st0 == H2_CS_FRAME_P)
2204 ret = h2c_handle_settings(h2c);
2205
2206 if (h2c->st0 == H2_CS_FRAME_A)
2207 ret = h2c_ack_settings(h2c);
2208 break;
2209
Willy Tarreaucf68c782017-10-10 17:11:41 +02002210 case H2_FT_PING:
2211 if (h2c->st0 == H2_CS_FRAME_P)
2212 ret = h2c_handle_ping(h2c);
2213
2214 if (h2c->st0 == H2_CS_FRAME_A)
2215 ret = h2c_ack_ping(h2c);
2216 break;
2217
Willy Tarreau26f95952017-07-27 17:18:30 +02002218 case H2_FT_WINDOW_UPDATE:
2219 if (h2c->st0 == H2_CS_FRAME_P)
2220 ret = h2c_handle_window_update(h2c, h2s);
2221 break;
2222
Willy Tarreau61290ec2017-10-17 08:19:21 +02002223 case H2_FT_CONTINUATION:
2224 /* we currently don't support CONTINUATION frames since
2225 * we have nowhere to store the partial HEADERS frame.
2226 * Let's abort the stream on an INTERNAL_ERROR here.
2227 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002228 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002229 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002230 h2c->st0 = H2_CS_FRAME_E;
2231 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002232 break;
2233
Willy Tarreau13278b42017-10-13 19:23:14 +02002234 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002235 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002236 if (h2c->flags & H2_CF_IS_BACK)
2237 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2238 else
2239 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002240 if (tmp_h2s) {
2241 h2s = tmp_h2s;
2242 ret = 1;
2243 }
2244 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002245 break;
2246
Willy Tarreau454f9052017-10-26 19:40:35 +02002247 case H2_FT_DATA:
2248 if (h2c->st0 == H2_CS_FRAME_P)
2249 ret = h2c_frt_handle_data(h2c, h2s);
2250
2251 if (h2c->st0 == H2_CS_FRAME_A)
2252 ret = h2c_send_strm_wu(h2c);
2253 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002254
Willy Tarreau92153fc2017-12-03 19:46:19 +01002255 case H2_FT_PRIORITY:
2256 if (h2c->st0 == H2_CS_FRAME_P)
2257 ret = h2c_handle_priority(h2c);
2258 break;
2259
Willy Tarreaucd234e92017-08-18 10:59:39 +02002260 case H2_FT_RST_STREAM:
2261 if (h2c->st0 == H2_CS_FRAME_P)
2262 ret = h2c_handle_rst_stream(h2c, h2s);
2263 break;
2264
Willy Tarreaue96b0922017-10-30 00:28:29 +01002265 case H2_FT_GOAWAY:
2266 if (h2c->st0 == H2_CS_FRAME_P)
2267 ret = h2c_handle_goaway(h2c);
2268 break;
2269
Willy Tarreau1c661982017-10-30 13:52:01 +01002270 case H2_FT_PUSH_PROMISE:
2271 /* not permitted here, RFC7540#5.1 */
2272 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002273 if (!h2c->nb_streams) {
2274 /* only log if no other stream can report the error */
2275 sess_log(h2c->conn->owner);
2276 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002277 break;
2278
2279 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002280 default:
2281 /* drop frames that we ignore. They may be larger than
2282 * the buffer so we drain all of their contents until
2283 * we reach the end.
2284 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002285 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2286 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002287 h2c->dfl -= ret;
2288 ret = h2c->dfl == 0;
2289 }
2290
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002291 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002292 /* We may have to send an RST if not done yet */
2293 if (h2s->st == H2_SS_ERROR)
2294 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002295
Willy Tarreaua20a5192017-12-27 11:02:06 +01002296 if (h2c->st0 == H2_CS_FRAME_E)
2297 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002298
Willy Tarreau7e98c052017-10-10 15:56:59 +02002299 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002300 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002301 break;
2302
2303 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002304 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002305 h2c->st0 = H2_CS_FRAME_H;
2306 }
2307 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002308
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002309 if (h2c->rcvd_c > 0 &&
2310 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2311 h2c_send_conn_wu(h2c);
2312
Willy Tarreau52eed752017-09-22 15:05:09 +02002313 fail:
2314 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002315 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002316 /* we may have to signal the upper layers */
2317 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002318 if (h2s->recv_wait) {
2319 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2320 tasklet_wakeup(h2s->recv_wait->task);
2321 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002322 }
2323 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002324
2325 if (h2_recv_allowed(h2c))
2326 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002327}
2328
2329/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2330 * the end.
2331 */
2332static int h2_process_mux(struct h2c *h2c)
2333{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002334 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002335
Willy Tarreau01b44822018-10-03 14:26:37 +02002336 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2337 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2338 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2339 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2340 if (h2c->st0 == H2_CS_ERROR) {
2341 h2c->st0 = H2_CS_ERROR2;
2342 sess_log(h2c->conn->owner);
2343 }
2344 goto fail;
2345 }
2346 h2c->st0 = H2_CS_SETTINGS1;
2347 }
2348 /* need to wait for the other side */
2349 if (h2c->st0 == H2_CS_SETTINGS1)
2350 return 1;
2351 }
2352
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002353 /* start by sending possibly pending window updates */
2354 if (h2c->rcvd_c > 0 &&
2355 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2356 h2c_send_conn_wu(h2c) < 0)
2357 goto fail;
2358
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002359 /* First we always process the flow control list because the streams
2360 * waiting there were already elected for immediate emission but were
2361 * blocked just on this.
2362 */
2363
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002364 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002365 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2366 h2c->st0 >= H2_CS_ERROR)
2367 break;
2368
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002369 h2s->flags &= ~H2_SF_BLK_ANY;
2370 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002371 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002372 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002373 LIST_DEL(&h2s->list);
2374 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002375 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002376 }
2377
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002378 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002379 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2380 break;
2381
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002382 h2s->flags &= ~H2_SF_BLK_ANY;
2383 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002384 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002385 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002386 LIST_DEL(&h2s->list);
2387 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002388 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002389 }
2390
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002391 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002392 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002393 if (h2c->st0 == H2_CS_ERROR) {
2394 if (h2c->max_id >= 0) {
2395 h2c_send_goaway_error(h2c, NULL);
2396 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2397 return 0;
2398 }
2399
2400 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2401 }
2402 return 1;
2403 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002404 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002405}
2406
Willy Tarreau62f52692017-10-08 23:01:42 +02002407
Willy Tarreau479998a2018-11-18 06:30:59 +01002408/* Attempt to read data, and subscribe if none available.
2409 * The function returns 1 if data has been received, otherwise zero.
2410 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002411static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002412{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002413 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002414 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002415 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002416 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002417
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002418 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002419 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002420
Willy Tarreau315d8072017-12-10 22:17:57 +01002421 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002422 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002423
Willy Tarreau44e973f2018-03-01 17:49:30 +01002424 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002425 if (!buf) {
2426 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002427 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002428 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002429
Olivier Houchard7505f942018-08-21 18:10:44 +02002430 do {
2431 max = buf->size - b_data(buf);
2432 if (max)
2433 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2434 else
2435 ret = 0;
2436 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002437
Olivier Houchard53216e72018-10-10 15:46:36 +02002438 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002439 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002440
Olivier Houcharda1411e62018-08-17 18:42:48 +02002441 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002442 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002443 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002444 }
2445
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002446 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002447 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002448 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002449}
2450
Willy Tarreau479998a2018-11-18 06:30:59 +01002451/* Try to send data if possible.
2452 * The function returns 1 if data have been sent, otherwise zero.
2453 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002454static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002455{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002456 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002457 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002458 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002459
2460 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002461 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002462
Olivier Houchard7505f942018-08-21 18:10:44 +02002463
Willy Tarreaua2af5122017-10-09 11:56:46 +02002464 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2465 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002466 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002467 }
2468
Willy Tarreaubc933932017-10-09 16:21:43 +02002469 /* This loop is quite simple : it tries to fill as much as it can from
2470 * pending streams into the existing buffer until it's reportedly full
2471 * or the end of send requests is reached. Then it tries to send this
2472 * buffer's contents out, marks it not full if at least one byte could
2473 * be sent, and tries again.
2474 *
2475 * The snd_buf() function normally takes a "flags" argument which may
2476 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2477 * data immediately comes and CO_SFL_STREAMER to indicate that the
2478 * connection is streaming lots of data (used to increase TLS record
2479 * size at the expense of latency). The former can be sent any time
2480 * there's a buffer full flag, as it indicates at least one stream
2481 * attempted to send and failed so there are pending data. An
2482 * alternative would be to set it as long as there's an active stream
2483 * but that would be problematic for ACKs until we have an absolute
2484 * guarantee that all waiters have at least one byte to send. The
2485 * latter should possibly not be set for now.
2486 */
2487
2488 done = 0;
2489 while (!done) {
2490 unsigned int flags = 0;
2491
2492 /* fill as much as we can into the current buffer */
2493 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2494 done = h2_process_mux(h2c);
2495
2496 if (conn->flags & CO_FL_ERROR)
2497 break;
2498
2499 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2500 flags |= CO_SFL_MSG_MORE;
2501
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002502 if (b_data(&h2c->mbuf)) {
2503 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002504 if (!ret)
2505 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002506 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002507 b_del(&h2c->mbuf, ret);
2508 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002509 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002510
2511 /* wrote at least one byte, the buffer is not full anymore */
2512 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2513 }
2514
Willy Tarreaua2af5122017-10-09 11:56:46 +02002515 if (conn->flags & CO_FL_SOCK_WR_SH) {
2516 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002517 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002518 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002519 /* We're not full anymore, so we can wake any task that are waiting
2520 * for us.
2521 */
2522 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002523 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002524 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2525 struct h2s *, list);
2526 LIST_DEL(&h2s->list);
2527 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002528 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002529 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002530 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002531 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002532 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002533 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002534 /* We're done, no more to send */
2535 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002536 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002537schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002538 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2539 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002540 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002541}
2542
2543static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2544{
2545 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002546 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002547
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002548 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002549 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002550 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002551 ret |= h2_recv(h2c);
2552 if (ret)
2553 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002554 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002555}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002556
Willy Tarreau62f52692017-10-08 23:01:42 +02002557/* callback called on any event by the connection handler.
2558 * It applies changes and returns zero, or < 0 if it wants immediate
2559 * destruction of the connection (which normally doesn not happen in h2).
2560 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002561static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002562{
Olivier Houchard7505f942018-08-21 18:10:44 +02002563 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002564
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002565 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002566 h2_process_demux(h2c);
2567
2568 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002569 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002570
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002571 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002572 h2c->flags &= ~H2_CF_DEM_DFULL;
2573 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002574 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002575
Willy Tarreau0b37d652018-10-03 10:33:02 +02002576 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002577 /* frontend is stopping, reload likely in progress, let's try
2578 * to announce a graceful shutdown if not yet done. We don't
2579 * care if it fails, it will be tried again later.
2580 */
2581 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2582 if (h2c->last_sid < 0)
2583 h2c->last_sid = (1U << 31) - 1;
2584 h2c_send_goaway_error(h2c, NULL);
2585 }
2586 }
2587
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002588 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002589 * If we received early data, and the handshake is done, wake
2590 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002591 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002592 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2593 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2594 struct eb32_node *node;
2595 struct h2s *h2s;
2596
2597 h2c->flags |= H2_CF_WAIT_FOR_HS;
2598 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2599
2600 while (node) {
2601 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002602 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002603 h2s->recv_wait) {
2604 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002605 sw->wait_reason &= ~SUB_CAN_RECV;
2606 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002607 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002608 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002609 node = eb32_next(node);
2610 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002611 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002612
Willy Tarreau26bd7612017-10-09 16:47:04 +02002613 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002614 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2615 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2616 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002617 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002618
2619 if (eb_is_empty(&h2c->streams_by_id)) {
2620 /* no more stream, kill the connection now */
2621 h2_release(conn);
2622 return -1;
2623 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002624 }
2625
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002626 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002627 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002628
Olivier Houchard53216e72018-10-10 15:46:36 +02002629 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2630 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2631 (h2c->st0 != H2_CS_ERROR &&
2632 !b_data(&h2c->mbuf) &&
2633 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2634 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002635 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002636
Willy Tarreau3f133572017-10-31 19:21:06 +01002637 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002638 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002639 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002640 task_queue(h2c->task);
2641 }
2642 else
2643 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002644 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002645
Olivier Houchard7505f942018-08-21 18:10:44 +02002646 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002647 return 0;
2648}
2649
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002650static int h2_wake(struct connection *conn)
2651{
2652 struct h2c *h2c = conn->mux_ctx;
2653
2654 return (h2_process(h2c));
2655}
2656
Willy Tarreauea392822017-10-31 10:02:25 +01002657/* Connection timeout management. The principle is that if there's no receipt
2658 * nor sending for a certain amount of time, the connection is closed. If the
2659 * MUX buffer still has lying data or is not allocatable, the connection is
2660 * immediately killed. If it's allocatable and empty, we attempt to send a
2661 * GOAWAY frame.
2662 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002663static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002664{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002665 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002666 int expired = tick_is_expired(t->expire, now_ms);
2667
Willy Tarreau0975f112018-03-29 15:22:59 +02002668 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002669 return t;
2670
Willy Tarreau0975f112018-03-29 15:22:59 +02002671 task_delete(t);
2672 task_free(t);
2673
2674 if (!h2c) {
2675 /* resources were already deleted */
2676 return NULL;
2677 }
2678
2679 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002680 h2c_error(h2c, H2_ERR_NO_ERROR);
2681 h2_wake_some_streams(h2c, 0, 0);
2682
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002683 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002684 /* don't even try to send a GOAWAY, the buffer is stuck */
2685 h2c->flags |= H2_CF_GOAWAY_FAILED;
2686 }
2687
2688 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002689 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002690 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2691 h2c->flags |= H2_CF_GOAWAY_FAILED;
2692
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002693 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2694 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002695 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002696 b_del(&h2c->mbuf, ret);
2697 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002698 }
2699 }
Willy Tarreauea392822017-10-31 10:02:25 +01002700
Willy Tarreau0975f112018-03-29 15:22:59 +02002701 /* either we can release everything now or it will be done later once
2702 * the last stream closes.
2703 */
2704 if (eb_is_empty(&h2c->streams_by_id))
2705 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002706
Willy Tarreauea392822017-10-31 10:02:25 +01002707 return NULL;
2708}
2709
2710
Willy Tarreau62f52692017-10-08 23:01:42 +02002711/*******************************************/
2712/* functions below are used by the streams */
2713/*******************************************/
2714
2715/*
2716 * Attach a new stream to a connection
2717 * (Used for outgoing connections)
2718 */
2719static struct conn_stream *h2_attach(struct connection *conn)
2720{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002721 struct conn_stream *cs;
2722 struct h2s *h2s;
2723 struct h2c *h2c = conn->mux_ctx;
2724
2725 cs = cs_new(conn);
2726 if (!cs)
2727 return NULL;
2728 h2s = h2c_bck_stream_new(h2c, cs);
2729 if (!h2s) {
2730 cs_free(cs);
2731 return NULL;
2732 }
2733 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002734}
2735
Willy Tarreaufafd3982018-11-18 21:29:20 +01002736/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2737 * We have to scan because we may have some orphan streams. It might be
2738 * beneficial to scan backwards from the end to reduce the likeliness to find
2739 * orphans.
2740 */
2741static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2742{
2743 struct h2c *h2c = conn->mux_ctx;
2744 struct h2s *h2s;
2745 struct eb32_node *node;
2746
2747 node = eb32_first(&h2c->streams_by_id);
2748 while (node) {
2749 h2s = container_of(node, struct h2s, by_id);
2750 if (h2s->cs)
2751 return h2s->cs;
2752 node = eb32_next(node);
2753 }
2754 return NULL;
2755}
2756
Willy Tarreau62f52692017-10-08 23:01:42 +02002757/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002758 * Destroy the mux and the associated connection, if it is no longer used
2759 */
2760static void h2_destroy(struct connection *conn)
2761{
2762 struct h2c *h2c = conn->mux_ctx;
2763
2764 if (eb_is_empty(&h2c->streams_by_id))
2765 h2_release(h2c->conn);
2766}
2767
2768/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002769 * Detach the stream from the connection and possibly release the connection.
2770 */
2771static void h2_detach(struct conn_stream *cs)
2772{
Willy Tarreau60935142017-10-16 18:11:19 +02002773 struct h2s *h2s = cs->ctx;
2774 struct h2c *h2c;
2775
2776 cs->ctx = NULL;
2777 if (!h2s)
2778 return;
2779
2780 h2c = h2s->h2c;
2781 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002782 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002783 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2784 !h2_has_too_many_cs(h2c)) {
2785 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002786 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002787 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002788 }
Willy Tarreau60935142017-10-16 18:11:19 +02002789
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002790 /* this stream may be blocked waiting for some data to leave (possibly
2791 * an ES or RST frame), so orphan it in this case.
2792 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002793 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002794 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002795 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002796 return;
2797
Willy Tarreau45f752e2017-10-30 15:44:59 +01002798 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2799 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2800 /* unblock the connection if it was blocked on this
2801 * stream.
2802 */
2803 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2804 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002805 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002806 }
2807
Willy Tarreau71049cc2018-03-28 13:56:39 +02002808 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002809
Willy Tarreaue323f342018-03-28 13:51:45 +02002810 /* We don't want to close right now unless we're removing the
2811 * last stream, and either the connection is in error, or it
2812 * reached the ID already specified in a GOAWAY frame received
2813 * or sent (as seen by last_sid >= 0).
2814 */
2815 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2816 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002817 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002818 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002819 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002820 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002821 (conn_xprt_read0_pending(h2c->conn) ||
2822 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2823 /* no more stream will come, kill it now */
2824 h2_release(h2c->conn);
2825 }
2826 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002827 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002828 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2829 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002830 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002831 else
2832 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002833 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002834}
2835
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002836static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002837{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002838 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002839 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002840
Willy Tarreau721c9742017-11-07 11:05:42 +01002841 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002842 return;
2843
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002844 /* if no outgoing data was seen on this stream, it means it was
2845 * closed with a "tcp-request content" rule that is normally
2846 * used to kill the connection ASAP (eg: limit abuse). In this
2847 * case we send a goaway to close the connection.
2848 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002849 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002850 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002851 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002852
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002853 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2854 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002855 h2c_send_goaway_error(h2c, h2s) <= 0)
2856 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002857
Willy Tarreau00dd0782018-03-01 16:31:34 +01002858 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002859
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002860 return;
2861add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002862 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002863 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002864 if (h2s->flags & H2_SF_BLK_MFCTL) {
2865 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2866 h2s->send_wait = sw;
2867 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2868 h2s->send_wait = sw;
2869 LIST_ADDQ(&h2c->send_list, &h2s->list);
2870 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002871 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002872 /* Let the handler know we want shutr */
2873 sw->handle = (void *)((long)sw->handle | 1);
2874
Willy Tarreau62f52692017-10-08 23:01:42 +02002875}
2876
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002877static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002878{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002879 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002880 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002881
Willy Tarreau721c9742017-11-07 11:05:42 +01002882 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002883 return;
2884
Willy Tarreau67434202017-11-06 20:20:51 +01002885 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002886 /* we can cleanly close using an empty data frame only after headers */
2887
2888 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2889 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002890 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002891
2892 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002893 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002894 else
2895 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002896 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002897 /* if no outgoing data was seen on this stream, it means it was
2898 * closed with a "tcp-request content" rule that is normally
2899 * used to kill the connection ASAP (eg: limit abuse). In this
2900 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002901 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002902 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002903 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002904 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002905
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002906 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2907 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002908 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002909 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002910
Willy Tarreau00dd0782018-03-01 16:31:34 +01002911 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002912 }
2913
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002914
2915 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002916 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002917 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002918 if (h2s->flags & H2_SF_BLK_MFCTL) {
2919 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2920 h2s->send_wait = sw;
2921 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2922 h2s->send_wait = sw;
2923 LIST_ADDQ(&h2c->send_list, &h2s->list);
2924 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002925 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002926 /* let the handler know we want to shutw */
2927 sw->handle = (void *)((long)(sw->handle) | 2);
2928
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002929}
2930
2931static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2932{
2933 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002934 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002935
2936 if (reason & 1)
2937 h2_do_shutr(h2s);
2938 if (reason & 2)
2939 h2_do_shutw(h2s);
2940
2941 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002942}
2943
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002944static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2945{
2946 struct h2s *h2s = cs->ctx;
2947
2948 if (!mode)
2949 return;
2950
2951 h2_do_shutr(h2s);
2952}
2953
2954static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2955{
2956 struct h2s *h2s = cs->ctx;
2957
2958 h2_do_shutw(h2s);
2959}
2960
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002961/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002962 * HTX request or response depending on the connection's side. Returns the
2963 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
2964 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002965 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002966static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002967{
2968 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002970 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002971 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002972 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002973 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002974 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002975 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002976 int flen = h2c->dfl;
2977 int outlen = 0;
2978 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002979 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002980
2981 if (!h2c->dfl) {
2982 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002983 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002984 return 0;
2985 }
2986
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002987 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002988 return 0; // incomplete input frame
2989
Willy Tarreau13278b42017-10-13 19:23:14 +02002990 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002991 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002992 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002993 copy = alloc_trash_chunk();
2994 if (!copy) {
2995 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2996 goto fail;
2997 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002998 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2999 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3000 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003001 }
3002
3003 /* The padlen is the first byte before data, and the padding appears
3004 * after data. padlen+data+padding are included in flen.
3005 */
3006 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003007 h2c->dpl = *hdrs;
3008 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003009 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3010 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003011 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003012 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003013 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003014 hdrs += 1; // skip Pad Length
3015 }
3016
3017 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3018 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003019 if (read_n32(hdrs) == h2s->id) {
3020 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3021 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003022 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003023 }
3024
Willy Tarreau13278b42017-10-13 19:23:14 +02003025 hdrs += 5; // stream dep = 4, weight = 1
3026 flen -= 5;
3027 }
3028
3029 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3030 * don't support this for now and can't even decompress so we have to
3031 * break the connection.
3032 */
3033 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3034 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003035 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003036 }
3037
Olivier Houchard638b7992018-08-16 15:41:52 +02003038 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003039 if (!csbuf) {
3040 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003041 goto fail;
3042 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003043
Willy Tarreau937f7602018-02-26 15:22:17 +01003044 /* we can't retry a failed decompression operation so we must be very
3045 * careful not to take any risks. In practice the output buffer is
3046 * always empty except maybe for trailers, in which case we simply have
3047 * to wait for the upper layer to finish consuming what is available.
3048 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003049
3050 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3051 htx = htx_from_buf(&h2s->rxbuf);
3052 if (!htx_is_empty(htx))
3053 goto fail;
3054 } else {
3055 if (b_data(csbuf))
3056 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003057
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003058 csbuf->head = 0;
3059 try = b_size(csbuf);
3060 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003061
3062 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3063 sizeof(list)/sizeof(list[0]), tmp);
3064 if (outlen < 0) {
3065 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3066 goto fail;
3067 }
3068
3069 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003070 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003071
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003072 if (htx) {
3073 /* HTX mode */
3074 if (h2c->flags & H2_CF_IS_BACK)
3075 outlen = h2_make_htx_response(list, htx, &msgf);
3076 else
3077 outlen = h2_make_htx_request(list, htx, &msgf);
3078 } else {
3079 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003080 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003081 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003082
3083 if (outlen < 0) {
3084 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3085 goto fail;
3086 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003087
Willy Tarreau174b06a2018-04-25 18:13:58 +02003088 if (msgf & H2_MSGF_BODY) {
3089 /* a payload is present */
3090 if (msgf & H2_MSGF_BODY_CL)
3091 h2s->flags |= H2_SF_DATA_CLEN;
3092 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
3093 h2s->flags |= H2_SF_DATA_CHNK;
3094 }
3095
Willy Tarreau13278b42017-10-13 19:23:14 +02003096 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003097 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003098 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003099 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003100
Willy Tarreau39d68502018-03-02 12:26:37 +01003101 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003102 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003103 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003104 if (htx)
3105 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003106 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003107
Willy Tarreau68dd9852017-07-03 14:44:26 +02003108 leave:
3109 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003110 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003111 fail:
3112 outlen = 0;
3113 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003114}
3115
Willy Tarreau454f9052017-10-26 19:40:35 +02003116/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3117 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3118 * in use, a new chunk is emitted for each frame. This is supposed to fit
3119 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3120 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3121 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003122 * parser state is automatically updated. Returns > 0 if it could completely
3123 * send the current frame, 0 if it couldn't complete, in which case
3124 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3125 * DATA frame can return 0 as a valid result). Stream errors are reported in
3126 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3127 * have checked the frame header and ensured that the frame was complete or the
3128 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003129 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003130static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003131{
3132 struct h2c *h2c = h2s->h2c;
3133 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003134 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003135 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003136 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003137 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003138
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003139 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003140
3141 /* The padlen is the first byte before data, and the padding appears
3142 * after data. padlen+data+padding are included in flen.
3143 */
Willy Tarreau79127812017-12-03 21:06:59 +01003144 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003145 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003146 return 0;
3147
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003148 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003149 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003150 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3151 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003152 return 0;
3153 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003154
3155 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003156 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003157 h2c->dfl--;
3158 h2c->rcvd_c++; h2c->rcvd_s++;
3159 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003160 }
3161
Olivier Houchard638b7992018-08-16 15:41:52 +02003162 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003163 if (!csbuf) {
3164 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003165 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003166 }
3167
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003168try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003169 flen = h2c->dfl - h2c->dpl;
3170 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003171 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003172
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003173 if (flen > b_data(&h2c->dbuf)) {
3174 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003175 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003176 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003177 }
3178
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003179 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3180 htx = htx_from_buf(csbuf);
3181 block1 = htx_free_data_space(htx);
3182 if (!block1) {
3183 h2c->flags |= H2_CF_DEM_SFULL;
3184 goto fail;
3185 }
3186 if (flen > block1)
3187 flen = block1;
3188
3189 /* here, flen is the max we can copy into the output buffer */
3190 block1 = b_contig_data(&h2c->dbuf, 0);
3191 if (flen > block1)
3192 flen = block1;
3193
3194 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3195 h2c->flags |= H2_CF_DEM_SFULL;
3196 goto fail;
3197 }
3198
3199 b_del(&h2c->dbuf, flen);
3200 h2c->dfl -= flen;
3201 h2c->rcvd_c += flen;
3202 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3203 goto try_again;
3204 }
3205 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003206 /* it doesn't fit and the buffer is fragmented,
3207 * so let's defragment it and try again.
3208 */
3209 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003210 }
3211
Willy Tarreaueba10f22018-04-25 20:44:22 +02003212 /* chunked-encoding requires more room */
3213 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003214 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003215 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3216 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3217 (chklen < 1048576) ? 4 : 8;
3218 chklen += 4; // CRLF, CRLF
3219 }
3220
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003221 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003222 if (flen + chklen > b_room(csbuf)) {
3223 if (chklen >= b_room(csbuf)) {
3224 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003225 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003226 }
3227 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003228 }
3229
3230 if (h2s->flags & H2_SF_DATA_CHNK) {
3231 /* emit the chunk size */
3232 unsigned int chksz = flen;
3233 char str[10];
3234 char *beg;
3235
3236 beg = str + sizeof(str);
3237 *--beg = '\n';
3238 *--beg = '\r';
3239 do {
3240 *--beg = hextab[chksz & 0xF];
3241 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003242 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003243 }
3244
Willy Tarreau454f9052017-10-26 19:40:35 +02003245 /* Block1 is the length of the first block before the buffer wraps,
3246 * block2 is the optional second block to reach the end of the frame.
3247 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003248 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003249 if (block1 > flen)
3250 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003251 block2 = flen - block1;
3252
3253 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003254 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003255
3256 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003257 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003258
Willy Tarreaueba10f22018-04-25 20:44:22 +02003259 if (h2s->flags & H2_SF_DATA_CHNK) {
3260 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003261 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003262 }
3263
Willy Tarreau454f9052017-10-26 19:40:35 +02003264 /* now mark the input data as consumed (will be deleted from the buffer
3265 * by the caller when seeing FRAME_A after sending the window update).
3266 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003267 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003268 h2c->dfl -= flen;
3269 h2c->rcvd_c += flen;
3270 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3271
3272 if (h2c->dfl > h2c->dpl) {
3273 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003274 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003275 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003276 }
3277
Willy Tarreau4a28da12018-01-04 14:41:00 +01003278 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003279 /* here we're done with the frame, all the payload (except padding) was
3280 * transferred.
3281 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003282
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003283 if (h2c->dff & H2_F_DATA_END_STREAM) {
3284 if (htx) {
3285 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3286 h2c->flags |= H2_CF_DEM_SFULL;
3287 goto fail;
3288 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003289 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003290 else if (h2s->flags & H2_SF_DATA_CHNK) {
3291 /* emit the trailing 0 CRLF CRLF */
3292 if (b_room(csbuf) < 5) {
3293 h2c->flags |= H2_CF_DEM_SFULL;
3294 goto fail;
3295 }
3296 chklen += 5;
3297 b_putblk(csbuf, "0\r\n\r\n", 5);
3298 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003299 }
3300
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003301 h2c->rcvd_c += h2c->dpl;
3302 h2c->rcvd_s += h2c->dpl;
3303 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003304 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3305
Willy Tarreau39d68502018-03-02 12:26:37 +01003306 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003307 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003308 h2s->cs->flags |= CS_FL_REOS;
3309 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003310
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003311 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003312 fail:
3313 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003314}
3315
Willy Tarreau5dd17352018-06-14 13:33:30 +02003316/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3317 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3318 * number of bytes sent. The caller must check the stream's status to detect
3319 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003320 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003321static 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 +02003322{
3323 struct http_hdr list[MAX_HTTP_HDR];
3324 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003325 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003326 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003327 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003328 int es_now = 0;
3329 int ret = 0;
3330 int hdr;
3331
3332 if (h2c_mux_busy(h2c, h2s)) {
3333 h2s->flags |= H2_SF_BLK_MBUSY;
3334 return 0;
3335 }
3336
Willy Tarreau44e973f2018-03-01 17:49:30 +01003337 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003338 h2c->flags |= H2_CF_MUX_MALLOC;
3339 h2s->flags |= H2_SF_BLK_MROOM;
3340 return 0;
3341 }
3342
3343 /* First, try to parse the H1 response and index it into <list>.
3344 * NOTE! Since it comes from haproxy, we *know* that a response header
3345 * block does not wrap and we can safely read it this way without
3346 * having to realign the buffer.
3347 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003348 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003349 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003350 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003351 /* incomplete or invalid response, this is abnormal coming from
3352 * haproxy and may only result in a bad errorfile or bad Lua code
3353 * so that won't be fixed, raise an error now.
3354 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003355 * FIXME: we should instead add the ability to only return a
3356 * 502 bad gateway. But in theory this is not supposed to
3357 * happen.
3358 */
3359 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3360 ret = 0;
3361 goto end;
3362 }
3363
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003364 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003365
3366 /* certain statuses have no body or an empty one, regardless of
3367 * what the headers say.
3368 */
3369 if (sl.st.status >= 100 && sl.st.status < 200) {
3370 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3371 h1m->curr_len = h1m->body_len = 0;
3372 }
3373 else if (sl.st.status == 204 || sl.st.status == 304) {
3374 /* no contents, claim c-len is present and set to zero */
3375 h1m->flags &= ~H1_MF_CHNK;
3376 h1m->flags |= H1_MF_CLEN;
3377 h1m->curr_len = h1m->body_len = 0;
3378 }
3379
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003380 chunk_reset(&outbuf);
3381
3382 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003383 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003384 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003385 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003386
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003387 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003388 break;
3389 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003390 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003391 }
3392
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003393 if (outbuf.size < 9)
3394 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003395
3396 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003397 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3398 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3399 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003400
3401 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003402 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003403 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003404 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003405 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003406 else if (unlikely(list[0].v.len != 3)) {
3407 /* this is an unparsable response */
3408 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3409 ret = 0;
3410 goto end;
3411 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003412 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003413 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003414 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3415 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3416 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3417 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3418 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003419 }
3420 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003421 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003422 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003423 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003424 }
3425
3426 /* encode all headers, stop at empty name */
3427 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003428 /* these ones do not exist in H2 and must be dropped. */
3429 if (isteq(list[hdr].n, ist("connection")) ||
3430 isteq(list[hdr].n, ist("proxy-connection")) ||
3431 isteq(list[hdr].n, ist("keep-alive")) ||
3432 isteq(list[hdr].n, ist("upgrade")) ||
3433 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003434 continue;
3435
3436 if (isteq(list[hdr].n, ist("")))
3437 break; // end
3438
3439 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3440 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003441 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003442 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003443 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003444 }
3445 }
3446
3447 /* we may need to add END_STREAM */
3448 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3449 es_now = 1;
3450
3451 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003452 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003453
3454 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003455 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003456
3457 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003458 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003459
3460 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003462 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003463
3464 /* for now we don't implemented CONTINUATION, so we wait for a
3465 * body or directly end in TRL2.
3466 */
3467 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003468 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003469 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003470
Willy Tarreau801250e2018-09-11 11:45:04 +02003471 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003472 h2s->flags |= H2_SF_ES_SENT;
3473 if (h2s->st == H2_SS_OPEN)
3474 h2s->st = H2_SS_HLOC;
3475 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003476 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003477 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003478 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003479 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003480 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003481 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003482 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003483 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003484 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003485
3486 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003487
3488 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003489 //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 +02003490 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003491 full:
3492 h1m_init_res(h1m);
3493 h1m->err_pos = -1; // don't care about errors on the response path
3494 h2c->flags |= H2_CF_MUX_MFULL;
3495 h2s->flags |= H2_SF_BLK_MROOM;
3496 ret = 0;
3497 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003498}
3499
Willy Tarreau5dd17352018-06-14 13:33:30 +02003500/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3501 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3502 * the number of bytes sent. The caller must check the stream's status to
3503 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003505static 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 +02003506{
3507 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003508 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003509 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003510 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003511 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003512 int es_now = 0;
3513 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003514 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003515 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003516
3517 if (h2c_mux_busy(h2c, h2s)) {
3518 h2s->flags |= H2_SF_BLK_MBUSY;
3519 goto end;
3520 }
3521
Willy Tarreau44e973f2018-03-01 17:49:30 +01003522 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523 h2c->flags |= H2_CF_MUX_MALLOC;
3524 h2s->flags |= H2_SF_BLK_MROOM;
3525 goto end;
3526 }
3527
3528 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003529 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003530 goto end;
3531
3532 chunk_reset(&outbuf);
3533
3534 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003535 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003536 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003537 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003538
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003539 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003540 break;
3541 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003542 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003543 }
3544
3545 if (outbuf.size < 9) {
3546 h2c->flags |= H2_CF_MUX_MFULL;
3547 h2s->flags |= H2_SF_BLK_MROOM;
3548 goto end;
3549 }
3550
3551 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003552 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3553 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3554 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003555
3556 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3557 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003558 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003559 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003560 break;
3561 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003562 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003563 if ((long long)size > h1m->curr_len)
3564 size = h1m->curr_len;
3565 break;
3566 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003567 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003568 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003569 if (!ret)
3570 goto end;
3571
3572 if (ret < 0) {
3573 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003574 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003575 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3576 goto end;
3577 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003578 max -= ret;
3579 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003580 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003581 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003582 }
3583
Willy Tarreau801250e2018-09-11 11:45:04 +02003584 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003585 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003586 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003587 if (!ret)
3588 goto end;
3589
3590 if (ret < 0) {
3591 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003592 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003593 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3594 goto end;
3595 }
3596
3597 size = chunk;
3598 h1m->curr_len = chunk;
3599 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003600 max -= ret;
3601 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003602 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003603 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003604 if (!size)
3605 goto send_empty;
3606 }
3607
3608 /* in MSG_DATA state, continue below */
3609 size = h1m->curr_len;
3610 break;
3611 }
3612
3613 /* we have in <size> the exact number of bytes we need to copy from
3614 * the H1 buffer. We need to check this against the connection's and
3615 * the stream's send windows, and to ensure that this fits in the max
3616 * frame size and in the buffer's available space minus 9 bytes (for
3617 * the frame header). The connection's flow control is applied last so
3618 * that we can use a separate list of streams which are immediately
3619 * unblocked on window opening. Note: we don't implement padding.
3620 */
3621
Willy Tarreau5dd17352018-06-14 13:33:30 +02003622 if (size > max)
3623 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003624
3625 if (size > h2s->mws)
3626 size = h2s->mws;
3627
3628 if (size <= 0) {
3629 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003630 if (h2s->send_wait) {
3631 LIST_DEL(&h2s->list);
3632 LIST_INIT(&h2s->list);
3633 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003634 goto end;
3635 }
3636
3637 if (h2c->mfs && size > h2c->mfs)
3638 size = h2c->mfs;
3639
3640 if (size + 9 > outbuf.size) {
3641 /* we have an opportunity for enlarging the too small
3642 * available space, let's try.
3643 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003644 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003645 goto realign_again;
3646 size = outbuf.size - 9;
3647 }
3648
3649 if (size <= 0) {
3650 h2c->flags |= H2_CF_MUX_MFULL;
3651 h2s->flags |= H2_SF_BLK_MROOM;
3652 goto end;
3653 }
3654
3655 if (size > h2c->mws)
3656 size = h2c->mws;
3657
3658 if (size <= 0) {
3659 h2s->flags |= H2_SF_BLK_MFCTL;
3660 goto end;
3661 }
3662
3663 /* copy whatever we can */
3664 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003665 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003666 if (ret == 1)
3667 len2 = 0;
3668
3669 if (!ret || len1 + len2 < size) {
3670 /* FIXME: must normally never happen */
3671 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3672 goto end;
3673 }
3674
3675 /* limit len1/len2 to size */
3676 if (len1 + len2 > size) {
3677 int sub = len1 + len2 - size;
3678
3679 if (len2 > sub)
3680 len2 -= sub;
3681 else {
3682 sub -= len2;
3683 len2 = 0;
3684 len1 -= sub;
3685 }
3686 }
3687
3688 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003689 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003690 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003691 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003692
3693 send_empty:
3694 /* we may need to add END_STREAM */
3695 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3696 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003697 *
3698 * FIXME: what we do here is not correct because we send end_stream
3699 * before knowing if we'll have to send a HEADERS frame for the
3700 * trailers. More importantly we're not consuming the trailing CRLF
3701 * after the end of trailers, so it will be left to the caller to
3702 * eat it. The right way to do it would be to measure trailers here
3703 * and to send ES only if there are no trailers.
3704 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003705 */
3706 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003707 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003708 es_now = 1;
3709
3710 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003711 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003712
3713 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003714 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003715
3716 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003717 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003718
3719 /* consume incoming H1 response */
3720 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003721 max -= size;
3722 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003723 total += size;
3724 h1m->curr_len -= size;
3725 h2s->mws -= size;
3726 h2c->mws -= size;
3727
3728 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003729 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003730 goto new_frame;
3731 }
3732 }
3733
3734 if (es_now) {
3735 if (h2s->st == H2_SS_OPEN)
3736 h2s->st = H2_SS_HLOC;
3737 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003738 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003739
Willy Tarreau35a62702018-02-27 15:37:25 +01003740 if (!(h1m->flags & H1_MF_CHNK)) {
3741 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003742 total += max;
3743 ofs += max;
3744 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003745
Willy Tarreau801250e2018-09-11 11:45:04 +02003746 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003747 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003748
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003749 h2s->flags |= H2_SF_ES_SENT;
3750 }
3751
3752 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003753 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 +02003754 return total;
3755}
3756
Willy Tarreau115e83b2018-12-01 19:17:53 +01003757/* Try to send a HEADERS frame matching HTX response present in HTX message
3758 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3759 * must check the stream's status to detect any error which might have happened
3760 * subsequently to a successful send. The htx blocks are automatically removed
3761 * from the message. The htx message is assumed to be valid since produced from
3762 * the internal code, hence it contains a start line, an optional series of
3763 * header blocks and an end of header, otherwise an invalid frame could be
3764 * emitted and the resulting htx message could be left in an inconsistent state.
3765 */
3766static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3767{
3768 struct http_hdr list[MAX_HTTP_HDR];
3769 struct h2c *h2c = h2s->h2c;
3770 struct htx_blk *blk;
3771 struct htx_blk *blk_end;
3772 struct buffer outbuf;
3773 struct htx_sl *sl;
3774 enum htx_blk_type type;
3775 int es_now = 0;
3776 int ret = 0;
3777 int hdr;
3778 int idx;
3779
3780 if (h2c_mux_busy(h2c, h2s)) {
3781 h2s->flags |= H2_SF_BLK_MBUSY;
3782 return 0;
3783 }
3784
3785 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3786 h2c->flags |= H2_CF_MUX_MALLOC;
3787 h2s->flags |= H2_SF_BLK_MROOM;
3788 return 0;
3789 }
3790
3791 /* determine the first block which must not be deleted, blk_end may
3792 * be NULL if all blocks have to be deleted.
3793 */
3794 idx = htx_get_head(htx);
3795 blk_end = NULL;
3796 while (idx != -1) {
3797 type = htx_get_blk_type(htx_get_blk(htx, idx));
3798 idx = htx_get_next(htx, idx);
3799 if (type == HTX_BLK_EOH) {
3800 if (idx != -1)
3801 blk_end = htx_get_blk(htx, idx);
3802 break;
3803 }
3804 }
3805
3806 /* get the start line, we do have one */
3807 blk = htx_get_blk(htx, htx->sl_off);
3808 sl = htx_get_blk_ptr(htx, blk);
3809 h2s->status = sl->info.res.status;
3810
3811 /* and the rest of the headers, that we dump starting at header 0 */
3812 hdr = 0;
3813
Willy Tarreaufab9bb02018-12-02 12:11:16 +01003814 idx = htx->sl_off;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003815 while ((idx = htx_get_next(htx, idx)) != -1) {
3816 blk = htx_get_blk(htx, idx);
3817 type = htx_get_blk_type(blk);
3818
3819 if (type == HTX_BLK_UNUSED)
3820 continue;
3821
3822 if (type != HTX_BLK_HDR)
3823 break;
3824
3825 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3826 goto fail;
3827
3828 list[hdr].n = htx_get_blk_name(htx, blk);
3829 list[hdr].v = htx_get_blk_value(htx, blk);
3830
3831#if 1
3832 {
3833 /* FIXME: header names MUST be lower case in H2. For now it's
3834 * not granted by HTX so let's force them now.
3835 */
3836 char *p;
3837 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
3838 if (unlikely(isupper(*p)))
3839 *p = tolower(*p);
3840 }
3841#endif
3842 hdr++;
3843 }
3844
3845 /* marker for end of headers */
3846 list[hdr].n = ist("");
3847
3848 if (h2s->status == 204 || h2s->status == 304) {
3849 /* no contents, claim c-len is present and set to zero */
3850 es_now = 1;
3851 }
3852
3853 chunk_reset(&outbuf);
3854
3855 while (1) {
3856 outbuf.area = b_tail(&h2c->mbuf);
3857 outbuf.size = b_contig_space(&h2c->mbuf);
3858 outbuf.data = 0;
3859
3860 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3861 break;
3862 realign_again:
3863 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3864 }
3865
3866 if (outbuf.size < 9)
3867 goto full;
3868
3869 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3870 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3871 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3872 outbuf.data = 9;
3873
3874 /* encode status, which necessarily is the first one */
3875 if (outbuf.data < outbuf.size && h2s->status == 200)
3876 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3877 else if (outbuf.data < outbuf.size && h2s->status == 304)
3878 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
3879 else if (unlikely(h2s->status < 100 || h2s->status > 999)) {
3880 /* this is an unparsable response */
3881 goto fail;
3882 }
3883 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
3884 /* basic encoding of the status code */
3885 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3886 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3887 outbuf.area[outbuf.data++] = '0' + h2s->status / 100;
3888 outbuf.area[outbuf.data++] = '0' + h2s->status / 10 % 10;
3889 outbuf.area[outbuf.data++] = '0' + h2s->status % 10;
3890 }
3891 else {
3892 if (b_space_wraps(&h2c->mbuf))
3893 goto realign_again;
3894 goto full;
3895 }
3896
3897 /* encode all headers, stop at empty name */
3898 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3899 /* these ones do not exist in H2 and must be dropped. */
3900 if (isteq(list[hdr].n, ist("connection")) ||
3901 isteq(list[hdr].n, ist("proxy-connection")) ||
3902 isteq(list[hdr].n, ist("keep-alive")) ||
3903 isteq(list[hdr].n, ist("upgrade")) ||
3904 isteq(list[hdr].n, ist("transfer-encoding")))
3905 continue;
3906
3907 if (isteq(list[hdr].n, ist("")))
3908 break; // end
3909
3910 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3911 /* output full */
3912 if (b_space_wraps(&h2c->mbuf))
3913 goto realign_again;
3914 goto full;
3915 }
3916 }
3917
3918 /* we may need to add END_STREAM.
3919 * FIXME: we should also set it when we know for sure that the
3920 * content-length is zero as well as on 204/304
3921 */
3922 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3923 es_now = 1;
3924
3925 if (h2s->cs->flags & CS_FL_SHW)
3926 es_now = 1;
3927
3928 /* update the frame's size */
3929 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3930
3931 if (es_now)
3932 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3933
3934 /* commit the H2 response */
3935 b_add(&h2c->mbuf, outbuf.data);
3936 h2s->flags |= H2_SF_HEADERS_SENT;
3937
3938 /* for now we don't implemented CONTINUATION, so we wait for a
3939 * body or directly end in TRL2.
3940 */
3941 if (es_now) {
3942 h2s->flags |= H2_SF_ES_SENT;
3943 if (h2s->st == H2_SS_OPEN)
3944 h2s->st = H2_SS_HLOC;
3945 else
3946 h2s_close(h2s);
3947 }
3948
3949 /* OK we could properly deliver the response */
3950
3951 /* remove all header blocks including the EOH and compute the
3952 * corresponding size.
3953 *
3954 * FIXME: We should remove everything when es_now is set.
3955 */
3956 ret = 0;
3957 idx = htx_get_head(htx);
3958 blk = htx_get_blk(htx, idx);
3959 while (blk != blk_end) {
3960 ret += htx_get_blksz(blk);
3961 blk = htx_remove_blk(htx, blk);
3962 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003963
3964 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3965 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003966 end:
3967 return ret;
3968 full:
3969 h2c->flags |= H2_CF_MUX_MFULL;
3970 h2s->flags |= H2_SF_BLK_MROOM;
3971 ret = 0;
3972 goto end;
3973 fail:
3974 /* unparsable HTX messages, too large ones to be produced in the local
3975 * list etc go here (unrecoverable errors).
3976 */
3977 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3978 ret = 0;
3979 goto end;
3980}
3981
Willy Tarreau80739692018-10-05 11:35:57 +02003982/* Try to send a HEADERS frame matching HTX request present in HTX message
3983 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3984 * must check the stream's status to detect any error which might have happened
3985 * subsequently to a successful send. The htx blocks are automatically removed
3986 * from the message. The htx message is assumed to be valid since produced from
3987 * the internal code, hence it contains a start line, an optional series of
3988 * header blocks and an end of header, otherwise an invalid frame could be
3989 * emitted and the resulting htx message could be left in an inconsistent state.
3990 */
3991static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
3992{
3993 struct http_hdr list[MAX_HTTP_HDR];
3994 struct h2c *h2c = h2s->h2c;
3995 struct htx_blk *blk;
3996 struct htx_blk *blk_end;
3997 struct buffer outbuf;
3998 struct htx_sl *sl;
3999 struct ist meth, path;
4000 enum htx_blk_type type;
4001 int es_now = 0;
4002 int ret = 0;
4003 int hdr;
4004 int idx;
4005
4006 if (h2c_mux_busy(h2c, h2s)) {
4007 h2s->flags |= H2_SF_BLK_MBUSY;
4008 return 0;
4009 }
4010
4011 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4012 h2c->flags |= H2_CF_MUX_MALLOC;
4013 h2s->flags |= H2_SF_BLK_MROOM;
4014 return 0;
4015 }
4016
4017 /* determine the first block which must not be deleted, blk_end may
4018 * be NULL if all blocks have to be deleted.
4019 */
4020 idx = htx_get_head(htx);
4021 blk_end = NULL;
4022 while (idx != -1) {
4023 type = htx_get_blk_type(htx_get_blk(htx, idx));
4024 idx = htx_get_next(htx, idx);
4025 if (type == HTX_BLK_EOH) {
4026 if (idx != -1)
4027 blk_end = htx_get_blk(htx, idx);
4028 break;
4029 }
4030 }
4031
4032 /* get the start line, we do have one */
4033 blk = htx_get_blk(htx, htx->sl_off);
4034 sl = htx_get_blk_ptr(htx, blk);
4035 meth = htx_sl_req_meth(sl);
4036 path = htx_sl_req_uri(sl);
4037
4038 /* and the rest of the headers, that we dump starting at header 0 */
4039 hdr = 0;
4040
4041 idx = htx->sl_off;
4042 while ((idx = htx_get_next(htx, idx)) != -1) {
4043 blk = htx_get_blk(htx, idx);
4044 type = htx_get_blk_type(blk);
4045
4046 if (type == HTX_BLK_UNUSED)
4047 continue;
4048
4049 if (type != HTX_BLK_HDR)
4050 break;
4051
4052 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4053 goto fail;
4054
4055 list[hdr].n = htx_get_blk_name(htx, blk);
4056 list[hdr].v = htx_get_blk_value(htx, blk);
4057
4058#if 1
4059 {
4060 /* FIXME: header names MUST be lower case in H2. For now it's
4061 * not granted by HTX so let's force them now.
4062 */
4063 char *p;
4064 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
4065 if (unlikely(isupper(*p)))
4066 *p = tolower(*p);
4067 }
4068#endif
4069 hdr++;
4070 }
4071
4072 /* marker for end of headers */
4073 list[hdr].n = ist("");
4074
4075 chunk_reset(&outbuf);
4076
4077 while (1) {
4078 outbuf.area = b_tail(&h2c->mbuf);
4079 outbuf.size = b_contig_space(&h2c->mbuf);
4080 outbuf.data = 0;
4081
4082 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4083 break;
4084 realign_again:
4085 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4086 }
4087
4088 if (outbuf.size < 9)
4089 goto full;
4090
4091 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4092 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4093 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4094 outbuf.data = 9;
4095
4096 /* encode the method, which necessarily is the first one */
4097 if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_GET)
4098 outbuf.area[outbuf.data++] = 0x82; // indexed field : idx[02]=(":method", "GET")
4099 else if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_POST)
4100 outbuf.area[outbuf.data++] = 0x83; // indexed field : idx[03]=(":method", "POST")
4101 else if (unlikely(outbuf.data + 2 + meth.len <= outbuf.size)) {
4102 /* basic encoding of the method code */
4103 outbuf.area[outbuf.data++] = 0x42; // indexed name -- name=":method" (idx 2)
4104 outbuf.area[outbuf.data++] = meth.len; // method length
4105 memcpy(&outbuf.area[outbuf.data], meth.ptr, meth.len);
4106 }
4107 else {
4108 if (b_space_wraps(&h2c->mbuf))
4109 goto realign_again;
4110 goto full;
4111 }
4112
4113 /* encode the scheme which is always "https" (or 0x86 for "http") */
4114 if (outbuf.data < outbuf.size)
4115 outbuf.area[outbuf.data++] = 0x87; // indexed field : idx[02]=(":scheme", "https")
4116
4117 /* encode the path, which necessarily is the second one */
4118 if (outbuf.data < outbuf.size && isteq(path, ist("/"))) {
4119 outbuf.area[outbuf.data++] = 0x84; // indexed field : idx[04]=(":path", "/")
4120 }
4121 else if (outbuf.data < outbuf.size && isteq(path, ist("/index.html"))) {
4122 outbuf.area[outbuf.data++] = 0x85; // indexed field : idx[04]=(":path", "/index.html")
4123 }
4124 else if (!hpack_encode_header(&outbuf, ist(":path"), path)) {
4125 /* output full */
4126 if (b_space_wraps(&h2c->mbuf))
4127 goto realign_again;
4128 goto full;
4129 }
4130
4131 /* encode all headers, stop at empty name */
4132 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4133 /* these ones do not exist in H2 and must be dropped. */
4134 if (isteq(list[hdr].n, ist("connection")) ||
4135 isteq(list[hdr].n, ist("proxy-connection")) ||
4136 isteq(list[hdr].n, ist("keep-alive")) ||
4137 isteq(list[hdr].n, ist("upgrade")) ||
4138 isteq(list[hdr].n, ist("transfer-encoding")))
4139 continue;
4140
4141 if (isteq(list[hdr].n, ist("")))
4142 break; // end
4143
4144 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4145 /* output full */
4146 if (b_space_wraps(&h2c->mbuf))
4147 goto realign_again;
4148 goto full;
4149 }
4150 }
4151
4152 /* we may need to add END_STREAM if we have no body :
4153 * - request already closed, or :
4154 * - no transfer-encoding, and :
4155 * - no content-length or content-length:0
4156 * Fixme: this doesn't take into account CONNECT requests.
4157 */
4158 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4159 es_now = 1;
4160
4161 if (sl->flags & HTX_SL_F_BODYLESS)
4162 es_now = 1;
4163
4164 if (h2s->cs->flags & CS_FL_SHW)
4165 es_now = 1;
4166
4167 /* update the frame's size */
4168 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4169
4170 if (es_now)
4171 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4172
4173 /* commit the H2 response */
4174 b_add(&h2c->mbuf, outbuf.data);
4175 h2s->flags |= H2_SF_HEADERS_SENT;
4176 h2s->st = H2_SS_OPEN;
4177
4178 /* for now we don't implemented CONTINUATION, so we wait for a
4179 * body or directly end in TRL2.
4180 */
4181 if (es_now) {
4182 // trim any possibly pending data (eg: inconsistent content-length)
4183 h2s->flags |= H2_SF_ES_SENT;
4184 h2s->st = H2_SS_HLOC;
4185 }
4186
4187 /* remove all header blocks including the EOH and compute the
4188 * corresponding size.
4189 *
4190 * FIXME: We should remove everything when es_now is set.
4191 */
4192 ret = 0;
4193 idx = htx_get_head(htx);
4194 blk = htx_get_blk(htx, idx);
4195 while (blk != blk_end) {
4196 ret += htx_get_blksz(blk);
4197 blk = htx_remove_blk(htx, blk);
4198 }
4199
4200 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4201 htx_remove_blk(htx, blk_end);
4202
4203 end:
4204 return ret;
4205 full:
4206 h2c->flags |= H2_CF_MUX_MFULL;
4207 h2s->flags |= H2_SF_BLK_MROOM;
4208 ret = 0;
4209 goto end;
4210 fail:
4211 /* unparsable HTX messages, too large ones to be produced in the local
4212 * list etc go here (unrecoverable errors).
4213 */
4214 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4215 ret = 0;
4216 goto end;
4217}
4218
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004219/* Try to send a DATA frame matching HTTP response present in HTX structure
4220 * <htx>, for stream <h2s>. Returns the number of bytes sent. The caller must
4221 * check the stream's status to detect any error which might have happened
4222 * subsequently to a successful send. Returns the number of data bytes
4223 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4224 */
4225static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct htx *htx)
4226{
4227 struct h2c *h2c = h2s->h2c;
4228 struct buffer outbuf;
4229 size_t total = 0;
4230 int es_now = 0;
4231 int bsize; /* htx block size */
4232 int fsize; /* h2 frame size */
4233 struct htx_blk *blk;
4234 enum htx_blk_type type;
4235 int idx;
4236
4237 if (h2c_mux_busy(h2c, h2s)) {
4238 h2s->flags |= H2_SF_BLK_MBUSY;
4239 goto end;
4240 }
4241
4242 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4243 h2c->flags |= H2_CF_MUX_MALLOC;
4244 h2s->flags |= H2_SF_BLK_MROOM;
4245 goto end;
4246 }
4247
4248 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4249 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4250 * the caller to handle.
4251 */
4252
4253 new_frame:
4254 if (htx_is_empty(htx))
4255 goto end;
4256
4257 idx = htx_get_head(htx);
4258 blk = htx_get_blk(htx, idx);
4259 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4260 bsize = htx_get_blksz(blk);
4261 fsize = bsize;
4262
4263 if (type == HTX_BLK_EOD) {
4264 /* if we have an EOD, we're dealing with chunked data. We may
4265 * have a set of trailers after us that the caller will want to
4266 * deal with. Let's simply remove the EOD and return.
4267 */
4268 htx_remove_blk(htx, blk);
4269 // FIXME, it seems we must not return it in the total bytes count?
4270 //total++; // EOD counts as one byte
4271 goto end;
4272 }
4273
4274 /* for DATA and EOM we'll have to emit a frame, even if empty */
4275
4276 while (1) {
4277 outbuf.area = b_tail(&h2c->mbuf);
4278 outbuf.size = b_contig_space(&h2c->mbuf);
4279 outbuf.data = 0;
4280
4281 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4282 break;
4283 realign_again:
4284 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4285 }
4286
4287 if (outbuf.size < 9) {
4288 h2c->flags |= H2_CF_MUX_MFULL;
4289 h2s->flags |= H2_SF_BLK_MROOM;
4290 goto end;
4291 }
4292
4293 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4294 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4295 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4296 outbuf.data = 9;
4297
4298 /* we have in <fsize> the exact number of bytes we need to copy from
4299 * the HTX buffer. We need to check this against the connection's and
4300 * the stream's send windows, and to ensure that this fits in the max
4301 * frame size and in the buffer's available space minus 9 bytes (for
4302 * the frame header). The connection's flow control is applied last so
4303 * that we can use a separate list of streams which are immediately
4304 * unblocked on window opening. Note: we don't implement padding.
4305 */
4306
4307 /* EOM is presented with bsize==1 but would lead to the emission of an
4308 * empty frame, thus we force it to zero here.
4309 */
4310 if (type == HTX_BLK_EOM)
4311 bsize = fsize = 0;
4312
4313 if (!fsize)
4314 goto send_empty;
4315
4316 if (h2s->mws <= 0) {
4317 h2s->flags |= H2_SF_BLK_SFCTL;
4318 if (h2s->send_wait) {
4319 LIST_DEL(&h2s->list);
4320 LIST_INIT(&h2s->list);
4321 }
4322 goto end;
4323 }
4324
4325 if (fsize > h2s->mws)
4326 fsize = h2s->mws; // >0
4327
4328 if (h2c->mfs && fsize > h2c->mfs)
4329 fsize = h2c->mfs; // >0
4330
4331 if (fsize + 9 > outbuf.size) {
4332 /* we have an opportunity for enlarging the too small
4333 * available space, let's try.
4334 * FIXME: is this really interesting to do? Maybe we'll
4335 * spend lots of time realigning instead of using two
4336 * frames.
4337 */
4338 if (b_space_wraps(&h2c->mbuf))
4339 goto realign_again;
4340 fsize = outbuf.size - 9;
4341
4342 if (fsize <= 0) {
4343 /* no need to send an empty frame here */
4344 h2c->flags |= H2_CF_MUX_MFULL;
4345 h2s->flags |= H2_SF_BLK_MROOM;
4346 goto end;
4347 }
4348 }
4349
4350 if (h2c->mws <= 0) {
4351 h2s->flags |= H2_SF_BLK_MFCTL;
4352 goto end;
4353 }
4354
4355 if (fsize > h2c->mws)
4356 fsize = h2c->mws;
4357
4358 /* now let's copy this this into the output buffer */
4359 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
4360
4361 send_empty:
4362 /* update the frame's size */
4363 h2_set_frame_size(outbuf.area, fsize);
4364
4365 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4366 * meeting EOM. We should optimize this later.
4367 */
4368 if (type == HTX_BLK_EOM) {
4369 // FIXME, it seems we must not return it in the total bytes count?
4370 // total++; // EOM counts as one byte
4371 es_now = 1;
4372 }
4373
4374 if (es_now)
4375 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4376
4377 /* commit the H2 response */
4378 b_add(&h2c->mbuf, fsize + 9);
4379
4380 /* consume incoming HTX block, including EOM */
4381 total += fsize;
4382 if (fsize == bsize) {
4383 htx_remove_blk(htx, blk);
4384 if (fsize)
4385 goto new_frame;
4386 } else {
4387 /* we've truncated this block */
4388 htx_cut_data_blk(htx, blk, fsize);
4389 }
4390
4391 if (es_now) {
4392 if (h2s->st == H2_SS_OPEN)
4393 h2s->st = H2_SS_HLOC;
4394 else
4395 h2s_close(h2s);
4396
4397 h2s->flags |= H2_SF_ES_SENT;
4398 }
4399
4400 end:
4401 return total;
4402}
4403
Olivier Houchard6ff20392018-07-17 18:46:31 +02004404/* Called from the upper layer, to subscribe to events, such as being able to send */
4405static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4406{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004407 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004408 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004409 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004410
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004411 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004412 sw = param;
4413 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4414 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004415 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004416 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004417 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004418 event_type &= ~SUB_CAN_RECV;
4419 }
4420 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004421 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004422 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004423 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004424 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004425 h2s->send_wait = sw;
4426 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4427 if (h2s->flags & H2_SF_BLK_MFCTL)
4428 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4429 else
4430 LIST_ADDQ(&h2c->send_list, &h2s->list);
4431 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004432 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004433 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004434 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004435 if (event_type != 0)
4436 return -1;
4437 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004438
4439
4440}
4441
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004442static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4443{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004444 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004445 struct h2s *h2s = cs->ctx;
4446
4447 if (event_type & SUB_CAN_RECV) {
4448 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004449 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004450 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004451 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004452 }
4453 }
4454 if (event_type & SUB_CAN_SEND) {
4455 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004456 if (h2s->send_wait == sw) {
4457 LIST_DEL(&h2s->list);
4458 LIST_INIT(&h2s->list);
4459 sw->wait_reason &= ~SUB_CAN_SEND;
4460 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004461 }
4462 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004463 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4464 sw = param;
4465 if (h2s->send_wait == sw) {
4466 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4467 h2s->send_wait = NULL;
4468 }
4469 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004470 return 0;
4471}
4472
4473
Olivier Houchard511efea2018-08-16 15:30:32 +02004474/* Called from the upper layer, to receive data */
4475static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4476{
Olivier Houchard638b7992018-08-16 15:41:52 +02004477 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004478 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004479 struct htx *h2s_htx = NULL;
4480 struct htx *buf_htx = NULL;
4481 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004482 size_t ret = 0;
4483
4484 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004485 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4486 /* in HTX mode we ignore the count argument */
4487 h2s_htx = htx_from_buf(&h2s->rxbuf);
4488 if (htx_is_empty(h2s_htx))
4489 goto end;
4490
4491 buf_htx = htx_from_buf(buf);
4492 count = htx_free_space(buf_htx);
4493
4494 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, (h2s_htx->sl_off != -1) ? HTX_BLK_EOH : HTX_BLK_EOM);
4495
4496 buf_htx->extra = h2s_htx->extra;
4497 if (htx_is_not_empty(buf_htx))
4498 b_set_data(buf, b_size(buf));
4499 ret = htx_ret.ret;
4500 }
4501 else {
4502 ret = b_xfer(buf, &h2s->rxbuf, count);
4503 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004504
Olivier Houchard638b7992018-08-16 15:41:52 +02004505 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02004506 cs->flags |= CS_FL_RCV_MORE;
4507 else {
4508 cs->flags &= ~CS_FL_RCV_MORE;
4509 if (cs->flags & CS_FL_REOS)
4510 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004511 if (b_size(&h2s->rxbuf)) {
4512 b_free(&h2s->rxbuf);
4513 offer_buffers(NULL, tasks_run_queue);
4514 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004515 }
4516
Willy Tarreau082f5592018-11-25 08:03:32 +01004517 if (ret && h2c->dsi == h2s->id) {
4518 /* demux is blocking on this stream's buffer */
4519 h2c->flags &= ~H2_CF_DEM_SFULL;
4520 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4521 if (h2_recv_allowed(h2c))
4522 tasklet_wakeup(h2c->wait_event.task);
4523 }
4524 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004525end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004526 return ret;
4527}
4528
Olivier Houchardd846c262018-10-19 17:24:29 +02004529static void h2_stop_senders(struct h2c *h2c)
4530{
4531 struct h2s *h2s, *h2s_back;
4532
4533 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4534 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4535 if (h2c->msi == h2s_id(h2s))
4536 continue;
4537 LIST_DEL(&h2s->list);
4538 LIST_INIT(&h2s->list);
4539 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4540 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4541 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4542 LIST_ADD(&h2c->send_list, &h2s->list);
4543 }
4544}
4545
Willy Tarreau62f52692017-10-08 23:01:42 +02004546/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004547static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004548{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004549 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004550 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004551 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004552 struct htx *htx;
4553 struct htx_blk *blk;
4554 enum htx_blk_type btype;
4555 uint32_t bsize;
4556 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004557
Olivier Houchardd846c262018-10-19 17:24:29 +02004558 if (h2s->send_wait) {
4559 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4560 h2s->send_wait = NULL;
4561 LIST_DEL(&h2s->list);
4562 LIST_INIT(&h2s->list);
4563 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004564 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4565 return 0;
4566
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004567 /* htx will be enough to decide if we're using HTX or legacy */
4568 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4569
Willy Tarreau0bad0432018-06-14 16:54:01 +02004570 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004571 h2s->flags |= H2_SF_OUTGOING_DATA;
4572
Willy Tarreau751f2d02018-10-05 09:35:00 +02004573 if (h2s->id == 0) {
4574 int32_t id = h2c_get_next_sid(h2s->h2c);
4575
4576 if (id < 0) {
4577 cs->ctx = NULL;
4578 cs->flags |= CS_FL_ERROR;
4579 h2s_destroy(h2s);
4580 return 0;
4581 }
4582
4583 eb32_delete(&h2s->by_id);
4584 h2s->by_id.key = h2s->id = id;
4585 h2s->h2c->max_id = id;
4586 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4587 }
4588
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004589 if (htx) {
4590 while (count && !htx_is_empty(htx)) {
4591 idx = htx_get_head(htx);
4592 blk = htx_get_blk(htx, idx);
4593 btype = htx_get_blk_type(blk);
4594 bsize = htx_get_blksz(blk);
4595
4596 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004597 case HTX_BLK_REQ_SL:
4598 /* start-line before headers */
4599 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4600 if (ret > 0) {
4601 total += ret;
4602 count -= ret;
4603 if (ret < bsize)
4604 goto done;
4605 }
4606 break;
4607
Willy Tarreau115e83b2018-12-01 19:17:53 +01004608 case HTX_BLK_RES_SL:
4609 /* start-line before headers */
4610 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4611 if (ret > 0) {
4612 total += ret;
4613 count -= ret;
4614 if (ret < bsize)
4615 goto done;
4616 }
4617 break;
4618
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004619 case HTX_BLK_DATA:
4620 case HTX_BLK_EOD:
4621 case HTX_BLK_EOM:
4622 /* all these cause the emission of a DATA frame (possibly empty) */
4623 ret = h2s_htx_frt_make_resp_data(h2s, htx);
4624 if (ret > 0) {
4625 total += ret;
4626 count -= ret;
4627 if (ret < bsize)
4628 goto done;
4629 }
4630 break;
4631
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004632 default:
4633 htx_remove_blk(htx, blk);
4634 total += bsize;
4635 count -= bsize;
4636 break;
4637 }
4638 }
4639 goto done;
4640 }
4641
4642 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004643 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004644 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004645 if (h2s->h2c->flags & H2_CF_IS_BACK)
4646 ret = -1;
4647 else
4648 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004649 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004650 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004651 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004652 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004653 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004654 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004655 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004656
Willy Tarreau5dd17352018-06-14 13:33:30 +02004657 if (unlikely((int)ret <= 0)) {
4658 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004659 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4660 break;
4661 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004662 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004663 total += count;
4664 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004665 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004666 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004667 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004668 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004669 cs->flags |= CS_FL_ERROR;
4670 break;
4671 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004672
4673 total += ret;
4674 count -= ret;
4675
4676 if (h2s->st >= H2_SS_ERROR)
4677 break;
4678
4679 if (h2s->flags & H2_SF_BLK_ANY)
4680 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004681 }
4682
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004683 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004684 if (h2s->st >= H2_SS_ERROR) {
4685 /* trim any possibly pending data after we close (extra CR-LF,
4686 * unprocessed trailers, abnormal extra data, ...)
4687 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004688 total += count;
4689 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004690 }
4691
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004692 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004693 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004694 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004695 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004696 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004697 }
4698
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004699 if (htx) {
4700 if (htx_is_empty(htx)) {
4701 htx_reset(htx);
4702 b_set_data(buf, 0);
4703 }
4704 } else {
4705 b_del(buf, total);
4706 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004707
4708 /* The mux is full, cancel the pending tasks */
4709 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4710 (h2s->flags & H2_SF_BLK_MBUSY))
4711 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004712
Olivier Houchard7505f942018-08-21 18:10:44 +02004713 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004714 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4715 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004716
Olivier Houchard7505f942018-08-21 18:10:44 +02004717 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004718 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004719}
4720
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004721/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004722static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004723{
4724 struct h2c *h2c = conn->mux_ctx;
4725 struct h2s *h2s;
4726 struct eb32_node *node;
4727 int fctl_cnt = 0;
4728 int send_cnt = 0;
4729 int tree_cnt = 0;
4730 int orph_cnt = 0;
4731
4732 if (!h2c)
4733 return;
4734
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004735 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004736 fctl_cnt++;
4737
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004738 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004739 send_cnt++;
4740
4741 node = eb32_first(&h2c->streams_by_id);
4742 while (node) {
4743 h2s = container_of(node, struct h2s, by_id);
4744 tree_cnt++;
4745 if (!h2s->cs)
4746 orph_cnt++;
4747 node = eb32_next(node);
4748 }
4749
Willy Tarreau616ac812018-07-24 14:12:42 +02004750 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4751 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4752 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4753 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4754 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4755 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004756}
Willy Tarreau62f52692017-10-08 23:01:42 +02004757
4758/*******************************************************/
4759/* functions below are dedicated to the config parsers */
4760/*******************************************************/
4761
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004762/* config parser for global "tune.h2.header-table-size" */
4763static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4764 struct proxy *defpx, const char *file, int line,
4765 char **err)
4766{
4767 if (too_many_args(1, args, err, NULL))
4768 return -1;
4769
4770 h2_settings_header_table_size = atoi(args[1]);
4771 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4772 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4773 return -1;
4774 }
4775 return 0;
4776}
Willy Tarreau62f52692017-10-08 23:01:42 +02004777
Willy Tarreaue6baec02017-07-27 11:45:11 +02004778/* config parser for global "tune.h2.initial-window-size" */
4779static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4780 struct proxy *defpx, const char *file, int line,
4781 char **err)
4782{
4783 if (too_many_args(1, args, err, NULL))
4784 return -1;
4785
4786 h2_settings_initial_window_size = atoi(args[1]);
4787 if (h2_settings_initial_window_size < 0) {
4788 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4789 return -1;
4790 }
4791 return 0;
4792}
4793
Willy Tarreau5242ef82017-07-27 11:47:28 +02004794/* config parser for global "tune.h2.max-concurrent-streams" */
4795static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4796 struct proxy *defpx, const char *file, int line,
4797 char **err)
4798{
4799 if (too_many_args(1, args, err, NULL))
4800 return -1;
4801
4802 h2_settings_max_concurrent_streams = atoi(args[1]);
4803 if (h2_settings_max_concurrent_streams < 0) {
4804 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4805 return -1;
4806 }
4807 return 0;
4808}
4809
Willy Tarreau62f52692017-10-08 23:01:42 +02004810
4811/****************************************/
4812/* MUX initialization and instanciation */
4813/***************************************/
4814
4815/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004816static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004817 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004818 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004819 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004820 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004821 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004822 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004823 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004824 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004825 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004826 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004827 .avail_streams = h2_avail_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004828 .shutr = h2_shutr,
4829 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004830 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004831 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004832 .name = "H2",
4833};
4834
Christopher Faulet32f61c02018-04-10 14:33:41 +02004835/* PROTO selection : this mux registers PROTO token "h2" */
4836static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004837 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004838
Willy Tarreau0108d902018-11-25 19:14:37 +01004839INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4840
Willy Tarreauf8957272018-10-03 10:25:20 +02004841static struct mux_proto_list mux_proto_h2_htx =
4842 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4843
4844INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4845
Willy Tarreau62f52692017-10-08 23:01:42 +02004846/* config keyword parsers */
4847static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004848 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004849 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004850 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004851 { 0, NULL, NULL }
4852}};
4853
Willy Tarreau0108d902018-11-25 19:14:37 +01004854INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);