blob: 3faf5aeef85f0eccad86cf152a8fd0afccd5574d [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 Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200123 struct wait_list wait_list; /* We're in a wait list, to send */
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 Houchard8ae735d2018-09-11 18:24:28 +0200186 struct wait_list wait_list; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_list *recv_wait_list; /* Address of the wait_list the conn_stream associated is waiting on */
Willy Tarreau18312642017-10-11 07:57:07 +0200188};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200189
Willy Tarreauc6405142017-09-21 20:23:50 +0200190/* descriptor for an h2 frame header */
191struct h2_fh {
192 uint32_t len; /* length, host order, 24 bits */
193 uint32_t sid; /* stream id, host order, 31 bits */
194 uint8_t ft; /* frame type */
195 uint8_t ff; /* frame flags */
196};
197
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200198/* a few settings from the global section */
199static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200200static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200201static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200202
Willy Tarreau2a856182017-05-16 15:20:39 +0200203/* a dmumy closed stream */
204static const struct h2s *h2_closed_stream = &(const struct h2s){
205 .cs = NULL,
206 .h2c = NULL,
207 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100208 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100209 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200210 .id = 0,
211};
212
213/* and a dummy idle stream for use with any unannounced stream */
214static const struct h2s *h2_idle_stream = &(const struct h2s){
215 .cs = NULL,
216 .h2c = NULL,
217 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100218 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200219 .id = 0,
220};
221
Olivier Houchard9f6af332018-05-25 14:04:04 +0200222static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200223static int h2_send(struct h2c *h2c);
224static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200225static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200226static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100227static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100228static int h2_frt_decode_headers(struct h2s *h2s);
229static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200230static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200232/*****************************************************/
233/* functions below are for dynamic buffer management */
234/*****************************************************/
235
Willy Tarreau315d8072017-12-10 22:17:57 +0100236/* indicates whether or not the we may call the h2_recv() function to attempt
237 * to receive data into the buffer and/or demux pending data. The condition is
238 * a bit complex due to some API limits for now. The rules are the following :
239 * - if an error or a shutdown was detected on the connection and the buffer
240 * is empty, we must not attempt to receive
241 * - if the demux buf failed to be allocated, we must not try to receive and
242 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100243 * - if no flag indicates a blocking condition, we may attempt to receive,
244 * regardless of whether the demux buffer is full or not, so that only
245 * de demux part decides whether or not to block. This is needed because
246 * the connection API indeed prevents us from re-enabling receipt that is
247 * already enabled in a polled state, so we must always immediately stop
248 * as soon as the demux can't proceed so as never to hit an end of read
249 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 * - otherwise must may not attempt
251 */
252static inline int h2_recv_allowed(const struct h2c *h2c)
253{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200254 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100255 (h2c->st0 >= H2_CS_ERROR ||
256 h2c->conn->flags & CO_FL_ERROR ||
257 conn_xprt_read0_pending(h2c->conn)))
258 return 0;
259
260 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100261 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100262 return 1;
263
264 return 0;
265}
266
Willy Tarreauf2101912018-07-19 10:11:38 +0200267/* returns true if the connection has too many conn_streams attached */
268static inline int h2_has_too_many_cs(const struct h2c *h2c)
269{
270 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
271}
272
Willy Tarreau44e973f2018-03-01 17:49:30 +0100273/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
274 * flags are used to figure what buffer was requested. It returns 1 if the
275 * allocation succeeds, in which case the connection is woken up, or 0 if it's
276 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100278static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200279{
280 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100281 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282
Willy Tarreau44e973f2018-03-01 17:49:30 +0100283 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200284 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200285 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200286 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200287 if (h2_recv(h2c))
288 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200289 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200290 return 1;
291 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200292
Willy Tarreau44e973f2018-03-01 17:49:30 +0100293 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
294 h2c->flags &= ~H2_CF_MUX_MALLOC;
295 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
296 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200297
298 if (h2c->flags & H2_CF_DEM_MROOM) {
299 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200300 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200302 if (h2_recv(h2c))
303 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200304 }
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200305 }
Willy Tarreau14398122017-09-22 14:26:04 +0200306 return 1;
307 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100308
309 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
310 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200311 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100312 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200313 if (h2_recv_allowed(h2c)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100314 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200315 if (h2_recv(h2c))
316 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200317 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100318 return 1;
319 }
320
Willy Tarreau14398122017-09-22 14:26:04 +0200321 return 0;
322}
323
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200324static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200325{
326 struct buffer *buf = NULL;
327
Willy Tarreau44e973f2018-03-01 17:49:30 +0100328 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
329 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
330 h2c->buf_wait.target = h2c;
331 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100333 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100334 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200335 __conn_xprt_stop_recv(h2c->conn);
336 }
337 return buf;
338}
339
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200340static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200341{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200342 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100343 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200344 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200345 }
346}
347
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200348
Willy Tarreau62f52692017-10-08 23:01:42 +0200349/*****************************************************************/
350/* functions below are dedicated to the mux setup and management */
351/*****************************************************************/
352
Willy Tarreau32218eb2017-09-22 08:07:25 +0200353/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
354static int h2c_frt_init(struct connection *conn)
355{
356 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100357 struct task *t = NULL;
358 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200359
Willy Tarreaubafbe012017-11-24 17:34:44 +0100360 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200361 if (!h2c)
362 goto fail;
363
Willy Tarreau3f133572017-10-31 19:21:06 +0100364
Willy Tarreau599391a2017-11-24 10:16:00 +0100365 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
366 if (tick_isset(sess->fe->timeout.clientfin))
367 h2c->shut_timeout = sess->fe->timeout.clientfin;
368
Willy Tarreau33400292017-11-05 11:23:40 +0100369 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100370 if (tick_isset(h2c->timeout)) {
371 t = task_new(tid_bit);
372 if (!t)
373 goto fail;
374
375 h2c->task = t;
376 t->process = h2_timeout_task;
377 t->context = h2c;
378 t->expire = tick_add(now_ms, h2c->timeout);
379 }
Willy Tarreauea392822017-10-31 10:02:25 +0100380
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200381 h2c->wait_list.task = tasklet_new();
382 if (!h2c->wait_list.task)
383 goto fail;
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200384 h2c->wait_list.task->process = h2_io_cb;
385 h2c->wait_list.task->context = h2c;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200386 h2c->wait_list.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200387
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
389 if (!h2c->ddht)
390 goto fail;
391
392 /* Initialise the context. */
393 h2c->st0 = H2_CS_PREFACE;
394 h2c->conn = conn;
395 h2c->max_id = -1;
396 h2c->errcode = H2_ERR_NO_ERROR;
397 h2c->flags = H2_CF_NONE;
398 h2c->rcvd_c = 0;
399 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100400 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200401 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200403 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 h2c->dsi = -1;
405 h2c->msi = -1;
406 h2c->last_sid = -1;
407
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200408 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409 h2c->miw = 65535; /* mux initial window size */
410 h2c->mws = 65535; /* mux window size */
411 h2c->mfs = 16384; /* initial max frame size */
412 h2c->streams_by_id = EB_ROOT_UNIQUE;
413 LIST_INIT(&h2c->send_list);
414 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100415 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416 conn->mux_ctx = h2c;
417
Willy Tarreau3f133572017-10-31 19:21:06 +0100418 if (t)
419 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 conn_xprt_want_recv(conn);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200421 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100422
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200423 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard7505f942018-08-21 18:10:44 +0200424 if (h2_recv(h2c))
425 h2_process(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 return 0;
427 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100428 if (t)
429 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200430 if (h2c->wait_list.task)
431 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100432 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 return -1;
434}
435
Willy Tarreau62f52692017-10-08 23:01:42 +0200436/* Initialize the mux once it's attached. For outgoing connections, the context
437 * is already initialized before installing the mux, so we detect incoming
438 * connections from the fact that the context is still NULL. Returns < 0 on
439 * error.
440 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +0200441static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau62f52692017-10-08 23:01:42 +0200442{
443 if (conn->mux_ctx) {
444 /* we don't support outgoing connections for now */
445 return -1;
446 }
447
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200449}
450
Willy Tarreau2373acc2017-10-12 17:35:14 +0200451/* returns the stream associated with id <id> or NULL if not found */
452static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
453{
454 struct eb32_node *node;
455
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 if (id > h2c->max_id)
457 return (struct h2s *)h2_idle_stream;
458
Willy Tarreau2373acc2017-10-12 17:35:14 +0200459 node = eb32_lookup(&h2c->streams_by_id, id);
460 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200461 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200462
463 return container_of(node, struct h2s, by_id);
464}
465
Willy Tarreau62f52692017-10-08 23:01:42 +0200466/* release function for a connection. This one should be called to free all
467 * resources allocated to the mux.
468 */
469static void h2_release(struct connection *conn)
470{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200471 struct h2c *h2c = conn->mux_ctx;
472
473 LIST_DEL(&conn->list);
474
475 if (h2c) {
476 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200477
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100479 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100480 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200481
Willy Tarreau44e973f2018-03-01 17:49:30 +0100482 h2_release_buf(h2c, &h2c->dbuf);
483 h2_release_buf(h2c, &h2c->mbuf);
484
Willy Tarreauea392822017-10-31 10:02:25 +0100485 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200486 h2c->task->context = NULL;
487 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100488 h2c->task = NULL;
489 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200490 if (h2c->wait_list.task)
491 tasklet_free(h2c->wait_list.task);
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200492 LIST_DEL(&h2c->wait_list.list);
493 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100494
Willy Tarreaubafbe012017-11-24 17:34:44 +0100495 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200496 }
497
498 conn->mux = NULL;
499 conn->mux_ctx = NULL;
500
501 conn_stop_tracking(conn);
502 conn_full_close(conn);
503 if (conn->destroy_cb)
504 conn->destroy_cb(conn);
505 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200506}
507
508
Willy Tarreau71681172017-10-23 14:39:06 +0200509/******************************************************/
510/* functions below are for the H2 protocol processing */
511/******************************************************/
512
513/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100514static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200515{
516 return h2s ? h2s->id : 0;
517}
518
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200519/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100520static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200521{
522 if (h2c->msi < 0)
523 return 0;
524
525 if (h2c->msi == h2s_id(h2s))
526 return 0;
527
528 return 1;
529}
530
Willy Tarreau741d6df2017-10-17 08:00:59 +0200531/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100532static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200533{
534 h2c->errcode = err;
535 h2c->st0 = H2_CS_ERROR;
536}
537
Willy Tarreau2e43f082017-10-17 08:03:59 +0200538/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100539static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200540{
541 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
542 h2s->errcode = err;
543 h2s->st = H2_SS_ERROR;
544 if (h2s->cs)
545 h2s->cs->flags |= CS_FL_ERROR;
546 }
547}
548
Willy Tarreaue4820742017-07-27 13:37:23 +0200549/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100550static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200551{
552 uint8_t *out = frame;
553
554 *out = len >> 16;
555 write_n16(out + 1, len);
556}
557
Willy Tarreau54c15062017-10-10 17:10:03 +0200558/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
559 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
560 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200561 * available in the buffer's input prior to calling this function. The buffer
562 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200563 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100564static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200565 const struct buffer *b, int o)
566{
Willy Tarreau591d4452018-06-15 17:21:00 +0200567 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 +0200568}
569
Willy Tarreau1f094672017-11-20 21:27:45 +0100570static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200571{
Willy Tarreau591d4452018-06-15 17:21:00 +0200572 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200573}
574
Willy Tarreau1f094672017-11-20 21:27:45 +0100575static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200576{
Willy Tarreau591d4452018-06-15 17:21:00 +0200577 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200578}
579
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200581{
Willy Tarreau591d4452018-06-15 17:21:00 +0200582 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200583}
584
585
Willy Tarreau715d5312017-07-11 15:20:24 +0200586/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
587 * is not obvious. It turns out that H2 headers are neither aligned nor do they
588 * use regular sizes. And to add to the trouble, the buffer may wrap so each
589 * byte read must be checked. The header is formed like this :
590 *
591 * b0 b1 b2 b3 b4 b5..b8
592 * +----------+---------+--------+----+----+----------------------+
593 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
594 * +----------+---------+--------+----+----+----------------------+
595 *
596 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
597 * we get the sid properly aligned and ordered, and 16 bits of len properly
598 * ordered as well. The type and flags can be extracted using bit shifts from
599 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200600 * Returns zero if some bytes are missing, otherwise non-zero on success. The
601 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200602 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100603static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200604{
605 uint64_t w;
606
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200607 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200608 return 0;
609
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200610 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200611 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200612 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
613 h->ff = w >> 32;
614 h->ft = w >> 40;
615 h->len += w >> 48;
616 return 1;
617}
618
619/* skip the next 9 bytes corresponding to the frame header possibly parsed by
620 * h2_peek_frame_hdr() above.
621 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100622static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200623{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200624 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200625}
626
627/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100628static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200629{
630 int ret;
631
632 ret = h2_peek_frame_hdr(b, h);
633 if (ret > 0)
634 h2_skip_frame_hdr(b);
635 return ret;
636}
637
Willy Tarreau00dd0782018-03-01 16:31:34 +0100638/* marks stream <h2s> as CLOSED and decrement the number of active streams for
639 * its connection if the stream was not yet closed. Please use this exclusively
640 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100641 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100642static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100643{
644 if (h2s->st != H2_SS_CLOSED)
645 h2s->h2c->nb_streams--;
646 h2s->st = H2_SS_CLOSED;
647}
648
Willy Tarreau71049cc2018-03-28 13:56:39 +0200649/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
650static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100651{
652 h2s_close(h2s);
653 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200654 if (b_size(&h2s->rxbuf)) {
655 b_free(&h2s->rxbuf);
656 offer_buffers(NULL, tasks_run_queue);
657 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200658 LIST_DEL(&h2s->wait_list.list);
659 LIST_INIT(&h2s->wait_list.list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200660 tasklet_free(h2s->wait_list.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100661 pool_free(pool_head_h2s, h2s);
662}
663
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200664/* creates a new stream <id> on the h2c connection and returns it, or NULL in
665 * case of memory allocation error.
666 */
667static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
668{
Willy Tarreau590a0512018-09-05 11:56:48 +0200669 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200670 struct conn_stream *cs;
671 struct h2s *h2s;
672
Willy Tarreaubafbe012017-11-24 17:34:44 +0100673 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200674 if (!h2s)
675 goto out;
676
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200677 h2s->wait_list.task = tasklet_new();
678 if (!h2s->wait_list.task) {
679 pool_free(pool_head_h2s, h2s);
680 goto out;
681 }
682 LIST_INIT(&h2s->wait_list.list);
683 h2s->recv_wait_list = NULL;
684 h2s->wait_list.task->process = h2_deferred_shut;
685 h2s->wait_list.task->context = h2s;
686 h2s->wait_list.handle = NULL;
687 h2s->wait_list.wait_reason = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200688 h2s->h2c = h2c;
689 h2s->mws = h2c->miw;
690 h2s->flags = H2_SF_NONE;
691 h2s->errcode = H2_ERR_NO_ERROR;
692 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200693 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200694 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200695 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200696 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200697 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200698 h2s->by_id.key = h2s->id = id;
699 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200700
701 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100702 h2c->nb_streams++;
703 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
704 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200705
706 cs = cs_new(h2c->conn);
707 if (!cs)
708 goto out_close;
709
710 h2s->cs = cs;
711 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200712 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200713
714 if (stream_create_from_cs(cs) < 0)
715 goto out_free_cs;
716
Willy Tarreau590a0512018-09-05 11:56:48 +0200717 /* We want the accept date presented to the next stream to be the one
718 * we have now, the handshake time to be null (since the next stream
719 * is not delayed by a handshake), and the idle time to count since
720 * right now.
721 */
722 sess->accept_date = date;
723 sess->tv_accept = now;
724 sess->t_handshake = 0;
725
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200726 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200727 if (h2_has_too_many_cs(h2c))
728 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200729 return h2s;
730
731 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200732 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200733 cs_free(cs);
734 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200735 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200736 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200737 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200738 out:
739 return h2s;
740}
741
Willy Tarreaube5b7152017-09-25 16:25:39 +0200742/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
743 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
744 * the various settings codes.
745 */
746static int h2c_snd_settings(struct h2c *h2c)
747{
748 struct buffer *res;
749 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200750 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200751 int ret;
752
753 if (h2c_mux_busy(h2c, NULL)) {
754 h2c->flags |= H2_CF_DEM_MBUSY;
755 return 0;
756 }
757
Willy Tarreau44e973f2018-03-01 17:49:30 +0100758 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200759 if (!res) {
760 h2c->flags |= H2_CF_MUX_MALLOC;
761 h2c->flags |= H2_CF_DEM_MROOM;
762 return 0;
763 }
764
765 chunk_init(&buf, buf_data, sizeof(buf_data));
766 chunk_memcpy(&buf,
767 "\x00\x00\x00" /* length : 0 for now */
768 "\x04\x00" /* type : 4 (settings), flags : 0 */
769 "\x00\x00\x00\x00", /* stream ID : 0 */
770 9);
771
772 if (h2_settings_header_table_size != 4096) {
773 char str[6] = "\x00\x01"; /* header_table_size */
774
775 write_n32(str + 2, h2_settings_header_table_size);
776 chunk_memcat(&buf, str, 6);
777 }
778
779 if (h2_settings_initial_window_size != 65535) {
780 char str[6] = "\x00\x04"; /* initial_window_size */
781
782 write_n32(str + 2, h2_settings_initial_window_size);
783 chunk_memcat(&buf, str, 6);
784 }
785
786 if (h2_settings_max_concurrent_streams != 0) {
787 char str[6] = "\x00\x03"; /* max_concurrent_streams */
788
789 /* Note: 0 means "unlimited" for haproxy's config but not for
790 * the protocol, so never send this value!
791 */
792 write_n32(str + 2, h2_settings_max_concurrent_streams);
793 chunk_memcat(&buf, str, 6);
794 }
795
796 if (global.tune.bufsize != 16384) {
797 char str[6] = "\x00\x05"; /* max_frame_size */
798
799 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
800 * match bufsize - rewrite size, but at the moment it seems
801 * that clients don't take care of it.
802 */
803 write_n32(str + 2, global.tune.bufsize);
804 chunk_memcat(&buf, str, 6);
805 }
806
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200807 h2_set_frame_size(buf.area, buf.data - 9);
808 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200809 if (unlikely(ret <= 0)) {
810 if (!ret) {
811 h2c->flags |= H2_CF_MUX_MFULL;
812 h2c->flags |= H2_CF_DEM_MROOM;
813 return 0;
814 }
815 else {
816 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
817 return 0;
818 }
819 }
820 return ret;
821}
822
Willy Tarreau52eed752017-09-22 15:05:09 +0200823/* Try to receive a connection preface, then upon success try to send our
824 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
825 * missing data. It may return an error in h2c.
826 */
827static int h2c_frt_recv_preface(struct h2c *h2c)
828{
829 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200830 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200831
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200832 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200833
834 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200835 if (ret1 < 0)
836 sess_log(h2c->conn->owner);
837
Willy Tarreau52eed752017-09-22 15:05:09 +0200838 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
839 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
840 return 0;
841 }
842
Willy Tarreaube5b7152017-09-25 16:25:39 +0200843 ret2 = h2c_snd_settings(h2c);
844 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200845 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200846
Willy Tarreaube5b7152017-09-25 16:25:39 +0200847 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200848}
849
Willy Tarreau081d4722017-05-16 21:51:05 +0200850/* try to send a GOAWAY frame on the connection to report an error or a graceful
851 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
852 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
853 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
854 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
855 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
856 * on unrecoverable failure. It will not attempt to send one again in this last
857 * case so that it is safe to use h2c_error() to report such errors.
858 */
859static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
860{
861 struct buffer *res;
862 char str[17];
863 int ret;
864
865 if (h2c->flags & H2_CF_GOAWAY_FAILED)
866 return 1; // claim that it worked
867
868 if (h2c_mux_busy(h2c, h2s)) {
869 if (h2s)
870 h2s->flags |= H2_SF_BLK_MBUSY;
871 else
872 h2c->flags |= H2_CF_DEM_MBUSY;
873 return 0;
874 }
875
Willy Tarreau44e973f2018-03-01 17:49:30 +0100876 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200877 if (!res) {
878 h2c->flags |= H2_CF_MUX_MALLOC;
879 if (h2s)
880 h2s->flags |= H2_SF_BLK_MROOM;
881 else
882 h2c->flags |= H2_CF_DEM_MROOM;
883 return 0;
884 }
885
886 /* len: 8, type: 7, flags: none, sid: 0 */
887 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
888
889 if (h2c->last_sid < 0)
890 h2c->last_sid = h2c->max_id;
891
892 write_n32(str + 9, h2c->last_sid);
893 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200894 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200895 if (unlikely(ret <= 0)) {
896 if (!ret) {
897 h2c->flags |= H2_CF_MUX_MFULL;
898 if (h2s)
899 h2s->flags |= H2_SF_BLK_MROOM;
900 else
901 h2c->flags |= H2_CF_DEM_MROOM;
902 return 0;
903 }
904 else {
905 /* we cannot report this error using GOAWAY, so we mark
906 * it and claim a success.
907 */
908 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
909 h2c->flags |= H2_CF_GOAWAY_FAILED;
910 return 1;
911 }
912 }
913 h2c->flags |= H2_CF_GOAWAY_SENT;
914 return ret;
915}
916
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100917/* Try to send an RST_STREAM frame on the connection for the indicated stream
918 * during mux operations. This stream must be valid and cannot be closed
919 * already. h2s->id will be used for the stream ID and h2s->errcode will be
920 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
921 * not yet.
922 *
923 * Returns > 0 on success or zero if nothing was done. In case of lack of room
924 * to write the message, it subscribes the stream to future notifications.
925 */
926static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
927{
928 struct buffer *res;
929 char str[13];
930 int ret;
931
932 if (!h2s || h2s->st == H2_SS_CLOSED)
933 return 1;
934
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100935 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
936 * RST_STREAM in response to a RST_STREAM frame.
937 */
938 if (h2c->dft == H2_FT_RST_STREAM) {
939 ret = 1;
940 goto ignore;
941 }
942
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100943 if (h2c_mux_busy(h2c, h2s)) {
944 h2s->flags |= H2_SF_BLK_MBUSY;
945 return 0;
946 }
947
Willy Tarreau44e973f2018-03-01 17:49:30 +0100948 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100949 if (!res) {
950 h2c->flags |= H2_CF_MUX_MALLOC;
951 h2s->flags |= H2_SF_BLK_MROOM;
952 return 0;
953 }
954
955 /* len: 4, type: 3, flags: none */
956 memcpy(str, "\x00\x00\x04\x03\x00", 5);
957 write_n32(str + 5, h2s->id);
958 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200959 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100960
961 if (unlikely(ret <= 0)) {
962 if (!ret) {
963 h2c->flags |= H2_CF_MUX_MFULL;
964 h2s->flags |= H2_SF_BLK_MROOM;
965 return 0;
966 }
967 else {
968 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
969 return 0;
970 }
971 }
972
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100973 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100974 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100975 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100976 return ret;
977}
978
979/* Try to send an RST_STREAM frame on the connection for the stream being
980 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
981 * error code unless the stream's state already is IDLE or CLOSED in which
982 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
983 * it was not yet.
984 *
985 * Returns > 0 on success or zero if nothing was done. In case of lack of room
986 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200987 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100988 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200989 */
990static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
991{
992 struct buffer *res;
993 char str[13];
994 int ret;
995
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100996 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
997 * RST_STREAM in response to a RST_STREAM frame.
998 */
999 if (h2c->dft == H2_FT_RST_STREAM) {
1000 ret = 1;
1001 goto ignore;
1002 }
1003
Willy Tarreau27a84c92017-10-17 08:10:17 +02001004 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001005 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001006 return 0;
1007 }
1008
Willy Tarreau44e973f2018-03-01 17:49:30 +01001009 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001010 if (!res) {
1011 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001012 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001013 return 0;
1014 }
1015
1016 /* len: 4, type: 3, flags: none */
1017 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001018
Willy Tarreau27a84c92017-10-17 08:10:17 +02001019 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +01001020 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +02001021 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001022 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001023
Willy Tarreau27a84c92017-10-17 08:10:17 +02001024 if (unlikely(ret <= 0)) {
1025 if (!ret) {
1026 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001027 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001028 return 0;
1029 }
1030 else {
1031 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1032 return 0;
1033 }
1034 }
1035
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001036 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001037 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001038 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001039 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001040 }
1041
Willy Tarreau27a84c92017-10-17 08:10:17 +02001042 return ret;
1043}
1044
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001045/* try to send an empty DATA frame with the ES flag set to notify about the
1046 * end of stream and match a shutdown(write). If an ES was already sent as
1047 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1048 * on success or zero if nothing was done. In case of lack of room to write the
1049 * message, it subscribes the requesting stream to future notifications.
1050 */
1051static int h2_send_empty_data_es(struct h2s *h2s)
1052{
1053 struct h2c *h2c = h2s->h2c;
1054 struct buffer *res;
1055 char str[9];
1056 int ret;
1057
Willy Tarreau721c9742017-11-07 11:05:42 +01001058 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001059 return 1;
1060
1061 if (h2c_mux_busy(h2c, h2s)) {
1062 h2s->flags |= H2_SF_BLK_MBUSY;
1063 return 0;
1064 }
1065
Willy Tarreau44e973f2018-03-01 17:49:30 +01001066 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001067 if (!res) {
1068 h2c->flags |= H2_CF_MUX_MALLOC;
1069 h2s->flags |= H2_SF_BLK_MROOM;
1070 return 0;
1071 }
1072
1073 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1074 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1075 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001076 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001077 if (likely(ret > 0)) {
1078 h2s->flags |= H2_SF_ES_SENT;
1079 }
1080 else if (!ret) {
1081 h2c->flags |= H2_CF_MUX_MFULL;
1082 h2s->flags |= H2_SF_BLK_MROOM;
1083 return 0;
1084 }
1085 else {
1086 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1087 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001088 }
1089 return ret;
1090}
1091
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001092/* wake the streams attached to the connection, whose id is greater than <last>,
1093 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001094 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1095 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001096 */
1097static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1098{
1099 struct eb32_node *node;
1100 struct h2s *h2s;
1101
1102 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1103 flags |= CS_FL_ERROR;
1104
1105 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001106 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001107
1108 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1109 while (node) {
1110 h2s = container_of(node, struct h2s, by_id);
1111 if (h2s->id <= last)
1112 break;
1113 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001114
1115 if (!h2s->cs) {
1116 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001117 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001118 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001119 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001120
1121 h2s->cs->flags |= flags;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001122 if (h2s->recv_wait_list) {
1123 struct wait_list *sw = h2s->recv_wait_list;
1124 sw->wait_reason &= ~SUB_CAN_RECV;
1125 tasklet_wakeup(sw->task);
1126 h2s->recv_wait_list = NULL;
1127 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001128
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001129 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1130 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001131 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001132 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001133 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001134 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001135 }
1136}
1137
Willy Tarreau3421aba2017-07-27 15:41:03 +02001138/* Increase all streams' outgoing window size by the difference passed in
1139 * argument. This is needed upon receipt of the settings frame if the initial
1140 * window size is different. The difference may be negative and the resulting
1141 * window size as well, for the time it takes to receive some window updates.
1142 */
1143static void h2c_update_all_ws(struct h2c *h2c, int diff)
1144{
1145 struct h2s *h2s;
1146 struct eb32_node *node;
1147
1148 if (!diff)
1149 return;
1150
1151 node = eb32_first(&h2c->streams_by_id);
1152 while (node) {
1153 h2s = container_of(node, struct h2s, by_id);
1154 h2s->mws += diff;
1155 node = eb32_next(node);
1156 }
1157}
1158
1159/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1160 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1161 * return an error in h2c. Described in RFC7540#6.5.
1162 */
1163static int h2c_handle_settings(struct h2c *h2c)
1164{
1165 unsigned int offset;
1166 int error;
1167
1168 if (h2c->dff & H2_F_SETTINGS_ACK) {
1169 if (h2c->dfl) {
1170 error = H2_ERR_FRAME_SIZE_ERROR;
1171 goto fail;
1172 }
1173 return 1;
1174 }
1175
1176 if (h2c->dsi != 0) {
1177 error = H2_ERR_PROTOCOL_ERROR;
1178 goto fail;
1179 }
1180
1181 if (h2c->dfl % 6) {
1182 error = H2_ERR_FRAME_SIZE_ERROR;
1183 goto fail;
1184 }
1185
1186 /* that's the limit we can process */
1187 if (h2c->dfl > global.tune.bufsize) {
1188 error = H2_ERR_FRAME_SIZE_ERROR;
1189 goto fail;
1190 }
1191
1192 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001193 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001194 return 0;
1195
1196 /* parse the frame */
1197 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001198 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1199 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001200
1201 switch (type) {
1202 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1203 /* we need to update all existing streams with the
1204 * difference from the previous iws.
1205 */
1206 if (arg < 0) { // RFC7540#6.5.2
1207 error = H2_ERR_FLOW_CONTROL_ERROR;
1208 goto fail;
1209 }
1210 h2c_update_all_ws(h2c, arg - h2c->miw);
1211 h2c->miw = arg;
1212 break;
1213 case H2_SETTINGS_MAX_FRAME_SIZE:
1214 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1215 error = H2_ERR_PROTOCOL_ERROR;
1216 goto fail;
1217 }
1218 h2c->mfs = arg;
1219 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001220 case H2_SETTINGS_ENABLE_PUSH:
1221 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1222 error = H2_ERR_PROTOCOL_ERROR;
1223 goto fail;
1224 }
1225 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001226 }
1227 }
1228
1229 /* need to ACK this frame now */
1230 h2c->st0 = H2_CS_FRAME_A;
1231 return 1;
1232 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001233 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001234 h2c_error(h2c, error);
1235 return 0;
1236}
1237
1238/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1239 * success or one of the h2_status values.
1240 */
1241static int h2c_ack_settings(struct h2c *h2c)
1242{
1243 struct buffer *res;
1244 char str[9];
1245 int ret = -1;
1246
1247 if (h2c_mux_busy(h2c, NULL)) {
1248 h2c->flags |= H2_CF_DEM_MBUSY;
1249 return 0;
1250 }
1251
Willy Tarreau44e973f2018-03-01 17:49:30 +01001252 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001253 if (!res) {
1254 h2c->flags |= H2_CF_MUX_MALLOC;
1255 h2c->flags |= H2_CF_DEM_MROOM;
1256 return 0;
1257 }
1258
1259 memcpy(str,
1260 "\x00\x00\x00" /* length : 0 (no data) */
1261 "\x04" "\x01" /* type : 4, flags : ACK */
1262 "\x00\x00\x00\x00" /* stream ID */, 9);
1263
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001264 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001265 if (unlikely(ret <= 0)) {
1266 if (!ret) {
1267 h2c->flags |= H2_CF_MUX_MFULL;
1268 h2c->flags |= H2_CF_DEM_MROOM;
1269 return 0;
1270 }
1271 else {
1272 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1273 return 0;
1274 }
1275 }
1276 return ret;
1277}
1278
Willy Tarreaucf68c782017-10-10 17:11:41 +02001279/* processes a PING frame and schedules an ACK if needed. The caller must pass
1280 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1281 * missing data. It may return an error in h2c.
1282 */
1283static int h2c_handle_ping(struct h2c *h2c)
1284{
1285 /* frame length must be exactly 8 */
1286 if (h2c->dfl != 8) {
1287 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1288 return 0;
1289 }
1290
1291 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001292 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001293 h2c->st0 = H2_CS_FRAME_A;
1294 return 1;
1295}
1296
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001297/* Try to send a window update for stream id <sid> and value <increment>.
1298 * Returns > 0 on success or zero on missing room or failure. It may return an
1299 * error in h2c.
1300 */
1301static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1302{
1303 struct buffer *res;
1304 char str[13];
1305 int ret = -1;
1306
1307 if (h2c_mux_busy(h2c, NULL)) {
1308 h2c->flags |= H2_CF_DEM_MBUSY;
1309 return 0;
1310 }
1311
Willy Tarreau44e973f2018-03-01 17:49:30 +01001312 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001313 if (!res) {
1314 h2c->flags |= H2_CF_MUX_MALLOC;
1315 h2c->flags |= H2_CF_DEM_MROOM;
1316 return 0;
1317 }
1318
1319 /* length: 4, type: 8, flags: none */
1320 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1321 write_n32(str + 5, sid);
1322 write_n32(str + 9, increment);
1323
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001324 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001325
1326 if (unlikely(ret <= 0)) {
1327 if (!ret) {
1328 h2c->flags |= H2_CF_MUX_MFULL;
1329 h2c->flags |= H2_CF_DEM_MROOM;
1330 return 0;
1331 }
1332 else {
1333 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1334 return 0;
1335 }
1336 }
1337 return ret;
1338}
1339
1340/* try to send pending window update for the connection. It's safe to call it
1341 * with no pending updates. Returns > 0 on success or zero on missing room or
1342 * failure. It may return an error in h2c.
1343 */
1344static int h2c_send_conn_wu(struct h2c *h2c)
1345{
1346 int ret = 1;
1347
1348 if (h2c->rcvd_c <= 0)
1349 return 1;
1350
1351 /* send WU for the connection */
1352 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1353 if (ret > 0)
1354 h2c->rcvd_c = 0;
1355
1356 return ret;
1357}
1358
1359/* try to send pending window update for the current dmux stream. It's safe to
1360 * call it with no pending updates. Returns > 0 on success or zero on missing
1361 * room or failure. It may return an error in h2c.
1362 */
1363static int h2c_send_strm_wu(struct h2c *h2c)
1364{
1365 int ret = 1;
1366
1367 if (h2c->rcvd_s <= 0)
1368 return 1;
1369
1370 /* send WU for the stream */
1371 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1372 if (ret > 0)
1373 h2c->rcvd_s = 0;
1374
1375 return ret;
1376}
1377
Willy Tarreaucf68c782017-10-10 17:11:41 +02001378/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1379 * success, 0 on missing data or one of the h2_status values.
1380 */
1381static int h2c_ack_ping(struct h2c *h2c)
1382{
1383 struct buffer *res;
1384 char str[17];
1385 int ret = -1;
1386
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001387 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001388 return 0;
1389
1390 if (h2c_mux_busy(h2c, NULL)) {
1391 h2c->flags |= H2_CF_DEM_MBUSY;
1392 return 0;
1393 }
1394
Willy Tarreau44e973f2018-03-01 17:49:30 +01001395 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001396 if (!res) {
1397 h2c->flags |= H2_CF_MUX_MALLOC;
1398 h2c->flags |= H2_CF_DEM_MROOM;
1399 return 0;
1400 }
1401
1402 memcpy(str,
1403 "\x00\x00\x08" /* length : 8 (same payload) */
1404 "\x06" "\x01" /* type : 6, flags : ACK */
1405 "\x00\x00\x00\x00" /* stream ID */, 9);
1406
1407 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001408 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001409
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001410 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001411 if (unlikely(ret <= 0)) {
1412 if (!ret) {
1413 h2c->flags |= H2_CF_MUX_MFULL;
1414 h2c->flags |= H2_CF_DEM_MROOM;
1415 return 0;
1416 }
1417 else {
1418 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1419 return 0;
1420 }
1421 }
1422 return ret;
1423}
1424
Willy Tarreau26f95952017-07-27 17:18:30 +02001425/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1426 * Returns > 0 on success or zero on missing data. It may return an error in
1427 * h2c or h2s. Described in RFC7540#6.9.
1428 */
1429static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1430{
1431 int32_t inc;
1432 int error;
1433
1434 if (h2c->dfl != 4) {
1435 error = H2_ERR_FRAME_SIZE_ERROR;
1436 goto conn_err;
1437 }
1438
1439 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001440 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001441 return 0;
1442
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001443 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001444
1445 if (h2c->dsi != 0) {
1446 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001447
1448 /* it's not an error to receive WU on a closed stream */
1449 if (h2s->st == H2_SS_CLOSED)
1450 return 1;
1451
1452 if (!inc) {
1453 error = H2_ERR_PROTOCOL_ERROR;
1454 goto strm_err;
1455 }
1456
1457 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1458 error = H2_ERR_FLOW_CONTROL_ERROR;
1459 goto strm_err;
1460 }
1461
1462 h2s->mws += inc;
1463 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1464 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02001465 /* The task will be waken up later */
Willy Tarreau26f95952017-07-27 17:18:30 +02001466 }
1467 }
1468 else {
1469 /* connection window update */
1470 if (!inc) {
1471 error = H2_ERR_PROTOCOL_ERROR;
1472 goto conn_err;
1473 }
1474
1475 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1476 error = H2_ERR_FLOW_CONTROL_ERROR;
1477 goto conn_err;
1478 }
1479
1480 h2c->mws += inc;
1481 }
1482
1483 return 1;
1484
1485 conn_err:
1486 h2c_error(h2c, error);
1487 return 0;
1488
1489 strm_err:
1490 if (h2s) {
1491 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001492 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001493 }
1494 else
1495 h2c_error(h2c, error);
1496 return 0;
1497}
1498
Willy Tarreaue96b0922017-10-30 00:28:29 +01001499/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1500 * the last ID. Returns > 0 on success or zero on missing data. It may return
1501 * an error in h2c. Described in RFC7540#6.8.
1502 */
1503static int h2c_handle_goaway(struct h2c *h2c)
1504{
1505 int error;
1506 int last;
1507
1508 if (h2c->dsi != 0) {
1509 error = H2_ERR_PROTOCOL_ERROR;
1510 goto conn_err;
1511 }
1512
1513 if (h2c->dfl < 8) {
1514 error = H2_ERR_FRAME_SIZE_ERROR;
1515 goto conn_err;
1516 }
1517
1518 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001519 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001520 return 0;
1521
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001522 last = h2_get_n32(&h2c->dbuf, 0);
1523 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001524 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001525 if (h2c->last_sid < 0)
1526 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001527 return 1;
1528
1529 conn_err:
1530 h2c_error(h2c, error);
1531 return 0;
1532}
1533
Willy Tarreau92153fc2017-12-03 19:46:19 +01001534/* processes a PRIORITY frame, and either skips it or rejects if it is
1535 * invalid. Returns > 0 on success or zero on missing data. It may return
1536 * an error in h2c. Described in RFC7540#6.3.
1537 */
1538static int h2c_handle_priority(struct h2c *h2c)
1539{
1540 int error;
1541
1542 if (h2c->dsi == 0) {
1543 error = H2_ERR_PROTOCOL_ERROR;
1544 goto conn_err;
1545 }
1546
1547 if (h2c->dfl != 5) {
1548 error = H2_ERR_FRAME_SIZE_ERROR;
1549 goto conn_err;
1550 }
1551
1552 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001553 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001554 return 0;
1555
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001556 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001557 /* 7540#5.3 : can't depend on itself */
1558 error = H2_ERR_PROTOCOL_ERROR;
1559 goto conn_err;
1560 }
1561 return 1;
1562
1563 conn_err:
1564 h2c_error(h2c, error);
1565 return 0;
1566}
1567
Willy Tarreaucd234e92017-08-18 10:59:39 +02001568/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1569 * Returns > 0 on success or zero on missing data. It may return an error in
1570 * h2c. Described in RFC7540#6.4.
1571 */
1572static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1573{
1574 int error;
1575
1576 if (h2c->dsi == 0) {
1577 error = H2_ERR_PROTOCOL_ERROR;
1578 goto conn_err;
1579 }
1580
Willy Tarreaucd234e92017-08-18 10:59:39 +02001581 if (h2c->dfl != 4) {
1582 error = H2_ERR_FRAME_SIZE_ERROR;
1583 goto conn_err;
1584 }
1585
1586 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001587 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001588 return 0;
1589
1590 /* late RST, already handled */
1591 if (h2s->st == H2_SS_CLOSED)
1592 return 1;
1593
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001594 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001595 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001596
1597 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001598 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001599 if (h2s->recv_wait_list) {
1600 struct wait_list *sw = h2s->recv_wait_list;
1601
1602 sw->wait_reason &= ~SUB_CAN_RECV;
1603 tasklet_wakeup(sw->task);
1604 h2s->recv_wait_list = NULL;
1605 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001606 }
1607
1608 h2s->flags |= H2_SF_RST_RCVD;
1609 return 1;
1610
1611 conn_err:
1612 h2c_error(h2c, error);
1613 return 0;
1614}
1615
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001616/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1617 * It may return an error in h2c or h2s. The caller must consider that the
1618 * return value is the new h2s in case one was allocated (most common case).
1619 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001620 * errors here are reported as connection errors since it's impossible to
1621 * recover from such errors after the compression context has been altered.
1622 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001623static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001624{
1625 int error;
1626
1627 if (!h2c->dfl) {
1628 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001629 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001630 goto strm_err;
1631 }
1632
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001633 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001634 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001635
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001636 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001637 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001638
Willy Tarreauf2101912018-07-19 10:11:38 +02001639 if (h2c->flags & H2_CF_DEM_TOOMANY)
1640 return 0; // too many cs still present
1641
Willy Tarreau13278b42017-10-13 19:23:14 +02001642 /* now either the frame is complete or the buffer is complete */
1643 if (h2s->st != H2_SS_IDLE) {
1644 /* FIXME: stream already exists, this is only allowed for
1645 * trailers (not supported for now).
1646 */
1647 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001648 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001649 goto conn_err;
1650 }
1651 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1652 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1653 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001654 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001655 goto conn_err;
1656 }
1657
Willy Tarreau22de8d32018-09-05 19:55:58 +02001658 /* Note: we don't emit any other logs below because ff we return
1659 * positively from h2c_stream_new(), the stream will report the error,
1660 * and if we return in error, h2c_stream_new() will emit the error.
1661 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001662 h2s = h2c_stream_new(h2c, h2c->dsi);
1663 if (!h2s) {
1664 error = H2_ERR_INTERNAL_ERROR;
1665 goto conn_err;
1666 }
1667
1668 h2s->st = H2_SS_OPEN;
1669 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1670 h2s->st = H2_SS_HREM;
1671 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001672 /* note: cs cannot be null for now (just created above) */
1673 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001674 }
1675
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001676 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001677 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001678
Willy Tarreau8f650c32017-11-21 19:36:21 +01001679 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001680 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001681
Willy Tarreau721c9742017-11-07 11:05:42 +01001682 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001683 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001684 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001685 }
1686 else {
1687 /* update the max stream ID if the request is being processed */
1688 if (h2s->id > h2c->max_id)
1689 h2c->max_id = h2s->id;
1690 }
1691
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001692 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001693
1694 conn_err:
1695 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001696 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001697
1698 strm_err:
1699 if (h2s) {
1700 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001701 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001702 }
1703 else
1704 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001705 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001706}
1707
Willy Tarreau454f9052017-10-26 19:40:35 +02001708/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1709 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1710 */
1711static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1712{
1713 int error;
1714
1715 /* note that empty DATA frames are perfectly valid and sometimes used
1716 * to signal an end of stream (with the ES flag).
1717 */
1718
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001720 return 0; // empty buffer
1721
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001722 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001723 return 0; // incomplete frame
1724
1725 /* now either the frame is complete or the buffer is complete */
1726
1727 if (!h2c->dsi) {
1728 /* RFC7540#6.1 */
1729 error = H2_ERR_PROTOCOL_ERROR;
1730 goto conn_err;
1731 }
1732
1733 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1734 /* RFC7540#6.1 */
1735 error = H2_ERR_STREAM_CLOSED;
1736 goto strm_err;
1737 }
1738
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001739 if (!h2_frt_transfer_data(h2s))
1740 return 0;
1741
Willy Tarreau454f9052017-10-26 19:40:35 +02001742 /* call the upper layers to process the frame, then let the upper layer
1743 * notify the stream about any change.
1744 */
1745 if (!h2s->cs) {
1746 error = H2_ERR_STREAM_CLOSED;
1747 goto strm_err;
1748 }
1749
Willy Tarreau8f650c32017-11-21 19:36:21 +01001750 if (h2c->st0 >= H2_CS_ERROR)
1751 return 0;
1752
Willy Tarreau721c9742017-11-07 11:05:42 +01001753 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001754 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001755 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001756 }
1757
1758 /* check for completion : the callee will change this to FRAME_A or
1759 * FRAME_H once done.
1760 */
1761 if (h2c->st0 == H2_CS_FRAME_P)
1762 return 0;
1763
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001764
1765 /* last frame */
1766 if (h2c->dff & H2_F_DATA_END_STREAM) {
1767 h2s->st = H2_SS_HREM;
1768 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001769 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001770 }
1771
Willy Tarreau454f9052017-10-26 19:40:35 +02001772 return 1;
1773
1774 conn_err:
1775 h2c_error(h2c, error);
1776 return 0;
1777
1778 strm_err:
1779 if (h2s) {
1780 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001781 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001782 }
1783 else
1784 h2c_error(h2c, error);
1785 return 0;
1786}
1787
Willy Tarreaubc933932017-10-09 16:21:43 +02001788/* process Rx frames to be demultiplexed */
1789static void h2_process_demux(struct h2c *h2c)
1790{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001791 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001792
Willy Tarreau081d4722017-05-16 21:51:05 +02001793 if (h2c->st0 >= H2_CS_ERROR)
1794 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001795
1796 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1797 if (h2c->st0 == H2_CS_PREFACE) {
1798 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1799 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001800 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001801 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001802 sess_log(h2c->conn->owner);
1803 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001804 goto fail;
1805 }
1806
1807 h2c->max_id = 0;
1808 h2c->st0 = H2_CS_SETTINGS1;
1809 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001810
1811 if (h2c->st0 == H2_CS_SETTINGS1) {
1812 struct h2_fh hdr;
1813
1814 /* ensure that what is pending is a valid SETTINGS frame
1815 * without an ACK.
1816 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001817 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001818 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001819 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001820 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001821 sess_log(h2c->conn->owner);
1822 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001823 goto fail;
1824 }
1825
1826 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1827 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1828 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1829 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001830 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001831 goto fail;
1832 }
1833
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001834 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001835 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1836 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1837 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001838 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001839 goto fail;
1840 }
1841
1842 /* that's OK, switch to FRAME_P to process it */
1843 h2c->dfl = hdr.len;
1844 h2c->dsi = hdr.sid;
1845 h2c->dft = hdr.ft;
1846 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001847 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001848 h2c->st0 = H2_CS_FRAME_P;
1849 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001850 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001851
1852 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001853 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001854 int ret = 0;
1855
1856 if (h2c->st0 >= H2_CS_ERROR)
1857 break;
1858
1859 if (h2c->st0 == H2_CS_FRAME_H) {
1860 struct h2_fh hdr;
1861
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001862 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001863 break;
1864
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001865 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001866 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1867 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001868 if (!h2c->nb_streams) {
1869 /* only log if no other stream can report the error */
1870 sess_log(h2c->conn->owner);
1871 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001872 break;
1873 }
1874
1875 h2c->dfl = hdr.len;
1876 h2c->dsi = hdr.sid;
1877 h2c->dft = hdr.ft;
1878 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001879 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001880 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001881 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001882 }
1883
1884 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001885 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1886
Olivier Houchard638b7992018-08-16 15:41:52 +02001887 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001888 /* we may have to signal the upper layers */
1889 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001890 if (h2s->recv_wait_list) {
1891 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
1892 tasklet_wakeup(h2s->recv_wait_list->task);
1893 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001894 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001895 if (h2c->st0 >= H2_CS_ERROR)
1896 goto strm_err;
1897
1898 if (h2s->st >= H2_SS_ERROR) {
1899 /* stream error : send RST_STREAM */
1900 h2c->st0 = H2_CS_FRAME_E;
1901 }
1902 }
1903 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001904
Willy Tarreaud7901432017-12-29 11:34:40 +01001905 if (h2c->st0 == H2_CS_FRAME_E)
1906 goto strm_err;
1907
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001908 if (h2s->st == H2_SS_IDLE &&
1909 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1910 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1911 * this state MUST be treated as a connection error
1912 */
1913 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1914 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001915 if (!h2c->nb_streams) {
1916 /* only log if no other stream can report the error */
1917 sess_log(h2c->conn->owner);
1918 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001919 break;
1920 }
1921
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001922 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1923 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1924 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1925 * this state MUST be treated as a stream error
1926 */
1927 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1928 goto strm_err;
1929 }
1930
Willy Tarreauab837502017-12-27 15:07:30 +01001931 /* Below the management of frames received in closed state is a
1932 * bit hackish because the spec makes strong differences between
1933 * streams closed by receiving RST, sending RST, and seeing ES
1934 * in both directions. In addition to this, the creation of a
1935 * new stream reusing the identifier of a closed one will be
1936 * detected here. Given that we cannot keep track of all closed
1937 * streams forever, we consider that unknown closed streams were
1938 * closed on RST received, which allows us to respond with an
1939 * RST without breaking the connection (eg: to abort a transfer).
1940 * Some frames have to be silently ignored as well.
1941 */
1942 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1943 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1944 /* #5.1.1: The identifier of a newly
1945 * established stream MUST be numerically
1946 * greater than all streams that the initiating
1947 * endpoint has opened or reserved. This
1948 * governs streams that are opened using a
1949 * HEADERS frame and streams that are reserved
1950 * using PUSH_PROMISE. An endpoint that
1951 * receives an unexpected stream identifier
1952 * MUST respond with a connection error.
1953 */
1954 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1955 goto strm_err;
1956 }
1957
1958 if (h2s->flags & H2_SF_RST_RCVD) {
1959 /* RFC7540#5.1:closed: an endpoint that
1960 * receives any frame other than PRIORITY after
1961 * receiving a RST_STREAM MUST treat that as a
1962 * stream error of type STREAM_CLOSED.
1963 *
1964 * Note that old streams fall into this category
1965 * and will lead to an RST being sent.
1966 */
1967 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1968 h2c->st0 = H2_CS_FRAME_E;
1969 goto strm_err;
1970 }
1971
1972 /* RFC7540#5.1:closed: if this state is reached as a
1973 * result of sending a RST_STREAM frame, the peer that
1974 * receives the RST_STREAM might have already sent
1975 * frames on the stream that cannot be withdrawn. An
1976 * endpoint MUST ignore frames that it receives on
1977 * closed streams after it has sent a RST_STREAM
1978 * frame. An endpoint MAY choose to limit the period
1979 * over which it ignores frames and treat frames that
1980 * arrive after this time as being in error.
1981 */
1982 if (!(h2s->flags & H2_SF_RST_SENT)) {
1983 /* RFC7540#5.1:closed: any frame other than
1984 * PRIO/WU/RST in this state MUST be treated as
1985 * a connection error
1986 */
1987 if (h2c->dft != H2_FT_RST_STREAM &&
1988 h2c->dft != H2_FT_PRIORITY &&
1989 h2c->dft != H2_FT_WINDOW_UPDATE) {
1990 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1991 goto strm_err;
1992 }
1993 }
1994 }
1995
Willy Tarreauc0da1962017-10-30 18:38:00 +01001996#if 0
1997 // problem below: it is not possible to completely ignore such
1998 // streams as we need to maintain the compression state as well
1999 // and for this we need to completely process these frames (eg:
2000 // HEADERS frames) as well as counting DATA frames to emit
2001 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2002 // This is a typical case of layer violation where the
2003 // transported contents are critical to the connection's
2004 // validity and must be ignored at the same time :-(
2005
2006 /* graceful shutdown, ignore streams whose ID is higher than
2007 * the one advertised in GOAWAY. RFC7540#6.8.
2008 */
2009 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002010 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2011 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002012 h2c->dfl -= ret;
2013 ret = h2c->dfl == 0;
2014 goto strm_err;
2015 }
2016#endif
2017
Willy Tarreau7e98c052017-10-10 15:56:59 +02002018 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002019 case H2_FT_SETTINGS:
2020 if (h2c->st0 == H2_CS_FRAME_P)
2021 ret = h2c_handle_settings(h2c);
2022
2023 if (h2c->st0 == H2_CS_FRAME_A)
2024 ret = h2c_ack_settings(h2c);
2025 break;
2026
Willy Tarreaucf68c782017-10-10 17:11:41 +02002027 case H2_FT_PING:
2028 if (h2c->st0 == H2_CS_FRAME_P)
2029 ret = h2c_handle_ping(h2c);
2030
2031 if (h2c->st0 == H2_CS_FRAME_A)
2032 ret = h2c_ack_ping(h2c);
2033 break;
2034
Willy Tarreau26f95952017-07-27 17:18:30 +02002035 case H2_FT_WINDOW_UPDATE:
2036 if (h2c->st0 == H2_CS_FRAME_P)
2037 ret = h2c_handle_window_update(h2c, h2s);
2038 break;
2039
Willy Tarreau61290ec2017-10-17 08:19:21 +02002040 case H2_FT_CONTINUATION:
2041 /* we currently don't support CONTINUATION frames since
2042 * we have nowhere to store the partial HEADERS frame.
2043 * Let's abort the stream on an INTERNAL_ERROR here.
2044 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002045 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002046 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002047 h2c->st0 = H2_CS_FRAME_E;
2048 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002049 break;
2050
Willy Tarreau13278b42017-10-13 19:23:14 +02002051 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002052 if (h2c->st0 == H2_CS_FRAME_P) {
2053 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2054 if (tmp_h2s) {
2055 h2s = tmp_h2s;
2056 ret = 1;
2057 }
2058 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002059 break;
2060
Willy Tarreau454f9052017-10-26 19:40:35 +02002061 case H2_FT_DATA:
2062 if (h2c->st0 == H2_CS_FRAME_P)
2063 ret = h2c_frt_handle_data(h2c, h2s);
2064
2065 if (h2c->st0 == H2_CS_FRAME_A)
2066 ret = h2c_send_strm_wu(h2c);
2067 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002068
Willy Tarreau92153fc2017-12-03 19:46:19 +01002069 case H2_FT_PRIORITY:
2070 if (h2c->st0 == H2_CS_FRAME_P)
2071 ret = h2c_handle_priority(h2c);
2072 break;
2073
Willy Tarreaucd234e92017-08-18 10:59:39 +02002074 case H2_FT_RST_STREAM:
2075 if (h2c->st0 == H2_CS_FRAME_P)
2076 ret = h2c_handle_rst_stream(h2c, h2s);
2077 break;
2078
Willy Tarreaue96b0922017-10-30 00:28:29 +01002079 case H2_FT_GOAWAY:
2080 if (h2c->st0 == H2_CS_FRAME_P)
2081 ret = h2c_handle_goaway(h2c);
2082 break;
2083
Willy Tarreau1c661982017-10-30 13:52:01 +01002084 case H2_FT_PUSH_PROMISE:
2085 /* not permitted here, RFC7540#5.1 */
2086 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002087 if (!h2c->nb_streams) {
2088 /* only log if no other stream can report the error */
2089 sess_log(h2c->conn->owner);
2090 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002091 break;
2092
2093 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002094 default:
2095 /* drop frames that we ignore. They may be larger than
2096 * the buffer so we drain all of their contents until
2097 * we reach the end.
2098 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002099 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2100 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002101 h2c->dfl -= ret;
2102 ret = h2c->dfl == 0;
2103 }
2104
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002105 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002106 /* We may have to send an RST if not done yet */
2107 if (h2s->st == H2_SS_ERROR)
2108 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002109
Willy Tarreaua20a5192017-12-27 11:02:06 +01002110 if (h2c->st0 == H2_CS_FRAME_E)
2111 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002112
Willy Tarreau7e98c052017-10-10 15:56:59 +02002113 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002114 if (ret <= 0) {
2115 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002116 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002117 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002118
2119 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002120 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002121 h2c->st0 = H2_CS_FRAME_H;
2122 }
2123 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002124
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002125 if (h2c->rcvd_c > 0 &&
2126 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2127 h2c_send_conn_wu(h2c);
2128
Willy Tarreau52eed752017-09-22 15:05:09 +02002129 fail:
2130 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002131 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002132 /* we may have to signal the upper layers */
2133 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002134 if (h2s->recv_wait_list) {
2135 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
2136 tasklet_wakeup(h2s->recv_wait_list->task);
2137 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002138 }
2139 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002140 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002141}
2142
2143/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2144 * the end.
2145 */
2146static int h2_process_mux(struct h2c *h2c)
2147{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002148 struct h2s *h2s;
2149 struct wait_list *sw, *sw_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002150
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002151 /* start by sending possibly pending window updates */
2152 if (h2c->rcvd_c > 0 &&
2153 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2154 h2c_send_conn_wu(h2c) < 0)
2155 goto fail;
2156
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002157 /* First we always process the flow control list because the streams
2158 * waiting there were already elected for immediate emission but were
2159 * blocked just on this.
2160 */
2161
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002162 list_for_each_entry_safe(sw, sw_back, &h2c->fctl_list, list) {
2163 h2s = sw->handle;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002164 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2165 h2c->st0 >= H2_CS_ERROR)
2166 break;
2167
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002168 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2169 if ((long)(h2s) & 3) {
2170 sw->wait_reason &= ~SUB_CAN_SEND;
2171 LIST_DEL(&sw->list);
2172 LIST_INIT(&sw->list);
2173 tasklet_wakeup(sw->task);
2174 } else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2175 h2s->flags &= ~H2_SF_BLK_ANY;
2176 LIST_DEL(&sw->list);
2177 LIST_INIT(&sw->list);
2178 sw->wait_reason &= ~SUB_CAN_SEND;
2179 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002180 }
2181 }
2182
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002183 list_for_each_entry_safe(sw, sw_back, &h2c->send_list, list) {
2184 h2s = sw->handle;
2185
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002186 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2187 break;
2188
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002189 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2190 if ((long)(h2s) & 3) {
2191 sw->wait_reason &= ~SUB_CAN_SEND;
2192 LIST_DEL(&sw->list);
2193 LIST_INIT(&sw->list);
2194 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002195 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002196 else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2197 h2s->flags &= ~H2_SF_BLK_ANY;
2198
2199 LIST_DEL(&sw->list);
2200 LIST_INIT(&sw->list);
2201 sw->wait_reason &= ~SUB_CAN_SEND;
2202 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002203 }
2204 }
2205
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002206 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002207 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002208 if (h2c->st0 == H2_CS_ERROR) {
2209 if (h2c->max_id >= 0) {
2210 h2c_send_goaway_error(h2c, NULL);
2211 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2212 return 0;
2213 }
2214
2215 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2216 }
2217 return 1;
2218 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002219 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002220}
2221
Willy Tarreau62f52692017-10-08 23:01:42 +02002222
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002223/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002224static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002225{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002226 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002227 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002228 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002229 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002230
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002231 if (h2c->wait_list.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002232 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002233
Willy Tarreau315d8072017-12-10 22:17:57 +01002234 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002235 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002236
Willy Tarreau44e973f2018-03-01 17:49:30 +01002237 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002238 if (!buf) {
2239 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002240 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002241 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002242
Olivier Houchard7505f942018-08-21 18:10:44 +02002243 do {
2244 max = buf->size - b_data(buf);
2245 if (max)
2246 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2247 else
2248 ret = 0;
2249 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002250
Olivier Houchard7505f942018-08-21 18:10:44 +02002251 if (h2_recv_allowed(h2c)) {
2252 conn_xprt_want_recv(conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002253 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_list);
Olivier Houchard7505f942018-08-21 18:10:44 +02002254 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002255 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002256 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002257 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002258 }
2259
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002260 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002261 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002262 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002263}
2264
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002265/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002266static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002267{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002268 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002269 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002270 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002271
2272 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002273 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002274
Olivier Houchard7505f942018-08-21 18:10:44 +02002275
Willy Tarreaua2af5122017-10-09 11:56:46 +02002276 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2277 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002278 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002279 }
2280
Willy Tarreaubc933932017-10-09 16:21:43 +02002281 /* This loop is quite simple : it tries to fill as much as it can from
2282 * pending streams into the existing buffer until it's reportedly full
2283 * or the end of send requests is reached. Then it tries to send this
2284 * buffer's contents out, marks it not full if at least one byte could
2285 * be sent, and tries again.
2286 *
2287 * The snd_buf() function normally takes a "flags" argument which may
2288 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2289 * data immediately comes and CO_SFL_STREAMER to indicate that the
2290 * connection is streaming lots of data (used to increase TLS record
2291 * size at the expense of latency). The former can be sent any time
2292 * there's a buffer full flag, as it indicates at least one stream
2293 * attempted to send and failed so there are pending data. An
2294 * alternative would be to set it as long as there's an active stream
2295 * but that would be problematic for ACKs until we have an absolute
2296 * guarantee that all waiters have at least one byte to send. The
2297 * latter should possibly not be set for now.
2298 */
2299
2300 done = 0;
2301 while (!done) {
2302 unsigned int flags = 0;
2303
2304 /* fill as much as we can into the current buffer */
2305 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2306 done = h2_process_mux(h2c);
2307
2308 if (conn->flags & CO_FL_ERROR)
2309 break;
2310
2311 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2312 flags |= CO_SFL_MSG_MORE;
2313
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002314 if (b_data(&h2c->mbuf)) {
2315 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002316 if (!ret)
2317 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002318 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002319 b_del(&h2c->mbuf, ret);
2320 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002321 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002322
2323 /* wrote at least one byte, the buffer is not full anymore */
2324 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2325 }
2326
Willy Tarreaua2af5122017-10-09 11:56:46 +02002327 if (conn->flags & CO_FL_SOCK_WR_SH) {
2328 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002329 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002330 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002331 /* We're not full anymore, so we can wake any task that are waiting
2332 * for us.
2333 */
2334 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002335 while (!LIST_ISEMPTY(&h2c->send_list)) {
2336 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
Olivier Houchard6ff20392018-07-17 18:46:31 +02002337 struct wait_list *, list);
2338 LIST_DEL(&sw->list);
2339 LIST_INIT(&sw->list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002340 sw->wait_reason &= ~SUB_CAN_SEND;
2341 tasklet_wakeup(sw->task);
2342 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002343 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002344 /* We're done, no more to send */
2345 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002346 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002347schedule:
2348 if (LIST_ISEMPTY(&h2c->wait_list.list))
2349 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002350 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002351}
2352
2353static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2354{
2355 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002356 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002357
2358 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002359 ret = h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002360 if (!(h2c->wait_list.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002361 ret |= h2_recv(h2c);
2362 if (ret)
2363 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002364 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002365}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002366
Willy Tarreau62f52692017-10-08 23:01:42 +02002367/* callback called on any event by the connection handler.
2368 * It applies changes and returns zero, or < 0 if it wants immediate
2369 * destruction of the connection (which normally doesn not happen in h2).
2370 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002371static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002372{
Olivier Houchard7505f942018-08-21 18:10:44 +02002373 struct connection *conn = h2c->conn;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002374 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002375
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002376 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002377 h2_process_demux(h2c);
2378
2379 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002380 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002381
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002382 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002383 h2c->flags &= ~H2_CF_DEM_DFULL;
2384 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002385 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002386
Willy Tarreau8ec14062017-12-30 18:08:13 +01002387 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2388 /* frontend is stopping, reload likely in progress, let's try
2389 * to announce a graceful shutdown if not yet done. We don't
2390 * care if it fails, it will be tried again later.
2391 */
2392 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2393 if (h2c->last_sid < 0)
2394 h2c->last_sid = (1U << 31) - 1;
2395 h2c_send_goaway_error(h2c, NULL);
2396 }
2397 }
2398
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002399 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002400 * If we received early data, and the handshake is done, wake
2401 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002402 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002403 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2404 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2405 struct eb32_node *node;
2406 struct h2s *h2s;
2407
2408 h2c->flags |= H2_CF_WAIT_FOR_HS;
2409 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2410
2411 while (node) {
2412 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002413 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
2414 h2s->recv_wait_list) {
2415 struct wait_list *sw = h2s->recv_wait_list;
2416 sw->wait_reason &= ~SUB_CAN_RECV;
2417 tasklet_wakeup(sw->task);
2418 h2s->recv_wait_list = NULL;
2419 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002420 node = eb32_next(node);
2421 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002422 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002423
Willy Tarreau26bd7612017-10-09 16:47:04 +02002424 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002425 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2426 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2427 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002428 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002429
2430 if (eb_is_empty(&h2c->streams_by_id)) {
2431 /* no more stream, kill the connection now */
2432 h2_release(conn);
2433 return -1;
2434 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002435 }
2436
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002437 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002438 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002439
2440 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002441 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002443 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002444 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002445
2446 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002447 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002448 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002449 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002450 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002451 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2452 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002453 __conn_xprt_want_send(conn);
2454 }
2455 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002456 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002457 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002458 }
2459
Willy Tarreau3f133572017-10-31 19:21:06 +01002460 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002461 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002462 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002463 task_queue(h2c->task);
2464 }
2465 else
2466 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002467 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002468
Olivier Houchard7505f942018-08-21 18:10:44 +02002469 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002470 return 0;
2471}
2472
Willy Tarreauea392822017-10-31 10:02:25 +01002473/* Connection timeout management. The principle is that if there's no receipt
2474 * nor sending for a certain amount of time, the connection is closed. If the
2475 * MUX buffer still has lying data or is not allocatable, the connection is
2476 * immediately killed. If it's allocatable and empty, we attempt to send a
2477 * GOAWAY frame.
2478 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002479static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002480{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002481 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002482 int expired = tick_is_expired(t->expire, now_ms);
2483
Willy Tarreau0975f112018-03-29 15:22:59 +02002484 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002485 return t;
2486
Willy Tarreau0975f112018-03-29 15:22:59 +02002487 task_delete(t);
2488 task_free(t);
2489
2490 if (!h2c) {
2491 /* resources were already deleted */
2492 return NULL;
2493 }
2494
2495 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002496 h2c_error(h2c, H2_ERR_NO_ERROR);
2497 h2_wake_some_streams(h2c, 0, 0);
2498
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002499 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002500 /* don't even try to send a GOAWAY, the buffer is stuck */
2501 h2c->flags |= H2_CF_GOAWAY_FAILED;
2502 }
2503
2504 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002505 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002506 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2507 h2c->flags |= H2_CF_GOAWAY_FAILED;
2508
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002509 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2510 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002511 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002512 b_del(&h2c->mbuf, ret);
2513 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002514 }
2515 }
Willy Tarreauea392822017-10-31 10:02:25 +01002516
Willy Tarreau0975f112018-03-29 15:22:59 +02002517 /* either we can release everything now or it will be done later once
2518 * the last stream closes.
2519 */
2520 if (eb_is_empty(&h2c->streams_by_id))
2521 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002522
Willy Tarreauea392822017-10-31 10:02:25 +01002523 return NULL;
2524}
2525
2526
Willy Tarreau62f52692017-10-08 23:01:42 +02002527/*******************************************/
2528/* functions below are used by the streams */
2529/*******************************************/
2530
2531/*
2532 * Attach a new stream to a connection
2533 * (Used for outgoing connections)
2534 */
2535static struct conn_stream *h2_attach(struct connection *conn)
2536{
2537 return NULL;
2538}
2539
2540/* callback used to update the mux's polling flags after changing a cs' status.
2541 * The caller (cs_update_mux_polling) will take care of propagating any changes
2542 * to the transport layer.
2543 */
2544static void h2_update_poll(struct conn_stream *cs)
2545{
Willy Tarreau1d393222017-10-17 10:26:19 +02002546 struct h2s *h2s = cs->ctx;
2547
2548 if (!h2s)
2549 return;
2550
Willy Tarreaud7739c82017-10-30 15:38:23 +01002551 /* we may unblock a blocked read */
2552
Willy Tarreau315d8072017-12-10 22:17:57 +01002553 if (cs->flags & CS_FL_DATA_RD_ENA) {
2554 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002555 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002556 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002557 conn_xprt_want_recv(cs->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002558 if (h2_recv(h2s->h2c))
2559 h2_process(h2s->h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002560 conn_xprt_want_send(cs->conn);
2561 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002562 }
2563
Willy Tarreau1d393222017-10-17 10:26:19 +02002564 /* Note: the stream and stream-int code doesn't allow us to perform a
2565 * synchronous send() here unfortunately, because this code is called
2566 * as si_update() from the process_stream() context. This means that
2567 * we have to queue the current cs and defer its processing after the
2568 * connection's cs list is processed anyway.
2569 */
2570
2571 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002572 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2573 conn_xprt_want_send(cs->conn);
2574 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002575 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002576 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002577
2578 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002579 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002580 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002581}
2582
2583/*
2584 * Detach the stream from the connection and possibly release the connection.
2585 */
2586static void h2_detach(struct conn_stream *cs)
2587{
Willy Tarreau60935142017-10-16 18:11:19 +02002588 struct h2s *h2s = cs->ctx;
2589 struct h2c *h2c;
2590
2591 cs->ctx = NULL;
2592 if (!h2s)
2593 return;
2594
2595 h2c = h2s->h2c;
2596 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002597 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002598 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2599 !h2_has_too_many_cs(h2c)) {
2600 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2601 if (h2_recv_allowed(h2c)) {
2602 __conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002603 h2_recv(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002604 conn_xprt_want_send(h2c->conn);
2605 }
2606 }
Willy Tarreau60935142017-10-16 18:11:19 +02002607
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002608 /* this stream may be blocked waiting for some data to leave (possibly
2609 * an ES or RST frame), so orphan it in this case.
2610 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002611 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002612 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002613 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002614 return;
2615
Willy Tarreau45f752e2017-10-30 15:44:59 +01002616 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2617 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2618 /* unblock the connection if it was blocked on this
2619 * stream.
2620 */
2621 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2622 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2623 conn_xprt_want_recv(cs->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002624 h2_recv(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002625 conn_xprt_want_send(cs->conn);
2626 }
2627
Willy Tarreau71049cc2018-03-28 13:56:39 +02002628 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002629
Willy Tarreaue323f342018-03-28 13:51:45 +02002630 /* We don't want to close right now unless we're removing the
2631 * last stream, and either the connection is in error, or it
2632 * reached the ID already specified in a GOAWAY frame received
2633 * or sent (as seen by last_sid >= 0).
2634 */
2635 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2636 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002637 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002638 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002639 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002640 (conn_xprt_read0_pending(h2c->conn) ||
2641 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2642 /* no more stream will come, kill it now */
2643 h2_release(h2c->conn);
2644 }
2645 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002646 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002647 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2648 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002649 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002650 else
2651 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002652 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002653}
2654
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002655static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002656{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002657 struct h2c *h2c = h2s->h2c;
2658 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002659
Willy Tarreau721c9742017-11-07 11:05:42 +01002660 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002661 return;
2662
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002663 /* if no outgoing data was seen on this stream, it means it was
2664 * closed with a "tcp-request content" rule that is normally
2665 * used to kill the connection ASAP (eg: limit abuse). In this
2666 * case we send a goaway to close the connection.
2667 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002668 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002669 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002670 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002671
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002672 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2673 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002674 h2c_send_goaway_error(h2c, h2s) <= 0)
2675 return;
2676 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2677 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002678
Willy Tarreau00dd0782018-03-01 16:31:34 +01002679 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002680
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002681 return;
2682add_to_list:
2683 if (LIST_ISEMPTY(&sw->list)) {
2684 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002685 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002686 LIST_ADDQ(&h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002687 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002688 LIST_ADDQ(&h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002689 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002690 /* Let the handler know we want shutr */
2691 sw->handle = (void *)((long)sw->handle | 1);
2692
Willy Tarreau62f52692017-10-08 23:01:42 +02002693}
2694
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002695static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002696{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002697 struct h2c *h2c = h2s->h2c;
2698 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002699
Willy Tarreau721c9742017-11-07 11:05:42 +01002700 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002701 return;
2702
Willy Tarreau67434202017-11-06 20:20:51 +01002703 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002704 /* we can cleanly close using an empty data frame only after headers */
2705
2706 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2707 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002708 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002709
2710 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002711 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002712 else
2713 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002714 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002715 /* if no outgoing data was seen on this stream, it means it was
2716 * closed with a "tcp-request content" rule that is normally
2717 * used to kill the connection ASAP (eg: limit abuse). In this
2718 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002719 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002720 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002721 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002722 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002723
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002724 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2725 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002726 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002727 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002728
Willy Tarreau00dd0782018-03-01 16:31:34 +01002729 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002730 }
2731
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002732 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2733 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002734
2735 add_to_list:
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002736 sw = &h2s->wait_list;
2737
2738 if (LIST_ISEMPTY(&sw->list)) {
2739 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002740 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002741 LIST_ADDQ(&h2s->h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002742 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002743 LIST_ADDQ(&h2s->h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002744 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002745 /* let the handler know we want to shutr */
2746 sw->handle = (void *)((long)(sw->handle) | 2);
2747}
2748
2749static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2750{
2751 struct h2s *h2s = ctx;
2752 long reason = (long)h2s->wait_list.handle;
2753
2754 if (reason & 1)
2755 h2_do_shutr(h2s);
2756 if (reason & 2)
2757 h2_do_shutw(h2s);
2758
2759 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002760}
2761
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002762static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2763{
2764 struct h2s *h2s = cs->ctx;
2765
2766 if (!mode)
2767 return;
2768
2769 h2_do_shutr(h2s);
2770}
2771
2772static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2773{
2774 struct h2s *h2s = cs->ctx;
2775
2776 h2_do_shutw(h2s);
2777}
2778
Willy Tarreau13278b42017-10-13 19:23:14 +02002779/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2780 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2781 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002782 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002783 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002784static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002785{
2786 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002787 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002788 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002789 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002790 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002791 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002792 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002793 int flen = h2c->dfl;
2794 int outlen = 0;
2795 int wrap;
2796 int try;
2797
2798 if (!h2c->dfl) {
2799 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002800 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002801 return 0;
2802 }
2803
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002804 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002805 return 0; // incomplete input frame
2806
Willy Tarreau13278b42017-10-13 19:23:14 +02002807 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002809 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002810 copy = alloc_trash_chunk();
2811 if (!copy) {
2812 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2813 goto fail;
2814 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002815 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2816 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2817 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002818 }
2819
2820 /* The padlen is the first byte before data, and the padding appears
2821 * after data. padlen+data+padding are included in flen.
2822 */
2823 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002824 h2c->dpl = *hdrs;
2825 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002826 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2827 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002828 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002829 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002830 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002831 hdrs += 1; // skip Pad Length
2832 }
2833
2834 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2835 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002836 if (read_n32(hdrs) == h2s->id) {
2837 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2838 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002839 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002840 }
2841
Willy Tarreau13278b42017-10-13 19:23:14 +02002842 hdrs += 5; // stream dep = 4, weight = 1
2843 flen -= 5;
2844 }
2845
2846 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2847 * don't support this for now and can't even decompress so we have to
2848 * break the connection.
2849 */
2850 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2851 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002852 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002853 }
2854
Olivier Houchard638b7992018-08-16 15:41:52 +02002855 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002856 if (!csbuf) {
2857 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002858 goto fail;
2859 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002860
Willy Tarreau937f7602018-02-26 15:22:17 +01002861 /* we can't retry a failed decompression operation so we must be very
2862 * careful not to take any risks. In practice the output buffer is
2863 * always empty except maybe for trailers, in which case we simply have
2864 * to wait for the upper layer to finish consuming what is available.
2865 */
2866 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002867 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002868
Willy Tarreau937f7602018-02-26 15:22:17 +01002869 csbuf->head = 0;
2870 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002871
2872 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2873 sizeof(list)/sizeof(list[0]), tmp);
2874 if (outlen < 0) {
2875 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2876 goto fail;
2877 }
2878
2879 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002880 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002881 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002882
2883 if (outlen < 0) {
2884 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2885 goto fail;
2886 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002887
Willy Tarreau174b06a2018-04-25 18:13:58 +02002888 if (msgf & H2_MSGF_BODY) {
2889 /* a payload is present */
2890 if (msgf & H2_MSGF_BODY_CL)
2891 h2s->flags |= H2_SF_DATA_CLEN;
2892 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2893 h2s->flags |= H2_SF_DATA_CHNK;
2894 }
2895
Willy Tarreau13278b42017-10-13 19:23:14 +02002896 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002897 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002898 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002899 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002900
Willy Tarreau39d68502018-03-02 12:26:37 +01002901 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002902 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002903 h2s->cs->flags |= CS_FL_REOS;
2904 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002905
Willy Tarreau68dd9852017-07-03 14:44:26 +02002906 leave:
2907 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002908 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002909 fail:
2910 outlen = 0;
2911 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002912}
2913
Willy Tarreau454f9052017-10-26 19:40:35 +02002914/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2915 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2916 * in use, a new chunk is emitted for each frame. This is supposed to fit
2917 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2918 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2919 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2920 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002921 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2922 * checked to know if some data remain pending (an empty DATA frame can return
2923 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2924 * connection errors in h2c->errcode. The caller must already have checked the
2925 * frame header and ensured that the frame was complete or the buffer full. It
2926 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002927 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002928static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002929{
2930 struct h2c *h2c = h2s->h2c;
2931 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002932 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002933 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002934 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002935
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002936 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002937
2938 /* The padlen is the first byte before data, and the padding appears
2939 * after data. padlen+data+padding are included in flen.
2940 */
Willy Tarreau79127812017-12-03 21:06:59 +01002941 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002942 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002943 return 0;
2944
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002945 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002946 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002947 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2948 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002949 return 0;
2950 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002951
2952 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002953 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002954 h2c->dfl--;
2955 h2c->rcvd_c++; h2c->rcvd_s++;
2956 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002957 }
2958
Olivier Houchard638b7992018-08-16 15:41:52 +02002959 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002960 if (!csbuf) {
2961 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002962 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002963 }
2964
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002965 flen = h2c->dfl - h2c->dpl;
2966 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002967 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002968
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 if (flen > b_data(&h2c->dbuf)) {
2970 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002971 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002972 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002973 }
2974
2975 if (unlikely(b_space_wraps(csbuf))) {
2976 /* it doesn't fit and the buffer is fragmented,
2977 * so let's defragment it and try again.
2978 */
2979 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002980 }
2981
Willy Tarreaueba10f22018-04-25 20:44:22 +02002982 /* chunked-encoding requires more room */
2983 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002984 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002985 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2986 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2987 (chklen < 1048576) ? 4 : 8;
2988 chklen += 4; // CRLF, CRLF
2989 }
2990
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002991 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002992 if (flen + chklen > b_room(csbuf)) {
2993 if (chklen >= b_room(csbuf)) {
2994 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002995 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002996 }
2997 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002998 }
2999
3000 if (h2s->flags & H2_SF_DATA_CHNK) {
3001 /* emit the chunk size */
3002 unsigned int chksz = flen;
3003 char str[10];
3004 char *beg;
3005
3006 beg = str + sizeof(str);
3007 *--beg = '\n';
3008 *--beg = '\r';
3009 do {
3010 *--beg = hextab[chksz & 0xF];
3011 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003012 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003013 }
3014
Willy Tarreau454f9052017-10-26 19:40:35 +02003015 /* Block1 is the length of the first block before the buffer wraps,
3016 * block2 is the optional second block to reach the end of the frame.
3017 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003018 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003019 if (block1 > flen)
3020 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003021 block2 = flen - block1;
3022
3023 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003024 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003025
3026 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003027 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003028
Willy Tarreaueba10f22018-04-25 20:44:22 +02003029 if (h2s->flags & H2_SF_DATA_CHNK) {
3030 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003031 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003032 }
3033
Willy Tarreau454f9052017-10-26 19:40:35 +02003034 /* now mark the input data as consumed (will be deleted from the buffer
3035 * by the caller when seeing FRAME_A after sending the window update).
3036 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003037 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003038 h2c->dfl -= flen;
3039 h2c->rcvd_c += flen;
3040 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3041
3042 if (h2c->dfl > h2c->dpl) {
3043 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003044 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003045 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003046 }
3047
Willy Tarreau4a28da12018-01-04 14:41:00 +01003048 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003049 /* here we're done with the frame, all the payload (except padding) was
3050 * transferred.
3051 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003052
3053 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3054 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003055 if (b_room(csbuf) < 5) {
3056 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003057 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003058 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003059 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003060 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003061 }
3062
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003063 h2c->rcvd_c += h2c->dpl;
3064 h2c->rcvd_s += h2c->dpl;
3065 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003066 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3067
Willy Tarreau39d68502018-03-02 12:26:37 +01003068 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003069 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003070 h2s->cs->flags |= CS_FL_REOS;
3071 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003072
Willy Tarreau454b57b2018-02-26 15:50:05 +01003073 return flen + chklen;
3074 fail:
3075 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003076}
3077
Willy Tarreau5dd17352018-06-14 13:33:30 +02003078/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3079 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3080 * number of bytes sent. The caller must check the stream's status to detect
3081 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003082 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003083static 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 +02003084{
3085 struct http_hdr list[MAX_HTTP_HDR];
3086 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003087 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003088 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003089 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003090 int es_now = 0;
3091 int ret = 0;
3092 int hdr;
3093
3094 if (h2c_mux_busy(h2c, h2s)) {
3095 h2s->flags |= H2_SF_BLK_MBUSY;
3096 return 0;
3097 }
3098
Willy Tarreau44e973f2018-03-01 17:49:30 +01003099 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100 h2c->flags |= H2_CF_MUX_MALLOC;
3101 h2s->flags |= H2_SF_BLK_MROOM;
3102 return 0;
3103 }
3104
3105 /* First, try to parse the H1 response and index it into <list>.
3106 * NOTE! Since it comes from haproxy, we *know* that a response header
3107 * block does not wrap and we can safely read it this way without
3108 * having to realign the buffer.
3109 */
Willy Tarreau4433c082018-09-11 15:33:32 +02003110 h1m->state = H1_MSG_RPBEFORE;
3111 h1m->next = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003112 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003113 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003114 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003115 /* incomplete or invalid response, this is abnormal coming from
3116 * haproxy and may only result in a bad errorfile or bad Lua code
3117 * so that won't be fixed, raise an error now.
3118 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003119 * FIXME: we should instead add the ability to only return a
3120 * 502 bad gateway. But in theory this is not supposed to
3121 * happen.
3122 */
3123 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3124 ret = 0;
3125 goto end;
3126 }
3127
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003128 h2s->status = sl.st.status;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003129 chunk_reset(&outbuf);
3130
3131 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003132 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003133 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003134 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003135
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003136 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003137 break;
3138 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003139 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003140 }
3141
3142 if (outbuf.size < 9) {
3143 h2c->flags |= H2_CF_MUX_MFULL;
3144 h2s->flags |= H2_SF_BLK_MROOM;
3145 ret = 0;
3146 goto end;
3147 }
3148
3149 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003150 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3151 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3152 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003153
3154 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003155 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003156 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003157 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003158 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003159 else if (unlikely(list[0].v.len != 3)) {
3160 /* this is an unparsable response */
3161 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3162 ret = 0;
3163 goto end;
3164 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003166 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003167 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3168 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3169 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3170 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3171 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003172 }
3173 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003174 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003175 goto realign_again;
3176
3177 h2c->flags |= H2_CF_MUX_MFULL;
3178 h2s->flags |= H2_SF_BLK_MROOM;
3179 ret = 0;
3180 goto end;
3181 }
3182
3183 /* encode all headers, stop at empty name */
3184 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003185 /* these ones do not exist in H2 and must be dropped. */
3186 if (isteq(list[hdr].n, ist("connection")) ||
3187 isteq(list[hdr].n, ist("proxy-connection")) ||
3188 isteq(list[hdr].n, ist("keep-alive")) ||
3189 isteq(list[hdr].n, ist("upgrade")) ||
3190 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003191 continue;
3192
3193 if (isteq(list[hdr].n, ist("")))
3194 break; // end
3195
3196 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3197 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003198 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003199 goto realign_again;
3200
3201 h2c->flags |= H2_CF_MUX_MFULL;
3202 h2s->flags |= H2_SF_BLK_MROOM;
3203 ret = 0;
3204 goto end;
3205 }
3206 }
3207
3208 /* we may need to add END_STREAM */
3209 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3210 es_now = 1;
3211
3212 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003213 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003214
3215 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003216 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003217
3218 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003219 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003220
3221 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003222 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003223 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003224
3225 /* for now we don't implemented CONTINUATION, so we wait for a
3226 * body or directly end in TRL2.
3227 */
3228 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003229 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003230 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003231
Willy Tarreau801250e2018-09-11 11:45:04 +02003232 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003233 h2s->flags |= H2_SF_ES_SENT;
3234 if (h2s->st == H2_SS_OPEN)
3235 h2s->st = H2_SS_HLOC;
3236 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003237 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003238 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003239 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003240 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003241 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003242 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003243 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003244 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003245 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003246
3247 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003248
3249 end:
3250 //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, h1_msg_state_str(h1m->err_state));
3251 return ret;
3252}
3253
Willy Tarreau5dd17352018-06-14 13:33:30 +02003254/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3255 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3256 * the number of bytes sent. The caller must check the stream's status to
3257 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003258 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003259static 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 +02003260{
3261 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003262 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003263 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003264 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003265 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003266 int es_now = 0;
3267 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003268 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003269 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003270
3271 if (h2c_mux_busy(h2c, h2s)) {
3272 h2s->flags |= H2_SF_BLK_MBUSY;
3273 goto end;
3274 }
3275
Willy Tarreau44e973f2018-03-01 17:49:30 +01003276 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003277 h2c->flags |= H2_CF_MUX_MALLOC;
3278 h2s->flags |= H2_SF_BLK_MROOM;
3279 goto end;
3280 }
3281
3282 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003283 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003284 goto end;
3285
3286 chunk_reset(&outbuf);
3287
3288 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003289 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003290 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003291 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003292
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003293 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003294 break;
3295 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003296 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003297 }
3298
3299 if (outbuf.size < 9) {
3300 h2c->flags |= H2_CF_MUX_MFULL;
3301 h2s->flags |= H2_SF_BLK_MROOM;
3302 goto end;
3303 }
3304
3305 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003306 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3307 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3308 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003309
3310 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3311 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003312 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003313 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003314 break;
3315 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003316 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003317 if ((long long)size > h1m->curr_len)
3318 size = h1m->curr_len;
3319 break;
3320 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003321 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003322 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003323 if (!ret)
3324 goto end;
3325
3326 if (ret < 0) {
3327 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003328 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003329 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3330 goto end;
3331 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003332 max -= ret;
3333 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003334 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003335 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003336 }
3337
Willy Tarreau801250e2018-09-11 11:45:04 +02003338 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003339 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003340 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 if (!ret)
3342 goto end;
3343
3344 if (ret < 0) {
3345 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003346 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003347 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3348 goto end;
3349 }
3350
3351 size = chunk;
3352 h1m->curr_len = chunk;
3353 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003354 max -= ret;
3355 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003356 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003357 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358 if (!size)
3359 goto send_empty;
3360 }
3361
3362 /* in MSG_DATA state, continue below */
3363 size = h1m->curr_len;
3364 break;
3365 }
3366
3367 /* we have in <size> the exact number of bytes we need to copy from
3368 * the H1 buffer. We need to check this against the connection's and
3369 * the stream's send windows, and to ensure that this fits in the max
3370 * frame size and in the buffer's available space minus 9 bytes (for
3371 * the frame header). The connection's flow control is applied last so
3372 * that we can use a separate list of streams which are immediately
3373 * unblocked on window opening. Note: we don't implement padding.
3374 */
3375
Willy Tarreau5dd17352018-06-14 13:33:30 +02003376 if (size > max)
3377 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003378
3379 if (size > h2s->mws)
3380 size = h2s->mws;
3381
3382 if (size <= 0) {
3383 h2s->flags |= H2_SF_BLK_SFCTL;
3384 goto end;
3385 }
3386
3387 if (h2c->mfs && size > h2c->mfs)
3388 size = h2c->mfs;
3389
3390 if (size + 9 > outbuf.size) {
3391 /* we have an opportunity for enlarging the too small
3392 * available space, let's try.
3393 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003394 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003395 goto realign_again;
3396 size = outbuf.size - 9;
3397 }
3398
3399 if (size <= 0) {
3400 h2c->flags |= H2_CF_MUX_MFULL;
3401 h2s->flags |= H2_SF_BLK_MROOM;
3402 goto end;
3403 }
3404
3405 if (size > h2c->mws)
3406 size = h2c->mws;
3407
3408 if (size <= 0) {
3409 h2s->flags |= H2_SF_BLK_MFCTL;
3410 goto end;
3411 }
3412
3413 /* copy whatever we can */
3414 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003415 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003416 if (ret == 1)
3417 len2 = 0;
3418
3419 if (!ret || len1 + len2 < size) {
3420 /* FIXME: must normally never happen */
3421 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3422 goto end;
3423 }
3424
3425 /* limit len1/len2 to size */
3426 if (len1 + len2 > size) {
3427 int sub = len1 + len2 - size;
3428
3429 if (len2 > sub)
3430 len2 -= sub;
3431 else {
3432 sub -= len2;
3433 len2 = 0;
3434 len1 -= sub;
3435 }
3436 }
3437
3438 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003439 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003440 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003441 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003442
3443 send_empty:
3444 /* we may need to add END_STREAM */
3445 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3446 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003447 *
3448 * FIXME: what we do here is not correct because we send end_stream
3449 * before knowing if we'll have to send a HEADERS frame for the
3450 * trailers. More importantly we're not consuming the trailing CRLF
3451 * after the end of trailers, so it will be left to the caller to
3452 * eat it. The right way to do it would be to measure trailers here
3453 * and to send ES only if there are no trailers.
3454 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003455 */
3456 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003457 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003458 es_now = 1;
3459
3460 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003462
3463 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003464 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003465
3466 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003467 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003468
3469 /* consume incoming H1 response */
3470 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003471 max -= size;
3472 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003473 total += size;
3474 h1m->curr_len -= size;
3475 h2s->mws -= size;
3476 h2c->mws -= size;
3477
3478 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003479 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003480 goto new_frame;
3481 }
3482 }
3483
3484 if (es_now) {
3485 if (h2s->st == H2_SS_OPEN)
3486 h2s->st = H2_SS_HLOC;
3487 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003488 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003489
Willy Tarreau35a62702018-02-27 15:37:25 +01003490 if (!(h1m->flags & H1_MF_CHNK)) {
3491 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003492 total += max;
3493 ofs += max;
3494 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003495
Willy Tarreau801250e2018-09-11 11:45:04 +02003496 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003497 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003498
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003499 h2s->flags |= H2_SF_ES_SENT;
3500 }
3501
3502 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003503 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, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504 return total;
3505}
3506
Olivier Houchard6ff20392018-07-17 18:46:31 +02003507/* Called from the upper layer, to subscribe to events, such as being able to send */
3508static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3509{
3510 struct wait_list *sw;
3511 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003512 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003513
3514 switch (event_type) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003515 case SUB_CAN_RECV:
3516 sw = param;
3517 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3518 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003519 sw->handle = h2s;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003520 h2s->recv_wait_list = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003521 }
3522 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003523 case SUB_CAN_SEND:
3524 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003525 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003526 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003527 sw->handle = h2s;
3528 if (h2s->flags & H2_SF_BLK_MFCTL)
3529 LIST_ADDQ(&h2c->fctl_list, &sw->list);
3530 else
3531 LIST_ADDQ(&h2c->send_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003532 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003533 return 0;
3534 default:
3535 break;
3536 }
3537 return -1;
3538
3539
3540}
3541
Olivier Houchard511efea2018-08-16 15:30:32 +02003542/* Called from the upper layer, to receive data */
3543static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3544{
Olivier Houchard638b7992018-08-16 15:41:52 +02003545 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003546 size_t ret = 0;
3547
3548 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003549 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003550
Olivier Houchard638b7992018-08-16 15:41:52 +02003551 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003552 cs->flags |= CS_FL_RCV_MORE;
3553 else {
3554 cs->flags &= ~CS_FL_RCV_MORE;
3555 if (cs->flags & CS_FL_REOS)
3556 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003557 if (b_size(&h2s->rxbuf)) {
3558 b_free(&h2s->rxbuf);
3559 offer_buffers(NULL, tasks_run_queue);
3560 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003561 }
3562
3563 return ret;
3564}
3565
Willy Tarreau62f52692017-10-08 23:01:42 +02003566/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003567static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003568{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003569 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003570 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003571 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003572
Willy Tarreau0bad0432018-06-14 16:54:01 +02003573 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003574 h2s->flags |= H2_SF_OUTGOING_DATA;
3575
Willy Tarreaua40704a2018-09-11 13:52:04 +02003576 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003577 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003578 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003579 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003580 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003581 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003582 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003583 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003584 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003585 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003586
Willy Tarreau5dd17352018-06-14 13:33:30 +02003587 if (unlikely((int)ret <= 0)) {
3588 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003589 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3590 break;
3591 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003592 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003593 total += count;
3594 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003595 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003596 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003597 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003598 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003599 cs->flags |= CS_FL_ERROR;
3600 break;
3601 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003602
3603 total += ret;
3604 count -= ret;
3605
3606 if (h2s->st >= H2_SS_ERROR)
3607 break;
3608
3609 if (h2s->flags & H2_SF_BLK_ANY)
3610 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003611 }
3612
Willy Tarreau00610962018-07-19 10:58:28 +02003613 if (h2s->st >= H2_SS_ERROR) {
3614 /* trim any possibly pending data after we close (extra CR-LF,
3615 * unprocessed trailers, abnormal extra data, ...)
3616 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003617 total += count;
3618 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003619 }
3620
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003621 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003622 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003623 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003624 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003625 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003626 }
3627
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003628 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003629 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003630 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02003631 if (!(h2s->h2c->wait_list.wait_reason & SUB_CAN_SEND))
3632 tasklet_wakeup(h2s->h2c->wait_list.task);
3633 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003634 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003635}
3636
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003637/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003638static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003639{
3640 struct h2c *h2c = conn->mux_ctx;
3641 struct h2s *h2s;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003642 struct wait_list *sw;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003643 struct eb32_node *node;
3644 int fctl_cnt = 0;
3645 int send_cnt = 0;
3646 int tree_cnt = 0;
3647 int orph_cnt = 0;
3648
3649 if (!h2c)
3650 return;
3651
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003652 list_for_each_entry(sw, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003653 fctl_cnt++;
3654
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003655 list_for_each_entry(sw, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003656 send_cnt++;
3657
3658 node = eb32_first(&h2c->streams_by_id);
3659 while (node) {
3660 h2s = container_of(node, struct h2s, by_id);
3661 tree_cnt++;
3662 if (!h2s->cs)
3663 orph_cnt++;
3664 node = eb32_next(node);
3665 }
3666
Willy Tarreau616ac812018-07-24 14:12:42 +02003667 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3668 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3669 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3670 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3671 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3672 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003673}
Willy Tarreau62f52692017-10-08 23:01:42 +02003674
3675/*******************************************************/
3676/* functions below are dedicated to the config parsers */
3677/*******************************************************/
3678
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003679/* config parser for global "tune.h2.header-table-size" */
3680static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3681 struct proxy *defpx, const char *file, int line,
3682 char **err)
3683{
3684 if (too_many_args(1, args, err, NULL))
3685 return -1;
3686
3687 h2_settings_header_table_size = atoi(args[1]);
3688 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3689 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3690 return -1;
3691 }
3692 return 0;
3693}
Willy Tarreau62f52692017-10-08 23:01:42 +02003694
Willy Tarreaue6baec02017-07-27 11:45:11 +02003695/* config parser for global "tune.h2.initial-window-size" */
3696static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3697 struct proxy *defpx, const char *file, int line,
3698 char **err)
3699{
3700 if (too_many_args(1, args, err, NULL))
3701 return -1;
3702
3703 h2_settings_initial_window_size = atoi(args[1]);
3704 if (h2_settings_initial_window_size < 0) {
3705 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3706 return -1;
3707 }
3708 return 0;
3709}
3710
Willy Tarreau5242ef82017-07-27 11:47:28 +02003711/* config parser for global "tune.h2.max-concurrent-streams" */
3712static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3713 struct proxy *defpx, const char *file, int line,
3714 char **err)
3715{
3716 if (too_many_args(1, args, err, NULL))
3717 return -1;
3718
3719 h2_settings_max_concurrent_streams = atoi(args[1]);
3720 if (h2_settings_max_concurrent_streams < 0) {
3721 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3722 return -1;
3723 }
3724 return 0;
3725}
3726
Willy Tarreau62f52692017-10-08 23:01:42 +02003727
3728/****************************************/
3729/* MUX initialization and instanciation */
3730/***************************************/
3731
3732/* The mux operations */
3733const struct mux_ops h2_ops = {
3734 .init = h2_init,
Willy Tarreau62f52692017-10-08 23:01:42 +02003735 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003736 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003737 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003738 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003739 .attach = h2_attach,
3740 .detach = h2_detach,
3741 .shutr = h2_shutr,
3742 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003743 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003744 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003745 .name = "H2",
3746};
3747
Christopher Faulet32f61c02018-04-10 14:33:41 +02003748/* PROTO selection : this mux registers PROTO token "h2" */
3749static struct mux_proto_list mux_proto_h2 =
3750 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003751
3752/* config keyword parsers */
3753static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003754 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003755 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003756 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003757 { 0, NULL, NULL }
3758}};
3759
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003760static void __h2_deinit(void)
3761{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003762 pool_destroy(pool_head_h2s);
3763 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003764}
3765
Willy Tarreau62f52692017-10-08 23:01:42 +02003766__attribute__((constructor))
3767static void __h2_init(void)
3768{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003769 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003770 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003771 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003772 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3773 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003774}