blob: 582509ff3efbe2ac2c6d196fc3e2d82d0b4ab0dc [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;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100620 LIST_DEL(&h2s->list);
621 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100622 }
623}
624
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100625/* alerts the data layer, trying to wake it up by all means, following
626 * this sequence :
627 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
628 * - if its subscribed to send, then it's woken up for send
629 * - if it was subscribed to neither, its ->wake() callback is called
630 * It is safe to call this function with a closed stream which doesn't have a
631 * conn_stream anymore.
632 */
633static void __maybe_unused h2s_alert(struct h2s *h2s)
634{
635 if (h2s->recv_wait || h2s->send_wait) {
636 h2s_notify_recv(h2s);
637 h2s_notify_send(h2s);
638 }
639 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
640 h2s->cs->data_cb->wake(h2s->cs);
641}
642
Willy Tarreaue4820742017-07-27 13:37:23 +0200643/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100644static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200645{
646 uint8_t *out = frame;
647
648 *out = len >> 16;
649 write_n16(out + 1, len);
650}
651
Willy Tarreau54c15062017-10-10 17:10:03 +0200652/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
653 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
654 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200655 * available in the buffer's input prior to calling this function. The buffer
656 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200657 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100658static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200659 const struct buffer *b, int o)
660{
Willy Tarreau591d4452018-06-15 17:21:00 +0200661 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 +0200662}
663
Willy Tarreau1f094672017-11-20 21:27:45 +0100664static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200665{
Willy Tarreau591d4452018-06-15 17:21:00 +0200666 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200667}
668
Willy Tarreau1f094672017-11-20 21:27:45 +0100669static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200670{
Willy Tarreau591d4452018-06-15 17:21:00 +0200671 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200672}
673
Willy Tarreau1f094672017-11-20 21:27:45 +0100674static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200675{
Willy Tarreau591d4452018-06-15 17:21:00 +0200676 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200677}
678
679
Willy Tarreau715d5312017-07-11 15:20:24 +0200680/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
681 * is not obvious. It turns out that H2 headers are neither aligned nor do they
682 * use regular sizes. And to add to the trouble, the buffer may wrap so each
683 * byte read must be checked. The header is formed like this :
684 *
685 * b0 b1 b2 b3 b4 b5..b8
686 * +----------+---------+--------+----+----+----------------------+
687 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
688 * +----------+---------+--------+----+----+----------------------+
689 *
690 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
691 * we get the sid properly aligned and ordered, and 16 bits of len properly
692 * ordered as well. The type and flags can be extracted using bit shifts from
693 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200694 * Returns zero if some bytes are missing, otherwise non-zero on success. The
695 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200696 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200698{
699 uint64_t w;
700
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200701 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200702 return 0;
703
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200704 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200705 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200706 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
707 h->ff = w >> 32;
708 h->ft = w >> 40;
709 h->len += w >> 48;
710 return 1;
711}
712
713/* skip the next 9 bytes corresponding to the frame header possibly parsed by
714 * h2_peek_frame_hdr() above.
715 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100716static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200717{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200718 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200719}
720
721/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100722static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200723{
724 int ret;
725
726 ret = h2_peek_frame_hdr(b, h);
727 if (ret > 0)
728 h2_skip_frame_hdr(b);
729 return ret;
730}
731
Willy Tarreau00dd0782018-03-01 16:31:34 +0100732/* marks stream <h2s> as CLOSED and decrement the number of active streams for
733 * its connection if the stream was not yet closed. Please use this exclusively
734 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100735 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100736static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100737{
738 if (h2s->st != H2_SS_CLOSED)
739 h2s->h2c->nb_streams--;
740 h2s->st = H2_SS_CLOSED;
741}
742
Willy Tarreau71049cc2018-03-28 13:56:39 +0200743/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
744static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100745{
746 h2s_close(h2s);
747 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200748 if (b_size(&h2s->rxbuf)) {
749 b_free(&h2s->rxbuf);
750 offer_buffers(NULL, tasks_run_queue);
751 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200752 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100753 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200754 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100755 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800756 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200757 * reference left would be in the h2c send_list/fctl_list, and if
758 * we're in it, we're getting out anyway
759 */
760 LIST_DEL(&h2s->list);
761 LIST_INIT(&h2s->list);
762 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100763 pool_free(pool_head_h2s, h2s);
764}
765
Willy Tarreaua8e49542018-10-03 18:53:55 +0200766/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
767 * stream tree. In case of error, nothing is added and NULL is returned. The
768 * causes of errors can be any failed memory allocation. The caller is
769 * responsible for checking if the connection may support an extra stream
770 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200771 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200772static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200773{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200774 struct h2s *h2s;
775
Willy Tarreaubafbe012017-11-24 17:34:44 +0100776 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200777 if (!h2s)
778 goto out;
779
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200780 h2s->wait_event.task = tasklet_new();
781 if (!h2s->wait_event.task) {
782 pool_free(pool_head_h2s, h2s);
783 goto out;
784 }
785 h2s->send_wait = NULL;
786 h2s->recv_wait = NULL;
787 h2s->wait_event.task->process = h2_deferred_shut;
788 h2s->wait_event.task->context = h2s;
789 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100790 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200791 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200792 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200793 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200794 h2s->mws = h2c->miw;
795 h2s->flags = H2_SF_NONE;
796 h2s->errcode = H2_ERR_NO_ERROR;
797 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200798 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200799 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200800
801 if (h2c->flags & H2_CF_IS_BACK) {
802 h1m_init_req(&h2s->h1m);
803 h2s->h1m.err_pos = -1; // don't care about errors on the request path
804 h2s->h1m.flags |= H1_MF_TOLOWER;
805 } else {
806 h1m_init_res(&h2s->h1m);
807 h2s->h1m.err_pos = -1; // don't care about errors on the response path
808 h2s->h1m.flags |= H1_MF_TOLOWER;
809 }
810
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200811 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200812 if (id > 0)
813 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200814
815 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100816 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200817
818 return h2s;
819
820 out_free_h2s:
821 pool_free(pool_head_h2s, h2s);
822 out:
823 return NULL;
824}
825
826/* creates a new stream <id> on the h2c connection and returns it, or NULL in
827 * case of memory allocation error.
828 */
829static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
830{
831 struct session *sess = h2c->conn->owner;
832 struct conn_stream *cs;
833 struct h2s *h2s;
834
835 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
836 goto out;
837
838 h2s = h2s_new(h2c, id);
839 if (!h2s)
840 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200841
842 cs = cs_new(h2c->conn);
843 if (!cs)
844 goto out_close;
845
Olivier Houchard746fb772018-12-15 19:42:00 +0100846 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200847 h2s->cs = cs;
848 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200849 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200850
851 if (stream_create_from_cs(cs) < 0)
852 goto out_free_cs;
853
Willy Tarreau590a0512018-09-05 11:56:48 +0200854 /* We want the accept date presented to the next stream to be the one
855 * we have now, the handshake time to be null (since the next stream
856 * is not delayed by a handshake), and the idle time to count since
857 * right now.
858 */
859 sess->accept_date = date;
860 sess->tv_accept = now;
861 sess->t_handshake = 0;
862
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200863 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200864 if (h2_has_too_many_cs(h2c))
865 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200866 return h2s;
867
868 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200869 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200870 cs_free(cs);
871 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200872 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200873 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200874 sess_log(sess);
875 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200876}
877
Willy Tarreau751f2d02018-10-05 09:35:00 +0200878/* allocates a new stream associated to conn_stream <cs> on the h2c connection
879 * and returns it, or NULL in case of memory allocation error or if the highest
880 * possible stream ID was reached.
881 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100882static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200883{
884 struct h2s *h2s = NULL;
885
886 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
887 goto out;
888
889 /* Defer choosing the ID until we send the first message to create the stream */
890 h2s = h2s_new(h2c, 0);
891 if (!h2s)
892 goto out;
893
894 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100895 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200896 cs->ctx = h2s;
897 h2c->nb_cs++;
898
Willy Tarreau751f2d02018-10-05 09:35:00 +0200899 out:
900 return h2s;
901}
902
Willy Tarreaube5b7152017-09-25 16:25:39 +0200903/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
904 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
905 * the various settings codes.
906 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200907static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200908{
909 struct buffer *res;
910 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200911 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200912 int ret;
913
914 if (h2c_mux_busy(h2c, NULL)) {
915 h2c->flags |= H2_CF_DEM_MBUSY;
916 return 0;
917 }
918
Willy Tarreau44e973f2018-03-01 17:49:30 +0100919 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200920 if (!res) {
921 h2c->flags |= H2_CF_MUX_MALLOC;
922 h2c->flags |= H2_CF_DEM_MROOM;
923 return 0;
924 }
925
926 chunk_init(&buf, buf_data, sizeof(buf_data));
927 chunk_memcpy(&buf,
928 "\x00\x00\x00" /* length : 0 for now */
929 "\x04\x00" /* type : 4 (settings), flags : 0 */
930 "\x00\x00\x00\x00", /* stream ID : 0 */
931 9);
932
933 if (h2_settings_header_table_size != 4096) {
934 char str[6] = "\x00\x01"; /* header_table_size */
935
936 write_n32(str + 2, h2_settings_header_table_size);
937 chunk_memcat(&buf, str, 6);
938 }
939
940 if (h2_settings_initial_window_size != 65535) {
941 char str[6] = "\x00\x04"; /* initial_window_size */
942
943 write_n32(str + 2, h2_settings_initial_window_size);
944 chunk_memcat(&buf, str, 6);
945 }
946
947 if (h2_settings_max_concurrent_streams != 0) {
948 char str[6] = "\x00\x03"; /* max_concurrent_streams */
949
950 /* Note: 0 means "unlimited" for haproxy's config but not for
951 * the protocol, so never send this value!
952 */
953 write_n32(str + 2, h2_settings_max_concurrent_streams);
954 chunk_memcat(&buf, str, 6);
955 }
956
957 if (global.tune.bufsize != 16384) {
958 char str[6] = "\x00\x05"; /* max_frame_size */
959
960 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
961 * match bufsize - rewrite size, but at the moment it seems
962 * that clients don't take care of it.
963 */
964 write_n32(str + 2, global.tune.bufsize);
965 chunk_memcat(&buf, str, 6);
966 }
967
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200968 h2_set_frame_size(buf.area, buf.data - 9);
969 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200970 if (unlikely(ret <= 0)) {
971 if (!ret) {
972 h2c->flags |= H2_CF_MUX_MFULL;
973 h2c->flags |= H2_CF_DEM_MROOM;
974 return 0;
975 }
976 else {
977 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
978 return 0;
979 }
980 }
981 return ret;
982}
983
Willy Tarreau52eed752017-09-22 15:05:09 +0200984/* Try to receive a connection preface, then upon success try to send our
985 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
986 * missing data. It may return an error in h2c.
987 */
988static int h2c_frt_recv_preface(struct h2c *h2c)
989{
990 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200991 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200992
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200993 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200994
995 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200996 if (ret1 < 0)
997 sess_log(h2c->conn->owner);
998
Willy Tarreau52eed752017-09-22 15:05:09 +0200999 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1000 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1001 return 0;
1002 }
1003
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001004 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001005 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001006 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001007
Willy Tarreaube5b7152017-09-25 16:25:39 +02001008 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001009}
1010
Willy Tarreau01b44822018-10-03 14:26:37 +02001011/* Try to send a connection preface, then upon success try to send our
1012 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1013 * missing data. It may return an error in h2c.
1014 */
1015static int h2c_bck_send_preface(struct h2c *h2c)
1016{
1017 struct buffer *res;
1018
1019 if (h2c_mux_busy(h2c, NULL)) {
1020 h2c->flags |= H2_CF_DEM_MBUSY;
1021 return 0;
1022 }
1023
1024 res = h2_get_buf(h2c, &h2c->mbuf);
1025 if (!res) {
1026 h2c->flags |= H2_CF_MUX_MALLOC;
1027 h2c->flags |= H2_CF_DEM_MROOM;
1028 return 0;
1029 }
1030
1031 if (!b_data(res)) {
1032 /* preface not yet sent */
1033 b_istput(res, ist(H2_CONN_PREFACE));
1034 }
1035
1036 return h2c_send_settings(h2c);
1037}
1038
Willy Tarreau081d4722017-05-16 21:51:05 +02001039/* try to send a GOAWAY frame on the connection to report an error or a graceful
1040 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1041 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1042 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1043 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1044 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1045 * on unrecoverable failure. It will not attempt to send one again in this last
1046 * case so that it is safe to use h2c_error() to report such errors.
1047 */
1048static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1049{
1050 struct buffer *res;
1051 char str[17];
1052 int ret;
1053
1054 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1055 return 1; // claim that it worked
1056
1057 if (h2c_mux_busy(h2c, h2s)) {
1058 if (h2s)
1059 h2s->flags |= H2_SF_BLK_MBUSY;
1060 else
1061 h2c->flags |= H2_CF_DEM_MBUSY;
1062 return 0;
1063 }
1064
Willy Tarreau44e973f2018-03-01 17:49:30 +01001065 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001066 if (!res) {
1067 h2c->flags |= H2_CF_MUX_MALLOC;
1068 if (h2s)
1069 h2s->flags |= H2_SF_BLK_MROOM;
1070 else
1071 h2c->flags |= H2_CF_DEM_MROOM;
1072 return 0;
1073 }
1074
1075 /* len: 8, type: 7, flags: none, sid: 0 */
1076 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1077
1078 if (h2c->last_sid < 0)
1079 h2c->last_sid = h2c->max_id;
1080
1081 write_n32(str + 9, h2c->last_sid);
1082 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001083 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001084 if (unlikely(ret <= 0)) {
1085 if (!ret) {
1086 h2c->flags |= H2_CF_MUX_MFULL;
1087 if (h2s)
1088 h2s->flags |= H2_SF_BLK_MROOM;
1089 else
1090 h2c->flags |= H2_CF_DEM_MROOM;
1091 return 0;
1092 }
1093 else {
1094 /* we cannot report this error using GOAWAY, so we mark
1095 * it and claim a success.
1096 */
1097 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1098 h2c->flags |= H2_CF_GOAWAY_FAILED;
1099 return 1;
1100 }
1101 }
1102 h2c->flags |= H2_CF_GOAWAY_SENT;
1103 return ret;
1104}
1105
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001106/* Try to send an RST_STREAM frame on the connection for the indicated stream
1107 * during mux operations. This stream must be valid and cannot be closed
1108 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1109 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1110 * not yet.
1111 *
1112 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1113 * to write the message, it subscribes the stream to future notifications.
1114 */
1115static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1116{
1117 struct buffer *res;
1118 char str[13];
1119 int ret;
1120
1121 if (!h2s || h2s->st == H2_SS_CLOSED)
1122 return 1;
1123
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001124 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1125 * RST_STREAM in response to a RST_STREAM frame.
1126 */
1127 if (h2c->dft == H2_FT_RST_STREAM) {
1128 ret = 1;
1129 goto ignore;
1130 }
1131
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001132 if (h2c_mux_busy(h2c, h2s)) {
1133 h2s->flags |= H2_SF_BLK_MBUSY;
1134 return 0;
1135 }
1136
Willy Tarreau44e973f2018-03-01 17:49:30 +01001137 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001138 if (!res) {
1139 h2c->flags |= H2_CF_MUX_MALLOC;
1140 h2s->flags |= H2_SF_BLK_MROOM;
1141 return 0;
1142 }
1143
1144 /* len: 4, type: 3, flags: none */
1145 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1146 write_n32(str + 5, h2s->id);
1147 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001148 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001149
1150 if (unlikely(ret <= 0)) {
1151 if (!ret) {
1152 h2c->flags |= H2_CF_MUX_MFULL;
1153 h2s->flags |= H2_SF_BLK_MROOM;
1154 return 0;
1155 }
1156 else {
1157 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1158 return 0;
1159 }
1160 }
1161
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001162 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001163 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001164 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001165 return ret;
1166}
1167
1168/* Try to send an RST_STREAM frame on the connection for the stream being
1169 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1170 * error code unless the stream's state already is IDLE or CLOSED in which
1171 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1172 * it was not yet.
1173 *
1174 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1175 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001176 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001177 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001178 */
1179static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1180{
1181 struct buffer *res;
1182 char str[13];
1183 int ret;
1184
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001185 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1186 * RST_STREAM in response to a RST_STREAM frame.
1187 */
1188 if (h2c->dft == H2_FT_RST_STREAM) {
1189 ret = 1;
1190 goto ignore;
1191 }
1192
Willy Tarreau27a84c92017-10-17 08:10:17 +02001193 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001194 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001195 return 0;
1196 }
1197
Willy Tarreau44e973f2018-03-01 17:49:30 +01001198 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001199 if (!res) {
1200 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001201 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001202 return 0;
1203 }
1204
1205 /* len: 4, type: 3, flags: none */
1206 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001207
Willy Tarreau27a84c92017-10-17 08:10:17 +02001208 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001209 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001210 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001211
Willy Tarreau27a84c92017-10-17 08:10:17 +02001212 if (unlikely(ret <= 0)) {
1213 if (!ret) {
1214 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001215 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001216 return 0;
1217 }
1218 else {
1219 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1220 return 0;
1221 }
1222 }
1223
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001224 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001225 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001226 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001227 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001228 }
1229
Willy Tarreau27a84c92017-10-17 08:10:17 +02001230 return ret;
1231}
1232
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001233/* try to send an empty DATA frame with the ES flag set to notify about the
1234 * end of stream and match a shutdown(write). If an ES was already sent as
1235 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1236 * on success or zero if nothing was done. In case of lack of room to write the
1237 * message, it subscribes the requesting stream to future notifications.
1238 */
1239static int h2_send_empty_data_es(struct h2s *h2s)
1240{
1241 struct h2c *h2c = h2s->h2c;
1242 struct buffer *res;
1243 char str[9];
1244 int ret;
1245
Willy Tarreau721c9742017-11-07 11:05:42 +01001246 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001247 return 1;
1248
1249 if (h2c_mux_busy(h2c, h2s)) {
1250 h2s->flags |= H2_SF_BLK_MBUSY;
1251 return 0;
1252 }
1253
Willy Tarreau44e973f2018-03-01 17:49:30 +01001254 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001255 if (!res) {
1256 h2c->flags |= H2_CF_MUX_MALLOC;
1257 h2s->flags |= H2_SF_BLK_MROOM;
1258 return 0;
1259 }
1260
1261 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1262 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1263 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001264 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001265 if (likely(ret > 0)) {
1266 h2s->flags |= H2_SF_ES_SENT;
1267 }
1268 else if (!ret) {
1269 h2c->flags |= H2_CF_MUX_MFULL;
1270 h2s->flags |= H2_SF_BLK_MROOM;
1271 return 0;
1272 }
1273 else {
1274 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1275 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001276 }
1277 return ret;
1278}
1279
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001280/* wake the streams attached to the connection, whose id is greater than <last>,
1281 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001282 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1283 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001284 */
1285static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1286{
1287 struct eb32_node *node;
1288 struct h2s *h2s;
1289
1290 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001291 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001292
1293 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001294 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001295
1296 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1297 while (node) {
1298 h2s = container_of(node, struct h2s, by_id);
1299 if (h2s->id <= last)
1300 break;
1301 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001302
1303 if (!h2s->cs) {
1304 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001305 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001306 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001307 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001308
1309 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001310 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1311 h2s->cs->flags |= CS_FL_ERROR;
1312
Willy Tarreauf830f012018-12-19 17:44:55 +01001313 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001314
Willy Tarreaua8519352018-12-18 16:44:28 +01001315 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001316 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001317 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001318 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001319 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001320 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001321 }
1322}
1323
Willy Tarreau3421aba2017-07-27 15:41:03 +02001324/* Increase all streams' outgoing window size by the difference passed in
1325 * argument. This is needed upon receipt of the settings frame if the initial
1326 * window size is different. The difference may be negative and the resulting
1327 * window size as well, for the time it takes to receive some window updates.
1328 */
1329static void h2c_update_all_ws(struct h2c *h2c, int diff)
1330{
1331 struct h2s *h2s;
1332 struct eb32_node *node;
1333
1334 if (!diff)
1335 return;
1336
1337 node = eb32_first(&h2c->streams_by_id);
1338 while (node) {
1339 h2s = container_of(node, struct h2s, by_id);
1340 h2s->mws += diff;
1341 node = eb32_next(node);
1342 }
1343}
1344
1345/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1346 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1347 * return an error in h2c. Described in RFC7540#6.5.
1348 */
1349static int h2c_handle_settings(struct h2c *h2c)
1350{
1351 unsigned int offset;
1352 int error;
1353
1354 if (h2c->dff & H2_F_SETTINGS_ACK) {
1355 if (h2c->dfl) {
1356 error = H2_ERR_FRAME_SIZE_ERROR;
1357 goto fail;
1358 }
1359 return 1;
1360 }
1361
1362 if (h2c->dsi != 0) {
1363 error = H2_ERR_PROTOCOL_ERROR;
1364 goto fail;
1365 }
1366
1367 if (h2c->dfl % 6) {
1368 error = H2_ERR_FRAME_SIZE_ERROR;
1369 goto fail;
1370 }
1371
1372 /* that's the limit we can process */
1373 if (h2c->dfl > global.tune.bufsize) {
1374 error = H2_ERR_FRAME_SIZE_ERROR;
1375 goto fail;
1376 }
1377
1378 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001379 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001380 return 0;
1381
1382 /* parse the frame */
1383 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001384 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1385 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001386
1387 switch (type) {
1388 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1389 /* we need to update all existing streams with the
1390 * difference from the previous iws.
1391 */
1392 if (arg < 0) { // RFC7540#6.5.2
1393 error = H2_ERR_FLOW_CONTROL_ERROR;
1394 goto fail;
1395 }
1396 h2c_update_all_ws(h2c, arg - h2c->miw);
1397 h2c->miw = arg;
1398 break;
1399 case H2_SETTINGS_MAX_FRAME_SIZE:
1400 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1401 error = H2_ERR_PROTOCOL_ERROR;
1402 goto fail;
1403 }
1404 h2c->mfs = arg;
1405 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001406 case H2_SETTINGS_ENABLE_PUSH:
1407 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1408 error = H2_ERR_PROTOCOL_ERROR;
1409 goto fail;
1410 }
1411 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001412 }
1413 }
1414
1415 /* need to ACK this frame now */
1416 h2c->st0 = H2_CS_FRAME_A;
1417 return 1;
1418 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001419 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001420 h2c_error(h2c, error);
1421 return 0;
1422}
1423
1424/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1425 * success or one of the h2_status values.
1426 */
1427static int h2c_ack_settings(struct h2c *h2c)
1428{
1429 struct buffer *res;
1430 char str[9];
1431 int ret = -1;
1432
1433 if (h2c_mux_busy(h2c, NULL)) {
1434 h2c->flags |= H2_CF_DEM_MBUSY;
1435 return 0;
1436 }
1437
Willy Tarreau44e973f2018-03-01 17:49:30 +01001438 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001439 if (!res) {
1440 h2c->flags |= H2_CF_MUX_MALLOC;
1441 h2c->flags |= H2_CF_DEM_MROOM;
1442 return 0;
1443 }
1444
1445 memcpy(str,
1446 "\x00\x00\x00" /* length : 0 (no data) */
1447 "\x04" "\x01" /* type : 4, flags : ACK */
1448 "\x00\x00\x00\x00" /* stream ID */, 9);
1449
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001450 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001451 if (unlikely(ret <= 0)) {
1452 if (!ret) {
1453 h2c->flags |= H2_CF_MUX_MFULL;
1454 h2c->flags |= H2_CF_DEM_MROOM;
1455 return 0;
1456 }
1457 else {
1458 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1459 return 0;
1460 }
1461 }
1462 return ret;
1463}
1464
Willy Tarreaucf68c782017-10-10 17:11:41 +02001465/* processes a PING frame and schedules an ACK if needed. The caller must pass
1466 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1467 * missing data. It may return an error in h2c.
1468 */
1469static int h2c_handle_ping(struct h2c *h2c)
1470{
1471 /* frame length must be exactly 8 */
1472 if (h2c->dfl != 8) {
1473 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1474 return 0;
1475 }
1476
1477 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001478 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001479 h2c->st0 = H2_CS_FRAME_A;
1480 return 1;
1481}
1482
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001483/* Try to send a window update for stream id <sid> and value <increment>.
1484 * Returns > 0 on success or zero on missing room or failure. It may return an
1485 * error in h2c.
1486 */
1487static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1488{
1489 struct buffer *res;
1490 char str[13];
1491 int ret = -1;
1492
1493 if (h2c_mux_busy(h2c, NULL)) {
1494 h2c->flags |= H2_CF_DEM_MBUSY;
1495 return 0;
1496 }
1497
Willy Tarreau44e973f2018-03-01 17:49:30 +01001498 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001499 if (!res) {
1500 h2c->flags |= H2_CF_MUX_MALLOC;
1501 h2c->flags |= H2_CF_DEM_MROOM;
1502 return 0;
1503 }
1504
1505 /* length: 4, type: 8, flags: none */
1506 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1507 write_n32(str + 5, sid);
1508 write_n32(str + 9, increment);
1509
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001510 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001511
1512 if (unlikely(ret <= 0)) {
1513 if (!ret) {
1514 h2c->flags |= H2_CF_MUX_MFULL;
1515 h2c->flags |= H2_CF_DEM_MROOM;
1516 return 0;
1517 }
1518 else {
1519 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1520 return 0;
1521 }
1522 }
1523 return ret;
1524}
1525
1526/* try to send pending window update for the connection. It's safe to call it
1527 * with no pending updates. Returns > 0 on success or zero on missing room or
1528 * failure. It may return an error in h2c.
1529 */
1530static int h2c_send_conn_wu(struct h2c *h2c)
1531{
1532 int ret = 1;
1533
1534 if (h2c->rcvd_c <= 0)
1535 return 1;
1536
1537 /* send WU for the connection */
1538 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1539 if (ret > 0)
1540 h2c->rcvd_c = 0;
1541
1542 return ret;
1543}
1544
1545/* try to send pending window update for the current dmux stream. It's safe to
1546 * call it with no pending updates. Returns > 0 on success or zero on missing
1547 * room or failure. It may return an error in h2c.
1548 */
1549static int h2c_send_strm_wu(struct h2c *h2c)
1550{
1551 int ret = 1;
1552
1553 if (h2c->rcvd_s <= 0)
1554 return 1;
1555
1556 /* send WU for the stream */
1557 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1558 if (ret > 0)
1559 h2c->rcvd_s = 0;
1560
1561 return ret;
1562}
1563
Willy Tarreaucf68c782017-10-10 17:11:41 +02001564/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1565 * success, 0 on missing data or one of the h2_status values.
1566 */
1567static int h2c_ack_ping(struct h2c *h2c)
1568{
1569 struct buffer *res;
1570 char str[17];
1571 int ret = -1;
1572
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001573 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001574 return 0;
1575
1576 if (h2c_mux_busy(h2c, NULL)) {
1577 h2c->flags |= H2_CF_DEM_MBUSY;
1578 return 0;
1579 }
1580
Willy Tarreau44e973f2018-03-01 17:49:30 +01001581 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001582 if (!res) {
1583 h2c->flags |= H2_CF_MUX_MALLOC;
1584 h2c->flags |= H2_CF_DEM_MROOM;
1585 return 0;
1586 }
1587
1588 memcpy(str,
1589 "\x00\x00\x08" /* length : 8 (same payload) */
1590 "\x06" "\x01" /* type : 6, flags : ACK */
1591 "\x00\x00\x00\x00" /* stream ID */, 9);
1592
1593 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001594 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001595
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001596 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001597 if (unlikely(ret <= 0)) {
1598 if (!ret) {
1599 h2c->flags |= H2_CF_MUX_MFULL;
1600 h2c->flags |= H2_CF_DEM_MROOM;
1601 return 0;
1602 }
1603 else {
1604 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1605 return 0;
1606 }
1607 }
1608 return ret;
1609}
1610
Willy Tarreau26f95952017-07-27 17:18:30 +02001611/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1612 * Returns > 0 on success or zero on missing data. It may return an error in
1613 * h2c or h2s. Described in RFC7540#6.9.
1614 */
1615static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1616{
1617 int32_t inc;
1618 int error;
1619
1620 if (h2c->dfl != 4) {
1621 error = H2_ERR_FRAME_SIZE_ERROR;
1622 goto conn_err;
1623 }
1624
1625 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001626 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001627 return 0;
1628
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001629 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001630
1631 if (h2c->dsi != 0) {
1632 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001633
1634 /* it's not an error to receive WU on a closed stream */
1635 if (h2s->st == H2_SS_CLOSED)
1636 return 1;
1637
1638 if (!inc) {
1639 error = H2_ERR_PROTOCOL_ERROR;
1640 goto strm_err;
1641 }
1642
1643 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1644 error = H2_ERR_FLOW_CONTROL_ERROR;
1645 goto strm_err;
1646 }
1647
1648 h2s->mws += inc;
1649 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1650 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001651 if (h2s->send_wait)
1652 LIST_ADDQ(&h2c->send_list, &h2s->list);
1653
Willy Tarreau26f95952017-07-27 17:18:30 +02001654 }
1655 }
1656 else {
1657 /* connection window update */
1658 if (!inc) {
1659 error = H2_ERR_PROTOCOL_ERROR;
1660 goto conn_err;
1661 }
1662
1663 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1664 error = H2_ERR_FLOW_CONTROL_ERROR;
1665 goto conn_err;
1666 }
1667
1668 h2c->mws += inc;
1669 }
1670
1671 return 1;
1672
1673 conn_err:
1674 h2c_error(h2c, error);
1675 return 0;
1676
1677 strm_err:
1678 if (h2s) {
1679 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001680 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001681 }
1682 else
1683 h2c_error(h2c, error);
1684 return 0;
1685}
1686
Willy Tarreaue96b0922017-10-30 00:28:29 +01001687/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1688 * the last ID. Returns > 0 on success or zero on missing data. It may return
1689 * an error in h2c. Described in RFC7540#6.8.
1690 */
1691static int h2c_handle_goaway(struct h2c *h2c)
1692{
1693 int error;
1694 int last;
1695
1696 if (h2c->dsi != 0) {
1697 error = H2_ERR_PROTOCOL_ERROR;
1698 goto conn_err;
1699 }
1700
1701 if (h2c->dfl < 8) {
1702 error = H2_ERR_FRAME_SIZE_ERROR;
1703 goto conn_err;
1704 }
1705
1706 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001707 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001708 return 0;
1709
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001710 last = h2_get_n32(&h2c->dbuf, 0);
1711 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001712 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001713 if (h2c->last_sid < 0)
1714 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001715 return 1;
1716
1717 conn_err:
1718 h2c_error(h2c, error);
1719 return 0;
1720}
1721
Willy Tarreau92153fc2017-12-03 19:46:19 +01001722/* processes a PRIORITY frame, and either skips it or rejects if it is
1723 * invalid. Returns > 0 on success or zero on missing data. It may return
1724 * an error in h2c. Described in RFC7540#6.3.
1725 */
1726static int h2c_handle_priority(struct h2c *h2c)
1727{
1728 int error;
1729
1730 if (h2c->dsi == 0) {
1731 error = H2_ERR_PROTOCOL_ERROR;
1732 goto conn_err;
1733 }
1734
1735 if (h2c->dfl != 5) {
1736 error = H2_ERR_FRAME_SIZE_ERROR;
1737 goto conn_err;
1738 }
1739
1740 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001741 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001742 return 0;
1743
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001744 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001745 /* 7540#5.3 : can't depend on itself */
1746 error = H2_ERR_PROTOCOL_ERROR;
1747 goto conn_err;
1748 }
1749 return 1;
1750
1751 conn_err:
1752 h2c_error(h2c, error);
1753 return 0;
1754}
1755
Willy Tarreaucd234e92017-08-18 10:59:39 +02001756/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1757 * Returns > 0 on success or zero on missing data. It may return an error in
1758 * h2c. Described in RFC7540#6.4.
1759 */
1760static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1761{
1762 int error;
1763
1764 if (h2c->dsi == 0) {
1765 error = H2_ERR_PROTOCOL_ERROR;
1766 goto conn_err;
1767 }
1768
Willy Tarreaucd234e92017-08-18 10:59:39 +02001769 if (h2c->dfl != 4) {
1770 error = H2_ERR_FRAME_SIZE_ERROR;
1771 goto conn_err;
1772 }
1773
1774 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001775 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001776 return 0;
1777
1778 /* late RST, already handled */
1779 if (h2s->st == H2_SS_CLOSED)
1780 return 1;
1781
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001782 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001783 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001784
1785 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001786 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001787 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001788 }
1789
1790 h2s->flags |= H2_SF_RST_RCVD;
1791 return 1;
1792
1793 conn_err:
1794 h2c_error(h2c, error);
1795 return 0;
1796}
1797
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001798/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1799 * It may return an error in h2c or h2s. The caller must consider that the
1800 * return value is the new h2s in case one was allocated (most common case).
1801 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001802 * errors here are reported as connection errors since it's impossible to
1803 * recover from such errors after the compression context has been altered.
1804 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001805static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001806{
1807 int error;
1808
1809 if (!h2c->dfl) {
1810 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001811 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001812 goto strm_err;
1813 }
1814
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001815 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001816 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001817
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001818 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001819 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001820
Willy Tarreauf2101912018-07-19 10:11:38 +02001821 if (h2c->flags & H2_CF_DEM_TOOMANY)
1822 return 0; // too many cs still present
1823
Willy Tarreau13278b42017-10-13 19:23:14 +02001824 /* now either the frame is complete or the buffer is complete */
1825 if (h2s->st != H2_SS_IDLE) {
1826 /* FIXME: stream already exists, this is only allowed for
1827 * trailers (not supported for now).
1828 */
1829 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001830 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001831 goto conn_err;
1832 }
1833 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1834 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1835 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001836 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001837 goto conn_err;
1838 }
1839
Willy Tarreau22de8d32018-09-05 19:55:58 +02001840 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001841 * positively from h2c_frt_stream_new(), the stream will report the error,
1842 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001843 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001844 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001845 if (!h2s) {
1846 error = H2_ERR_INTERNAL_ERROR;
1847 goto conn_err;
1848 }
1849
1850 h2s->st = H2_SS_OPEN;
1851 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1852 h2s->st = H2_SS_HREM;
1853 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001854 /* note: cs cannot be null for now (just created above) */
1855 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001856 }
1857
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001858 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001859 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001860
Willy Tarreau8f650c32017-11-21 19:36:21 +01001861 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001862 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001863
Willy Tarreau721c9742017-11-07 11:05:42 +01001864 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001865 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001866 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001867 }
1868 else {
1869 /* update the max stream ID if the request is being processed */
1870 if (h2s->id > h2c->max_id)
1871 h2c->max_id = h2s->id;
1872 }
1873
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001874 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001875
1876 conn_err:
1877 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001878 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001879
1880 strm_err:
1881 if (h2s) {
1882 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001883 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001884 }
1885 else
1886 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001887 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001888}
1889
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001890/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1891 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1892 * errors here are reported as connection errors since it's impossible to
1893 * recover from such errors after the compression context has been altered.
1894 */
1895static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1896{
1897 int error;
1898
1899 if (!h2c->dfl) {
1900 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1901 sess_log(h2c->conn->owner);
1902 goto strm_err;
1903 }
1904
1905 if (!b_size(&h2c->dbuf))
1906 return NULL; // empty buffer
1907
1908 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1909 return NULL; // incomplete frame
1910
1911 if (h2c->flags & H2_CF_DEM_TOOMANY)
1912 return 0; // too many cs still present
1913
1914 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1915 h2s->flags |= H2_SF_ES_RCVD;
1916 h2s->cs->flags |= CS_FL_REOS;
1917 }
1918
1919 if (!h2s_decode_headers(h2s))
1920 return NULL;
1921
1922 if (h2c->st0 >= H2_CS_ERROR)
1923 return NULL;
1924
1925 if (h2s->st >= H2_SS_ERROR) {
1926 /* stream error : send RST_STREAM */
1927 h2c->st0 = H2_CS_FRAME_E;
1928 }
1929
1930 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1931 h2s->st = H2_SS_ERROR;
1932 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1933 h2s->st = H2_SS_HREM;
1934 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1935 h2s_close(h2s);
1936
1937 return h2s;
1938
1939 conn_err:
1940 h2c_error(h2c, error);
1941 return NULL;
1942
1943 strm_err:
1944 if (h2s) {
1945 h2s_error(h2s, error);
1946 h2c->st0 = H2_CS_FRAME_E;
1947 }
1948 else
1949 h2c_error(h2c, error);
1950 return NULL;
1951}
1952
Willy Tarreau454f9052017-10-26 19:40:35 +02001953/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1954 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1955 */
1956static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1957{
1958 int error;
1959
1960 /* note that empty DATA frames are perfectly valid and sometimes used
1961 * to signal an end of stream (with the ES flag).
1962 */
1963
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001964 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001965 return 0; // empty buffer
1966
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001967 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001968 return 0; // incomplete frame
1969
1970 /* now either the frame is complete or the buffer is complete */
1971
1972 if (!h2c->dsi) {
1973 /* RFC7540#6.1 */
1974 error = H2_ERR_PROTOCOL_ERROR;
1975 goto conn_err;
1976 }
1977
1978 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1979 /* RFC7540#6.1 */
1980 error = H2_ERR_STREAM_CLOSED;
1981 goto strm_err;
1982 }
1983
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001984 if (!h2_frt_transfer_data(h2s))
1985 return 0;
1986
Willy Tarreau454f9052017-10-26 19:40:35 +02001987 /* call the upper layers to process the frame, then let the upper layer
1988 * notify the stream about any change.
1989 */
1990 if (!h2s->cs) {
1991 error = H2_ERR_STREAM_CLOSED;
1992 goto strm_err;
1993 }
1994
Willy Tarreau8f650c32017-11-21 19:36:21 +01001995 if (h2c->st0 >= H2_CS_ERROR)
1996 return 0;
1997
Willy Tarreau721c9742017-11-07 11:05:42 +01001998 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001999 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002000 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002001 }
2002
2003 /* check for completion : the callee will change this to FRAME_A or
2004 * FRAME_H once done.
2005 */
2006 if (h2c->st0 == H2_CS_FRAME_P)
2007 return 0;
2008
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002009
2010 /* last frame */
2011 if (h2c->dff & H2_F_DATA_END_STREAM) {
2012 h2s->st = H2_SS_HREM;
2013 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002014 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002015 }
2016
Willy Tarreau454f9052017-10-26 19:40:35 +02002017 return 1;
2018
2019 conn_err:
2020 h2c_error(h2c, error);
2021 return 0;
2022
2023 strm_err:
2024 if (h2s) {
2025 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002026 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002027 }
2028 else
2029 h2c_error(h2c, error);
2030 return 0;
2031}
2032
Willy Tarreaubc933932017-10-09 16:21:43 +02002033/* process Rx frames to be demultiplexed */
2034static void h2_process_demux(struct h2c *h2c)
2035{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002036 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002037
Willy Tarreau081d4722017-05-16 21:51:05 +02002038 if (h2c->st0 >= H2_CS_ERROR)
2039 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002040
2041 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2042 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002043 if (h2c->flags & H2_CF_IS_BACK)
2044 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002045 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2046 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002047 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002048 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002049 sess_log(h2c->conn->owner);
2050 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002051 goto fail;
2052 }
2053
2054 h2c->max_id = 0;
2055 h2c->st0 = H2_CS_SETTINGS1;
2056 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002057
2058 if (h2c->st0 == H2_CS_SETTINGS1) {
2059 struct h2_fh hdr;
2060
2061 /* ensure that what is pending is a valid SETTINGS frame
2062 * without an ACK.
2063 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002064 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002065 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002066 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002067 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002068 sess_log(h2c->conn->owner);
2069 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002070 goto fail;
2071 }
2072
2073 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2074 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2075 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2076 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002077 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002078 goto fail;
2079 }
2080
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002081 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002082 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2083 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2084 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002085 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002086 goto fail;
2087 }
2088
2089 /* that's OK, switch to FRAME_P to process it */
2090 h2c->dfl = hdr.len;
2091 h2c->dsi = hdr.sid;
2092 h2c->dft = hdr.ft;
2093 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002094 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002095 h2c->st0 = H2_CS_FRAME_P;
2096 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002097 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002098
2099 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002100 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002101 int ret = 0;
2102
2103 if (h2c->st0 >= H2_CS_ERROR)
2104 break;
2105
2106 if (h2c->st0 == H2_CS_FRAME_H) {
2107 struct h2_fh hdr;
2108
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002109 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002110 break;
2111
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002112 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002113 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2114 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002115 if (!h2c->nb_streams) {
2116 /* only log if no other stream can report the error */
2117 sess_log(h2c->conn->owner);
2118 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002119 break;
2120 }
2121
2122 h2c->dfl = hdr.len;
2123 h2c->dsi = hdr.sid;
2124 h2c->dft = hdr.ft;
2125 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002126 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002127 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002128 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002129 }
2130
2131 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002132 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2133
Willy Tarreau567beb82018-12-18 16:52:44 +01002134 if (tmp_h2s != h2s && h2s && h2s->cs &&
2135 (b_data(&h2s->rxbuf) ||
2136 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002137 /* we may have to signal the upper layers */
2138 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002139 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002140 }
2141 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002142
Willy Tarreaud7901432017-12-29 11:34:40 +01002143 if (h2c->st0 == H2_CS_FRAME_E)
2144 goto strm_err;
2145
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002146 if (h2s->st == H2_SS_IDLE &&
2147 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2148 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2149 * this state MUST be treated as a connection error
2150 */
2151 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2152 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002153 if (!h2c->nb_streams) {
2154 /* only log if no other stream can report the error */
2155 sess_log(h2c->conn->owner);
2156 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002157 break;
2158 }
2159
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002160 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2161 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2162 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2163 * this state MUST be treated as a stream error
2164 */
2165 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2166 goto strm_err;
2167 }
2168
Willy Tarreauab837502017-12-27 15:07:30 +01002169 /* Below the management of frames received in closed state is a
2170 * bit hackish because the spec makes strong differences between
2171 * streams closed by receiving RST, sending RST, and seeing ES
2172 * in both directions. In addition to this, the creation of a
2173 * new stream reusing the identifier of a closed one will be
2174 * detected here. Given that we cannot keep track of all closed
2175 * streams forever, we consider that unknown closed streams were
2176 * closed on RST received, which allows us to respond with an
2177 * RST without breaking the connection (eg: to abort a transfer).
2178 * Some frames have to be silently ignored as well.
2179 */
2180 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2181 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2182 /* #5.1.1: The identifier of a newly
2183 * established stream MUST be numerically
2184 * greater than all streams that the initiating
2185 * endpoint has opened or reserved. This
2186 * governs streams that are opened using a
2187 * HEADERS frame and streams that are reserved
2188 * using PUSH_PROMISE. An endpoint that
2189 * receives an unexpected stream identifier
2190 * MUST respond with a connection error.
2191 */
2192 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2193 goto strm_err;
2194 }
2195
2196 if (h2s->flags & H2_SF_RST_RCVD) {
2197 /* RFC7540#5.1:closed: an endpoint that
2198 * receives any frame other than PRIORITY after
2199 * receiving a RST_STREAM MUST treat that as a
2200 * stream error of type STREAM_CLOSED.
2201 *
2202 * Note that old streams fall into this category
2203 * and will lead to an RST being sent.
2204 */
2205 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2206 h2c->st0 = H2_CS_FRAME_E;
2207 goto strm_err;
2208 }
2209
2210 /* RFC7540#5.1:closed: if this state is reached as a
2211 * result of sending a RST_STREAM frame, the peer that
2212 * receives the RST_STREAM might have already sent
2213 * frames on the stream that cannot be withdrawn. An
2214 * endpoint MUST ignore frames that it receives on
2215 * closed streams after it has sent a RST_STREAM
2216 * frame. An endpoint MAY choose to limit the period
2217 * over which it ignores frames and treat frames that
2218 * arrive after this time as being in error.
2219 */
2220 if (!(h2s->flags & H2_SF_RST_SENT)) {
2221 /* RFC7540#5.1:closed: any frame other than
2222 * PRIO/WU/RST in this state MUST be treated as
2223 * a connection error
2224 */
2225 if (h2c->dft != H2_FT_RST_STREAM &&
2226 h2c->dft != H2_FT_PRIORITY &&
2227 h2c->dft != H2_FT_WINDOW_UPDATE) {
2228 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2229 goto strm_err;
2230 }
2231 }
2232 }
2233
Willy Tarreauc0da1962017-10-30 18:38:00 +01002234#if 0
2235 // problem below: it is not possible to completely ignore such
2236 // streams as we need to maintain the compression state as well
2237 // and for this we need to completely process these frames (eg:
2238 // HEADERS frames) as well as counting DATA frames to emit
2239 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2240 // This is a typical case of layer violation where the
2241 // transported contents are critical to the connection's
2242 // validity and must be ignored at the same time :-(
2243
2244 /* graceful shutdown, ignore streams whose ID is higher than
2245 * the one advertised in GOAWAY. RFC7540#6.8.
2246 */
2247 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002248 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2249 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002250 h2c->dfl -= ret;
2251 ret = h2c->dfl == 0;
2252 goto strm_err;
2253 }
2254#endif
2255
Willy Tarreau7e98c052017-10-10 15:56:59 +02002256 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002257 case H2_FT_SETTINGS:
2258 if (h2c->st0 == H2_CS_FRAME_P)
2259 ret = h2c_handle_settings(h2c);
2260
2261 if (h2c->st0 == H2_CS_FRAME_A)
2262 ret = h2c_ack_settings(h2c);
2263 break;
2264
Willy Tarreaucf68c782017-10-10 17:11:41 +02002265 case H2_FT_PING:
2266 if (h2c->st0 == H2_CS_FRAME_P)
2267 ret = h2c_handle_ping(h2c);
2268
2269 if (h2c->st0 == H2_CS_FRAME_A)
2270 ret = h2c_ack_ping(h2c);
2271 break;
2272
Willy Tarreau26f95952017-07-27 17:18:30 +02002273 case H2_FT_WINDOW_UPDATE:
2274 if (h2c->st0 == H2_CS_FRAME_P)
2275 ret = h2c_handle_window_update(h2c, h2s);
2276 break;
2277
Willy Tarreau61290ec2017-10-17 08:19:21 +02002278 case H2_FT_CONTINUATION:
2279 /* we currently don't support CONTINUATION frames since
2280 * we have nowhere to store the partial HEADERS frame.
2281 * Let's abort the stream on an INTERNAL_ERROR here.
2282 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002283 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002284 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002285 h2c->st0 = H2_CS_FRAME_E;
2286 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002287 break;
2288
Willy Tarreau13278b42017-10-13 19:23:14 +02002289 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002290 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002291 if (h2c->flags & H2_CF_IS_BACK)
2292 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2293 else
2294 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002295 if (tmp_h2s) {
2296 h2s = tmp_h2s;
2297 ret = 1;
2298 }
2299 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002300 break;
2301
Willy Tarreau454f9052017-10-26 19:40:35 +02002302 case H2_FT_DATA:
2303 if (h2c->st0 == H2_CS_FRAME_P)
2304 ret = h2c_frt_handle_data(h2c, h2s);
2305
2306 if (h2c->st0 == H2_CS_FRAME_A)
2307 ret = h2c_send_strm_wu(h2c);
2308 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002309
Willy Tarreau92153fc2017-12-03 19:46:19 +01002310 case H2_FT_PRIORITY:
2311 if (h2c->st0 == H2_CS_FRAME_P)
2312 ret = h2c_handle_priority(h2c);
2313 break;
2314
Willy Tarreaucd234e92017-08-18 10:59:39 +02002315 case H2_FT_RST_STREAM:
2316 if (h2c->st0 == H2_CS_FRAME_P)
2317 ret = h2c_handle_rst_stream(h2c, h2s);
2318 break;
2319
Willy Tarreaue96b0922017-10-30 00:28:29 +01002320 case H2_FT_GOAWAY:
2321 if (h2c->st0 == H2_CS_FRAME_P)
2322 ret = h2c_handle_goaway(h2c);
2323 break;
2324
Willy Tarreau1c661982017-10-30 13:52:01 +01002325 case H2_FT_PUSH_PROMISE:
2326 /* not permitted here, RFC7540#5.1 */
2327 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002328 if (!h2c->nb_streams) {
2329 /* only log if no other stream can report the error */
2330 sess_log(h2c->conn->owner);
2331 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002332 break;
2333
2334 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002335 default:
2336 /* drop frames that we ignore. They may be larger than
2337 * the buffer so we drain all of their contents until
2338 * we reach the end.
2339 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002340 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2341 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002342 h2c->dfl -= ret;
2343 ret = h2c->dfl == 0;
2344 }
2345
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002346 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002347 /* We may have to send an RST if not done yet */
2348 if (h2s->st == H2_SS_ERROR)
2349 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002350
Willy Tarreaua20a5192017-12-27 11:02:06 +01002351 if (h2c->st0 == H2_CS_FRAME_E)
2352 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002353
Willy Tarreau7e98c052017-10-10 15:56:59 +02002354 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002355 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002356 break;
2357
2358 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002359 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002360 h2c->st0 = H2_CS_FRAME_H;
2361 }
2362 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002363
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002364 if (h2c->rcvd_c > 0 &&
2365 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2366 h2c_send_conn_wu(h2c);
2367
Willy Tarreau52eed752017-09-22 15:05:09 +02002368 fail:
2369 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002370 if (h2s && h2s->cs &&
2371 (b_data(&h2s->rxbuf) ||
2372 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002373 /* we may have to signal the upper layers */
2374 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002375 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002376 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002377
2378 if (h2_recv_allowed(h2c))
2379 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002380}
2381
2382/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2383 * the end.
2384 */
2385static int h2_process_mux(struct h2c *h2c)
2386{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002387 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002388
Willy Tarreau01b44822018-10-03 14:26:37 +02002389 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2390 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2391 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2392 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2393 if (h2c->st0 == H2_CS_ERROR) {
2394 h2c->st0 = H2_CS_ERROR2;
2395 sess_log(h2c->conn->owner);
2396 }
2397 goto fail;
2398 }
2399 h2c->st0 = H2_CS_SETTINGS1;
2400 }
2401 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002402 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002403 return 1;
2404 }
2405
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002406 /* start by sending possibly pending window updates */
2407 if (h2c->rcvd_c > 0 &&
2408 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2409 h2c_send_conn_wu(h2c) < 0)
2410 goto fail;
2411
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002412 /* First we always process the flow control list because the streams
2413 * waiting there were already elected for immediate emission but were
2414 * blocked just on this.
2415 */
2416
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002417 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002418 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2419 h2c->st0 >= H2_CS_ERROR)
2420 break;
2421
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002422 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002423 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2424 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002425 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002426 LIST_DEL(&h2s->list);
2427 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002428 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002429 }
2430
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002431 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002432 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2433 break;
2434
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002435 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002436 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2437 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002438 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002439 LIST_DEL(&h2s->list);
2440 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002441 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002442 }
2443
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002444 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002445 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002446 if (h2c->st0 == H2_CS_ERROR) {
2447 if (h2c->max_id >= 0) {
2448 h2c_send_goaway_error(h2c, NULL);
2449 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2450 return 0;
2451 }
2452
2453 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2454 }
2455 return 1;
2456 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002457 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002458}
2459
Willy Tarreau62f52692017-10-08 23:01:42 +02002460
Willy Tarreau479998a2018-11-18 06:30:59 +01002461/* Attempt to read data, and subscribe if none available.
2462 * The function returns 1 if data has been received, otherwise zero.
2463 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002464static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002465{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002466 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002467 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002468 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002469 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002470
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002471 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002472 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002473
Willy Tarreau315d8072017-12-10 22:17:57 +01002474 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002475 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002476
Willy Tarreau44e973f2018-03-01 17:49:30 +01002477 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002478 if (!buf) {
2479 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002480 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002481 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002482
Olivier Houchard7505f942018-08-21 18:10:44 +02002483 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002484 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002485 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2486 /* HTX in use : try to pre-align the buffer like the
2487 * rxbufs will be to optimize memory copies. We'll make
2488 * sure that the frame header lands at the end of the
2489 * HTX block to alias it upon recv. We cannot use the
2490 * head because rcv_buf() will realign the buffer if
2491 * it's empty. Thus we cheat and pretend we already
2492 * have a few bytes there.
2493 */
2494 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002495 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002496 }
2497 else
2498 max = b_room(buf);
2499
Olivier Houchard7505f942018-08-21 18:10:44 +02002500 if (max)
2501 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2502 else
2503 ret = 0;
2504 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002505
Olivier Houchard53216e72018-10-10 15:46:36 +02002506 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002507 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002508
Olivier Houcharda1411e62018-08-17 18:42:48 +02002509 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002510 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002511 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002512 }
2513
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002514 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002515 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002516 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002517}
2518
Willy Tarreau479998a2018-11-18 06:30:59 +01002519/* Try to send data if possible.
2520 * The function returns 1 if data have been sent, otherwise zero.
2521 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002522static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002523{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002524 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002525 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002526 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002527
2528 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002529 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002530
Olivier Houchard7505f942018-08-21 18:10:44 +02002531
Willy Tarreaua2af5122017-10-09 11:56:46 +02002532 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2533 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002534 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002535 }
2536
Willy Tarreaubc933932017-10-09 16:21:43 +02002537 /* This loop is quite simple : it tries to fill as much as it can from
2538 * pending streams into the existing buffer until it's reportedly full
2539 * or the end of send requests is reached. Then it tries to send this
2540 * buffer's contents out, marks it not full if at least one byte could
2541 * be sent, and tries again.
2542 *
2543 * The snd_buf() function normally takes a "flags" argument which may
2544 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2545 * data immediately comes and CO_SFL_STREAMER to indicate that the
2546 * connection is streaming lots of data (used to increase TLS record
2547 * size at the expense of latency). The former can be sent any time
2548 * there's a buffer full flag, as it indicates at least one stream
2549 * attempted to send and failed so there are pending data. An
2550 * alternative would be to set it as long as there's an active stream
2551 * but that would be problematic for ACKs until we have an absolute
2552 * guarantee that all waiters have at least one byte to send. The
2553 * latter should possibly not be set for now.
2554 */
2555
2556 done = 0;
2557 while (!done) {
2558 unsigned int flags = 0;
2559
2560 /* fill as much as we can into the current buffer */
2561 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2562 done = h2_process_mux(h2c);
2563
2564 if (conn->flags & CO_FL_ERROR)
2565 break;
2566
2567 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2568 flags |= CO_SFL_MSG_MORE;
2569
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002570 if (b_data(&h2c->mbuf)) {
2571 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002572 if (!ret)
2573 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002574 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002575 b_del(&h2c->mbuf, ret);
2576 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002577 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002578
2579 /* wrote at least one byte, the buffer is not full anymore */
2580 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2581 }
2582
Willy Tarreaua2af5122017-10-09 11:56:46 +02002583 if (conn->flags & CO_FL_SOCK_WR_SH) {
2584 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002585 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002586 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002587 /* We're not full anymore, so we can wake any task that are waiting
2588 * for us.
2589 */
2590 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002591 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002592 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2593 struct h2s *, list);
2594 LIST_DEL(&h2s->list);
2595 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002596 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002597 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2598 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002599 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002600 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002601 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002602 /* We're done, no more to send */
2603 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002604 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002605schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002606 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2607 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002608 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002609}
2610
2611static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2612{
2613 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002614 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002615
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002616 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002617 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002618 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002619 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002620 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002621 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002622 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002623}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002624
Willy Tarreau62f52692017-10-08 23:01:42 +02002625/* callback called on any event by the connection handler.
2626 * It applies changes and returns zero, or < 0 if it wants immediate
2627 * destruction of the connection (which normally doesn not happen in h2).
2628 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002629static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002630{
Olivier Houchard7505f942018-08-21 18:10:44 +02002631 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002632
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002633 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002634 h2_process_demux(h2c);
2635
2636 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002637 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002638
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002639 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002640 h2c->flags &= ~H2_CF_DEM_DFULL;
2641 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002642 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002643
Willy Tarreau0b37d652018-10-03 10:33:02 +02002644 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002645 /* frontend is stopping, reload likely in progress, let's try
2646 * to announce a graceful shutdown if not yet done. We don't
2647 * care if it fails, it will be tried again later.
2648 */
2649 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2650 if (h2c->last_sid < 0)
2651 h2c->last_sid = (1U << 31) - 1;
2652 h2c_send_goaway_error(h2c, NULL);
2653 }
2654 }
2655
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002656 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002657 * If we received early data, and the handshake is done, wake
2658 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002659 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002660 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2661 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2662 struct eb32_node *node;
2663 struct h2s *h2s;
2664
2665 h2c->flags |= H2_CF_WAIT_FOR_HS;
2666 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2667
2668 while (node) {
2669 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002670 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002671 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002672 node = eb32_next(node);
2673 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002674 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002675
Willy Tarreau26bd7612017-10-09 16:47:04 +02002676 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002677 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2678 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2679 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002680 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002681
2682 if (eb_is_empty(&h2c->streams_by_id)) {
2683 /* no more stream, kill the connection now */
2684 h2_release(conn);
2685 return -1;
2686 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002687 }
2688
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002689 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002690 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002691
Olivier Houchard53216e72018-10-10 15:46:36 +02002692 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2693 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2694 (h2c->st0 != H2_CS_ERROR &&
2695 !b_data(&h2c->mbuf) &&
2696 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2697 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002698 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002699
Willy Tarreau3f133572017-10-31 19:21:06 +01002700 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002701 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002702 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002703 task_queue(h2c->task);
2704 }
2705 else
2706 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002707 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002708
Olivier Houchard7505f942018-08-21 18:10:44 +02002709 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002710 return 0;
2711}
2712
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002713static int h2_wake(struct connection *conn)
2714{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002715 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002716
2717 return (h2_process(h2c));
2718}
2719
Willy Tarreauea392822017-10-31 10:02:25 +01002720/* Connection timeout management. The principle is that if there's no receipt
2721 * nor sending for a certain amount of time, the connection is closed. If the
2722 * MUX buffer still has lying data or is not allocatable, the connection is
2723 * immediately killed. If it's allocatable and empty, we attempt to send a
2724 * GOAWAY frame.
2725 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002726static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002727{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002728 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002729 int expired = tick_is_expired(t->expire, now_ms);
2730
Willy Tarreau0975f112018-03-29 15:22:59 +02002731 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002732 return t;
2733
Willy Tarreau0975f112018-03-29 15:22:59 +02002734 task_delete(t);
2735 task_free(t);
2736
2737 if (!h2c) {
2738 /* resources were already deleted */
2739 return NULL;
2740 }
2741
2742 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002743 h2c_error(h2c, H2_ERR_NO_ERROR);
2744 h2_wake_some_streams(h2c, 0, 0);
2745
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002746 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002747 /* don't even try to send a GOAWAY, the buffer is stuck */
2748 h2c->flags |= H2_CF_GOAWAY_FAILED;
2749 }
2750
2751 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002752 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002753 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2754 h2c->flags |= H2_CF_GOAWAY_FAILED;
2755
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002756 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2757 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002758 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002759 b_del(&h2c->mbuf, ret);
2760 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002761 }
2762 }
Willy Tarreauea392822017-10-31 10:02:25 +01002763
Willy Tarreau0975f112018-03-29 15:22:59 +02002764 /* either we can release everything now or it will be done later once
2765 * the last stream closes.
2766 */
2767 if (eb_is_empty(&h2c->streams_by_id))
2768 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002769
Willy Tarreauea392822017-10-31 10:02:25 +01002770 return NULL;
2771}
2772
2773
Willy Tarreau62f52692017-10-08 23:01:42 +02002774/*******************************************/
2775/* functions below are used by the streams */
2776/*******************************************/
2777
2778/*
2779 * Attach a new stream to a connection
2780 * (Used for outgoing connections)
2781 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002782static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002783{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002784 struct conn_stream *cs;
2785 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002786 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002787
2788 cs = cs_new(conn);
2789 if (!cs)
2790 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002791 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002792 if (!h2s) {
2793 cs_free(cs);
2794 return NULL;
2795 }
2796 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002797}
2798
Willy Tarreaufafd3982018-11-18 21:29:20 +01002799/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2800 * We have to scan because we may have some orphan streams. It might be
2801 * beneficial to scan backwards from the end to reduce the likeliness to find
2802 * orphans.
2803 */
2804static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2805{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002806 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002807 struct h2s *h2s;
2808 struct eb32_node *node;
2809
2810 node = eb32_first(&h2c->streams_by_id);
2811 while (node) {
2812 h2s = container_of(node, struct h2s, by_id);
2813 if (h2s->cs)
2814 return h2s->cs;
2815 node = eb32_next(node);
2816 }
2817 return NULL;
2818}
2819
Willy Tarreau62f52692017-10-08 23:01:42 +02002820/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002821 * Destroy the mux and the associated connection, if it is no longer used
2822 */
2823static void h2_destroy(struct connection *conn)
2824{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002825 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002826
2827 if (eb_is_empty(&h2c->streams_by_id))
2828 h2_release(h2c->conn);
2829}
2830
2831/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002832 * Detach the stream from the connection and possibly release the connection.
2833 */
2834static void h2_detach(struct conn_stream *cs)
2835{
Willy Tarreau60935142017-10-16 18:11:19 +02002836 struct h2s *h2s = cs->ctx;
2837 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002838 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002839
2840 cs->ctx = NULL;
2841 if (!h2s)
2842 return;
2843
Olivier Houchardf502aca2018-12-14 19:42:40 +01002844 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002845 h2c = h2s->h2c;
2846 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002847 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002848 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2849 !h2_has_too_many_cs(h2c)) {
2850 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002851 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002852 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002853 }
Willy Tarreau60935142017-10-16 18:11:19 +02002854
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002855 /* this stream may be blocked waiting for some data to leave (possibly
2856 * an ES or RST frame), so orphan it in this case.
2857 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002858 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002859 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002860 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002861 return;
2862
Willy Tarreau45f752e2017-10-30 15:44:59 +01002863 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2864 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2865 /* unblock the connection if it was blocked on this
2866 * stream.
2867 */
2868 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2869 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002870 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002871 }
2872
Willy Tarreau71049cc2018-03-28 13:56:39 +02002873 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002874
Olivier Houchard8a786902018-12-15 16:05:40 +01002875 if (h2c->flags & H2_CF_IS_BACK &&
2876 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002877 if (!(h2c->conn->flags &
2878 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2879 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002880 h2c->conn->owner = sess;
2881 session_add_conn(sess, h2c->conn, h2c->conn->target);
Olivier Houchard8a786902018-12-15 16:05:40 +01002882 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002883 if (eb_is_empty(&h2c->streams_by_id)) {
2884 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2885 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2886 return;
2887 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002888 /* Never ever allow to reuse a connection from a non-reuse backend */
2889 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2890 h2c->conn->flags |= CO_FL_PRIVATE;
2891 if (LIST_ISEMPTY(&h2c->conn->list)) {
2892 struct server *srv = objt_server(h2c->conn->target);
2893
2894 if (srv) {
2895 if (h2c->conn->flags & CO_FL_PRIVATE)
2896 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2897 else
2898 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2899 }
2900
2901 }
2902 }
2903 }
2904
Willy Tarreaue323f342018-03-28 13:51:45 +02002905 /* We don't want to close right now unless we're removing the
2906 * last stream, and either the connection is in error, or it
2907 * reached the ID already specified in a GOAWAY frame received
2908 * or sent (as seen by last_sid >= 0).
2909 */
2910 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2911 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002912 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002913 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002914 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002915 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002916 (conn_xprt_read0_pending(h2c->conn) ||
2917 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2918 /* no more stream will come, kill it now */
2919 h2_release(h2c->conn);
2920 }
2921 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002922 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002923 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2924 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002925 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002926 else
2927 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002928 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002929}
2930
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002931static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002932{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002933 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002934 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002935
Willy Tarreau721c9742017-11-07 11:05:42 +01002936 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002937 return;
2938
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002939 /* if no outgoing data was seen on this stream, it means it was
2940 * closed with a "tcp-request content" rule that is normally
2941 * used to kill the connection ASAP (eg: limit abuse). In this
2942 * case we send a goaway to close the connection.
2943 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002944 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002945 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002946 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002947
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002948 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2949 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002950 h2c_send_goaway_error(h2c, h2s) <= 0)
2951 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002952
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002953 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002954 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002955 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002956
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002957 return;
2958add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002959 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002960 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002961 if (h2s->flags & H2_SF_BLK_MFCTL) {
2962 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2963 h2s->send_wait = sw;
2964 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2965 h2s->send_wait = sw;
2966 LIST_ADDQ(&h2c->send_list, &h2s->list);
2967 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002968 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002969 /* Let the handler know we want shutr */
2970 sw->handle = (void *)((long)sw->handle | 1);
2971
Willy Tarreau62f52692017-10-08 23:01:42 +02002972}
2973
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002974static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002975{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002976 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002977 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002978
Willy Tarreau721c9742017-11-07 11:05:42 +01002979 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002980 return;
2981
Willy Tarreau67434202017-11-06 20:20:51 +01002982 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002983 /* we can cleanly close using an empty data frame only after headers */
2984
2985 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2986 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002987 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002988
2989 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002990 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002991 else
2992 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002993 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002994 /* if no outgoing data was seen on this stream, it means it was
2995 * closed with a "tcp-request content" rule that is normally
2996 * used to kill the connection ASAP (eg: limit abuse). In this
2997 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002998 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002999 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003000 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003001 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003002
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003003 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3004 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003005 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003006 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003007
Willy Tarreau00dd0782018-03-01 16:31:34 +01003008 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003009 }
3010
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003011 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003012 tasklet_wakeup(h2c->wait_event.task);
3013 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003014
3015 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003016 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003017 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003018 if (h2s->flags & H2_SF_BLK_MFCTL) {
3019 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3020 h2s->send_wait = sw;
3021 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3022 h2s->send_wait = sw;
3023 LIST_ADDQ(&h2c->send_list, &h2s->list);
3024 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003025 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003026 /* let the handler know we want to shutw */
3027 sw->handle = (void *)((long)(sw->handle) | 2);
3028
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003029}
3030
3031static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3032{
3033 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003034 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003035
Olivier Houchard2c68a462018-12-15 22:42:20 +01003036 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003037 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003038 h2s->send_wait = NULL;
3039 LIST_DEL(&h2s->list);
3040 LIST_INIT(&h2s->list);
3041 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003042 if (reason & 2)
3043 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003044 if (reason & 1)
3045 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003046
Olivier Houchard2c68a462018-12-15 22:42:20 +01003047 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003048 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003049 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003050 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003051}
3052
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003053static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3054{
3055 struct h2s *h2s = cs->ctx;
3056
3057 if (!mode)
3058 return;
3059
3060 h2_do_shutr(h2s);
3061}
3062
3063static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3064{
3065 struct h2s *h2s = cs->ctx;
3066
3067 h2_do_shutw(h2s);
3068}
3069
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003070/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003071 * HTX request or response depending on the connection's side. Returns the
3072 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3073 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003074 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003075static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003076{
3077 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003078 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003079 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003080 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003081 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003082 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003083 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003084 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003085 int flen = h2c->dfl;
3086 int outlen = 0;
3087 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003088 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003089
3090 if (!h2c->dfl) {
3091 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003092 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003093 return 0;
3094 }
3095
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003096 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003097 return 0; // incomplete input frame
3098
Willy Tarreau13278b42017-10-13 19:23:14 +02003099 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003100 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003101 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003102 copy = alloc_trash_chunk();
3103 if (!copy) {
3104 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3105 goto fail;
3106 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003107 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3108 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3109 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003110 }
3111
3112 /* The padlen is the first byte before data, and the padding appears
3113 * after data. padlen+data+padding are included in flen.
3114 */
3115 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003116 h2c->dpl = *hdrs;
3117 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003118 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3119 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003120 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003121 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003122 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003123 hdrs += 1; // skip Pad Length
3124 }
3125
3126 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3127 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003128 if (read_n32(hdrs) == h2s->id) {
3129 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3130 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003131 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003132 }
3133
Willy Tarreau13278b42017-10-13 19:23:14 +02003134 hdrs += 5; // stream dep = 4, weight = 1
3135 flen -= 5;
3136 }
3137
3138 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3139 * don't support this for now and can't even decompress so we have to
3140 * break the connection.
3141 */
3142 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3143 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003144 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003145 }
3146
Olivier Houchard638b7992018-08-16 15:41:52 +02003147 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003148 if (!csbuf) {
3149 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003150 goto fail;
3151 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003152
Willy Tarreau937f7602018-02-26 15:22:17 +01003153 /* we can't retry a failed decompression operation so we must be very
3154 * careful not to take any risks. In practice the output buffer is
3155 * always empty except maybe for trailers, in which case we simply have
3156 * to wait for the upper layer to finish consuming what is available.
3157 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003158
3159 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3160 htx = htx_from_buf(&h2s->rxbuf);
3161 if (!htx_is_empty(htx))
3162 goto fail;
3163 } else {
3164 if (b_data(csbuf))
3165 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003166
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003167 csbuf->head = 0;
3168 try = b_size(csbuf);
3169 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003170
3171 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3172 sizeof(list)/sizeof(list[0]), tmp);
3173 if (outlen < 0) {
3174 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3175 goto fail;
3176 }
3177
3178 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003179 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003180
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003181 if (htx) {
3182 /* HTX mode */
3183 if (h2c->flags & H2_CF_IS_BACK)
3184 outlen = h2_make_htx_response(list, htx, &msgf);
3185 else
3186 outlen = h2_make_htx_request(list, htx, &msgf);
3187 } else {
3188 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003189 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003190 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003191
3192 if (outlen < 0) {
3193 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3194 goto fail;
3195 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003196
Willy Tarreau174b06a2018-04-25 18:13:58 +02003197 if (msgf & H2_MSGF_BODY) {
3198 /* a payload is present */
3199 if (msgf & H2_MSGF_BODY_CL)
3200 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003201 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003202 h2s->flags |= H2_SF_DATA_CHNK;
3203 }
3204
Willy Tarreau13278b42017-10-13 19:23:14 +02003205 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003206 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003207 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003208 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003209
Willy Tarreau39d68502018-03-02 12:26:37 +01003210 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003211 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003212 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003213 if (htx)
3214 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003215 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003216
Willy Tarreau68dd9852017-07-03 14:44:26 +02003217 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003218 if (htx)
3219 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003220 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003221 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003222 fail:
3223 outlen = 0;
3224 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003225}
3226
Willy Tarreau454f9052017-10-26 19:40:35 +02003227/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3228 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3229 * in use, a new chunk is emitted for each frame. This is supposed to fit
3230 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3231 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3232 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003233 * parser state is automatically updated. Returns > 0 if it could completely
3234 * send the current frame, 0 if it couldn't complete, in which case
3235 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3236 * DATA frame can return 0 as a valid result). Stream errors are reported in
3237 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3238 * have checked the frame header and ensured that the frame was complete or the
3239 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003240 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003241static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003242{
3243 struct h2c *h2c = h2s->h2c;
3244 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003245 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003246 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003247 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003248 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003249
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003250 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003251
3252 /* The padlen is the first byte before data, and the padding appears
3253 * after data. padlen+data+padding are included in flen.
3254 */
Willy Tarreau79127812017-12-03 21:06:59 +01003255 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003256 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003257 return 0;
3258
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003259 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003260 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003261 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3262 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003263 return 0;
3264 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003265
3266 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003267 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003268 h2c->dfl--;
3269 h2c->rcvd_c++; h2c->rcvd_s++;
3270 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003271 }
3272
Olivier Houchard638b7992018-08-16 15:41:52 +02003273 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003274 if (!csbuf) {
3275 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003276 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003277 }
3278
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003279try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003280 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003281 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3282 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003283 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003284 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003285
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003286 if (flen > b_data(&h2c->dbuf)) {
3287 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003288 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003289 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003290 }
3291
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003292 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003293 block1 = htx_free_data_space(htx);
3294 if (!block1) {
3295 h2c->flags |= H2_CF_DEM_SFULL;
3296 goto fail;
3297 }
3298 if (flen > block1)
3299 flen = block1;
3300
3301 /* here, flen is the max we can copy into the output buffer */
3302 block1 = b_contig_data(&h2c->dbuf, 0);
3303 if (flen > block1)
3304 flen = block1;
3305
3306 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3307 h2c->flags |= H2_CF_DEM_SFULL;
3308 goto fail;
3309 }
3310
3311 b_del(&h2c->dbuf, flen);
3312 h2c->dfl -= flen;
3313 h2c->rcvd_c += flen;
3314 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3315 goto try_again;
3316 }
3317 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003318 /* it doesn't fit and the buffer is fragmented,
3319 * so let's defragment it and try again.
3320 */
3321 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003322 }
3323
Willy Tarreaueba10f22018-04-25 20:44:22 +02003324 /* chunked-encoding requires more room */
3325 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003326 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003327 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3328 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3329 (chklen < 1048576) ? 4 : 8;
3330 chklen += 4; // CRLF, CRLF
3331 }
3332
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003333 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003334 if (flen + chklen > b_room(csbuf)) {
3335 if (chklen >= b_room(csbuf)) {
3336 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003337 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003338 }
3339 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003340 }
3341
3342 if (h2s->flags & H2_SF_DATA_CHNK) {
3343 /* emit the chunk size */
3344 unsigned int chksz = flen;
3345 char str[10];
3346 char *beg;
3347
3348 beg = str + sizeof(str);
3349 *--beg = '\n';
3350 *--beg = '\r';
3351 do {
3352 *--beg = hextab[chksz & 0xF];
3353 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003354 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003355 }
3356
Willy Tarreau454f9052017-10-26 19:40:35 +02003357 /* Block1 is the length of the first block before the buffer wraps,
3358 * block2 is the optional second block to reach the end of the frame.
3359 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003360 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003361 if (block1 > flen)
3362 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003363 block2 = flen - block1;
3364
3365 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003366 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003367
3368 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003369 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003370
Willy Tarreaueba10f22018-04-25 20:44:22 +02003371 if (h2s->flags & H2_SF_DATA_CHNK) {
3372 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003373 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003374 }
3375
Willy Tarreau454f9052017-10-26 19:40:35 +02003376 /* now mark the input data as consumed (will be deleted from the buffer
3377 * by the caller when seeing FRAME_A after sending the window update).
3378 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003379 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003380 h2c->dfl -= flen;
3381 h2c->rcvd_c += flen;
3382 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3383
3384 if (h2c->dfl > h2c->dpl) {
3385 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003386 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003387 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003388 }
3389
Willy Tarreau4a28da12018-01-04 14:41:00 +01003390 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003391 /* here we're done with the frame, all the payload (except padding) was
3392 * transferred.
3393 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003394
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003395 if (h2c->dff & H2_F_DATA_END_STREAM) {
3396 if (htx) {
3397 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3398 h2c->flags |= H2_CF_DEM_SFULL;
3399 goto fail;
3400 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003401 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003402 else if (h2s->flags & H2_SF_DATA_CHNK) {
3403 /* emit the trailing 0 CRLF CRLF */
3404 if (b_room(csbuf) < 5) {
3405 h2c->flags |= H2_CF_DEM_SFULL;
3406 goto fail;
3407 }
3408 chklen += 5;
3409 b_putblk(csbuf, "0\r\n\r\n", 5);
3410 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003411 }
3412
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003413 h2c->rcvd_c += h2c->dpl;
3414 h2c->rcvd_s += h2c->dpl;
3415 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003416 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3417
Willy Tarreau39d68502018-03-02 12:26:37 +01003418 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003419 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003420 h2s->cs->flags |= CS_FL_REOS;
3421 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003422 if (htx)
3423 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003424 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003425 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003426 if (htx)
3427 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003428 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003429}
3430
Willy Tarreau5dd17352018-06-14 13:33:30 +02003431/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3432 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3433 * number of bytes sent. The caller must check the stream's status to detect
3434 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003435 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003436static 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 +02003437{
3438 struct http_hdr list[MAX_HTTP_HDR];
3439 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003440 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003441 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003442 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003443 int es_now = 0;
3444 int ret = 0;
3445 int hdr;
3446
3447 if (h2c_mux_busy(h2c, h2s)) {
3448 h2s->flags |= H2_SF_BLK_MBUSY;
3449 return 0;
3450 }
3451
Willy Tarreau44e973f2018-03-01 17:49:30 +01003452 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003453 h2c->flags |= H2_CF_MUX_MALLOC;
3454 h2s->flags |= H2_SF_BLK_MROOM;
3455 return 0;
3456 }
3457
3458 /* First, try to parse the H1 response and index it into <list>.
3459 * NOTE! Since it comes from haproxy, we *know* that a response header
3460 * block does not wrap and we can safely read it this way without
3461 * having to realign the buffer.
3462 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003463 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003464 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003465 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003466 /* incomplete or invalid response, this is abnormal coming from
3467 * haproxy and may only result in a bad errorfile or bad Lua code
3468 * so that won't be fixed, raise an error now.
3469 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003470 * FIXME: we should instead add the ability to only return a
3471 * 502 bad gateway. But in theory this is not supposed to
3472 * happen.
3473 */
3474 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3475 ret = 0;
3476 goto end;
3477 }
3478
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003479 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003480
3481 /* certain statuses have no body or an empty one, regardless of
3482 * what the headers say.
3483 */
3484 if (sl.st.status >= 100 && sl.st.status < 200) {
3485 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3486 h1m->curr_len = h1m->body_len = 0;
3487 }
3488 else if (sl.st.status == 204 || sl.st.status == 304) {
3489 /* no contents, claim c-len is present and set to zero */
3490 h1m->flags &= ~H1_MF_CHNK;
3491 h1m->flags |= H1_MF_CLEN;
3492 h1m->curr_len = h1m->body_len = 0;
3493 }
3494
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003495 chunk_reset(&outbuf);
3496
3497 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003498 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003499 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003500 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003501
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003502 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003503 break;
3504 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003505 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003506 }
3507
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003508 if (outbuf.size < 9)
3509 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003510
3511 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003512 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3513 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3514 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003515
3516 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003517 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003518 /* this is an unparsable response */
3519 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3520 ret = 0;
3521 goto end;
3522 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003523
3524 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003525 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003526 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003527 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003528 }
3529
3530 /* encode all headers, stop at empty name */
3531 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003532 /* these ones do not exist in H2 and must be dropped. */
3533 if (isteq(list[hdr].n, ist("connection")) ||
3534 isteq(list[hdr].n, ist("proxy-connection")) ||
3535 isteq(list[hdr].n, ist("keep-alive")) ||
3536 isteq(list[hdr].n, ist("upgrade")) ||
3537 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003538 continue;
3539
3540 if (isteq(list[hdr].n, ist("")))
3541 break; // end
3542
3543 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3544 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003545 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003546 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003547 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003548 }
3549 }
3550
3551 /* we may need to add END_STREAM */
3552 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3553 es_now = 1;
3554
3555 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003556 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003557
3558 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003559 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003560
3561 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003562 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003563
3564 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003565 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003566 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003567
3568 /* for now we don't implemented CONTINUATION, so we wait for a
3569 * body or directly end in TRL2.
3570 */
3571 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003572 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003573 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003574
Willy Tarreau801250e2018-09-11 11:45:04 +02003575 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003576 h2s->flags |= H2_SF_ES_SENT;
3577 if (h2s->st == H2_SS_OPEN)
3578 h2s->st = H2_SS_HLOC;
3579 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003580 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003581 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003582 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003583 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003584 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003585 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003586 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003587 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003588 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003589
3590 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003591
3592 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003593 //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 +02003594 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003595 full:
3596 h1m_init_res(h1m);
3597 h1m->err_pos = -1; // don't care about errors on the response path
3598 h2c->flags |= H2_CF_MUX_MFULL;
3599 h2s->flags |= H2_SF_BLK_MROOM;
3600 ret = 0;
3601 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003602}
3603
Willy Tarreau5dd17352018-06-14 13:33:30 +02003604/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3605 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3606 * the number of bytes sent. The caller must check the stream's status to
3607 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003608 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003609static 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 +02003610{
3611 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003612 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003613 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003614 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003615 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003616 int es_now = 0;
3617 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003618 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003619 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003620
3621 if (h2c_mux_busy(h2c, h2s)) {
3622 h2s->flags |= H2_SF_BLK_MBUSY;
3623 goto end;
3624 }
3625
Willy Tarreau44e973f2018-03-01 17:49:30 +01003626 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003627 h2c->flags |= H2_CF_MUX_MALLOC;
3628 h2s->flags |= H2_SF_BLK_MROOM;
3629 goto end;
3630 }
3631
3632 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003633 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003634 goto end;
3635
3636 chunk_reset(&outbuf);
3637
3638 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003639 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003640 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003641 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003642
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003643 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003644 break;
3645 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003646 /* If there are pending data in the output buffer, and we have
3647 * less than 1/4 of the mbuf's size and everything fits, we'll
3648 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3649 * is full and wait, to save some slow realign calls.
3650 */
3651 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3652 h2c->flags |= H2_CF_MUX_MFULL;
3653 h2s->flags |= H2_SF_BLK_MROOM;
3654 goto end;
3655 }
3656
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003657 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003658 }
3659
3660 if (outbuf.size < 9) {
3661 h2c->flags |= H2_CF_MUX_MFULL;
3662 h2s->flags |= H2_SF_BLK_MROOM;
3663 goto end;
3664 }
3665
3666 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003667 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3668 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3669 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003670
3671 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3672 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003673 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003674 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003675 break;
3676 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003677 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003678 if ((long long)size > h1m->curr_len)
3679 size = h1m->curr_len;
3680 break;
3681 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003682 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003683 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003684 if (!ret)
3685 goto end;
3686
3687 if (ret < 0) {
3688 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003689 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003690 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3691 goto end;
3692 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003693 max -= ret;
3694 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003695 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003696 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003697 }
3698
Willy Tarreau801250e2018-09-11 11:45:04 +02003699 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003700 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003701 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003702 if (!ret)
3703 goto end;
3704
3705 if (ret < 0) {
3706 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003707 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003708 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3709 goto end;
3710 }
3711
3712 size = chunk;
3713 h1m->curr_len = chunk;
3714 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003715 max -= ret;
3716 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003717 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003718 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003719 if (!size)
3720 goto send_empty;
3721 }
3722
3723 /* in MSG_DATA state, continue below */
3724 size = h1m->curr_len;
3725 break;
3726 }
3727
3728 /* we have in <size> the exact number of bytes we need to copy from
3729 * the H1 buffer. We need to check this against the connection's and
3730 * the stream's send windows, and to ensure that this fits in the max
3731 * frame size and in the buffer's available space minus 9 bytes (for
3732 * the frame header). The connection's flow control is applied last so
3733 * that we can use a separate list of streams which are immediately
3734 * unblocked on window opening. Note: we don't implement padding.
3735 */
3736
Willy Tarreau5dd17352018-06-14 13:33:30 +02003737 if (size > max)
3738 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003739
3740 if (size > h2s->mws)
3741 size = h2s->mws;
3742
3743 if (size <= 0) {
3744 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003745 if (h2s->send_wait) {
3746 LIST_DEL(&h2s->list);
3747 LIST_INIT(&h2s->list);
3748 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003749 goto end;
3750 }
3751
3752 if (h2c->mfs && size > h2c->mfs)
3753 size = h2c->mfs;
3754
3755 if (size + 9 > outbuf.size) {
3756 /* we have an opportunity for enlarging the too small
3757 * available space, let's try.
3758 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003759 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003760 goto realign_again;
3761 size = outbuf.size - 9;
3762 }
3763
3764 if (size <= 0) {
3765 h2c->flags |= H2_CF_MUX_MFULL;
3766 h2s->flags |= H2_SF_BLK_MROOM;
3767 goto end;
3768 }
3769
3770 if (size > h2c->mws)
3771 size = h2c->mws;
3772
3773 if (size <= 0) {
3774 h2s->flags |= H2_SF_BLK_MFCTL;
3775 goto end;
3776 }
3777
3778 /* copy whatever we can */
3779 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003780 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003781 if (ret == 1)
3782 len2 = 0;
3783
3784 if (!ret || len1 + len2 < size) {
3785 /* FIXME: must normally never happen */
3786 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3787 goto end;
3788 }
3789
3790 /* limit len1/len2 to size */
3791 if (len1 + len2 > size) {
3792 int sub = len1 + len2 - size;
3793
3794 if (len2 > sub)
3795 len2 -= sub;
3796 else {
3797 sub -= len2;
3798 len2 = 0;
3799 len1 -= sub;
3800 }
3801 }
3802
3803 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003804 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003805 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003806 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003807
3808 send_empty:
3809 /* we may need to add END_STREAM */
3810 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3811 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003812 *
3813 * FIXME: what we do here is not correct because we send end_stream
3814 * before knowing if we'll have to send a HEADERS frame for the
3815 * trailers. More importantly we're not consuming the trailing CRLF
3816 * after the end of trailers, so it will be left to the caller to
3817 * eat it. The right way to do it would be to measure trailers here
3818 * and to send ES only if there are no trailers.
3819 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003820 */
3821 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003822 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003823 es_now = 1;
3824
3825 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003826 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003827
3828 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003829 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003830
3831 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003832 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003833
3834 /* consume incoming H1 response */
3835 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003836 max -= size;
3837 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003838 total += size;
3839 h1m->curr_len -= size;
3840 h2s->mws -= size;
3841 h2c->mws -= size;
3842
3843 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003844 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003845 goto new_frame;
3846 }
3847 }
3848
3849 if (es_now) {
3850 if (h2s->st == H2_SS_OPEN)
3851 h2s->st = H2_SS_HLOC;
3852 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003853 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003854
Willy Tarreau35a62702018-02-27 15:37:25 +01003855 if (!(h1m->flags & H1_MF_CHNK)) {
3856 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003857 total += max;
3858 ofs += max;
3859 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003860
Willy Tarreau801250e2018-09-11 11:45:04 +02003861 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003862 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003863
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003864 h2s->flags |= H2_SF_ES_SENT;
3865 }
3866
3867 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003868 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 +02003869 return total;
3870}
3871
Willy Tarreau115e83b2018-12-01 19:17:53 +01003872/* Try to send a HEADERS frame matching HTX response present in HTX message
3873 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3874 * must check the stream's status to detect any error which might have happened
3875 * subsequently to a successful send. The htx blocks are automatically removed
3876 * from the message. The htx message is assumed to be valid since produced from
3877 * the internal code, hence it contains a start line, an optional series of
3878 * header blocks and an end of header, otherwise an invalid frame could be
3879 * emitted and the resulting htx message could be left in an inconsistent state.
3880 */
3881static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3882{
3883 struct http_hdr list[MAX_HTTP_HDR];
3884 struct h2c *h2c = h2s->h2c;
3885 struct htx_blk *blk;
3886 struct htx_blk *blk_end;
3887 struct buffer outbuf;
3888 struct htx_sl *sl;
3889 enum htx_blk_type type;
3890 int es_now = 0;
3891 int ret = 0;
3892 int hdr;
3893 int idx;
3894
3895 if (h2c_mux_busy(h2c, h2s)) {
3896 h2s->flags |= H2_SF_BLK_MBUSY;
3897 return 0;
3898 }
3899
3900 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3901 h2c->flags |= H2_CF_MUX_MALLOC;
3902 h2s->flags |= H2_SF_BLK_MROOM;
3903 return 0;
3904 }
3905
3906 /* determine the first block which must not be deleted, blk_end may
3907 * be NULL if all blocks have to be deleted.
3908 */
3909 idx = htx_get_head(htx);
3910 blk_end = NULL;
3911 while (idx != -1) {
3912 type = htx_get_blk_type(htx_get_blk(htx, idx));
3913 idx = htx_get_next(htx, idx);
3914 if (type == HTX_BLK_EOH) {
3915 if (idx != -1)
3916 blk_end = htx_get_blk(htx, idx);
3917 break;
3918 }
3919 }
3920
3921 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003922 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003923 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003924 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003925 if (h2s->status < 100 || h2s->status > 999)
3926 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003927
3928 /* and the rest of the headers, that we dump starting at header 0 */
3929 hdr = 0;
3930
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003931 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003932 while ((idx = htx_get_next(htx, idx)) != -1) {
3933 blk = htx_get_blk(htx, idx);
3934 type = htx_get_blk_type(blk);
3935
3936 if (type == HTX_BLK_UNUSED)
3937 continue;
3938
3939 if (type != HTX_BLK_HDR)
3940 break;
3941
3942 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3943 goto fail;
3944
3945 list[hdr].n = htx_get_blk_name(htx, blk);
3946 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003947 hdr++;
3948 }
3949
3950 /* marker for end of headers */
3951 list[hdr].n = ist("");
3952
3953 if (h2s->status == 204 || h2s->status == 304) {
3954 /* no contents, claim c-len is present and set to zero */
3955 es_now = 1;
3956 }
3957
3958 chunk_reset(&outbuf);
3959
3960 while (1) {
3961 outbuf.area = b_tail(&h2c->mbuf);
3962 outbuf.size = b_contig_space(&h2c->mbuf);
3963 outbuf.data = 0;
3964
3965 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3966 break;
3967 realign_again:
3968 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3969 }
3970
3971 if (outbuf.size < 9)
3972 goto full;
3973
3974 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3975 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3976 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3977 outbuf.data = 9;
3978
3979 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003980 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003981 if (b_space_wraps(&h2c->mbuf))
3982 goto realign_again;
3983 goto full;
3984 }
3985
3986 /* encode all headers, stop at empty name */
3987 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3988 /* these ones do not exist in H2 and must be dropped. */
3989 if (isteq(list[hdr].n, ist("connection")) ||
3990 isteq(list[hdr].n, ist("proxy-connection")) ||
3991 isteq(list[hdr].n, ist("keep-alive")) ||
3992 isteq(list[hdr].n, ist("upgrade")) ||
3993 isteq(list[hdr].n, ist("transfer-encoding")))
3994 continue;
3995
3996 if (isteq(list[hdr].n, ist("")))
3997 break; // end
3998
3999 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4000 /* output full */
4001 if (b_space_wraps(&h2c->mbuf))
4002 goto realign_again;
4003 goto full;
4004 }
4005 }
4006
4007 /* we may need to add END_STREAM.
4008 * FIXME: we should also set it when we know for sure that the
4009 * content-length is zero as well as on 204/304
4010 */
4011 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4012 es_now = 1;
4013
4014 if (h2s->cs->flags & CS_FL_SHW)
4015 es_now = 1;
4016
4017 /* update the frame's size */
4018 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4019
4020 if (es_now)
4021 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4022
4023 /* commit the H2 response */
4024 b_add(&h2c->mbuf, outbuf.data);
4025 h2s->flags |= H2_SF_HEADERS_SENT;
4026
4027 /* for now we don't implemented CONTINUATION, so we wait for a
4028 * body or directly end in TRL2.
4029 */
4030 if (es_now) {
4031 h2s->flags |= H2_SF_ES_SENT;
4032 if (h2s->st == H2_SS_OPEN)
4033 h2s->st = H2_SS_HLOC;
4034 else
4035 h2s_close(h2s);
4036 }
4037
4038 /* OK we could properly deliver the response */
4039
4040 /* remove all header blocks including the EOH and compute the
4041 * corresponding size.
4042 *
4043 * FIXME: We should remove everything when es_now is set.
4044 */
4045 ret = 0;
4046 idx = htx_get_head(htx);
4047 blk = htx_get_blk(htx, idx);
4048 while (blk != blk_end) {
4049 ret += htx_get_blksz(blk);
4050 blk = htx_remove_blk(htx, blk);
4051 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004052
4053 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4054 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004055 end:
4056 return ret;
4057 full:
4058 h2c->flags |= H2_CF_MUX_MFULL;
4059 h2s->flags |= H2_SF_BLK_MROOM;
4060 ret = 0;
4061 goto end;
4062 fail:
4063 /* unparsable HTX messages, too large ones to be produced in the local
4064 * list etc go here (unrecoverable errors).
4065 */
4066 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4067 ret = 0;
4068 goto end;
4069}
4070
Willy Tarreau80739692018-10-05 11:35:57 +02004071/* Try to send a HEADERS frame matching HTX request present in HTX message
4072 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4073 * must check the stream's status to detect any error which might have happened
4074 * subsequently to a successful send. The htx blocks are automatically removed
4075 * from the message. The htx message is assumed to be valid since produced from
4076 * the internal code, hence it contains a start line, an optional series of
4077 * header blocks and an end of header, otherwise an invalid frame could be
4078 * emitted and the resulting htx message could be left in an inconsistent state.
4079 */
4080static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4081{
4082 struct http_hdr list[MAX_HTTP_HDR];
4083 struct h2c *h2c = h2s->h2c;
4084 struct htx_blk *blk;
4085 struct htx_blk *blk_end;
4086 struct buffer outbuf;
4087 struct htx_sl *sl;
4088 struct ist meth, path;
4089 enum htx_blk_type type;
4090 int es_now = 0;
4091 int ret = 0;
4092 int hdr;
4093 int idx;
4094
4095 if (h2c_mux_busy(h2c, h2s)) {
4096 h2s->flags |= H2_SF_BLK_MBUSY;
4097 return 0;
4098 }
4099
4100 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4101 h2c->flags |= H2_CF_MUX_MALLOC;
4102 h2s->flags |= H2_SF_BLK_MROOM;
4103 return 0;
4104 }
4105
4106 /* determine the first block which must not be deleted, blk_end may
4107 * be NULL if all blocks have to be deleted.
4108 */
4109 idx = htx_get_head(htx);
4110 blk_end = NULL;
4111 while (idx != -1) {
4112 type = htx_get_blk_type(htx_get_blk(htx, idx));
4113 idx = htx_get_next(htx, idx);
4114 if (type == HTX_BLK_EOH) {
4115 if (idx != -1)
4116 blk_end = htx_get_blk(htx, idx);
4117 break;
4118 }
4119 }
4120
4121 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004122 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004123 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004124 meth = htx_sl_req_meth(sl);
4125 path = htx_sl_req_uri(sl);
4126
4127 /* and the rest of the headers, that we dump starting at header 0 */
4128 hdr = 0;
4129
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004130 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004131 while ((idx = htx_get_next(htx, idx)) != -1) {
4132 blk = htx_get_blk(htx, idx);
4133 type = htx_get_blk_type(blk);
4134
4135 if (type == HTX_BLK_UNUSED)
4136 continue;
4137
4138 if (type != HTX_BLK_HDR)
4139 break;
4140
4141 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4142 goto fail;
4143
4144 list[hdr].n = htx_get_blk_name(htx, blk);
4145 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004146 hdr++;
4147 }
4148
4149 /* marker for end of headers */
4150 list[hdr].n = ist("");
4151
4152 chunk_reset(&outbuf);
4153
4154 while (1) {
4155 outbuf.area = b_tail(&h2c->mbuf);
4156 outbuf.size = b_contig_space(&h2c->mbuf);
4157 outbuf.data = 0;
4158
4159 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4160 break;
4161 realign_again:
4162 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4163 }
4164
4165 if (outbuf.size < 9)
4166 goto full;
4167
4168 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4169 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4170 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4171 outbuf.data = 9;
4172
4173 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004174 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004175 if (b_space_wraps(&h2c->mbuf))
4176 goto realign_again;
4177 goto full;
4178 }
4179
4180 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004181 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4182 /* output full */
4183 if (b_space_wraps(&h2c->mbuf))
4184 goto realign_again;
4185 goto full;
4186 }
Willy Tarreau80739692018-10-05 11:35:57 +02004187
4188 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004189 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004190 /* output full */
4191 if (b_space_wraps(&h2c->mbuf))
4192 goto realign_again;
4193 goto full;
4194 }
4195
4196 /* encode all headers, stop at empty name */
4197 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4198 /* these ones do not exist in H2 and must be dropped. */
4199 if (isteq(list[hdr].n, ist("connection")) ||
4200 isteq(list[hdr].n, ist("proxy-connection")) ||
4201 isteq(list[hdr].n, ist("keep-alive")) ||
4202 isteq(list[hdr].n, ist("upgrade")) ||
4203 isteq(list[hdr].n, ist("transfer-encoding")))
4204 continue;
4205
4206 if (isteq(list[hdr].n, ist("")))
4207 break; // end
4208
4209 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4210 /* output full */
4211 if (b_space_wraps(&h2c->mbuf))
4212 goto realign_again;
4213 goto full;
4214 }
4215 }
4216
4217 /* we may need to add END_STREAM if we have no body :
4218 * - request already closed, or :
4219 * - no transfer-encoding, and :
4220 * - no content-length or content-length:0
4221 * Fixme: this doesn't take into account CONNECT requests.
4222 */
4223 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4224 es_now = 1;
4225
4226 if (sl->flags & HTX_SL_F_BODYLESS)
4227 es_now = 1;
4228
4229 if (h2s->cs->flags & CS_FL_SHW)
4230 es_now = 1;
4231
4232 /* update the frame's size */
4233 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4234
4235 if (es_now)
4236 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4237
4238 /* commit the H2 response */
4239 b_add(&h2c->mbuf, outbuf.data);
4240 h2s->flags |= H2_SF_HEADERS_SENT;
4241 h2s->st = H2_SS_OPEN;
4242
4243 /* for now we don't implemented CONTINUATION, so we wait for a
4244 * body or directly end in TRL2.
4245 */
4246 if (es_now) {
4247 // trim any possibly pending data (eg: inconsistent content-length)
4248 h2s->flags |= H2_SF_ES_SENT;
4249 h2s->st = H2_SS_HLOC;
4250 }
4251
4252 /* remove all header blocks including the EOH and compute the
4253 * corresponding size.
4254 *
4255 * FIXME: We should remove everything when es_now is set.
4256 */
4257 ret = 0;
4258 idx = htx_get_head(htx);
4259 blk = htx_get_blk(htx, idx);
4260 while (blk != blk_end) {
4261 ret += htx_get_blksz(blk);
4262 blk = htx_remove_blk(htx, blk);
4263 }
4264
4265 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4266 htx_remove_blk(htx, blk_end);
4267
4268 end:
4269 return ret;
4270 full:
4271 h2c->flags |= H2_CF_MUX_MFULL;
4272 h2s->flags |= H2_SF_BLK_MROOM;
4273 ret = 0;
4274 goto end;
4275 fail:
4276 /* unparsable HTX messages, too large ones to be produced in the local
4277 * list etc go here (unrecoverable errors).
4278 */
4279 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4280 ret = 0;
4281 goto end;
4282}
4283
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004284/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004285 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4286 * caller must check the stream's status to detect any error which might have
4287 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004288 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4289 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004290static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004291{
4292 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004293 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004294 struct buffer outbuf;
4295 size_t total = 0;
4296 int es_now = 0;
4297 int bsize; /* htx block size */
4298 int fsize; /* h2 frame size */
4299 struct htx_blk *blk;
4300 enum htx_blk_type type;
4301 int idx;
4302
4303 if (h2c_mux_busy(h2c, h2s)) {
4304 h2s->flags |= H2_SF_BLK_MBUSY;
4305 goto end;
4306 }
4307
4308 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4309 h2c->flags |= H2_CF_MUX_MALLOC;
4310 h2s->flags |= H2_SF_BLK_MROOM;
4311 goto end;
4312 }
4313
Willy Tarreau98de12a2018-12-12 07:03:00 +01004314 htx = htx_from_buf(buf);
4315
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004316 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4317 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4318 * the caller to handle.
4319 */
4320
4321 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004322 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004323 goto end;
4324
4325 idx = htx_get_head(htx);
4326 blk = htx_get_blk(htx, idx);
4327 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4328 bsize = htx_get_blksz(blk);
4329 fsize = bsize;
4330
4331 if (type == HTX_BLK_EOD) {
4332 /* if we have an EOD, we're dealing with chunked data. We may
4333 * have a set of trailers after us that the caller will want to
4334 * deal with. Let's simply remove the EOD and return.
4335 */
4336 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004337 total++; // EOD counts as one byte
4338 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004339 goto end;
4340 }
4341
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004342 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4343 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004344
4345 /* Perform some optimizations to reduce the number of buffer copies.
4346 * First, if the mux's buffer is empty and the htx area contains
4347 * exactly one data block of the same size as the requested count, and
4348 * this count fits within the frame size, the stream's window size, and
4349 * the connection's window size, then it's possible to simply swap the
4350 * caller's buffer with the mux's output buffer and adjust offsets and
4351 * length to match the entire DATA HTX block in the middle. In this
4352 * case we perform a true zero-copy operation from end-to-end. This is
4353 * the situation that happens all the time with large files. Second, if
4354 * this is not possible, but the mux's output buffer is empty, we still
4355 * have an opportunity to avoid the copy to the intermediary buffer, by
4356 * making the intermediary buffer's area point to the output buffer's
4357 * area. In this case we want to skip the HTX header to make sure that
4358 * copies remain aligned and that this operation remains possible all
4359 * the time. This goes for headers, data blocks and any data extracted
4360 * from the HTX blocks.
4361 */
4362 if (unlikely(fsize == count &&
4363 htx->used == 1 && type == HTX_BLK_DATA &&
4364 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4365 void *old_area = h2c->mbuf.area;
4366
4367 if (b_data(&h2c->mbuf)) {
4368 /* too bad there are data left there. If we have less
4369 * than 1/4 of the mbuf's size and everything fits,
4370 * we'll perform a copy anyway. Otherwise we'll pretend
4371 * the mbuf is full and wait.
4372 */
4373 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4374 goto copy;
4375 h2c->flags |= H2_CF_MUX_MFULL;
4376 h2s->flags |= H2_SF_BLK_MROOM;
4377 goto end;
4378 }
4379
4380 /* map an H2 frame to the HTX block so that we can put the
4381 * frame header there.
4382 */
4383 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004384 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004385 h2c->mbuf.data = fsize + 9;
4386 outbuf.area = b_head(&h2c->mbuf);
4387
4388 /* prepend an H2 DATA frame header just before the DATA block */
4389 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4390 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4391 h2_set_frame_size(outbuf.area, fsize);
4392
4393 /* update windows */
4394 h2s->mws -= fsize;
4395 h2c->mws -= fsize;
4396
4397 /* and exchange with our old area */
4398 buf->area = old_area;
4399 buf->data = buf->head = 0;
4400 total += fsize;
4401 goto end;
4402 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004403
Willy Tarreau98de12a2018-12-12 07:03:00 +01004404 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004405 /* for DATA and EOM we'll have to emit a frame, even if empty */
4406
4407 while (1) {
4408 outbuf.area = b_tail(&h2c->mbuf);
4409 outbuf.size = b_contig_space(&h2c->mbuf);
4410 outbuf.data = 0;
4411
4412 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4413 break;
4414 realign_again:
4415 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4416 }
4417
4418 if (outbuf.size < 9) {
4419 h2c->flags |= H2_CF_MUX_MFULL;
4420 h2s->flags |= H2_SF_BLK_MROOM;
4421 goto end;
4422 }
4423
4424 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4425 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4426 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4427 outbuf.data = 9;
4428
4429 /* we have in <fsize> the exact number of bytes we need to copy from
4430 * the HTX buffer. We need to check this against the connection's and
4431 * the stream's send windows, and to ensure that this fits in the max
4432 * frame size and in the buffer's available space minus 9 bytes (for
4433 * the frame header). The connection's flow control is applied last so
4434 * that we can use a separate list of streams which are immediately
4435 * unblocked on window opening. Note: we don't implement padding.
4436 */
4437
4438 /* EOM is presented with bsize==1 but would lead to the emission of an
4439 * empty frame, thus we force it to zero here.
4440 */
4441 if (type == HTX_BLK_EOM)
4442 bsize = fsize = 0;
4443
4444 if (!fsize)
4445 goto send_empty;
4446
4447 if (h2s->mws <= 0) {
4448 h2s->flags |= H2_SF_BLK_SFCTL;
4449 if (h2s->send_wait) {
4450 LIST_DEL(&h2s->list);
4451 LIST_INIT(&h2s->list);
4452 }
4453 goto end;
4454 }
4455
Willy Tarreauee573762018-12-04 15:25:57 +01004456 if (fsize > count)
4457 fsize = count;
4458
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004459 if (fsize > h2s->mws)
4460 fsize = h2s->mws; // >0
4461
4462 if (h2c->mfs && fsize > h2c->mfs)
4463 fsize = h2c->mfs; // >0
4464
4465 if (fsize + 9 > outbuf.size) {
4466 /* we have an opportunity for enlarging the too small
4467 * available space, let's try.
4468 * FIXME: is this really interesting to do? Maybe we'll
4469 * spend lots of time realigning instead of using two
4470 * frames.
4471 */
4472 if (b_space_wraps(&h2c->mbuf))
4473 goto realign_again;
4474 fsize = outbuf.size - 9;
4475
4476 if (fsize <= 0) {
4477 /* no need to send an empty frame here */
4478 h2c->flags |= H2_CF_MUX_MFULL;
4479 h2s->flags |= H2_SF_BLK_MROOM;
4480 goto end;
4481 }
4482 }
4483
4484 if (h2c->mws <= 0) {
4485 h2s->flags |= H2_SF_BLK_MFCTL;
4486 goto end;
4487 }
4488
4489 if (fsize > h2c->mws)
4490 fsize = h2c->mws;
4491
4492 /* now let's copy this this into the output buffer */
4493 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004494 h2s->mws -= fsize;
4495 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004496 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004497
4498 send_empty:
4499 /* update the frame's size */
4500 h2_set_frame_size(outbuf.area, fsize);
4501
4502 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4503 * meeting EOM. We should optimize this later.
4504 */
4505 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004506 total++; // EOM counts as one byte
4507 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004508 es_now = 1;
4509 }
4510
4511 if (es_now)
4512 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4513
4514 /* commit the H2 response */
4515 b_add(&h2c->mbuf, fsize + 9);
4516
4517 /* consume incoming HTX block, including EOM */
4518 total += fsize;
4519 if (fsize == bsize) {
4520 htx_remove_blk(htx, blk);
4521 if (fsize)
4522 goto new_frame;
4523 } else {
4524 /* we've truncated this block */
4525 htx_cut_data_blk(htx, blk, fsize);
4526 }
4527
4528 if (es_now) {
4529 if (h2s->st == H2_SS_OPEN)
4530 h2s->st = H2_SS_HLOC;
4531 else
4532 h2s_close(h2s);
4533
4534 h2s->flags |= H2_SF_ES_SENT;
4535 }
4536
4537 end:
4538 return total;
4539}
4540
Olivier Houchard6ff20392018-07-17 18:46:31 +02004541/* Called from the upper layer, to subscribe to events, such as being able to send */
4542static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4543{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004544 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004545 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004546 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004547
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004548 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004549 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004550 if (!(sw->events & SUB_RETRY_RECV)) {
4551 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004552 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004553 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004554 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004555 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004556 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004557 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004558 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004559 if (!(sw->events & SUB_RETRY_SEND)) {
4560 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004561 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004562 h2s->send_wait = sw;
4563 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4564 if (h2s->flags & H2_SF_BLK_MFCTL)
4565 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4566 else
4567 LIST_ADDQ(&h2c->send_list, &h2s->list);
4568 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004569 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004570 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004571 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004572 if (event_type != 0)
4573 return -1;
4574 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004575
4576
4577}
4578
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004579static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4580{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004581 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004582 struct h2s *h2s = cs->ctx;
4583
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004584 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004585 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004586 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004587 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004588 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004589 }
4590 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004591 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004592 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004593 if (h2s->send_wait == sw) {
4594 LIST_DEL(&h2s->list);
4595 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004596 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004597 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004598 }
4599 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004600 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4601 sw = param;
4602 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004603 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004604 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01004605 LIST_DEL(&h2s->list);
4606 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004607 }
4608 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004609 return 0;
4610}
4611
4612
Olivier Houchard511efea2018-08-16 15:30:32 +02004613/* Called from the upper layer, to receive data */
4614static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4615{
Olivier Houchard638b7992018-08-16 15:41:52 +02004616 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004617 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004618 struct htx *h2s_htx = NULL;
4619 struct htx *buf_htx = NULL;
4620 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004621 size_t ret = 0;
4622
4623 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004624 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4625 /* in HTX mode we ignore the count argument */
4626 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004627 if (htx_is_empty(h2s_htx)) {
4628 if (cs->flags & CS_FL_REOS)
4629 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004630 if (cs->flags & CS_FL_ERR_PENDING)
4631 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004632 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004633 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004634
4635 buf_htx = htx_from_buf(buf);
4636 count = htx_free_space(buf_htx);
4637
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004638 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004639
4640 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004641 htx_to_buf(buf_htx, buf);
4642 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004643 ret = htx_ret.ret;
4644 }
4645 else {
4646 ret = b_xfer(buf, &h2s->rxbuf, count);
4647 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004648
Olivier Houchard638b7992018-08-16 15:41:52 +02004649 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004650 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004651 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004652 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004653 if (cs->flags & CS_FL_REOS)
4654 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004655 if (cs->flags & CS_FL_ERR_PENDING)
4656 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004657 if (b_size(&h2s->rxbuf)) {
4658 b_free(&h2s->rxbuf);
4659 offer_buffers(NULL, tasks_run_queue);
4660 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004661 }
4662
Willy Tarreau082f5592018-11-25 08:03:32 +01004663 if (ret && h2c->dsi == h2s->id) {
4664 /* demux is blocking on this stream's buffer */
4665 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004666 if (b_data(&h2c->dbuf) || !(h2c->wait_event.events & SUB_RETRY_RECV)) {
Willy Tarreau082f5592018-11-25 08:03:32 +01004667 if (h2_recv_allowed(h2c))
4668 tasklet_wakeup(h2c->wait_event.task);
4669 }
4670 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004671end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004672 return ret;
4673}
4674
Olivier Houchardd846c262018-10-19 17:24:29 +02004675static void h2_stop_senders(struct h2c *h2c)
4676{
4677 struct h2s *h2s, *h2s_back;
4678
4679 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4680 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4681 if (h2c->msi == h2s_id(h2s))
4682 continue;
4683 LIST_DEL(&h2s->list);
4684 LIST_INIT(&h2s->list);
4685 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004686 h2s->send_wait->events |= SUB_RETRY_SEND;
4687 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004688 LIST_ADD(&h2c->send_list, &h2s->list);
4689 }
4690}
4691
Willy Tarreau62f52692017-10-08 23:01:42 +02004692/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004693static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004694{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004695 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004696 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004697 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004698 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004699 struct htx *htx;
4700 struct htx_blk *blk;
4701 enum htx_blk_type btype;
4702 uint32_t bsize;
4703 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004704
Olivier Houchardd846c262018-10-19 17:24:29 +02004705 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004706 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004707 h2s->send_wait = NULL;
4708 LIST_DEL(&h2s->list);
4709 LIST_INIT(&h2s->list);
4710 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004711 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4712 return 0;
4713
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004714 /* htx will be enough to decide if we're using HTX or legacy */
4715 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4716
Willy Tarreau0bad0432018-06-14 16:54:01 +02004717 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004718 h2s->flags |= H2_SF_OUTGOING_DATA;
4719
Willy Tarreau751f2d02018-10-05 09:35:00 +02004720 if (h2s->id == 0) {
4721 int32_t id = h2c_get_next_sid(h2s->h2c);
4722
4723 if (id < 0) {
4724 cs->ctx = NULL;
4725 cs->flags |= CS_FL_ERROR;
4726 h2s_destroy(h2s);
4727 return 0;
4728 }
4729
4730 eb32_delete(&h2s->by_id);
4731 h2s->by_id.key = h2s->id = id;
4732 h2s->h2c->max_id = id;
4733 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4734 }
4735
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004736 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004737 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4738 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004739 idx = htx_get_head(htx);
4740 blk = htx_get_blk(htx, idx);
4741 btype = htx_get_blk_type(blk);
4742 bsize = htx_get_blksz(blk);
4743
4744 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004745 case HTX_BLK_REQ_SL:
4746 /* start-line before headers */
4747 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4748 if (ret > 0) {
4749 total += ret;
4750 count -= ret;
4751 if (ret < bsize)
4752 goto done;
4753 }
4754 break;
4755
Willy Tarreau115e83b2018-12-01 19:17:53 +01004756 case HTX_BLK_RES_SL:
4757 /* start-line before headers */
4758 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4759 if (ret > 0) {
4760 total += ret;
4761 count -= ret;
4762 if (ret < bsize)
4763 goto done;
4764 }
4765 break;
4766
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004767 case HTX_BLK_DATA:
4768 case HTX_BLK_EOD:
4769 case HTX_BLK_EOM:
4770 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004771 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004772 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004773 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004774 total += ret;
4775 count -= ret;
4776 if (ret < bsize)
4777 goto done;
4778 }
4779 break;
4780
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004781 default:
4782 htx_remove_blk(htx, blk);
4783 total += bsize;
4784 count -= bsize;
4785 break;
4786 }
4787 }
4788 goto done;
4789 }
4790
4791 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004792 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004793 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004794 if (h2s->h2c->flags & H2_CF_IS_BACK)
4795 ret = -1;
4796 else
4797 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004798 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004799 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004800 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004801 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004802 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004803 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004804 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004805
Willy Tarreau5dd17352018-06-14 13:33:30 +02004806 if (unlikely((int)ret <= 0)) {
4807 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004808 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4809 break;
4810 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004811 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004812 total += count;
4813 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004814 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004815 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004816 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004817 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004818 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004819 break;
4820 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004821
4822 total += ret;
4823 count -= ret;
4824
4825 if (h2s->st >= H2_SS_ERROR)
4826 break;
4827
4828 if (h2s->flags & H2_SF_BLK_ANY)
4829 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004830 }
4831
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004832 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004833 if (h2s->st >= H2_SS_ERROR) {
4834 /* trim any possibly pending data after we close (extra CR-LF,
4835 * unprocessed trailers, abnormal extra data, ...)
4836 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004837 total += count;
4838 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004839 }
4840
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004841 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004842 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004843 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004844 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004845 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004846 }
4847
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004848 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004849 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004850 } else {
4851 b_del(buf, total);
4852 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004853
4854 /* The mux is full, cancel the pending tasks */
4855 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4856 (h2s->flags & H2_SF_BLK_MBUSY))
4857 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004858
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004859 /* If we're running HTX, and we read the whole buffer, then pretend
4860 * we read exactly what the caller specified, as with HTX the caller
4861 * will always give the buffer size, instead of the amount of data
4862 * available.
4863 */
4864 if (htx && !b_data(buf))
4865 total = orig_count;
4866
Olivier Houchard7505f942018-08-21 18:10:44 +02004867 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004868 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004869 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004870
Olivier Houchard7505f942018-08-21 18:10:44 +02004871 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01004872 /* If we're waiting for flow control, and we got a shutr on the
4873 * connection, we will never be unlocked, so add an error on
4874 * the conn_stream.
4875 */
4876 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
4877 !b_data(&h2s->h2c->dbuf) &&
4878 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
4879 if (cs->flags & CS_FL_EOS)
4880 cs->flags |= CS_FL_ERROR;
4881 else
4882 cs->flags |= CS_FL_ERR_PENDING;
4883 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004884 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004885}
4886
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004887/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004888static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004889{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01004890 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01004891 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004892 struct eb32_node *node;
4893 int fctl_cnt = 0;
4894 int send_cnt = 0;
4895 int tree_cnt = 0;
4896 int orph_cnt = 0;
4897
4898 if (!h2c)
4899 return;
4900
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004901 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004902 fctl_cnt++;
4903
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004904 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004905 send_cnt++;
4906
Willy Tarreau3af37712018-12-18 14:34:41 +01004907 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004908 node = eb32_first(&h2c->streams_by_id);
4909 while (node) {
4910 h2s = container_of(node, struct h2s, by_id);
4911 tree_cnt++;
4912 if (!h2s->cs)
4913 orph_cnt++;
4914 node = eb32_next(node);
4915 }
4916
Willy Tarreau987c0632018-12-18 10:32:05 +01004917 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
4918 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
4919 " .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 +02004920 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4921 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004922 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01004923 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
4924 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4925 h2c->msi,
4926 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
4927 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
4928
4929 if (h2s) {
4930 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4931 h2s, h2s->id, h2s->flags,
4932 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
4933 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
4934 h2s->cs);
4935 if (h2s->cs)
4936 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4937 h2s->cs->flags, h2s->cs->data);
4938 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004939}
Willy Tarreau62f52692017-10-08 23:01:42 +02004940
4941/*******************************************************/
4942/* functions below are dedicated to the config parsers */
4943/*******************************************************/
4944
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004945/* config parser for global "tune.h2.header-table-size" */
4946static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4947 struct proxy *defpx, const char *file, int line,
4948 char **err)
4949{
4950 if (too_many_args(1, args, err, NULL))
4951 return -1;
4952
4953 h2_settings_header_table_size = atoi(args[1]);
4954 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4955 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4956 return -1;
4957 }
4958 return 0;
4959}
Willy Tarreau62f52692017-10-08 23:01:42 +02004960
Willy Tarreaue6baec02017-07-27 11:45:11 +02004961/* config parser for global "tune.h2.initial-window-size" */
4962static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4963 struct proxy *defpx, const char *file, int line,
4964 char **err)
4965{
4966 if (too_many_args(1, args, err, NULL))
4967 return -1;
4968
4969 h2_settings_initial_window_size = atoi(args[1]);
4970 if (h2_settings_initial_window_size < 0) {
4971 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4972 return -1;
4973 }
4974 return 0;
4975}
4976
Willy Tarreau5242ef82017-07-27 11:47:28 +02004977/* config parser for global "tune.h2.max-concurrent-streams" */
4978static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4979 struct proxy *defpx, const char *file, int line,
4980 char **err)
4981{
4982 if (too_many_args(1, args, err, NULL))
4983 return -1;
4984
4985 h2_settings_max_concurrent_streams = atoi(args[1]);
4986 if (h2_settings_max_concurrent_streams < 0) {
4987 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4988 return -1;
4989 }
4990 return 0;
4991}
4992
Willy Tarreau62f52692017-10-08 23:01:42 +02004993
4994/****************************************/
4995/* MUX initialization and instanciation */
4996/***************************************/
4997
4998/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004999static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005000 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005001 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005002 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005003 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005004 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005005 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005006 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005007 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005008 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005009 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005010 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005011 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005012 .shutr = h2_shutr,
5013 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005014 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005015 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005016 .name = "H2",
5017};
5018
Christopher Faulet32f61c02018-04-10 14:33:41 +02005019/* PROTO selection : this mux registers PROTO token "h2" */
5020static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005021 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005022
Willy Tarreau0108d902018-11-25 19:14:37 +01005023INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5024
Willy Tarreauf8957272018-10-03 10:25:20 +02005025static struct mux_proto_list mux_proto_h2_htx =
5026 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5027
5028INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5029
Willy Tarreau62f52692017-10-08 23:01:42 +02005030/* config keyword parsers */
5031static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005032 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005033 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005034 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005035 { 0, NULL, NULL }
5036}};
5037
Willy Tarreau0108d902018-11-25 19:14:37 +01005038INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);