blob: 506226ce9beac7e17aea0bb6114390d831fd0c68 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
335static inline void h2c_restart_reading(const struct h2c *h2c)
336{
337 if (!h2_recv_allowed(h2c))
338 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100339 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100340 return;
341 tasklet_wakeup(h2c->wait_event.task);
342}
343
344
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100345/* returns true if the front connection has too many conn_streams attached */
346static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200347{
Willy Tarreaua8754662018-12-23 20:43:58 +0100348 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200349}
350
Willy Tarreau44e973f2018-03-01 17:49:30 +0100351/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
352 * flags are used to figure what buffer was requested. It returns 1 if the
353 * allocation succeeds, in which case the connection is woken up, or 0 if it's
354 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200355 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100356static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200357{
358 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100359 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau44e973f2018-03-01 17:49:30 +0100361 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200362 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100363 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200364 return 1;
365 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200366
Willy Tarreau44e973f2018-03-01 17:49:30 +0100367 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
368 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200369
370 if (h2c->flags & H2_CF_DEM_MROOM) {
371 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100372 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200373 }
Willy Tarreau14398122017-09-22 14:26:04 +0200374 return 1;
375 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100376
377 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
378 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200379 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100380 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100381 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100382 return 1;
383 }
384
Willy Tarreau14398122017-09-22 14:26:04 +0200385 return 0;
386}
387
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200388static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200389{
390 struct buffer *buf = NULL;
391
Willy Tarreau44e973f2018-03-01 17:49:30 +0100392 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
393 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
394 h2c->buf_wait.target = h2c;
395 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100396 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100397 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100398 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200399 __conn_xprt_stop_recv(h2c->conn);
400 }
401 return buf;
402}
403
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200404static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200405{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200406 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100407 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200408 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200409 }
410}
411
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100412/* returns the number of allocatable outgoing streams for the connection taking
413 * the last_sid and the reserved ones into account.
414 */
415static inline int h2_streams_left(const struct h2c *h2c)
416{
417 int ret;
418
419 /* consider the number of outgoing streams we're allowed to create before
420 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
421 * nb_reserved is the number of streams which don't yet have an ID.
422 */
423 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
424 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
425 if (ret < 0)
426 ret = 0;
427 return ret;
428}
429
Willy Tarreau00f18a32019-01-26 12:19:01 +0100430/* returns the number of streams in use on a connection to figure if it's
431 * idle or not. We check nb_cs and not nb_streams as the caller will want
432 * to know if it was the last one after a detach().
433 */
434static int h2_used_streams(struct connection *conn)
435{
436 struct h2c *h2c = conn->ctx;
437
438 return h2c->nb_cs;
439}
440
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100441/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100442static int h2_avail_streams(struct connection *conn)
443{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100444 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100445 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100446 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100447
Willy Tarreau6afec462019-01-28 06:40:19 +0100448 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
449 * streams on the connection.
450 */
451 if (h2c->last_sid >= 0)
452 return 0;
453
Willy Tarreau86949782019-01-31 10:42:05 +0100454 /* note: may be negative if a SETTINGS frame changes the limit */
455 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100456
457 /* we must also consider the limit imposed by stream IDs */
458 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100459 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100460 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100461 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
462 ret1 = MIN(ret1, ret2);
463 }
464 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100465}
466
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200467
Willy Tarreau62f52692017-10-08 23:01:42 +0200468/*****************************************************************/
469/* functions below are dedicated to the mux setup and management */
470/*****************************************************************/
471
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200472/* Initialize the mux once it's attached. For outgoing connections, the context
473 * is already initialized before installing the mux, so we detect incoming
474 * connections from the fact that the context is still NULL. Returns < 0 on
475 * error.
476 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100477static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200478{
479 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100480 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481
Willy Tarreaubafbe012017-11-24 17:34:44 +0100482 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200483 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200484 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200485
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100486 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200487 h2c->flags = H2_CF_IS_BACK;
488 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
489 if (tick_isset(prx->timeout.serverfin))
490 h2c->shut_timeout = prx->timeout.serverfin;
491 } else {
492 h2c->flags = H2_CF_NONE;
493 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
494 if (tick_isset(prx->timeout.clientfin))
495 h2c->shut_timeout = prx->timeout.clientfin;
496 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100497
Willy Tarreau0b37d652018-10-03 10:33:02 +0200498 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100499 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100500 if (tick_isset(h2c->timeout)) {
501 t = task_new(tid_bit);
502 if (!t)
503 goto fail;
504
505 h2c->task = t;
506 t->process = h2_timeout_task;
507 t->context = h2c;
508 t->expire = tick_add(now_ms, h2c->timeout);
509 }
Willy Tarreauea392822017-10-31 10:02:25 +0100510
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200511 h2c->wait_event.task = tasklet_new();
512 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200513 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task->process = h2_io_cb;
515 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100516 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200517
Willy Tarreau32218eb2017-09-22 08:07:25 +0200518 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
519 if (!h2c->ddht)
520 goto fail;
521
522 /* Initialise the context. */
523 h2c->st0 = H2_CS_PREFACE;
524 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100525 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200526 h2c->max_id = -1;
527 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100528 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100530 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200531 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100532 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100533 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200534
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200535 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200536 h2c->dsi = -1;
537 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100538
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->last_sid = -1;
540
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200541 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->miw = 65535; /* mux initial window size */
543 h2c->mws = 65535; /* mux window size */
544 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200545 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546 LIST_INIT(&h2c->send_list);
547 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200548 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100549 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550
Willy Tarreau3f133572017-10-31 19:21:06 +0100551 if (t)
552 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100553
Willy Tarreau01b44822018-10-03 14:26:37 +0200554 if (h2c->flags & H2_CF_IS_BACK) {
555 /* FIXME: this is temporary, for outgoing connections we need
556 * to immediately allocate a stream until the code is modified
557 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100558 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200559 */
560 struct h2s *h2s;
561
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100562 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200563 if (!h2s)
564 goto fail_stream;
565 }
566
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100567 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200568
Willy Tarreau0f383582018-10-03 14:22:21 +0200569 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100570 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200571 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200572 fail_stream:
573 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200574 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100575 if (t)
576 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200577 if (h2c->wait_event.task)
578 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100579 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200580 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200581 return -1;
582}
583
Willy Tarreau751f2d02018-10-05 09:35:00 +0200584/* returns the next allocatable outgoing stream ID for the H2 connection, or
585 * -1 if no more is allocatable.
586 */
587static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
588{
589 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100590
591 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200592 id = -1;
593 return id;
594}
595
Willy Tarreau2373acc2017-10-12 17:35:14 +0200596/* returns the stream associated with id <id> or NULL if not found */
597static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
598{
599 struct eb32_node *node;
600
Willy Tarreau751f2d02018-10-05 09:35:00 +0200601 if (id == 0)
602 return (struct h2s *)h2_closed_stream;
603
Willy Tarreau2a856182017-05-16 15:20:39 +0200604 if (id > h2c->max_id)
605 return (struct h2s *)h2_idle_stream;
606
Willy Tarreau2373acc2017-10-12 17:35:14 +0200607 node = eb32_lookup(&h2c->streams_by_id, id);
608 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200609 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610
611 return container_of(node, struct h2s, by_id);
612}
613
Willy Tarreau62f52692017-10-08 23:01:42 +0200614/* release function for a connection. This one should be called to free all
615 * resources allocated to the mux.
616 */
617static void h2_release(struct connection *conn)
618{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100619 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200620
Willy Tarreau32218eb2017-09-22 08:07:25 +0200621 if (h2c) {
622 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200623
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100624 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100625 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100626 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200627
Willy Tarreau44e973f2018-03-01 17:49:30 +0100628 h2_release_buf(h2c, &h2c->dbuf);
629 h2_release_buf(h2c, &h2c->mbuf);
630
Willy Tarreauea392822017-10-31 10:02:25 +0100631 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200632 h2c->task->context = NULL;
633 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100634 h2c->task = NULL;
635 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200636 if (h2c->wait_event.task)
637 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100638 if (h2c->wait_event.events != 0)
639 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200640 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100641
Willy Tarreaubafbe012017-11-24 17:34:44 +0100642 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200643 }
644
645 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100646 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200647
648 conn_stop_tracking(conn);
649 conn_full_close(conn);
650 if (conn->destroy_cb)
651 conn->destroy_cb(conn);
652 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200653}
654
655
Willy Tarreau71681172017-10-23 14:39:06 +0200656/******************************************************/
657/* functions below are for the H2 protocol processing */
658/******************************************************/
659
660/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100661static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200662{
663 return h2s ? h2s->id : 0;
664}
665
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200666/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100667static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200668{
669 if (h2c->msi < 0)
670 return 0;
671
672 if (h2c->msi == h2s_id(h2s))
673 return 0;
674
675 return 1;
676}
677
Willy Tarreau741d6df2017-10-17 08:00:59 +0200678/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100679static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200680{
681 h2c->errcode = err;
682 h2c->st0 = H2_CS_ERROR;
683}
684
Willy Tarreau175cebb2019-01-24 10:02:24 +0100685/* marks an error on the stream. It may also update an already closed stream
686 * (e.g. to report an error after an RST was received).
687 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200689{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100690 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200691 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100692 if (h2s->st < H2_SS_ERROR)
693 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100694 if (h2s->cs)
695 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200696 }
697}
698
Willy Tarreau7e094452018-12-19 18:08:52 +0100699/* attempt to notify the data layer of recv availability */
700static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
701{
702 struct wait_event *sw;
703
704 if (h2s->recv_wait) {
705 sw = h2s->recv_wait;
706 sw->events &= ~SUB_RETRY_RECV;
707 tasklet_wakeup(sw->task);
708 h2s->recv_wait = NULL;
709 }
710}
711
712/* attempt to notify the data layer of send availability */
713static void __maybe_unused h2s_notify_send(struct h2s *h2s)
714{
715 struct wait_event *sw;
716
717 if (h2s->send_wait) {
718 sw = h2s->send_wait;
719 sw->events &= ~SUB_RETRY_SEND;
720 tasklet_wakeup(sw->task);
721 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100722 LIST_DEL(&h2s->list);
723 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100724 }
725}
726
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100727/* alerts the data layer, trying to wake it up by all means, following
728 * this sequence :
729 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
730 * - if its subscribed to send, then it's woken up for send
731 * - if it was subscribed to neither, its ->wake() callback is called
732 * It is safe to call this function with a closed stream which doesn't have a
733 * conn_stream anymore.
734 */
735static void __maybe_unused h2s_alert(struct h2s *h2s)
736{
737 if (h2s->recv_wait || h2s->send_wait) {
738 h2s_notify_recv(h2s);
739 h2s_notify_send(h2s);
740 }
741 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
742 h2s->cs->data_cb->wake(h2s->cs);
743}
744
Willy Tarreaue4820742017-07-27 13:37:23 +0200745/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100746static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200747{
748 uint8_t *out = frame;
749
750 *out = len >> 16;
751 write_n16(out + 1, len);
752}
753
Willy Tarreau54c15062017-10-10 17:10:03 +0200754/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
755 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
756 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200757 * available in the buffer's input prior to calling this function. The buffer
758 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200759 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100760static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200761 const struct buffer *b, int o)
762{
Willy Tarreau591d4452018-06-15 17:21:00 +0200763 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 +0200764}
765
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200767{
Willy Tarreau591d4452018-06-15 17:21:00 +0200768 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200769}
770
Willy Tarreau1f094672017-11-20 21:27:45 +0100771static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200772{
Willy Tarreau591d4452018-06-15 17:21:00 +0200773 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200774}
775
Willy Tarreau1f094672017-11-20 21:27:45 +0100776static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200777{
Willy Tarreau591d4452018-06-15 17:21:00 +0200778 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200779}
780
781
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100782/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
783 * The algorithm is not obvious. It turns out that H2 headers are neither
784 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
785 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200786 *
787 * b0 b1 b2 b3 b4 b5..b8
788 * +----------+---------+--------+----+----+----------------------+
789 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
790 * +----------+---------+--------+----+----+----------------------+
791 *
792 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
793 * we get the sid properly aligned and ordered, and 16 bits of len properly
794 * ordered as well. The type and flags can be extracted using bit shifts from
795 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200796 * Returns zero if some bytes are missing, otherwise non-zero on success. The
797 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200798 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100799static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200800{
801 uint64_t w;
802
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100803 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 return 0;
805
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100806 w = h2_get_n64(b, o + 1);
807 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200808 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
809 h->ff = w >> 32;
810 h->ft = w >> 40;
811 h->len += w >> 48;
812 return 1;
813}
814
815/* skip the next 9 bytes corresponding to the frame header possibly parsed by
816 * h2_peek_frame_hdr() above.
817 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100818static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200819{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200820 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200821}
822
823/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
826 int ret;
827
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100828 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200829 if (ret > 0)
830 h2_skip_frame_hdr(b);
831 return ret;
832}
833
Willy Tarreau00dd0782018-03-01 16:31:34 +0100834/* marks stream <h2s> as CLOSED and decrement the number of active streams for
835 * its connection if the stream was not yet closed. Please use this exclusively
836 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100837 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100838static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100839{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100840 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100841 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100842 if (!h2s->id)
843 h2s->h2c->nb_reserved--;
Christopher Faulet63768a62019-03-22 14:05:52 +0100844 if (h2s->cs)
845 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100846 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847 h2s->st = H2_SS_CLOSED;
848}
849
Willy Tarreau71049cc2018-03-28 13:56:39 +0200850/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
851static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100852{
853 h2s_close(h2s);
854 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200855 if (b_size(&h2s->rxbuf)) {
856 b_free(&h2s->rxbuf);
857 offer_buffers(NULL, tasks_run_queue);
858 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200859 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100860 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200861 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100862 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800863 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200864 * reference left would be in the h2c send_list/fctl_list, and if
865 * we're in it, we're getting out anyway
866 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100867 LIST_DEL_INIT(&h2s->list);
868 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200869 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100870 pool_free(pool_head_h2s, h2s);
871}
872
Willy Tarreaua8e49542018-10-03 18:53:55 +0200873/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
874 * stream tree. In case of error, nothing is added and NULL is returned. The
875 * causes of errors can be any failed memory allocation. The caller is
876 * responsible for checking if the connection may support an extra stream
877 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200878 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200879static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200880{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200881 struct h2s *h2s;
882
Willy Tarreaubafbe012017-11-24 17:34:44 +0100883 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200884 if (!h2s)
885 goto out;
886
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200887 h2s->wait_event.task = tasklet_new();
888 if (!h2s->wait_event.task) {
889 pool_free(pool_head_h2s, h2s);
890 goto out;
891 }
892 h2s->send_wait = NULL;
893 h2s->recv_wait = NULL;
894 h2s->wait_event.task->process = h2_deferred_shut;
895 h2s->wait_event.task->context = h2s;
896 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100897 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200898 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100899 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200900 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200901 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200902 h2s->mws = h2c->miw;
903 h2s->flags = H2_SF_NONE;
904 h2s->errcode = H2_ERR_NO_ERROR;
905 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200906 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100907 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200908 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200909
910 if (h2c->flags & H2_CF_IS_BACK) {
911 h1m_init_req(&h2s->h1m);
912 h2s->h1m.err_pos = -1; // don't care about errors on the request path
913 h2s->h1m.flags |= H1_MF_TOLOWER;
914 } else {
915 h1m_init_res(&h2s->h1m);
916 h2s->h1m.err_pos = -1; // don't care about errors on the response path
917 h2s->h1m.flags |= H1_MF_TOLOWER;
918 }
919
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200920 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200921 if (id > 0)
922 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100923 else
924 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200925
926 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100927 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100928 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200929
930 return h2s;
931
932 out_free_h2s:
933 pool_free(pool_head_h2s, h2s);
934 out:
935 return NULL;
936}
937
938/* creates a new stream <id> on the h2c connection and returns it, or NULL in
939 * case of memory allocation error.
940 */
941static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
942{
943 struct session *sess = h2c->conn->owner;
944 struct conn_stream *cs;
945 struct h2s *h2s;
946
947 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
948 goto out;
949
950 h2s = h2s_new(h2c, id);
951 if (!h2s)
952 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200953
954 cs = cs_new(h2c->conn);
955 if (!cs)
956 goto out_close;
957
Olivier Houchard746fb772018-12-15 19:42:00 +0100958 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200959 h2s->cs = cs;
960 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200961 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200962
963 if (stream_create_from_cs(cs) < 0)
964 goto out_free_cs;
965
Willy Tarreau590a0512018-09-05 11:56:48 +0200966 /* We want the accept date presented to the next stream to be the one
967 * we have now, the handshake time to be null (since the next stream
968 * is not delayed by a handshake), and the idle time to count since
969 * right now.
970 */
971 sess->accept_date = date;
972 sess->tv_accept = now;
973 sess->t_handshake = 0;
974
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200975 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100976 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200977 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200978 return h2s;
979
980 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200981 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200982 cs_free(cs);
983 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200984 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200985 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200986 sess_log(sess);
987 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988}
989
Willy Tarreau751f2d02018-10-05 09:35:00 +0200990/* allocates a new stream associated to conn_stream <cs> on the h2c connection
991 * and returns it, or NULL in case of memory allocation error or if the highest
992 * possible stream ID was reached.
993 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100994static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200995{
996 struct h2s *h2s = NULL;
997
Willy Tarreau86949782019-01-31 10:42:05 +0100998 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200999 goto out;
1000
Willy Tarreaua80dca82019-01-24 17:08:28 +01001001 if (h2_streams_left(h2c) < 1)
1002 goto out;
1003
Willy Tarreau751f2d02018-10-05 09:35:00 +02001004 /* Defer choosing the ID until we send the first message to create the stream */
1005 h2s = h2s_new(h2c, 0);
1006 if (!h2s)
1007 goto out;
1008
1009 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001010 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001011 cs->ctx = h2s;
1012 h2c->nb_cs++;
1013
Willy Tarreau751f2d02018-10-05 09:35:00 +02001014 out:
1015 return h2s;
1016}
1017
Willy Tarreaube5b7152017-09-25 16:25:39 +02001018/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1019 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1020 * the various settings codes.
1021 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001022static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001023{
1024 struct buffer *res;
1025 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001026 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001027 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001028 int ret;
1029
1030 if (h2c_mux_busy(h2c, NULL)) {
1031 h2c->flags |= H2_CF_DEM_MBUSY;
1032 return 0;
1033 }
1034
Willy Tarreau44e973f2018-03-01 17:49:30 +01001035 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001036 if (!res) {
1037 h2c->flags |= H2_CF_MUX_MALLOC;
1038 h2c->flags |= H2_CF_DEM_MROOM;
1039 return 0;
1040 }
1041
1042 chunk_init(&buf, buf_data, sizeof(buf_data));
1043 chunk_memcpy(&buf,
1044 "\x00\x00\x00" /* length : 0 for now */
1045 "\x04\x00" /* type : 4 (settings), flags : 0 */
1046 "\x00\x00\x00\x00", /* stream ID : 0 */
1047 9);
1048
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001049 if (h2c->flags & H2_CF_IS_BACK) {
1050 /* send settings_enable_push=0 */
1051 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1052 }
1053
Willy Tarreaube5b7152017-09-25 16:25:39 +02001054 if (h2_settings_header_table_size != 4096) {
1055 char str[6] = "\x00\x01"; /* header_table_size */
1056
1057 write_n32(str + 2, h2_settings_header_table_size);
1058 chunk_memcat(&buf, str, 6);
1059 }
1060
1061 if (h2_settings_initial_window_size != 65535) {
1062 char str[6] = "\x00\x04"; /* initial_window_size */
1063
1064 write_n32(str + 2, h2_settings_initial_window_size);
1065 chunk_memcat(&buf, str, 6);
1066 }
1067
1068 if (h2_settings_max_concurrent_streams != 0) {
1069 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1070
1071 /* Note: 0 means "unlimited" for haproxy's config but not for
1072 * the protocol, so never send this value!
1073 */
1074 write_n32(str + 2, h2_settings_max_concurrent_streams);
1075 chunk_memcat(&buf, str, 6);
1076 }
1077
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001078 mfs = h2_settings_max_frame_size;
1079 if (mfs > global.tune.bufsize)
1080 mfs = global.tune.bufsize;
1081
1082 if (!mfs)
1083 mfs = global.tune.bufsize;
1084
1085 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001086 char str[6] = "\x00\x05"; /* max_frame_size */
1087
1088 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1089 * match bufsize - rewrite size, but at the moment it seems
1090 * that clients don't take care of it.
1091 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001092 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001093 chunk_memcat(&buf, str, 6);
1094 }
1095
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001096 h2_set_frame_size(buf.area, buf.data - 9);
1097 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001098 if (unlikely(ret <= 0)) {
1099 if (!ret) {
1100 h2c->flags |= H2_CF_MUX_MFULL;
1101 h2c->flags |= H2_CF_DEM_MROOM;
1102 return 0;
1103 }
1104 else {
1105 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1106 return 0;
1107 }
1108 }
1109 return ret;
1110}
1111
Willy Tarreau52eed752017-09-22 15:05:09 +02001112/* Try to receive a connection preface, then upon success try to send our
1113 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1114 * missing data. It may return an error in h2c.
1115 */
1116static int h2c_frt_recv_preface(struct h2c *h2c)
1117{
1118 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001119 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001120
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001121 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001122
1123 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001124 if (ret1 < 0)
1125 sess_log(h2c->conn->owner);
1126
Willy Tarreau52eed752017-09-22 15:05:09 +02001127 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1128 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1129 return 0;
1130 }
1131
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001132 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001133 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001134 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001135
Willy Tarreaube5b7152017-09-25 16:25:39 +02001136 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001137}
1138
Willy Tarreau01b44822018-10-03 14:26:37 +02001139/* Try to send a connection preface, then upon success try to send our
1140 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1141 * missing data. It may return an error in h2c.
1142 */
1143static int h2c_bck_send_preface(struct h2c *h2c)
1144{
1145 struct buffer *res;
1146
1147 if (h2c_mux_busy(h2c, NULL)) {
1148 h2c->flags |= H2_CF_DEM_MBUSY;
1149 return 0;
1150 }
1151
1152 res = h2_get_buf(h2c, &h2c->mbuf);
1153 if (!res) {
1154 h2c->flags |= H2_CF_MUX_MALLOC;
1155 h2c->flags |= H2_CF_DEM_MROOM;
1156 return 0;
1157 }
1158
1159 if (!b_data(res)) {
1160 /* preface not yet sent */
1161 b_istput(res, ist(H2_CONN_PREFACE));
1162 }
1163
1164 return h2c_send_settings(h2c);
1165}
1166
Willy Tarreau081d4722017-05-16 21:51:05 +02001167/* try to send a GOAWAY frame on the connection to report an error or a graceful
1168 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1169 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1170 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1171 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1172 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1173 * on unrecoverable failure. It will not attempt to send one again in this last
1174 * case so that it is safe to use h2c_error() to report such errors.
1175 */
1176static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1177{
1178 struct buffer *res;
1179 char str[17];
1180 int ret;
1181
1182 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1183 return 1; // claim that it worked
1184
1185 if (h2c_mux_busy(h2c, h2s)) {
1186 if (h2s)
1187 h2s->flags |= H2_SF_BLK_MBUSY;
1188 else
1189 h2c->flags |= H2_CF_DEM_MBUSY;
1190 return 0;
1191 }
1192
Willy Tarreau44e973f2018-03-01 17:49:30 +01001193 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001194 if (!res) {
1195 h2c->flags |= H2_CF_MUX_MALLOC;
1196 if (h2s)
1197 h2s->flags |= H2_SF_BLK_MROOM;
1198 else
1199 h2c->flags |= H2_CF_DEM_MROOM;
1200 return 0;
1201 }
1202
1203 /* len: 8, type: 7, flags: none, sid: 0 */
1204 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1205
1206 if (h2c->last_sid < 0)
1207 h2c->last_sid = h2c->max_id;
1208
1209 write_n32(str + 9, h2c->last_sid);
1210 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001211 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001212 if (unlikely(ret <= 0)) {
1213 if (!ret) {
1214 h2c->flags |= H2_CF_MUX_MFULL;
1215 if (h2s)
1216 h2s->flags |= H2_SF_BLK_MROOM;
1217 else
1218 h2c->flags |= H2_CF_DEM_MROOM;
1219 return 0;
1220 }
1221 else {
1222 /* we cannot report this error using GOAWAY, so we mark
1223 * it and claim a success.
1224 */
1225 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1226 h2c->flags |= H2_CF_GOAWAY_FAILED;
1227 return 1;
1228 }
1229 }
1230 h2c->flags |= H2_CF_GOAWAY_SENT;
1231 return ret;
1232}
1233
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001234/* Try to send an RST_STREAM frame on the connection for the indicated stream
1235 * during mux operations. This stream must be valid and cannot be closed
1236 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1237 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1238 * not yet.
1239 *
1240 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1241 * to write the message, it subscribes the stream to future notifications.
1242 */
1243static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1244{
1245 struct buffer *res;
1246 char str[13];
1247 int ret;
1248
1249 if (!h2s || h2s->st == H2_SS_CLOSED)
1250 return 1;
1251
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001252 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1253 * RST_STREAM in response to a RST_STREAM frame.
1254 */
1255 if (h2c->dft == H2_FT_RST_STREAM) {
1256 ret = 1;
1257 goto ignore;
1258 }
1259
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001260 if (h2c_mux_busy(h2c, h2s)) {
1261 h2s->flags |= H2_SF_BLK_MBUSY;
1262 return 0;
1263 }
1264
Willy Tarreau44e973f2018-03-01 17:49:30 +01001265 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001266 if (!res) {
1267 h2c->flags |= H2_CF_MUX_MALLOC;
1268 h2s->flags |= H2_SF_BLK_MROOM;
1269 return 0;
1270 }
1271
1272 /* len: 4, type: 3, flags: none */
1273 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1274 write_n32(str + 5, h2s->id);
1275 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001276 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001277
1278 if (unlikely(ret <= 0)) {
1279 if (!ret) {
1280 h2c->flags |= H2_CF_MUX_MFULL;
1281 h2s->flags |= H2_SF_BLK_MROOM;
1282 return 0;
1283 }
1284 else {
1285 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1286 return 0;
1287 }
1288 }
1289
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001290 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001291 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001292 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001293 return ret;
1294}
1295
1296/* Try to send an RST_STREAM frame on the connection for the stream being
1297 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001298 * error code, even if the stream is one of the dummy ones, and will update
1299 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001300 *
1301 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1302 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001303 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001305 */
1306static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1307{
1308 struct buffer *res;
1309 char str[13];
1310 int ret;
1311
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001312 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1313 * RST_STREAM in response to a RST_STREAM frame.
1314 */
1315 if (h2c->dft == H2_FT_RST_STREAM) {
1316 ret = 1;
1317 goto ignore;
1318 }
1319
Willy Tarreau27a84c92017-10-17 08:10:17 +02001320 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001321 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001322 return 0;
1323 }
1324
Willy Tarreau44e973f2018-03-01 17:49:30 +01001325 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001326 if (!res) {
1327 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001328 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001329 return 0;
1330 }
1331
1332 /* len: 4, type: 3, flags: none */
1333 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001334
Willy Tarreau27a84c92017-10-17 08:10:17 +02001335 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001336 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001337 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001338
Willy Tarreau27a84c92017-10-17 08:10:17 +02001339 if (unlikely(ret <= 0)) {
1340 if (!ret) {
1341 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001342 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001343 return 0;
1344 }
1345 else {
1346 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1347 return 0;
1348 }
1349 }
1350
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001351 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001352 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001353 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001354 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001355 }
1356
Willy Tarreau27a84c92017-10-17 08:10:17 +02001357 return ret;
1358}
1359
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001360/* try to send an empty DATA frame with the ES flag set to notify about the
1361 * end of stream and match a shutdown(write). If an ES was already sent as
1362 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1363 * on success or zero if nothing was done. In case of lack of room to write the
1364 * message, it subscribes the requesting stream to future notifications.
1365 */
1366static int h2_send_empty_data_es(struct h2s *h2s)
1367{
1368 struct h2c *h2c = h2s->h2c;
1369 struct buffer *res;
1370 char str[9];
1371 int ret;
1372
Willy Tarreau721c9742017-11-07 11:05:42 +01001373 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001374 return 1;
1375
1376 if (h2c_mux_busy(h2c, h2s)) {
1377 h2s->flags |= H2_SF_BLK_MBUSY;
1378 return 0;
1379 }
1380
Willy Tarreau44e973f2018-03-01 17:49:30 +01001381 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001382 if (!res) {
1383 h2c->flags |= H2_CF_MUX_MALLOC;
1384 h2s->flags |= H2_SF_BLK_MROOM;
1385 return 0;
1386 }
1387
1388 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1389 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1390 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001391 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001392 if (likely(ret > 0)) {
1393 h2s->flags |= H2_SF_ES_SENT;
1394 }
1395 else if (!ret) {
1396 h2c->flags |= H2_CF_MUX_MFULL;
1397 h2s->flags |= H2_SF_BLK_MROOM;
1398 return 0;
1399 }
1400 else {
1401 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1402 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001403 }
1404 return ret;
1405}
1406
Christopher Fauletf02ca002019-03-07 16:21:34 +01001407/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1408 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1409 * connection. The stream's state is automatically updated accordingly. If the
1410 * stream is orphaned, it is destroyed.
1411 */
1412static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1413{
1414 if (!h2s->cs) {
1415 /* this stream was already orphaned */
1416 h2s_destroy(h2s);
1417 return;
1418 }
1419
1420 h2s->cs->flags |= flags;
1421 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1422 h2s->cs->flags |= CS_FL_ERROR;
1423
1424 h2s_alert(h2s);
1425
1426 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1427 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001428 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001429 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001430 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001431 h2s_close(h2s);
1432}
1433
1434/* wake the streams attached to the connection, whose id is greater than <last>
1435 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001436 */
1437static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1438{
1439 struct eb32_node *node;
1440 struct h2s *h2s;
1441
1442 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001443 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001444
1445 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet203b2b02019-03-08 09:23:46 +01001446 flags |= (CS_FL_REOS|CS_FL_READ_NULL);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001447
Christopher Fauletf02ca002019-03-07 16:21:34 +01001448 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001449 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1450 while (node) {
1451 h2s = container_of(node, struct h2s, by_id);
1452 if (h2s->id <= last)
1453 break;
1454 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001455 h2s_wake_one_stream(h2s, flags);
1456 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001457
Christopher Fauletf02ca002019-03-07 16:21:34 +01001458 /* Wake all streams with unassigned ID (ID == 0) */
1459 node = eb32_lookup(&h2c->streams_by_id, 0);
1460 while (node) {
1461 h2s = container_of(node, struct h2s, by_id);
1462 if (h2s->id > 0)
1463 break;
1464 node = eb32_next(node);
1465 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001466 }
1467}
1468
Willy Tarreau3421aba2017-07-27 15:41:03 +02001469/* Increase all streams' outgoing window size by the difference passed in
1470 * argument. This is needed upon receipt of the settings frame if the initial
1471 * window size is different. The difference may be negative and the resulting
1472 * window size as well, for the time it takes to receive some window updates.
1473 */
1474static void h2c_update_all_ws(struct h2c *h2c, int diff)
1475{
1476 struct h2s *h2s;
1477 struct eb32_node *node;
1478
1479 if (!diff)
1480 return;
1481
1482 node = eb32_first(&h2c->streams_by_id);
1483 while (node) {
1484 h2s = container_of(node, struct h2s, by_id);
1485 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001486
1487 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1488 h2s->flags &= ~H2_SF_BLK_SFCTL;
1489 if (h2s->send_wait)
1490 LIST_ADDQ(&h2c->send_list, &h2s->list);
1491
1492 }
1493
Willy Tarreau3421aba2017-07-27 15:41:03 +02001494 node = eb32_next(node);
1495 }
1496}
1497
1498/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1499 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001500 * return an error in h2c. The caller must have already verified frame length
1501 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001502 */
1503static int h2c_handle_settings(struct h2c *h2c)
1504{
1505 unsigned int offset;
1506 int error;
1507
1508 if (h2c->dff & H2_F_SETTINGS_ACK) {
1509 if (h2c->dfl) {
1510 error = H2_ERR_FRAME_SIZE_ERROR;
1511 goto fail;
1512 }
1513 return 1;
1514 }
1515
Willy Tarreau3421aba2017-07-27 15:41:03 +02001516 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001517 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001518 return 0;
1519
1520 /* parse the frame */
1521 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001522 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1523 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001524
1525 switch (type) {
1526 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1527 /* we need to update all existing streams with the
1528 * difference from the previous iws.
1529 */
1530 if (arg < 0) { // RFC7540#6.5.2
1531 error = H2_ERR_FLOW_CONTROL_ERROR;
1532 goto fail;
1533 }
1534 h2c_update_all_ws(h2c, arg - h2c->miw);
1535 h2c->miw = arg;
1536 break;
1537 case H2_SETTINGS_MAX_FRAME_SIZE:
1538 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1539 error = H2_ERR_PROTOCOL_ERROR;
1540 goto fail;
1541 }
1542 h2c->mfs = arg;
1543 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001544 case H2_SETTINGS_ENABLE_PUSH:
1545 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1546 error = H2_ERR_PROTOCOL_ERROR;
1547 goto fail;
1548 }
1549 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001550 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1551 if (h2c->flags & H2_CF_IS_BACK) {
1552 /* the limit is only for the backend; for the frontend it is our limit */
1553 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1554 arg = h2_settings_max_concurrent_streams;
1555 h2c->streams_limit = arg;
1556 }
1557 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001558 }
1559 }
1560
1561 /* need to ACK this frame now */
1562 h2c->st0 = H2_CS_FRAME_A;
1563 return 1;
1564 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001565 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001566 h2c_error(h2c, error);
1567 return 0;
1568}
1569
1570/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1571 * success or one of the h2_status values.
1572 */
1573static int h2c_ack_settings(struct h2c *h2c)
1574{
1575 struct buffer *res;
1576 char str[9];
1577 int ret = -1;
1578
1579 if (h2c_mux_busy(h2c, NULL)) {
1580 h2c->flags |= H2_CF_DEM_MBUSY;
1581 return 0;
1582 }
1583
Willy Tarreau44e973f2018-03-01 17:49:30 +01001584 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001585 if (!res) {
1586 h2c->flags |= H2_CF_MUX_MALLOC;
1587 h2c->flags |= H2_CF_DEM_MROOM;
1588 return 0;
1589 }
1590
1591 memcpy(str,
1592 "\x00\x00\x00" /* length : 0 (no data) */
1593 "\x04" "\x01" /* type : 4, flags : ACK */
1594 "\x00\x00\x00\x00" /* stream ID */, 9);
1595
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001596 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001597 if (unlikely(ret <= 0)) {
1598 if (!ret) {
1599 h2c->flags |= H2_CF_MUX_MFULL;
1600 h2c->flags |= H2_CF_DEM_MROOM;
1601 return 0;
1602 }
1603 else {
1604 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1605 return 0;
1606 }
1607 }
1608 return ret;
1609}
1610
Willy Tarreaucf68c782017-10-10 17:11:41 +02001611/* processes a PING frame and schedules an ACK if needed. The caller must pass
1612 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001613 * missing data. The caller must have already verified frame length
1614 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001615 */
1616static int h2c_handle_ping(struct h2c *h2c)
1617{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001618 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001619 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001620 h2c->st0 = H2_CS_FRAME_A;
1621 return 1;
1622}
1623
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001624/* Try to send a window update for stream id <sid> and value <increment>.
1625 * Returns > 0 on success or zero on missing room or failure. It may return an
1626 * error in h2c.
1627 */
1628static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1629{
1630 struct buffer *res;
1631 char str[13];
1632 int ret = -1;
1633
1634 if (h2c_mux_busy(h2c, NULL)) {
1635 h2c->flags |= H2_CF_DEM_MBUSY;
1636 return 0;
1637 }
1638
Willy Tarreau44e973f2018-03-01 17:49:30 +01001639 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001640 if (!res) {
1641 h2c->flags |= H2_CF_MUX_MALLOC;
1642 h2c->flags |= H2_CF_DEM_MROOM;
1643 return 0;
1644 }
1645
1646 /* length: 4, type: 8, flags: none */
1647 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1648 write_n32(str + 5, sid);
1649 write_n32(str + 9, increment);
1650
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001651 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001652
1653 if (unlikely(ret <= 0)) {
1654 if (!ret) {
1655 h2c->flags |= H2_CF_MUX_MFULL;
1656 h2c->flags |= H2_CF_DEM_MROOM;
1657 return 0;
1658 }
1659 else {
1660 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1661 return 0;
1662 }
1663 }
1664 return ret;
1665}
1666
1667/* try to send pending window update for the connection. It's safe to call it
1668 * with no pending updates. Returns > 0 on success or zero on missing room or
1669 * failure. It may return an error in h2c.
1670 */
1671static int h2c_send_conn_wu(struct h2c *h2c)
1672{
1673 int ret = 1;
1674
1675 if (h2c->rcvd_c <= 0)
1676 return 1;
1677
Willy Tarreau97aaa672018-12-23 09:49:04 +01001678 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1679 /* increase the advertised connection window to 2G on
1680 * first update.
1681 */
1682 h2c->flags |= H2_CF_WINDOW_OPENED;
1683 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1684 }
1685
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001686 /* send WU for the connection */
1687 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1688 if (ret > 0)
1689 h2c->rcvd_c = 0;
1690
1691 return ret;
1692}
1693
1694/* try to send pending window update for the current dmux stream. It's safe to
1695 * call it with no pending updates. Returns > 0 on success or zero on missing
1696 * room or failure. It may return an error in h2c.
1697 */
1698static int h2c_send_strm_wu(struct h2c *h2c)
1699{
1700 int ret = 1;
1701
1702 if (h2c->rcvd_s <= 0)
1703 return 1;
1704
1705 /* send WU for the stream */
1706 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1707 if (ret > 0)
1708 h2c->rcvd_s = 0;
1709
1710 return ret;
1711}
1712
Willy Tarreaucf68c782017-10-10 17:11:41 +02001713/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1714 * success, 0 on missing data or one of the h2_status values.
1715 */
1716static int h2c_ack_ping(struct h2c *h2c)
1717{
1718 struct buffer *res;
1719 char str[17];
1720 int ret = -1;
1721
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001722 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001723 return 0;
1724
1725 if (h2c_mux_busy(h2c, NULL)) {
1726 h2c->flags |= H2_CF_DEM_MBUSY;
1727 return 0;
1728 }
1729
Willy Tarreau44e973f2018-03-01 17:49:30 +01001730 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001731 if (!res) {
1732 h2c->flags |= H2_CF_MUX_MALLOC;
1733 h2c->flags |= H2_CF_DEM_MROOM;
1734 return 0;
1735 }
1736
1737 memcpy(str,
1738 "\x00\x00\x08" /* length : 8 (same payload) */
1739 "\x06" "\x01" /* type : 6, flags : ACK */
1740 "\x00\x00\x00\x00" /* stream ID */, 9);
1741
1742 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001743 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001744
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001745 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001746 if (unlikely(ret <= 0)) {
1747 if (!ret) {
1748 h2c->flags |= H2_CF_MUX_MFULL;
1749 h2c->flags |= H2_CF_DEM_MROOM;
1750 return 0;
1751 }
1752 else {
1753 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1754 return 0;
1755 }
1756 }
1757 return ret;
1758}
1759
Willy Tarreau26f95952017-07-27 17:18:30 +02001760/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1761 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001762 * h2c or h2s. The caller must have already verified frame length and stream ID
1763 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001764 */
1765static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1766{
1767 int32_t inc;
1768 int error;
1769
Willy Tarreau26f95952017-07-27 17:18:30 +02001770 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001771 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001772 return 0;
1773
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001774 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001775
1776 if (h2c->dsi != 0) {
1777 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001778
1779 /* it's not an error to receive WU on a closed stream */
1780 if (h2s->st == H2_SS_CLOSED)
1781 return 1;
1782
1783 if (!inc) {
1784 error = H2_ERR_PROTOCOL_ERROR;
1785 goto strm_err;
1786 }
1787
1788 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1789 error = H2_ERR_FLOW_CONTROL_ERROR;
1790 goto strm_err;
1791 }
1792
1793 h2s->mws += inc;
1794 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1795 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001796 if (h2s->send_wait)
1797 LIST_ADDQ(&h2c->send_list, &h2s->list);
1798
Willy Tarreau26f95952017-07-27 17:18:30 +02001799 }
1800 }
1801 else {
1802 /* connection window update */
1803 if (!inc) {
1804 error = H2_ERR_PROTOCOL_ERROR;
1805 goto conn_err;
1806 }
1807
1808 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1809 error = H2_ERR_FLOW_CONTROL_ERROR;
1810 goto conn_err;
1811 }
1812
1813 h2c->mws += inc;
1814 }
1815
1816 return 1;
1817
1818 conn_err:
1819 h2c_error(h2c, error);
1820 return 0;
1821
1822 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001823 h2s_error(h2s, error);
1824 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001825 return 0;
1826}
1827
Willy Tarreaue96b0922017-10-30 00:28:29 +01001828/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001829 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1830 * have already verified frame length and stream ID validity. Described in
1831 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001832 */
1833static int h2c_handle_goaway(struct h2c *h2c)
1834{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001835 int last;
1836
Willy Tarreaue96b0922017-10-30 00:28:29 +01001837 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001838 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001839 return 0;
1840
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001841 last = h2_get_n32(&h2c->dbuf, 0);
1842 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001843 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001844 if (h2c->last_sid < 0)
1845 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001846 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001847}
1848
Willy Tarreau92153fc2017-12-03 19:46:19 +01001849/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001850 * invalid. Returns > 0 on success or zero on missing data. It may return an
1851 * error in h2c. The caller must have already verified frame length and stream
1852 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001853 */
1854static int h2c_handle_priority(struct h2c *h2c)
1855{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001856 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001857 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001858 return 0;
1859
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001860 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001861 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001862 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1863 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001864 }
1865 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866}
1867
Willy Tarreaucd234e92017-08-18 10:59:39 +02001868/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001869 * Returns > 0 on success or zero on missing data. The caller must have already
1870 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001871 */
1872static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1873{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001874 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001875 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001876 return 0;
1877
1878 /* late RST, already handled */
1879 if (h2s->st == H2_SS_CLOSED)
1880 return 1;
1881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001883 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884
1885 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001886 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001887 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001888 }
1889
1890 h2s->flags |= H2_SF_RST_RCVD;
1891 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001892}
1893
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001894/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1895 * It may return an error in h2c or h2s. The caller must consider that the
1896 * return value is the new h2s in case one was allocated (most common case).
1897 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001898 * errors here are reported as connection errors since it's impossible to
1899 * recover from such errors after the compression context has been altered.
1900 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001901static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001902{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001903 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001904 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001905 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001906 int error;
1907
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001908 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001909 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001910
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001911 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001912 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001913
1914 /* now either the frame is complete or the buffer is complete */
1915 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001916 /* The stream exists/existed, this must be a trailers frame */
1917 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001918 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001919 goto out;
1920 goto done;
1921 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001922 /* the connection was already killed by an RST, let's consume
1923 * the data and send another RST.
1924 */
1925 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1926 h2s = (struct h2s*)h2_error_stream;
1927 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001928 }
1929 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1930 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1931 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001932 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001933 goto conn_err;
1934 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001935 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1936 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001937
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001938 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001939
Willy Tarreau25919232019-01-03 14:48:18 +01001940 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001941 if (h2c->st0 >= H2_CS_ERROR)
1942 goto out;
1943
Willy Tarreau25919232019-01-03 14:48:18 +01001944 if (error <= 0) {
1945 if (error == 0)
1946 goto out; // missing data
1947
1948 /* Failed to decode this stream (e.g. too large request)
1949 * but the HPACK decompressor is still synchronized.
1950 */
1951 h2s = (struct h2s*)h2_error_stream;
1952 goto send_rst;
1953 }
1954
Willy Tarreau22de8d32018-09-05 19:55:58 +02001955 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001956 * positively from h2c_frt_stream_new(), the stream will report the error,
1957 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001958 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001959 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001960 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001961 h2s = (struct h2s*)h2_refused_stream;
1962 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001963 }
1964
1965 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001966 h2s->rxbuf = rxbuf;
1967 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001968 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001969
Willy Tarreau88d138e2019-01-02 19:38:14 +01001970 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001971 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001972 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001973
1974 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001975 if (h2s->cs)
1976 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001977 if (h2s->st == H2_SS_OPEN)
1978 h2s->st = H2_SS_HREM;
1979 else
1980 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001981 }
1982
Willy Tarreau3a429f02019-01-03 11:41:50 +01001983 /* update the max stream ID if the request is being processed */
1984 if (h2s->id > h2c->max_id)
1985 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001986
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001987 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001988
1989 conn_err:
1990 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001991 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001992
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001993 out:
1994 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001995 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001996
1997 send_rst:
1998 /* make the demux send an RST for the current stream. We may only
1999 * do this if we're certain that the HEADERS frame was properly
2000 * decompressed so that the HPACK decoder is still kept up to date.
2001 */
2002 h2_release_buf(h2c, &rxbuf);
2003 h2c->st0 = H2_CS_FRAME_E;
2004 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002005}
2006
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002007/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2008 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2009 * errors here are reported as connection errors since it's impossible to
2010 * recover from such errors after the compression context has been altered.
2011 */
2012static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2013{
2014 int error;
2015
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002016 if (!b_size(&h2c->dbuf))
2017 return NULL; // empty buffer
2018
2019 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2020 return NULL; // incomplete frame
2021
Willy Tarreau1915ca22019-01-24 11:49:37 +01002022 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002023
Willy Tarreau25919232019-01-03 14:48:18 +01002024 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002025 if (h2c->st0 >= H2_CS_ERROR)
2026 return NULL;
2027
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002028 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2029 /* RFC7540#5.1 */
2030 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2031 h2c->st0 = H2_CS_FRAME_E;
2032 return NULL;
2033 }
2034
Willy Tarreau25919232019-01-03 14:48:18 +01002035 if (error <= 0) {
2036 if (error == 0)
2037 return NULL; // missing data
2038
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002039 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002040 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002041 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002042 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002043 }
2044
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002045 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2046 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002047 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002048 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002049 }
2050
Willy Tarreau927b88b2019-03-04 08:03:25 +01002051 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002053 else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002055 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002056 h2s_close(h2s);
2057
2058 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002059}
2060
Willy Tarreau454f9052017-10-26 19:40:35 +02002061/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2062 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2063 */
2064static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2065{
2066 int error;
2067
2068 /* note that empty DATA frames are perfectly valid and sometimes used
2069 * to signal an end of stream (with the ES flag).
2070 */
2071
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002072 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002073 return 0; // empty buffer
2074
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002075 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002076 return 0; // incomplete frame
2077
2078 /* now either the frame is complete or the buffer is complete */
2079
Willy Tarreau454f9052017-10-26 19:40:35 +02002080 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2081 /* RFC7540#6.1 */
2082 error = H2_ERR_STREAM_CLOSED;
2083 goto strm_err;
2084 }
2085
Willy Tarreau1915ca22019-01-24 11:49:37 +01002086 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2087 /* RFC7540#8.1.2 */
2088 error = H2_ERR_PROTOCOL_ERROR;
2089 goto strm_err;
2090 }
2091
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002092 if (!h2_frt_transfer_data(h2s))
2093 return 0;
2094
Willy Tarreau454f9052017-10-26 19:40:35 +02002095 /* call the upper layers to process the frame, then let the upper layer
2096 * notify the stream about any change.
2097 */
2098 if (!h2s->cs) {
2099 error = H2_ERR_STREAM_CLOSED;
2100 goto strm_err;
2101 }
2102
Willy Tarreau8f650c32017-11-21 19:36:21 +01002103 if (h2c->st0 >= H2_CS_ERROR)
2104 return 0;
2105
Willy Tarreau721c9742017-11-07 11:05:42 +01002106 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002107 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002108 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002109 }
2110
2111 /* check for completion : the callee will change this to FRAME_A or
2112 * FRAME_H once done.
2113 */
2114 if (h2c->st0 == H2_CS_FRAME_P)
2115 return 0;
2116
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002117 /* last frame */
2118 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002119 if (h2s->cs)
2120 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002121 if (h2s->st == H2_SS_OPEN)
2122 h2s->st = H2_SS_HREM;
2123 else
2124 h2s_close(h2s);
2125
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002126 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002127
2128 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2129 /* RFC7540#8.1.2 */
2130 error = H2_ERR_PROTOCOL_ERROR;
2131 goto strm_err;
2132 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002133 }
2134
Willy Tarreau454f9052017-10-26 19:40:35 +02002135 return 1;
2136
Willy Tarreau454f9052017-10-26 19:40:35 +02002137 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002138 h2s_error(h2s, error);
2139 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002140 return 0;
2141}
2142
Willy Tarreaubc933932017-10-09 16:21:43 +02002143/* process Rx frames to be demultiplexed */
2144static void h2_process_demux(struct h2c *h2c)
2145{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002146 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002147 struct h2_fh hdr;
2148 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002149
Willy Tarreau081d4722017-05-16 21:51:05 +02002150 if (h2c->st0 >= H2_CS_ERROR)
2151 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002152
2153 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2154 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002155 if (h2c->flags & H2_CF_IS_BACK)
2156 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002157 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2158 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002159 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002160 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002161 sess_log(h2c->conn->owner);
2162 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002163 goto fail;
2164 }
2165
2166 h2c->max_id = 0;
2167 h2c->st0 = H2_CS_SETTINGS1;
2168 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002169
2170 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002171 /* ensure that what is pending is a valid SETTINGS frame
2172 * without an ACK.
2173 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002174 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002175 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002176 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002177 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002178 sess_log(h2c->conn->owner);
2179 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002180 goto fail;
2181 }
2182
2183 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2184 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2185 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2186 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002187 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002188 goto fail;
2189 }
2190
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002191 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002192 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2193 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2194 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002195 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002196 goto fail;
2197 }
2198
Willy Tarreau3bf69182018-12-21 15:34:50 +01002199 /* that's OK, switch to FRAME_P to process it. This is
2200 * a SETTINGS frame whose header has already been
2201 * deleted above.
2202 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002203 padlen = 0;
2204 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002205 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002206 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002207
2208 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002209 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002210 int ret = 0;
2211
2212 if (h2c->st0 >= H2_CS_ERROR)
2213 break;
2214
2215 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002216 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002217 break;
2218
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002219 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002221 if (!h2c->nb_streams) {
2222 /* only log if no other stream can report the error */
2223 sess_log(h2c->conn->owner);
2224 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002225 break;
2226 }
2227
Willy Tarreau3bf69182018-12-21 15:34:50 +01002228 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2229 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2230 * we read the pad length and drop it from the remaining
2231 * payload (one byte + the 9 remaining ones = 10 total
2232 * removed), so we have a frame payload starting after the
2233 * pad len. Flow controlled frames (DATA) also count the
2234 * padlen in the flow control, so it must be adjusted.
2235 */
2236 if (hdr.len < 1) {
2237 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2238 sess_log(h2c->conn->owner);
2239 goto fail;
2240 }
2241 hdr.len--;
2242
2243 if (b_data(&h2c->dbuf) < 10)
2244 break; // missing padlen
2245
2246 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2247
2248 if (padlen > hdr.len) {
2249 /* RFC7540#6.1 : pad length = length of
2250 * frame payload or greater => error.
2251 */
2252 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2253 sess_log(h2c->conn->owner);
2254 goto fail;
2255 }
2256
2257 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2258 h2c->rcvd_c++;
2259 h2c->rcvd_s++;
2260 }
2261 b_del(&h2c->dbuf, 1);
2262 }
2263 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002264
2265 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002266 h2c->dfl = hdr.len;
2267 h2c->dsi = hdr.sid;
2268 h2c->dft = hdr.ft;
2269 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002270 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002271 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002272
2273 /* check for minimum basic frame format validity */
2274 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2275 if (ret != H2_ERR_NO_ERROR) {
2276 h2c_error(h2c, ret);
2277 sess_log(h2c->conn->owner);
2278 goto fail;
2279 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002280 }
2281
2282 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002283 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2284
Willy Tarreau567beb82018-12-18 16:52:44 +01002285 if (tmp_h2s != h2s && h2s && h2s->cs &&
2286 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002287 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002288 /* we may have to signal the upper layers */
2289 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002290 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002291 }
2292 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002293
Willy Tarreaud7901432017-12-29 11:34:40 +01002294 if (h2c->st0 == H2_CS_FRAME_E)
2295 goto strm_err;
2296
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002297 if (h2s->st == H2_SS_IDLE &&
2298 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2299 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2300 * this state MUST be treated as a connection error
2301 */
2302 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002303 if (!h2c->nb_streams) {
2304 /* only log if no other stream can report the error */
2305 sess_log(h2c->conn->owner);
2306 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002307 break;
2308 }
2309
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002310 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2311 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2312 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002313 * this state MUST be treated as a stream error.
2314 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2315 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002316 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002317 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2318 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2319 else
2320 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002321 goto strm_err;
2322 }
2323
Willy Tarreauab837502017-12-27 15:07:30 +01002324 /* Below the management of frames received in closed state is a
2325 * bit hackish because the spec makes strong differences between
2326 * streams closed by receiving RST, sending RST, and seeing ES
2327 * in both directions. In addition to this, the creation of a
2328 * new stream reusing the identifier of a closed one will be
2329 * detected here. Given that we cannot keep track of all closed
2330 * streams forever, we consider that unknown closed streams were
2331 * closed on RST received, which allows us to respond with an
2332 * RST without breaking the connection (eg: to abort a transfer).
2333 * Some frames have to be silently ignored as well.
2334 */
2335 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002336 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002337 /* #5.1.1: The identifier of a newly
2338 * established stream MUST be numerically
2339 * greater than all streams that the initiating
2340 * endpoint has opened or reserved. This
2341 * governs streams that are opened using a
2342 * HEADERS frame and streams that are reserved
2343 * using PUSH_PROMISE. An endpoint that
2344 * receives an unexpected stream identifier
2345 * MUST respond with a connection error.
2346 */
2347 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2348 goto strm_err;
2349 }
2350
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002351 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002352 /* RFC7540#5.1:closed: an endpoint that
2353 * receives any frame other than PRIORITY after
2354 * receiving a RST_STREAM MUST treat that as a
2355 * stream error of type STREAM_CLOSED.
2356 *
2357 * Note that old streams fall into this category
2358 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002359 *
2360 * However, we cannot generalize this to all frame types. Those
2361 * carrying compression state must still be processed before
2362 * being dropped or we'll desynchronize the decoder. This can
2363 * happen with request trailers received after sending an
2364 * RST_STREAM, or with header/trailers responses received after
2365 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002366 */
2367 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2368 h2c->st0 = H2_CS_FRAME_E;
2369 goto strm_err;
2370 }
2371
2372 /* RFC7540#5.1:closed: if this state is reached as a
2373 * result of sending a RST_STREAM frame, the peer that
2374 * receives the RST_STREAM might have already sent
2375 * frames on the stream that cannot be withdrawn. An
2376 * endpoint MUST ignore frames that it receives on
2377 * closed streams after it has sent a RST_STREAM
2378 * frame. An endpoint MAY choose to limit the period
2379 * over which it ignores frames and treat frames that
2380 * arrive after this time as being in error.
2381 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002382 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002383 /* RFC7540#5.1:closed: any frame other than
2384 * PRIO/WU/RST in this state MUST be treated as
2385 * a connection error
2386 */
2387 if (h2c->dft != H2_FT_RST_STREAM &&
2388 h2c->dft != H2_FT_PRIORITY &&
2389 h2c->dft != H2_FT_WINDOW_UPDATE) {
2390 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2391 goto strm_err;
2392 }
2393 }
2394 }
2395
Willy Tarreauc0da1962017-10-30 18:38:00 +01002396#if 0
2397 // problem below: it is not possible to completely ignore such
2398 // streams as we need to maintain the compression state as well
2399 // and for this we need to completely process these frames (eg:
2400 // HEADERS frames) as well as counting DATA frames to emit
2401 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2402 // This is a typical case of layer violation where the
2403 // transported contents are critical to the connection's
2404 // validity and must be ignored at the same time :-(
2405
2406 /* graceful shutdown, ignore streams whose ID is higher than
2407 * the one advertised in GOAWAY. RFC7540#6.8.
2408 */
2409 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002410 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2411 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002412 h2c->dfl -= ret;
2413 ret = h2c->dfl == 0;
2414 goto strm_err;
2415 }
2416#endif
2417
Willy Tarreau7e98c052017-10-10 15:56:59 +02002418 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002419 case H2_FT_SETTINGS:
2420 if (h2c->st0 == H2_CS_FRAME_P)
2421 ret = h2c_handle_settings(h2c);
2422
2423 if (h2c->st0 == H2_CS_FRAME_A)
2424 ret = h2c_ack_settings(h2c);
2425 break;
2426
Willy Tarreaucf68c782017-10-10 17:11:41 +02002427 case H2_FT_PING:
2428 if (h2c->st0 == H2_CS_FRAME_P)
2429 ret = h2c_handle_ping(h2c);
2430
2431 if (h2c->st0 == H2_CS_FRAME_A)
2432 ret = h2c_ack_ping(h2c);
2433 break;
2434
Willy Tarreau26f95952017-07-27 17:18:30 +02002435 case H2_FT_WINDOW_UPDATE:
2436 if (h2c->st0 == H2_CS_FRAME_P)
2437 ret = h2c_handle_window_update(h2c, h2s);
2438 break;
2439
Willy Tarreau61290ec2017-10-17 08:19:21 +02002440 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002441 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2442 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2443 * frames' parsers consume all following CONTINUATION
2444 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002445 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002446 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2447 sess_log(h2c->conn->owner);
2448 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002449
Willy Tarreau13278b42017-10-13 19:23:14 +02002450 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002451 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002452 if (h2c->flags & H2_CF_IS_BACK)
2453 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2454 else
2455 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002456 if (tmp_h2s) {
2457 h2s = tmp_h2s;
2458 ret = 1;
2459 }
2460 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002461 break;
2462
Willy Tarreau454f9052017-10-26 19:40:35 +02002463 case H2_FT_DATA:
2464 if (h2c->st0 == H2_CS_FRAME_P)
2465 ret = h2c_frt_handle_data(h2c, h2s);
2466
2467 if (h2c->st0 == H2_CS_FRAME_A)
2468 ret = h2c_send_strm_wu(h2c);
2469 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002470
Willy Tarreau92153fc2017-12-03 19:46:19 +01002471 case H2_FT_PRIORITY:
2472 if (h2c->st0 == H2_CS_FRAME_P)
2473 ret = h2c_handle_priority(h2c);
2474 break;
2475
Willy Tarreaucd234e92017-08-18 10:59:39 +02002476 case H2_FT_RST_STREAM:
2477 if (h2c->st0 == H2_CS_FRAME_P)
2478 ret = h2c_handle_rst_stream(h2c, h2s);
2479 break;
2480
Willy Tarreaue96b0922017-10-30 00:28:29 +01002481 case H2_FT_GOAWAY:
2482 if (h2c->st0 == H2_CS_FRAME_P)
2483 ret = h2c_handle_goaway(h2c);
2484 break;
2485
Willy Tarreau1c661982017-10-30 13:52:01 +01002486 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002487 default:
2488 /* drop frames that we ignore. They may be larger than
2489 * the buffer so we drain all of their contents until
2490 * we reach the end.
2491 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002492 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2493 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002494 h2c->dfl -= ret;
2495 ret = h2c->dfl == 0;
2496 }
2497
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002498 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002499 /* We may have to send an RST if not done yet */
2500 if (h2s->st == H2_SS_ERROR)
2501 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002502
Willy Tarreaua20a5192017-12-27 11:02:06 +01002503 if (h2c->st0 == H2_CS_FRAME_E)
2504 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002505
Willy Tarreau7e98c052017-10-10 15:56:59 +02002506 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002507 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002508 break;
2509
2510 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002511 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002512 h2c->st0 = H2_CS_FRAME_H;
2513 }
2514 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002515
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002516 if (h2c->rcvd_c > 0 &&
2517 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2518 h2c_send_conn_wu(h2c);
2519
Willy Tarreau52eed752017-09-22 15:05:09 +02002520 fail:
2521 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002522 if (h2s && h2s->cs &&
2523 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002524 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002525 /* we may have to signal the upper layers */
2526 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002527 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002528 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002529
Willy Tarreau47b515a2018-12-21 16:09:41 +01002530 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002531}
2532
2533/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2534 * the end.
2535 */
2536static int h2_process_mux(struct h2c *h2c)
2537{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002538 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002539
Willy Tarreau01b44822018-10-03 14:26:37 +02002540 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2541 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2542 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2543 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2544 if (h2c->st0 == H2_CS_ERROR) {
2545 h2c->st0 = H2_CS_ERROR2;
2546 sess_log(h2c->conn->owner);
2547 }
2548 goto fail;
2549 }
2550 h2c->st0 = H2_CS_SETTINGS1;
2551 }
2552 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002553 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002554 return 1;
2555 }
2556
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002557 /* start by sending possibly pending window updates */
2558 if (h2c->rcvd_c > 0 &&
2559 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2560 h2c_send_conn_wu(h2c) < 0)
2561 goto fail;
2562
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002563 /* First we always process the flow control list because the streams
2564 * waiting there were already elected for immediate emission but were
2565 * blocked just on this.
2566 */
2567
Olivier Houchardd360ac62019-03-22 17:37:16 +01002568 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002569 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2570 h2c->st0 >= H2_CS_ERROR)
2571 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002572 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2573 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002574
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002575 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002576 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2577 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002578 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002579 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002580 }
2581
Olivier Houchardd360ac62019-03-22 17:37:16 +01002582 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002583 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2584 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002585 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2586 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002587
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002588 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002589 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2590 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002591 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002592 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002593 }
2594
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002595 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002596 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002597 if (h2c->st0 == H2_CS_ERROR) {
2598 if (h2c->max_id >= 0) {
2599 h2c_send_goaway_error(h2c, NULL);
2600 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2601 return 0;
2602 }
2603
2604 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2605 }
2606 return 1;
2607 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002608 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002609}
2610
Willy Tarreau62f52692017-10-08 23:01:42 +02002611
Willy Tarreau479998a2018-11-18 06:30:59 +01002612/* Attempt to read data, and subscribe if none available.
2613 * The function returns 1 if data has been received, otherwise zero.
2614 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002615static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002616{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002617 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002618 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002619 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002620 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002621
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002622 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002623 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002624
Willy Tarreau315d8072017-12-10 22:17:57 +01002625 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002626 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002627
Willy Tarreau44e973f2018-03-01 17:49:30 +01002628 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002629 if (!buf) {
2630 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002631 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002632 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002633
Olivier Houchard7505f942018-08-21 18:10:44 +02002634 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002635 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002636 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2637 /* HTX in use : try to pre-align the buffer like the
2638 * rxbufs will be to optimize memory copies. We'll make
2639 * sure that the frame header lands at the end of the
2640 * HTX block to alias it upon recv. We cannot use the
2641 * head because rcv_buf() will realign the buffer if
2642 * it's empty. Thus we cheat and pretend we already
2643 * have a few bytes there.
2644 */
2645 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002646 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002647 }
2648 else
2649 max = b_room(buf);
2650
Olivier Houchard7505f942018-08-21 18:10:44 +02002651 if (max)
2652 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2653 else
2654 ret = 0;
2655 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002656
Olivier Houchard53216e72018-10-10 15:46:36 +02002657 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002658 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002659
Olivier Houcharda1411e62018-08-17 18:42:48 +02002660 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002661 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002662 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002663 }
2664
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002665 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002666 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002667 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002668}
2669
Willy Tarreau479998a2018-11-18 06:30:59 +01002670/* Try to send data if possible.
2671 * The function returns 1 if data have been sent, otherwise zero.
2672 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002673static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002674{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002675 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002676 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002677 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002678
2679 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002680 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002681
Olivier Houchard7505f942018-08-21 18:10:44 +02002682
Willy Tarreaua2af5122017-10-09 11:56:46 +02002683 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2684 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002685 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002686 }
2687
Willy Tarreaubc933932017-10-09 16:21:43 +02002688 /* This loop is quite simple : it tries to fill as much as it can from
2689 * pending streams into the existing buffer until it's reportedly full
2690 * or the end of send requests is reached. Then it tries to send this
2691 * buffer's contents out, marks it not full if at least one byte could
2692 * be sent, and tries again.
2693 *
2694 * The snd_buf() function normally takes a "flags" argument which may
2695 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2696 * data immediately comes and CO_SFL_STREAMER to indicate that the
2697 * connection is streaming lots of data (used to increase TLS record
2698 * size at the expense of latency). The former can be sent any time
2699 * there's a buffer full flag, as it indicates at least one stream
2700 * attempted to send and failed so there are pending data. An
2701 * alternative would be to set it as long as there's an active stream
2702 * but that would be problematic for ACKs until we have an absolute
2703 * guarantee that all waiters have at least one byte to send. The
2704 * latter should possibly not be set for now.
2705 */
2706
2707 done = 0;
2708 while (!done) {
2709 unsigned int flags = 0;
2710
2711 /* fill as much as we can into the current buffer */
2712 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2713 done = h2_process_mux(h2c);
2714
Olivier Houchard2b094432019-01-29 18:28:36 +01002715 if (h2c->flags & H2_CF_MUX_MALLOC)
2716 break;
2717
Willy Tarreaubc933932017-10-09 16:21:43 +02002718 if (conn->flags & CO_FL_ERROR)
2719 break;
2720
2721 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2722 flags |= CO_SFL_MSG_MORE;
2723
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002724 if (b_data(&h2c->mbuf)) {
2725 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002726 if (!ret)
2727 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002728 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002729 b_del(&h2c->mbuf, ret);
2730 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002731 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002732
2733 /* wrote at least one byte, the buffer is not full anymore */
2734 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2735 }
2736
Willy Tarreaua2af5122017-10-09 11:56:46 +02002737 if (conn->flags & CO_FL_SOCK_WR_SH) {
2738 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002739 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002740 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002741 /* We're not full anymore, so we can wake any task that are waiting
2742 * for us.
2743 */
2744 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002745 struct h2s *h2s;
2746
2747 list_for_each_entry(h2s, &h2c->send_list, list) {
2748 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2749 break;
2750 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2751 continue;
2752
2753 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002754 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2755 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002756 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002757 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002758 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002759 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002760 /* We're done, no more to send */
2761 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002762 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002763schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002764 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2765 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002766 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002767}
2768
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002769/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002770static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2771{
2772 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002773 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002774
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002775 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002776 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002777 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002778 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002779 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002780 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002781 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002782}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002783
Willy Tarreau62f52692017-10-08 23:01:42 +02002784/* callback called on any event by the connection handler.
2785 * It applies changes and returns zero, or < 0 if it wants immediate
2786 * destruction of the connection (which normally doesn not happen in h2).
2787 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002788static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002789{
Olivier Houchard7505f942018-08-21 18:10:44 +02002790 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002791
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002792 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002793 h2_process_demux(h2c);
2794
2795 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002796 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002797
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002798 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002799 h2c->flags &= ~H2_CF_DEM_DFULL;
2800 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002801 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002802
Willy Tarreau0b37d652018-10-03 10:33:02 +02002803 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002804 /* frontend is stopping, reload likely in progress, let's try
2805 * to announce a graceful shutdown if not yet done. We don't
2806 * care if it fails, it will be tried again later.
2807 */
2808 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2809 if (h2c->last_sid < 0)
2810 h2c->last_sid = (1U << 31) - 1;
2811 h2c_send_goaway_error(h2c, NULL);
2812 }
2813 }
2814
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002815 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002816 * If we received early data, and the handshake is done, wake
2817 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002818 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002819 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2820 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2821 struct eb32_node *node;
2822 struct h2s *h2s;
2823
2824 h2c->flags |= H2_CF_WAIT_FOR_HS;
2825 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2826
2827 while (node) {
2828 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002829 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002830 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002831 node = eb32_next(node);
2832 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002833 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002834
Willy Tarreau26bd7612017-10-09 16:47:04 +02002835 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002836 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2837 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2838 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002839 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002840
2841 if (eb_is_empty(&h2c->streams_by_id)) {
2842 /* no more stream, kill the connection now */
2843 h2_release(conn);
2844 return -1;
2845 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002846 }
2847
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002849 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002850
Olivier Houchard53216e72018-10-10 15:46:36 +02002851 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2852 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2853 (h2c->st0 != H2_CS_ERROR &&
2854 !b_data(&h2c->mbuf) &&
2855 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2856 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002857 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002858
Willy Tarreau3f133572017-10-31 19:21:06 +01002859 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002860 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002861 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002862 task_queue(h2c->task);
2863 }
2864 else
2865 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002866 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002867
Olivier Houchard7505f942018-08-21 18:10:44 +02002868 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002869 return 0;
2870}
2871
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002872/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002873static int h2_wake(struct connection *conn)
2874{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002875 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002876
2877 return (h2_process(h2c));
2878}
2879
Willy Tarreauea392822017-10-31 10:02:25 +01002880/* Connection timeout management. The principle is that if there's no receipt
2881 * nor sending for a certain amount of time, the connection is closed. If the
2882 * MUX buffer still has lying data or is not allocatable, the connection is
2883 * immediately killed. If it's allocatable and empty, we attempt to send a
2884 * GOAWAY frame.
2885 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002886static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002887{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002888 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002889 int expired = tick_is_expired(t->expire, now_ms);
2890
Willy Tarreau0975f112018-03-29 15:22:59 +02002891 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002892 return t;
2893
Willy Tarreau0975f112018-03-29 15:22:59 +02002894 task_delete(t);
2895 task_free(t);
2896
2897 if (!h2c) {
2898 /* resources were already deleted */
2899 return NULL;
2900 }
2901
2902 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002903 h2c_error(h2c, H2_ERR_NO_ERROR);
2904 h2_wake_some_streams(h2c, 0, 0);
2905
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002906 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002907 /* don't even try to send a GOAWAY, the buffer is stuck */
2908 h2c->flags |= H2_CF_GOAWAY_FAILED;
2909 }
2910
2911 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002912 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002913 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2914 h2c->flags |= H2_CF_GOAWAY_FAILED;
2915
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002916 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2917 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002918 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002919 b_del(&h2c->mbuf, ret);
2920 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002921 }
2922 }
Willy Tarreauea392822017-10-31 10:02:25 +01002923
Willy Tarreau0975f112018-03-29 15:22:59 +02002924 /* either we can release everything now or it will be done later once
2925 * the last stream closes.
2926 */
2927 if (eb_is_empty(&h2c->streams_by_id))
2928 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002929
Willy Tarreauea392822017-10-31 10:02:25 +01002930 return NULL;
2931}
2932
2933
Willy Tarreau62f52692017-10-08 23:01:42 +02002934/*******************************************/
2935/* functions below are used by the streams */
2936/*******************************************/
2937
2938/*
2939 * Attach a new stream to a connection
2940 * (Used for outgoing connections)
2941 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002942static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002943{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002944 struct conn_stream *cs;
2945 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002946 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002947
2948 cs = cs_new(conn);
2949 if (!cs)
2950 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002951 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002952 if (!h2s) {
2953 cs_free(cs);
2954 return NULL;
2955 }
2956 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002957}
2958
Willy Tarreaufafd3982018-11-18 21:29:20 +01002959/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2960 * We have to scan because we may have some orphan streams. It might be
2961 * beneficial to scan backwards from the end to reduce the likeliness to find
2962 * orphans.
2963 */
2964static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2965{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002966 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002967 struct h2s *h2s;
2968 struct eb32_node *node;
2969
2970 node = eb32_first(&h2c->streams_by_id);
2971 while (node) {
2972 h2s = container_of(node, struct h2s, by_id);
2973 if (h2s->cs)
2974 return h2s->cs;
2975 node = eb32_next(node);
2976 }
2977 return NULL;
2978}
2979
Willy Tarreau62f52692017-10-08 23:01:42 +02002980/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002981 * Destroy the mux and the associated connection, if it is no longer used
2982 */
2983static void h2_destroy(struct connection *conn)
2984{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002985 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002986
2987 if (eb_is_empty(&h2c->streams_by_id))
2988 h2_release(h2c->conn);
2989}
2990
2991/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002992 * Detach the stream from the connection and possibly release the connection.
2993 */
2994static void h2_detach(struct conn_stream *cs)
2995{
Willy Tarreau60935142017-10-16 18:11:19 +02002996 struct h2s *h2s = cs->ctx;
2997 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002998 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002999
3000 cs->ctx = NULL;
3001 if (!h2s)
3002 return;
3003
Olivier Houchardf502aca2018-12-14 19:42:40 +01003004 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003005 h2c = h2s->h2c;
3006 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003007 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003008 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3009 !h2_frt_has_too_many_cs(h2c)) {
3010 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003011 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003012 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02003013 }
Willy Tarreau60935142017-10-16 18:11:19 +02003014
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003015 /* this stream may be blocked waiting for some data to leave (possibly
3016 * an ES or RST frame), so orphan it in this case.
3017 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003018 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003019 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003020 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003021 return;
3022
Willy Tarreau45f752e2017-10-30 15:44:59 +01003023 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3024 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3025 /* unblock the connection if it was blocked on this
3026 * stream.
3027 */
3028 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3029 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003030 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003031 }
3032
Willy Tarreau71049cc2018-03-28 13:56:39 +02003033 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003034
Olivier Houchard8a786902018-12-15 16:05:40 +01003035 if (h2c->flags & H2_CF_IS_BACK &&
3036 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003037 if (!(h2c->conn->flags &
3038 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3039 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003040 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003041 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3042 h2c->conn->owner = NULL;
3043 if (eb_is_empty(&h2c->streams_by_id)) {
3044 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3045 /* The server doesn't want it, let's kill the connection right away */
3046 h2c->conn->mux->destroy(h2c->conn);
3047 return;
3048 }
3049 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003050 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003051 if (eb_is_empty(&h2c->streams_by_id)) {
3052 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3053 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3054 return;
3055 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003056 /* Never ever allow to reuse a connection from a non-reuse backend */
3057 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3058 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003059 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003060 struct server *srv = objt_server(h2c->conn->target);
3061
3062 if (srv) {
3063 if (h2c->conn->flags & CO_FL_PRIVATE)
3064 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3065 else
3066 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3067 }
3068
3069 }
3070 }
3071 }
3072
Willy Tarreaue323f342018-03-28 13:51:45 +02003073 /* We don't want to close right now unless we're removing the
3074 * last stream, and either the connection is in error, or it
3075 * reached the ID already specified in a GOAWAY frame received
3076 * or sent (as seen by last_sid >= 0).
3077 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003078 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003079 /* no more stream will come, kill it now */
3080 h2_release(h2c->conn);
3081 }
3082 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003083 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003084 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3085 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003086 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003087 else
3088 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003089 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003090}
3091
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003092/* Performs a synchronous or asynchronous shutr().
3093 * FIXME: guess what the return code tries to indicate!
3094 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003095static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003096{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003097 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003098 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003099
Willy Tarreau721c9742017-11-07 11:05:42 +01003100 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003101 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003102
Willy Tarreau18059042019-01-31 19:12:48 +01003103 /* a connstream may require us to immediately kill the whole connection
3104 * for example because of a "tcp-request content reject" rule that is
3105 * normally used to limit abuse. In this case we schedule a goaway to
3106 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003107 */
Willy Tarreau18059042019-01-31 19:12:48 +01003108 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3109 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3110 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3111 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3112 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003113 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3114 /* Nothing was never sent for this stream, so reset with
3115 * REFUSED_STREAM error to let the client retry the
3116 * request.
3117 */
3118 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3119 }
Willy Tarreau18059042019-01-31 19:12:48 +01003120
Willy Tarreau90c32322017-11-24 08:00:30 +01003121 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003122 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003123 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003124
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003125 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003126 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003127 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003128
Olivier Houchard7a977432019-03-21 15:47:13 +01003129 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003130add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003131 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003132 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003133 if (h2s->flags & H2_SF_BLK_MFCTL) {
3134 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3135 h2s->send_wait = sw;
3136 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3137 h2s->send_wait = sw;
3138 LIST_ADDQ(&h2c->send_list, &h2s->list);
3139 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003140 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003141 /* Let the handler know we want shutr */
3142 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003143 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003144}
3145
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003146/* Performs a synchronous or asynchronous shutw().
3147 * FIXME: guess what the return code tries to indicate!
3148 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003149static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003150{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003151 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003152 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003153
Willy Tarreau721c9742017-11-07 11:05:42 +01003154 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003155 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003156
Willy Tarreau67434202017-11-06 20:20:51 +01003157 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003158 /* we can cleanly close using an empty data frame only after headers */
3159
3160 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3161 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003162 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003163
3164 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003165 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003166 else
3167 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003168 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003169 /* a connstream may require us to immediately kill the whole connection
3170 * for example because of a "tcp-request content reject" rule that is
3171 * normally used to limit abuse. In this case we schedule a goaway to
3172 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003173 */
Willy Tarreau18059042019-01-31 19:12:48 +01003174 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3175 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3176 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3177 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3178 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003179 else {
3180 /* Nothing was never sent for this stream, so reset with
3181 * REFUSED_STREAM error to let the client retry the
3182 * request.
3183 */
3184 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3185 }
Willy Tarreau18059042019-01-31 19:12:48 +01003186
Willy Tarreau90c32322017-11-24 08:00:30 +01003187 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003188 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003189 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003190
Willy Tarreau00dd0782018-03-01 16:31:34 +01003191 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003192 }
3193
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003194 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003195 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003196 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003197
3198 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003199 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003200 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003201 if (h2s->flags & H2_SF_BLK_MFCTL) {
3202 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3203 h2s->send_wait = sw;
3204 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3205 h2s->send_wait = sw;
3206 LIST_ADDQ(&h2c->send_list, &h2s->list);
3207 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003208 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003209 /* let the handler know we want to shutw */
3210 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003211 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003212}
3213
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003214/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3215 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3216 * and prevented the last frame from being emitted.
3217 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003218static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3219{
3220 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003221 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003222 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003223
Olivier Houchard2c68a462018-12-15 22:42:20 +01003224 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003225 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003226 h2s->send_wait = NULL;
3227 LIST_DEL(&h2s->list);
3228 LIST_INIT(&h2s->list);
3229 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003230 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003231 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003232 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003233 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003234
Olivier Houchard7a977432019-03-21 15:47:13 +01003235 /* We're no longer trying to send anything, let's destroy the h2s */
3236 if (!ret) {
3237 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003238 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003239
3240 if (h2c_is_dead(h2c))
3241 h2_release(h2c->conn);
3242 }
3243
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003244 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003245}
3246
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003247/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003248static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3249{
3250 struct h2s *h2s = cs->ctx;
3251
3252 if (!mode)
3253 return;
3254
3255 h2_do_shutr(h2s);
3256}
3257
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003258/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003259static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3260{
3261 struct h2s *h2s = cs->ctx;
3262
3263 h2_do_shutw(h2s);
3264}
3265
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003266/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003267 * HTX request or response depending on the connection's side. Returns a
3268 * positive value on success, a negative value on failure, or 0 if it couldn't
3269 * proceed. May report connection errors in h2c->errcode if the frame is
3270 * non-decodable and the connection unrecoverable. In absence of connection
3271 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003272 *
3273 * The function may fold CONTINUATION frames into the initial HEADERS frame
3274 * by removing padding and next frame header, then moving the CONTINUATION
3275 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3276 * leaving a hole between the main frame and the beginning of the next one.
3277 * The possibly remaining incomplete or next frame at the end may be moved
3278 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3279 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3280 *
3281 * A buffer at the beginning of processing may look like this :
3282 *
3283 * ,---.---------.-----.--------------.--------------.------.---.
3284 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3285 * `---^---------^-----^--------------^--------------^------^---'
3286 * | | <-----> | |
3287 * area | dpl | wrap
3288 * |<--------------> |
3289 * | dfl |
3290 * |<-------------------------------------------------->|
3291 * head data
3292 *
3293 * Padding is automatically overwritten when folding, participating to the
3294 * hole size after dfl :
3295 *
3296 * ,---.------------------------.-----.--------------.------.---.
3297 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3298 * `---^------------------------^-----^--------------^------^---'
3299 * | | <-----> | |
3300 * area | hole | wrap
3301 * |<-----------------------> |
3302 * | dfl |
3303 * |<-------------------------------------------------->|
3304 * head data
3305 *
3306 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3307 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3308 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003309 *
3310 * The <flags> field must point to either the stream's flags or to a copy of it
3311 * so that the function can update the following flags :
3312 * - H2_SF_DATA_CLEN when content-length is seen
3313 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3314 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003315 *
3316 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3317 * decoding, in order to detect if we're dealing with a headers or a trailers
3318 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003319 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003320static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len)
Willy Tarreau13278b42017-10-13 19:23:14 +02003321{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003322 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003323 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003324 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003325 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003326 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003327 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003328 int flen; // header frame len
3329 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003330 int ret = 0;
3331 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003332 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003333 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003334
Willy Tarreauea18f862018-12-22 20:19:26 +01003335next_frame:
3336 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3337 goto leave; // incomplete input frame
3338
3339 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3340 * this case, we'll try to paste it immediately after the initial
3341 * HEADERS frame payload and kill any possible padding. The initial
3342 * frame's length will be increased to represent the concatenation
3343 * of the two frames. The next frame is read from position <tlen>
3344 * and written at position <flen> (minus padding if some is present).
3345 */
3346 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3347 struct h2_fh hdr;
3348 int clen; // CONTINUATION frame's payload length
3349
3350 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3351 /* no more data, the buffer may be full, either due to
3352 * too large a frame or because of too large a hole that
3353 * we're going to compact at the end.
3354 */
3355 goto leave;
3356 }
3357
3358 if (hdr.ft != H2_FT_CONTINUATION) {
3359 /* RFC7540#6.10: frame of unexpected type */
3360 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3361 goto fail;
3362 }
3363
3364 if (hdr.sid != h2c->dsi) {
3365 /* RFC7540#6.10: frame of different stream */
3366 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3367 goto fail;
3368 }
3369
3370 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3371 /* RFC7540#4.2: invalid frame length */
3372 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3373 goto fail;
3374 }
3375
3376 /* detect when we must stop aggragating frames */
3377 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3378
3379 /* Take as much as we can of the CONTINUATION frame's payload */
3380 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3381 if (clen > hdr.len)
3382 clen = hdr.len;
3383
3384 /* Move the frame's payload over the padding, hole and frame
3385 * header. At least one of hole or dpl is null (see diagrams
3386 * above). The hole moves after the new aggragated frame.
3387 */
3388 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3389 h2c->dfl += clen - h2c->dpl;
3390 hole += h2c->dpl + 9;
3391 h2c->dpl = 0;
3392 goto next_frame;
3393 }
3394
3395 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003396
Willy Tarreau13278b42017-10-13 19:23:14 +02003397 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003398 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003399 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003400 copy = alloc_trash_chunk();
3401 if (!copy) {
3402 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3403 goto fail;
3404 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003405 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3406 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3407 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003408 }
3409
Willy Tarreau13278b42017-10-13 19:23:14 +02003410 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3411 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003412 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003413 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3414 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003415 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003416 }
3417
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003418 if (flen < 5) {
3419 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3420 goto fail;
3421 }
3422
Willy Tarreau13278b42017-10-13 19:23:14 +02003423 hdrs += 5; // stream dep = 4, weight = 1
3424 flen -= 5;
3425 }
3426
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003427 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003428 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003429 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003430 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003431
Willy Tarreau937f7602018-02-26 15:22:17 +01003432 /* we can't retry a failed decompression operation so we must be very
3433 * careful not to take any risks. In practice the output buffer is
3434 * always empty except maybe for trailers, in which case we simply have
3435 * to wait for the upper layer to finish consuming what is available.
3436 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003437
3438 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003439 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003440 if (!htx_is_empty(htx)) {
3441 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003442 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003443 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003444 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003445 if (b_data(rxbuf)) {
3446 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003447 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003448 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003449
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003450 rxbuf->head = 0;
3451 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003452 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003453
Willy Tarreau25919232019-01-03 14:48:18 +01003454 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003455 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3456 sizeof(list)/sizeof(list[0]), tmp);
3457 if (outlen < 0) {
3458 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3459 goto fail;
3460 }
3461
Willy Tarreau25919232019-01-03 14:48:18 +01003462 /* The PACK decompressor was updated, let's update the input buffer and
3463 * the parser's state to commit these changes and allow us to later
3464 * fail solely on the stream if needed.
3465 */
3466 b_del(&h2c->dbuf, h2c->dfl + hole);
3467 h2c->dfl = hole = 0;
3468 h2c->st0 = H2_CS_FRAME_H;
3469
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003470 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003471 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003472
Willy Tarreau88d138e2019-01-02 19:38:14 +01003473 if (*flags & H2_SF_HEADERS_RCVD)
3474 goto trailers;
3475
3476 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003477 if (htx) {
3478 /* HTX mode */
3479 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003480 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003481 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003482 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003483 } else {
3484 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003485 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003486 if (outlen > 0)
3487 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003488 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003489
3490 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003491 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003492 goto fail;
3493 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003494
Willy Tarreau174b06a2018-04-25 18:13:58 +02003495 if (msgf & H2_MSGF_BODY) {
3496 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003497 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003498 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003499 if (htx)
3500 htx->extra = *body_len;
3501 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003502 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003503 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003504 }
3505
Willy Tarreau88d138e2019-01-02 19:38:14 +01003506 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003507 /* indicate that a HEADERS frame was received for this stream, except
3508 * for 1xx responses. For 1xx responses, another HEADERS frame is
3509 * expected.
3510 */
3511 if (!(msgf & H2_MSGF_RSP_1XX))
3512 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003513
Christopher Faulet0b465482019-02-19 15:14:23 +01003514 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003515 /* Mark the end of message, either using EOM in HTX or with the
3516 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3517 * is not set during headers with END_STREAM.
3518 */
3519 if (htx) {
3520 if (!htx_add_endof(htx, HTX_BLK_EOM))
3521 goto fail;
3522 }
3523 else if (*flags & H2_SF_DATA_CHNK) {
3524 if (!b_putblk(rxbuf, "\r\n", 2))
3525 goto fail;
3526 }
3527 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003528
Willy Tarreau86277d42019-01-02 15:36:11 +01003529 /* success */
3530 ret = 1;
3531
Willy Tarreau68dd9852017-07-03 14:44:26 +02003532 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003533 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003534 * move the remaining data over it.
3535 */
3536 if (hole) {
3537 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3538 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3539 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3540 b_sub(&h2c->dbuf, hole);
3541 }
3542
3543 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3544 /* too large frames */
3545 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003546 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003547 }
3548
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003549 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003550 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003551 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003552 return ret;
3553
Willy Tarreau68dd9852017-07-03 14:44:26 +02003554 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003555 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003556 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003557
3558 trailers:
3559 /* This is the last HEADERS frame hence a trailer */
3560
3561 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3562 /* It's a trailer but it's missing ES flag */
3563 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3564 goto fail;
3565 }
3566
3567 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3568 * block, and when using chunks we must send the 0 CRLF marker. For
3569 * other modes, the trailers are silently dropped.
3570 */
3571 if (htx) {
3572 if (!htx_add_endof(htx, HTX_BLK_EOD))
3573 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003574 if (h2_make_htx_trailers(list, htx) <= 0)
3575 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003576 }
3577 else if (*flags & H2_SF_DATA_CHNK) {
3578 /* Legacy mode with chunked encoding : we must finalize the
3579 * data block message emit the trailing CRLF */
3580 if (!b_putblk(rxbuf, "0\r\n", 3))
3581 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003582
3583 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3584 if (outlen > 0)
3585 b_add(rxbuf, outlen);
3586 else
3587 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003588 }
3589
3590 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003591}
3592
Willy Tarreau454f9052017-10-26 19:40:35 +02003593/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3594 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3595 * in use, a new chunk is emitted for each frame. This is supposed to fit
3596 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3597 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3598 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003599 * parser state is automatically updated. Returns > 0 if it could completely
3600 * send the current frame, 0 if it couldn't complete, in which case
3601 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3602 * DATA frame can return 0 as a valid result). Stream errors are reported in
3603 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3604 * have checked the frame header and ensured that the frame was complete or the
3605 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003606 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003607static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003608{
3609 struct h2c *h2c = h2s->h2c;
3610 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003611 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003612 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003613 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003614 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003615
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003616 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003617
Olivier Houchard638b7992018-08-16 15:41:52 +02003618 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003619 if (!csbuf) {
3620 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003621 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003622 }
3623
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003624try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003625 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003626 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3627 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003628 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003629 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003630
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003631 if (flen > b_data(&h2c->dbuf)) {
3632 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003633 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003634 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003635 }
3636
Willy Tarreaua9b77962019-01-31 07:23:00 +01003637 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003638 block1 = htx_free_data_space(htx);
3639 if (!block1) {
3640 h2c->flags |= H2_CF_DEM_SFULL;
3641 goto fail;
3642 }
3643 if (flen > block1)
3644 flen = block1;
3645
3646 /* here, flen is the max we can copy into the output buffer */
3647 block1 = b_contig_data(&h2c->dbuf, 0);
3648 if (flen > block1)
3649 flen = block1;
3650
3651 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3652 h2c->flags |= H2_CF_DEM_SFULL;
3653 goto fail;
3654 }
3655
3656 b_del(&h2c->dbuf, flen);
3657 h2c->dfl -= flen;
3658 h2c->rcvd_c += flen;
3659 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003660
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003661 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003662 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003663 htx->extra = h2s->body_len;
3664 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003665 goto try_again;
3666 }
3667 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003668 /* it doesn't fit and the buffer is fragmented,
3669 * so let's defragment it and try again.
3670 */
3671 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003672 }
3673
Willy Tarreaueba10f22018-04-25 20:44:22 +02003674 /* chunked-encoding requires more room */
3675 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003676 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003677 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3678 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3679 (chklen < 1048576) ? 4 : 8;
3680 chklen += 4; // CRLF, CRLF
3681 }
3682
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003683 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 if (flen + chklen > b_room(csbuf)) {
3685 if (chklen >= b_room(csbuf)) {
3686 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003687 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003688 }
3689 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003690 }
3691
3692 if (h2s->flags & H2_SF_DATA_CHNK) {
3693 /* emit the chunk size */
3694 unsigned int chksz = flen;
3695 char str[10];
3696 char *beg;
3697
3698 beg = str + sizeof(str);
3699 *--beg = '\n';
3700 *--beg = '\r';
3701 do {
3702 *--beg = hextab[chksz & 0xF];
3703 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003704 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003705 }
3706
Willy Tarreau454f9052017-10-26 19:40:35 +02003707 /* Block1 is the length of the first block before the buffer wraps,
3708 * block2 is the optional second block to reach the end of the frame.
3709 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003710 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003711 if (block1 > flen)
3712 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003713 block2 = flen - block1;
3714
3715 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003716 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003717
3718 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003719 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003720
Willy Tarreaueba10f22018-04-25 20:44:22 +02003721 if (h2s->flags & H2_SF_DATA_CHNK) {
3722 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003723 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003724 }
3725
Willy Tarreau454f9052017-10-26 19:40:35 +02003726 /* now mark the input data as consumed (will be deleted from the buffer
3727 * by the caller when seeing FRAME_A after sending the window update).
3728 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003729 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003730 h2c->dfl -= flen;
3731 h2c->rcvd_c += flen;
3732 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3733
Willy Tarreau1915ca22019-01-24 11:49:37 +01003734 if (h2s->flags & H2_SF_DATA_CLEN)
3735 h2s->body_len -= flen;
3736
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003737 if (h2c->dfl > h2c->dpl) {
3738 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003739 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003740 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003741 }
3742
Willy Tarreau4a28da12018-01-04 14:41:00 +01003743 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003744 /* here we're done with the frame, all the payload (except padding) was
3745 * transferred.
3746 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003747
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003748 if (h2c->dff & H2_F_DATA_END_STREAM) {
3749 if (htx) {
3750 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3751 h2c->flags |= H2_CF_DEM_SFULL;
3752 goto fail;
3753 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003754 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003755 else if (h2s->flags & H2_SF_DATA_CHNK) {
3756 /* emit the trailing 0 CRLF CRLF */
3757 if (b_room(csbuf) < 5) {
3758 h2c->flags |= H2_CF_DEM_SFULL;
3759 goto fail;
3760 }
3761 chklen += 5;
3762 b_putblk(csbuf, "0\r\n\r\n", 5);
3763 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003764 }
3765
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003766 h2c->rcvd_c += h2c->dpl;
3767 h2c->rcvd_s += h2c->dpl;
3768 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003769 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003770 if (htx)
3771 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003772 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003773 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003774 if (htx)
3775 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003776 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003777}
3778
Willy Tarreau5dd17352018-06-14 13:33:30 +02003779/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3780 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3781 * number of bytes sent. The caller must check the stream's status to detect
3782 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003783 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003784static 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 +02003785{
3786 struct http_hdr list[MAX_HTTP_HDR];
3787 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003788 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003789 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003790 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003791 int es_now = 0;
3792 int ret = 0;
3793 int hdr;
3794
3795 if (h2c_mux_busy(h2c, h2s)) {
3796 h2s->flags |= H2_SF_BLK_MBUSY;
3797 return 0;
3798 }
3799
Willy Tarreau44e973f2018-03-01 17:49:30 +01003800 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003801 h2c->flags |= H2_CF_MUX_MALLOC;
3802 h2s->flags |= H2_SF_BLK_MROOM;
3803 return 0;
3804 }
3805
3806 /* First, try to parse the H1 response and index it into <list>.
3807 * NOTE! Since it comes from haproxy, we *know* that a response header
3808 * block does not wrap and we can safely read it this way without
3809 * having to realign the buffer.
3810 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003811 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003812 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003813 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003814 /* incomplete or invalid response, this is abnormal coming from
3815 * haproxy and may only result in a bad errorfile or bad Lua code
3816 * so that won't be fixed, raise an error now.
3817 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003818 * FIXME: we should instead add the ability to only return a
3819 * 502 bad gateway. But in theory this is not supposed to
3820 * happen.
3821 */
3822 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3823 ret = 0;
3824 goto end;
3825 }
3826
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003827 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003828
3829 /* certain statuses have no body or an empty one, regardless of
3830 * what the headers say.
3831 */
3832 if (sl.st.status >= 100 && sl.st.status < 200) {
3833 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3834 h1m->curr_len = h1m->body_len = 0;
3835 }
3836 else if (sl.st.status == 204 || sl.st.status == 304) {
3837 /* no contents, claim c-len is present and set to zero */
3838 h1m->flags &= ~H1_MF_CHNK;
3839 h1m->flags |= H1_MF_CLEN;
3840 h1m->curr_len = h1m->body_len = 0;
3841 }
3842
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003843 chunk_reset(&outbuf);
3844
3845 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003847 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003848 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003850 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003851 break;
3852 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003853 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003854 }
3855
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003856 if (outbuf.size < 9)
3857 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003858
3859 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003860 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3861 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3862 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003863
3864 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003865 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003866 /* this is an unparsable response */
3867 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3868 ret = 0;
3869 goto end;
3870 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003871
3872 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003873 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003874 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003875 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003876 }
3877
3878 /* encode all headers, stop at empty name */
3879 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003880 /* these ones do not exist in H2 and must be dropped. */
3881 if (isteq(list[hdr].n, ist("connection")) ||
3882 isteq(list[hdr].n, ist("proxy-connection")) ||
3883 isteq(list[hdr].n, ist("keep-alive")) ||
3884 isteq(list[hdr].n, ist("upgrade")) ||
3885 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003886 continue;
3887
3888 if (isteq(list[hdr].n, ist("")))
3889 break; // end
3890
3891 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3892 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003893 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003894 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003895 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003896 }
3897 }
3898
3899 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003900 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003901 es_now = 1;
3902
3903 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003904 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003905
3906 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003907 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003908
3909 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003910 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003911
3912 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003913 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003914 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003916 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003917 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003918 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003919
Willy Tarreau801250e2018-09-11 11:45:04 +02003920 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003921 h2s->flags |= H2_SF_ES_SENT;
3922 if (h2s->st == H2_SS_OPEN)
3923 h2s->st = H2_SS_HLOC;
3924 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003925 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003926 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003927 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003928 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003929 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003930 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003931 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003932 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003933 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003934
3935 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003936
3937 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003938 //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003939 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003940 full:
3941 h1m_init_res(h1m);
3942 h1m->err_pos = -1; // don't care about errors on the response path
3943 h2c->flags |= H2_CF_MUX_MFULL;
3944 h2s->flags |= H2_SF_BLK_MROOM;
3945 ret = 0;
3946 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003947}
3948
Willy Tarreau5dd17352018-06-14 13:33:30 +02003949/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3950 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3951 * the number of bytes sent. The caller must check the stream's status to
3952 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003953 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003954static 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 +02003955{
3956 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003957 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003958 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003959 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003960 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003961 int es_now = 0;
3962 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003963 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003964 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003965
3966 if (h2c_mux_busy(h2c, h2s)) {
3967 h2s->flags |= H2_SF_BLK_MBUSY;
3968 goto end;
3969 }
3970
Willy Tarreau44e973f2018-03-01 17:49:30 +01003971 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003972 h2c->flags |= H2_CF_MUX_MALLOC;
3973 h2s->flags |= H2_SF_BLK_MROOM;
3974 goto end;
3975 }
3976
3977 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003978 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003979 goto end;
3980
3981 chunk_reset(&outbuf);
3982
3983 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003984 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003985 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003986 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003987
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003988 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003989 break;
3990 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003991 /* If there are pending data in the output buffer, and we have
3992 * less than 1/4 of the mbuf's size and everything fits, we'll
3993 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3994 * is full and wait, to save some slow realign calls.
3995 */
3996 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3997 h2c->flags |= H2_CF_MUX_MFULL;
3998 h2s->flags |= H2_SF_BLK_MROOM;
3999 goto end;
4000 }
4001
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004002 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004003 }
4004
4005 if (outbuf.size < 9) {
4006 h2c->flags |= H2_CF_MUX_MFULL;
4007 h2s->flags |= H2_SF_BLK_MROOM;
4008 goto end;
4009 }
4010
4011 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004012 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4013 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4014 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004015
4016 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4017 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004018 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004019 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004020 break;
4021 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004022 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004023 if ((long long)size > h1m->curr_len)
4024 size = h1m->curr_len;
4025 break;
4026 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004027 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004028 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 if (!ret)
4030 goto end;
4031
4032 if (ret < 0) {
4033 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004034 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004035 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4036 goto end;
4037 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004038 max -= ret;
4039 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004040 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004041 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004042 }
4043
Willy Tarreau801250e2018-09-11 11:45:04 +02004044 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004045 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004046 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004047 if (!ret)
4048 goto end;
4049
4050 if (ret < 0) {
4051 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004052 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004053 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4054 goto end;
4055 }
4056
4057 size = chunk;
4058 h1m->curr_len = chunk;
4059 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004060 max -= ret;
4061 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004062 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004063 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004064 if (!size)
4065 goto send_empty;
4066 }
4067
4068 /* in MSG_DATA state, continue below */
4069 size = h1m->curr_len;
4070 break;
4071 }
4072
4073 /* we have in <size> the exact number of bytes we need to copy from
4074 * the H1 buffer. We need to check this against the connection's and
4075 * the stream's send windows, and to ensure that this fits in the max
4076 * frame size and in the buffer's available space minus 9 bytes (for
4077 * the frame header). The connection's flow control is applied last so
4078 * that we can use a separate list of streams which are immediately
4079 * unblocked on window opening. Note: we don't implement padding.
4080 */
4081
Willy Tarreau5dd17352018-06-14 13:33:30 +02004082 if (size > max)
4083 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004084
4085 if (size > h2s->mws)
4086 size = h2s->mws;
4087
4088 if (size <= 0) {
4089 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004090 if (h2s->send_wait) {
4091 LIST_DEL(&h2s->list);
4092 LIST_INIT(&h2s->list);
4093 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004094 goto end;
4095 }
4096
4097 if (h2c->mfs && size > h2c->mfs)
4098 size = h2c->mfs;
4099
4100 if (size + 9 > outbuf.size) {
4101 /* we have an opportunity for enlarging the too small
4102 * available space, let's try.
4103 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004104 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004105 goto realign_again;
4106 size = outbuf.size - 9;
4107 }
4108
4109 if (size <= 0) {
4110 h2c->flags |= H2_CF_MUX_MFULL;
4111 h2s->flags |= H2_SF_BLK_MROOM;
4112 goto end;
4113 }
4114
4115 if (size > h2c->mws)
4116 size = h2c->mws;
4117
4118 if (size <= 0) {
4119 h2s->flags |= H2_SF_BLK_MFCTL;
4120 goto end;
4121 }
4122
4123 /* copy whatever we can */
4124 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004125 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004126 if (ret == 1)
4127 len2 = 0;
4128
4129 if (!ret || len1 + len2 < size) {
4130 /* FIXME: must normally never happen */
4131 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4132 goto end;
4133 }
4134
4135 /* limit len1/len2 to size */
4136 if (len1 + len2 > size) {
4137 int sub = len1 + len2 - size;
4138
4139 if (len2 > sub)
4140 len2 -= sub;
4141 else {
4142 sub -= len2;
4143 len2 = 0;
4144 len1 -= sub;
4145 }
4146 }
4147
4148 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004149 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004150 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004151 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004152
4153 send_empty:
4154 /* we may need to add END_STREAM */
4155 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4156 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004157 *
4158 * FIXME: what we do here is not correct because we send end_stream
4159 * before knowing if we'll have to send a HEADERS frame for the
4160 * trailers. More importantly we're not consuming the trailing CRLF
4161 * after the end of trailers, so it will be left to the caller to
4162 * eat it. The right way to do it would be to measure trailers here
4163 * and to send ES only if there are no trailers.
4164 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004165 */
4166 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004167 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004168 es_now = 1;
4169
4170 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004171 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004172
4173 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004174 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004175
4176 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004177 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004178
4179 /* consume incoming H1 response */
4180 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004181 max -= size;
4182 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004183 total += size;
4184 h1m->curr_len -= size;
4185 h2s->mws -= size;
4186 h2c->mws -= size;
4187
4188 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004189 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004190 goto new_frame;
4191 }
4192 }
4193
4194 if (es_now) {
4195 if (h2s->st == H2_SS_OPEN)
4196 h2s->st = H2_SS_HLOC;
4197 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004198 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004199
Willy Tarreau35a62702018-02-27 15:37:25 +01004200 if (!(h1m->flags & H1_MF_CHNK)) {
4201 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004202 total += max;
4203 ofs += max;
4204 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004205
Willy Tarreau801250e2018-09-11 11:45:04 +02004206 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004207 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004208
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004209 h2s->flags |= H2_SF_ES_SENT;
4210 }
4211
4212 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004213 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004214 return total;
4215}
4216
Willy Tarreau115e83b2018-12-01 19:17:53 +01004217/* Try to send a HEADERS frame matching HTX response present in HTX message
4218 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4219 * must check the stream's status to detect any error which might have happened
4220 * subsequently to a successful send. The htx blocks are automatically removed
4221 * from the message. The htx message is assumed to be valid since produced from
4222 * the internal code, hence it contains a start line, an optional series of
4223 * header blocks and an end of header, otherwise an invalid frame could be
4224 * emitted and the resulting htx message could be left in an inconsistent state.
4225 */
4226static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4227{
4228 struct http_hdr list[MAX_HTTP_HDR];
4229 struct h2c *h2c = h2s->h2c;
4230 struct htx_blk *blk;
4231 struct htx_blk *blk_end;
4232 struct buffer outbuf;
4233 struct htx_sl *sl;
4234 enum htx_blk_type type;
4235 int es_now = 0;
4236 int ret = 0;
4237 int hdr;
4238 int idx;
4239
4240 if (h2c_mux_busy(h2c, h2s)) {
4241 h2s->flags |= H2_SF_BLK_MBUSY;
4242 return 0;
4243 }
4244
4245 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4246 h2c->flags |= H2_CF_MUX_MALLOC;
4247 h2s->flags |= H2_SF_BLK_MROOM;
4248 return 0;
4249 }
4250
4251 /* determine the first block which must not be deleted, blk_end may
4252 * be NULL if all blocks have to be deleted.
4253 */
4254 idx = htx_get_head(htx);
4255 blk_end = NULL;
4256 while (idx != -1) {
4257 type = htx_get_blk_type(htx_get_blk(htx, idx));
4258 idx = htx_get_next(htx, idx);
4259 if (type == HTX_BLK_EOH) {
4260 if (idx != -1)
4261 blk_end = htx_get_blk(htx, idx);
4262 break;
4263 }
4264 }
4265
4266 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004267 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004268 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004269 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004270 if (h2s->status < 100 || h2s->status > 999)
4271 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004272
4273 /* and the rest of the headers, that we dump starting at header 0 */
4274 hdr = 0;
4275
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004276 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004277 while ((idx = htx_get_next(htx, idx)) != -1) {
4278 blk = htx_get_blk(htx, idx);
4279 type = htx_get_blk_type(blk);
4280
4281 if (type == HTX_BLK_UNUSED)
4282 continue;
4283
4284 if (type != HTX_BLK_HDR)
4285 break;
4286
4287 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4288 goto fail;
4289
4290 list[hdr].n = htx_get_blk_name(htx, blk);
4291 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004292 hdr++;
4293 }
4294
4295 /* marker for end of headers */
4296 list[hdr].n = ist("");
4297
4298 if (h2s->status == 204 || h2s->status == 304) {
4299 /* no contents, claim c-len is present and set to zero */
4300 es_now = 1;
4301 }
4302
4303 chunk_reset(&outbuf);
4304
4305 while (1) {
4306 outbuf.area = b_tail(&h2c->mbuf);
4307 outbuf.size = b_contig_space(&h2c->mbuf);
4308 outbuf.data = 0;
4309
4310 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4311 break;
4312 realign_again:
4313 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4314 }
4315
4316 if (outbuf.size < 9)
4317 goto full;
4318
4319 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4320 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4321 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4322 outbuf.data = 9;
4323
4324 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004325 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004326 if (b_space_wraps(&h2c->mbuf))
4327 goto realign_again;
4328 goto full;
4329 }
4330
4331 /* encode all headers, stop at empty name */
4332 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4333 /* these ones do not exist in H2 and must be dropped. */
4334 if (isteq(list[hdr].n, ist("connection")) ||
4335 isteq(list[hdr].n, ist("proxy-connection")) ||
4336 isteq(list[hdr].n, ist("keep-alive")) ||
4337 isteq(list[hdr].n, ist("upgrade")) ||
4338 isteq(list[hdr].n, ist("transfer-encoding")))
4339 continue;
4340
4341 if (isteq(list[hdr].n, ist("")))
4342 break; // end
4343
4344 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4345 /* output full */
4346 if (b_space_wraps(&h2c->mbuf))
4347 goto realign_again;
4348 goto full;
4349 }
4350 }
4351
Christopher Faulet0b465482019-02-19 15:14:23 +01004352 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004353 * FIXME: we should also set it when we know for sure that the
4354 * content-length is zero as well as on 204/304
4355 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004356 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4357 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004358 es_now = 1;
4359
Willy Tarreau927b88b2019-03-04 08:03:25 +01004360 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004361 es_now = 1;
4362
4363 /* update the frame's size */
4364 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4365
4366 if (es_now)
4367 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4368
4369 /* commit the H2 response */
4370 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004371
4372 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4373 * 1xx responses, another HEADERS frame is expected.
4374 */
4375 if (h2s->status >= 200 || h2s->status == 101)
4376 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004377
Willy Tarreau115e83b2018-12-01 19:17:53 +01004378 if (es_now) {
4379 h2s->flags |= H2_SF_ES_SENT;
4380 if (h2s->st == H2_SS_OPEN)
4381 h2s->st = H2_SS_HLOC;
4382 else
4383 h2s_close(h2s);
4384 }
4385
4386 /* OK we could properly deliver the response */
4387
4388 /* remove all header blocks including the EOH and compute the
4389 * corresponding size.
4390 *
4391 * FIXME: We should remove everything when es_now is set.
4392 */
4393 ret = 0;
4394 idx = htx_get_head(htx);
4395 blk = htx_get_blk(htx, idx);
4396 while (blk != blk_end) {
4397 ret += htx_get_blksz(blk);
4398 blk = htx_remove_blk(htx, blk);
4399 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004400
4401 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4402 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004403 end:
4404 return ret;
4405 full:
4406 h2c->flags |= H2_CF_MUX_MFULL;
4407 h2s->flags |= H2_SF_BLK_MROOM;
4408 ret = 0;
4409 goto end;
4410 fail:
4411 /* unparsable HTX messages, too large ones to be produced in the local
4412 * list etc go here (unrecoverable errors).
4413 */
4414 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4415 ret = 0;
4416 goto end;
4417}
4418
Willy Tarreau80739692018-10-05 11:35:57 +02004419/* Try to send a HEADERS frame matching HTX request present in HTX message
4420 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4421 * must check the stream's status to detect any error which might have happened
4422 * subsequently to a successful send. The htx blocks are automatically removed
4423 * from the message. The htx message is assumed to be valid since produced from
4424 * the internal code, hence it contains a start line, an optional series of
4425 * header blocks and an end of header, otherwise an invalid frame could be
4426 * emitted and the resulting htx message could be left in an inconsistent state.
4427 */
4428static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4429{
4430 struct http_hdr list[MAX_HTTP_HDR];
4431 struct h2c *h2c = h2s->h2c;
4432 struct htx_blk *blk;
4433 struct htx_blk *blk_end;
4434 struct buffer outbuf;
4435 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004436 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004437 enum htx_blk_type type;
4438 int es_now = 0;
4439 int ret = 0;
4440 int hdr;
4441 int idx;
4442
4443 if (h2c_mux_busy(h2c, h2s)) {
4444 h2s->flags |= H2_SF_BLK_MBUSY;
4445 return 0;
4446 }
4447
4448 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4449 h2c->flags |= H2_CF_MUX_MALLOC;
4450 h2s->flags |= H2_SF_BLK_MROOM;
4451 return 0;
4452 }
4453
4454 /* determine the first block which must not be deleted, blk_end may
4455 * be NULL if all blocks have to be deleted.
4456 */
4457 idx = htx_get_head(htx);
4458 blk_end = NULL;
4459 while (idx != -1) {
4460 type = htx_get_blk_type(htx_get_blk(htx, idx));
4461 idx = htx_get_next(htx, idx);
4462 if (type == HTX_BLK_EOH) {
4463 if (idx != -1)
4464 blk_end = htx_get_blk(htx, idx);
4465 break;
4466 }
4467 }
4468
4469 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004470 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004471 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004472 meth = htx_sl_req_meth(sl);
4473 path = htx_sl_req_uri(sl);
4474
4475 /* and the rest of the headers, that we dump starting at header 0 */
4476 hdr = 0;
4477
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004478 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004479 while ((idx = htx_get_next(htx, idx)) != -1) {
4480 blk = htx_get_blk(htx, idx);
4481 type = htx_get_blk_type(blk);
4482
4483 if (type == HTX_BLK_UNUSED)
4484 continue;
4485
4486 if (type != HTX_BLK_HDR)
4487 break;
4488
4489 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4490 goto fail;
4491
4492 list[hdr].n = htx_get_blk_name(htx, blk);
4493 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004494 hdr++;
4495 }
4496
4497 /* marker for end of headers */
4498 list[hdr].n = ist("");
4499
4500 chunk_reset(&outbuf);
4501
4502 while (1) {
4503 outbuf.area = b_tail(&h2c->mbuf);
4504 outbuf.size = b_contig_space(&h2c->mbuf);
4505 outbuf.data = 0;
4506
4507 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4508 break;
4509 realign_again:
4510 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4511 }
4512
4513 if (outbuf.size < 9)
4514 goto full;
4515
4516 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4517 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4518 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4519 outbuf.data = 9;
4520
4521 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004522 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004523 if (b_space_wraps(&h2c->mbuf))
4524 goto realign_again;
4525 goto full;
4526 }
4527
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004528 /* RFC7540 #8.3: the CONNECT method must have :
4529 * - :authority set to the URI part (host:port)
4530 * - :method set to CONNECT
4531 * - :scheme and :path omitted
4532 */
4533 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4534 /* encode the scheme which is always "https" (or 0x86 for "http") */
4535 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4536 /* output full */
4537 if (b_space_wraps(&h2c->mbuf))
4538 goto realign_again;
4539 goto full;
4540 }
Willy Tarreau80739692018-10-05 11:35:57 +02004541
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004542 /* encode the path, which necessarily is the second one */
4543 if (!hpack_encode_path(&outbuf, path)) {
4544 /* output full */
4545 if (b_space_wraps(&h2c->mbuf))
4546 goto realign_again;
4547 goto full;
4548 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004549
4550 /* look for the Host header and place it in :authority */
4551 auth = ist2(NULL, 0);
4552 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4553 if (isteq(list[hdr].n, ist("")))
4554 break; // end
4555
4556 if (isteq(list[hdr].n, ist("host"))) {
4557 auth = list[hdr].v;
4558 break;
4559 }
4560 }
4561 }
4562 else {
4563 /* for CONNECT, :authority is taken from the path */
4564 auth = path;
4565 }
4566
4567 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4568 /* output full */
4569 if (b_space_wraps(&h2c->mbuf))
4570 goto realign_again;
4571 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004572 }
4573
4574 /* encode all headers, stop at empty name */
4575 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4576 /* these ones do not exist in H2 and must be dropped. */
4577 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004578 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004579 isteq(list[hdr].n, ist("proxy-connection")) ||
4580 isteq(list[hdr].n, ist("keep-alive")) ||
4581 isteq(list[hdr].n, ist("upgrade")) ||
4582 isteq(list[hdr].n, ist("transfer-encoding")))
4583 continue;
4584
4585 if (isteq(list[hdr].n, ist("")))
4586 break; // end
4587
4588 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4589 /* output full */
4590 if (b_space_wraps(&h2c->mbuf))
4591 goto realign_again;
4592 goto full;
4593 }
4594 }
4595
4596 /* we may need to add END_STREAM if we have no body :
4597 * - request already closed, or :
4598 * - no transfer-encoding, and :
4599 * - no content-length or content-length:0
4600 * Fixme: this doesn't take into account CONNECT requests.
4601 */
4602 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4603 es_now = 1;
4604
4605 if (sl->flags & HTX_SL_F_BODYLESS)
4606 es_now = 1;
4607
Willy Tarreau927b88b2019-03-04 08:03:25 +01004608 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004609 es_now = 1;
4610
4611 /* update the frame's size */
4612 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4613
4614 if (es_now)
4615 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4616
4617 /* commit the H2 response */
4618 b_add(&h2c->mbuf, outbuf.data);
4619 h2s->flags |= H2_SF_HEADERS_SENT;
4620 h2s->st = H2_SS_OPEN;
4621
Willy Tarreau80739692018-10-05 11:35:57 +02004622 if (es_now) {
4623 // trim any possibly pending data (eg: inconsistent content-length)
4624 h2s->flags |= H2_SF_ES_SENT;
4625 h2s->st = H2_SS_HLOC;
4626 }
4627
4628 /* remove all header blocks including the EOH and compute the
4629 * corresponding size.
4630 *
4631 * FIXME: We should remove everything when es_now is set.
4632 */
4633 ret = 0;
4634 idx = htx_get_head(htx);
4635 blk = htx_get_blk(htx, idx);
4636 while (blk != blk_end) {
4637 ret += htx_get_blksz(blk);
4638 blk = htx_remove_blk(htx, blk);
4639 }
4640
4641 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4642 htx_remove_blk(htx, blk_end);
4643
4644 end:
4645 return ret;
4646 full:
4647 h2c->flags |= H2_CF_MUX_MFULL;
4648 h2s->flags |= H2_SF_BLK_MROOM;
4649 ret = 0;
4650 goto end;
4651 fail:
4652 /* unparsable HTX messages, too large ones to be produced in the local
4653 * list etc go here (unrecoverable errors).
4654 */
4655 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4656 ret = 0;
4657 goto end;
4658}
4659
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004660/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004661 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4662 * caller must check the stream's status to detect any error which might have
4663 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004664 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4665 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004666static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004667{
4668 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004669 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004670 struct buffer outbuf;
4671 size_t total = 0;
4672 int es_now = 0;
4673 int bsize; /* htx block size */
4674 int fsize; /* h2 frame size */
4675 struct htx_blk *blk;
4676 enum htx_blk_type type;
4677 int idx;
4678
4679 if (h2c_mux_busy(h2c, h2s)) {
4680 h2s->flags |= H2_SF_BLK_MBUSY;
4681 goto end;
4682 }
4683
4684 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4685 h2c->flags |= H2_CF_MUX_MALLOC;
4686 h2s->flags |= H2_SF_BLK_MROOM;
4687 goto end;
4688 }
4689
Willy Tarreau98de12a2018-12-12 07:03:00 +01004690 htx = htx_from_buf(buf);
4691
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004692 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4693 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4694 * the caller to handle.
4695 */
4696
4697 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004698 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004699 goto end;
4700
4701 idx = htx_get_head(htx);
4702 blk = htx_get_blk(htx, idx);
4703 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4704 bsize = htx_get_blksz(blk);
4705 fsize = bsize;
4706
4707 if (type == HTX_BLK_EOD) {
4708 /* if we have an EOD, we're dealing with chunked data. We may
4709 * have a set of trailers after us that the caller will want to
4710 * deal with. Let's simply remove the EOD and return.
4711 */
4712 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004713 total++; // EOD counts as one byte
4714 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004715 goto end;
4716 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004717 else if (type == HTX_BLK_EOM) {
4718 if (h2s->flags & H2_SF_ES_SENT) {
4719 /* ES already sent */
4720 htx_remove_blk(htx, blk);
4721 total++; // EOM counts as one byte
4722 count--;
4723 goto end;
4724 }
4725 }
4726 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004727 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004728
4729 /* Perform some optimizations to reduce the number of buffer copies.
4730 * First, if the mux's buffer is empty and the htx area contains
4731 * exactly one data block of the same size as the requested count, and
4732 * this count fits within the frame size, the stream's window size, and
4733 * the connection's window size, then it's possible to simply swap the
4734 * caller's buffer with the mux's output buffer and adjust offsets and
4735 * length to match the entire DATA HTX block in the middle. In this
4736 * case we perform a true zero-copy operation from end-to-end. This is
4737 * the situation that happens all the time with large files. Second, if
4738 * this is not possible, but the mux's output buffer is empty, we still
4739 * have an opportunity to avoid the copy to the intermediary buffer, by
4740 * making the intermediary buffer's area point to the output buffer's
4741 * area. In this case we want to skip the HTX header to make sure that
4742 * copies remain aligned and that this operation remains possible all
4743 * the time. This goes for headers, data blocks and any data extracted
4744 * from the HTX blocks.
4745 */
4746 if (unlikely(fsize == count &&
4747 htx->used == 1 && type == HTX_BLK_DATA &&
4748 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4749 void *old_area = h2c->mbuf.area;
4750
4751 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004752 /* Too bad there are data left there. We're willing to memcpy/memmove
4753 * up to 1/4 of the buffer, which means that it's OK to copy a large
4754 * frame into a buffer containing few data if it needs to be realigned,
4755 * and that it's also OK to copy few data without realigning. Otherwise
4756 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004757 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004758 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4759 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4760 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004761 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004762
Willy Tarreau98de12a2018-12-12 07:03:00 +01004763 h2c->flags |= H2_CF_MUX_MFULL;
4764 h2s->flags |= H2_SF_BLK_MROOM;
4765 goto end;
4766 }
4767
4768 /* map an H2 frame to the HTX block so that we can put the
4769 * frame header there.
4770 */
4771 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004772 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004773 h2c->mbuf.data = fsize + 9;
4774 outbuf.area = b_head(&h2c->mbuf);
4775
4776 /* prepend an H2 DATA frame header just before the DATA block */
4777 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4778 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4779 h2_set_frame_size(outbuf.area, fsize);
4780
4781 /* update windows */
4782 h2s->mws -= fsize;
4783 h2c->mws -= fsize;
4784
4785 /* and exchange with our old area */
4786 buf->area = old_area;
4787 buf->data = buf->head = 0;
4788 total += fsize;
4789 goto end;
4790 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004791
Willy Tarreau98de12a2018-12-12 07:03:00 +01004792 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004793 /* for DATA and EOM we'll have to emit a frame, even if empty */
4794
4795 while (1) {
4796 outbuf.area = b_tail(&h2c->mbuf);
4797 outbuf.size = b_contig_space(&h2c->mbuf);
4798 outbuf.data = 0;
4799
4800 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4801 break;
4802 realign_again:
4803 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4804 }
4805
4806 if (outbuf.size < 9) {
4807 h2c->flags |= H2_CF_MUX_MFULL;
4808 h2s->flags |= H2_SF_BLK_MROOM;
4809 goto end;
4810 }
4811
4812 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4813 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4814 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4815 outbuf.data = 9;
4816
4817 /* we have in <fsize> the exact number of bytes we need to copy from
4818 * the HTX buffer. We need to check this against the connection's and
4819 * the stream's send windows, and to ensure that this fits in the max
4820 * frame size and in the buffer's available space minus 9 bytes (for
4821 * the frame header). The connection's flow control is applied last so
4822 * that we can use a separate list of streams which are immediately
4823 * unblocked on window opening. Note: we don't implement padding.
4824 */
4825
4826 /* EOM is presented with bsize==1 but would lead to the emission of an
4827 * empty frame, thus we force it to zero here.
4828 */
4829 if (type == HTX_BLK_EOM)
4830 bsize = fsize = 0;
4831
4832 if (!fsize)
4833 goto send_empty;
4834
4835 if (h2s->mws <= 0) {
4836 h2s->flags |= H2_SF_BLK_SFCTL;
4837 if (h2s->send_wait) {
4838 LIST_DEL(&h2s->list);
4839 LIST_INIT(&h2s->list);
4840 }
4841 goto end;
4842 }
4843
Willy Tarreauee573762018-12-04 15:25:57 +01004844 if (fsize > count)
4845 fsize = count;
4846
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004847 if (fsize > h2s->mws)
4848 fsize = h2s->mws; // >0
4849
4850 if (h2c->mfs && fsize > h2c->mfs)
4851 fsize = h2c->mfs; // >0
4852
4853 if (fsize + 9 > outbuf.size) {
4854 /* we have an opportunity for enlarging the too small
4855 * available space, let's try.
4856 * FIXME: is this really interesting to do? Maybe we'll
4857 * spend lots of time realigning instead of using two
4858 * frames.
4859 */
4860 if (b_space_wraps(&h2c->mbuf))
4861 goto realign_again;
4862 fsize = outbuf.size - 9;
4863
4864 if (fsize <= 0) {
4865 /* no need to send an empty frame here */
4866 h2c->flags |= H2_CF_MUX_MFULL;
4867 h2s->flags |= H2_SF_BLK_MROOM;
4868 goto end;
4869 }
4870 }
4871
4872 if (h2c->mws <= 0) {
4873 h2s->flags |= H2_SF_BLK_MFCTL;
4874 goto end;
4875 }
4876
4877 if (fsize > h2c->mws)
4878 fsize = h2c->mws;
4879
4880 /* now let's copy this this into the output buffer */
4881 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004882 h2s->mws -= fsize;
4883 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004884 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004885
4886 send_empty:
4887 /* update the frame's size */
4888 h2_set_frame_size(outbuf.area, fsize);
4889
4890 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4891 * meeting EOM. We should optimize this later.
4892 */
4893 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004894 total++; // EOM counts as one byte
4895 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004896 es_now = 1;
4897 }
4898
4899 if (es_now)
4900 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4901
4902 /* commit the H2 response */
4903 b_add(&h2c->mbuf, fsize + 9);
4904
4905 /* consume incoming HTX block, including EOM */
4906 total += fsize;
4907 if (fsize == bsize) {
4908 htx_remove_blk(htx, blk);
4909 if (fsize)
4910 goto new_frame;
4911 } else {
4912 /* we've truncated this block */
4913 htx_cut_data_blk(htx, blk, fsize);
4914 }
4915
4916 if (es_now) {
4917 if (h2s->st == H2_SS_OPEN)
4918 h2s->st = H2_SS_HLOC;
4919 else
4920 h2s_close(h2s);
4921
4922 h2s->flags |= H2_SF_ES_SENT;
4923 }
4924
4925 end:
4926 return total;
4927}
4928
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004929/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4930 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4931 * processed. The caller must check the stream's status to detect any error
4932 * which might have happened subsequently to a successful send. The htx blocks
4933 * are automatically removed from the message. The htx message is assumed to be
4934 * valid since produced from the internal code. Processing stops when meeting
4935 * the EOM, which is also removed. All trailers are processed at once and sent
4936 * as a single frame. The ES flag is always set.
4937 */
4938static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4939{
4940 struct http_hdr list[MAX_HTTP_HDR];
4941 struct h2c *h2c = h2s->h2c;
4942 struct htx_blk *blk;
4943 struct htx_blk *blk_end;
4944 struct buffer outbuf;
4945 struct h1m h1m;
4946 enum htx_blk_type type;
4947 uint32_t size;
4948 int ret = 0;
4949 int hdr;
4950 int idx;
4951 void *start;
4952
4953 if (h2c_mux_busy(h2c, h2s)) {
4954 h2s->flags |= H2_SF_BLK_MBUSY;
4955 goto end;
4956 }
4957
4958 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4959 h2c->flags |= H2_CF_MUX_MALLOC;
4960 h2s->flags |= H2_SF_BLK_MROOM;
4961 goto end;
4962 }
4963
4964 /* The principle is that we parse each and every trailers block using
4965 * the H1 headers parser, and append it to the list. We don't proceed
4966 * until EOM is met. blk_end will point to the EOM block.
4967 */
4968 hdr = 0;
4969 memset(list, 0, sizeof(list));
4970 blk_end = NULL;
4971
4972 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4973 blk = htx_get_blk(htx, idx);
4974 type = htx_get_blk_type(blk);
4975
4976 if (type == HTX_BLK_UNUSED)
4977 continue;
4978
4979 if (type != HTX_BLK_TLR) {
4980 if (type == HTX_BLK_EOM)
4981 blk_end = blk;
4982 break;
4983 }
4984
4985 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4986 goto fail;
4987
4988 size = htx_get_blksz(blk);
4989 start = htx_get_blk_ptr(htx, blk);
4990
4991 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4992 h1m.err_pos = 0;
4993 ret = h1_headers_to_hdr_list(start, start + size,
4994 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4995 &h1m, NULL);
4996 if (ret < 0)
4997 goto fail;
4998
4999 /* ret == 0 if an incomplete trailers block was found (missing
5000 * empty line), or > 0 if it was found. We have to continue on
5001 * incomplete messages because the trailers block might be
5002 * incomplete.
5003 */
5004
5005 /* search the new end */
5006 while (hdr <= sizeof(list)/sizeof(list[0])) {
5007 if (!list[hdr].n.len)
5008 break;
5009 hdr++;
5010 }
5011 }
5012
5013 if (!blk_end)
5014 goto end; // end not found yet
5015
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005016 chunk_reset(&outbuf);
5017
5018 while (1) {
5019 outbuf.area = b_tail(&h2c->mbuf);
5020 outbuf.size = b_contig_space(&h2c->mbuf);
5021 outbuf.data = 0;
5022
5023 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5024 break;
5025 realign_again:
5026 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5027 }
5028
5029 if (outbuf.size < 9)
5030 goto full;
5031
5032 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5033 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5034 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5035 outbuf.data = 9;
5036
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005037 /* encode all headers */
5038 for (idx = 0; idx < hdr; idx++) {
5039 /* these ones do not exist in H2 or must not appear in
5040 * trailers and must be dropped.
5041 */
5042 if (isteq(list[idx].n, ist("host")) ||
5043 isteq(list[idx].n, ist("content-length")) ||
5044 isteq(list[idx].n, ist("connection")) ||
5045 isteq(list[idx].n, ist("proxy-connection")) ||
5046 isteq(list[idx].n, ist("keep-alive")) ||
5047 isteq(list[idx].n, ist("upgrade")) ||
5048 isteq(list[idx].n, ist("te")) ||
5049 isteq(list[idx].n, ist("transfer-encoding")))
5050 continue;
5051
5052 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5053 /* output full */
5054 if (b_space_wraps(&h2c->mbuf))
5055 goto realign_again;
5056 goto full;
5057 }
5058 }
5059
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005060 if (!hdr) {
5061 /* here we have a problem, we've received an empty trailers
5062 * block followed by an EOM. Because of this we can't send a
5063 * HEADERS frame, so we have to cheat and instead send an empty
5064 * DATA frame conveying the ES flag.
5065 */
5066 outbuf.area[3] = H2_FT_DATA;
5067 outbuf.area[4] = H2_F_DATA_END_STREAM;
5068 }
5069
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005070 /* update the frame's size */
5071 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5072
5073 /* commit the H2 response */
5074 b_add(&h2c->mbuf, outbuf.data);
5075 h2s->flags |= H2_SF_ES_SENT;
5076
5077 if (h2s->st == H2_SS_OPEN)
5078 h2s->st = H2_SS_HLOC;
5079 else
5080 h2s_close(h2s);
5081
5082 /* OK we could properly deliver the response */
5083 done:
5084 /* remove all header blocks including EOM and compute the corresponding size. */
5085 ret = 0;
5086 idx = htx_get_head(htx);
5087 blk = htx_get_blk(htx, idx);
5088 while (blk != blk_end) {
5089 ret += htx_get_blksz(blk);
5090 blk = htx_remove_blk(htx, blk);
5091 }
5092 blk = htx_remove_blk(htx, blk);
5093 end:
5094 return ret;
5095 full:
5096 h2c->flags |= H2_CF_MUX_MFULL;
5097 h2s->flags |= H2_SF_BLK_MROOM;
5098 ret = 0;
5099 goto end;
5100 fail:
5101 /* unparsable HTX messages, too large ones to be produced in the local
5102 * list etc go here (unrecoverable errors).
5103 */
5104 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5105 ret = 0;
5106 goto end;
5107}
5108
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005109/* Called from the upper layer, to subscribe to events, such as being able to send.
5110 * The <param> argument here is supposed to be a pointer to a wait_event struct
5111 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5112 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5113 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5114 * returns 0 except for the error above.
5115 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005116static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5117{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005118 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005119 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005120 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005121
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005122 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005123 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005124 if (!(sw->events & SUB_RETRY_RECV)) {
5125 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005126 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005127 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005128 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005129 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005130 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005131 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005132 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005133 if (!(sw->events & SUB_RETRY_SEND)) {
5134 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005135 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005136 h2s->send_wait = sw;
5137 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5138 if (h2s->flags & H2_SF_BLK_MFCTL)
5139 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5140 else
5141 LIST_ADDQ(&h2c->send_list, &h2s->list);
5142 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005143 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005144 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005145 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005146 if (event_type != 0)
5147 return -1;
5148 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005149}
5150
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005151/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5152 * The <param> argument here is supposed to be a pointer to the same wait_event
5153 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5154 * It always returns zero.
5155 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005156static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5157{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005158 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005159 struct h2s *h2s = cs->ctx;
5160
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005161 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005162 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005163 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005164 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005165 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005166 }
5167 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005168 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005169 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005170 if (h2s->send_wait == sw) {
5171 LIST_DEL(&h2s->list);
5172 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005173 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005174 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005175 }
5176 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005177 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5178 sw = param;
5179 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005180 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005181 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005182 LIST_DEL(&h2s->list);
5183 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005184 }
5185 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005186 return 0;
5187}
5188
5189
Olivier Houchard511efea2018-08-16 15:30:32 +02005190/* Called from the upper layer, to receive data */
5191static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5192{
Olivier Houchard638b7992018-08-16 15:41:52 +02005193 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005194 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005195 struct htx *h2s_htx = NULL;
5196 struct htx *buf_htx = NULL;
5197 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005198 size_t ret = 0;
5199
5200 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005201 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5202 /* in HTX mode we ignore the count argument */
5203 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005204 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005205 /* Here htx_to_buf() will set buffer data to 0 because
5206 * the HTX is empty.
5207 */
5208 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005209 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005210 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005211
5212 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005213 count = htx_free_data_space(buf_htx);
5214 if (flags & CO_RFL_KEEP_RSV) {
5215 if (count <= global.tune.maxrewrite)
5216 goto end;
5217 count -= global.tune.maxrewrite;
5218 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005219
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005220 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005221
5222 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5223 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5224
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005225 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005226 htx_to_buf(buf_htx, buf);
5227 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005228 ret = htx_ret.ret;
5229 }
5230 else {
5231 ret = b_xfer(buf, &h2s->rxbuf, count);
5232 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005233
Christopher Faulet37070b22019-02-14 15:12:14 +01005234 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005235 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005236 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005237 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005238 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005239 if (cs->flags & CS_FL_REOS)
5240 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005241 if (cs->flags & CS_FL_ERR_PENDING)
5242 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005243 if (b_size(&h2s->rxbuf)) {
5244 b_free(&h2s->rxbuf);
5245 offer_buffers(NULL, tasks_run_queue);
5246 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005247 }
5248
Willy Tarreau082f5592018-11-25 08:03:32 +01005249 if (ret && h2c->dsi == h2s->id) {
5250 /* demux is blocking on this stream's buffer */
5251 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005252 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005253 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005254
Olivier Houchard511efea2018-08-16 15:30:32 +02005255 return ret;
5256}
5257
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005258/* stops all senders of this connection for example when the mux buffer is full.
5259 * They are moved from the sending_list to either fctl_list or send_list.
5260 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005261static void h2_stop_senders(struct h2c *h2c)
5262{
5263 struct h2s *h2s, *h2s_back;
5264
Olivier Houchardd360ac62019-03-22 17:37:16 +01005265 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5266 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005267 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005268 h2s->send_wait->events |= SUB_RETRY_SEND;
5269 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005270 }
5271}
5272
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005273/* Called from the upper layer, to send data from buffer <buf> for no more than
5274 * <count> bytes. Returns the number of bytes effectively sent. Some status
5275 * flags may be updated on the conn_stream.
5276 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005277static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005278{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005279 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005280 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005281 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005282 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005283 struct htx *htx;
5284 struct htx_blk *blk;
5285 enum htx_blk_type btype;
5286 uint32_t bsize;
5287 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005288
Olivier Houchardd360ac62019-03-22 17:37:16 +01005289 /* If we were not just woken because we wanted to send but couldn't,
5290 * and there's somebody else that is waiting to send, do nothing,
5291 * we will subscribe later and be put at the end of the list
5292 */
5293 LIST_DEL_INIT(&h2s->sending_list);
5294 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5295 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5296 return 0;
5297
Olivier Houchardd846c262018-10-19 17:24:29 +02005298 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005299 /* We want to stay in the send_list, so prepare ourself to be
5300 * eventually recalled if needed, and only remove ourself from
5301 * the list if we managed to send anything.
5302 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005303 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005304 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005305 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005306 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5307 return 0;
5308
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005309 /* htx will be enough to decide if we're using HTX or legacy */
5310 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5311
Willy Tarreau0bad0432018-06-14 16:54:01 +02005312 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005313 h2s->flags |= H2_SF_OUTGOING_DATA;
5314
Willy Tarreau751f2d02018-10-05 09:35:00 +02005315 if (h2s->id == 0) {
5316 int32_t id = h2c_get_next_sid(h2s->h2c);
5317
5318 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005319 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005320 return 0;
5321 }
5322
5323 eb32_delete(&h2s->by_id);
5324 h2s->by_id.key = h2s->id = id;
5325 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005326 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005327 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5328 }
5329
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005330 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005331 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5332 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005333 idx = htx_get_head(htx);
5334 blk = htx_get_blk(htx, idx);
5335 btype = htx_get_blk_type(blk);
5336 bsize = htx_get_blksz(blk);
5337
5338 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005339 case HTX_BLK_REQ_SL:
5340 /* start-line before headers */
5341 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5342 if (ret > 0) {
5343 total += ret;
5344 count -= ret;
5345 if (ret < bsize)
5346 goto done;
5347 }
5348 break;
5349
Willy Tarreau115e83b2018-12-01 19:17:53 +01005350 case HTX_BLK_RES_SL:
5351 /* start-line before headers */
5352 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5353 if (ret > 0) {
5354 total += ret;
5355 count -= ret;
5356 if (ret < bsize)
5357 goto done;
5358 }
5359 break;
5360
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005361 case HTX_BLK_DATA:
5362 case HTX_BLK_EOD:
5363 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005364 /* all these cause the emission of a DATA frame (possibly empty).
5365 * This EOM necessarily is one before trailers, as the EOM following
5366 * trailers would have been consumed by the trailers parser.
5367 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005368 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005369 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005370 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005371 total += ret;
5372 count -= ret;
5373 if (ret < bsize)
5374 goto done;
5375 }
5376 break;
5377
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005378 case HTX_BLK_TLR:
5379 /* This is the first trailers block, all the subsequent ones AND
5380 * the EOM will be swallowed by the parser.
5381 */
5382 ret = h2s_htx_make_trailers(h2s, htx);
5383 if (ret > 0) {
5384 total += ret;
5385 count -= ret;
5386 if (ret < bsize)
5387 goto done;
5388 }
5389 break;
5390
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005391 default:
5392 htx_remove_blk(htx, blk);
5393 total += bsize;
5394 count -= bsize;
5395 break;
5396 }
5397 }
5398 goto done;
5399 }
5400
5401 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005402 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005403 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005404 if (h2s->h2c->flags & H2_CF_IS_BACK)
5405 ret = -1;
5406 else
5407 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005408 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005409 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005410 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005411 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005412 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005413 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005414 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005415
Willy Tarreau5dd17352018-06-14 13:33:30 +02005416 if (unlikely((int)ret <= 0)) {
5417 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005418 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5419 break;
5420 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005421 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005422 total += count;
5423 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005424 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005425 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005426 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005427 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005428 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005429 break;
5430 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005431
5432 total += ret;
5433 count -= ret;
5434
5435 if (h2s->st >= H2_SS_ERROR)
5436 break;
5437
5438 if (h2s->flags & H2_SF_BLK_ANY)
5439 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005440 }
5441
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005442 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005443 if (h2s->st >= H2_SS_ERROR) {
5444 /* trim any possibly pending data after we close (extra CR-LF,
5445 * unprocessed trailers, abnormal extra data, ...)
5446 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005447 total += count;
5448 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005449 }
5450
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005451 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005452 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005453 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005454 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005455 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005456 }
5457
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005458 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005459 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005460 } else {
5461 b_del(buf, total);
5462 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005463
5464 /* The mux is full, cancel the pending tasks */
5465 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5466 (h2s->flags & H2_SF_BLK_MBUSY))
5467 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005468
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005469 /* If we're running HTX, and we read the whole buffer, then pretend
5470 * we read exactly what the caller specified, as with HTX the caller
5471 * will always give the buffer size, instead of the amount of data
5472 * available.
5473 */
5474 if (htx && !b_data(buf))
5475 total = orig_count;
5476
Olivier Houchard7505f942018-08-21 18:10:44 +02005477 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005478 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005479 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005480
Olivier Houchard7505f942018-08-21 18:10:44 +02005481 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005482 /* If we're waiting for flow control, and we got a shutr on the
5483 * connection, we will never be unlocked, so add an error on
5484 * the conn_stream.
5485 */
5486 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5487 !b_data(&h2s->h2c->dbuf) &&
5488 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5489 if (cs->flags & CS_FL_EOS)
5490 cs->flags |= CS_FL_ERROR;
5491 else
5492 cs->flags |= CS_FL_ERR_PENDING;
5493 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005494 if (total > 0 && h2s->send_wait) {
5495 /* Ok we managed to send something, leave the send_list */
5496 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5497 h2s->send_wait = NULL;
5498 LIST_DEL_INIT(&h2s->list);
5499 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005500 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005501}
5502
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005503/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005504static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005505{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005506 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005507 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005508 struct eb32_node *node;
5509 int fctl_cnt = 0;
5510 int send_cnt = 0;
5511 int tree_cnt = 0;
5512 int orph_cnt = 0;
5513
5514 if (!h2c)
5515 return;
5516
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005517 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005518 fctl_cnt++;
5519
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005520 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005521 send_cnt++;
5522
Willy Tarreau3af37712018-12-18 14:34:41 +01005523 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005524 node = eb32_first(&h2c->streams_by_id);
5525 while (node) {
5526 h2s = container_of(node, struct h2s, by_id);
5527 tree_cnt++;
5528 if (!h2s->cs)
5529 orph_cnt++;
5530 node = eb32_next(node);
5531 }
5532
Willy Tarreau987c0632018-12-18 10:32:05 +01005533 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5534 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5535 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005536 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5537 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005538 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005539 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5540 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5541 h2c->msi,
5542 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5543 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5544
5545 if (h2s) {
5546 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5547 h2s, h2s->id, h2s->flags,
5548 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5549 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5550 h2s->cs);
5551 if (h2s->cs)
5552 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5553 h2s->cs->flags, h2s->cs->data);
5554 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005555}
Willy Tarreau62f52692017-10-08 23:01:42 +02005556
5557/*******************************************************/
5558/* functions below are dedicated to the config parsers */
5559/*******************************************************/
5560
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005561/* config parser for global "tune.h2.header-table-size" */
5562static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5563 struct proxy *defpx, const char *file, int line,
5564 char **err)
5565{
5566 if (too_many_args(1, args, err, NULL))
5567 return -1;
5568
5569 h2_settings_header_table_size = atoi(args[1]);
5570 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5571 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5572 return -1;
5573 }
5574 return 0;
5575}
Willy Tarreau62f52692017-10-08 23:01:42 +02005576
Willy Tarreaue6baec02017-07-27 11:45:11 +02005577/* config parser for global "tune.h2.initial-window-size" */
5578static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5579 struct proxy *defpx, const char *file, int line,
5580 char **err)
5581{
5582 if (too_many_args(1, args, err, NULL))
5583 return -1;
5584
5585 h2_settings_initial_window_size = atoi(args[1]);
5586 if (h2_settings_initial_window_size < 0) {
5587 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5588 return -1;
5589 }
5590 return 0;
5591}
5592
Willy Tarreau5242ef82017-07-27 11:47:28 +02005593/* config parser for global "tune.h2.max-concurrent-streams" */
5594static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5595 struct proxy *defpx, const char *file, int line,
5596 char **err)
5597{
5598 if (too_many_args(1, args, err, NULL))
5599 return -1;
5600
5601 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005602 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005603 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5604 return -1;
5605 }
5606 return 0;
5607}
5608
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005609/* config parser for global "tune.h2.max-frame-size" */
5610static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5611 struct proxy *defpx, const char *file, int line,
5612 char **err)
5613{
5614 if (too_many_args(1, args, err, NULL))
5615 return -1;
5616
5617 h2_settings_max_frame_size = atoi(args[1]);
5618 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5619 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5620 return -1;
5621 }
5622 return 0;
5623}
5624
Willy Tarreau62f52692017-10-08 23:01:42 +02005625
5626/****************************************/
5627/* MUX initialization and instanciation */
5628/***************************************/
5629
5630/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005631static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005632 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005633 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005634 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005635 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005636 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005637 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005638 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005639 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005640 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005641 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005642 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005643 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005644 .shutr = h2_shutr,
5645 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005646 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005647 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005648 .name = "H2",
5649};
5650
Christopher Faulet32f61c02018-04-10 14:33:41 +02005651/* PROTO selection : this mux registers PROTO token "h2" */
5652static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005653 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005654
Willy Tarreau0108d902018-11-25 19:14:37 +01005655INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5656
Willy Tarreauf8957272018-10-03 10:25:20 +02005657static struct mux_proto_list mux_proto_h2_htx =
5658 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5659
5660INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5661
Willy Tarreau62f52692017-10-08 23:01:42 +02005662/* config keyword parsers */
5663static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005664 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005665 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005666 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005667 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005668 { 0, NULL, NULL }
5669}};
5670
Willy Tarreau0108d902018-11-25 19:14:37 +01005671INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);