blob: a0357be4b78ca4d3e2d4639d03e82bfa0873b625 [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;
178 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200179 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200180 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200186 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200187 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200188 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
189 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
190 struct wait_event *send_wait; /* The streeam is waiting for flow control */
191 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200192};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200193
Willy Tarreauc6405142017-09-21 20:23:50 +0200194/* descriptor for an h2 frame header */
195struct h2_fh {
196 uint32_t len; /* length, host order, 24 bits */
197 uint32_t sid; /* stream id, host order, 31 bits */
198 uint8_t ft; /* frame type */
199 uint8_t ff; /* frame flags */
200};
201
Willy Tarreau8ceae722018-11-26 11:58:30 +0100202/* the h2c connection pool */
203DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
204
205/* the h2s stream pool */
206DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
207
Willy Tarreaudc572362018-12-12 08:08:05 +0100208/* The default connection window size is 65535, it may only be enlarged using
209 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
210 * we'll pretend we already received the difference between the two to send
211 * an equivalent window update to enlarge it to 2G-1.
212 */
213#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
214
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200215/* a few settings from the global section */
216static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200217static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200218static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200219
Willy Tarreau2a856182017-05-16 15:20:39 +0200220/* a dmumy closed stream */
221static const struct h2s *h2_closed_stream = &(const struct h2s){
222 .cs = NULL,
223 .h2c = NULL,
224 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100225 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100226 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200227 .id = 0,
228};
229
230/* and a dummy idle stream for use with any unannounced stream */
231static const struct h2s *h2_idle_stream = &(const struct h2s){
232 .cs = NULL,
233 .h2c = NULL,
234 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100235 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200236 .id = 0,
237};
238
Olivier Houchard9f6af332018-05-25 14:04:04 +0200239static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200240static int h2_send(struct h2c *h2c);
241static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200242static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200243static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100244static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200245static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100246static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200247static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreau751f2d02018-10-05 09:35:00 +0200248static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200249
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200250/*****************************************************/
251/* functions below are for dynamic buffer management */
252/*****************************************************/
253
Willy Tarreau315d8072017-12-10 22:17:57 +0100254/* indicates whether or not the we may call the h2_recv() function to attempt
255 * to receive data into the buffer and/or demux pending data. The condition is
256 * a bit complex due to some API limits for now. The rules are the following :
257 * - if an error or a shutdown was detected on the connection and the buffer
258 * is empty, we must not attempt to receive
259 * - if the demux buf failed to be allocated, we must not try to receive and
260 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100261 * - if no flag indicates a blocking condition, we may attempt to receive,
262 * regardless of whether the demux buffer is full or not, so that only
263 * de demux part decides whether or not to block. This is needed because
264 * the connection API indeed prevents us from re-enabling receipt that is
265 * already enabled in a polled state, so we must always immediately stop
266 * as soon as the demux can't proceed so as never to hit an end of read
267 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100268 * - otherwise must may not attempt
269 */
270static inline int h2_recv_allowed(const struct h2c *h2c)
271{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200272 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100273 (h2c->st0 >= H2_CS_ERROR ||
274 h2c->conn->flags & CO_FL_ERROR ||
275 conn_xprt_read0_pending(h2c->conn)))
276 return 0;
277
278 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100279 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100280 return 1;
281
282 return 0;
283}
284
Willy Tarreauf2101912018-07-19 10:11:38 +0200285/* returns true if the connection has too many conn_streams attached */
286static inline int h2_has_too_many_cs(const struct h2c *h2c)
287{
288 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
289}
290
Willy Tarreau44e973f2018-03-01 17:49:30 +0100291/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
292 * flags are used to figure what buffer was requested. It returns 1 if the
293 * allocation succeeds, in which case the connection is woken up, or 0 if it's
294 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200295 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100296static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200297{
298 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100299 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200300
Willy Tarreau44e973f2018-03-01 17:49:30 +0100301 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200302 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200303 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200304 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200305 return 1;
306 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200307
Willy Tarreau44e973f2018-03-01 17:49:30 +0100308 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
309 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200310
311 if (h2c->flags & H2_CF_DEM_MROOM) {
312 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200313 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200314 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200315 }
Willy Tarreau14398122017-09-22 14:26:04 +0200316 return 1;
317 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100318
319 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
320 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200321 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100322 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200323 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200324 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100325 return 1;
326 }
327
Willy Tarreau14398122017-09-22 14:26:04 +0200328 return 0;
329}
330
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200331static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200332{
333 struct buffer *buf = NULL;
334
Willy Tarreau44e973f2018-03-01 17:49:30 +0100335 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
336 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
337 h2c->buf_wait.target = h2c;
338 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100339 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100340 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200342 __conn_xprt_stop_recv(h2c->conn);
343 }
344 return buf;
345}
346
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200347static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200348{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200349 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100350 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200351 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200352 }
353}
354
Olivier Houchardd540b362018-11-05 18:37:53 +0100355static int h2_avail_streams(struct connection *conn)
356{
357 struct h2c *h2c = conn->mux_ctx;
358
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100359 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100360 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
361}
362
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100363static int h2_max_streams(struct connection *conn)
364{
365 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
366 return h2_settings_max_concurrent_streams;
367}
368
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200369
Willy Tarreau62f52692017-10-08 23:01:42 +0200370/*****************************************************************/
371/* functions below are dedicated to the mux setup and management */
372/*****************************************************************/
373
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200374/* Initialize the mux once it's attached. For outgoing connections, the context
375 * is already initialized before installing the mux, so we detect incoming
376 * connections from the fact that the context is still NULL. Returns < 0 on
377 * error.
378 */
379static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200380{
381 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100382 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383
Willy Tarreaubafbe012017-11-24 17:34:44 +0100384 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200385 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200386 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200387
Willy Tarreau01b44822018-10-03 14:26:37 +0200388 if (conn->mux_ctx) {
389 h2c->flags = H2_CF_IS_BACK;
390 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
391 if (tick_isset(prx->timeout.serverfin))
392 h2c->shut_timeout = prx->timeout.serverfin;
393 } else {
394 h2c->flags = H2_CF_NONE;
395 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
396 if (tick_isset(prx->timeout.clientfin))
397 h2c->shut_timeout = prx->timeout.clientfin;
398 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100399
Willy Tarreau0b37d652018-10-03 10:33:02 +0200400 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100401 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100402 if (tick_isset(h2c->timeout)) {
403 t = task_new(tid_bit);
404 if (!t)
405 goto fail;
406
407 h2c->task = t;
408 t->process = h2_timeout_task;
409 t->context = h2c;
410 t->expire = tick_add(now_ms, h2c->timeout);
411 }
Willy Tarreauea392822017-10-31 10:02:25 +0100412
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200413 h2c->wait_event.task = tasklet_new();
414 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200415 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200416 h2c->wait_event.task->process = h2_io_cb;
417 h2c->wait_event.task->context = h2c;
418 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200419
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
421 if (!h2c->ddht)
422 goto fail;
423
424 /* Initialise the context. */
425 h2c->st0 = H2_CS_PREFACE;
426 h2c->conn = conn;
427 h2c->max_id = -1;
428 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreaudc572362018-12-12 08:08:05 +0100429 h2c->rcvd_c = H2_INITIAL_WINDOW_INCREMENT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200430 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100431 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200432 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200434 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200435 h2c->dsi = -1;
436 h2c->msi = -1;
437 h2c->last_sid = -1;
438
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200439 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200440 h2c->miw = 65535; /* mux initial window size */
441 h2c->mws = 65535; /* mux window size */
442 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200443 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200444 LIST_INIT(&h2c->send_list);
445 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200446 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100447 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448
Willy Tarreau3f133572017-10-31 19:21:06 +0100449 if (t)
450 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100451
Willy Tarreau01b44822018-10-03 14:26:37 +0200452 if (h2c->flags & H2_CF_IS_BACK) {
453 /* FIXME: this is temporary, for outgoing connections we need
454 * to immediately allocate a stream until the code is modified
455 * so that the caller calls ->attach(). For now the outgoing cs
456 * is stored as conn->mux_ctx by the caller.
457 */
458 struct h2s *h2s;
459
460 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx);
461 if (!h2s)
462 goto fail_stream;
463 }
464
465 conn->mux_ctx = h2c;
466
Willy Tarreau0f383582018-10-03 14:22:21 +0200467 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200468 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200469 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200470 fail_stream:
471 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200472 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100473 if (t)
474 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200475 if (h2c->wait_event.task)
476 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100477 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200478 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200479 return -1;
480}
481
Willy Tarreau751f2d02018-10-05 09:35:00 +0200482/* returns the next allocatable outgoing stream ID for the H2 connection, or
483 * -1 if no more is allocatable.
484 */
485static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
486{
487 int32_t id = (h2c->max_id + 1) | 1;
488 if (id & 0x80000000U)
489 id = -1;
490 return id;
491}
492
Willy Tarreau2373acc2017-10-12 17:35:14 +0200493/* returns the stream associated with id <id> or NULL if not found */
494static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
495{
496 struct eb32_node *node;
497
Willy Tarreau751f2d02018-10-05 09:35:00 +0200498 if (id == 0)
499 return (struct h2s *)h2_closed_stream;
500
Willy Tarreau2a856182017-05-16 15:20:39 +0200501 if (id > h2c->max_id)
502 return (struct h2s *)h2_idle_stream;
503
Willy Tarreau2373acc2017-10-12 17:35:14 +0200504 node = eb32_lookup(&h2c->streams_by_id, id);
505 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200506 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200507
508 return container_of(node, struct h2s, by_id);
509}
510
Willy Tarreau62f52692017-10-08 23:01:42 +0200511/* release function for a connection. This one should be called to free all
512 * resources allocated to the mux.
513 */
514static void h2_release(struct connection *conn)
515{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200516 struct h2c *h2c = conn->mux_ctx;
517
518 LIST_DEL(&conn->list);
519
520 if (h2c) {
521 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200522
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100523 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100524 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100525 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200526
Willy Tarreau44e973f2018-03-01 17:49:30 +0100527 h2_release_buf(h2c, &h2c->dbuf);
528 h2_release_buf(h2c, &h2c->mbuf);
529
Willy Tarreauea392822017-10-31 10:02:25 +0100530 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200531 h2c->task->context = NULL;
532 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100533 h2c->task = NULL;
534 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200535 if (h2c->wait_event.task)
536 tasklet_free(h2c->wait_event.task);
537 if (h2c->wait_event.wait_reason != 0)
538 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
539 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100540
Willy Tarreaubafbe012017-11-24 17:34:44 +0100541 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 }
543
544 conn->mux = NULL;
545 conn->mux_ctx = NULL;
546
547 conn_stop_tracking(conn);
548 conn_full_close(conn);
549 if (conn->destroy_cb)
550 conn->destroy_cb(conn);
551 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200552}
553
554
Willy Tarreau71681172017-10-23 14:39:06 +0200555/******************************************************/
556/* functions below are for the H2 protocol processing */
557/******************************************************/
558
559/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200561{
562 return h2s ? h2s->id : 0;
563}
564
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200565/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100566static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200567{
568 if (h2c->msi < 0)
569 return 0;
570
571 if (h2c->msi == h2s_id(h2s))
572 return 0;
573
574 return 1;
575}
576
Willy Tarreau741d6df2017-10-17 08:00:59 +0200577/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100578static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200579{
580 h2c->errcode = err;
581 h2c->st0 = H2_CS_ERROR;
582}
583
Willy Tarreau2e43f082017-10-17 08:03:59 +0200584/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100585static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200586{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200587 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200588 h2s->errcode = err;
589 h2s->st = H2_SS_ERROR;
590 if (h2s->cs)
591 h2s->cs->flags |= CS_FL_ERROR;
592 }
593}
594
Willy Tarreaue4820742017-07-27 13:37:23 +0200595/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100596static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200597{
598 uint8_t *out = frame;
599
600 *out = len >> 16;
601 write_n16(out + 1, len);
602}
603
Willy Tarreau54c15062017-10-10 17:10:03 +0200604/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
605 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
606 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200607 * available in the buffer's input prior to calling this function. The buffer
608 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200609 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100610static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200611 const struct buffer *b, int o)
612{
Willy Tarreau591d4452018-06-15 17:21:00 +0200613 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 +0200614}
615
Willy Tarreau1f094672017-11-20 21:27:45 +0100616static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200617{
Willy Tarreau591d4452018-06-15 17:21:00 +0200618 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200619}
620
Willy Tarreau1f094672017-11-20 21:27:45 +0100621static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200622{
Willy Tarreau591d4452018-06-15 17:21:00 +0200623 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200624}
625
Willy Tarreau1f094672017-11-20 21:27:45 +0100626static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200627{
Willy Tarreau591d4452018-06-15 17:21:00 +0200628 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200629}
630
631
Willy Tarreau715d5312017-07-11 15:20:24 +0200632/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
633 * is not obvious. It turns out that H2 headers are neither aligned nor do they
634 * use regular sizes. And to add to the trouble, the buffer may wrap so each
635 * byte read must be checked. The header is formed like this :
636 *
637 * b0 b1 b2 b3 b4 b5..b8
638 * +----------+---------+--------+----+----+----------------------+
639 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
640 * +----------+---------+--------+----+----+----------------------+
641 *
642 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
643 * we get the sid properly aligned and ordered, and 16 bits of len properly
644 * ordered as well. The type and flags can be extracted using bit shifts from
645 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200646 * Returns zero if some bytes are missing, otherwise non-zero on success. The
647 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200648 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100649static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200650{
651 uint64_t w;
652
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200653 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200654 return 0;
655
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200656 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200657 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200658 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
659 h->ff = w >> 32;
660 h->ft = w >> 40;
661 h->len += w >> 48;
662 return 1;
663}
664
665/* skip the next 9 bytes corresponding to the frame header possibly parsed by
666 * h2_peek_frame_hdr() above.
667 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100668static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200669{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200670 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200671}
672
673/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100674static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200675{
676 int ret;
677
678 ret = h2_peek_frame_hdr(b, h);
679 if (ret > 0)
680 h2_skip_frame_hdr(b);
681 return ret;
682}
683
Willy Tarreau00dd0782018-03-01 16:31:34 +0100684/* marks stream <h2s> as CLOSED and decrement the number of active streams for
685 * its connection if the stream was not yet closed. Please use this exclusively
686 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100687 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100688static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100689{
690 if (h2s->st != H2_SS_CLOSED)
691 h2s->h2c->nb_streams--;
692 h2s->st = H2_SS_CLOSED;
693}
694
Willy Tarreau71049cc2018-03-28 13:56:39 +0200695/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
696static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100697{
698 h2s_close(h2s);
699 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200700 if (b_size(&h2s->rxbuf)) {
701 b_free(&h2s->rxbuf);
702 offer_buffers(NULL, tasks_run_queue);
703 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200704 if (h2s->send_wait != NULL)
705 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
706 if (h2s->recv_wait != NULL)
707 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800708 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200709 * reference left would be in the h2c send_list/fctl_list, and if
710 * we're in it, we're getting out anyway
711 */
712 LIST_DEL(&h2s->list);
713 LIST_INIT(&h2s->list);
714 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100715 pool_free(pool_head_h2s, h2s);
716}
717
Willy Tarreaua8e49542018-10-03 18:53:55 +0200718/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
719 * stream tree. In case of error, nothing is added and NULL is returned. The
720 * causes of errors can be any failed memory allocation. The caller is
721 * responsible for checking if the connection may support an extra stream
722 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200723 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200724static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200725{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200726 struct h2s *h2s;
727
Willy Tarreaubafbe012017-11-24 17:34:44 +0100728 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200729 if (!h2s)
730 goto out;
731
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200732 h2s->wait_event.task = tasklet_new();
733 if (!h2s->wait_event.task) {
734 pool_free(pool_head_h2s, h2s);
735 goto out;
736 }
737 h2s->send_wait = NULL;
738 h2s->recv_wait = NULL;
739 h2s->wait_event.task->process = h2_deferred_shut;
740 h2s->wait_event.task->context = h2s;
741 h2s->wait_event.handle = NULL;
742 h2s->wait_event.wait_reason = 0;
743 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200744 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200745 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200746 h2s->mws = h2c->miw;
747 h2s->flags = H2_SF_NONE;
748 h2s->errcode = H2_ERR_NO_ERROR;
749 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200750 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200751 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200752
753 if (h2c->flags & H2_CF_IS_BACK) {
754 h1m_init_req(&h2s->h1m);
755 h2s->h1m.err_pos = -1; // don't care about errors on the request path
756 h2s->h1m.flags |= H1_MF_TOLOWER;
757 } else {
758 h1m_init_res(&h2s->h1m);
759 h2s->h1m.err_pos = -1; // don't care about errors on the response path
760 h2s->h1m.flags |= H1_MF_TOLOWER;
761 }
762
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200763 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200764 if (id > 0)
765 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200766
767 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100768 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200769
770 return h2s;
771
772 out_free_h2s:
773 pool_free(pool_head_h2s, h2s);
774 out:
775 return NULL;
776}
777
778/* creates a new stream <id> on the h2c connection and returns it, or NULL in
779 * case of memory allocation error.
780 */
781static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
782{
783 struct session *sess = h2c->conn->owner;
784 struct conn_stream *cs;
785 struct h2s *h2s;
786
787 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
788 goto out;
789
790 h2s = h2s_new(h2c, id);
791 if (!h2s)
792 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200793
794 cs = cs_new(h2c->conn);
795 if (!cs)
796 goto out_close;
797
798 h2s->cs = cs;
799 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200800 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200801
802 if (stream_create_from_cs(cs) < 0)
803 goto out_free_cs;
804
Willy Tarreau590a0512018-09-05 11:56:48 +0200805 /* We want the accept date presented to the next stream to be the one
806 * we have now, the handshake time to be null (since the next stream
807 * is not delayed by a handshake), and the idle time to count since
808 * right now.
809 */
810 sess->accept_date = date;
811 sess->tv_accept = now;
812 sess->t_handshake = 0;
813
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200814 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200815 if (h2_has_too_many_cs(h2c))
816 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200817 return h2s;
818
819 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200820 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200821 cs_free(cs);
822 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200823 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200824 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200825 sess_log(sess);
826 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200827}
828
Willy Tarreau751f2d02018-10-05 09:35:00 +0200829/* allocates a new stream associated to conn_stream <cs> on the h2c connection
830 * and returns it, or NULL in case of memory allocation error or if the highest
831 * possible stream ID was reached.
832 */
833static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs)
834{
835 struct h2s *h2s = NULL;
836
837 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
838 goto out;
839
840 /* Defer choosing the ID until we send the first message to create the stream */
841 h2s = h2s_new(h2c, 0);
842 if (!h2s)
843 goto out;
844
845 h2s->cs = cs;
846 cs->ctx = h2s;
847 h2c->nb_cs++;
848
Willy Tarreau751f2d02018-10-05 09:35:00 +0200849 out:
850 return h2s;
851}
852
Willy Tarreaube5b7152017-09-25 16:25:39 +0200853/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
854 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
855 * the various settings codes.
856 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200857static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200858{
859 struct buffer *res;
860 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200861 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200862 int ret;
863
864 if (h2c_mux_busy(h2c, NULL)) {
865 h2c->flags |= H2_CF_DEM_MBUSY;
866 return 0;
867 }
868
Willy Tarreau44e973f2018-03-01 17:49:30 +0100869 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200870 if (!res) {
871 h2c->flags |= H2_CF_MUX_MALLOC;
872 h2c->flags |= H2_CF_DEM_MROOM;
873 return 0;
874 }
875
876 chunk_init(&buf, buf_data, sizeof(buf_data));
877 chunk_memcpy(&buf,
878 "\x00\x00\x00" /* length : 0 for now */
879 "\x04\x00" /* type : 4 (settings), flags : 0 */
880 "\x00\x00\x00\x00", /* stream ID : 0 */
881 9);
882
883 if (h2_settings_header_table_size != 4096) {
884 char str[6] = "\x00\x01"; /* header_table_size */
885
886 write_n32(str + 2, h2_settings_header_table_size);
887 chunk_memcat(&buf, str, 6);
888 }
889
890 if (h2_settings_initial_window_size != 65535) {
891 char str[6] = "\x00\x04"; /* initial_window_size */
892
893 write_n32(str + 2, h2_settings_initial_window_size);
894 chunk_memcat(&buf, str, 6);
895 }
896
897 if (h2_settings_max_concurrent_streams != 0) {
898 char str[6] = "\x00\x03"; /* max_concurrent_streams */
899
900 /* Note: 0 means "unlimited" for haproxy's config but not for
901 * the protocol, so never send this value!
902 */
903 write_n32(str + 2, h2_settings_max_concurrent_streams);
904 chunk_memcat(&buf, str, 6);
905 }
906
907 if (global.tune.bufsize != 16384) {
908 char str[6] = "\x00\x05"; /* max_frame_size */
909
910 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
911 * match bufsize - rewrite size, but at the moment it seems
912 * that clients don't take care of it.
913 */
914 write_n32(str + 2, global.tune.bufsize);
915 chunk_memcat(&buf, str, 6);
916 }
917
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200918 h2_set_frame_size(buf.area, buf.data - 9);
919 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200920 if (unlikely(ret <= 0)) {
921 if (!ret) {
922 h2c->flags |= H2_CF_MUX_MFULL;
923 h2c->flags |= H2_CF_DEM_MROOM;
924 return 0;
925 }
926 else {
927 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
928 return 0;
929 }
930 }
931 return ret;
932}
933
Willy Tarreau52eed752017-09-22 15:05:09 +0200934/* Try to receive a connection preface, then upon success try to send our
935 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
936 * missing data. It may return an error in h2c.
937 */
938static int h2c_frt_recv_preface(struct h2c *h2c)
939{
940 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200941 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200942
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200943 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200944
945 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200946 if (ret1 < 0)
947 sess_log(h2c->conn->owner);
948
Willy Tarreau52eed752017-09-22 15:05:09 +0200949 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
950 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
951 return 0;
952 }
953
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200954 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200955 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200956 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200957
Willy Tarreaube5b7152017-09-25 16:25:39 +0200958 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200959}
960
Willy Tarreau01b44822018-10-03 14:26:37 +0200961/* Try to send a connection preface, then upon success try to send our
962 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
963 * missing data. It may return an error in h2c.
964 */
965static int h2c_bck_send_preface(struct h2c *h2c)
966{
967 struct buffer *res;
968
969 if (h2c_mux_busy(h2c, NULL)) {
970 h2c->flags |= H2_CF_DEM_MBUSY;
971 return 0;
972 }
973
974 res = h2_get_buf(h2c, &h2c->mbuf);
975 if (!res) {
976 h2c->flags |= H2_CF_MUX_MALLOC;
977 h2c->flags |= H2_CF_DEM_MROOM;
978 return 0;
979 }
980
981 if (!b_data(res)) {
982 /* preface not yet sent */
983 b_istput(res, ist(H2_CONN_PREFACE));
984 }
985
986 return h2c_send_settings(h2c);
987}
988
Willy Tarreau081d4722017-05-16 21:51:05 +0200989/* try to send a GOAWAY frame on the connection to report an error or a graceful
990 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
991 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
992 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
993 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
994 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
995 * on unrecoverable failure. It will not attempt to send one again in this last
996 * case so that it is safe to use h2c_error() to report such errors.
997 */
998static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
999{
1000 struct buffer *res;
1001 char str[17];
1002 int ret;
1003
1004 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1005 return 1; // claim that it worked
1006
1007 if (h2c_mux_busy(h2c, h2s)) {
1008 if (h2s)
1009 h2s->flags |= H2_SF_BLK_MBUSY;
1010 else
1011 h2c->flags |= H2_CF_DEM_MBUSY;
1012 return 0;
1013 }
1014
Willy Tarreau44e973f2018-03-01 17:49:30 +01001015 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001016 if (!res) {
1017 h2c->flags |= H2_CF_MUX_MALLOC;
1018 if (h2s)
1019 h2s->flags |= H2_SF_BLK_MROOM;
1020 else
1021 h2c->flags |= H2_CF_DEM_MROOM;
1022 return 0;
1023 }
1024
1025 /* len: 8, type: 7, flags: none, sid: 0 */
1026 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1027
1028 if (h2c->last_sid < 0)
1029 h2c->last_sid = h2c->max_id;
1030
1031 write_n32(str + 9, h2c->last_sid);
1032 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001033 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001034 if (unlikely(ret <= 0)) {
1035 if (!ret) {
1036 h2c->flags |= H2_CF_MUX_MFULL;
1037 if (h2s)
1038 h2s->flags |= H2_SF_BLK_MROOM;
1039 else
1040 h2c->flags |= H2_CF_DEM_MROOM;
1041 return 0;
1042 }
1043 else {
1044 /* we cannot report this error using GOAWAY, so we mark
1045 * it and claim a success.
1046 */
1047 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1048 h2c->flags |= H2_CF_GOAWAY_FAILED;
1049 return 1;
1050 }
1051 }
1052 h2c->flags |= H2_CF_GOAWAY_SENT;
1053 return ret;
1054}
1055
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001056/* Try to send an RST_STREAM frame on the connection for the indicated stream
1057 * during mux operations. This stream must be valid and cannot be closed
1058 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1059 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1060 * not yet.
1061 *
1062 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1063 * to write the message, it subscribes the stream to future notifications.
1064 */
1065static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1066{
1067 struct buffer *res;
1068 char str[13];
1069 int ret;
1070
1071 if (!h2s || h2s->st == H2_SS_CLOSED)
1072 return 1;
1073
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001074 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1075 * RST_STREAM in response to a RST_STREAM frame.
1076 */
1077 if (h2c->dft == H2_FT_RST_STREAM) {
1078 ret = 1;
1079 goto ignore;
1080 }
1081
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001082 if (h2c_mux_busy(h2c, h2s)) {
1083 h2s->flags |= H2_SF_BLK_MBUSY;
1084 return 0;
1085 }
1086
Willy Tarreau44e973f2018-03-01 17:49:30 +01001087 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001088 if (!res) {
1089 h2c->flags |= H2_CF_MUX_MALLOC;
1090 h2s->flags |= H2_SF_BLK_MROOM;
1091 return 0;
1092 }
1093
1094 /* len: 4, type: 3, flags: none */
1095 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1096 write_n32(str + 5, h2s->id);
1097 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001098 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001099
1100 if (unlikely(ret <= 0)) {
1101 if (!ret) {
1102 h2c->flags |= H2_CF_MUX_MFULL;
1103 h2s->flags |= H2_SF_BLK_MROOM;
1104 return 0;
1105 }
1106 else {
1107 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1108 return 0;
1109 }
1110 }
1111
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001112 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001113 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001114 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001115 return ret;
1116}
1117
1118/* Try to send an RST_STREAM frame on the connection for the stream being
1119 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1120 * error code unless the stream's state already is IDLE or CLOSED in which
1121 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1122 * it was not yet.
1123 *
1124 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1125 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001126 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001127 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001128 */
1129static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1130{
1131 struct buffer *res;
1132 char str[13];
1133 int ret;
1134
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001135 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1136 * RST_STREAM in response to a RST_STREAM frame.
1137 */
1138 if (h2c->dft == H2_FT_RST_STREAM) {
1139 ret = 1;
1140 goto ignore;
1141 }
1142
Willy Tarreau27a84c92017-10-17 08:10:17 +02001143 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001144 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001145 return 0;
1146 }
1147
Willy Tarreau44e973f2018-03-01 17:49:30 +01001148 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001149 if (!res) {
1150 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001151 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001152 return 0;
1153 }
1154
1155 /* len: 4, type: 3, flags: none */
1156 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001157
Willy Tarreau27a84c92017-10-17 08:10:17 +02001158 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001159 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001160 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001161
Willy Tarreau27a84c92017-10-17 08:10:17 +02001162 if (unlikely(ret <= 0)) {
1163 if (!ret) {
1164 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001165 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001166 return 0;
1167 }
1168 else {
1169 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1170 return 0;
1171 }
1172 }
1173
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001174 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001175 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001176 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001177 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001178 }
1179
Willy Tarreau27a84c92017-10-17 08:10:17 +02001180 return ret;
1181}
1182
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001183/* try to send an empty DATA frame with the ES flag set to notify about the
1184 * end of stream and match a shutdown(write). If an ES was already sent as
1185 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1186 * on success or zero if nothing was done. In case of lack of room to write the
1187 * message, it subscribes the requesting stream to future notifications.
1188 */
1189static int h2_send_empty_data_es(struct h2s *h2s)
1190{
1191 struct h2c *h2c = h2s->h2c;
1192 struct buffer *res;
1193 char str[9];
1194 int ret;
1195
Willy Tarreau721c9742017-11-07 11:05:42 +01001196 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001197 return 1;
1198
1199 if (h2c_mux_busy(h2c, h2s)) {
1200 h2s->flags |= H2_SF_BLK_MBUSY;
1201 return 0;
1202 }
1203
Willy Tarreau44e973f2018-03-01 17:49:30 +01001204 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001205 if (!res) {
1206 h2c->flags |= H2_CF_MUX_MALLOC;
1207 h2s->flags |= H2_SF_BLK_MROOM;
1208 return 0;
1209 }
1210
1211 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1212 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1213 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001214 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001215 if (likely(ret > 0)) {
1216 h2s->flags |= H2_SF_ES_SENT;
1217 }
1218 else if (!ret) {
1219 h2c->flags |= H2_CF_MUX_MFULL;
1220 h2s->flags |= H2_SF_BLK_MROOM;
1221 return 0;
1222 }
1223 else {
1224 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1225 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001226 }
1227 return ret;
1228}
1229
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001230/* wake the streams attached to the connection, whose id is greater than <last>,
1231 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001232 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1233 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001234 */
1235static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1236{
1237 struct eb32_node *node;
1238 struct h2s *h2s;
1239
1240 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1241 flags |= CS_FL_ERROR;
1242
1243 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001244 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001245
1246 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1247 while (node) {
1248 h2s = container_of(node, struct h2s, by_id);
1249 if (h2s->id <= last)
1250 break;
1251 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001252
1253 if (!h2s->cs) {
1254 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001255 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001256 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001257 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001258
1259 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001260 if (h2s->recv_wait) {
1261 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001262 sw->wait_reason &= ~SUB_CAN_RECV;
1263 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001264 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001265 } else if (h2s->cs->data_cb->wake != NULL)
1266 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001267
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001268 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1269 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001270 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001271 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001272 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001273 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001274 }
1275}
1276
Willy Tarreau3421aba2017-07-27 15:41:03 +02001277/* Increase all streams' outgoing window size by the difference passed in
1278 * argument. This is needed upon receipt of the settings frame if the initial
1279 * window size is different. The difference may be negative and the resulting
1280 * window size as well, for the time it takes to receive some window updates.
1281 */
1282static void h2c_update_all_ws(struct h2c *h2c, int diff)
1283{
1284 struct h2s *h2s;
1285 struct eb32_node *node;
1286
1287 if (!diff)
1288 return;
1289
1290 node = eb32_first(&h2c->streams_by_id);
1291 while (node) {
1292 h2s = container_of(node, struct h2s, by_id);
1293 h2s->mws += diff;
1294 node = eb32_next(node);
1295 }
1296}
1297
1298/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1299 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1300 * return an error in h2c. Described in RFC7540#6.5.
1301 */
1302static int h2c_handle_settings(struct h2c *h2c)
1303{
1304 unsigned int offset;
1305 int error;
1306
1307 if (h2c->dff & H2_F_SETTINGS_ACK) {
1308 if (h2c->dfl) {
1309 error = H2_ERR_FRAME_SIZE_ERROR;
1310 goto fail;
1311 }
1312 return 1;
1313 }
1314
1315 if (h2c->dsi != 0) {
1316 error = H2_ERR_PROTOCOL_ERROR;
1317 goto fail;
1318 }
1319
1320 if (h2c->dfl % 6) {
1321 error = H2_ERR_FRAME_SIZE_ERROR;
1322 goto fail;
1323 }
1324
1325 /* that's the limit we can process */
1326 if (h2c->dfl > global.tune.bufsize) {
1327 error = H2_ERR_FRAME_SIZE_ERROR;
1328 goto fail;
1329 }
1330
1331 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001332 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001333 return 0;
1334
1335 /* parse the frame */
1336 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001337 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1338 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001339
1340 switch (type) {
1341 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1342 /* we need to update all existing streams with the
1343 * difference from the previous iws.
1344 */
1345 if (arg < 0) { // RFC7540#6.5.2
1346 error = H2_ERR_FLOW_CONTROL_ERROR;
1347 goto fail;
1348 }
1349 h2c_update_all_ws(h2c, arg - h2c->miw);
1350 h2c->miw = arg;
1351 break;
1352 case H2_SETTINGS_MAX_FRAME_SIZE:
1353 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1354 error = H2_ERR_PROTOCOL_ERROR;
1355 goto fail;
1356 }
1357 h2c->mfs = arg;
1358 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001359 case H2_SETTINGS_ENABLE_PUSH:
1360 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1361 error = H2_ERR_PROTOCOL_ERROR;
1362 goto fail;
1363 }
1364 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001365 }
1366 }
1367
1368 /* need to ACK this frame now */
1369 h2c->st0 = H2_CS_FRAME_A;
1370 return 1;
1371 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001372 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001373 h2c_error(h2c, error);
1374 return 0;
1375}
1376
1377/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1378 * success or one of the h2_status values.
1379 */
1380static int h2c_ack_settings(struct h2c *h2c)
1381{
1382 struct buffer *res;
1383 char str[9];
1384 int ret = -1;
1385
1386 if (h2c_mux_busy(h2c, NULL)) {
1387 h2c->flags |= H2_CF_DEM_MBUSY;
1388 return 0;
1389 }
1390
Willy Tarreau44e973f2018-03-01 17:49:30 +01001391 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001392 if (!res) {
1393 h2c->flags |= H2_CF_MUX_MALLOC;
1394 h2c->flags |= H2_CF_DEM_MROOM;
1395 return 0;
1396 }
1397
1398 memcpy(str,
1399 "\x00\x00\x00" /* length : 0 (no data) */
1400 "\x04" "\x01" /* type : 4, flags : ACK */
1401 "\x00\x00\x00\x00" /* stream ID */, 9);
1402
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001403 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001404 if (unlikely(ret <= 0)) {
1405 if (!ret) {
1406 h2c->flags |= H2_CF_MUX_MFULL;
1407 h2c->flags |= H2_CF_DEM_MROOM;
1408 return 0;
1409 }
1410 else {
1411 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1412 return 0;
1413 }
1414 }
1415 return ret;
1416}
1417
Willy Tarreaucf68c782017-10-10 17:11:41 +02001418/* processes a PING frame and schedules an ACK if needed. The caller must pass
1419 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1420 * missing data. It may return an error in h2c.
1421 */
1422static int h2c_handle_ping(struct h2c *h2c)
1423{
1424 /* frame length must be exactly 8 */
1425 if (h2c->dfl != 8) {
1426 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1427 return 0;
1428 }
1429
1430 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001431 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001432 h2c->st0 = H2_CS_FRAME_A;
1433 return 1;
1434}
1435
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001436/* Try to send a window update for stream id <sid> and value <increment>.
1437 * Returns > 0 on success or zero on missing room or failure. It may return an
1438 * error in h2c.
1439 */
1440static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1441{
1442 struct buffer *res;
1443 char str[13];
1444 int ret = -1;
1445
1446 if (h2c_mux_busy(h2c, NULL)) {
1447 h2c->flags |= H2_CF_DEM_MBUSY;
1448 return 0;
1449 }
1450
Willy Tarreau44e973f2018-03-01 17:49:30 +01001451 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001452 if (!res) {
1453 h2c->flags |= H2_CF_MUX_MALLOC;
1454 h2c->flags |= H2_CF_DEM_MROOM;
1455 return 0;
1456 }
1457
1458 /* length: 4, type: 8, flags: none */
1459 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1460 write_n32(str + 5, sid);
1461 write_n32(str + 9, increment);
1462
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001463 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001464
1465 if (unlikely(ret <= 0)) {
1466 if (!ret) {
1467 h2c->flags |= H2_CF_MUX_MFULL;
1468 h2c->flags |= H2_CF_DEM_MROOM;
1469 return 0;
1470 }
1471 else {
1472 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1473 return 0;
1474 }
1475 }
1476 return ret;
1477}
1478
1479/* try to send pending window update for the connection. It's safe to call it
1480 * with no pending updates. Returns > 0 on success or zero on missing room or
1481 * failure. It may return an error in h2c.
1482 */
1483static int h2c_send_conn_wu(struct h2c *h2c)
1484{
1485 int ret = 1;
1486
1487 if (h2c->rcvd_c <= 0)
1488 return 1;
1489
1490 /* send WU for the connection */
1491 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1492 if (ret > 0)
1493 h2c->rcvd_c = 0;
1494
1495 return ret;
1496}
1497
1498/* try to send pending window update for the current dmux stream. It's safe to
1499 * call it with no pending updates. Returns > 0 on success or zero on missing
1500 * room or failure. It may return an error in h2c.
1501 */
1502static int h2c_send_strm_wu(struct h2c *h2c)
1503{
1504 int ret = 1;
1505
1506 if (h2c->rcvd_s <= 0)
1507 return 1;
1508
1509 /* send WU for the stream */
1510 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1511 if (ret > 0)
1512 h2c->rcvd_s = 0;
1513
1514 return ret;
1515}
1516
Willy Tarreaucf68c782017-10-10 17:11:41 +02001517/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1518 * success, 0 on missing data or one of the h2_status values.
1519 */
1520static int h2c_ack_ping(struct h2c *h2c)
1521{
1522 struct buffer *res;
1523 char str[17];
1524 int ret = -1;
1525
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001526 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001527 return 0;
1528
1529 if (h2c_mux_busy(h2c, NULL)) {
1530 h2c->flags |= H2_CF_DEM_MBUSY;
1531 return 0;
1532 }
1533
Willy Tarreau44e973f2018-03-01 17:49:30 +01001534 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001535 if (!res) {
1536 h2c->flags |= H2_CF_MUX_MALLOC;
1537 h2c->flags |= H2_CF_DEM_MROOM;
1538 return 0;
1539 }
1540
1541 memcpy(str,
1542 "\x00\x00\x08" /* length : 8 (same payload) */
1543 "\x06" "\x01" /* type : 6, flags : ACK */
1544 "\x00\x00\x00\x00" /* stream ID */, 9);
1545
1546 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001547 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001548
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001549 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001550 if (unlikely(ret <= 0)) {
1551 if (!ret) {
1552 h2c->flags |= H2_CF_MUX_MFULL;
1553 h2c->flags |= H2_CF_DEM_MROOM;
1554 return 0;
1555 }
1556 else {
1557 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1558 return 0;
1559 }
1560 }
1561 return ret;
1562}
1563
Willy Tarreau26f95952017-07-27 17:18:30 +02001564/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1565 * Returns > 0 on success or zero on missing data. It may return an error in
1566 * h2c or h2s. Described in RFC7540#6.9.
1567 */
1568static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1569{
1570 int32_t inc;
1571 int error;
1572
1573 if (h2c->dfl != 4) {
1574 error = H2_ERR_FRAME_SIZE_ERROR;
1575 goto conn_err;
1576 }
1577
1578 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001579 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001580 return 0;
1581
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001582 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001583
1584 if (h2c->dsi != 0) {
1585 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001586
1587 /* it's not an error to receive WU on a closed stream */
1588 if (h2s->st == H2_SS_CLOSED)
1589 return 1;
1590
1591 if (!inc) {
1592 error = H2_ERR_PROTOCOL_ERROR;
1593 goto strm_err;
1594 }
1595
1596 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1597 error = H2_ERR_FLOW_CONTROL_ERROR;
1598 goto strm_err;
1599 }
1600
1601 h2s->mws += inc;
1602 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1603 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001604 if (h2s->send_wait)
1605 LIST_ADDQ(&h2c->send_list, &h2s->list);
1606
Willy Tarreau26f95952017-07-27 17:18:30 +02001607 }
1608 }
1609 else {
1610 /* connection window update */
1611 if (!inc) {
1612 error = H2_ERR_PROTOCOL_ERROR;
1613 goto conn_err;
1614 }
1615
1616 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1617 error = H2_ERR_FLOW_CONTROL_ERROR;
1618 goto conn_err;
1619 }
1620
1621 h2c->mws += inc;
1622 }
1623
1624 return 1;
1625
1626 conn_err:
1627 h2c_error(h2c, error);
1628 return 0;
1629
1630 strm_err:
1631 if (h2s) {
1632 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001633 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001634 }
1635 else
1636 h2c_error(h2c, error);
1637 return 0;
1638}
1639
Willy Tarreaue96b0922017-10-30 00:28:29 +01001640/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1641 * the last ID. Returns > 0 on success or zero on missing data. It may return
1642 * an error in h2c. Described in RFC7540#6.8.
1643 */
1644static int h2c_handle_goaway(struct h2c *h2c)
1645{
1646 int error;
1647 int last;
1648
1649 if (h2c->dsi != 0) {
1650 error = H2_ERR_PROTOCOL_ERROR;
1651 goto conn_err;
1652 }
1653
1654 if (h2c->dfl < 8) {
1655 error = H2_ERR_FRAME_SIZE_ERROR;
1656 goto conn_err;
1657 }
1658
1659 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001660 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001661 return 0;
1662
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001663 last = h2_get_n32(&h2c->dbuf, 0);
1664 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001665 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001666 if (h2c->last_sid < 0)
1667 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001668 return 1;
1669
1670 conn_err:
1671 h2c_error(h2c, error);
1672 return 0;
1673}
1674
Willy Tarreau92153fc2017-12-03 19:46:19 +01001675/* processes a PRIORITY frame, and either skips it or rejects if it is
1676 * invalid. Returns > 0 on success or zero on missing data. It may return
1677 * an error in h2c. Described in RFC7540#6.3.
1678 */
1679static int h2c_handle_priority(struct h2c *h2c)
1680{
1681 int error;
1682
1683 if (h2c->dsi == 0) {
1684 error = H2_ERR_PROTOCOL_ERROR;
1685 goto conn_err;
1686 }
1687
1688 if (h2c->dfl != 5) {
1689 error = H2_ERR_FRAME_SIZE_ERROR;
1690 goto conn_err;
1691 }
1692
1693 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001694 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001695 return 0;
1696
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001697 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001698 /* 7540#5.3 : can't depend on itself */
1699 error = H2_ERR_PROTOCOL_ERROR;
1700 goto conn_err;
1701 }
1702 return 1;
1703
1704 conn_err:
1705 h2c_error(h2c, error);
1706 return 0;
1707}
1708
Willy Tarreaucd234e92017-08-18 10:59:39 +02001709/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1710 * Returns > 0 on success or zero on missing data. It may return an error in
1711 * h2c. Described in RFC7540#6.4.
1712 */
1713static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1714{
1715 int error;
1716
1717 if (h2c->dsi == 0) {
1718 error = H2_ERR_PROTOCOL_ERROR;
1719 goto conn_err;
1720 }
1721
Willy Tarreaucd234e92017-08-18 10:59:39 +02001722 if (h2c->dfl != 4) {
1723 error = H2_ERR_FRAME_SIZE_ERROR;
1724 goto conn_err;
1725 }
1726
1727 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001728 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001729 return 0;
1730
1731 /* late RST, already handled */
1732 if (h2s->st == H2_SS_CLOSED)
1733 return 1;
1734
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001735 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001736 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001737
1738 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001739 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001740 if (h2s->recv_wait) {
1741 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001742
1743 sw->wait_reason &= ~SUB_CAN_RECV;
1744 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001745 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001746 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001747 }
1748
1749 h2s->flags |= H2_SF_RST_RCVD;
1750 return 1;
1751
1752 conn_err:
1753 h2c_error(h2c, error);
1754 return 0;
1755}
1756
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001757/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1758 * It may return an error in h2c or h2s. The caller must consider that the
1759 * return value is the new h2s in case one was allocated (most common case).
1760 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001761 * errors here are reported as connection errors since it's impossible to
1762 * recover from such errors after the compression context has been altered.
1763 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001764static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001765{
1766 int error;
1767
1768 if (!h2c->dfl) {
1769 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001770 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001771 goto strm_err;
1772 }
1773
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001774 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001775 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001776
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001777 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001778 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001779
Willy Tarreauf2101912018-07-19 10:11:38 +02001780 if (h2c->flags & H2_CF_DEM_TOOMANY)
1781 return 0; // too many cs still present
1782
Willy Tarreau13278b42017-10-13 19:23:14 +02001783 /* now either the frame is complete or the buffer is complete */
1784 if (h2s->st != H2_SS_IDLE) {
1785 /* FIXME: stream already exists, this is only allowed for
1786 * trailers (not supported for now).
1787 */
1788 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001789 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001790 goto conn_err;
1791 }
1792 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1793 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1794 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001795 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001796 goto conn_err;
1797 }
1798
Willy Tarreau22de8d32018-09-05 19:55:58 +02001799 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001800 * positively from h2c_frt_stream_new(), the stream will report the error,
1801 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001802 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001803 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001804 if (!h2s) {
1805 error = H2_ERR_INTERNAL_ERROR;
1806 goto conn_err;
1807 }
1808
1809 h2s->st = H2_SS_OPEN;
1810 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1811 h2s->st = H2_SS_HREM;
1812 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001813 /* note: cs cannot be null for now (just created above) */
1814 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001815 }
1816
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001817 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001818 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001819
Willy Tarreau8f650c32017-11-21 19:36:21 +01001820 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001821 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001822
Willy Tarreau721c9742017-11-07 11:05:42 +01001823 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001824 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001825 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001826 }
1827 else {
1828 /* update the max stream ID if the request is being processed */
1829 if (h2s->id > h2c->max_id)
1830 h2c->max_id = h2s->id;
1831 }
1832
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001833 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001834
1835 conn_err:
1836 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001837 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001838
1839 strm_err:
1840 if (h2s) {
1841 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001842 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001843 }
1844 else
1845 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001846 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001847}
1848
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001849/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1850 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1851 * errors here are reported as connection errors since it's impossible to
1852 * recover from such errors after the compression context has been altered.
1853 */
1854static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1855{
1856 int error;
1857
1858 if (!h2c->dfl) {
1859 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1860 sess_log(h2c->conn->owner);
1861 goto strm_err;
1862 }
1863
1864 if (!b_size(&h2c->dbuf))
1865 return NULL; // empty buffer
1866
1867 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1868 return NULL; // incomplete frame
1869
1870 if (h2c->flags & H2_CF_DEM_TOOMANY)
1871 return 0; // too many cs still present
1872
1873 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1874 h2s->flags |= H2_SF_ES_RCVD;
1875 h2s->cs->flags |= CS_FL_REOS;
1876 }
1877
1878 if (!h2s_decode_headers(h2s))
1879 return NULL;
1880
1881 if (h2c->st0 >= H2_CS_ERROR)
1882 return NULL;
1883
1884 if (h2s->st >= H2_SS_ERROR) {
1885 /* stream error : send RST_STREAM */
1886 h2c->st0 = H2_CS_FRAME_E;
1887 }
1888
1889 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1890 h2s->st = H2_SS_ERROR;
1891 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1892 h2s->st = H2_SS_HREM;
1893 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1894 h2s_close(h2s);
1895
1896 return h2s;
1897
1898 conn_err:
1899 h2c_error(h2c, error);
1900 return NULL;
1901
1902 strm_err:
1903 if (h2s) {
1904 h2s_error(h2s, error);
1905 h2c->st0 = H2_CS_FRAME_E;
1906 }
1907 else
1908 h2c_error(h2c, error);
1909 return NULL;
1910}
1911
Willy Tarreau454f9052017-10-26 19:40:35 +02001912/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1913 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1914 */
1915static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1916{
1917 int error;
1918
1919 /* note that empty DATA frames are perfectly valid and sometimes used
1920 * to signal an end of stream (with the ES flag).
1921 */
1922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001923 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001924 return 0; // empty buffer
1925
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001926 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001927 return 0; // incomplete frame
1928
1929 /* now either the frame is complete or the buffer is complete */
1930
1931 if (!h2c->dsi) {
1932 /* RFC7540#6.1 */
1933 error = H2_ERR_PROTOCOL_ERROR;
1934 goto conn_err;
1935 }
1936
1937 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1938 /* RFC7540#6.1 */
1939 error = H2_ERR_STREAM_CLOSED;
1940 goto strm_err;
1941 }
1942
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001943 if (!h2_frt_transfer_data(h2s))
1944 return 0;
1945
Willy Tarreau454f9052017-10-26 19:40:35 +02001946 /* call the upper layers to process the frame, then let the upper layer
1947 * notify the stream about any change.
1948 */
1949 if (!h2s->cs) {
1950 error = H2_ERR_STREAM_CLOSED;
1951 goto strm_err;
1952 }
1953
Willy Tarreau8f650c32017-11-21 19:36:21 +01001954 if (h2c->st0 >= H2_CS_ERROR)
1955 return 0;
1956
Willy Tarreau721c9742017-11-07 11:05:42 +01001957 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001958 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001959 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001960 }
1961
1962 /* check for completion : the callee will change this to FRAME_A or
1963 * FRAME_H once done.
1964 */
1965 if (h2c->st0 == H2_CS_FRAME_P)
1966 return 0;
1967
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001968
1969 /* last frame */
1970 if (h2c->dff & H2_F_DATA_END_STREAM) {
1971 h2s->st = H2_SS_HREM;
1972 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001973 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001974 }
1975
Willy Tarreau454f9052017-10-26 19:40:35 +02001976 return 1;
1977
1978 conn_err:
1979 h2c_error(h2c, error);
1980 return 0;
1981
1982 strm_err:
1983 if (h2s) {
1984 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001985 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001986 }
1987 else
1988 h2c_error(h2c, error);
1989 return 0;
1990}
1991
Willy Tarreaubc933932017-10-09 16:21:43 +02001992/* process Rx frames to be demultiplexed */
1993static void h2_process_demux(struct h2c *h2c)
1994{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001995 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001996
Willy Tarreau081d4722017-05-16 21:51:05 +02001997 if (h2c->st0 >= H2_CS_ERROR)
1998 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001999
2000 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2001 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002002 if (h2c->flags & H2_CF_IS_BACK)
2003 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002004 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2005 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002006 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002007 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002008 sess_log(h2c->conn->owner);
2009 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002010 goto fail;
2011 }
2012
2013 h2c->max_id = 0;
2014 h2c->st0 = H2_CS_SETTINGS1;
2015 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002016
2017 if (h2c->st0 == H2_CS_SETTINGS1) {
2018 struct h2_fh hdr;
2019
2020 /* ensure that what is pending is a valid SETTINGS frame
2021 * without an ACK.
2022 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002023 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002024 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002025 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002026 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002027 sess_log(h2c->conn->owner);
2028 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002029 goto fail;
2030 }
2031
2032 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2033 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2034 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2035 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002036 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002037 goto fail;
2038 }
2039
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002040 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002041 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2042 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2043 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002044 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002045 goto fail;
2046 }
2047
2048 /* that's OK, switch to FRAME_P to process it */
2049 h2c->dfl = hdr.len;
2050 h2c->dsi = hdr.sid;
2051 h2c->dft = hdr.ft;
2052 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002053 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002054 h2c->st0 = H2_CS_FRAME_P;
2055 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002056 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002057
2058 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002059 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002060 int ret = 0;
2061
2062 if (h2c->st0 >= H2_CS_ERROR)
2063 break;
2064
2065 if (h2c->st0 == H2_CS_FRAME_H) {
2066 struct h2_fh hdr;
2067
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002068 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002069 break;
2070
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002071 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002072 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2073 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002074 if (!h2c->nb_streams) {
2075 /* only log if no other stream can report the error */
2076 sess_log(h2c->conn->owner);
2077 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002078 break;
2079 }
2080
2081 h2c->dfl = hdr.len;
2082 h2c->dsi = hdr.sid;
2083 h2c->dft = hdr.ft;
2084 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002085 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002086 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002087 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002088 }
2089
2090 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002091 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2092
Olivier Houchard638b7992018-08-16 15:41:52 +02002093 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002094 /* we may have to signal the upper layers */
2095 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002096 if (h2s->recv_wait) {
2097 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2098 tasklet_wakeup(h2s->recv_wait->task);
2099 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002100 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002101 }
2102 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002103
Willy Tarreaud7901432017-12-29 11:34:40 +01002104 if (h2c->st0 == H2_CS_FRAME_E)
2105 goto strm_err;
2106
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002107 if (h2s->st == H2_SS_IDLE &&
2108 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2109 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2110 * this state MUST be treated as a connection error
2111 */
2112 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2113 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002114 if (!h2c->nb_streams) {
2115 /* only log if no other stream can report the error */
2116 sess_log(h2c->conn->owner);
2117 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002118 break;
2119 }
2120
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002121 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2122 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2123 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2124 * this state MUST be treated as a stream error
2125 */
2126 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2127 goto strm_err;
2128 }
2129
Willy Tarreauab837502017-12-27 15:07:30 +01002130 /* Below the management of frames received in closed state is a
2131 * bit hackish because the spec makes strong differences between
2132 * streams closed by receiving RST, sending RST, and seeing ES
2133 * in both directions. In addition to this, the creation of a
2134 * new stream reusing the identifier of a closed one will be
2135 * detected here. Given that we cannot keep track of all closed
2136 * streams forever, we consider that unknown closed streams were
2137 * closed on RST received, which allows us to respond with an
2138 * RST without breaking the connection (eg: to abort a transfer).
2139 * Some frames have to be silently ignored as well.
2140 */
2141 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2142 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2143 /* #5.1.1: The identifier of a newly
2144 * established stream MUST be numerically
2145 * greater than all streams that the initiating
2146 * endpoint has opened or reserved. This
2147 * governs streams that are opened using a
2148 * HEADERS frame and streams that are reserved
2149 * using PUSH_PROMISE. An endpoint that
2150 * receives an unexpected stream identifier
2151 * MUST respond with a connection error.
2152 */
2153 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2154 goto strm_err;
2155 }
2156
2157 if (h2s->flags & H2_SF_RST_RCVD) {
2158 /* RFC7540#5.1:closed: an endpoint that
2159 * receives any frame other than PRIORITY after
2160 * receiving a RST_STREAM MUST treat that as a
2161 * stream error of type STREAM_CLOSED.
2162 *
2163 * Note that old streams fall into this category
2164 * and will lead to an RST being sent.
2165 */
2166 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2167 h2c->st0 = H2_CS_FRAME_E;
2168 goto strm_err;
2169 }
2170
2171 /* RFC7540#5.1:closed: if this state is reached as a
2172 * result of sending a RST_STREAM frame, the peer that
2173 * receives the RST_STREAM might have already sent
2174 * frames on the stream that cannot be withdrawn. An
2175 * endpoint MUST ignore frames that it receives on
2176 * closed streams after it has sent a RST_STREAM
2177 * frame. An endpoint MAY choose to limit the period
2178 * over which it ignores frames and treat frames that
2179 * arrive after this time as being in error.
2180 */
2181 if (!(h2s->flags & H2_SF_RST_SENT)) {
2182 /* RFC7540#5.1:closed: any frame other than
2183 * PRIO/WU/RST in this state MUST be treated as
2184 * a connection error
2185 */
2186 if (h2c->dft != H2_FT_RST_STREAM &&
2187 h2c->dft != H2_FT_PRIORITY &&
2188 h2c->dft != H2_FT_WINDOW_UPDATE) {
2189 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2190 goto strm_err;
2191 }
2192 }
2193 }
2194
Willy Tarreauc0da1962017-10-30 18:38:00 +01002195#if 0
2196 // problem below: it is not possible to completely ignore such
2197 // streams as we need to maintain the compression state as well
2198 // and for this we need to completely process these frames (eg:
2199 // HEADERS frames) as well as counting DATA frames to emit
2200 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2201 // This is a typical case of layer violation where the
2202 // transported contents are critical to the connection's
2203 // validity and must be ignored at the same time :-(
2204
2205 /* graceful shutdown, ignore streams whose ID is higher than
2206 * the one advertised in GOAWAY. RFC7540#6.8.
2207 */
2208 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002209 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2210 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002211 h2c->dfl -= ret;
2212 ret = h2c->dfl == 0;
2213 goto strm_err;
2214 }
2215#endif
2216
Willy Tarreau7e98c052017-10-10 15:56:59 +02002217 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002218 case H2_FT_SETTINGS:
2219 if (h2c->st0 == H2_CS_FRAME_P)
2220 ret = h2c_handle_settings(h2c);
2221
2222 if (h2c->st0 == H2_CS_FRAME_A)
2223 ret = h2c_ack_settings(h2c);
2224 break;
2225
Willy Tarreaucf68c782017-10-10 17:11:41 +02002226 case H2_FT_PING:
2227 if (h2c->st0 == H2_CS_FRAME_P)
2228 ret = h2c_handle_ping(h2c);
2229
2230 if (h2c->st0 == H2_CS_FRAME_A)
2231 ret = h2c_ack_ping(h2c);
2232 break;
2233
Willy Tarreau26f95952017-07-27 17:18:30 +02002234 case H2_FT_WINDOW_UPDATE:
2235 if (h2c->st0 == H2_CS_FRAME_P)
2236 ret = h2c_handle_window_update(h2c, h2s);
2237 break;
2238
Willy Tarreau61290ec2017-10-17 08:19:21 +02002239 case H2_FT_CONTINUATION:
2240 /* we currently don't support CONTINUATION frames since
2241 * we have nowhere to store the partial HEADERS frame.
2242 * Let's abort the stream on an INTERNAL_ERROR here.
2243 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002244 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002245 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002246 h2c->st0 = H2_CS_FRAME_E;
2247 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002248 break;
2249
Willy Tarreau13278b42017-10-13 19:23:14 +02002250 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002251 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002252 if (h2c->flags & H2_CF_IS_BACK)
2253 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2254 else
2255 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002256 if (tmp_h2s) {
2257 h2s = tmp_h2s;
2258 ret = 1;
2259 }
2260 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002261 break;
2262
Willy Tarreau454f9052017-10-26 19:40:35 +02002263 case H2_FT_DATA:
2264 if (h2c->st0 == H2_CS_FRAME_P)
2265 ret = h2c_frt_handle_data(h2c, h2s);
2266
2267 if (h2c->st0 == H2_CS_FRAME_A)
2268 ret = h2c_send_strm_wu(h2c);
2269 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002270
Willy Tarreau92153fc2017-12-03 19:46:19 +01002271 case H2_FT_PRIORITY:
2272 if (h2c->st0 == H2_CS_FRAME_P)
2273 ret = h2c_handle_priority(h2c);
2274 break;
2275
Willy Tarreaucd234e92017-08-18 10:59:39 +02002276 case H2_FT_RST_STREAM:
2277 if (h2c->st0 == H2_CS_FRAME_P)
2278 ret = h2c_handle_rst_stream(h2c, h2s);
2279 break;
2280
Willy Tarreaue96b0922017-10-30 00:28:29 +01002281 case H2_FT_GOAWAY:
2282 if (h2c->st0 == H2_CS_FRAME_P)
2283 ret = h2c_handle_goaway(h2c);
2284 break;
2285
Willy Tarreau1c661982017-10-30 13:52:01 +01002286 case H2_FT_PUSH_PROMISE:
2287 /* not permitted here, RFC7540#5.1 */
2288 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002289 if (!h2c->nb_streams) {
2290 /* only log if no other stream can report the error */
2291 sess_log(h2c->conn->owner);
2292 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002293 break;
2294
2295 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002296 default:
2297 /* drop frames that we ignore. They may be larger than
2298 * the buffer so we drain all of their contents until
2299 * we reach the end.
2300 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002301 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2302 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002303 h2c->dfl -= ret;
2304 ret = h2c->dfl == 0;
2305 }
2306
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002307 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002308 /* We may have to send an RST if not done yet */
2309 if (h2s->st == H2_SS_ERROR)
2310 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002311
Willy Tarreaua20a5192017-12-27 11:02:06 +01002312 if (h2c->st0 == H2_CS_FRAME_E)
2313 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002314
Willy Tarreau7e98c052017-10-10 15:56:59 +02002315 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002316 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002317 break;
2318
2319 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002320 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002321 h2c->st0 = H2_CS_FRAME_H;
2322 }
2323 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002324
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002325 if (h2c->rcvd_c > 0 &&
2326 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2327 h2c_send_conn_wu(h2c);
2328
Willy Tarreau52eed752017-09-22 15:05:09 +02002329 fail:
2330 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002331 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002332 /* we may have to signal the upper layers */
2333 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002334 if (h2s->recv_wait) {
2335 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2336 tasklet_wakeup(h2s->recv_wait->task);
2337 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002338 }
2339 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002340
2341 if (h2_recv_allowed(h2c))
2342 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002343}
2344
2345/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2346 * the end.
2347 */
2348static int h2_process_mux(struct h2c *h2c)
2349{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002350 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002351
Willy Tarreau01b44822018-10-03 14:26:37 +02002352 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2353 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2354 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2355 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2356 if (h2c->st0 == H2_CS_ERROR) {
2357 h2c->st0 = H2_CS_ERROR2;
2358 sess_log(h2c->conn->owner);
2359 }
2360 goto fail;
2361 }
2362 h2c->st0 = H2_CS_SETTINGS1;
2363 }
2364 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002365 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002366 return 1;
2367 }
2368
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002369 /* start by sending possibly pending window updates */
2370 if (h2c->rcvd_c > 0 &&
2371 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2372 h2c_send_conn_wu(h2c) < 0)
2373 goto fail;
2374
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002375 /* First we always process the flow control list because the streams
2376 * waiting there were already elected for immediate emission but were
2377 * blocked just on this.
2378 */
2379
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002380 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002381 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2382 h2c->st0 >= H2_CS_ERROR)
2383 break;
2384
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002385 h2s->flags &= ~H2_SF_BLK_ANY;
2386 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002387 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002388 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002389 LIST_DEL(&h2s->list);
2390 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002391 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002392 }
2393
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002394 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002395 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2396 break;
2397
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002398 h2s->flags &= ~H2_SF_BLK_ANY;
2399 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002400 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002401 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002402 LIST_DEL(&h2s->list);
2403 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002404 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002405 }
2406
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002407 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002408 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002409 if (h2c->st0 == H2_CS_ERROR) {
2410 if (h2c->max_id >= 0) {
2411 h2c_send_goaway_error(h2c, NULL);
2412 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2413 return 0;
2414 }
2415
2416 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2417 }
2418 return 1;
2419 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002420 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002421}
2422
Willy Tarreau62f52692017-10-08 23:01:42 +02002423
Willy Tarreau479998a2018-11-18 06:30:59 +01002424/* Attempt to read data, and subscribe if none available.
2425 * The function returns 1 if data has been received, otherwise zero.
2426 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002427static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002428{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002429 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002430 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002431 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002432 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002433
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002434 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002435 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002436
Willy Tarreau315d8072017-12-10 22:17:57 +01002437 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002438 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002439
Willy Tarreau44e973f2018-03-01 17:49:30 +01002440 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002441 if (!buf) {
2442 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002443 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002444 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002445
Olivier Houchard7505f942018-08-21 18:10:44 +02002446 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002447 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002448 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2449 /* HTX in use : try to pre-align the buffer like the
2450 * rxbufs will be to optimize memory copies. We'll make
2451 * sure that the frame header lands at the end of the
2452 * HTX block to alias it upon recv. We cannot use the
2453 * head because rcv_buf() will realign the buffer if
2454 * it's empty. Thus we cheat and pretend we already
2455 * have a few bytes there.
2456 */
2457 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002458 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002459 }
2460 else
2461 max = b_room(buf);
2462
Olivier Houchard7505f942018-08-21 18:10:44 +02002463 if (max)
2464 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2465 else
2466 ret = 0;
2467 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002468
Olivier Houchard53216e72018-10-10 15:46:36 +02002469 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002470 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002471
Olivier Houcharda1411e62018-08-17 18:42:48 +02002472 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002473 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002474 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002475 }
2476
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002477 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002478 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002479 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002480}
2481
Willy Tarreau479998a2018-11-18 06:30:59 +01002482/* Try to send data if possible.
2483 * The function returns 1 if data have been sent, otherwise zero.
2484 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002485static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002486{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002487 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002488 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002489 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002490
2491 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002492 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002493
Olivier Houchard7505f942018-08-21 18:10:44 +02002494
Willy Tarreaua2af5122017-10-09 11:56:46 +02002495 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2496 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002497 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002498 }
2499
Willy Tarreaubc933932017-10-09 16:21:43 +02002500 /* This loop is quite simple : it tries to fill as much as it can from
2501 * pending streams into the existing buffer until it's reportedly full
2502 * or the end of send requests is reached. Then it tries to send this
2503 * buffer's contents out, marks it not full if at least one byte could
2504 * be sent, and tries again.
2505 *
2506 * The snd_buf() function normally takes a "flags" argument which may
2507 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2508 * data immediately comes and CO_SFL_STREAMER to indicate that the
2509 * connection is streaming lots of data (used to increase TLS record
2510 * size at the expense of latency). The former can be sent any time
2511 * there's a buffer full flag, as it indicates at least one stream
2512 * attempted to send and failed so there are pending data. An
2513 * alternative would be to set it as long as there's an active stream
2514 * but that would be problematic for ACKs until we have an absolute
2515 * guarantee that all waiters have at least one byte to send. The
2516 * latter should possibly not be set for now.
2517 */
2518
2519 done = 0;
2520 while (!done) {
2521 unsigned int flags = 0;
2522
2523 /* fill as much as we can into the current buffer */
2524 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2525 done = h2_process_mux(h2c);
2526
2527 if (conn->flags & CO_FL_ERROR)
2528 break;
2529
2530 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2531 flags |= CO_SFL_MSG_MORE;
2532
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002533 if (b_data(&h2c->mbuf)) {
2534 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002535 if (!ret)
2536 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002537 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002538 b_del(&h2c->mbuf, ret);
2539 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002540 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002541
2542 /* wrote at least one byte, the buffer is not full anymore */
2543 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2544 }
2545
Willy Tarreaua2af5122017-10-09 11:56:46 +02002546 if (conn->flags & CO_FL_SOCK_WR_SH) {
2547 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002548 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002549 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002550 /* We're not full anymore, so we can wake any task that are waiting
2551 * for us.
2552 */
2553 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002554 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002555 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2556 struct h2s *, list);
2557 LIST_DEL(&h2s->list);
2558 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002559 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002560 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002561 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002562 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002563 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002564 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002565 /* We're done, no more to send */
2566 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002567 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002568schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002569 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2570 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002571 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002572}
2573
2574static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2575{
2576 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002577 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002578
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002579 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002580 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002581 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002582 ret |= h2_recv(h2c);
2583 if (ret)
2584 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002585 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002586}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002587
Willy Tarreau62f52692017-10-08 23:01:42 +02002588/* callback called on any event by the connection handler.
2589 * It applies changes and returns zero, or < 0 if it wants immediate
2590 * destruction of the connection (which normally doesn not happen in h2).
2591 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002592static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002593{
Olivier Houchard7505f942018-08-21 18:10:44 +02002594 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002595
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002596 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002597 h2_process_demux(h2c);
2598
2599 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002600 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002601
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002602 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002603 h2c->flags &= ~H2_CF_DEM_DFULL;
2604 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002605 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002606
Willy Tarreau0b37d652018-10-03 10:33:02 +02002607 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002608 /* frontend is stopping, reload likely in progress, let's try
2609 * to announce a graceful shutdown if not yet done. We don't
2610 * care if it fails, it will be tried again later.
2611 */
2612 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2613 if (h2c->last_sid < 0)
2614 h2c->last_sid = (1U << 31) - 1;
2615 h2c_send_goaway_error(h2c, NULL);
2616 }
2617 }
2618
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002619 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002620 * If we received early data, and the handshake is done, wake
2621 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002622 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002623 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2624 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2625 struct eb32_node *node;
2626 struct h2s *h2s;
2627
2628 h2c->flags |= H2_CF_WAIT_FOR_HS;
2629 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2630
2631 while (node) {
2632 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002633 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002634 h2s->recv_wait) {
2635 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002636 sw->wait_reason &= ~SUB_CAN_RECV;
2637 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002638 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002639 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002640 node = eb32_next(node);
2641 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002642 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002643
Willy Tarreau26bd7612017-10-09 16:47:04 +02002644 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002645 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2646 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2647 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002648 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002649
2650 if (eb_is_empty(&h2c->streams_by_id)) {
2651 /* no more stream, kill the connection now */
2652 h2_release(conn);
2653 return -1;
2654 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002655 }
2656
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002657 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002658 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002659
Olivier Houchard53216e72018-10-10 15:46:36 +02002660 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2661 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2662 (h2c->st0 != H2_CS_ERROR &&
2663 !b_data(&h2c->mbuf) &&
2664 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2665 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002666 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002667
Willy Tarreau3f133572017-10-31 19:21:06 +01002668 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002669 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002670 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002671 task_queue(h2c->task);
2672 }
2673 else
2674 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002675 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002676
Olivier Houchard7505f942018-08-21 18:10:44 +02002677 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002678 return 0;
2679}
2680
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002681static int h2_wake(struct connection *conn)
2682{
2683 struct h2c *h2c = conn->mux_ctx;
2684
2685 return (h2_process(h2c));
2686}
2687
Willy Tarreauea392822017-10-31 10:02:25 +01002688/* Connection timeout management. The principle is that if there's no receipt
2689 * nor sending for a certain amount of time, the connection is closed. If the
2690 * MUX buffer still has lying data or is not allocatable, the connection is
2691 * immediately killed. If it's allocatable and empty, we attempt to send a
2692 * GOAWAY frame.
2693 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002694static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002695{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002696 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002697 int expired = tick_is_expired(t->expire, now_ms);
2698
Willy Tarreau0975f112018-03-29 15:22:59 +02002699 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002700 return t;
2701
Willy Tarreau0975f112018-03-29 15:22:59 +02002702 task_delete(t);
2703 task_free(t);
2704
2705 if (!h2c) {
2706 /* resources were already deleted */
2707 return NULL;
2708 }
2709
2710 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002711 h2c_error(h2c, H2_ERR_NO_ERROR);
2712 h2_wake_some_streams(h2c, 0, 0);
2713
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002714 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002715 /* don't even try to send a GOAWAY, the buffer is stuck */
2716 h2c->flags |= H2_CF_GOAWAY_FAILED;
2717 }
2718
2719 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002720 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002721 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2722 h2c->flags |= H2_CF_GOAWAY_FAILED;
2723
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002724 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2725 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002726 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002727 b_del(&h2c->mbuf, ret);
2728 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002729 }
2730 }
Willy Tarreauea392822017-10-31 10:02:25 +01002731
Willy Tarreau0975f112018-03-29 15:22:59 +02002732 /* either we can release everything now or it will be done later once
2733 * the last stream closes.
2734 */
2735 if (eb_is_empty(&h2c->streams_by_id))
2736 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002737
Willy Tarreauea392822017-10-31 10:02:25 +01002738 return NULL;
2739}
2740
2741
Willy Tarreau62f52692017-10-08 23:01:42 +02002742/*******************************************/
2743/* functions below are used by the streams */
2744/*******************************************/
2745
2746/*
2747 * Attach a new stream to a connection
2748 * (Used for outgoing connections)
2749 */
2750static struct conn_stream *h2_attach(struct connection *conn)
2751{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002752 struct conn_stream *cs;
2753 struct h2s *h2s;
2754 struct h2c *h2c = conn->mux_ctx;
2755
2756 cs = cs_new(conn);
2757 if (!cs)
2758 return NULL;
2759 h2s = h2c_bck_stream_new(h2c, cs);
2760 if (!h2s) {
2761 cs_free(cs);
2762 return NULL;
2763 }
2764 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002765}
2766
Willy Tarreaufafd3982018-11-18 21:29:20 +01002767/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2768 * We have to scan because we may have some orphan streams. It might be
2769 * beneficial to scan backwards from the end to reduce the likeliness to find
2770 * orphans.
2771 */
2772static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2773{
2774 struct h2c *h2c = conn->mux_ctx;
2775 struct h2s *h2s;
2776 struct eb32_node *node;
2777
2778 node = eb32_first(&h2c->streams_by_id);
2779 while (node) {
2780 h2s = container_of(node, struct h2s, by_id);
2781 if (h2s->cs)
2782 return h2s->cs;
2783 node = eb32_next(node);
2784 }
2785 return NULL;
2786}
2787
Willy Tarreau62f52692017-10-08 23:01:42 +02002788/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002789 * Destroy the mux and the associated connection, if it is no longer used
2790 */
2791static void h2_destroy(struct connection *conn)
2792{
2793 struct h2c *h2c = conn->mux_ctx;
2794
2795 if (eb_is_empty(&h2c->streams_by_id))
2796 h2_release(h2c->conn);
2797}
2798
2799/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002800 * Detach the stream from the connection and possibly release the connection.
2801 */
2802static void h2_detach(struct conn_stream *cs)
2803{
Willy Tarreau60935142017-10-16 18:11:19 +02002804 struct h2s *h2s = cs->ctx;
2805 struct h2c *h2c;
2806
2807 cs->ctx = NULL;
2808 if (!h2s)
2809 return;
2810
2811 h2c = h2s->h2c;
Olivier Houchard44d59142018-12-13 18:46:22 +01002812 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
2813 struct stream_interface *si;
2814 struct stream *s;
2815
2816 si = cs->data;
2817 s = si_strm(si);
2818 if (!(h2c->conn->flags &
2819 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2820 if (!h2c->conn->owner) {
2821 h2c->conn->owner = s->sess;
2822 session_add_conn(s->sess, h2c->conn, s->target);
2823 }
2824 /* Never ever allow to reuse a connection from a non-reuse backend */
2825 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2826 h2c->conn->flags |= CO_FL_PRIVATE;
2827 if (LIST_ISEMPTY(&h2c->conn->list)) {
2828 struct server *srv = objt_server(h2c->conn->target);
2829
2830 if (srv) {
2831 if (h2c->conn->flags & CO_FL_PRIVATE)
2832 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2833 else
2834 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2835 }
2836
2837 }
2838 }
2839 }
Willy Tarreau60935142017-10-16 18:11:19 +02002840 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002841 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002842 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2843 !h2_has_too_many_cs(h2c)) {
2844 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002845 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002846 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002847 }
Willy Tarreau60935142017-10-16 18:11:19 +02002848
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002849 /* this stream may be blocked waiting for some data to leave (possibly
2850 * an ES or RST frame), so orphan it in this case.
2851 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002852 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002853 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002854 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002855 return;
2856
Willy Tarreau45f752e2017-10-30 15:44:59 +01002857 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2858 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2859 /* unblock the connection if it was blocked on this
2860 * stream.
2861 */
2862 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2863 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002864 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002865 }
2866
Willy Tarreau71049cc2018-03-28 13:56:39 +02002867 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002868
Willy Tarreaue323f342018-03-28 13:51:45 +02002869 /* We don't want to close right now unless we're removing the
2870 * last stream, and either the connection is in error, or it
2871 * reached the ID already specified in a GOAWAY frame received
2872 * or sent (as seen by last_sid >= 0).
2873 */
2874 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2875 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002876 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002877 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002878 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002879 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002880 (conn_xprt_read0_pending(h2c->conn) ||
2881 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2882 /* no more stream will come, kill it now */
2883 h2_release(h2c->conn);
2884 }
2885 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002886 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002887 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2888 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002889 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002890 else
2891 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002892 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002893}
2894
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002895static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002896{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002897 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002898 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002899
Willy Tarreau721c9742017-11-07 11:05:42 +01002900 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002901 return;
2902
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002903 /* if no outgoing data was seen on this stream, it means it was
2904 * closed with a "tcp-request content" rule that is normally
2905 * used to kill the connection ASAP (eg: limit abuse). In this
2906 * case we send a goaway to close the connection.
2907 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002908 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002909 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002910 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002911
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002912 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2913 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002914 h2c_send_goaway_error(h2c, h2s) <= 0)
2915 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002916
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002917 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2918 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002919 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002920
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002921 return;
2922add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002923 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002924 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002925 if (h2s->flags & H2_SF_BLK_MFCTL) {
2926 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2927 h2s->send_wait = sw;
2928 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2929 h2s->send_wait = sw;
2930 LIST_ADDQ(&h2c->send_list, &h2s->list);
2931 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002932 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002933 /* Let the handler know we want shutr */
2934 sw->handle = (void *)((long)sw->handle | 1);
2935
Willy Tarreau62f52692017-10-08 23:01:42 +02002936}
2937
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002938static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002939{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002940 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002941 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002942
Willy Tarreau721c9742017-11-07 11:05:42 +01002943 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002944 return;
2945
Willy Tarreau67434202017-11-06 20:20:51 +01002946 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002947 /* we can cleanly close using an empty data frame only after headers */
2948
2949 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2950 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002951 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002952
2953 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002954 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002955 else
2956 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002957 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002958 /* if no outgoing data was seen on this stream, it means it was
2959 * closed with a "tcp-request content" rule that is normally
2960 * used to kill the connection ASAP (eg: limit abuse). In this
2961 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002962 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002963 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002964 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002965 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002966
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002967 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2968 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002969 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002970 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002971
Willy Tarreau00dd0782018-03-01 16:31:34 +01002972 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002973 }
2974
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002975 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2976 tasklet_wakeup(h2c->wait_event.task);
2977 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002978
2979 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002980 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002981 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002982 if (h2s->flags & H2_SF_BLK_MFCTL) {
2983 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2984 h2s->send_wait = sw;
2985 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2986 h2s->send_wait = sw;
2987 LIST_ADDQ(&h2c->send_list, &h2s->list);
2988 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002989 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002990 /* let the handler know we want to shutw */
2991 sw->handle = (void *)((long)(sw->handle) | 2);
2992
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002993}
2994
2995static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2996{
2997 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002998 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002999
Olivier Houchard2c68a462018-12-15 22:42:20 +01003000 if (h2s->send_wait) {
3001 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3002 h2s->send_wait = NULL;
3003 LIST_DEL(&h2s->list);
3004 LIST_INIT(&h2s->list);
3005 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003006 if (reason & 2)
3007 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003008 if (reason & 1)
3009 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003010
Olivier Houchard2c68a462018-12-15 22:42:20 +01003011 if (h2s->st == H2_SS_CLOSED &&
3012 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))))
3013 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003014 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003015}
3016
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003017static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3018{
3019 struct h2s *h2s = cs->ctx;
3020
3021 if (!mode)
3022 return;
3023
3024 h2_do_shutr(h2s);
3025}
3026
3027static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3028{
3029 struct h2s *h2s = cs->ctx;
3030
3031 h2_do_shutw(h2s);
3032}
3033
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003034/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003035 * HTX request or response depending on the connection's side. Returns the
3036 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3037 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003038 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003039static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003040{
3041 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003042 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003043 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003044 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003045 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003046 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003047 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003048 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003049 int flen = h2c->dfl;
3050 int outlen = 0;
3051 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003052 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003053
3054 if (!h2c->dfl) {
3055 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003056 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003057 return 0;
3058 }
3059
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003060 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003061 return 0; // incomplete input frame
3062
Willy Tarreau13278b42017-10-13 19:23:14 +02003063 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003064 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003065 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003066 copy = alloc_trash_chunk();
3067 if (!copy) {
3068 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3069 goto fail;
3070 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003071 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3072 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3073 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003074 }
3075
3076 /* The padlen is the first byte before data, and the padding appears
3077 * after data. padlen+data+padding are included in flen.
3078 */
3079 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003080 h2c->dpl = *hdrs;
3081 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003082 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3083 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003084 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003085 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003086 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003087 hdrs += 1; // skip Pad Length
3088 }
3089
3090 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3091 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003092 if (read_n32(hdrs) == h2s->id) {
3093 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3094 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003095 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003096 }
3097
Willy Tarreau13278b42017-10-13 19:23:14 +02003098 hdrs += 5; // stream dep = 4, weight = 1
3099 flen -= 5;
3100 }
3101
3102 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3103 * don't support this for now and can't even decompress so we have to
3104 * break the connection.
3105 */
3106 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3107 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003108 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003109 }
3110
Olivier Houchard638b7992018-08-16 15:41:52 +02003111 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003112 if (!csbuf) {
3113 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003114 goto fail;
3115 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003116
Willy Tarreau937f7602018-02-26 15:22:17 +01003117 /* we can't retry a failed decompression operation so we must be very
3118 * careful not to take any risks. In practice the output buffer is
3119 * always empty except maybe for trailers, in which case we simply have
3120 * to wait for the upper layer to finish consuming what is available.
3121 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003122
3123 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3124 htx = htx_from_buf(&h2s->rxbuf);
3125 if (!htx_is_empty(htx))
3126 goto fail;
3127 } else {
3128 if (b_data(csbuf))
3129 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003130
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003131 csbuf->head = 0;
3132 try = b_size(csbuf);
3133 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003134
3135 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3136 sizeof(list)/sizeof(list[0]), tmp);
3137 if (outlen < 0) {
3138 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3139 goto fail;
3140 }
3141
3142 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003143 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003144
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003145 if (htx) {
3146 /* HTX mode */
3147 if (h2c->flags & H2_CF_IS_BACK)
3148 outlen = h2_make_htx_response(list, htx, &msgf);
3149 else
3150 outlen = h2_make_htx_request(list, htx, &msgf);
3151 } else {
3152 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003153 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003154 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003155
3156 if (outlen < 0) {
3157 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3158 goto fail;
3159 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003160
Willy Tarreau174b06a2018-04-25 18:13:58 +02003161 if (msgf & H2_MSGF_BODY) {
3162 /* a payload is present */
3163 if (msgf & H2_MSGF_BODY_CL)
3164 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003165 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003166 h2s->flags |= H2_SF_DATA_CHNK;
3167 }
3168
Willy Tarreau13278b42017-10-13 19:23:14 +02003169 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003170 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003171 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003172 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003173
Willy Tarreau39d68502018-03-02 12:26:37 +01003174 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003175 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003176 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003177 if (htx)
3178 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003179 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003180
Willy Tarreau68dd9852017-07-03 14:44:26 +02003181 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003182 if (htx)
3183 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003184 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003185 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003186 fail:
3187 outlen = 0;
3188 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003189}
3190
Willy Tarreau454f9052017-10-26 19:40:35 +02003191/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3192 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3193 * in use, a new chunk is emitted for each frame. This is supposed to fit
3194 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3195 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3196 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003197 * parser state is automatically updated. Returns > 0 if it could completely
3198 * send the current frame, 0 if it couldn't complete, in which case
3199 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3200 * DATA frame can return 0 as a valid result). Stream errors are reported in
3201 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3202 * have checked the frame header and ensured that the frame was complete or the
3203 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003204 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003205static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003206{
3207 struct h2c *h2c = h2s->h2c;
3208 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003209 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003210 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003211 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003212 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003213
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003214 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003215
3216 /* The padlen is the first byte before data, and the padding appears
3217 * after data. padlen+data+padding are included in flen.
3218 */
Willy Tarreau79127812017-12-03 21:06:59 +01003219 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003220 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003221 return 0;
3222
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003223 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003224 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003225 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3226 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003227 return 0;
3228 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003229
3230 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003231 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003232 h2c->dfl--;
3233 h2c->rcvd_c++; h2c->rcvd_s++;
3234 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003235 }
3236
Olivier Houchard638b7992018-08-16 15:41:52 +02003237 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003238 if (!csbuf) {
3239 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003240 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003241 }
3242
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003243try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003244 flen = h2c->dfl - h2c->dpl;
3245 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003246 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003247
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003248 if (flen > b_data(&h2c->dbuf)) {
3249 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003250 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003251 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003252 }
3253
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003254 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3255 htx = htx_from_buf(csbuf);
3256 block1 = htx_free_data_space(htx);
3257 if (!block1) {
3258 h2c->flags |= H2_CF_DEM_SFULL;
3259 goto fail;
3260 }
3261 if (flen > block1)
3262 flen = block1;
3263
3264 /* here, flen is the max we can copy into the output buffer */
3265 block1 = b_contig_data(&h2c->dbuf, 0);
3266 if (flen > block1)
3267 flen = block1;
3268
3269 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3270 h2c->flags |= H2_CF_DEM_SFULL;
3271 goto fail;
3272 }
3273
3274 b_del(&h2c->dbuf, flen);
3275 h2c->dfl -= flen;
3276 h2c->rcvd_c += flen;
3277 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3278 goto try_again;
3279 }
3280 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003281 /* it doesn't fit and the buffer is fragmented,
3282 * so let's defragment it and try again.
3283 */
3284 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003285 }
3286
Willy Tarreaueba10f22018-04-25 20:44:22 +02003287 /* chunked-encoding requires more room */
3288 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003289 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003290 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3291 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3292 (chklen < 1048576) ? 4 : 8;
3293 chklen += 4; // CRLF, CRLF
3294 }
3295
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003296 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003297 if (flen + chklen > b_room(csbuf)) {
3298 if (chklen >= b_room(csbuf)) {
3299 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003300 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003301 }
3302 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003303 }
3304
3305 if (h2s->flags & H2_SF_DATA_CHNK) {
3306 /* emit the chunk size */
3307 unsigned int chksz = flen;
3308 char str[10];
3309 char *beg;
3310
3311 beg = str + sizeof(str);
3312 *--beg = '\n';
3313 *--beg = '\r';
3314 do {
3315 *--beg = hextab[chksz & 0xF];
3316 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003317 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003318 }
3319
Willy Tarreau454f9052017-10-26 19:40:35 +02003320 /* Block1 is the length of the first block before the buffer wraps,
3321 * block2 is the optional second block to reach the end of the frame.
3322 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003323 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003324 if (block1 > flen)
3325 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003326 block2 = flen - block1;
3327
3328 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003329 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003330
3331 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003332 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003333
Willy Tarreaueba10f22018-04-25 20:44:22 +02003334 if (h2s->flags & H2_SF_DATA_CHNK) {
3335 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003336 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003337 }
3338
Willy Tarreau454f9052017-10-26 19:40:35 +02003339 /* now mark the input data as consumed (will be deleted from the buffer
3340 * by the caller when seeing FRAME_A after sending the window update).
3341 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003342 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003343 h2c->dfl -= flen;
3344 h2c->rcvd_c += flen;
3345 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3346
3347 if (h2c->dfl > h2c->dpl) {
3348 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003349 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003350 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003351 }
3352
Willy Tarreau4a28da12018-01-04 14:41:00 +01003353 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003354 /* here we're done with the frame, all the payload (except padding) was
3355 * transferred.
3356 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003357
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003358 if (h2c->dff & H2_F_DATA_END_STREAM) {
3359 if (htx) {
3360 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3361 h2c->flags |= H2_CF_DEM_SFULL;
3362 goto fail;
3363 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003364 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003365 else if (h2s->flags & H2_SF_DATA_CHNK) {
3366 /* emit the trailing 0 CRLF CRLF */
3367 if (b_room(csbuf) < 5) {
3368 h2c->flags |= H2_CF_DEM_SFULL;
3369 goto fail;
3370 }
3371 chklen += 5;
3372 b_putblk(csbuf, "0\r\n\r\n", 5);
3373 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003374 }
3375
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003376 h2c->rcvd_c += h2c->dpl;
3377 h2c->rcvd_s += h2c->dpl;
3378 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003379 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3380
Willy Tarreau39d68502018-03-02 12:26:37 +01003381 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003382 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003383 h2s->cs->flags |= CS_FL_REOS;
3384 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003385 if (htx)
3386 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003387 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003388 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003389 if (htx)
3390 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003391 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003392}
3393
Willy Tarreau5dd17352018-06-14 13:33:30 +02003394/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3395 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3396 * number of bytes sent. The caller must check the stream's status to detect
3397 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003398 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003399static 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 +02003400{
3401 struct http_hdr list[MAX_HTTP_HDR];
3402 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003403 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003404 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003405 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003406 int es_now = 0;
3407 int ret = 0;
3408 int hdr;
3409
3410 if (h2c_mux_busy(h2c, h2s)) {
3411 h2s->flags |= H2_SF_BLK_MBUSY;
3412 return 0;
3413 }
3414
Willy Tarreau44e973f2018-03-01 17:49:30 +01003415 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003416 h2c->flags |= H2_CF_MUX_MALLOC;
3417 h2s->flags |= H2_SF_BLK_MROOM;
3418 return 0;
3419 }
3420
3421 /* First, try to parse the H1 response and index it into <list>.
3422 * NOTE! Since it comes from haproxy, we *know* that a response header
3423 * block does not wrap and we can safely read it this way without
3424 * having to realign the buffer.
3425 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003426 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003427 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003428 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003429 /* incomplete or invalid response, this is abnormal coming from
3430 * haproxy and may only result in a bad errorfile or bad Lua code
3431 * so that won't be fixed, raise an error now.
3432 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003433 * FIXME: we should instead add the ability to only return a
3434 * 502 bad gateway. But in theory this is not supposed to
3435 * happen.
3436 */
3437 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3438 ret = 0;
3439 goto end;
3440 }
3441
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003442 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003443
3444 /* certain statuses have no body or an empty one, regardless of
3445 * what the headers say.
3446 */
3447 if (sl.st.status >= 100 && sl.st.status < 200) {
3448 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3449 h1m->curr_len = h1m->body_len = 0;
3450 }
3451 else if (sl.st.status == 204 || sl.st.status == 304) {
3452 /* no contents, claim c-len is present and set to zero */
3453 h1m->flags &= ~H1_MF_CHNK;
3454 h1m->flags |= H1_MF_CLEN;
3455 h1m->curr_len = h1m->body_len = 0;
3456 }
3457
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003458 chunk_reset(&outbuf);
3459
3460 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003462 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003463 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003464
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003465 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003466 break;
3467 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003468 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003469 }
3470
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003471 if (outbuf.size < 9)
3472 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003473
3474 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003475 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3476 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3477 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003478
3479 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003480 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003481 /* this is an unparsable response */
3482 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3483 ret = 0;
3484 goto end;
3485 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003486
3487 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003488 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003489 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003490 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003491 }
3492
3493 /* encode all headers, stop at empty name */
3494 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003495 /* these ones do not exist in H2 and must be dropped. */
3496 if (isteq(list[hdr].n, ist("connection")) ||
3497 isteq(list[hdr].n, ist("proxy-connection")) ||
3498 isteq(list[hdr].n, ist("keep-alive")) ||
3499 isteq(list[hdr].n, ist("upgrade")) ||
3500 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003501 continue;
3502
3503 if (isteq(list[hdr].n, ist("")))
3504 break; // end
3505
3506 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3507 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003508 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003509 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003510 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003511 }
3512 }
3513
3514 /* we may need to add END_STREAM */
3515 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3516 es_now = 1;
3517
3518 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003519 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003520
3521 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003522 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003523
3524 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003525 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003526
3527 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003528 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003529 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003530
3531 /* for now we don't implemented CONTINUATION, so we wait for a
3532 * body or directly end in TRL2.
3533 */
3534 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003535 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003536 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003537
Willy Tarreau801250e2018-09-11 11:45:04 +02003538 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003539 h2s->flags |= H2_SF_ES_SENT;
3540 if (h2s->st == H2_SS_OPEN)
3541 h2s->st = H2_SS_HLOC;
3542 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003543 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003544 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003545 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003546 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003547 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003548 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003549 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003550 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003551 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003552
3553 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003554
3555 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003556 //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 +02003557 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003558 full:
3559 h1m_init_res(h1m);
3560 h1m->err_pos = -1; // don't care about errors on the response path
3561 h2c->flags |= H2_CF_MUX_MFULL;
3562 h2s->flags |= H2_SF_BLK_MROOM;
3563 ret = 0;
3564 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003565}
3566
Willy Tarreau5dd17352018-06-14 13:33:30 +02003567/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3568 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3569 * the number of bytes sent. The caller must check the stream's status to
3570 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003571 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003572static 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 +02003573{
3574 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003575 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003576 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003577 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003578 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003579 int es_now = 0;
3580 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003581 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003582 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003583
3584 if (h2c_mux_busy(h2c, h2s)) {
3585 h2s->flags |= H2_SF_BLK_MBUSY;
3586 goto end;
3587 }
3588
Willy Tarreau44e973f2018-03-01 17:49:30 +01003589 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003590 h2c->flags |= H2_CF_MUX_MALLOC;
3591 h2s->flags |= H2_SF_BLK_MROOM;
3592 goto end;
3593 }
3594
3595 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003596 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003597 goto end;
3598
3599 chunk_reset(&outbuf);
3600
3601 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003602 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003603 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003604 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003605
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003606 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003607 break;
3608 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003609 /* If there are pending data in the output buffer, and we have
3610 * less than 1/4 of the mbuf's size and everything fits, we'll
3611 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3612 * is full and wait, to save some slow realign calls.
3613 */
3614 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3615 h2c->flags |= H2_CF_MUX_MFULL;
3616 h2s->flags |= H2_SF_BLK_MROOM;
3617 goto end;
3618 }
3619
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003620 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003621 }
3622
3623 if (outbuf.size < 9) {
3624 h2c->flags |= H2_CF_MUX_MFULL;
3625 h2s->flags |= H2_SF_BLK_MROOM;
3626 goto end;
3627 }
3628
3629 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003630 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3631 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3632 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003633
3634 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3635 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003636 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003637 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003638 break;
3639 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003640 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003641 if ((long long)size > h1m->curr_len)
3642 size = h1m->curr_len;
3643 break;
3644 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003645 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003646 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003647 if (!ret)
3648 goto end;
3649
3650 if (ret < 0) {
3651 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003652 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003653 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3654 goto end;
3655 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003656 max -= ret;
3657 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003658 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003659 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003660 }
3661
Willy Tarreau801250e2018-09-11 11:45:04 +02003662 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003663 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003664 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003665 if (!ret)
3666 goto end;
3667
3668 if (ret < 0) {
3669 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003670 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003671 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3672 goto end;
3673 }
3674
3675 size = chunk;
3676 h1m->curr_len = chunk;
3677 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003678 max -= ret;
3679 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003680 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003681 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003682 if (!size)
3683 goto send_empty;
3684 }
3685
3686 /* in MSG_DATA state, continue below */
3687 size = h1m->curr_len;
3688 break;
3689 }
3690
3691 /* we have in <size> the exact number of bytes we need to copy from
3692 * the H1 buffer. We need to check this against the connection's and
3693 * the stream's send windows, and to ensure that this fits in the max
3694 * frame size and in the buffer's available space minus 9 bytes (for
3695 * the frame header). The connection's flow control is applied last so
3696 * that we can use a separate list of streams which are immediately
3697 * unblocked on window opening. Note: we don't implement padding.
3698 */
3699
Willy Tarreau5dd17352018-06-14 13:33:30 +02003700 if (size > max)
3701 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003702
3703 if (size > h2s->mws)
3704 size = h2s->mws;
3705
3706 if (size <= 0) {
3707 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003708 if (h2s->send_wait) {
3709 LIST_DEL(&h2s->list);
3710 LIST_INIT(&h2s->list);
3711 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003712 goto end;
3713 }
3714
3715 if (h2c->mfs && size > h2c->mfs)
3716 size = h2c->mfs;
3717
3718 if (size + 9 > outbuf.size) {
3719 /* we have an opportunity for enlarging the too small
3720 * available space, let's try.
3721 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003722 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003723 goto realign_again;
3724 size = outbuf.size - 9;
3725 }
3726
3727 if (size <= 0) {
3728 h2c->flags |= H2_CF_MUX_MFULL;
3729 h2s->flags |= H2_SF_BLK_MROOM;
3730 goto end;
3731 }
3732
3733 if (size > h2c->mws)
3734 size = h2c->mws;
3735
3736 if (size <= 0) {
3737 h2s->flags |= H2_SF_BLK_MFCTL;
3738 goto end;
3739 }
3740
3741 /* copy whatever we can */
3742 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003743 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003744 if (ret == 1)
3745 len2 = 0;
3746
3747 if (!ret || len1 + len2 < size) {
3748 /* FIXME: must normally never happen */
3749 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3750 goto end;
3751 }
3752
3753 /* limit len1/len2 to size */
3754 if (len1 + len2 > size) {
3755 int sub = len1 + len2 - size;
3756
3757 if (len2 > sub)
3758 len2 -= sub;
3759 else {
3760 sub -= len2;
3761 len2 = 0;
3762 len1 -= sub;
3763 }
3764 }
3765
3766 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003767 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003768 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003769 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003770
3771 send_empty:
3772 /* we may need to add END_STREAM */
3773 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3774 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003775 *
3776 * FIXME: what we do here is not correct because we send end_stream
3777 * before knowing if we'll have to send a HEADERS frame for the
3778 * trailers. More importantly we're not consuming the trailing CRLF
3779 * after the end of trailers, so it will be left to the caller to
3780 * eat it. The right way to do it would be to measure trailers here
3781 * and to send ES only if there are no trailers.
3782 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003783 */
3784 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003785 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003786 es_now = 1;
3787
3788 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003789 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003790
3791 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003792 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003793
3794 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003795 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003796
3797 /* consume incoming H1 response */
3798 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003799 max -= size;
3800 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003801 total += size;
3802 h1m->curr_len -= size;
3803 h2s->mws -= size;
3804 h2c->mws -= size;
3805
3806 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003807 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003808 goto new_frame;
3809 }
3810 }
3811
3812 if (es_now) {
3813 if (h2s->st == H2_SS_OPEN)
3814 h2s->st = H2_SS_HLOC;
3815 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003816 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003817
Willy Tarreau35a62702018-02-27 15:37:25 +01003818 if (!(h1m->flags & H1_MF_CHNK)) {
3819 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003820 total += max;
3821 ofs += max;
3822 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003823
Willy Tarreau801250e2018-09-11 11:45:04 +02003824 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003825 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003826
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003827 h2s->flags |= H2_SF_ES_SENT;
3828 }
3829
3830 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003831 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 +02003832 return total;
3833}
3834
Willy Tarreau115e83b2018-12-01 19:17:53 +01003835/* Try to send a HEADERS frame matching HTX response present in HTX message
3836 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3837 * must check the stream's status to detect any error which might have happened
3838 * subsequently to a successful send. The htx blocks are automatically removed
3839 * from the message. The htx message is assumed to be valid since produced from
3840 * the internal code, hence it contains a start line, an optional series of
3841 * header blocks and an end of header, otherwise an invalid frame could be
3842 * emitted and the resulting htx message could be left in an inconsistent state.
3843 */
3844static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3845{
3846 struct http_hdr list[MAX_HTTP_HDR];
3847 struct h2c *h2c = h2s->h2c;
3848 struct htx_blk *blk;
3849 struct htx_blk *blk_end;
3850 struct buffer outbuf;
3851 struct htx_sl *sl;
3852 enum htx_blk_type type;
3853 int es_now = 0;
3854 int ret = 0;
3855 int hdr;
3856 int idx;
3857
3858 if (h2c_mux_busy(h2c, h2s)) {
3859 h2s->flags |= H2_SF_BLK_MBUSY;
3860 return 0;
3861 }
3862
3863 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3864 h2c->flags |= H2_CF_MUX_MALLOC;
3865 h2s->flags |= H2_SF_BLK_MROOM;
3866 return 0;
3867 }
3868
3869 /* determine the first block which must not be deleted, blk_end may
3870 * be NULL if all blocks have to be deleted.
3871 */
3872 idx = htx_get_head(htx);
3873 blk_end = NULL;
3874 while (idx != -1) {
3875 type = htx_get_blk_type(htx_get_blk(htx, idx));
3876 idx = htx_get_next(htx, idx);
3877 if (type == HTX_BLK_EOH) {
3878 if (idx != -1)
3879 blk_end = htx_get_blk(htx, idx);
3880 break;
3881 }
3882 }
3883
3884 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003885 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003886 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003887 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003888 if (h2s->status < 100 || h2s->status > 999)
3889 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003890
3891 /* and the rest of the headers, that we dump starting at header 0 */
3892 hdr = 0;
3893
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003894 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003895 while ((idx = htx_get_next(htx, idx)) != -1) {
3896 blk = htx_get_blk(htx, idx);
3897 type = htx_get_blk_type(blk);
3898
3899 if (type == HTX_BLK_UNUSED)
3900 continue;
3901
3902 if (type != HTX_BLK_HDR)
3903 break;
3904
3905 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3906 goto fail;
3907
3908 list[hdr].n = htx_get_blk_name(htx, blk);
3909 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003910 hdr++;
3911 }
3912
3913 /* marker for end of headers */
3914 list[hdr].n = ist("");
3915
3916 if (h2s->status == 204 || h2s->status == 304) {
3917 /* no contents, claim c-len is present and set to zero */
3918 es_now = 1;
3919 }
3920
3921 chunk_reset(&outbuf);
3922
3923 while (1) {
3924 outbuf.area = b_tail(&h2c->mbuf);
3925 outbuf.size = b_contig_space(&h2c->mbuf);
3926 outbuf.data = 0;
3927
3928 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3929 break;
3930 realign_again:
3931 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3932 }
3933
3934 if (outbuf.size < 9)
3935 goto full;
3936
3937 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3938 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3939 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3940 outbuf.data = 9;
3941
3942 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003943 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003944 if (b_space_wraps(&h2c->mbuf))
3945 goto realign_again;
3946 goto full;
3947 }
3948
3949 /* encode all headers, stop at empty name */
3950 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3951 /* these ones do not exist in H2 and must be dropped. */
3952 if (isteq(list[hdr].n, ist("connection")) ||
3953 isteq(list[hdr].n, ist("proxy-connection")) ||
3954 isteq(list[hdr].n, ist("keep-alive")) ||
3955 isteq(list[hdr].n, ist("upgrade")) ||
3956 isteq(list[hdr].n, ist("transfer-encoding")))
3957 continue;
3958
3959 if (isteq(list[hdr].n, ist("")))
3960 break; // end
3961
3962 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3963 /* output full */
3964 if (b_space_wraps(&h2c->mbuf))
3965 goto realign_again;
3966 goto full;
3967 }
3968 }
3969
3970 /* we may need to add END_STREAM.
3971 * FIXME: we should also set it when we know for sure that the
3972 * content-length is zero as well as on 204/304
3973 */
3974 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3975 es_now = 1;
3976
3977 if (h2s->cs->flags & CS_FL_SHW)
3978 es_now = 1;
3979
3980 /* update the frame's size */
3981 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3982
3983 if (es_now)
3984 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3985
3986 /* commit the H2 response */
3987 b_add(&h2c->mbuf, outbuf.data);
3988 h2s->flags |= H2_SF_HEADERS_SENT;
3989
3990 /* for now we don't implemented CONTINUATION, so we wait for a
3991 * body or directly end in TRL2.
3992 */
3993 if (es_now) {
3994 h2s->flags |= H2_SF_ES_SENT;
3995 if (h2s->st == H2_SS_OPEN)
3996 h2s->st = H2_SS_HLOC;
3997 else
3998 h2s_close(h2s);
3999 }
4000
4001 /* OK we could properly deliver the response */
4002
4003 /* remove all header blocks including the EOH and compute the
4004 * corresponding size.
4005 *
4006 * FIXME: We should remove everything when es_now is set.
4007 */
4008 ret = 0;
4009 idx = htx_get_head(htx);
4010 blk = htx_get_blk(htx, idx);
4011 while (blk != blk_end) {
4012 ret += htx_get_blksz(blk);
4013 blk = htx_remove_blk(htx, blk);
4014 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004015
4016 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4017 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004018 end:
4019 return ret;
4020 full:
4021 h2c->flags |= H2_CF_MUX_MFULL;
4022 h2s->flags |= H2_SF_BLK_MROOM;
4023 ret = 0;
4024 goto end;
4025 fail:
4026 /* unparsable HTX messages, too large ones to be produced in the local
4027 * list etc go here (unrecoverable errors).
4028 */
4029 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4030 ret = 0;
4031 goto end;
4032}
4033
Willy Tarreau80739692018-10-05 11:35:57 +02004034/* Try to send a HEADERS frame matching HTX request present in HTX message
4035 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4036 * must check the stream's status to detect any error which might have happened
4037 * subsequently to a successful send. The htx blocks are automatically removed
4038 * from the message. The htx message is assumed to be valid since produced from
4039 * the internal code, hence it contains a start line, an optional series of
4040 * header blocks and an end of header, otherwise an invalid frame could be
4041 * emitted and the resulting htx message could be left in an inconsistent state.
4042 */
4043static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4044{
4045 struct http_hdr list[MAX_HTTP_HDR];
4046 struct h2c *h2c = h2s->h2c;
4047 struct htx_blk *blk;
4048 struct htx_blk *blk_end;
4049 struct buffer outbuf;
4050 struct htx_sl *sl;
4051 struct ist meth, path;
4052 enum htx_blk_type type;
4053 int es_now = 0;
4054 int ret = 0;
4055 int hdr;
4056 int idx;
4057
4058 if (h2c_mux_busy(h2c, h2s)) {
4059 h2s->flags |= H2_SF_BLK_MBUSY;
4060 return 0;
4061 }
4062
4063 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4064 h2c->flags |= H2_CF_MUX_MALLOC;
4065 h2s->flags |= H2_SF_BLK_MROOM;
4066 return 0;
4067 }
4068
4069 /* determine the first block which must not be deleted, blk_end may
4070 * be NULL if all blocks have to be deleted.
4071 */
4072 idx = htx_get_head(htx);
4073 blk_end = NULL;
4074 while (idx != -1) {
4075 type = htx_get_blk_type(htx_get_blk(htx, idx));
4076 idx = htx_get_next(htx, idx);
4077 if (type == HTX_BLK_EOH) {
4078 if (idx != -1)
4079 blk_end = htx_get_blk(htx, idx);
4080 break;
4081 }
4082 }
4083
4084 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004085 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004086 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004087 meth = htx_sl_req_meth(sl);
4088 path = htx_sl_req_uri(sl);
4089
4090 /* and the rest of the headers, that we dump starting at header 0 */
4091 hdr = 0;
4092
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004093 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004094 while ((idx = htx_get_next(htx, idx)) != -1) {
4095 blk = htx_get_blk(htx, idx);
4096 type = htx_get_blk_type(blk);
4097
4098 if (type == HTX_BLK_UNUSED)
4099 continue;
4100
4101 if (type != HTX_BLK_HDR)
4102 break;
4103
4104 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4105 goto fail;
4106
4107 list[hdr].n = htx_get_blk_name(htx, blk);
4108 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004109 hdr++;
4110 }
4111
4112 /* marker for end of headers */
4113 list[hdr].n = ist("");
4114
4115 chunk_reset(&outbuf);
4116
4117 while (1) {
4118 outbuf.area = b_tail(&h2c->mbuf);
4119 outbuf.size = b_contig_space(&h2c->mbuf);
4120 outbuf.data = 0;
4121
4122 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4123 break;
4124 realign_again:
4125 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4126 }
4127
4128 if (outbuf.size < 9)
4129 goto full;
4130
4131 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4132 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4133 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4134 outbuf.data = 9;
4135
4136 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004137 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004138 if (b_space_wraps(&h2c->mbuf))
4139 goto realign_again;
4140 goto full;
4141 }
4142
4143 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004144 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4145 /* output full */
4146 if (b_space_wraps(&h2c->mbuf))
4147 goto realign_again;
4148 goto full;
4149 }
Willy Tarreau80739692018-10-05 11:35:57 +02004150
4151 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004152 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004153 /* output full */
4154 if (b_space_wraps(&h2c->mbuf))
4155 goto realign_again;
4156 goto full;
4157 }
4158
4159 /* encode all headers, stop at empty name */
4160 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4161 /* these ones do not exist in H2 and must be dropped. */
4162 if (isteq(list[hdr].n, ist("connection")) ||
4163 isteq(list[hdr].n, ist("proxy-connection")) ||
4164 isteq(list[hdr].n, ist("keep-alive")) ||
4165 isteq(list[hdr].n, ist("upgrade")) ||
4166 isteq(list[hdr].n, ist("transfer-encoding")))
4167 continue;
4168
4169 if (isteq(list[hdr].n, ist("")))
4170 break; // end
4171
4172 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4173 /* output full */
4174 if (b_space_wraps(&h2c->mbuf))
4175 goto realign_again;
4176 goto full;
4177 }
4178 }
4179
4180 /* we may need to add END_STREAM if we have no body :
4181 * - request already closed, or :
4182 * - no transfer-encoding, and :
4183 * - no content-length or content-length:0
4184 * Fixme: this doesn't take into account CONNECT requests.
4185 */
4186 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4187 es_now = 1;
4188
4189 if (sl->flags & HTX_SL_F_BODYLESS)
4190 es_now = 1;
4191
4192 if (h2s->cs->flags & CS_FL_SHW)
4193 es_now = 1;
4194
4195 /* update the frame's size */
4196 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4197
4198 if (es_now)
4199 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4200
4201 /* commit the H2 response */
4202 b_add(&h2c->mbuf, outbuf.data);
4203 h2s->flags |= H2_SF_HEADERS_SENT;
4204 h2s->st = H2_SS_OPEN;
4205
4206 /* for now we don't implemented CONTINUATION, so we wait for a
4207 * body or directly end in TRL2.
4208 */
4209 if (es_now) {
4210 // trim any possibly pending data (eg: inconsistent content-length)
4211 h2s->flags |= H2_SF_ES_SENT;
4212 h2s->st = H2_SS_HLOC;
4213 }
4214
4215 /* remove all header blocks including the EOH and compute the
4216 * corresponding size.
4217 *
4218 * FIXME: We should remove everything when es_now is set.
4219 */
4220 ret = 0;
4221 idx = htx_get_head(htx);
4222 blk = htx_get_blk(htx, idx);
4223 while (blk != blk_end) {
4224 ret += htx_get_blksz(blk);
4225 blk = htx_remove_blk(htx, blk);
4226 }
4227
4228 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4229 htx_remove_blk(htx, blk_end);
4230
4231 end:
4232 return ret;
4233 full:
4234 h2c->flags |= H2_CF_MUX_MFULL;
4235 h2s->flags |= H2_SF_BLK_MROOM;
4236 ret = 0;
4237 goto end;
4238 fail:
4239 /* unparsable HTX messages, too large ones to be produced in the local
4240 * list etc go here (unrecoverable errors).
4241 */
4242 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4243 ret = 0;
4244 goto end;
4245}
4246
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004247/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004248 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4249 * caller must check the stream's status to detect any error which might have
4250 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004251 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4252 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004253static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004254{
4255 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004256 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004257 struct buffer outbuf;
4258 size_t total = 0;
4259 int es_now = 0;
4260 int bsize; /* htx block size */
4261 int fsize; /* h2 frame size */
4262 struct htx_blk *blk;
4263 enum htx_blk_type type;
4264 int idx;
4265
4266 if (h2c_mux_busy(h2c, h2s)) {
4267 h2s->flags |= H2_SF_BLK_MBUSY;
4268 goto end;
4269 }
4270
4271 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4272 h2c->flags |= H2_CF_MUX_MALLOC;
4273 h2s->flags |= H2_SF_BLK_MROOM;
4274 goto end;
4275 }
4276
Willy Tarreau98de12a2018-12-12 07:03:00 +01004277 htx = htx_from_buf(buf);
4278
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004279 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4280 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4281 * the caller to handle.
4282 */
4283
4284 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004285 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004286 goto end;
4287
4288 idx = htx_get_head(htx);
4289 blk = htx_get_blk(htx, idx);
4290 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4291 bsize = htx_get_blksz(blk);
4292 fsize = bsize;
4293
4294 if (type == HTX_BLK_EOD) {
4295 /* if we have an EOD, we're dealing with chunked data. We may
4296 * have a set of trailers after us that the caller will want to
4297 * deal with. Let's simply remove the EOD and return.
4298 */
4299 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004300 total++; // EOD counts as one byte
4301 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004302 goto end;
4303 }
4304
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004305 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4306 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004307
4308 /* Perform some optimizations to reduce the number of buffer copies.
4309 * First, if the mux's buffer is empty and the htx area contains
4310 * exactly one data block of the same size as the requested count, and
4311 * this count fits within the frame size, the stream's window size, and
4312 * the connection's window size, then it's possible to simply swap the
4313 * caller's buffer with the mux's output buffer and adjust offsets and
4314 * length to match the entire DATA HTX block in the middle. In this
4315 * case we perform a true zero-copy operation from end-to-end. This is
4316 * the situation that happens all the time with large files. Second, if
4317 * this is not possible, but the mux's output buffer is empty, we still
4318 * have an opportunity to avoid the copy to the intermediary buffer, by
4319 * making the intermediary buffer's area point to the output buffer's
4320 * area. In this case we want to skip the HTX header to make sure that
4321 * copies remain aligned and that this operation remains possible all
4322 * the time. This goes for headers, data blocks and any data extracted
4323 * from the HTX blocks.
4324 */
4325 if (unlikely(fsize == count &&
4326 htx->used == 1 && type == HTX_BLK_DATA &&
4327 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4328 void *old_area = h2c->mbuf.area;
4329
4330 if (b_data(&h2c->mbuf)) {
4331 /* too bad there are data left there. If we have less
4332 * than 1/4 of the mbuf's size and everything fits,
4333 * we'll perform a copy anyway. Otherwise we'll pretend
4334 * the mbuf is full and wait.
4335 */
4336 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4337 goto copy;
4338 h2c->flags |= H2_CF_MUX_MFULL;
4339 h2s->flags |= H2_SF_BLK_MROOM;
4340 goto end;
4341 }
4342
4343 /* map an H2 frame to the HTX block so that we can put the
4344 * frame header there.
4345 */
4346 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004347 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004348 h2c->mbuf.data = fsize + 9;
4349 outbuf.area = b_head(&h2c->mbuf);
4350
4351 /* prepend an H2 DATA frame header just before the DATA block */
4352 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4353 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4354 h2_set_frame_size(outbuf.area, fsize);
4355
4356 /* update windows */
4357 h2s->mws -= fsize;
4358 h2c->mws -= fsize;
4359
4360 /* and exchange with our old area */
4361 buf->area = old_area;
4362 buf->data = buf->head = 0;
4363 total += fsize;
4364 goto end;
4365 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004366
Willy Tarreau98de12a2018-12-12 07:03:00 +01004367 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004368 /* for DATA and EOM we'll have to emit a frame, even if empty */
4369
4370 while (1) {
4371 outbuf.area = b_tail(&h2c->mbuf);
4372 outbuf.size = b_contig_space(&h2c->mbuf);
4373 outbuf.data = 0;
4374
4375 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4376 break;
4377 realign_again:
4378 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4379 }
4380
4381 if (outbuf.size < 9) {
4382 h2c->flags |= H2_CF_MUX_MFULL;
4383 h2s->flags |= H2_SF_BLK_MROOM;
4384 goto end;
4385 }
4386
4387 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4388 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4389 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4390 outbuf.data = 9;
4391
4392 /* we have in <fsize> the exact number of bytes we need to copy from
4393 * the HTX buffer. We need to check this against the connection's and
4394 * the stream's send windows, and to ensure that this fits in the max
4395 * frame size and in the buffer's available space minus 9 bytes (for
4396 * the frame header). The connection's flow control is applied last so
4397 * that we can use a separate list of streams which are immediately
4398 * unblocked on window opening. Note: we don't implement padding.
4399 */
4400
4401 /* EOM is presented with bsize==1 but would lead to the emission of an
4402 * empty frame, thus we force it to zero here.
4403 */
4404 if (type == HTX_BLK_EOM)
4405 bsize = fsize = 0;
4406
4407 if (!fsize)
4408 goto send_empty;
4409
4410 if (h2s->mws <= 0) {
4411 h2s->flags |= H2_SF_BLK_SFCTL;
4412 if (h2s->send_wait) {
4413 LIST_DEL(&h2s->list);
4414 LIST_INIT(&h2s->list);
4415 }
4416 goto end;
4417 }
4418
Willy Tarreauee573762018-12-04 15:25:57 +01004419 if (fsize > count)
4420 fsize = count;
4421
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004422 if (fsize > h2s->mws)
4423 fsize = h2s->mws; // >0
4424
4425 if (h2c->mfs && fsize > h2c->mfs)
4426 fsize = h2c->mfs; // >0
4427
4428 if (fsize + 9 > outbuf.size) {
4429 /* we have an opportunity for enlarging the too small
4430 * available space, let's try.
4431 * FIXME: is this really interesting to do? Maybe we'll
4432 * spend lots of time realigning instead of using two
4433 * frames.
4434 */
4435 if (b_space_wraps(&h2c->mbuf))
4436 goto realign_again;
4437 fsize = outbuf.size - 9;
4438
4439 if (fsize <= 0) {
4440 /* no need to send an empty frame here */
4441 h2c->flags |= H2_CF_MUX_MFULL;
4442 h2s->flags |= H2_SF_BLK_MROOM;
4443 goto end;
4444 }
4445 }
4446
4447 if (h2c->mws <= 0) {
4448 h2s->flags |= H2_SF_BLK_MFCTL;
4449 goto end;
4450 }
4451
4452 if (fsize > h2c->mws)
4453 fsize = h2c->mws;
4454
4455 /* now let's copy this this into the output buffer */
4456 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004457 h2s->mws -= fsize;
4458 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004459 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004460
4461 send_empty:
4462 /* update the frame's size */
4463 h2_set_frame_size(outbuf.area, fsize);
4464
4465 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4466 * meeting EOM. We should optimize this later.
4467 */
4468 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004469 total++; // EOM counts as one byte
4470 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004471 es_now = 1;
4472 }
4473
4474 if (es_now)
4475 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4476
4477 /* commit the H2 response */
4478 b_add(&h2c->mbuf, fsize + 9);
4479
4480 /* consume incoming HTX block, including EOM */
4481 total += fsize;
4482 if (fsize == bsize) {
4483 htx_remove_blk(htx, blk);
4484 if (fsize)
4485 goto new_frame;
4486 } else {
4487 /* we've truncated this block */
4488 htx_cut_data_blk(htx, blk, fsize);
4489 }
4490
4491 if (es_now) {
4492 if (h2s->st == H2_SS_OPEN)
4493 h2s->st = H2_SS_HLOC;
4494 else
4495 h2s_close(h2s);
4496
4497 h2s->flags |= H2_SF_ES_SENT;
4498 }
4499
4500 end:
4501 return total;
4502}
4503
Olivier Houchard6ff20392018-07-17 18:46:31 +02004504/* Called from the upper layer, to subscribe to events, such as being able to send */
4505static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4506{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004507 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004508 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004509 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004510
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004511 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004512 sw = param;
4513 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4514 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004515 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004516 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004517 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004518 event_type &= ~SUB_CAN_RECV;
4519 }
4520 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004521 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004522 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004523 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004524 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004525 h2s->send_wait = sw;
4526 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4527 if (h2s->flags & H2_SF_BLK_MFCTL)
4528 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4529 else
4530 LIST_ADDQ(&h2c->send_list, &h2s->list);
4531 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004532 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004533 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004534 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004535 if (event_type != 0)
4536 return -1;
4537 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004538
4539
4540}
4541
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004542static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4543{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004544 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004545 struct h2s *h2s = cs->ctx;
4546
4547 if (event_type & SUB_CAN_RECV) {
4548 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004549 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004550 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004551 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004552 }
4553 }
4554 if (event_type & SUB_CAN_SEND) {
4555 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004556 if (h2s->send_wait == sw) {
4557 LIST_DEL(&h2s->list);
4558 LIST_INIT(&h2s->list);
4559 sw->wait_reason &= ~SUB_CAN_SEND;
4560 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004561 }
4562 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004563 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4564 sw = param;
4565 if (h2s->send_wait == sw) {
4566 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4567 h2s->send_wait = NULL;
4568 }
4569 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004570 return 0;
4571}
4572
4573
Olivier Houchard511efea2018-08-16 15:30:32 +02004574/* Called from the upper layer, to receive data */
4575static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4576{
Olivier Houchard638b7992018-08-16 15:41:52 +02004577 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004578 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004579 struct htx *h2s_htx = NULL;
4580 struct htx *buf_htx = NULL;
4581 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004582 size_t ret = 0;
4583
4584 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004585 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4586 /* in HTX mode we ignore the count argument */
4587 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004588 if (htx_is_empty(h2s_htx)) {
4589 if (cs->flags & CS_FL_REOS)
4590 cs->flags |= CS_FL_EOS;
Willy Tarreau86724e22018-12-01 23:19:43 +01004591 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004592 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004593
4594 buf_htx = htx_from_buf(buf);
4595 count = htx_free_space(buf_htx);
4596
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004597 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004598
4599 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004600 htx_to_buf(buf_htx, buf);
4601 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004602 ret = htx_ret.ret;
4603 }
4604 else {
4605 ret = b_xfer(buf, &h2s->rxbuf, count);
4606 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004607
Olivier Houchard638b7992018-08-16 15:41:52 +02004608 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004609 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004610 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004611 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004612 if (cs->flags & CS_FL_REOS)
4613 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004614 if (b_size(&h2s->rxbuf)) {
4615 b_free(&h2s->rxbuf);
4616 offer_buffers(NULL, tasks_run_queue);
4617 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004618 }
4619
Willy Tarreau082f5592018-11-25 08:03:32 +01004620 if (ret && h2c->dsi == h2s->id) {
4621 /* demux is blocking on this stream's buffer */
4622 h2c->flags &= ~H2_CF_DEM_SFULL;
4623 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4624 if (h2_recv_allowed(h2c))
4625 tasklet_wakeup(h2c->wait_event.task);
4626 }
4627 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004628end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004629 return ret;
4630}
4631
Olivier Houchardd846c262018-10-19 17:24:29 +02004632static void h2_stop_senders(struct h2c *h2c)
4633{
4634 struct h2s *h2s, *h2s_back;
4635
4636 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4637 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4638 if (h2c->msi == h2s_id(h2s))
4639 continue;
4640 LIST_DEL(&h2s->list);
4641 LIST_INIT(&h2s->list);
4642 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4643 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4644 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4645 LIST_ADD(&h2c->send_list, &h2s->list);
4646 }
4647}
4648
Willy Tarreau62f52692017-10-08 23:01:42 +02004649/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004650static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004651{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004652 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004653 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004654 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004655 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004656 struct htx *htx;
4657 struct htx_blk *blk;
4658 enum htx_blk_type btype;
4659 uint32_t bsize;
4660 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004661
Olivier Houchardd846c262018-10-19 17:24:29 +02004662 if (h2s->send_wait) {
4663 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4664 h2s->send_wait = NULL;
4665 LIST_DEL(&h2s->list);
4666 LIST_INIT(&h2s->list);
4667 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004668 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4669 return 0;
4670
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004671 /* htx will be enough to decide if we're using HTX or legacy */
4672 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4673
Willy Tarreau0bad0432018-06-14 16:54:01 +02004674 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004675 h2s->flags |= H2_SF_OUTGOING_DATA;
4676
Willy Tarreau751f2d02018-10-05 09:35:00 +02004677 if (h2s->id == 0) {
4678 int32_t id = h2c_get_next_sid(h2s->h2c);
4679
4680 if (id < 0) {
4681 cs->ctx = NULL;
4682 cs->flags |= CS_FL_ERROR;
4683 h2s_destroy(h2s);
4684 return 0;
4685 }
4686
4687 eb32_delete(&h2s->by_id);
4688 h2s->by_id.key = h2s->id = id;
4689 h2s->h2c->max_id = id;
4690 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4691 }
4692
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004693 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004694 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4695 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004696 idx = htx_get_head(htx);
4697 blk = htx_get_blk(htx, idx);
4698 btype = htx_get_blk_type(blk);
4699 bsize = htx_get_blksz(blk);
4700
4701 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004702 case HTX_BLK_REQ_SL:
4703 /* start-line before headers */
4704 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4705 if (ret > 0) {
4706 total += ret;
4707 count -= ret;
4708 if (ret < bsize)
4709 goto done;
4710 }
4711 break;
4712
Willy Tarreau115e83b2018-12-01 19:17:53 +01004713 case HTX_BLK_RES_SL:
4714 /* start-line before headers */
4715 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4716 if (ret > 0) {
4717 total += ret;
4718 count -= ret;
4719 if (ret < bsize)
4720 goto done;
4721 }
4722 break;
4723
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004724 case HTX_BLK_DATA:
4725 case HTX_BLK_EOD:
4726 case HTX_BLK_EOM:
4727 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004728 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004729 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004730 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004731 total += ret;
4732 count -= ret;
4733 if (ret < bsize)
4734 goto done;
4735 }
4736 break;
4737
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004738 default:
4739 htx_remove_blk(htx, blk);
4740 total += bsize;
4741 count -= bsize;
4742 break;
4743 }
4744 }
4745 goto done;
4746 }
4747
4748 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004749 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004750 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004751 if (h2s->h2c->flags & H2_CF_IS_BACK)
4752 ret = -1;
4753 else
4754 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004755 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004756 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004757 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004758 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004759 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004760 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004761 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004762
Willy Tarreau5dd17352018-06-14 13:33:30 +02004763 if (unlikely((int)ret <= 0)) {
4764 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004765 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4766 break;
4767 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004768 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004769 total += count;
4770 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004771 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004772 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004773 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004774 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004775 cs->flags |= CS_FL_ERROR;
4776 break;
4777 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004778
4779 total += ret;
4780 count -= ret;
4781
4782 if (h2s->st >= H2_SS_ERROR)
4783 break;
4784
4785 if (h2s->flags & H2_SF_BLK_ANY)
4786 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004787 }
4788
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004789 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004790 if (h2s->st >= H2_SS_ERROR) {
4791 /* trim any possibly pending data after we close (extra CR-LF,
4792 * unprocessed trailers, abnormal extra data, ...)
4793 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004794 total += count;
4795 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004796 }
4797
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004798 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004799 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004800 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004801 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004802 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004803 }
4804
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004805 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004806 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004807 } else {
4808 b_del(buf, total);
4809 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004810
4811 /* The mux is full, cancel the pending tasks */
4812 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4813 (h2s->flags & H2_SF_BLK_MBUSY))
4814 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004815
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004816 /* If we're running HTX, and we read the whole buffer, then pretend
4817 * we read exactly what the caller specified, as with HTX the caller
4818 * will always give the buffer size, instead of the amount of data
4819 * available.
4820 */
4821 if (htx && !b_data(buf))
4822 total = orig_count;
4823
Olivier Houchard7505f942018-08-21 18:10:44 +02004824 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004825 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4826 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004827
Olivier Houchard7505f942018-08-21 18:10:44 +02004828 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004829 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004830}
4831
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004832/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004833static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004834{
4835 struct h2c *h2c = conn->mux_ctx;
4836 struct h2s *h2s;
4837 struct eb32_node *node;
4838 int fctl_cnt = 0;
4839 int send_cnt = 0;
4840 int tree_cnt = 0;
4841 int orph_cnt = 0;
4842
4843 if (!h2c)
4844 return;
4845
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004846 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004847 fctl_cnt++;
4848
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004849 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004850 send_cnt++;
4851
4852 node = eb32_first(&h2c->streams_by_id);
4853 while (node) {
4854 h2s = container_of(node, struct h2s, by_id);
4855 tree_cnt++;
4856 if (!h2s->cs)
4857 orph_cnt++;
4858 node = eb32_next(node);
4859 }
4860
Willy Tarreau616ac812018-07-24 14:12:42 +02004861 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4862 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4863 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4864 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4865 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4866 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004867}
Willy Tarreau62f52692017-10-08 23:01:42 +02004868
4869/*******************************************************/
4870/* functions below are dedicated to the config parsers */
4871/*******************************************************/
4872
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004873/* config parser for global "tune.h2.header-table-size" */
4874static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4875 struct proxy *defpx, const char *file, int line,
4876 char **err)
4877{
4878 if (too_many_args(1, args, err, NULL))
4879 return -1;
4880
4881 h2_settings_header_table_size = atoi(args[1]);
4882 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4883 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4884 return -1;
4885 }
4886 return 0;
4887}
Willy Tarreau62f52692017-10-08 23:01:42 +02004888
Willy Tarreaue6baec02017-07-27 11:45:11 +02004889/* config parser for global "tune.h2.initial-window-size" */
4890static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4891 struct proxy *defpx, const char *file, int line,
4892 char **err)
4893{
4894 if (too_many_args(1, args, err, NULL))
4895 return -1;
4896
4897 h2_settings_initial_window_size = atoi(args[1]);
4898 if (h2_settings_initial_window_size < 0) {
4899 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4900 return -1;
4901 }
4902 return 0;
4903}
4904
Willy Tarreau5242ef82017-07-27 11:47:28 +02004905/* config parser for global "tune.h2.max-concurrent-streams" */
4906static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4907 struct proxy *defpx, const char *file, int line,
4908 char **err)
4909{
4910 if (too_many_args(1, args, err, NULL))
4911 return -1;
4912
4913 h2_settings_max_concurrent_streams = atoi(args[1]);
4914 if (h2_settings_max_concurrent_streams < 0) {
4915 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4916 return -1;
4917 }
4918 return 0;
4919}
4920
Willy Tarreau62f52692017-10-08 23:01:42 +02004921
4922/****************************************/
4923/* MUX initialization and instanciation */
4924/***************************************/
4925
4926/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004927static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004928 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004929 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004930 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004931 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004932 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004933 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004934 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004935 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004936 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004937 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004938 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004939 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004940 .shutr = h2_shutr,
4941 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004942 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004943 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004944 .name = "H2",
4945};
4946
Christopher Faulet32f61c02018-04-10 14:33:41 +02004947/* PROTO selection : this mux registers PROTO token "h2" */
4948static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004949 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004950
Willy Tarreau0108d902018-11-25 19:14:37 +01004951INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4952
Willy Tarreauf8957272018-10-03 10:25:20 +02004953static struct mux_proto_list mux_proto_h2_htx =
4954 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4955
4956INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4957
Willy Tarreau62f52692017-10-08 23:01:42 +02004958/* config keyword parsers */
4959static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004960 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004961 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004962 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004963 { 0, NULL, NULL }
4964}};
4965
Willy Tarreau0108d902018-11-25 19:14:37 +01004966INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);