blob: 0b28c38c95f2a58a6a31b76902e8e47d17959650 [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 Tarreau2a856182017-05-16 15:20:39 +020032/* dummy streams returned for idle and closed states */
33static const struct h2s *h2_closed_stream;
34static const struct h2s *h2_idle_stream;
35
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020063#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020064
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200118 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100119 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200120 struct eb_root streams_by_id; /* all active streams by their ID */
121 struct list send_list; /* list of blocked streams requesting to send */
122 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200123 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100124 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200125 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126};
127
Willy Tarreau18312642017-10-11 07:57:07 +0200128/* H2 stream state, in h2s->st */
129enum h2_ss {
130 H2_SS_IDLE = 0, // idle
131 H2_SS_RLOC, // reserved(local)
132 H2_SS_RREM, // reserved(remote)
133 H2_SS_OPEN, // open
134 H2_SS_HREM, // half-closed(remote)
135 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200136 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200137 H2_SS_CLOSED, // closed
138 H2_SS_ENTRIES // must be last
139} __attribute__((packed));
140
141/* HTTP/2 stream flags (32 bit), in h2s->flags */
142#define H2_SF_NONE 0x00000000
143#define H2_SF_ES_RCVD 0x00000001
144#define H2_SF_ES_SENT 0x00000002
145
146#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
147#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
148
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200149/* stream flags indicating the reason the stream is blocked */
150#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
151#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
152#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
153#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
154#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
155
Willy Tarreau454f9052017-10-26 19:40:35 +0200156/* stream flags indicating how data is supposed to be sent */
157#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
158#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
159
160/* step we're currently in when sending chunks. This is needed because we may
161 * have to transfer chunks as large as a full buffer so there's no room left
162 * for size nor crlf around.
163 */
164#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
165#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
166#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
167
168#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
169
Willy Tarreau67434202017-11-06 20:20:51 +0100170#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100171#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100172
Willy Tarreau18312642017-10-11 07:57:07 +0200173/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
174 * it is being processed in the internal HTTP representation (H1 for now).
175 */
176struct h2s {
177 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100178 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200179 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200180 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200181 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200182 int32_t id; /* stream ID */
183 uint32_t flags; /* H2_SF_* */
184 int mws; /* mux window size for this stream */
185 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
186 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200187 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200188 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200189 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
190 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
191 struct wait_event *send_wait; /* The streeam is waiting for flow control */
192 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200193};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200194
Willy Tarreauc6405142017-09-21 20:23:50 +0200195/* descriptor for an h2 frame header */
196struct h2_fh {
197 uint32_t len; /* length, host order, 24 bits */
198 uint32_t sid; /* stream id, host order, 31 bits */
199 uint8_t ft; /* frame type */
200 uint8_t ff; /* frame flags */
201};
202
Willy Tarreau8ceae722018-11-26 11:58:30 +0100203/* the h2c connection pool */
204DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
205
206/* the h2s stream pool */
207DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
208
Willy Tarreaudc572362018-12-12 08:08:05 +0100209/* The default connection window size is 65535, it may only be enlarged using
210 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
211 * we'll pretend we already received the difference between the two to send
212 * an equivalent window update to enlarge it to 2G-1.
213 */
214#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
215
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200216/* a few settings from the global section */
217static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200218static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200219static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200220
Willy Tarreau2a856182017-05-16 15:20:39 +0200221/* a dmumy closed stream */
222static const struct h2s *h2_closed_stream = &(const struct h2s){
223 .cs = NULL,
224 .h2c = NULL,
225 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100226 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100227 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200228 .id = 0,
229};
230
231/* and a dummy idle stream for use with any unannounced stream */
232static const struct h2s *h2_idle_stream = &(const struct h2s){
233 .cs = NULL,
234 .h2c = NULL,
235 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100236 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200237 .id = 0,
238};
239
Olivier Houchard9f6af332018-05-25 14:04:04 +0200240static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200241static int h2_send(struct h2c *h2c);
242static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200243static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200244static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100245static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200246static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100247static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200248static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100249static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100250static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200251
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200252/*****************************************************/
253/* functions below are for dynamic buffer management */
254/*****************************************************/
255
Willy Tarreau315d8072017-12-10 22:17:57 +0100256/* indicates whether or not the we may call the h2_recv() function to attempt
257 * to receive data into the buffer and/or demux pending data. The condition is
258 * a bit complex due to some API limits for now. The rules are the following :
259 * - if an error or a shutdown was detected on the connection and the buffer
260 * is empty, we must not attempt to receive
261 * - if the demux buf failed to be allocated, we must not try to receive and
262 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100263 * - if no flag indicates a blocking condition, we may attempt to receive,
264 * regardless of whether the demux buffer is full or not, so that only
265 * de demux part decides whether or not to block. This is needed because
266 * the connection API indeed prevents us from re-enabling receipt that is
267 * already enabled in a polled state, so we must always immediately stop
268 * as soon as the demux can't proceed so as never to hit an end of read
269 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100270 * - otherwise must may not attempt
271 */
272static inline int h2_recv_allowed(const struct h2c *h2c)
273{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200274 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100275 (h2c->st0 >= H2_CS_ERROR ||
276 h2c->conn->flags & CO_FL_ERROR ||
277 conn_xprt_read0_pending(h2c->conn)))
278 return 0;
279
280 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100281 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100282 return 1;
283
284 return 0;
285}
286
Willy Tarreauf2101912018-07-19 10:11:38 +0200287/* returns true if the connection has too many conn_streams attached */
288static inline int h2_has_too_many_cs(const struct h2c *h2c)
289{
290 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
291}
292
Willy Tarreau44e973f2018-03-01 17:49:30 +0100293/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
294 * flags are used to figure what buffer was requested. It returns 1 if the
295 * allocation succeeds, in which case the connection is woken up, or 0 if it's
296 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200297 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100298static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299{
300 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100301 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200302
Willy Tarreau44e973f2018-03-01 17:49:30 +0100303 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200304 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200305 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200306 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200307 return 1;
308 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200309
Willy Tarreau44e973f2018-03-01 17:49:30 +0100310 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
311 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200312
313 if (h2c->flags & H2_CF_DEM_MROOM) {
314 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200315 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200316 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200317 }
Willy Tarreau14398122017-09-22 14:26:04 +0200318 return 1;
319 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100320
321 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
322 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200323 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100324 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200325 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200326 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100327 return 1;
328 }
329
Willy Tarreau14398122017-09-22 14:26:04 +0200330 return 0;
331}
332
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200333static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200334{
335 struct buffer *buf = NULL;
336
Willy Tarreau44e973f2018-03-01 17:49:30 +0100337 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
338 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
339 h2c->buf_wait.target = h2c;
340 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100342 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200344 __conn_xprt_stop_recv(h2c->conn);
345 }
346 return buf;
347}
348
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200349static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200350{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200351 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200353 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200354 }
355}
356
Olivier Houchardd540b362018-11-05 18:37:53 +0100357static int h2_avail_streams(struct connection *conn)
358{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100359 struct h2c *h2c = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100360
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100361 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100362 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
363}
364
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100365static int h2_max_streams(struct connection *conn)
366{
367 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
368 return h2_settings_max_concurrent_streams;
369}
370
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200371
Willy Tarreau62f52692017-10-08 23:01:42 +0200372/*****************************************************************/
373/* functions below are dedicated to the mux setup and management */
374/*****************************************************************/
375
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200376/* Initialize the mux once it's attached. For outgoing connections, the context
377 * is already initialized before installing the mux, so we detect incoming
378 * connections from the fact that the context is still NULL. Returns < 0 on
379 * error.
380 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100381static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382{
383 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100384 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200385
Willy Tarreaubafbe012017-11-24 17:34:44 +0100386 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200387 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200388 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200389
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100390 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200391 h2c->flags = H2_CF_IS_BACK;
392 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
393 if (tick_isset(prx->timeout.serverfin))
394 h2c->shut_timeout = prx->timeout.serverfin;
395 } else {
396 h2c->flags = H2_CF_NONE;
397 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
398 if (tick_isset(prx->timeout.clientfin))
399 h2c->shut_timeout = prx->timeout.clientfin;
400 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100401
Willy Tarreau0b37d652018-10-03 10:33:02 +0200402 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100403 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100404 if (tick_isset(h2c->timeout)) {
405 t = task_new(tid_bit);
406 if (!t)
407 goto fail;
408
409 h2c->task = t;
410 t->process = h2_timeout_task;
411 t->context = h2c;
412 t->expire = tick_add(now_ms, h2c->timeout);
413 }
Willy Tarreauea392822017-10-31 10:02:25 +0100414
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200415 h2c->wait_event.task = tasklet_new();
416 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200417 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200418 h2c->wait_event.task->process = h2_io_cb;
419 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100420 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200421
Willy Tarreau32218eb2017-09-22 08:07:25 +0200422 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
423 if (!h2c->ddht)
424 goto fail;
425
426 /* Initialise the context. */
427 h2c->st0 = H2_CS_PREFACE;
428 h2c->conn = conn;
429 h2c->max_id = -1;
430 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreaudc572362018-12-12 08:08:05 +0100431 h2c->rcvd_c = H2_INITIAL_WINDOW_INCREMENT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100433 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200434 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200435
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200436 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200437 h2c->dsi = -1;
438 h2c->msi = -1;
439 h2c->last_sid = -1;
440
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200441 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200442 h2c->miw = 65535; /* mux initial window size */
443 h2c->mws = 65535; /* mux window size */
444 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200445 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200446 LIST_INIT(&h2c->send_list);
447 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200448 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100449 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200450
Willy Tarreau3f133572017-10-31 19:21:06 +0100451 if (t)
452 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100453
Willy Tarreau01b44822018-10-03 14:26:37 +0200454 if (h2c->flags & H2_CF_IS_BACK) {
455 /* FIXME: this is temporary, for outgoing connections we need
456 * to immediately allocate a stream until the code is modified
457 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100458 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200459 */
460 struct h2s *h2s;
461
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100462 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200463 if (!h2s)
464 goto fail_stream;
465 }
466
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100467 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200468
Willy Tarreau0f383582018-10-03 14:22:21 +0200469 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200470 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200471 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200472 fail_stream:
473 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200474 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100475 if (t)
476 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200477 if (h2c->wait_event.task)
478 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100479 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200480 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481 return -1;
482}
483
Willy Tarreau751f2d02018-10-05 09:35:00 +0200484/* returns the next allocatable outgoing stream ID for the H2 connection, or
485 * -1 if no more is allocatable.
486 */
487static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
488{
489 int32_t id = (h2c->max_id + 1) | 1;
490 if (id & 0x80000000U)
491 id = -1;
492 return id;
493}
494
Willy Tarreau2373acc2017-10-12 17:35:14 +0200495/* returns the stream associated with id <id> or NULL if not found */
496static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
497{
498 struct eb32_node *node;
499
Willy Tarreau751f2d02018-10-05 09:35:00 +0200500 if (id == 0)
501 return (struct h2s *)h2_closed_stream;
502
Willy Tarreau2a856182017-05-16 15:20:39 +0200503 if (id > h2c->max_id)
504 return (struct h2s *)h2_idle_stream;
505
Willy Tarreau2373acc2017-10-12 17:35:14 +0200506 node = eb32_lookup(&h2c->streams_by_id, id);
507 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200508 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200509
510 return container_of(node, struct h2s, by_id);
511}
512
Willy Tarreau62f52692017-10-08 23:01:42 +0200513/* release function for a connection. This one should be called to free all
514 * resources allocated to the mux.
515 */
516static void h2_release(struct connection *conn)
517{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100518 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200519
520 LIST_DEL(&conn->list);
521
522 if (h2c) {
523 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200524
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100525 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100526 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100527 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200528
Willy Tarreau44e973f2018-03-01 17:49:30 +0100529 h2_release_buf(h2c, &h2c->dbuf);
530 h2_release_buf(h2c, &h2c->mbuf);
531
Willy Tarreauea392822017-10-31 10:02:25 +0100532 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200533 h2c->task->context = NULL;
534 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100535 h2c->task = NULL;
536 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200537 if (h2c->wait_event.task)
538 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100539 if (h2c->wait_event.events != 0)
540 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200541 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100542
Willy Tarreaubafbe012017-11-24 17:34:44 +0100543 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200544 }
545
546 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100547 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200548
549 conn_stop_tracking(conn);
550 conn_full_close(conn);
551 if (conn->destroy_cb)
552 conn->destroy_cb(conn);
553 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200554}
555
556
Willy Tarreau71681172017-10-23 14:39:06 +0200557/******************************************************/
558/* functions below are for the H2 protocol processing */
559/******************************************************/
560
561/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100562static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200563{
564 return h2s ? h2s->id : 0;
565}
566
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200567/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100568static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200569{
570 if (h2c->msi < 0)
571 return 0;
572
573 if (h2c->msi == h2s_id(h2s))
574 return 0;
575
576 return 1;
577}
578
Willy Tarreau741d6df2017-10-17 08:00:59 +0200579/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200581{
582 h2c->errcode = err;
583 h2c->st0 = H2_CS_ERROR;
584}
585
Willy Tarreau2e43f082017-10-17 08:03:59 +0200586/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100587static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200588{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200589 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200590 h2s->errcode = err;
591 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100592 if (h2s->cs)
593 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200594 }
595}
596
Willy Tarreau7e094452018-12-19 18:08:52 +0100597/* attempt to notify the data layer of recv availability */
598static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
599{
600 struct wait_event *sw;
601
602 if (h2s->recv_wait) {
603 sw = h2s->recv_wait;
604 sw->events &= ~SUB_RETRY_RECV;
605 tasklet_wakeup(sw->task);
606 h2s->recv_wait = NULL;
607 }
608}
609
610/* attempt to notify the data layer of send availability */
611static void __maybe_unused h2s_notify_send(struct h2s *h2s)
612{
613 struct wait_event *sw;
614
615 if (h2s->send_wait) {
616 sw = h2s->send_wait;
617 sw->events &= ~SUB_RETRY_SEND;
618 tasklet_wakeup(sw->task);
619 h2s->send_wait = NULL;
620 }
621}
622
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100623/* alerts the data layer, trying to wake it up by all means, following
624 * this sequence :
625 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
626 * - if its subscribed to send, then it's woken up for send
627 * - if it was subscribed to neither, its ->wake() callback is called
628 * It is safe to call this function with a closed stream which doesn't have a
629 * conn_stream anymore.
630 */
631static void __maybe_unused h2s_alert(struct h2s *h2s)
632{
633 if (h2s->recv_wait || h2s->send_wait) {
634 h2s_notify_recv(h2s);
635 h2s_notify_send(h2s);
636 }
637 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
638 h2s->cs->data_cb->wake(h2s->cs);
639}
640
Willy Tarreaue4820742017-07-27 13:37:23 +0200641/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100642static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200643{
644 uint8_t *out = frame;
645
646 *out = len >> 16;
647 write_n16(out + 1, len);
648}
649
Willy Tarreau54c15062017-10-10 17:10:03 +0200650/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
651 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
652 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200653 * available in the buffer's input prior to calling this function. The buffer
654 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200655 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100656static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200657 const struct buffer *b, int o)
658{
Willy Tarreau591d4452018-06-15 17:21:00 +0200659 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 +0200660}
661
Willy Tarreau1f094672017-11-20 21:27:45 +0100662static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200663{
Willy Tarreau591d4452018-06-15 17:21:00 +0200664 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200665}
666
Willy Tarreau1f094672017-11-20 21:27:45 +0100667static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200668{
Willy Tarreau591d4452018-06-15 17:21:00 +0200669 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200670}
671
Willy Tarreau1f094672017-11-20 21:27:45 +0100672static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200673{
Willy Tarreau591d4452018-06-15 17:21:00 +0200674 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200675}
676
677
Willy Tarreau715d5312017-07-11 15:20:24 +0200678/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
679 * is not obvious. It turns out that H2 headers are neither aligned nor do they
680 * use regular sizes. And to add to the trouble, the buffer may wrap so each
681 * byte read must be checked. The header is formed like this :
682 *
683 * b0 b1 b2 b3 b4 b5..b8
684 * +----------+---------+--------+----+----+----------------------+
685 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
686 * +----------+---------+--------+----+----+----------------------+
687 *
688 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
689 * we get the sid properly aligned and ordered, and 16 bits of len properly
690 * ordered as well. The type and flags can be extracted using bit shifts from
691 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200692 * Returns zero if some bytes are missing, otherwise non-zero on success. The
693 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200694 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100695static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200696{
697 uint64_t w;
698
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200699 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200700 return 0;
701
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200702 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200703 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200704 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
705 h->ff = w >> 32;
706 h->ft = w >> 40;
707 h->len += w >> 48;
708 return 1;
709}
710
711/* skip the next 9 bytes corresponding to the frame header possibly parsed by
712 * h2_peek_frame_hdr() above.
713 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100714static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200715{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200716 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200717}
718
719/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100720static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200721{
722 int ret;
723
724 ret = h2_peek_frame_hdr(b, h);
725 if (ret > 0)
726 h2_skip_frame_hdr(b);
727 return ret;
728}
729
Willy Tarreau00dd0782018-03-01 16:31:34 +0100730/* marks stream <h2s> as CLOSED and decrement the number of active streams for
731 * its connection if the stream was not yet closed. Please use this exclusively
732 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100733 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100734static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100735{
736 if (h2s->st != H2_SS_CLOSED)
737 h2s->h2c->nb_streams--;
738 h2s->st = H2_SS_CLOSED;
739}
740
Willy Tarreau71049cc2018-03-28 13:56:39 +0200741/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
742static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100743{
744 h2s_close(h2s);
745 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200746 if (b_size(&h2s->rxbuf)) {
747 b_free(&h2s->rxbuf);
748 offer_buffers(NULL, tasks_run_queue);
749 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200750 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100751 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200752 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100753 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800754 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200755 * reference left would be in the h2c send_list/fctl_list, and if
756 * we're in it, we're getting out anyway
757 */
758 LIST_DEL(&h2s->list);
759 LIST_INIT(&h2s->list);
760 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100761 pool_free(pool_head_h2s, h2s);
762}
763
Willy Tarreaua8e49542018-10-03 18:53:55 +0200764/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
765 * stream tree. In case of error, nothing is added and NULL is returned. The
766 * causes of errors can be any failed memory allocation. The caller is
767 * responsible for checking if the connection may support an extra stream
768 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200769 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200770static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200771{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200772 struct h2s *h2s;
773
Willy Tarreaubafbe012017-11-24 17:34:44 +0100774 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200775 if (!h2s)
776 goto out;
777
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200778 h2s->wait_event.task = tasklet_new();
779 if (!h2s->wait_event.task) {
780 pool_free(pool_head_h2s, h2s);
781 goto out;
782 }
783 h2s->send_wait = NULL;
784 h2s->recv_wait = NULL;
785 h2s->wait_event.task->process = h2_deferred_shut;
786 h2s->wait_event.task->context = h2s;
787 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100788 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200789 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200790 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200791 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200792 h2s->mws = h2c->miw;
793 h2s->flags = H2_SF_NONE;
794 h2s->errcode = H2_ERR_NO_ERROR;
795 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200796 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200797 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200798
799 if (h2c->flags & H2_CF_IS_BACK) {
800 h1m_init_req(&h2s->h1m);
801 h2s->h1m.err_pos = -1; // don't care about errors on the request path
802 h2s->h1m.flags |= H1_MF_TOLOWER;
803 } else {
804 h1m_init_res(&h2s->h1m);
805 h2s->h1m.err_pos = -1; // don't care about errors on the response path
806 h2s->h1m.flags |= H1_MF_TOLOWER;
807 }
808
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200809 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200810 if (id > 0)
811 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200812
813 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100814 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200815
816 return h2s;
817
818 out_free_h2s:
819 pool_free(pool_head_h2s, h2s);
820 out:
821 return NULL;
822}
823
824/* creates a new stream <id> on the h2c connection and returns it, or NULL in
825 * case of memory allocation error.
826 */
827static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
828{
829 struct session *sess = h2c->conn->owner;
830 struct conn_stream *cs;
831 struct h2s *h2s;
832
833 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
834 goto out;
835
836 h2s = h2s_new(h2c, id);
837 if (!h2s)
838 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200839
840 cs = cs_new(h2c->conn);
841 if (!cs)
842 goto out_close;
843
Olivier Houchard746fb772018-12-15 19:42:00 +0100844 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200845 h2s->cs = cs;
846 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200847 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200848
849 if (stream_create_from_cs(cs) < 0)
850 goto out_free_cs;
851
Willy Tarreau590a0512018-09-05 11:56:48 +0200852 /* We want the accept date presented to the next stream to be the one
853 * we have now, the handshake time to be null (since the next stream
854 * is not delayed by a handshake), and the idle time to count since
855 * right now.
856 */
857 sess->accept_date = date;
858 sess->tv_accept = now;
859 sess->t_handshake = 0;
860
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200861 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200862 if (h2_has_too_many_cs(h2c))
863 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200864 return h2s;
865
866 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200867 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200868 cs_free(cs);
869 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200870 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200871 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200872 sess_log(sess);
873 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200874}
875
Willy Tarreau751f2d02018-10-05 09:35:00 +0200876/* allocates a new stream associated to conn_stream <cs> on the h2c connection
877 * and returns it, or NULL in case of memory allocation error or if the highest
878 * possible stream ID was reached.
879 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100880static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200881{
882 struct h2s *h2s = NULL;
883
884 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
885 goto out;
886
887 /* Defer choosing the ID until we send the first message to create the stream */
888 h2s = h2s_new(h2c, 0);
889 if (!h2s)
890 goto out;
891
892 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100893 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200894 cs->ctx = h2s;
895 h2c->nb_cs++;
896
Willy Tarreau751f2d02018-10-05 09:35:00 +0200897 out:
898 return h2s;
899}
900
Willy Tarreaube5b7152017-09-25 16:25:39 +0200901/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
902 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
903 * the various settings codes.
904 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200905static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200906{
907 struct buffer *res;
908 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200909 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200910 int ret;
911
912 if (h2c_mux_busy(h2c, NULL)) {
913 h2c->flags |= H2_CF_DEM_MBUSY;
914 return 0;
915 }
916
Willy Tarreau44e973f2018-03-01 17:49:30 +0100917 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200918 if (!res) {
919 h2c->flags |= H2_CF_MUX_MALLOC;
920 h2c->flags |= H2_CF_DEM_MROOM;
921 return 0;
922 }
923
924 chunk_init(&buf, buf_data, sizeof(buf_data));
925 chunk_memcpy(&buf,
926 "\x00\x00\x00" /* length : 0 for now */
927 "\x04\x00" /* type : 4 (settings), flags : 0 */
928 "\x00\x00\x00\x00", /* stream ID : 0 */
929 9);
930
931 if (h2_settings_header_table_size != 4096) {
932 char str[6] = "\x00\x01"; /* header_table_size */
933
934 write_n32(str + 2, h2_settings_header_table_size);
935 chunk_memcat(&buf, str, 6);
936 }
937
938 if (h2_settings_initial_window_size != 65535) {
939 char str[6] = "\x00\x04"; /* initial_window_size */
940
941 write_n32(str + 2, h2_settings_initial_window_size);
942 chunk_memcat(&buf, str, 6);
943 }
944
945 if (h2_settings_max_concurrent_streams != 0) {
946 char str[6] = "\x00\x03"; /* max_concurrent_streams */
947
948 /* Note: 0 means "unlimited" for haproxy's config but not for
949 * the protocol, so never send this value!
950 */
951 write_n32(str + 2, h2_settings_max_concurrent_streams);
952 chunk_memcat(&buf, str, 6);
953 }
954
955 if (global.tune.bufsize != 16384) {
956 char str[6] = "\x00\x05"; /* max_frame_size */
957
958 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
959 * match bufsize - rewrite size, but at the moment it seems
960 * that clients don't take care of it.
961 */
962 write_n32(str + 2, global.tune.bufsize);
963 chunk_memcat(&buf, str, 6);
964 }
965
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200966 h2_set_frame_size(buf.area, buf.data - 9);
967 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200968 if (unlikely(ret <= 0)) {
969 if (!ret) {
970 h2c->flags |= H2_CF_MUX_MFULL;
971 h2c->flags |= H2_CF_DEM_MROOM;
972 return 0;
973 }
974 else {
975 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
976 return 0;
977 }
978 }
979 return ret;
980}
981
Willy Tarreau52eed752017-09-22 15:05:09 +0200982/* Try to receive a connection preface, then upon success try to send our
983 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
984 * missing data. It may return an error in h2c.
985 */
986static int h2c_frt_recv_preface(struct h2c *h2c)
987{
988 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200989 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200990
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200991 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200992
993 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200994 if (ret1 < 0)
995 sess_log(h2c->conn->owner);
996
Willy Tarreau52eed752017-09-22 15:05:09 +0200997 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
998 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
999 return 0;
1000 }
1001
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001002 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001003 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001004 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001005
Willy Tarreaube5b7152017-09-25 16:25:39 +02001006 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001007}
1008
Willy Tarreau01b44822018-10-03 14:26:37 +02001009/* Try to send a connection preface, then upon success try to send our
1010 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1011 * missing data. It may return an error in h2c.
1012 */
1013static int h2c_bck_send_preface(struct h2c *h2c)
1014{
1015 struct buffer *res;
1016
1017 if (h2c_mux_busy(h2c, NULL)) {
1018 h2c->flags |= H2_CF_DEM_MBUSY;
1019 return 0;
1020 }
1021
1022 res = h2_get_buf(h2c, &h2c->mbuf);
1023 if (!res) {
1024 h2c->flags |= H2_CF_MUX_MALLOC;
1025 h2c->flags |= H2_CF_DEM_MROOM;
1026 return 0;
1027 }
1028
1029 if (!b_data(res)) {
1030 /* preface not yet sent */
1031 b_istput(res, ist(H2_CONN_PREFACE));
1032 }
1033
1034 return h2c_send_settings(h2c);
1035}
1036
Willy Tarreau081d4722017-05-16 21:51:05 +02001037/* try to send a GOAWAY frame on the connection to report an error or a graceful
1038 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1039 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1040 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1041 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1042 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1043 * on unrecoverable failure. It will not attempt to send one again in this last
1044 * case so that it is safe to use h2c_error() to report such errors.
1045 */
1046static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1047{
1048 struct buffer *res;
1049 char str[17];
1050 int ret;
1051
1052 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1053 return 1; // claim that it worked
1054
1055 if (h2c_mux_busy(h2c, h2s)) {
1056 if (h2s)
1057 h2s->flags |= H2_SF_BLK_MBUSY;
1058 else
1059 h2c->flags |= H2_CF_DEM_MBUSY;
1060 return 0;
1061 }
1062
Willy Tarreau44e973f2018-03-01 17:49:30 +01001063 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001064 if (!res) {
1065 h2c->flags |= H2_CF_MUX_MALLOC;
1066 if (h2s)
1067 h2s->flags |= H2_SF_BLK_MROOM;
1068 else
1069 h2c->flags |= H2_CF_DEM_MROOM;
1070 return 0;
1071 }
1072
1073 /* len: 8, type: 7, flags: none, sid: 0 */
1074 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1075
1076 if (h2c->last_sid < 0)
1077 h2c->last_sid = h2c->max_id;
1078
1079 write_n32(str + 9, h2c->last_sid);
1080 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001081 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001082 if (unlikely(ret <= 0)) {
1083 if (!ret) {
1084 h2c->flags |= H2_CF_MUX_MFULL;
1085 if (h2s)
1086 h2s->flags |= H2_SF_BLK_MROOM;
1087 else
1088 h2c->flags |= H2_CF_DEM_MROOM;
1089 return 0;
1090 }
1091 else {
1092 /* we cannot report this error using GOAWAY, so we mark
1093 * it and claim a success.
1094 */
1095 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1096 h2c->flags |= H2_CF_GOAWAY_FAILED;
1097 return 1;
1098 }
1099 }
1100 h2c->flags |= H2_CF_GOAWAY_SENT;
1101 return ret;
1102}
1103
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001104/* Try to send an RST_STREAM frame on the connection for the indicated stream
1105 * during mux operations. This stream must be valid and cannot be closed
1106 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1107 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1108 * not yet.
1109 *
1110 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1111 * to write the message, it subscribes the stream to future notifications.
1112 */
1113static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1114{
1115 struct buffer *res;
1116 char str[13];
1117 int ret;
1118
1119 if (!h2s || h2s->st == H2_SS_CLOSED)
1120 return 1;
1121
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001122 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1123 * RST_STREAM in response to a RST_STREAM frame.
1124 */
1125 if (h2c->dft == H2_FT_RST_STREAM) {
1126 ret = 1;
1127 goto ignore;
1128 }
1129
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001130 if (h2c_mux_busy(h2c, h2s)) {
1131 h2s->flags |= H2_SF_BLK_MBUSY;
1132 return 0;
1133 }
1134
Willy Tarreau44e973f2018-03-01 17:49:30 +01001135 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001136 if (!res) {
1137 h2c->flags |= H2_CF_MUX_MALLOC;
1138 h2s->flags |= H2_SF_BLK_MROOM;
1139 return 0;
1140 }
1141
1142 /* len: 4, type: 3, flags: none */
1143 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1144 write_n32(str + 5, h2s->id);
1145 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001146 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001147
1148 if (unlikely(ret <= 0)) {
1149 if (!ret) {
1150 h2c->flags |= H2_CF_MUX_MFULL;
1151 h2s->flags |= H2_SF_BLK_MROOM;
1152 return 0;
1153 }
1154 else {
1155 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1156 return 0;
1157 }
1158 }
1159
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001160 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001161 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001162 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001163 return ret;
1164}
1165
1166/* Try to send an RST_STREAM frame on the connection for the stream being
1167 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1168 * error code unless the stream's state already is IDLE or CLOSED in which
1169 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1170 * it was not yet.
1171 *
1172 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1173 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001174 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001175 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001176 */
1177static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1178{
1179 struct buffer *res;
1180 char str[13];
1181 int ret;
1182
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001183 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1184 * RST_STREAM in response to a RST_STREAM frame.
1185 */
1186 if (h2c->dft == H2_FT_RST_STREAM) {
1187 ret = 1;
1188 goto ignore;
1189 }
1190
Willy Tarreau27a84c92017-10-17 08:10:17 +02001191 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001192 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001193 return 0;
1194 }
1195
Willy Tarreau44e973f2018-03-01 17:49:30 +01001196 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001197 if (!res) {
1198 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001199 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001200 return 0;
1201 }
1202
1203 /* len: 4, type: 3, flags: none */
1204 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001205
Willy Tarreau27a84c92017-10-17 08:10:17 +02001206 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001207 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001208 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001209
Willy Tarreau27a84c92017-10-17 08:10:17 +02001210 if (unlikely(ret <= 0)) {
1211 if (!ret) {
1212 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001213 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001214 return 0;
1215 }
1216 else {
1217 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1218 return 0;
1219 }
1220 }
1221
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001222 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001223 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001224 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001225 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001226 }
1227
Willy Tarreau27a84c92017-10-17 08:10:17 +02001228 return ret;
1229}
1230
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001231/* try to send an empty DATA frame with the ES flag set to notify about the
1232 * end of stream and match a shutdown(write). If an ES was already sent as
1233 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1234 * on success or zero if nothing was done. In case of lack of room to write the
1235 * message, it subscribes the requesting stream to future notifications.
1236 */
1237static int h2_send_empty_data_es(struct h2s *h2s)
1238{
1239 struct h2c *h2c = h2s->h2c;
1240 struct buffer *res;
1241 char str[9];
1242 int ret;
1243
Willy Tarreau721c9742017-11-07 11:05:42 +01001244 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001245 return 1;
1246
1247 if (h2c_mux_busy(h2c, h2s)) {
1248 h2s->flags |= H2_SF_BLK_MBUSY;
1249 return 0;
1250 }
1251
Willy Tarreau44e973f2018-03-01 17:49:30 +01001252 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001253 if (!res) {
1254 h2c->flags |= H2_CF_MUX_MALLOC;
1255 h2s->flags |= H2_SF_BLK_MROOM;
1256 return 0;
1257 }
1258
1259 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1260 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1261 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001262 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001263 if (likely(ret > 0)) {
1264 h2s->flags |= H2_SF_ES_SENT;
1265 }
1266 else if (!ret) {
1267 h2c->flags |= H2_CF_MUX_MFULL;
1268 h2s->flags |= H2_SF_BLK_MROOM;
1269 return 0;
1270 }
1271 else {
1272 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1273 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001274 }
1275 return ret;
1276}
1277
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001278/* wake the streams attached to the connection, whose id is greater than <last>,
1279 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001280 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1281 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001282 */
1283static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1284{
1285 struct eb32_node *node;
1286 struct h2s *h2s;
1287
1288 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001289 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001290
1291 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001292 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001293
1294 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1295 while (node) {
1296 h2s = container_of(node, struct h2s, by_id);
1297 if (h2s->id <= last)
1298 break;
1299 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001300
1301 if (!h2s->cs) {
1302 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001303 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001304 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001305 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001306
1307 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001308 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1309 h2s->cs->flags |= CS_FL_ERROR;
1310
Willy Tarreauf830f012018-12-19 17:44:55 +01001311 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001312
Willy Tarreaua8519352018-12-18 16:44:28 +01001313 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001314 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001315 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001316 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001317 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001318 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001319 }
1320}
1321
Willy Tarreau3421aba2017-07-27 15:41:03 +02001322/* Increase all streams' outgoing window size by the difference passed in
1323 * argument. This is needed upon receipt of the settings frame if the initial
1324 * window size is different. The difference may be negative and the resulting
1325 * window size as well, for the time it takes to receive some window updates.
1326 */
1327static void h2c_update_all_ws(struct h2c *h2c, int diff)
1328{
1329 struct h2s *h2s;
1330 struct eb32_node *node;
1331
1332 if (!diff)
1333 return;
1334
1335 node = eb32_first(&h2c->streams_by_id);
1336 while (node) {
1337 h2s = container_of(node, struct h2s, by_id);
1338 h2s->mws += diff;
1339 node = eb32_next(node);
1340 }
1341}
1342
1343/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1344 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1345 * return an error in h2c. Described in RFC7540#6.5.
1346 */
1347static int h2c_handle_settings(struct h2c *h2c)
1348{
1349 unsigned int offset;
1350 int error;
1351
1352 if (h2c->dff & H2_F_SETTINGS_ACK) {
1353 if (h2c->dfl) {
1354 error = H2_ERR_FRAME_SIZE_ERROR;
1355 goto fail;
1356 }
1357 return 1;
1358 }
1359
1360 if (h2c->dsi != 0) {
1361 error = H2_ERR_PROTOCOL_ERROR;
1362 goto fail;
1363 }
1364
1365 if (h2c->dfl % 6) {
1366 error = H2_ERR_FRAME_SIZE_ERROR;
1367 goto fail;
1368 }
1369
1370 /* that's the limit we can process */
1371 if (h2c->dfl > global.tune.bufsize) {
1372 error = H2_ERR_FRAME_SIZE_ERROR;
1373 goto fail;
1374 }
1375
1376 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001377 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001378 return 0;
1379
1380 /* parse the frame */
1381 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001382 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1383 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001384
1385 switch (type) {
1386 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1387 /* we need to update all existing streams with the
1388 * difference from the previous iws.
1389 */
1390 if (arg < 0) { // RFC7540#6.5.2
1391 error = H2_ERR_FLOW_CONTROL_ERROR;
1392 goto fail;
1393 }
1394 h2c_update_all_ws(h2c, arg - h2c->miw);
1395 h2c->miw = arg;
1396 break;
1397 case H2_SETTINGS_MAX_FRAME_SIZE:
1398 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1399 error = H2_ERR_PROTOCOL_ERROR;
1400 goto fail;
1401 }
1402 h2c->mfs = arg;
1403 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001404 case H2_SETTINGS_ENABLE_PUSH:
1405 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1406 error = H2_ERR_PROTOCOL_ERROR;
1407 goto fail;
1408 }
1409 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001410 }
1411 }
1412
1413 /* need to ACK this frame now */
1414 h2c->st0 = H2_CS_FRAME_A;
1415 return 1;
1416 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001417 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001418 h2c_error(h2c, error);
1419 return 0;
1420}
1421
1422/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1423 * success or one of the h2_status values.
1424 */
1425static int h2c_ack_settings(struct h2c *h2c)
1426{
1427 struct buffer *res;
1428 char str[9];
1429 int ret = -1;
1430
1431 if (h2c_mux_busy(h2c, NULL)) {
1432 h2c->flags |= H2_CF_DEM_MBUSY;
1433 return 0;
1434 }
1435
Willy Tarreau44e973f2018-03-01 17:49:30 +01001436 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001437 if (!res) {
1438 h2c->flags |= H2_CF_MUX_MALLOC;
1439 h2c->flags |= H2_CF_DEM_MROOM;
1440 return 0;
1441 }
1442
1443 memcpy(str,
1444 "\x00\x00\x00" /* length : 0 (no data) */
1445 "\x04" "\x01" /* type : 4, flags : ACK */
1446 "\x00\x00\x00\x00" /* stream ID */, 9);
1447
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001448 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001449 if (unlikely(ret <= 0)) {
1450 if (!ret) {
1451 h2c->flags |= H2_CF_MUX_MFULL;
1452 h2c->flags |= H2_CF_DEM_MROOM;
1453 return 0;
1454 }
1455 else {
1456 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1457 return 0;
1458 }
1459 }
1460 return ret;
1461}
1462
Willy Tarreaucf68c782017-10-10 17:11:41 +02001463/* processes a PING frame and schedules an ACK if needed. The caller must pass
1464 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1465 * missing data. It may return an error in h2c.
1466 */
1467static int h2c_handle_ping(struct h2c *h2c)
1468{
1469 /* frame length must be exactly 8 */
1470 if (h2c->dfl != 8) {
1471 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1472 return 0;
1473 }
1474
1475 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001476 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001477 h2c->st0 = H2_CS_FRAME_A;
1478 return 1;
1479}
1480
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001481/* Try to send a window update for stream id <sid> and value <increment>.
1482 * Returns > 0 on success or zero on missing room or failure. It may return an
1483 * error in h2c.
1484 */
1485static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1486{
1487 struct buffer *res;
1488 char str[13];
1489 int ret = -1;
1490
1491 if (h2c_mux_busy(h2c, NULL)) {
1492 h2c->flags |= H2_CF_DEM_MBUSY;
1493 return 0;
1494 }
1495
Willy Tarreau44e973f2018-03-01 17:49:30 +01001496 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001497 if (!res) {
1498 h2c->flags |= H2_CF_MUX_MALLOC;
1499 h2c->flags |= H2_CF_DEM_MROOM;
1500 return 0;
1501 }
1502
1503 /* length: 4, type: 8, flags: none */
1504 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1505 write_n32(str + 5, sid);
1506 write_n32(str + 9, increment);
1507
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001508 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001509
1510 if (unlikely(ret <= 0)) {
1511 if (!ret) {
1512 h2c->flags |= H2_CF_MUX_MFULL;
1513 h2c->flags |= H2_CF_DEM_MROOM;
1514 return 0;
1515 }
1516 else {
1517 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1518 return 0;
1519 }
1520 }
1521 return ret;
1522}
1523
1524/* try to send pending window update for the connection. It's safe to call it
1525 * with no pending updates. Returns > 0 on success or zero on missing room or
1526 * failure. It may return an error in h2c.
1527 */
1528static int h2c_send_conn_wu(struct h2c *h2c)
1529{
1530 int ret = 1;
1531
1532 if (h2c->rcvd_c <= 0)
1533 return 1;
1534
1535 /* send WU for the connection */
1536 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1537 if (ret > 0)
1538 h2c->rcvd_c = 0;
1539
1540 return ret;
1541}
1542
1543/* try to send pending window update for the current dmux stream. It's safe to
1544 * call it with no pending updates. Returns > 0 on success or zero on missing
1545 * room or failure. It may return an error in h2c.
1546 */
1547static int h2c_send_strm_wu(struct h2c *h2c)
1548{
1549 int ret = 1;
1550
1551 if (h2c->rcvd_s <= 0)
1552 return 1;
1553
1554 /* send WU for the stream */
1555 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1556 if (ret > 0)
1557 h2c->rcvd_s = 0;
1558
1559 return ret;
1560}
1561
Willy Tarreaucf68c782017-10-10 17:11:41 +02001562/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1563 * success, 0 on missing data or one of the h2_status values.
1564 */
1565static int h2c_ack_ping(struct h2c *h2c)
1566{
1567 struct buffer *res;
1568 char str[17];
1569 int ret = -1;
1570
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001571 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001572 return 0;
1573
1574 if (h2c_mux_busy(h2c, NULL)) {
1575 h2c->flags |= H2_CF_DEM_MBUSY;
1576 return 0;
1577 }
1578
Willy Tarreau44e973f2018-03-01 17:49:30 +01001579 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001580 if (!res) {
1581 h2c->flags |= H2_CF_MUX_MALLOC;
1582 h2c->flags |= H2_CF_DEM_MROOM;
1583 return 0;
1584 }
1585
1586 memcpy(str,
1587 "\x00\x00\x08" /* length : 8 (same payload) */
1588 "\x06" "\x01" /* type : 6, flags : ACK */
1589 "\x00\x00\x00\x00" /* stream ID */, 9);
1590
1591 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001592 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001593
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001594 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001595 if (unlikely(ret <= 0)) {
1596 if (!ret) {
1597 h2c->flags |= H2_CF_MUX_MFULL;
1598 h2c->flags |= H2_CF_DEM_MROOM;
1599 return 0;
1600 }
1601 else {
1602 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1603 return 0;
1604 }
1605 }
1606 return ret;
1607}
1608
Willy Tarreau26f95952017-07-27 17:18:30 +02001609/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1610 * Returns > 0 on success or zero on missing data. It may return an error in
1611 * h2c or h2s. Described in RFC7540#6.9.
1612 */
1613static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1614{
1615 int32_t inc;
1616 int error;
1617
1618 if (h2c->dfl != 4) {
1619 error = H2_ERR_FRAME_SIZE_ERROR;
1620 goto conn_err;
1621 }
1622
1623 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001624 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001625 return 0;
1626
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001627 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001628
1629 if (h2c->dsi != 0) {
1630 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001631
1632 /* it's not an error to receive WU on a closed stream */
1633 if (h2s->st == H2_SS_CLOSED)
1634 return 1;
1635
1636 if (!inc) {
1637 error = H2_ERR_PROTOCOL_ERROR;
1638 goto strm_err;
1639 }
1640
1641 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1642 error = H2_ERR_FLOW_CONTROL_ERROR;
1643 goto strm_err;
1644 }
1645
1646 h2s->mws += inc;
1647 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1648 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001649 if (h2s->send_wait)
1650 LIST_ADDQ(&h2c->send_list, &h2s->list);
1651
Willy Tarreau26f95952017-07-27 17:18:30 +02001652 }
1653 }
1654 else {
1655 /* connection window update */
1656 if (!inc) {
1657 error = H2_ERR_PROTOCOL_ERROR;
1658 goto conn_err;
1659 }
1660
1661 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1662 error = H2_ERR_FLOW_CONTROL_ERROR;
1663 goto conn_err;
1664 }
1665
1666 h2c->mws += inc;
1667 }
1668
1669 return 1;
1670
1671 conn_err:
1672 h2c_error(h2c, error);
1673 return 0;
1674
1675 strm_err:
1676 if (h2s) {
1677 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001678 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001679 }
1680 else
1681 h2c_error(h2c, error);
1682 return 0;
1683}
1684
Willy Tarreaue96b0922017-10-30 00:28:29 +01001685/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1686 * the last ID. Returns > 0 on success or zero on missing data. It may return
1687 * an error in h2c. Described in RFC7540#6.8.
1688 */
1689static int h2c_handle_goaway(struct h2c *h2c)
1690{
1691 int error;
1692 int last;
1693
1694 if (h2c->dsi != 0) {
1695 error = H2_ERR_PROTOCOL_ERROR;
1696 goto conn_err;
1697 }
1698
1699 if (h2c->dfl < 8) {
1700 error = H2_ERR_FRAME_SIZE_ERROR;
1701 goto conn_err;
1702 }
1703
1704 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001705 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001706 return 0;
1707
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001708 last = h2_get_n32(&h2c->dbuf, 0);
1709 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001710 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001711 if (h2c->last_sid < 0)
1712 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001713 return 1;
1714
1715 conn_err:
1716 h2c_error(h2c, error);
1717 return 0;
1718}
1719
Willy Tarreau92153fc2017-12-03 19:46:19 +01001720/* processes a PRIORITY frame, and either skips it or rejects if it is
1721 * invalid. Returns > 0 on success or zero on missing data. It may return
1722 * an error in h2c. Described in RFC7540#6.3.
1723 */
1724static int h2c_handle_priority(struct h2c *h2c)
1725{
1726 int error;
1727
1728 if (h2c->dsi == 0) {
1729 error = H2_ERR_PROTOCOL_ERROR;
1730 goto conn_err;
1731 }
1732
1733 if (h2c->dfl != 5) {
1734 error = H2_ERR_FRAME_SIZE_ERROR;
1735 goto conn_err;
1736 }
1737
1738 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001739 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001740 return 0;
1741
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001742 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001743 /* 7540#5.3 : can't depend on itself */
1744 error = H2_ERR_PROTOCOL_ERROR;
1745 goto conn_err;
1746 }
1747 return 1;
1748
1749 conn_err:
1750 h2c_error(h2c, error);
1751 return 0;
1752}
1753
Willy Tarreaucd234e92017-08-18 10:59:39 +02001754/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1755 * Returns > 0 on success or zero on missing data. It may return an error in
1756 * h2c. Described in RFC7540#6.4.
1757 */
1758static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1759{
1760 int error;
1761
1762 if (h2c->dsi == 0) {
1763 error = H2_ERR_PROTOCOL_ERROR;
1764 goto conn_err;
1765 }
1766
Willy Tarreaucd234e92017-08-18 10:59:39 +02001767 if (h2c->dfl != 4) {
1768 error = H2_ERR_FRAME_SIZE_ERROR;
1769 goto conn_err;
1770 }
1771
1772 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001773 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001774 return 0;
1775
1776 /* late RST, already handled */
1777 if (h2s->st == H2_SS_CLOSED)
1778 return 1;
1779
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001780 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001781 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001782
1783 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001784 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001785 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001786 }
1787
1788 h2s->flags |= H2_SF_RST_RCVD;
1789 return 1;
1790
1791 conn_err:
1792 h2c_error(h2c, error);
1793 return 0;
1794}
1795
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001796/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1797 * It may return an error in h2c or h2s. The caller must consider that the
1798 * return value is the new h2s in case one was allocated (most common case).
1799 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001800 * errors here are reported as connection errors since it's impossible to
1801 * recover from such errors after the compression context has been altered.
1802 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001803static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001804{
1805 int error;
1806
1807 if (!h2c->dfl) {
1808 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001809 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001810 goto strm_err;
1811 }
1812
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001813 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001814 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001815
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001816 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001817 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001818
Willy Tarreauf2101912018-07-19 10:11:38 +02001819 if (h2c->flags & H2_CF_DEM_TOOMANY)
1820 return 0; // too many cs still present
1821
Willy Tarreau13278b42017-10-13 19:23:14 +02001822 /* now either the frame is complete or the buffer is complete */
1823 if (h2s->st != H2_SS_IDLE) {
1824 /* FIXME: stream already exists, this is only allowed for
1825 * trailers (not supported for now).
1826 */
1827 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001828 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001829 goto conn_err;
1830 }
1831 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1832 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1833 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001834 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001835 goto conn_err;
1836 }
1837
Willy Tarreau22de8d32018-09-05 19:55:58 +02001838 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001839 * positively from h2c_frt_stream_new(), the stream will report the error,
1840 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001841 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001842 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001843 if (!h2s) {
1844 error = H2_ERR_INTERNAL_ERROR;
1845 goto conn_err;
1846 }
1847
1848 h2s->st = H2_SS_OPEN;
1849 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1850 h2s->st = H2_SS_HREM;
1851 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001852 /* note: cs cannot be null for now (just created above) */
1853 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001854 }
1855
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001856 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001857 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001858
Willy Tarreau8f650c32017-11-21 19:36:21 +01001859 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001860 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001861
Willy Tarreau721c9742017-11-07 11:05:42 +01001862 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001863 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001864 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001865 }
1866 else {
1867 /* update the max stream ID if the request is being processed */
1868 if (h2s->id > h2c->max_id)
1869 h2c->max_id = h2s->id;
1870 }
1871
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001872 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001873
1874 conn_err:
1875 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001876 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001877
1878 strm_err:
1879 if (h2s) {
1880 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001881 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001882 }
1883 else
1884 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001885 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001886}
1887
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001888/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1889 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1890 * errors here are reported as connection errors since it's impossible to
1891 * recover from such errors after the compression context has been altered.
1892 */
1893static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1894{
1895 int error;
1896
1897 if (!h2c->dfl) {
1898 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1899 sess_log(h2c->conn->owner);
1900 goto strm_err;
1901 }
1902
1903 if (!b_size(&h2c->dbuf))
1904 return NULL; // empty buffer
1905
1906 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1907 return NULL; // incomplete frame
1908
1909 if (h2c->flags & H2_CF_DEM_TOOMANY)
1910 return 0; // too many cs still present
1911
1912 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1913 h2s->flags |= H2_SF_ES_RCVD;
1914 h2s->cs->flags |= CS_FL_REOS;
1915 }
1916
1917 if (!h2s_decode_headers(h2s))
1918 return NULL;
1919
1920 if (h2c->st0 >= H2_CS_ERROR)
1921 return NULL;
1922
1923 if (h2s->st >= H2_SS_ERROR) {
1924 /* stream error : send RST_STREAM */
1925 h2c->st0 = H2_CS_FRAME_E;
1926 }
1927
1928 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1929 h2s->st = H2_SS_ERROR;
1930 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1931 h2s->st = H2_SS_HREM;
1932 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1933 h2s_close(h2s);
1934
1935 return h2s;
1936
1937 conn_err:
1938 h2c_error(h2c, error);
1939 return NULL;
1940
1941 strm_err:
1942 if (h2s) {
1943 h2s_error(h2s, error);
1944 h2c->st0 = H2_CS_FRAME_E;
1945 }
1946 else
1947 h2c_error(h2c, error);
1948 return NULL;
1949}
1950
Willy Tarreau454f9052017-10-26 19:40:35 +02001951/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1952 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1953 */
1954static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1955{
1956 int error;
1957
1958 /* note that empty DATA frames are perfectly valid and sometimes used
1959 * to signal an end of stream (with the ES flag).
1960 */
1961
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001962 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001963 return 0; // empty buffer
1964
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001965 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001966 return 0; // incomplete frame
1967
1968 /* now either the frame is complete or the buffer is complete */
1969
1970 if (!h2c->dsi) {
1971 /* RFC7540#6.1 */
1972 error = H2_ERR_PROTOCOL_ERROR;
1973 goto conn_err;
1974 }
1975
1976 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1977 /* RFC7540#6.1 */
1978 error = H2_ERR_STREAM_CLOSED;
1979 goto strm_err;
1980 }
1981
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001982 if (!h2_frt_transfer_data(h2s))
1983 return 0;
1984
Willy Tarreau454f9052017-10-26 19:40:35 +02001985 /* call the upper layers to process the frame, then let the upper layer
1986 * notify the stream about any change.
1987 */
1988 if (!h2s->cs) {
1989 error = H2_ERR_STREAM_CLOSED;
1990 goto strm_err;
1991 }
1992
Willy Tarreau8f650c32017-11-21 19:36:21 +01001993 if (h2c->st0 >= H2_CS_ERROR)
1994 return 0;
1995
Willy Tarreau721c9742017-11-07 11:05:42 +01001996 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001997 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001998 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001999 }
2000
2001 /* check for completion : the callee will change this to FRAME_A or
2002 * FRAME_H once done.
2003 */
2004 if (h2c->st0 == H2_CS_FRAME_P)
2005 return 0;
2006
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002007
2008 /* last frame */
2009 if (h2c->dff & H2_F_DATA_END_STREAM) {
2010 h2s->st = H2_SS_HREM;
2011 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002012 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002013 }
2014
Willy Tarreau454f9052017-10-26 19:40:35 +02002015 return 1;
2016
2017 conn_err:
2018 h2c_error(h2c, error);
2019 return 0;
2020
2021 strm_err:
2022 if (h2s) {
2023 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002024 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002025 }
2026 else
2027 h2c_error(h2c, error);
2028 return 0;
2029}
2030
Willy Tarreaubc933932017-10-09 16:21:43 +02002031/* process Rx frames to be demultiplexed */
2032static void h2_process_demux(struct h2c *h2c)
2033{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002034 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002035
Willy Tarreau081d4722017-05-16 21:51:05 +02002036 if (h2c->st0 >= H2_CS_ERROR)
2037 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002038
2039 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2040 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002041 if (h2c->flags & H2_CF_IS_BACK)
2042 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002043 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2044 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002045 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002046 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002047 sess_log(h2c->conn->owner);
2048 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002049 goto fail;
2050 }
2051
2052 h2c->max_id = 0;
2053 h2c->st0 = H2_CS_SETTINGS1;
2054 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002055
2056 if (h2c->st0 == H2_CS_SETTINGS1) {
2057 struct h2_fh hdr;
2058
2059 /* ensure that what is pending is a valid SETTINGS frame
2060 * without an ACK.
2061 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002062 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002063 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002064 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002065 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002066 sess_log(h2c->conn->owner);
2067 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002068 goto fail;
2069 }
2070
2071 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2072 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2073 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2074 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002075 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002076 goto fail;
2077 }
2078
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002079 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002080 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2081 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2082 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002083 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002084 goto fail;
2085 }
2086
2087 /* that's OK, switch to FRAME_P to process it */
2088 h2c->dfl = hdr.len;
2089 h2c->dsi = hdr.sid;
2090 h2c->dft = hdr.ft;
2091 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002092 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002093 h2c->st0 = H2_CS_FRAME_P;
2094 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002095 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002096
2097 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002098 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002099 int ret = 0;
2100
2101 if (h2c->st0 >= H2_CS_ERROR)
2102 break;
2103
2104 if (h2c->st0 == H2_CS_FRAME_H) {
2105 struct h2_fh hdr;
2106
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002107 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002108 break;
2109
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002110 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002111 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2112 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002113 if (!h2c->nb_streams) {
2114 /* only log if no other stream can report the error */
2115 sess_log(h2c->conn->owner);
2116 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002117 break;
2118 }
2119
2120 h2c->dfl = hdr.len;
2121 h2c->dsi = hdr.sid;
2122 h2c->dft = hdr.ft;
2123 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002124 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002125 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002126 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002127 }
2128
2129 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002130 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2131
Willy Tarreau567beb82018-12-18 16:52:44 +01002132 if (tmp_h2s != h2s && h2s && h2s->cs &&
2133 (b_data(&h2s->rxbuf) ||
2134 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002135 /* we may have to signal the upper layers */
2136 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002137 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002138 }
2139 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002140
Willy Tarreaud7901432017-12-29 11:34:40 +01002141 if (h2c->st0 == H2_CS_FRAME_E)
2142 goto strm_err;
2143
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002144 if (h2s->st == H2_SS_IDLE &&
2145 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2146 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2147 * this state MUST be treated as a connection error
2148 */
2149 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2150 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002151 if (!h2c->nb_streams) {
2152 /* only log if no other stream can report the error */
2153 sess_log(h2c->conn->owner);
2154 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002155 break;
2156 }
2157
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002158 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2159 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2160 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2161 * this state MUST be treated as a stream error
2162 */
2163 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2164 goto strm_err;
2165 }
2166
Willy Tarreauab837502017-12-27 15:07:30 +01002167 /* Below the management of frames received in closed state is a
2168 * bit hackish because the spec makes strong differences between
2169 * streams closed by receiving RST, sending RST, and seeing ES
2170 * in both directions. In addition to this, the creation of a
2171 * new stream reusing the identifier of a closed one will be
2172 * detected here. Given that we cannot keep track of all closed
2173 * streams forever, we consider that unknown closed streams were
2174 * closed on RST received, which allows us to respond with an
2175 * RST without breaking the connection (eg: to abort a transfer).
2176 * Some frames have to be silently ignored as well.
2177 */
2178 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2179 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2180 /* #5.1.1: The identifier of a newly
2181 * established stream MUST be numerically
2182 * greater than all streams that the initiating
2183 * endpoint has opened or reserved. This
2184 * governs streams that are opened using a
2185 * HEADERS frame and streams that are reserved
2186 * using PUSH_PROMISE. An endpoint that
2187 * receives an unexpected stream identifier
2188 * MUST respond with a connection error.
2189 */
2190 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2191 goto strm_err;
2192 }
2193
2194 if (h2s->flags & H2_SF_RST_RCVD) {
2195 /* RFC7540#5.1:closed: an endpoint that
2196 * receives any frame other than PRIORITY after
2197 * receiving a RST_STREAM MUST treat that as a
2198 * stream error of type STREAM_CLOSED.
2199 *
2200 * Note that old streams fall into this category
2201 * and will lead to an RST being sent.
2202 */
2203 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2204 h2c->st0 = H2_CS_FRAME_E;
2205 goto strm_err;
2206 }
2207
2208 /* RFC7540#5.1:closed: if this state is reached as a
2209 * result of sending a RST_STREAM frame, the peer that
2210 * receives the RST_STREAM might have already sent
2211 * frames on the stream that cannot be withdrawn. An
2212 * endpoint MUST ignore frames that it receives on
2213 * closed streams after it has sent a RST_STREAM
2214 * frame. An endpoint MAY choose to limit the period
2215 * over which it ignores frames and treat frames that
2216 * arrive after this time as being in error.
2217 */
2218 if (!(h2s->flags & H2_SF_RST_SENT)) {
2219 /* RFC7540#5.1:closed: any frame other than
2220 * PRIO/WU/RST in this state MUST be treated as
2221 * a connection error
2222 */
2223 if (h2c->dft != H2_FT_RST_STREAM &&
2224 h2c->dft != H2_FT_PRIORITY &&
2225 h2c->dft != H2_FT_WINDOW_UPDATE) {
2226 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2227 goto strm_err;
2228 }
2229 }
2230 }
2231
Willy Tarreauc0da1962017-10-30 18:38:00 +01002232#if 0
2233 // problem below: it is not possible to completely ignore such
2234 // streams as we need to maintain the compression state as well
2235 // and for this we need to completely process these frames (eg:
2236 // HEADERS frames) as well as counting DATA frames to emit
2237 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2238 // This is a typical case of layer violation where the
2239 // transported contents are critical to the connection's
2240 // validity and must be ignored at the same time :-(
2241
2242 /* graceful shutdown, ignore streams whose ID is higher than
2243 * the one advertised in GOAWAY. RFC7540#6.8.
2244 */
2245 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002246 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2247 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002248 h2c->dfl -= ret;
2249 ret = h2c->dfl == 0;
2250 goto strm_err;
2251 }
2252#endif
2253
Willy Tarreau7e98c052017-10-10 15:56:59 +02002254 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002255 case H2_FT_SETTINGS:
2256 if (h2c->st0 == H2_CS_FRAME_P)
2257 ret = h2c_handle_settings(h2c);
2258
2259 if (h2c->st0 == H2_CS_FRAME_A)
2260 ret = h2c_ack_settings(h2c);
2261 break;
2262
Willy Tarreaucf68c782017-10-10 17:11:41 +02002263 case H2_FT_PING:
2264 if (h2c->st0 == H2_CS_FRAME_P)
2265 ret = h2c_handle_ping(h2c);
2266
2267 if (h2c->st0 == H2_CS_FRAME_A)
2268 ret = h2c_ack_ping(h2c);
2269 break;
2270
Willy Tarreau26f95952017-07-27 17:18:30 +02002271 case H2_FT_WINDOW_UPDATE:
2272 if (h2c->st0 == H2_CS_FRAME_P)
2273 ret = h2c_handle_window_update(h2c, h2s);
2274 break;
2275
Willy Tarreau61290ec2017-10-17 08:19:21 +02002276 case H2_FT_CONTINUATION:
2277 /* we currently don't support CONTINUATION frames since
2278 * we have nowhere to store the partial HEADERS frame.
2279 * Let's abort the stream on an INTERNAL_ERROR here.
2280 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002281 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002282 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002283 h2c->st0 = H2_CS_FRAME_E;
2284 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002285 break;
2286
Willy Tarreau13278b42017-10-13 19:23:14 +02002287 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002288 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002289 if (h2c->flags & H2_CF_IS_BACK)
2290 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2291 else
2292 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002293 if (tmp_h2s) {
2294 h2s = tmp_h2s;
2295 ret = 1;
2296 }
2297 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002298 break;
2299
Willy Tarreau454f9052017-10-26 19:40:35 +02002300 case H2_FT_DATA:
2301 if (h2c->st0 == H2_CS_FRAME_P)
2302 ret = h2c_frt_handle_data(h2c, h2s);
2303
2304 if (h2c->st0 == H2_CS_FRAME_A)
2305 ret = h2c_send_strm_wu(h2c);
2306 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002307
Willy Tarreau92153fc2017-12-03 19:46:19 +01002308 case H2_FT_PRIORITY:
2309 if (h2c->st0 == H2_CS_FRAME_P)
2310 ret = h2c_handle_priority(h2c);
2311 break;
2312
Willy Tarreaucd234e92017-08-18 10:59:39 +02002313 case H2_FT_RST_STREAM:
2314 if (h2c->st0 == H2_CS_FRAME_P)
2315 ret = h2c_handle_rst_stream(h2c, h2s);
2316 break;
2317
Willy Tarreaue96b0922017-10-30 00:28:29 +01002318 case H2_FT_GOAWAY:
2319 if (h2c->st0 == H2_CS_FRAME_P)
2320 ret = h2c_handle_goaway(h2c);
2321 break;
2322
Willy Tarreau1c661982017-10-30 13:52:01 +01002323 case H2_FT_PUSH_PROMISE:
2324 /* not permitted here, RFC7540#5.1 */
2325 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002326 if (!h2c->nb_streams) {
2327 /* only log if no other stream can report the error */
2328 sess_log(h2c->conn->owner);
2329 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002330 break;
2331
2332 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002333 default:
2334 /* drop frames that we ignore. They may be larger than
2335 * the buffer so we drain all of their contents until
2336 * we reach the end.
2337 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002338 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2339 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002340 h2c->dfl -= ret;
2341 ret = h2c->dfl == 0;
2342 }
2343
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002344 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002345 /* We may have to send an RST if not done yet */
2346 if (h2s->st == H2_SS_ERROR)
2347 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002348
Willy Tarreaua20a5192017-12-27 11:02:06 +01002349 if (h2c->st0 == H2_CS_FRAME_E)
2350 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002351
Willy Tarreau7e98c052017-10-10 15:56:59 +02002352 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002353 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002354 break;
2355
2356 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002357 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002358 h2c->st0 = H2_CS_FRAME_H;
2359 }
2360 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002361
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002362 if (h2c->rcvd_c > 0 &&
2363 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2364 h2c_send_conn_wu(h2c);
2365
Willy Tarreau52eed752017-09-22 15:05:09 +02002366 fail:
2367 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002368 if (h2s && h2s->cs &&
2369 (b_data(&h2s->rxbuf) ||
2370 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002371 /* we may have to signal the upper layers */
2372 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002373 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002374 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002375
2376 if (h2_recv_allowed(h2c))
2377 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002378}
2379
2380/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2381 * the end.
2382 */
2383static int h2_process_mux(struct h2c *h2c)
2384{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002385 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002386
Willy Tarreau01b44822018-10-03 14:26:37 +02002387 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2388 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2389 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2390 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2391 if (h2c->st0 == H2_CS_ERROR) {
2392 h2c->st0 = H2_CS_ERROR2;
2393 sess_log(h2c->conn->owner);
2394 }
2395 goto fail;
2396 }
2397 h2c->st0 = H2_CS_SETTINGS1;
2398 }
2399 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002400 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002401 return 1;
2402 }
2403
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002404 /* start by sending possibly pending window updates */
2405 if (h2c->rcvd_c > 0 &&
2406 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2407 h2c_send_conn_wu(h2c) < 0)
2408 goto fail;
2409
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002410 /* First we always process the flow control list because the streams
2411 * waiting there were already elected for immediate emission but were
2412 * blocked just on this.
2413 */
2414
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002415 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002416 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2417 h2c->st0 >= H2_CS_ERROR)
2418 break;
2419
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002420 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002421 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2422 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002423 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002424 LIST_DEL(&h2s->list);
2425 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002426 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002427 }
2428
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002429 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002430 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2431 break;
2432
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002433 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002434 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2435 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002436 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002437 LIST_DEL(&h2s->list);
2438 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002439 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002440 }
2441
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002442 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002443 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002444 if (h2c->st0 == H2_CS_ERROR) {
2445 if (h2c->max_id >= 0) {
2446 h2c_send_goaway_error(h2c, NULL);
2447 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2448 return 0;
2449 }
2450
2451 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2452 }
2453 return 1;
2454 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002455 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002456}
2457
Willy Tarreau62f52692017-10-08 23:01:42 +02002458
Willy Tarreau479998a2018-11-18 06:30:59 +01002459/* Attempt to read data, and subscribe if none available.
2460 * The function returns 1 if data has been received, otherwise zero.
2461 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002462static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002463{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002464 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002465 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002466 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002467 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002468
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002469 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002470 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002471
Willy Tarreau315d8072017-12-10 22:17:57 +01002472 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002473 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002474
Willy Tarreau44e973f2018-03-01 17:49:30 +01002475 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002476 if (!buf) {
2477 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002478 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002479 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002480
Olivier Houchard7505f942018-08-21 18:10:44 +02002481 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002482 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002483 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2484 /* HTX in use : try to pre-align the buffer like the
2485 * rxbufs will be to optimize memory copies. We'll make
2486 * sure that the frame header lands at the end of the
2487 * HTX block to alias it upon recv. We cannot use the
2488 * head because rcv_buf() will realign the buffer if
2489 * it's empty. Thus we cheat and pretend we already
2490 * have a few bytes there.
2491 */
2492 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002493 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002494 }
2495 else
2496 max = b_room(buf);
2497
Olivier Houchard7505f942018-08-21 18:10:44 +02002498 if (max)
2499 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2500 else
2501 ret = 0;
2502 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002503
Olivier Houchard53216e72018-10-10 15:46:36 +02002504 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002505 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002506
Olivier Houcharda1411e62018-08-17 18:42:48 +02002507 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002508 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002509 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002510 }
2511
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002512 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002513 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002514 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002515}
2516
Willy Tarreau479998a2018-11-18 06:30:59 +01002517/* Try to send data if possible.
2518 * The function returns 1 if data have been sent, otherwise zero.
2519 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002520static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002521{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002522 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002523 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002524 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002525
2526 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002527 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002528
Olivier Houchard7505f942018-08-21 18:10:44 +02002529
Willy Tarreaua2af5122017-10-09 11:56:46 +02002530 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2531 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002532 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002533 }
2534
Willy Tarreaubc933932017-10-09 16:21:43 +02002535 /* This loop is quite simple : it tries to fill as much as it can from
2536 * pending streams into the existing buffer until it's reportedly full
2537 * or the end of send requests is reached. Then it tries to send this
2538 * buffer's contents out, marks it not full if at least one byte could
2539 * be sent, and tries again.
2540 *
2541 * The snd_buf() function normally takes a "flags" argument which may
2542 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2543 * data immediately comes and CO_SFL_STREAMER to indicate that the
2544 * connection is streaming lots of data (used to increase TLS record
2545 * size at the expense of latency). The former can be sent any time
2546 * there's a buffer full flag, as it indicates at least one stream
2547 * attempted to send and failed so there are pending data. An
2548 * alternative would be to set it as long as there's an active stream
2549 * but that would be problematic for ACKs until we have an absolute
2550 * guarantee that all waiters have at least one byte to send. The
2551 * latter should possibly not be set for now.
2552 */
2553
2554 done = 0;
2555 while (!done) {
2556 unsigned int flags = 0;
2557
2558 /* fill as much as we can into the current buffer */
2559 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2560 done = h2_process_mux(h2c);
2561
2562 if (conn->flags & CO_FL_ERROR)
2563 break;
2564
2565 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2566 flags |= CO_SFL_MSG_MORE;
2567
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002568 if (b_data(&h2c->mbuf)) {
2569 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002570 if (!ret)
2571 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002572 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002573 b_del(&h2c->mbuf, ret);
2574 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002575 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002576
2577 /* wrote at least one byte, the buffer is not full anymore */
2578 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2579 }
2580
Willy Tarreaua2af5122017-10-09 11:56:46 +02002581 if (conn->flags & CO_FL_SOCK_WR_SH) {
2582 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002583 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002584 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002585 /* We're not full anymore, so we can wake any task that are waiting
2586 * for us.
2587 */
2588 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002589 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002590 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2591 struct h2s *, list);
2592 LIST_DEL(&h2s->list);
2593 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002594 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002595 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2596 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002597 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002598 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002599 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002600 /* We're done, no more to send */
2601 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002602 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002603schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002604 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2605 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002606 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002607}
2608
2609static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2610{
2611 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002612 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002613
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002614 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002615 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002616 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002617 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002618 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002619 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002620 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002621}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002622
Willy Tarreau62f52692017-10-08 23:01:42 +02002623/* callback called on any event by the connection handler.
2624 * It applies changes and returns zero, or < 0 if it wants immediate
2625 * destruction of the connection (which normally doesn not happen in h2).
2626 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002627static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002628{
Olivier Houchard7505f942018-08-21 18:10:44 +02002629 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002630
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002631 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002632 h2_process_demux(h2c);
2633
2634 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002635 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002636
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002637 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002638 h2c->flags &= ~H2_CF_DEM_DFULL;
2639 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002640 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002641
Willy Tarreau0b37d652018-10-03 10:33:02 +02002642 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002643 /* frontend is stopping, reload likely in progress, let's try
2644 * to announce a graceful shutdown if not yet done. We don't
2645 * care if it fails, it will be tried again later.
2646 */
2647 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2648 if (h2c->last_sid < 0)
2649 h2c->last_sid = (1U << 31) - 1;
2650 h2c_send_goaway_error(h2c, NULL);
2651 }
2652 }
2653
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002654 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002655 * If we received early data, and the handshake is done, wake
2656 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002657 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002658 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2659 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2660 struct eb32_node *node;
2661 struct h2s *h2s;
2662
2663 h2c->flags |= H2_CF_WAIT_FOR_HS;
2664 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2665
2666 while (node) {
2667 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002668 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002669 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002670 node = eb32_next(node);
2671 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002672 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002673
Willy Tarreau26bd7612017-10-09 16:47:04 +02002674 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002675 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2676 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2677 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002678 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002679
2680 if (eb_is_empty(&h2c->streams_by_id)) {
2681 /* no more stream, kill the connection now */
2682 h2_release(conn);
2683 return -1;
2684 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002685 }
2686
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002687 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002688 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002689
Olivier Houchard53216e72018-10-10 15:46:36 +02002690 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2691 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2692 (h2c->st0 != H2_CS_ERROR &&
2693 !b_data(&h2c->mbuf) &&
2694 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2695 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002696 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002697
Willy Tarreau3f133572017-10-31 19:21:06 +01002698 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002699 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002700 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002701 task_queue(h2c->task);
2702 }
2703 else
2704 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002705 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002706
Olivier Houchard7505f942018-08-21 18:10:44 +02002707 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002708 return 0;
2709}
2710
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002711static int h2_wake(struct connection *conn)
2712{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002713 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002714
2715 return (h2_process(h2c));
2716}
2717
Willy Tarreauea392822017-10-31 10:02:25 +01002718/* Connection timeout management. The principle is that if there's no receipt
2719 * nor sending for a certain amount of time, the connection is closed. If the
2720 * MUX buffer still has lying data or is not allocatable, the connection is
2721 * immediately killed. If it's allocatable and empty, we attempt to send a
2722 * GOAWAY frame.
2723 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002724static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002725{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002726 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002727 int expired = tick_is_expired(t->expire, now_ms);
2728
Willy Tarreau0975f112018-03-29 15:22:59 +02002729 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002730 return t;
2731
Willy Tarreau0975f112018-03-29 15:22:59 +02002732 task_delete(t);
2733 task_free(t);
2734
2735 if (!h2c) {
2736 /* resources were already deleted */
2737 return NULL;
2738 }
2739
2740 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002741 h2c_error(h2c, H2_ERR_NO_ERROR);
2742 h2_wake_some_streams(h2c, 0, 0);
2743
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002744 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002745 /* don't even try to send a GOAWAY, the buffer is stuck */
2746 h2c->flags |= H2_CF_GOAWAY_FAILED;
2747 }
2748
2749 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002750 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002751 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2752 h2c->flags |= H2_CF_GOAWAY_FAILED;
2753
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002754 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2755 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002756 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002757 b_del(&h2c->mbuf, ret);
2758 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002759 }
2760 }
Willy Tarreauea392822017-10-31 10:02:25 +01002761
Willy Tarreau0975f112018-03-29 15:22:59 +02002762 /* either we can release everything now or it will be done later once
2763 * the last stream closes.
2764 */
2765 if (eb_is_empty(&h2c->streams_by_id))
2766 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002767
Willy Tarreauea392822017-10-31 10:02:25 +01002768 return NULL;
2769}
2770
2771
Willy Tarreau62f52692017-10-08 23:01:42 +02002772/*******************************************/
2773/* functions below are used by the streams */
2774/*******************************************/
2775
2776/*
2777 * Attach a new stream to a connection
2778 * (Used for outgoing connections)
2779 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002780static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002781{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002782 struct conn_stream *cs;
2783 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002784 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002785
2786 cs = cs_new(conn);
2787 if (!cs)
2788 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002789 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002790 if (!h2s) {
2791 cs_free(cs);
2792 return NULL;
2793 }
2794 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002795}
2796
Willy Tarreaufafd3982018-11-18 21:29:20 +01002797/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2798 * We have to scan because we may have some orphan streams. It might be
2799 * beneficial to scan backwards from the end to reduce the likeliness to find
2800 * orphans.
2801 */
2802static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2803{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002804 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002805 struct h2s *h2s;
2806 struct eb32_node *node;
2807
2808 node = eb32_first(&h2c->streams_by_id);
2809 while (node) {
2810 h2s = container_of(node, struct h2s, by_id);
2811 if (h2s->cs)
2812 return h2s->cs;
2813 node = eb32_next(node);
2814 }
2815 return NULL;
2816}
2817
Willy Tarreau62f52692017-10-08 23:01:42 +02002818/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002819 * Destroy the mux and the associated connection, if it is no longer used
2820 */
2821static void h2_destroy(struct connection *conn)
2822{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002823 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002824
2825 if (eb_is_empty(&h2c->streams_by_id))
2826 h2_release(h2c->conn);
2827}
2828
2829/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002830 * Detach the stream from the connection and possibly release the connection.
2831 */
2832static void h2_detach(struct conn_stream *cs)
2833{
Willy Tarreau60935142017-10-16 18:11:19 +02002834 struct h2s *h2s = cs->ctx;
2835 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002836 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002837
2838 cs->ctx = NULL;
2839 if (!h2s)
2840 return;
2841
Olivier Houchardf502aca2018-12-14 19:42:40 +01002842 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002843 h2c = h2s->h2c;
2844 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002845 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002846 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2847 !h2_has_too_many_cs(h2c)) {
2848 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002849 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002850 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002851 }
Willy Tarreau60935142017-10-16 18:11:19 +02002852
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002853 /* this stream may be blocked waiting for some data to leave (possibly
2854 * an ES or RST frame), so orphan it in this case.
2855 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002856 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002857 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002858 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002859 return;
2860
Willy Tarreau45f752e2017-10-30 15:44:59 +01002861 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2862 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2863 /* unblock the connection if it was blocked on this
2864 * stream.
2865 */
2866 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2867 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002868 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002869 }
2870
Willy Tarreau71049cc2018-03-28 13:56:39 +02002871 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002872
Olivier Houchard8a786902018-12-15 16:05:40 +01002873 if (h2c->flags & H2_CF_IS_BACK &&
2874 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002875 if (!(h2c->conn->flags &
2876 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2877 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002878 h2c->conn->owner = sess;
2879 session_add_conn(sess, h2c->conn, h2c->conn->target);
Olivier Houchard8a786902018-12-15 16:05:40 +01002880 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002881 if (eb_is_empty(&h2c->streams_by_id)) {
2882 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2883 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2884 return;
2885 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002886 /* Never ever allow to reuse a connection from a non-reuse backend */
2887 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2888 h2c->conn->flags |= CO_FL_PRIVATE;
2889 if (LIST_ISEMPTY(&h2c->conn->list)) {
2890 struct server *srv = objt_server(h2c->conn->target);
2891
2892 if (srv) {
2893 if (h2c->conn->flags & CO_FL_PRIVATE)
2894 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2895 else
2896 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2897 }
2898
2899 }
2900 }
2901 }
2902
Willy Tarreaue323f342018-03-28 13:51:45 +02002903 /* We don't want to close right now unless we're removing the
2904 * last stream, and either the connection is in error, or it
2905 * reached the ID already specified in a GOAWAY frame received
2906 * or sent (as seen by last_sid >= 0).
2907 */
2908 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2909 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002910 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002911 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002912 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002913 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002914 (conn_xprt_read0_pending(h2c->conn) ||
2915 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2916 /* no more stream will come, kill it now */
2917 h2_release(h2c->conn);
2918 }
2919 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002920 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002921 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2922 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002923 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002924 else
2925 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002926 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002927}
2928
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002929static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002930{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002931 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002932 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002933
Willy Tarreau721c9742017-11-07 11:05:42 +01002934 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002935 return;
2936
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002937 /* if no outgoing data was seen on this stream, it means it was
2938 * closed with a "tcp-request content" rule that is normally
2939 * used to kill the connection ASAP (eg: limit abuse). In this
2940 * case we send a goaway to close the connection.
2941 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002942 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002943 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002944 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002945
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002946 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2947 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002948 h2c_send_goaway_error(h2c, h2s) <= 0)
2949 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002950
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002951 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002952 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002953 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002954
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002955 return;
2956add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002957 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002958 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002959 if (h2s->flags & H2_SF_BLK_MFCTL) {
2960 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2961 h2s->send_wait = sw;
2962 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2963 h2s->send_wait = sw;
2964 LIST_ADDQ(&h2c->send_list, &h2s->list);
2965 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002966 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002967 /* Let the handler know we want shutr */
2968 sw->handle = (void *)((long)sw->handle | 1);
2969
Willy Tarreau62f52692017-10-08 23:01:42 +02002970}
2971
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002972static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002973{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002974 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002975 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002976
Willy Tarreau721c9742017-11-07 11:05:42 +01002977 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002978 return;
2979
Willy Tarreau67434202017-11-06 20:20:51 +01002980 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002981 /* we can cleanly close using an empty data frame only after headers */
2982
2983 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2984 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002985 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002986
2987 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002988 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002989 else
2990 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002991 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002992 /* if no outgoing data was seen on this stream, it means it was
2993 * closed with a "tcp-request content" rule that is normally
2994 * used to kill the connection ASAP (eg: limit abuse). In this
2995 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002996 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002997 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002998 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002999 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003000
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003001 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3002 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003003 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003004 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003005
Willy Tarreau00dd0782018-03-01 16:31:34 +01003006 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003007 }
3008
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003009 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003010 tasklet_wakeup(h2c->wait_event.task);
3011 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003012
3013 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003014 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003015 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003016 if (h2s->flags & H2_SF_BLK_MFCTL) {
3017 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3018 h2s->send_wait = sw;
3019 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3020 h2s->send_wait = sw;
3021 LIST_ADDQ(&h2c->send_list, &h2s->list);
3022 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003023 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003024 /* let the handler know we want to shutw */
3025 sw->handle = (void *)((long)(sw->handle) | 2);
3026
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003027}
3028
3029static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3030{
3031 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003032 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003033
Olivier Houchard2c68a462018-12-15 22:42:20 +01003034 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003035 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003036 h2s->send_wait = NULL;
3037 LIST_DEL(&h2s->list);
3038 LIST_INIT(&h2s->list);
3039 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003040 if (reason & 2)
3041 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003042 if (reason & 1)
3043 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003044
Olivier Houchard2c68a462018-12-15 22:42:20 +01003045 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003046 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003047 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003048 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003049}
3050
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003051static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3052{
3053 struct h2s *h2s = cs->ctx;
3054
3055 if (!mode)
3056 return;
3057
3058 h2_do_shutr(h2s);
3059}
3060
3061static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3062{
3063 struct h2s *h2s = cs->ctx;
3064
3065 h2_do_shutw(h2s);
3066}
3067
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003068/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003069 * HTX request or response depending on the connection's side. Returns the
3070 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3071 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003072 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003073static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003074{
3075 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003076 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003077 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003078 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003079 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003080 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003081 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003082 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003083 int flen = h2c->dfl;
3084 int outlen = 0;
3085 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003086 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003087
3088 if (!h2c->dfl) {
3089 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003090 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003091 return 0;
3092 }
3093
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003094 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003095 return 0; // incomplete input frame
3096
Willy Tarreau13278b42017-10-13 19:23:14 +02003097 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003098 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003099 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003100 copy = alloc_trash_chunk();
3101 if (!copy) {
3102 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3103 goto fail;
3104 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003105 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3106 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3107 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003108 }
3109
3110 /* The padlen is the first byte before data, and the padding appears
3111 * after data. padlen+data+padding are included in flen.
3112 */
3113 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003114 h2c->dpl = *hdrs;
3115 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003116 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3117 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003118 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003119 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003120 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003121 hdrs += 1; // skip Pad Length
3122 }
3123
3124 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3125 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003126 if (read_n32(hdrs) == h2s->id) {
3127 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3128 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003129 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003130 }
3131
Willy Tarreau13278b42017-10-13 19:23:14 +02003132 hdrs += 5; // stream dep = 4, weight = 1
3133 flen -= 5;
3134 }
3135
3136 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3137 * don't support this for now and can't even decompress so we have to
3138 * break the connection.
3139 */
3140 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3141 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003142 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003143 }
3144
Olivier Houchard638b7992018-08-16 15:41:52 +02003145 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003146 if (!csbuf) {
3147 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003148 goto fail;
3149 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003150
Willy Tarreau937f7602018-02-26 15:22:17 +01003151 /* we can't retry a failed decompression operation so we must be very
3152 * careful not to take any risks. In practice the output buffer is
3153 * always empty except maybe for trailers, in which case we simply have
3154 * to wait for the upper layer to finish consuming what is available.
3155 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003156
3157 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3158 htx = htx_from_buf(&h2s->rxbuf);
3159 if (!htx_is_empty(htx))
3160 goto fail;
3161 } else {
3162 if (b_data(csbuf))
3163 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003164
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003165 csbuf->head = 0;
3166 try = b_size(csbuf);
3167 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003168
3169 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3170 sizeof(list)/sizeof(list[0]), tmp);
3171 if (outlen < 0) {
3172 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3173 goto fail;
3174 }
3175
3176 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003177 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003178
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003179 if (htx) {
3180 /* HTX mode */
3181 if (h2c->flags & H2_CF_IS_BACK)
3182 outlen = h2_make_htx_response(list, htx, &msgf);
3183 else
3184 outlen = h2_make_htx_request(list, htx, &msgf);
3185 } else {
3186 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003187 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003188 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003189
3190 if (outlen < 0) {
3191 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3192 goto fail;
3193 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003194
Willy Tarreau174b06a2018-04-25 18:13:58 +02003195 if (msgf & H2_MSGF_BODY) {
3196 /* a payload is present */
3197 if (msgf & H2_MSGF_BODY_CL)
3198 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003199 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003200 h2s->flags |= H2_SF_DATA_CHNK;
3201 }
3202
Willy Tarreau13278b42017-10-13 19:23:14 +02003203 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003204 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003205 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003206 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003207
Willy Tarreau39d68502018-03-02 12:26:37 +01003208 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003209 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003210 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003211 if (htx)
3212 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003213 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003214
Willy Tarreau68dd9852017-07-03 14:44:26 +02003215 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003216 if (htx)
3217 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003218 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003219 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003220 fail:
3221 outlen = 0;
3222 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003223}
3224
Willy Tarreau454f9052017-10-26 19:40:35 +02003225/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3226 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3227 * in use, a new chunk is emitted for each frame. This is supposed to fit
3228 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3229 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3230 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003231 * parser state is automatically updated. Returns > 0 if it could completely
3232 * send the current frame, 0 if it couldn't complete, in which case
3233 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3234 * DATA frame can return 0 as a valid result). Stream errors are reported in
3235 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3236 * have checked the frame header and ensured that the frame was complete or the
3237 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003238 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003239static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003240{
3241 struct h2c *h2c = h2s->h2c;
3242 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003243 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003244 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003245 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003246 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003247
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003248 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003249
3250 /* The padlen is the first byte before data, and the padding appears
3251 * after data. padlen+data+padding are included in flen.
3252 */
Willy Tarreau79127812017-12-03 21:06:59 +01003253 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003254 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003255 return 0;
3256
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003257 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003258 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003259 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3260 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003261 return 0;
3262 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003263
3264 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003265 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003266 h2c->dfl--;
3267 h2c->rcvd_c++; h2c->rcvd_s++;
3268 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003269 }
3270
Olivier Houchard638b7992018-08-16 15:41:52 +02003271 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003272 if (!csbuf) {
3273 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003274 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003275 }
3276
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003277try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003278 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003279 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3280 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003281 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003282 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003283
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003284 if (flen > b_data(&h2c->dbuf)) {
3285 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003286 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003287 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003288 }
3289
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003290 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003291 block1 = htx_free_data_space(htx);
3292 if (!block1) {
3293 h2c->flags |= H2_CF_DEM_SFULL;
3294 goto fail;
3295 }
3296 if (flen > block1)
3297 flen = block1;
3298
3299 /* here, flen is the max we can copy into the output buffer */
3300 block1 = b_contig_data(&h2c->dbuf, 0);
3301 if (flen > block1)
3302 flen = block1;
3303
3304 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3305 h2c->flags |= H2_CF_DEM_SFULL;
3306 goto fail;
3307 }
3308
3309 b_del(&h2c->dbuf, flen);
3310 h2c->dfl -= flen;
3311 h2c->rcvd_c += flen;
3312 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3313 goto try_again;
3314 }
3315 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003316 /* it doesn't fit and the buffer is fragmented,
3317 * so let's defragment it and try again.
3318 */
3319 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003320 }
3321
Willy Tarreaueba10f22018-04-25 20:44:22 +02003322 /* chunked-encoding requires more room */
3323 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003324 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003325 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3326 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3327 (chklen < 1048576) ? 4 : 8;
3328 chklen += 4; // CRLF, CRLF
3329 }
3330
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003331 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003332 if (flen + chklen > b_room(csbuf)) {
3333 if (chklen >= b_room(csbuf)) {
3334 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003335 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003336 }
3337 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003338 }
3339
3340 if (h2s->flags & H2_SF_DATA_CHNK) {
3341 /* emit the chunk size */
3342 unsigned int chksz = flen;
3343 char str[10];
3344 char *beg;
3345
3346 beg = str + sizeof(str);
3347 *--beg = '\n';
3348 *--beg = '\r';
3349 do {
3350 *--beg = hextab[chksz & 0xF];
3351 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003352 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003353 }
3354
Willy Tarreau454f9052017-10-26 19:40:35 +02003355 /* Block1 is the length of the first block before the buffer wraps,
3356 * block2 is the optional second block to reach the end of the frame.
3357 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003358 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003359 if (block1 > flen)
3360 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003361 block2 = flen - block1;
3362
3363 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003364 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003365
3366 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003367 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003368
Willy Tarreaueba10f22018-04-25 20:44:22 +02003369 if (h2s->flags & H2_SF_DATA_CHNK) {
3370 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003371 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003372 }
3373
Willy Tarreau454f9052017-10-26 19:40:35 +02003374 /* now mark the input data as consumed (will be deleted from the buffer
3375 * by the caller when seeing FRAME_A after sending the window update).
3376 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003377 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003378 h2c->dfl -= flen;
3379 h2c->rcvd_c += flen;
3380 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3381
3382 if (h2c->dfl > h2c->dpl) {
3383 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003384 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003385 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003386 }
3387
Willy Tarreau4a28da12018-01-04 14:41:00 +01003388 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003389 /* here we're done with the frame, all the payload (except padding) was
3390 * transferred.
3391 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003392
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003393 if (h2c->dff & H2_F_DATA_END_STREAM) {
3394 if (htx) {
3395 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3396 h2c->flags |= H2_CF_DEM_SFULL;
3397 goto fail;
3398 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003399 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003400 else if (h2s->flags & H2_SF_DATA_CHNK) {
3401 /* emit the trailing 0 CRLF CRLF */
3402 if (b_room(csbuf) < 5) {
3403 h2c->flags |= H2_CF_DEM_SFULL;
3404 goto fail;
3405 }
3406 chklen += 5;
3407 b_putblk(csbuf, "0\r\n\r\n", 5);
3408 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003409 }
3410
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003411 h2c->rcvd_c += h2c->dpl;
3412 h2c->rcvd_s += h2c->dpl;
3413 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003414 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3415
Willy Tarreau39d68502018-03-02 12:26:37 +01003416 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003417 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003418 h2s->cs->flags |= CS_FL_REOS;
3419 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003420 if (htx)
3421 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003422 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003423 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003424 if (htx)
3425 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003426 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003427}
3428
Willy Tarreau5dd17352018-06-14 13:33:30 +02003429/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3430 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3431 * number of bytes sent. The caller must check the stream's status to detect
3432 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003433 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003434static 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 +02003435{
3436 struct http_hdr list[MAX_HTTP_HDR];
3437 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003438 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003439 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003440 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003441 int es_now = 0;
3442 int ret = 0;
3443 int hdr;
3444
3445 if (h2c_mux_busy(h2c, h2s)) {
3446 h2s->flags |= H2_SF_BLK_MBUSY;
3447 return 0;
3448 }
3449
Willy Tarreau44e973f2018-03-01 17:49:30 +01003450 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003451 h2c->flags |= H2_CF_MUX_MALLOC;
3452 h2s->flags |= H2_SF_BLK_MROOM;
3453 return 0;
3454 }
3455
3456 /* First, try to parse the H1 response and index it into <list>.
3457 * NOTE! Since it comes from haproxy, we *know* that a response header
3458 * block does not wrap and we can safely read it this way without
3459 * having to realign the buffer.
3460 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003461 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003462 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003463 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003464 /* incomplete or invalid response, this is abnormal coming from
3465 * haproxy and may only result in a bad errorfile or bad Lua code
3466 * so that won't be fixed, raise an error now.
3467 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003468 * FIXME: we should instead add the ability to only return a
3469 * 502 bad gateway. But in theory this is not supposed to
3470 * happen.
3471 */
3472 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3473 ret = 0;
3474 goto end;
3475 }
3476
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003477 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003478
3479 /* certain statuses have no body or an empty one, regardless of
3480 * what the headers say.
3481 */
3482 if (sl.st.status >= 100 && sl.st.status < 200) {
3483 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3484 h1m->curr_len = h1m->body_len = 0;
3485 }
3486 else if (sl.st.status == 204 || sl.st.status == 304) {
3487 /* no contents, claim c-len is present and set to zero */
3488 h1m->flags &= ~H1_MF_CHNK;
3489 h1m->flags |= H1_MF_CLEN;
3490 h1m->curr_len = h1m->body_len = 0;
3491 }
3492
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003493 chunk_reset(&outbuf);
3494
3495 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003496 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003497 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003498 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003499
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003500 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003501 break;
3502 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003503 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003504 }
3505
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003506 if (outbuf.size < 9)
3507 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003508
3509 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003510 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3511 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3512 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003513
3514 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003515 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003516 /* this is an unparsable response */
3517 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3518 ret = 0;
3519 goto end;
3520 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003521
3522 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003523 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003524 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003525 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003526 }
3527
3528 /* encode all headers, stop at empty name */
3529 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003530 /* these ones do not exist in H2 and must be dropped. */
3531 if (isteq(list[hdr].n, ist("connection")) ||
3532 isteq(list[hdr].n, ist("proxy-connection")) ||
3533 isteq(list[hdr].n, ist("keep-alive")) ||
3534 isteq(list[hdr].n, ist("upgrade")) ||
3535 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003536 continue;
3537
3538 if (isteq(list[hdr].n, ist("")))
3539 break; // end
3540
3541 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3542 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003543 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003544 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003545 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003546 }
3547 }
3548
3549 /* we may need to add END_STREAM */
3550 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3551 es_now = 1;
3552
3553 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003554 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003555
3556 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003557 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003558
3559 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003560 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003561
3562 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003563 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003564 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003565
3566 /* for now we don't implemented CONTINUATION, so we wait for a
3567 * body or directly end in TRL2.
3568 */
3569 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003570 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003571 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003572
Willy Tarreau801250e2018-09-11 11:45:04 +02003573 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003574 h2s->flags |= H2_SF_ES_SENT;
3575 if (h2s->st == H2_SS_OPEN)
3576 h2s->st = H2_SS_HLOC;
3577 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003578 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003579 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003580 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003581 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003582 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003583 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003584 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003585 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003586 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003587
3588 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003589
3590 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003591 //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 +02003592 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003593 full:
3594 h1m_init_res(h1m);
3595 h1m->err_pos = -1; // don't care about errors on the response path
3596 h2c->flags |= H2_CF_MUX_MFULL;
3597 h2s->flags |= H2_SF_BLK_MROOM;
3598 ret = 0;
3599 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003600}
3601
Willy Tarreau5dd17352018-06-14 13:33:30 +02003602/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3603 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3604 * the number of bytes sent. The caller must check the stream's status to
3605 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003606 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003607static 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 +02003608{
3609 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003610 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003611 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003612 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003613 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003614 int es_now = 0;
3615 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003616 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003617 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003618
3619 if (h2c_mux_busy(h2c, h2s)) {
3620 h2s->flags |= H2_SF_BLK_MBUSY;
3621 goto end;
3622 }
3623
Willy Tarreau44e973f2018-03-01 17:49:30 +01003624 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003625 h2c->flags |= H2_CF_MUX_MALLOC;
3626 h2s->flags |= H2_SF_BLK_MROOM;
3627 goto end;
3628 }
3629
3630 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003631 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003632 goto end;
3633
3634 chunk_reset(&outbuf);
3635
3636 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003637 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003638 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003639 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003640
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003641 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003642 break;
3643 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003644 /* If there are pending data in the output buffer, and we have
3645 * less than 1/4 of the mbuf's size and everything fits, we'll
3646 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3647 * is full and wait, to save some slow realign calls.
3648 */
3649 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3650 h2c->flags |= H2_CF_MUX_MFULL;
3651 h2s->flags |= H2_SF_BLK_MROOM;
3652 goto end;
3653 }
3654
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003655 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003656 }
3657
3658 if (outbuf.size < 9) {
3659 h2c->flags |= H2_CF_MUX_MFULL;
3660 h2s->flags |= H2_SF_BLK_MROOM;
3661 goto end;
3662 }
3663
3664 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003665 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3666 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3667 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003668
3669 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3670 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003671 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003672 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003673 break;
3674 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003675 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003676 if ((long long)size > h1m->curr_len)
3677 size = h1m->curr_len;
3678 break;
3679 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003680 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003681 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003682 if (!ret)
3683 goto end;
3684
3685 if (ret < 0) {
3686 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003687 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003688 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3689 goto end;
3690 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003691 max -= ret;
3692 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003693 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003694 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003695 }
3696
Willy Tarreau801250e2018-09-11 11:45:04 +02003697 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003698 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003699 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003700 if (!ret)
3701 goto end;
3702
3703 if (ret < 0) {
3704 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003705 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003706 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3707 goto end;
3708 }
3709
3710 size = chunk;
3711 h1m->curr_len = chunk;
3712 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003713 max -= ret;
3714 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003715 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003716 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003717 if (!size)
3718 goto send_empty;
3719 }
3720
3721 /* in MSG_DATA state, continue below */
3722 size = h1m->curr_len;
3723 break;
3724 }
3725
3726 /* we have in <size> the exact number of bytes we need to copy from
3727 * the H1 buffer. We need to check this against the connection's and
3728 * the stream's send windows, and to ensure that this fits in the max
3729 * frame size and in the buffer's available space minus 9 bytes (for
3730 * the frame header). The connection's flow control is applied last so
3731 * that we can use a separate list of streams which are immediately
3732 * unblocked on window opening. Note: we don't implement padding.
3733 */
3734
Willy Tarreau5dd17352018-06-14 13:33:30 +02003735 if (size > max)
3736 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003737
3738 if (size > h2s->mws)
3739 size = h2s->mws;
3740
3741 if (size <= 0) {
3742 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003743 if (h2s->send_wait) {
3744 LIST_DEL(&h2s->list);
3745 LIST_INIT(&h2s->list);
3746 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003747 goto end;
3748 }
3749
3750 if (h2c->mfs && size > h2c->mfs)
3751 size = h2c->mfs;
3752
3753 if (size + 9 > outbuf.size) {
3754 /* we have an opportunity for enlarging the too small
3755 * available space, let's try.
3756 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003757 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003758 goto realign_again;
3759 size = outbuf.size - 9;
3760 }
3761
3762 if (size <= 0) {
3763 h2c->flags |= H2_CF_MUX_MFULL;
3764 h2s->flags |= H2_SF_BLK_MROOM;
3765 goto end;
3766 }
3767
3768 if (size > h2c->mws)
3769 size = h2c->mws;
3770
3771 if (size <= 0) {
3772 h2s->flags |= H2_SF_BLK_MFCTL;
3773 goto end;
3774 }
3775
3776 /* copy whatever we can */
3777 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003778 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003779 if (ret == 1)
3780 len2 = 0;
3781
3782 if (!ret || len1 + len2 < size) {
3783 /* FIXME: must normally never happen */
3784 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3785 goto end;
3786 }
3787
3788 /* limit len1/len2 to size */
3789 if (len1 + len2 > size) {
3790 int sub = len1 + len2 - size;
3791
3792 if (len2 > sub)
3793 len2 -= sub;
3794 else {
3795 sub -= len2;
3796 len2 = 0;
3797 len1 -= sub;
3798 }
3799 }
3800
3801 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003802 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003803 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003804 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003805
3806 send_empty:
3807 /* we may need to add END_STREAM */
3808 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3809 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003810 *
3811 * FIXME: what we do here is not correct because we send end_stream
3812 * before knowing if we'll have to send a HEADERS frame for the
3813 * trailers. More importantly we're not consuming the trailing CRLF
3814 * after the end of trailers, so it will be left to the caller to
3815 * eat it. The right way to do it would be to measure trailers here
3816 * and to send ES only if there are no trailers.
3817 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003818 */
3819 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003820 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003821 es_now = 1;
3822
3823 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003824 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003825
3826 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003827 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003828
3829 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003830 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003831
3832 /* consume incoming H1 response */
3833 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003834 max -= size;
3835 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003836 total += size;
3837 h1m->curr_len -= size;
3838 h2s->mws -= size;
3839 h2c->mws -= size;
3840
3841 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003842 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003843 goto new_frame;
3844 }
3845 }
3846
3847 if (es_now) {
3848 if (h2s->st == H2_SS_OPEN)
3849 h2s->st = H2_SS_HLOC;
3850 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003851 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003852
Willy Tarreau35a62702018-02-27 15:37:25 +01003853 if (!(h1m->flags & H1_MF_CHNK)) {
3854 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003855 total += max;
3856 ofs += max;
3857 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003858
Willy Tarreau801250e2018-09-11 11:45:04 +02003859 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003860 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003861
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003862 h2s->flags |= H2_SF_ES_SENT;
3863 }
3864
3865 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003866 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 +02003867 return total;
3868}
3869
Willy Tarreau115e83b2018-12-01 19:17:53 +01003870/* Try to send a HEADERS frame matching HTX response present in HTX message
3871 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3872 * must check the stream's status to detect any error which might have happened
3873 * subsequently to a successful send. The htx blocks are automatically removed
3874 * from the message. The htx message is assumed to be valid since produced from
3875 * the internal code, hence it contains a start line, an optional series of
3876 * header blocks and an end of header, otherwise an invalid frame could be
3877 * emitted and the resulting htx message could be left in an inconsistent state.
3878 */
3879static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3880{
3881 struct http_hdr list[MAX_HTTP_HDR];
3882 struct h2c *h2c = h2s->h2c;
3883 struct htx_blk *blk;
3884 struct htx_blk *blk_end;
3885 struct buffer outbuf;
3886 struct htx_sl *sl;
3887 enum htx_blk_type type;
3888 int es_now = 0;
3889 int ret = 0;
3890 int hdr;
3891 int idx;
3892
3893 if (h2c_mux_busy(h2c, h2s)) {
3894 h2s->flags |= H2_SF_BLK_MBUSY;
3895 return 0;
3896 }
3897
3898 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3899 h2c->flags |= H2_CF_MUX_MALLOC;
3900 h2s->flags |= H2_SF_BLK_MROOM;
3901 return 0;
3902 }
3903
3904 /* determine the first block which must not be deleted, blk_end may
3905 * be NULL if all blocks have to be deleted.
3906 */
3907 idx = htx_get_head(htx);
3908 blk_end = NULL;
3909 while (idx != -1) {
3910 type = htx_get_blk_type(htx_get_blk(htx, idx));
3911 idx = htx_get_next(htx, idx);
3912 if (type == HTX_BLK_EOH) {
3913 if (idx != -1)
3914 blk_end = htx_get_blk(htx, idx);
3915 break;
3916 }
3917 }
3918
3919 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003920 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003921 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003922 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003923 if (h2s->status < 100 || h2s->status > 999)
3924 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003925
3926 /* and the rest of the headers, that we dump starting at header 0 */
3927 hdr = 0;
3928
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003929 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003930 while ((idx = htx_get_next(htx, idx)) != -1) {
3931 blk = htx_get_blk(htx, idx);
3932 type = htx_get_blk_type(blk);
3933
3934 if (type == HTX_BLK_UNUSED)
3935 continue;
3936
3937 if (type != HTX_BLK_HDR)
3938 break;
3939
3940 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3941 goto fail;
3942
3943 list[hdr].n = htx_get_blk_name(htx, blk);
3944 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003945 hdr++;
3946 }
3947
3948 /* marker for end of headers */
3949 list[hdr].n = ist("");
3950
3951 if (h2s->status == 204 || h2s->status == 304) {
3952 /* no contents, claim c-len is present and set to zero */
3953 es_now = 1;
3954 }
3955
3956 chunk_reset(&outbuf);
3957
3958 while (1) {
3959 outbuf.area = b_tail(&h2c->mbuf);
3960 outbuf.size = b_contig_space(&h2c->mbuf);
3961 outbuf.data = 0;
3962
3963 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3964 break;
3965 realign_again:
3966 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3967 }
3968
3969 if (outbuf.size < 9)
3970 goto full;
3971
3972 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3973 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3974 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3975 outbuf.data = 9;
3976
3977 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003978 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003979 if (b_space_wraps(&h2c->mbuf))
3980 goto realign_again;
3981 goto full;
3982 }
3983
3984 /* encode all headers, stop at empty name */
3985 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3986 /* these ones do not exist in H2 and must be dropped. */
3987 if (isteq(list[hdr].n, ist("connection")) ||
3988 isteq(list[hdr].n, ist("proxy-connection")) ||
3989 isteq(list[hdr].n, ist("keep-alive")) ||
3990 isteq(list[hdr].n, ist("upgrade")) ||
3991 isteq(list[hdr].n, ist("transfer-encoding")))
3992 continue;
3993
3994 if (isteq(list[hdr].n, ist("")))
3995 break; // end
3996
3997 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3998 /* output full */
3999 if (b_space_wraps(&h2c->mbuf))
4000 goto realign_again;
4001 goto full;
4002 }
4003 }
4004
4005 /* we may need to add END_STREAM.
4006 * FIXME: we should also set it when we know for sure that the
4007 * content-length is zero as well as on 204/304
4008 */
4009 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4010 es_now = 1;
4011
4012 if (h2s->cs->flags & CS_FL_SHW)
4013 es_now = 1;
4014
4015 /* update the frame's size */
4016 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4017
4018 if (es_now)
4019 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4020
4021 /* commit the H2 response */
4022 b_add(&h2c->mbuf, outbuf.data);
4023 h2s->flags |= H2_SF_HEADERS_SENT;
4024
4025 /* for now we don't implemented CONTINUATION, so we wait for a
4026 * body or directly end in TRL2.
4027 */
4028 if (es_now) {
4029 h2s->flags |= H2_SF_ES_SENT;
4030 if (h2s->st == H2_SS_OPEN)
4031 h2s->st = H2_SS_HLOC;
4032 else
4033 h2s_close(h2s);
4034 }
4035
4036 /* OK we could properly deliver the response */
4037
4038 /* remove all header blocks including the EOH and compute the
4039 * corresponding size.
4040 *
4041 * FIXME: We should remove everything when es_now is set.
4042 */
4043 ret = 0;
4044 idx = htx_get_head(htx);
4045 blk = htx_get_blk(htx, idx);
4046 while (blk != blk_end) {
4047 ret += htx_get_blksz(blk);
4048 blk = htx_remove_blk(htx, blk);
4049 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004050
4051 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4052 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004053 end:
4054 return ret;
4055 full:
4056 h2c->flags |= H2_CF_MUX_MFULL;
4057 h2s->flags |= H2_SF_BLK_MROOM;
4058 ret = 0;
4059 goto end;
4060 fail:
4061 /* unparsable HTX messages, too large ones to be produced in the local
4062 * list etc go here (unrecoverable errors).
4063 */
4064 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4065 ret = 0;
4066 goto end;
4067}
4068
Willy Tarreau80739692018-10-05 11:35:57 +02004069/* Try to send a HEADERS frame matching HTX request present in HTX message
4070 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4071 * must check the stream's status to detect any error which might have happened
4072 * subsequently to a successful send. The htx blocks are automatically removed
4073 * from the message. The htx message is assumed to be valid since produced from
4074 * the internal code, hence it contains a start line, an optional series of
4075 * header blocks and an end of header, otherwise an invalid frame could be
4076 * emitted and the resulting htx message could be left in an inconsistent state.
4077 */
4078static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4079{
4080 struct http_hdr list[MAX_HTTP_HDR];
4081 struct h2c *h2c = h2s->h2c;
4082 struct htx_blk *blk;
4083 struct htx_blk *blk_end;
4084 struct buffer outbuf;
4085 struct htx_sl *sl;
4086 struct ist meth, path;
4087 enum htx_blk_type type;
4088 int es_now = 0;
4089 int ret = 0;
4090 int hdr;
4091 int idx;
4092
4093 if (h2c_mux_busy(h2c, h2s)) {
4094 h2s->flags |= H2_SF_BLK_MBUSY;
4095 return 0;
4096 }
4097
4098 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4099 h2c->flags |= H2_CF_MUX_MALLOC;
4100 h2s->flags |= H2_SF_BLK_MROOM;
4101 return 0;
4102 }
4103
4104 /* determine the first block which must not be deleted, blk_end may
4105 * be NULL if all blocks have to be deleted.
4106 */
4107 idx = htx_get_head(htx);
4108 blk_end = NULL;
4109 while (idx != -1) {
4110 type = htx_get_blk_type(htx_get_blk(htx, idx));
4111 idx = htx_get_next(htx, idx);
4112 if (type == HTX_BLK_EOH) {
4113 if (idx != -1)
4114 blk_end = htx_get_blk(htx, idx);
4115 break;
4116 }
4117 }
4118
4119 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004120 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004121 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004122 meth = htx_sl_req_meth(sl);
4123 path = htx_sl_req_uri(sl);
4124
4125 /* and the rest of the headers, that we dump starting at header 0 */
4126 hdr = 0;
4127
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004128 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004129 while ((idx = htx_get_next(htx, idx)) != -1) {
4130 blk = htx_get_blk(htx, idx);
4131 type = htx_get_blk_type(blk);
4132
4133 if (type == HTX_BLK_UNUSED)
4134 continue;
4135
4136 if (type != HTX_BLK_HDR)
4137 break;
4138
4139 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4140 goto fail;
4141
4142 list[hdr].n = htx_get_blk_name(htx, blk);
4143 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004144 hdr++;
4145 }
4146
4147 /* marker for end of headers */
4148 list[hdr].n = ist("");
4149
4150 chunk_reset(&outbuf);
4151
4152 while (1) {
4153 outbuf.area = b_tail(&h2c->mbuf);
4154 outbuf.size = b_contig_space(&h2c->mbuf);
4155 outbuf.data = 0;
4156
4157 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4158 break;
4159 realign_again:
4160 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4161 }
4162
4163 if (outbuf.size < 9)
4164 goto full;
4165
4166 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4167 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4168 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4169 outbuf.data = 9;
4170
4171 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004172 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004173 if (b_space_wraps(&h2c->mbuf))
4174 goto realign_again;
4175 goto full;
4176 }
4177
4178 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004179 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4180 /* output full */
4181 if (b_space_wraps(&h2c->mbuf))
4182 goto realign_again;
4183 goto full;
4184 }
Willy Tarreau80739692018-10-05 11:35:57 +02004185
4186 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004187 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004188 /* output full */
4189 if (b_space_wraps(&h2c->mbuf))
4190 goto realign_again;
4191 goto full;
4192 }
4193
4194 /* encode all headers, stop at empty name */
4195 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4196 /* these ones do not exist in H2 and must be dropped. */
4197 if (isteq(list[hdr].n, ist("connection")) ||
4198 isteq(list[hdr].n, ist("proxy-connection")) ||
4199 isteq(list[hdr].n, ist("keep-alive")) ||
4200 isteq(list[hdr].n, ist("upgrade")) ||
4201 isteq(list[hdr].n, ist("transfer-encoding")))
4202 continue;
4203
4204 if (isteq(list[hdr].n, ist("")))
4205 break; // end
4206
4207 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4208 /* output full */
4209 if (b_space_wraps(&h2c->mbuf))
4210 goto realign_again;
4211 goto full;
4212 }
4213 }
4214
4215 /* we may need to add END_STREAM if we have no body :
4216 * - request already closed, or :
4217 * - no transfer-encoding, and :
4218 * - no content-length or content-length:0
4219 * Fixme: this doesn't take into account CONNECT requests.
4220 */
4221 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4222 es_now = 1;
4223
4224 if (sl->flags & HTX_SL_F_BODYLESS)
4225 es_now = 1;
4226
4227 if (h2s->cs->flags & CS_FL_SHW)
4228 es_now = 1;
4229
4230 /* update the frame's size */
4231 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4232
4233 if (es_now)
4234 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4235
4236 /* commit the H2 response */
4237 b_add(&h2c->mbuf, outbuf.data);
4238 h2s->flags |= H2_SF_HEADERS_SENT;
4239 h2s->st = H2_SS_OPEN;
4240
4241 /* for now we don't implemented CONTINUATION, so we wait for a
4242 * body or directly end in TRL2.
4243 */
4244 if (es_now) {
4245 // trim any possibly pending data (eg: inconsistent content-length)
4246 h2s->flags |= H2_SF_ES_SENT;
4247 h2s->st = H2_SS_HLOC;
4248 }
4249
4250 /* remove all header blocks including the EOH and compute the
4251 * corresponding size.
4252 *
4253 * FIXME: We should remove everything when es_now is set.
4254 */
4255 ret = 0;
4256 idx = htx_get_head(htx);
4257 blk = htx_get_blk(htx, idx);
4258 while (blk != blk_end) {
4259 ret += htx_get_blksz(blk);
4260 blk = htx_remove_blk(htx, blk);
4261 }
4262
4263 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4264 htx_remove_blk(htx, blk_end);
4265
4266 end:
4267 return ret;
4268 full:
4269 h2c->flags |= H2_CF_MUX_MFULL;
4270 h2s->flags |= H2_SF_BLK_MROOM;
4271 ret = 0;
4272 goto end;
4273 fail:
4274 /* unparsable HTX messages, too large ones to be produced in the local
4275 * list etc go here (unrecoverable errors).
4276 */
4277 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4278 ret = 0;
4279 goto end;
4280}
4281
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004282/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004283 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4284 * caller must check the stream's status to detect any error which might have
4285 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004286 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4287 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004288static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004289{
4290 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004291 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004292 struct buffer outbuf;
4293 size_t total = 0;
4294 int es_now = 0;
4295 int bsize; /* htx block size */
4296 int fsize; /* h2 frame size */
4297 struct htx_blk *blk;
4298 enum htx_blk_type type;
4299 int idx;
4300
4301 if (h2c_mux_busy(h2c, h2s)) {
4302 h2s->flags |= H2_SF_BLK_MBUSY;
4303 goto end;
4304 }
4305
4306 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4307 h2c->flags |= H2_CF_MUX_MALLOC;
4308 h2s->flags |= H2_SF_BLK_MROOM;
4309 goto end;
4310 }
4311
Willy Tarreau98de12a2018-12-12 07:03:00 +01004312 htx = htx_from_buf(buf);
4313
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004314 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4315 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4316 * the caller to handle.
4317 */
4318
4319 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004320 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004321 goto end;
4322
4323 idx = htx_get_head(htx);
4324 blk = htx_get_blk(htx, idx);
4325 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4326 bsize = htx_get_blksz(blk);
4327 fsize = bsize;
4328
4329 if (type == HTX_BLK_EOD) {
4330 /* if we have an EOD, we're dealing with chunked data. We may
4331 * have a set of trailers after us that the caller will want to
4332 * deal with. Let's simply remove the EOD and return.
4333 */
4334 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004335 total++; // EOD counts as one byte
4336 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004337 goto end;
4338 }
4339
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004340 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4341 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004342
4343 /* Perform some optimizations to reduce the number of buffer copies.
4344 * First, if the mux's buffer is empty and the htx area contains
4345 * exactly one data block of the same size as the requested count, and
4346 * this count fits within the frame size, the stream's window size, and
4347 * the connection's window size, then it's possible to simply swap the
4348 * caller's buffer with the mux's output buffer and adjust offsets and
4349 * length to match the entire DATA HTX block in the middle. In this
4350 * case we perform a true zero-copy operation from end-to-end. This is
4351 * the situation that happens all the time with large files. Second, if
4352 * this is not possible, but the mux's output buffer is empty, we still
4353 * have an opportunity to avoid the copy to the intermediary buffer, by
4354 * making the intermediary buffer's area point to the output buffer's
4355 * area. In this case we want to skip the HTX header to make sure that
4356 * copies remain aligned and that this operation remains possible all
4357 * the time. This goes for headers, data blocks and any data extracted
4358 * from the HTX blocks.
4359 */
4360 if (unlikely(fsize == count &&
4361 htx->used == 1 && type == HTX_BLK_DATA &&
4362 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4363 void *old_area = h2c->mbuf.area;
4364
4365 if (b_data(&h2c->mbuf)) {
4366 /* too bad there are data left there. If we have less
4367 * than 1/4 of the mbuf's size and everything fits,
4368 * we'll perform a copy anyway. Otherwise we'll pretend
4369 * the mbuf is full and wait.
4370 */
4371 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4372 goto copy;
4373 h2c->flags |= H2_CF_MUX_MFULL;
4374 h2s->flags |= H2_SF_BLK_MROOM;
4375 goto end;
4376 }
4377
4378 /* map an H2 frame to the HTX block so that we can put the
4379 * frame header there.
4380 */
4381 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004382 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004383 h2c->mbuf.data = fsize + 9;
4384 outbuf.area = b_head(&h2c->mbuf);
4385
4386 /* prepend an H2 DATA frame header just before the DATA block */
4387 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4388 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4389 h2_set_frame_size(outbuf.area, fsize);
4390
4391 /* update windows */
4392 h2s->mws -= fsize;
4393 h2c->mws -= fsize;
4394
4395 /* and exchange with our old area */
4396 buf->area = old_area;
4397 buf->data = buf->head = 0;
4398 total += fsize;
4399 goto end;
4400 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004401
Willy Tarreau98de12a2018-12-12 07:03:00 +01004402 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004403 /* for DATA and EOM we'll have to emit a frame, even if empty */
4404
4405 while (1) {
4406 outbuf.area = b_tail(&h2c->mbuf);
4407 outbuf.size = b_contig_space(&h2c->mbuf);
4408 outbuf.data = 0;
4409
4410 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4411 break;
4412 realign_again:
4413 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4414 }
4415
4416 if (outbuf.size < 9) {
4417 h2c->flags |= H2_CF_MUX_MFULL;
4418 h2s->flags |= H2_SF_BLK_MROOM;
4419 goto end;
4420 }
4421
4422 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4423 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4424 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4425 outbuf.data = 9;
4426
4427 /* we have in <fsize> the exact number of bytes we need to copy from
4428 * the HTX buffer. We need to check this against the connection's and
4429 * the stream's send windows, and to ensure that this fits in the max
4430 * frame size and in the buffer's available space minus 9 bytes (for
4431 * the frame header). The connection's flow control is applied last so
4432 * that we can use a separate list of streams which are immediately
4433 * unblocked on window opening. Note: we don't implement padding.
4434 */
4435
4436 /* EOM is presented with bsize==1 but would lead to the emission of an
4437 * empty frame, thus we force it to zero here.
4438 */
4439 if (type == HTX_BLK_EOM)
4440 bsize = fsize = 0;
4441
4442 if (!fsize)
4443 goto send_empty;
4444
4445 if (h2s->mws <= 0) {
4446 h2s->flags |= H2_SF_BLK_SFCTL;
4447 if (h2s->send_wait) {
4448 LIST_DEL(&h2s->list);
4449 LIST_INIT(&h2s->list);
4450 }
4451 goto end;
4452 }
4453
Willy Tarreauee573762018-12-04 15:25:57 +01004454 if (fsize > count)
4455 fsize = count;
4456
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004457 if (fsize > h2s->mws)
4458 fsize = h2s->mws; // >0
4459
4460 if (h2c->mfs && fsize > h2c->mfs)
4461 fsize = h2c->mfs; // >0
4462
4463 if (fsize + 9 > outbuf.size) {
4464 /* we have an opportunity for enlarging the too small
4465 * available space, let's try.
4466 * FIXME: is this really interesting to do? Maybe we'll
4467 * spend lots of time realigning instead of using two
4468 * frames.
4469 */
4470 if (b_space_wraps(&h2c->mbuf))
4471 goto realign_again;
4472 fsize = outbuf.size - 9;
4473
4474 if (fsize <= 0) {
4475 /* no need to send an empty frame here */
4476 h2c->flags |= H2_CF_MUX_MFULL;
4477 h2s->flags |= H2_SF_BLK_MROOM;
4478 goto end;
4479 }
4480 }
4481
4482 if (h2c->mws <= 0) {
4483 h2s->flags |= H2_SF_BLK_MFCTL;
4484 goto end;
4485 }
4486
4487 if (fsize > h2c->mws)
4488 fsize = h2c->mws;
4489
4490 /* now let's copy this this into the output buffer */
4491 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004492 h2s->mws -= fsize;
4493 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004494 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004495
4496 send_empty:
4497 /* update the frame's size */
4498 h2_set_frame_size(outbuf.area, fsize);
4499
4500 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4501 * meeting EOM. We should optimize this later.
4502 */
4503 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004504 total++; // EOM counts as one byte
4505 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004506 es_now = 1;
4507 }
4508
4509 if (es_now)
4510 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4511
4512 /* commit the H2 response */
4513 b_add(&h2c->mbuf, fsize + 9);
4514
4515 /* consume incoming HTX block, including EOM */
4516 total += fsize;
4517 if (fsize == bsize) {
4518 htx_remove_blk(htx, blk);
4519 if (fsize)
4520 goto new_frame;
4521 } else {
4522 /* we've truncated this block */
4523 htx_cut_data_blk(htx, blk, fsize);
4524 }
4525
4526 if (es_now) {
4527 if (h2s->st == H2_SS_OPEN)
4528 h2s->st = H2_SS_HLOC;
4529 else
4530 h2s_close(h2s);
4531
4532 h2s->flags |= H2_SF_ES_SENT;
4533 }
4534
4535 end:
4536 return total;
4537}
4538
Olivier Houchard6ff20392018-07-17 18:46:31 +02004539/* Called from the upper layer, to subscribe to events, such as being able to send */
4540static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4541{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004542 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004543 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004544 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004545
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004546 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004547 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004548 if (!(sw->events & SUB_RETRY_RECV)) {
4549 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004550 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004551 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004552 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004553 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004554 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004555 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004556 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004557 if (!(sw->events & SUB_RETRY_SEND)) {
4558 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004559 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004560 h2s->send_wait = sw;
4561 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4562 if (h2s->flags & H2_SF_BLK_MFCTL)
4563 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4564 else
4565 LIST_ADDQ(&h2c->send_list, &h2s->list);
4566 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004567 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004568 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004569 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004570 if (event_type != 0)
4571 return -1;
4572 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004573
4574
4575}
4576
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004577static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4578{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004579 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004580 struct h2s *h2s = cs->ctx;
4581
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004582 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004583 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004584 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004585 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004586 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004587 }
4588 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004589 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004590 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004591 if (h2s->send_wait == sw) {
4592 LIST_DEL(&h2s->list);
4593 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004594 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004595 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004596 }
4597 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004598 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4599 sw = param;
4600 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004601 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004602 h2s->send_wait = NULL;
4603 }
4604 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004605 return 0;
4606}
4607
4608
Olivier Houchard511efea2018-08-16 15:30:32 +02004609/* Called from the upper layer, to receive data */
4610static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4611{
Olivier Houchard638b7992018-08-16 15:41:52 +02004612 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004613 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004614 struct htx *h2s_htx = NULL;
4615 struct htx *buf_htx = NULL;
4616 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004617 size_t ret = 0;
4618
4619 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004620 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4621 /* in HTX mode we ignore the count argument */
4622 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004623 if (htx_is_empty(h2s_htx)) {
4624 if (cs->flags & CS_FL_REOS)
4625 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004626 if (cs->flags & CS_FL_ERR_PENDING)
4627 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004628 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004629 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004630
4631 buf_htx = htx_from_buf(buf);
4632 count = htx_free_space(buf_htx);
4633
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004634 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004635
4636 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004637 htx_to_buf(buf_htx, buf);
4638 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004639 ret = htx_ret.ret;
4640 }
4641 else {
4642 ret = b_xfer(buf, &h2s->rxbuf, count);
4643 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004644
Olivier Houchard638b7992018-08-16 15:41:52 +02004645 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004646 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004647 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004648 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004649 if (cs->flags & CS_FL_REOS)
4650 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004651 if (cs->flags & CS_FL_ERR_PENDING)
4652 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004653 if (b_size(&h2s->rxbuf)) {
4654 b_free(&h2s->rxbuf);
4655 offer_buffers(NULL, tasks_run_queue);
4656 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004657 }
4658
Willy Tarreau082f5592018-11-25 08:03:32 +01004659 if (ret && h2c->dsi == h2s->id) {
4660 /* demux is blocking on this stream's buffer */
4661 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004662 if (b_data(&h2c->dbuf) || !(h2c->wait_event.events & SUB_RETRY_RECV)) {
Willy Tarreau082f5592018-11-25 08:03:32 +01004663 if (h2_recv_allowed(h2c))
4664 tasklet_wakeup(h2c->wait_event.task);
4665 }
4666 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004667end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004668 return ret;
4669}
4670
Olivier Houchardd846c262018-10-19 17:24:29 +02004671static void h2_stop_senders(struct h2c *h2c)
4672{
4673 struct h2s *h2s, *h2s_back;
4674
4675 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4676 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4677 if (h2c->msi == h2s_id(h2s))
4678 continue;
4679 LIST_DEL(&h2s->list);
4680 LIST_INIT(&h2s->list);
4681 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004682 h2s->send_wait->events |= SUB_RETRY_SEND;
4683 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004684 LIST_ADD(&h2c->send_list, &h2s->list);
4685 }
4686}
4687
Willy Tarreau62f52692017-10-08 23:01:42 +02004688/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004689static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004690{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004691 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004692 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004693 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004694 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004695 struct htx *htx;
4696 struct htx_blk *blk;
4697 enum htx_blk_type btype;
4698 uint32_t bsize;
4699 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004700
Olivier Houchardd846c262018-10-19 17:24:29 +02004701 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004702 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004703 h2s->send_wait = NULL;
4704 LIST_DEL(&h2s->list);
4705 LIST_INIT(&h2s->list);
4706 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004707 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4708 return 0;
4709
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004710 /* htx will be enough to decide if we're using HTX or legacy */
4711 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4712
Willy Tarreau0bad0432018-06-14 16:54:01 +02004713 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004714 h2s->flags |= H2_SF_OUTGOING_DATA;
4715
Willy Tarreau751f2d02018-10-05 09:35:00 +02004716 if (h2s->id == 0) {
4717 int32_t id = h2c_get_next_sid(h2s->h2c);
4718
4719 if (id < 0) {
4720 cs->ctx = NULL;
4721 cs->flags |= CS_FL_ERROR;
4722 h2s_destroy(h2s);
4723 return 0;
4724 }
4725
4726 eb32_delete(&h2s->by_id);
4727 h2s->by_id.key = h2s->id = id;
4728 h2s->h2c->max_id = id;
4729 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4730 }
4731
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004732 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004733 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4734 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004735 idx = htx_get_head(htx);
4736 blk = htx_get_blk(htx, idx);
4737 btype = htx_get_blk_type(blk);
4738 bsize = htx_get_blksz(blk);
4739
4740 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004741 case HTX_BLK_REQ_SL:
4742 /* start-line before headers */
4743 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4744 if (ret > 0) {
4745 total += ret;
4746 count -= ret;
4747 if (ret < bsize)
4748 goto done;
4749 }
4750 break;
4751
Willy Tarreau115e83b2018-12-01 19:17:53 +01004752 case HTX_BLK_RES_SL:
4753 /* start-line before headers */
4754 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4755 if (ret > 0) {
4756 total += ret;
4757 count -= ret;
4758 if (ret < bsize)
4759 goto done;
4760 }
4761 break;
4762
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004763 case HTX_BLK_DATA:
4764 case HTX_BLK_EOD:
4765 case HTX_BLK_EOM:
4766 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004767 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004768 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004769 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004770 total += ret;
4771 count -= ret;
4772 if (ret < bsize)
4773 goto done;
4774 }
4775 break;
4776
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004777 default:
4778 htx_remove_blk(htx, blk);
4779 total += bsize;
4780 count -= bsize;
4781 break;
4782 }
4783 }
4784 goto done;
4785 }
4786
4787 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004788 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004789 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004790 if (h2s->h2c->flags & H2_CF_IS_BACK)
4791 ret = -1;
4792 else
4793 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004794 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004795 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004796 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004797 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004798 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004799 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004800 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004801
Willy Tarreau5dd17352018-06-14 13:33:30 +02004802 if (unlikely((int)ret <= 0)) {
4803 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004804 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4805 break;
4806 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004807 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004808 total += count;
4809 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004810 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004811 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004812 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004813 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004814 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004815 break;
4816 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004817
4818 total += ret;
4819 count -= ret;
4820
4821 if (h2s->st >= H2_SS_ERROR)
4822 break;
4823
4824 if (h2s->flags & H2_SF_BLK_ANY)
4825 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004826 }
4827
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004828 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004829 if (h2s->st >= H2_SS_ERROR) {
4830 /* trim any possibly pending data after we close (extra CR-LF,
4831 * unprocessed trailers, abnormal extra data, ...)
4832 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004833 total += count;
4834 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004835 }
4836
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004837 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004838 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004839 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004840 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004841 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004842 }
4843
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004844 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004845 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004846 } else {
4847 b_del(buf, total);
4848 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004849
4850 /* The mux is full, cancel the pending tasks */
4851 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4852 (h2s->flags & H2_SF_BLK_MBUSY))
4853 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004854
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004855 /* If we're running HTX, and we read the whole buffer, then pretend
4856 * we read exactly what the caller specified, as with HTX the caller
4857 * will always give the buffer size, instead of the amount of data
4858 * available.
4859 */
4860 if (htx && !b_data(buf))
4861 total = orig_count;
4862
Olivier Houchard7505f942018-08-21 18:10:44 +02004863 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004864 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004865 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004866
Olivier Houchard7505f942018-08-21 18:10:44 +02004867 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01004868 /* If we're waiting for flow control, and we got a shutr on the
4869 * connection, we will never be unlocked, so add an error on
4870 * the conn_stream.
4871 */
4872 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
4873 !b_data(&h2s->h2c->dbuf) &&
4874 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
4875 if (cs->flags & CS_FL_EOS)
4876 cs->flags |= CS_FL_ERROR;
4877 else
4878 cs->flags |= CS_FL_ERR_PENDING;
4879 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004880 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004881}
4882
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004883/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004884static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004885{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004886 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01004887 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004888 struct eb32_node *node;
4889 int fctl_cnt = 0;
4890 int send_cnt = 0;
4891 int tree_cnt = 0;
4892 int orph_cnt = 0;
4893
4894 if (!h2c)
4895 return;
4896
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004897 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004898 fctl_cnt++;
4899
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004900 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004901 send_cnt++;
4902
Willy Tarreau3af37712018-12-18 14:34:41 +01004903 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004904 node = eb32_first(&h2c->streams_by_id);
4905 while (node) {
4906 h2s = container_of(node, struct h2s, by_id);
4907 tree_cnt++;
4908 if (!h2s->cs)
4909 orph_cnt++;
4910 node = eb32_next(node);
4911 }
4912
Willy Tarreau987c0632018-12-18 10:32:05 +01004913 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
4914 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
4915 " .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 +02004916 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4917 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004918 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01004919 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
4920 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4921 h2c->msi,
4922 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
4923 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
4924
4925 if (h2s) {
4926 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4927 h2s, h2s->id, h2s->flags,
4928 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
4929 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
4930 h2s->cs);
4931 if (h2s->cs)
4932 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4933 h2s->cs->flags, h2s->cs->data);
4934 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004935}
Willy Tarreau62f52692017-10-08 23:01:42 +02004936
4937/*******************************************************/
4938/* functions below are dedicated to the config parsers */
4939/*******************************************************/
4940
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004941/* config parser for global "tune.h2.header-table-size" */
4942static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4943 struct proxy *defpx, const char *file, int line,
4944 char **err)
4945{
4946 if (too_many_args(1, args, err, NULL))
4947 return -1;
4948
4949 h2_settings_header_table_size = atoi(args[1]);
4950 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4951 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4952 return -1;
4953 }
4954 return 0;
4955}
Willy Tarreau62f52692017-10-08 23:01:42 +02004956
Willy Tarreaue6baec02017-07-27 11:45:11 +02004957/* config parser for global "tune.h2.initial-window-size" */
4958static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4959 struct proxy *defpx, const char *file, int line,
4960 char **err)
4961{
4962 if (too_many_args(1, args, err, NULL))
4963 return -1;
4964
4965 h2_settings_initial_window_size = atoi(args[1]);
4966 if (h2_settings_initial_window_size < 0) {
4967 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4968 return -1;
4969 }
4970 return 0;
4971}
4972
Willy Tarreau5242ef82017-07-27 11:47:28 +02004973/* config parser for global "tune.h2.max-concurrent-streams" */
4974static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4975 struct proxy *defpx, const char *file, int line,
4976 char **err)
4977{
4978 if (too_many_args(1, args, err, NULL))
4979 return -1;
4980
4981 h2_settings_max_concurrent_streams = atoi(args[1]);
4982 if (h2_settings_max_concurrent_streams < 0) {
4983 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4984 return -1;
4985 }
4986 return 0;
4987}
4988
Willy Tarreau62f52692017-10-08 23:01:42 +02004989
4990/****************************************/
4991/* MUX initialization and instanciation */
4992/***************************************/
4993
4994/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004995static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004996 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004997 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004998 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004999 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005000 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005001 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005002 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005003 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005004 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005005 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005006 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005007 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005008 .shutr = h2_shutr,
5009 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005010 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005011 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005012 .name = "H2",
5013};
5014
Christopher Faulet32f61c02018-04-10 14:33:41 +02005015/* PROTO selection : this mux registers PROTO token "h2" */
5016static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005017 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005018
Willy Tarreau0108d902018-11-25 19:14:37 +01005019INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5020
Willy Tarreauf8957272018-10-03 10:25:20 +02005021static struct mux_proto_list mux_proto_h2_htx =
5022 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5023
5024INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5025
Willy Tarreau62f52692017-10-08 23:01:42 +02005026/* config keyword parsers */
5027static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005028 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005029 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005030 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005031 { 0, NULL, NULL }
5032}};
5033
Willy Tarreau0108d902018-11-25 19:14:37 +01005034INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);