blob: 8d0851cc74bdc851a53bc364d08a5cd6aa305f15 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020020#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010023#include <proto/http_htx.h>
24#include <proto/htx.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010026#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020027#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020028
29
Willy Tarreau2a856182017-05-16 15:20:39 +020030/* dummy streams returned for idle and closed states */
31static const struct h2s *h2_closed_stream;
32static const struct h2s *h2_idle_stream;
33
Willy Tarreau5ab6b572017-09-22 08:05:00 +020034/* Connection flags (32 bit), in h2c->flags */
35#define H2_CF_NONE 0x00000000
36
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020037/* Flags indicating why writing to the mux is blocked. */
38#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
39#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
40#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
41
Willy Tarreau315d8072017-12-10 22:17:57 +010042/* Flags indicating why writing to the demux is blocked.
43 * The first two ones directly affect the ability for the mux to receive data
44 * from the connection. The other ones affect the mux's ability to demux
45 * received data.
46 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020047#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
48#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010049
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
51#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
52#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
53#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020054#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
55#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020056
Willy Tarreau081d4722017-05-16 21:51:05 +020057/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
59#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
60#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 +020061#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020062
Willy Tarreau5ab6b572017-09-22 08:05:00 +020063/* H2 connection state, in h2c->st0 */
64enum h2_cs {
65 H2_CS_PREFACE, // init done, waiting for connection preface
66 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
67 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
68 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010069 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
70 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020071 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
72 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
73 H2_CS_ENTRIES // must be last
74} __attribute__((packed));
75
76/* H2 connection descriptor */
77struct h2c {
78 struct connection *conn;
79
80 enum h2_cs st0; /* mux state */
81 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
82
83 /* 16 bit hole here */
84 uint32_t flags; /* connection flags: H2_CF_* */
85 int32_t max_id; /* highest ID known on this connection, <0 before preface */
86 uint32_t rcvd_c; /* newly received data to ACK for the connection */
87 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
88
89 /* states for the demux direction */
90 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020091 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020092
93 int32_t dsi; /* demux stream ID (<0 = idle) */
94 int32_t dfl; /* demux frame length (if dsi >= 0) */
95 int8_t dft; /* demux frame type (if dsi >= 0) */
96 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010097 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
98 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020099 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
100
101 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103 int32_t msi; /* mux stream ID (<0 = idle) */
104 int32_t mfl; /* mux frame length (if dsi >= 0) */
105 int8_t mft; /* mux frame type (if dsi >= 0) */
106 int8_t mff; /* mux frame flags (if dsi >= 0) */
107 /* 16 bit hole here */
108 int32_t miw; /* mux initial window size for all new streams */
109 int32_t mws; /* mux window size. Can be negative. */
110 int32_t mfs; /* mux's max frame size */
111
Willy Tarreauea392822017-10-31 10:02:25 +0100112 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100113 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100114 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200115 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200116 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100117 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200118 struct eb_root streams_by_id; /* all active streams by their ID */
119 struct list send_list; /* list of blocked streams requesting to send */
120 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200121 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200123 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200177 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200178 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200184 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200185 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200186 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
188 struct wait_event *send_wait; /* The streeam is waiting for flow control */
189 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200190};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200191
Willy Tarreauc6405142017-09-21 20:23:50 +0200192/* descriptor for an h2 frame header */
193struct h2_fh {
194 uint32_t len; /* length, host order, 24 bits */
195 uint32_t sid; /* stream id, host order, 31 bits */
196 uint8_t ft; /* frame type */
197 uint8_t ff; /* frame flags */
198};
199
Willy Tarreau8ceae722018-11-26 11:58:30 +0100200/* the h2c connection pool */
201DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
202
203/* the h2s stream pool */
204DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
205
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200206/* a few settings from the global section */
207static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200208static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200209static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200210
Willy Tarreau2a856182017-05-16 15:20:39 +0200211/* a dmumy closed stream */
212static const struct h2s *h2_closed_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100217 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
221/* and a dummy idle stream for use with any unannounced stream */
222static const struct h2s *h2_idle_stream = &(const struct h2s){
223 .cs = NULL,
224 .h2c = NULL,
225 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100226 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200227 .id = 0,
228};
229
Olivier Houchard9f6af332018-05-25 14:04:04 +0200230static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200231static int h2_send(struct h2c *h2c);
232static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200233static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200234static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100235static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200236static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100237static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200238static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreau751f2d02018-10-05 09:35:00 +0200239static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200240
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200241/*****************************************************/
242/* functions below are for dynamic buffer management */
243/*****************************************************/
244
Willy Tarreau315d8072017-12-10 22:17:57 +0100245/* indicates whether or not the we may call the h2_recv() function to attempt
246 * to receive data into the buffer and/or demux pending data. The condition is
247 * a bit complex due to some API limits for now. The rules are the following :
248 * - if an error or a shutdown was detected on the connection and the buffer
249 * is empty, we must not attempt to receive
250 * - if the demux buf failed to be allocated, we must not try to receive and
251 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100252 * - if no flag indicates a blocking condition, we may attempt to receive,
253 * regardless of whether the demux buffer is full or not, so that only
254 * de demux part decides whether or not to block. This is needed because
255 * the connection API indeed prevents us from re-enabling receipt that is
256 * already enabled in a polled state, so we must always immediately stop
257 * as soon as the demux can't proceed so as never to hit an end of read
258 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100259 * - otherwise must may not attempt
260 */
261static inline int h2_recv_allowed(const struct h2c *h2c)
262{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200263 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100264 (h2c->st0 >= H2_CS_ERROR ||
265 h2c->conn->flags & CO_FL_ERROR ||
266 conn_xprt_read0_pending(h2c->conn)))
267 return 0;
268
269 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100270 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100271 return 1;
272
273 return 0;
274}
275
Willy Tarreauf2101912018-07-19 10:11:38 +0200276/* returns true if the connection has too many conn_streams attached */
277static inline int h2_has_too_many_cs(const struct h2c *h2c)
278{
279 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
280}
281
Willy Tarreau44e973f2018-03-01 17:49:30 +0100282/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
283 * flags are used to figure what buffer was requested. It returns 1 if the
284 * allocation succeeds, in which case the connection is woken up, or 0 if it's
285 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200286 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100287static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200288{
289 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100290 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200293 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200294 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200295 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200296 return 1;
297 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200298
Willy Tarreau44e973f2018-03-01 17:49:30 +0100299 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
300 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301
302 if (h2c->flags & H2_CF_DEM_MROOM) {
303 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200304 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200305 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200306 }
Willy Tarreau14398122017-09-22 14:26:04 +0200307 return 1;
308 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100309
310 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
311 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200312 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200314 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200315 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100316 return 1;
317 }
318
Willy Tarreau14398122017-09-22 14:26:04 +0200319 return 0;
320}
321
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200322static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200323{
324 struct buffer *buf = NULL;
325
Willy Tarreau44e973f2018-03-01 17:49:30 +0100326 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
327 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
328 h2c->buf_wait.target = h2c;
329 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100331 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200333 __conn_xprt_stop_recv(h2c->conn);
334 }
335 return buf;
336}
337
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200338static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200339{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200340 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100341 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200342 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200343 }
344}
345
Olivier Houchardd540b362018-11-05 18:37:53 +0100346static int h2_avail_streams(struct connection *conn)
347{
348 struct h2c *h2c = conn->mux_ctx;
349
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100350 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100351 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
352}
353
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100354static int h2_max_streams(struct connection *conn)
355{
356 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
357 return h2_settings_max_concurrent_streams;
358}
359
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau62f52692017-10-08 23:01:42 +0200361/*****************************************************************/
362/* functions below are dedicated to the mux setup and management */
363/*****************************************************************/
364
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200365/* Initialize the mux once it's attached. For outgoing connections, the context
366 * is already initialized before installing the mux, so we detect incoming
367 * connections from the fact that the context is still NULL. Returns < 0 on
368 * error.
369 */
370static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371{
372 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100373 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200374
Willy Tarreaubafbe012017-11-24 17:34:44 +0100375 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200376 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200377 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200378
Willy Tarreau01b44822018-10-03 14:26:37 +0200379 if (conn->mux_ctx) {
380 h2c->flags = H2_CF_IS_BACK;
381 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
382 if (tick_isset(prx->timeout.serverfin))
383 h2c->shut_timeout = prx->timeout.serverfin;
384 } else {
385 h2c->flags = H2_CF_NONE;
386 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
387 if (tick_isset(prx->timeout.clientfin))
388 h2c->shut_timeout = prx->timeout.clientfin;
389 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100390
Willy Tarreau0b37d652018-10-03 10:33:02 +0200391 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100392 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100393 if (tick_isset(h2c->timeout)) {
394 t = task_new(tid_bit);
395 if (!t)
396 goto fail;
397
398 h2c->task = t;
399 t->process = h2_timeout_task;
400 t->context = h2c;
401 t->expire = tick_add(now_ms, h2c->timeout);
402 }
Willy Tarreauea392822017-10-31 10:02:25 +0100403
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200404 h2c->wait_event.task = tasklet_new();
405 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200406 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200407 h2c->wait_event.task->process = h2_io_cb;
408 h2c->wait_event.task->context = h2c;
409 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200410
Willy Tarreau32218eb2017-09-22 08:07:25 +0200411 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
412 if (!h2c->ddht)
413 goto fail;
414
415 /* Initialise the context. */
416 h2c->st0 = H2_CS_PREFACE;
417 h2c->conn = conn;
418 h2c->max_id = -1;
419 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 h2c->rcvd_c = 0;
421 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100422 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200423 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200424
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200425 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 h2c->dsi = -1;
427 h2c->msi = -1;
428 h2c->last_sid = -1;
429
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200430 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 h2c->miw = 65535; /* mux initial window size */
432 h2c->mws = 65535; /* mux window size */
433 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200434 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200435 LIST_INIT(&h2c->send_list);
436 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200437 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100438 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200439
Willy Tarreau3f133572017-10-31 19:21:06 +0100440 if (t)
441 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100442
Willy Tarreau01b44822018-10-03 14:26:37 +0200443 if (h2c->flags & H2_CF_IS_BACK) {
444 /* FIXME: this is temporary, for outgoing connections we need
445 * to immediately allocate a stream until the code is modified
446 * so that the caller calls ->attach(). For now the outgoing cs
447 * is stored as conn->mux_ctx by the caller.
448 */
449 struct h2s *h2s;
450
451 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx);
452 if (!h2s)
453 goto fail_stream;
454 }
455
456 conn->mux_ctx = h2c;
457
Willy Tarreau0f383582018-10-03 14:22:21 +0200458 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200459 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200460 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200461 fail_stream:
462 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200463 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100464 if (t)
465 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200466 if (h2c->wait_event.task)
467 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100468 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200469 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470 return -1;
471}
472
Willy Tarreau751f2d02018-10-05 09:35:00 +0200473/* returns the next allocatable outgoing stream ID for the H2 connection, or
474 * -1 if no more is allocatable.
475 */
476static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
477{
478 int32_t id = (h2c->max_id + 1) | 1;
479 if (id & 0x80000000U)
480 id = -1;
481 return id;
482}
483
Willy Tarreau2373acc2017-10-12 17:35:14 +0200484/* returns the stream associated with id <id> or NULL if not found */
485static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
486{
487 struct eb32_node *node;
488
Willy Tarreau751f2d02018-10-05 09:35:00 +0200489 if (id == 0)
490 return (struct h2s *)h2_closed_stream;
491
Willy Tarreau2a856182017-05-16 15:20:39 +0200492 if (id > h2c->max_id)
493 return (struct h2s *)h2_idle_stream;
494
Willy Tarreau2373acc2017-10-12 17:35:14 +0200495 node = eb32_lookup(&h2c->streams_by_id, id);
496 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200497 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200498
499 return container_of(node, struct h2s, by_id);
500}
501
Willy Tarreau62f52692017-10-08 23:01:42 +0200502/* release function for a connection. This one should be called to free all
503 * resources allocated to the mux.
504 */
505static void h2_release(struct connection *conn)
506{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200507 struct h2c *h2c = conn->mux_ctx;
508
509 LIST_DEL(&conn->list);
510
511 if (h2c) {
512 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200513
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100514 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100515 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100516 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200517
Willy Tarreau44e973f2018-03-01 17:49:30 +0100518 h2_release_buf(h2c, &h2c->dbuf);
519 h2_release_buf(h2c, &h2c->mbuf);
520
Willy Tarreauea392822017-10-31 10:02:25 +0100521 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200522 h2c->task->context = NULL;
523 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100524 h2c->task = NULL;
525 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200526 if (h2c->wait_event.task)
527 tasklet_free(h2c->wait_event.task);
528 if (h2c->wait_event.wait_reason != 0)
529 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
530 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100531
Willy Tarreaubafbe012017-11-24 17:34:44 +0100532 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200533 }
534
535 conn->mux = NULL;
536 conn->mux_ctx = NULL;
537
538 conn_stop_tracking(conn);
539 conn_full_close(conn);
540 if (conn->destroy_cb)
541 conn->destroy_cb(conn);
542 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200543}
544
545
Willy Tarreau71681172017-10-23 14:39:06 +0200546/******************************************************/
547/* functions below are for the H2 protocol processing */
548/******************************************************/
549
550/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100551static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200552{
553 return h2s ? h2s->id : 0;
554}
555
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200556/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200558{
559 if (h2c->msi < 0)
560 return 0;
561
562 if (h2c->msi == h2s_id(h2s))
563 return 0;
564
565 return 1;
566}
567
Willy Tarreau741d6df2017-10-17 08:00:59 +0200568/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100569static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200570{
571 h2c->errcode = err;
572 h2c->st0 = H2_CS_ERROR;
573}
574
Willy Tarreau2e43f082017-10-17 08:03:59 +0200575/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200577{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200578 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200579 h2s->errcode = err;
580 h2s->st = H2_SS_ERROR;
581 if (h2s->cs)
582 h2s->cs->flags |= CS_FL_ERROR;
583 }
584}
585
Willy Tarreaue4820742017-07-27 13:37:23 +0200586/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100587static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200588{
589 uint8_t *out = frame;
590
591 *out = len >> 16;
592 write_n16(out + 1, len);
593}
594
Willy Tarreau54c15062017-10-10 17:10:03 +0200595/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
596 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
597 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200598 * available in the buffer's input prior to calling this function. The buffer
599 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200600 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100601static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200602 const struct buffer *b, int o)
603{
Willy Tarreau591d4452018-06-15 17:21:00 +0200604 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 +0200605}
606
Willy Tarreau1f094672017-11-20 21:27:45 +0100607static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200608{
Willy Tarreau591d4452018-06-15 17:21:00 +0200609 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200610}
611
Willy Tarreau1f094672017-11-20 21:27:45 +0100612static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200613{
Willy Tarreau591d4452018-06-15 17:21:00 +0200614 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200615}
616
Willy Tarreau1f094672017-11-20 21:27:45 +0100617static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200618{
Willy Tarreau591d4452018-06-15 17:21:00 +0200619 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200620}
621
622
Willy Tarreau715d5312017-07-11 15:20:24 +0200623/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
624 * is not obvious. It turns out that H2 headers are neither aligned nor do they
625 * use regular sizes. And to add to the trouble, the buffer may wrap so each
626 * byte read must be checked. The header is formed like this :
627 *
628 * b0 b1 b2 b3 b4 b5..b8
629 * +----------+---------+--------+----+----+----------------------+
630 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
631 * +----------+---------+--------+----+----+----------------------+
632 *
633 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
634 * we get the sid properly aligned and ordered, and 16 bits of len properly
635 * ordered as well. The type and flags can be extracted using bit shifts from
636 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200637 * Returns zero if some bytes are missing, otherwise non-zero on success. The
638 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200639 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100640static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200641{
642 uint64_t w;
643
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200644 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200645 return 0;
646
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200647 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200648 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200649 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
650 h->ff = w >> 32;
651 h->ft = w >> 40;
652 h->len += w >> 48;
653 return 1;
654}
655
656/* skip the next 9 bytes corresponding to the frame header possibly parsed by
657 * h2_peek_frame_hdr() above.
658 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100659static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200660{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200661 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200662}
663
664/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100665static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200666{
667 int ret;
668
669 ret = h2_peek_frame_hdr(b, h);
670 if (ret > 0)
671 h2_skip_frame_hdr(b);
672 return ret;
673}
674
Willy Tarreau00dd0782018-03-01 16:31:34 +0100675/* marks stream <h2s> as CLOSED and decrement the number of active streams for
676 * its connection if the stream was not yet closed. Please use this exclusively
677 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100678 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100679static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100680{
681 if (h2s->st != H2_SS_CLOSED)
682 h2s->h2c->nb_streams--;
683 h2s->st = H2_SS_CLOSED;
684}
685
Willy Tarreau71049cc2018-03-28 13:56:39 +0200686/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
687static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100688{
689 h2s_close(h2s);
690 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200691 if (b_size(&h2s->rxbuf)) {
692 b_free(&h2s->rxbuf);
693 offer_buffers(NULL, tasks_run_queue);
694 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200695 if (h2s->send_wait != NULL)
696 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
697 if (h2s->recv_wait != NULL)
698 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
699 /* There's no need to explicitely call unsubscribe here, the only
700 * reference left would be in the h2c send_list/fctl_list, and if
701 * we're in it, we're getting out anyway
702 */
703 LIST_DEL(&h2s->list);
704 LIST_INIT(&h2s->list);
705 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100706 pool_free(pool_head_h2s, h2s);
707}
708
Willy Tarreaua8e49542018-10-03 18:53:55 +0200709/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
710 * stream tree. In case of error, nothing is added and NULL is returned. The
711 * causes of errors can be any failed memory allocation. The caller is
712 * responsible for checking if the connection may support an extra stream
713 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200714 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200715static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200716{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200717 struct h2s *h2s;
718
Willy Tarreaubafbe012017-11-24 17:34:44 +0100719 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200720 if (!h2s)
721 goto out;
722
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200723 h2s->wait_event.task = tasklet_new();
724 if (!h2s->wait_event.task) {
725 pool_free(pool_head_h2s, h2s);
726 goto out;
727 }
728 h2s->send_wait = NULL;
729 h2s->recv_wait = NULL;
730 h2s->wait_event.task->process = h2_deferred_shut;
731 h2s->wait_event.task->context = h2s;
732 h2s->wait_event.handle = NULL;
733 h2s->wait_event.wait_reason = 0;
734 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200735 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200736 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200737 h2s->mws = h2c->miw;
738 h2s->flags = H2_SF_NONE;
739 h2s->errcode = H2_ERR_NO_ERROR;
740 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200741 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200742 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200743
744 if (h2c->flags & H2_CF_IS_BACK) {
745 h1m_init_req(&h2s->h1m);
746 h2s->h1m.err_pos = -1; // don't care about errors on the request path
747 h2s->h1m.flags |= H1_MF_TOLOWER;
748 } else {
749 h1m_init_res(&h2s->h1m);
750 h2s->h1m.err_pos = -1; // don't care about errors on the response path
751 h2s->h1m.flags |= H1_MF_TOLOWER;
752 }
753
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200754 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200755 if (id > 0)
756 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200757
758 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100759 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200760
761 return h2s;
762
763 out_free_h2s:
764 pool_free(pool_head_h2s, h2s);
765 out:
766 return NULL;
767}
768
769/* creates a new stream <id> on the h2c connection and returns it, or NULL in
770 * case of memory allocation error.
771 */
772static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
773{
774 struct session *sess = h2c->conn->owner;
775 struct conn_stream *cs;
776 struct h2s *h2s;
777
778 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
779 goto out;
780
781 h2s = h2s_new(h2c, id);
782 if (!h2s)
783 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200784
785 cs = cs_new(h2c->conn);
786 if (!cs)
787 goto out_close;
788
789 h2s->cs = cs;
790 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200791 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200792
793 if (stream_create_from_cs(cs) < 0)
794 goto out_free_cs;
795
Willy Tarreau590a0512018-09-05 11:56:48 +0200796 /* We want the accept date presented to the next stream to be the one
797 * we have now, the handshake time to be null (since the next stream
798 * is not delayed by a handshake), and the idle time to count since
799 * right now.
800 */
801 sess->accept_date = date;
802 sess->tv_accept = now;
803 sess->t_handshake = 0;
804
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200805 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200806 if (h2_has_too_many_cs(h2c))
807 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200808 return h2s;
809
810 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200811 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200812 cs_free(cs);
813 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200814 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200815 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200816 sess_log(sess);
817 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200818}
819
Willy Tarreau751f2d02018-10-05 09:35:00 +0200820/* allocates a new stream associated to conn_stream <cs> on the h2c connection
821 * and returns it, or NULL in case of memory allocation error or if the highest
822 * possible stream ID was reached.
823 */
824static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs)
825{
826 struct h2s *h2s = NULL;
827
828 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
829 goto out;
830
831 /* Defer choosing the ID until we send the first message to create the stream */
832 h2s = h2s_new(h2c, 0);
833 if (!h2s)
834 goto out;
835
836 h2s->cs = cs;
837 cs->ctx = h2s;
838 h2c->nb_cs++;
839
Willy Tarreau751f2d02018-10-05 09:35:00 +0200840 out:
841 return h2s;
842}
843
Willy Tarreaube5b7152017-09-25 16:25:39 +0200844/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
845 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
846 * the various settings codes.
847 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200848static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200849{
850 struct buffer *res;
851 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200852 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200853 int ret;
854
855 if (h2c_mux_busy(h2c, NULL)) {
856 h2c->flags |= H2_CF_DEM_MBUSY;
857 return 0;
858 }
859
Willy Tarreau44e973f2018-03-01 17:49:30 +0100860 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200861 if (!res) {
862 h2c->flags |= H2_CF_MUX_MALLOC;
863 h2c->flags |= H2_CF_DEM_MROOM;
864 return 0;
865 }
866
867 chunk_init(&buf, buf_data, sizeof(buf_data));
868 chunk_memcpy(&buf,
869 "\x00\x00\x00" /* length : 0 for now */
870 "\x04\x00" /* type : 4 (settings), flags : 0 */
871 "\x00\x00\x00\x00", /* stream ID : 0 */
872 9);
873
874 if (h2_settings_header_table_size != 4096) {
875 char str[6] = "\x00\x01"; /* header_table_size */
876
877 write_n32(str + 2, h2_settings_header_table_size);
878 chunk_memcat(&buf, str, 6);
879 }
880
881 if (h2_settings_initial_window_size != 65535) {
882 char str[6] = "\x00\x04"; /* initial_window_size */
883
884 write_n32(str + 2, h2_settings_initial_window_size);
885 chunk_memcat(&buf, str, 6);
886 }
887
888 if (h2_settings_max_concurrent_streams != 0) {
889 char str[6] = "\x00\x03"; /* max_concurrent_streams */
890
891 /* Note: 0 means "unlimited" for haproxy's config but not for
892 * the protocol, so never send this value!
893 */
894 write_n32(str + 2, h2_settings_max_concurrent_streams);
895 chunk_memcat(&buf, str, 6);
896 }
897
898 if (global.tune.bufsize != 16384) {
899 char str[6] = "\x00\x05"; /* max_frame_size */
900
901 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
902 * match bufsize - rewrite size, but at the moment it seems
903 * that clients don't take care of it.
904 */
905 write_n32(str + 2, global.tune.bufsize);
906 chunk_memcat(&buf, str, 6);
907 }
908
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200909 h2_set_frame_size(buf.area, buf.data - 9);
910 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200911 if (unlikely(ret <= 0)) {
912 if (!ret) {
913 h2c->flags |= H2_CF_MUX_MFULL;
914 h2c->flags |= H2_CF_DEM_MROOM;
915 return 0;
916 }
917 else {
918 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
919 return 0;
920 }
921 }
922 return ret;
923}
924
Willy Tarreau52eed752017-09-22 15:05:09 +0200925/* Try to receive a connection preface, then upon success try to send our
926 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
927 * missing data. It may return an error in h2c.
928 */
929static int h2c_frt_recv_preface(struct h2c *h2c)
930{
931 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200932 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200933
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200934 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200935
936 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200937 if (ret1 < 0)
938 sess_log(h2c->conn->owner);
939
Willy Tarreau52eed752017-09-22 15:05:09 +0200940 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
941 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
942 return 0;
943 }
944
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200945 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200946 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200947 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200948
Willy Tarreaube5b7152017-09-25 16:25:39 +0200949 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200950}
951
Willy Tarreau01b44822018-10-03 14:26:37 +0200952/* Try to send a connection preface, then upon success try to send our
953 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
954 * missing data. It may return an error in h2c.
955 */
956static int h2c_bck_send_preface(struct h2c *h2c)
957{
958 struct buffer *res;
959
960 if (h2c_mux_busy(h2c, NULL)) {
961 h2c->flags |= H2_CF_DEM_MBUSY;
962 return 0;
963 }
964
965 res = h2_get_buf(h2c, &h2c->mbuf);
966 if (!res) {
967 h2c->flags |= H2_CF_MUX_MALLOC;
968 h2c->flags |= H2_CF_DEM_MROOM;
969 return 0;
970 }
971
972 if (!b_data(res)) {
973 /* preface not yet sent */
974 b_istput(res, ist(H2_CONN_PREFACE));
975 }
976
977 return h2c_send_settings(h2c);
978}
979
Willy Tarreau081d4722017-05-16 21:51:05 +0200980/* try to send a GOAWAY frame on the connection to report an error or a graceful
981 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
982 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
983 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
984 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
985 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
986 * on unrecoverable failure. It will not attempt to send one again in this last
987 * case so that it is safe to use h2c_error() to report such errors.
988 */
989static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
990{
991 struct buffer *res;
992 char str[17];
993 int ret;
994
995 if (h2c->flags & H2_CF_GOAWAY_FAILED)
996 return 1; // claim that it worked
997
998 if (h2c_mux_busy(h2c, h2s)) {
999 if (h2s)
1000 h2s->flags |= H2_SF_BLK_MBUSY;
1001 else
1002 h2c->flags |= H2_CF_DEM_MBUSY;
1003 return 0;
1004 }
1005
Willy Tarreau44e973f2018-03-01 17:49:30 +01001006 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001007 if (!res) {
1008 h2c->flags |= H2_CF_MUX_MALLOC;
1009 if (h2s)
1010 h2s->flags |= H2_SF_BLK_MROOM;
1011 else
1012 h2c->flags |= H2_CF_DEM_MROOM;
1013 return 0;
1014 }
1015
1016 /* len: 8, type: 7, flags: none, sid: 0 */
1017 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1018
1019 if (h2c->last_sid < 0)
1020 h2c->last_sid = h2c->max_id;
1021
1022 write_n32(str + 9, h2c->last_sid);
1023 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001024 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001025 if (unlikely(ret <= 0)) {
1026 if (!ret) {
1027 h2c->flags |= H2_CF_MUX_MFULL;
1028 if (h2s)
1029 h2s->flags |= H2_SF_BLK_MROOM;
1030 else
1031 h2c->flags |= H2_CF_DEM_MROOM;
1032 return 0;
1033 }
1034 else {
1035 /* we cannot report this error using GOAWAY, so we mark
1036 * it and claim a success.
1037 */
1038 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1039 h2c->flags |= H2_CF_GOAWAY_FAILED;
1040 return 1;
1041 }
1042 }
1043 h2c->flags |= H2_CF_GOAWAY_SENT;
1044 return ret;
1045}
1046
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001047/* Try to send an RST_STREAM frame on the connection for the indicated stream
1048 * during mux operations. This stream must be valid and cannot be closed
1049 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1050 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1051 * not yet.
1052 *
1053 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1054 * to write the message, it subscribes the stream to future notifications.
1055 */
1056static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1057{
1058 struct buffer *res;
1059 char str[13];
1060 int ret;
1061
1062 if (!h2s || h2s->st == H2_SS_CLOSED)
1063 return 1;
1064
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001065 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1066 * RST_STREAM in response to a RST_STREAM frame.
1067 */
1068 if (h2c->dft == H2_FT_RST_STREAM) {
1069 ret = 1;
1070 goto ignore;
1071 }
1072
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001073 if (h2c_mux_busy(h2c, h2s)) {
1074 h2s->flags |= H2_SF_BLK_MBUSY;
1075 return 0;
1076 }
1077
Willy Tarreau44e973f2018-03-01 17:49:30 +01001078 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001079 if (!res) {
1080 h2c->flags |= H2_CF_MUX_MALLOC;
1081 h2s->flags |= H2_SF_BLK_MROOM;
1082 return 0;
1083 }
1084
1085 /* len: 4, type: 3, flags: none */
1086 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1087 write_n32(str + 5, h2s->id);
1088 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001089 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001090
1091 if (unlikely(ret <= 0)) {
1092 if (!ret) {
1093 h2c->flags |= H2_CF_MUX_MFULL;
1094 h2s->flags |= H2_SF_BLK_MROOM;
1095 return 0;
1096 }
1097 else {
1098 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1099 return 0;
1100 }
1101 }
1102
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001103 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001104 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001105 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001106 return ret;
1107}
1108
1109/* Try to send an RST_STREAM frame on the connection for the stream being
1110 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1111 * error code unless the stream's state already is IDLE or CLOSED in which
1112 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1113 * it was not yet.
1114 *
1115 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1116 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001117 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001118 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001119 */
1120static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1121{
1122 struct buffer *res;
1123 char str[13];
1124 int ret;
1125
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001126 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1127 * RST_STREAM in response to a RST_STREAM frame.
1128 */
1129 if (h2c->dft == H2_FT_RST_STREAM) {
1130 ret = 1;
1131 goto ignore;
1132 }
1133
Willy Tarreau27a84c92017-10-17 08:10:17 +02001134 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001135 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001136 return 0;
1137 }
1138
Willy Tarreau44e973f2018-03-01 17:49:30 +01001139 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001140 if (!res) {
1141 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001142 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001143 return 0;
1144 }
1145
1146 /* len: 4, type: 3, flags: none */
1147 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001148
Willy Tarreau27a84c92017-10-17 08:10:17 +02001149 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001150 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001151 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001152
Willy Tarreau27a84c92017-10-17 08:10:17 +02001153 if (unlikely(ret <= 0)) {
1154 if (!ret) {
1155 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001156 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001157 return 0;
1158 }
1159 else {
1160 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1161 return 0;
1162 }
1163 }
1164
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001165 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001166 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001167 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001168 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001169 }
1170
Willy Tarreau27a84c92017-10-17 08:10:17 +02001171 return ret;
1172}
1173
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001174/* try to send an empty DATA frame with the ES flag set to notify about the
1175 * end of stream and match a shutdown(write). If an ES was already sent as
1176 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1177 * on success or zero if nothing was done. In case of lack of room to write the
1178 * message, it subscribes the requesting stream to future notifications.
1179 */
1180static int h2_send_empty_data_es(struct h2s *h2s)
1181{
1182 struct h2c *h2c = h2s->h2c;
1183 struct buffer *res;
1184 char str[9];
1185 int ret;
1186
Willy Tarreau721c9742017-11-07 11:05:42 +01001187 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001188 return 1;
1189
1190 if (h2c_mux_busy(h2c, h2s)) {
1191 h2s->flags |= H2_SF_BLK_MBUSY;
1192 return 0;
1193 }
1194
Willy Tarreau44e973f2018-03-01 17:49:30 +01001195 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001196 if (!res) {
1197 h2c->flags |= H2_CF_MUX_MALLOC;
1198 h2s->flags |= H2_SF_BLK_MROOM;
1199 return 0;
1200 }
1201
1202 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1203 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1204 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001205 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001206 if (likely(ret > 0)) {
1207 h2s->flags |= H2_SF_ES_SENT;
1208 }
1209 else if (!ret) {
1210 h2c->flags |= H2_CF_MUX_MFULL;
1211 h2s->flags |= H2_SF_BLK_MROOM;
1212 return 0;
1213 }
1214 else {
1215 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1216 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001217 }
1218 return ret;
1219}
1220
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001221/* wake the streams attached to the connection, whose id is greater than <last>,
1222 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001223 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1224 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001225 */
1226static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1227{
1228 struct eb32_node *node;
1229 struct h2s *h2s;
1230
1231 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1232 flags |= CS_FL_ERROR;
1233
1234 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001235 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001236
1237 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1238 while (node) {
1239 h2s = container_of(node, struct h2s, by_id);
1240 if (h2s->id <= last)
1241 break;
1242 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001243
1244 if (!h2s->cs) {
1245 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001246 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001247 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001248 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001249
1250 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001251 if (h2s->recv_wait) {
1252 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001253 sw->wait_reason &= ~SUB_CAN_RECV;
1254 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001255 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001256 } else if (h2s->cs->data_cb->wake != NULL)
1257 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001258
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001259 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1260 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001261 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001262 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001263 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001264 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001265 }
1266}
1267
Willy Tarreau3421aba2017-07-27 15:41:03 +02001268/* Increase all streams' outgoing window size by the difference passed in
1269 * argument. This is needed upon receipt of the settings frame if the initial
1270 * window size is different. The difference may be negative and the resulting
1271 * window size as well, for the time it takes to receive some window updates.
1272 */
1273static void h2c_update_all_ws(struct h2c *h2c, int diff)
1274{
1275 struct h2s *h2s;
1276 struct eb32_node *node;
1277
1278 if (!diff)
1279 return;
1280
1281 node = eb32_first(&h2c->streams_by_id);
1282 while (node) {
1283 h2s = container_of(node, struct h2s, by_id);
1284 h2s->mws += diff;
1285 node = eb32_next(node);
1286 }
1287}
1288
1289/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1290 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1291 * return an error in h2c. Described in RFC7540#6.5.
1292 */
1293static int h2c_handle_settings(struct h2c *h2c)
1294{
1295 unsigned int offset;
1296 int error;
1297
1298 if (h2c->dff & H2_F_SETTINGS_ACK) {
1299 if (h2c->dfl) {
1300 error = H2_ERR_FRAME_SIZE_ERROR;
1301 goto fail;
1302 }
1303 return 1;
1304 }
1305
1306 if (h2c->dsi != 0) {
1307 error = H2_ERR_PROTOCOL_ERROR;
1308 goto fail;
1309 }
1310
1311 if (h2c->dfl % 6) {
1312 error = H2_ERR_FRAME_SIZE_ERROR;
1313 goto fail;
1314 }
1315
1316 /* that's the limit we can process */
1317 if (h2c->dfl > global.tune.bufsize) {
1318 error = H2_ERR_FRAME_SIZE_ERROR;
1319 goto fail;
1320 }
1321
1322 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001323 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001324 return 0;
1325
1326 /* parse the frame */
1327 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001328 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1329 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001330
1331 switch (type) {
1332 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1333 /* we need to update all existing streams with the
1334 * difference from the previous iws.
1335 */
1336 if (arg < 0) { // RFC7540#6.5.2
1337 error = H2_ERR_FLOW_CONTROL_ERROR;
1338 goto fail;
1339 }
1340 h2c_update_all_ws(h2c, arg - h2c->miw);
1341 h2c->miw = arg;
1342 break;
1343 case H2_SETTINGS_MAX_FRAME_SIZE:
1344 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1345 error = H2_ERR_PROTOCOL_ERROR;
1346 goto fail;
1347 }
1348 h2c->mfs = arg;
1349 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001350 case H2_SETTINGS_ENABLE_PUSH:
1351 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1352 error = H2_ERR_PROTOCOL_ERROR;
1353 goto fail;
1354 }
1355 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001356 }
1357 }
1358
1359 /* need to ACK this frame now */
1360 h2c->st0 = H2_CS_FRAME_A;
1361 return 1;
1362 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001363 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001364 h2c_error(h2c, error);
1365 return 0;
1366}
1367
1368/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1369 * success or one of the h2_status values.
1370 */
1371static int h2c_ack_settings(struct h2c *h2c)
1372{
1373 struct buffer *res;
1374 char str[9];
1375 int ret = -1;
1376
1377 if (h2c_mux_busy(h2c, NULL)) {
1378 h2c->flags |= H2_CF_DEM_MBUSY;
1379 return 0;
1380 }
1381
Willy Tarreau44e973f2018-03-01 17:49:30 +01001382 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001383 if (!res) {
1384 h2c->flags |= H2_CF_MUX_MALLOC;
1385 h2c->flags |= H2_CF_DEM_MROOM;
1386 return 0;
1387 }
1388
1389 memcpy(str,
1390 "\x00\x00\x00" /* length : 0 (no data) */
1391 "\x04" "\x01" /* type : 4, flags : ACK */
1392 "\x00\x00\x00\x00" /* stream ID */, 9);
1393
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001394 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001395 if (unlikely(ret <= 0)) {
1396 if (!ret) {
1397 h2c->flags |= H2_CF_MUX_MFULL;
1398 h2c->flags |= H2_CF_DEM_MROOM;
1399 return 0;
1400 }
1401 else {
1402 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1403 return 0;
1404 }
1405 }
1406 return ret;
1407}
1408
Willy Tarreaucf68c782017-10-10 17:11:41 +02001409/* processes a PING frame and schedules an ACK if needed. The caller must pass
1410 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1411 * missing data. It may return an error in h2c.
1412 */
1413static int h2c_handle_ping(struct h2c *h2c)
1414{
1415 /* frame length must be exactly 8 */
1416 if (h2c->dfl != 8) {
1417 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1418 return 0;
1419 }
1420
1421 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001422 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001423 h2c->st0 = H2_CS_FRAME_A;
1424 return 1;
1425}
1426
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001427/* Try to send a window update for stream id <sid> and value <increment>.
1428 * Returns > 0 on success or zero on missing room or failure. It may return an
1429 * error in h2c.
1430 */
1431static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1432{
1433 struct buffer *res;
1434 char str[13];
1435 int ret = -1;
1436
1437 if (h2c_mux_busy(h2c, NULL)) {
1438 h2c->flags |= H2_CF_DEM_MBUSY;
1439 return 0;
1440 }
1441
Willy Tarreau44e973f2018-03-01 17:49:30 +01001442 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001443 if (!res) {
1444 h2c->flags |= H2_CF_MUX_MALLOC;
1445 h2c->flags |= H2_CF_DEM_MROOM;
1446 return 0;
1447 }
1448
1449 /* length: 4, type: 8, flags: none */
1450 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1451 write_n32(str + 5, sid);
1452 write_n32(str + 9, increment);
1453
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001454 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001455
1456 if (unlikely(ret <= 0)) {
1457 if (!ret) {
1458 h2c->flags |= H2_CF_MUX_MFULL;
1459 h2c->flags |= H2_CF_DEM_MROOM;
1460 return 0;
1461 }
1462 else {
1463 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1464 return 0;
1465 }
1466 }
1467 return ret;
1468}
1469
1470/* try to send pending window update for the connection. It's safe to call it
1471 * with no pending updates. Returns > 0 on success or zero on missing room or
1472 * failure. It may return an error in h2c.
1473 */
1474static int h2c_send_conn_wu(struct h2c *h2c)
1475{
1476 int ret = 1;
1477
1478 if (h2c->rcvd_c <= 0)
1479 return 1;
1480
1481 /* send WU for the connection */
1482 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1483 if (ret > 0)
1484 h2c->rcvd_c = 0;
1485
1486 return ret;
1487}
1488
1489/* try to send pending window update for the current dmux stream. It's safe to
1490 * call it with no pending updates. Returns > 0 on success or zero on missing
1491 * room or failure. It may return an error in h2c.
1492 */
1493static int h2c_send_strm_wu(struct h2c *h2c)
1494{
1495 int ret = 1;
1496
1497 if (h2c->rcvd_s <= 0)
1498 return 1;
1499
1500 /* send WU for the stream */
1501 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1502 if (ret > 0)
1503 h2c->rcvd_s = 0;
1504
1505 return ret;
1506}
1507
Willy Tarreaucf68c782017-10-10 17:11:41 +02001508/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1509 * success, 0 on missing data or one of the h2_status values.
1510 */
1511static int h2c_ack_ping(struct h2c *h2c)
1512{
1513 struct buffer *res;
1514 char str[17];
1515 int ret = -1;
1516
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001517 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001518 return 0;
1519
1520 if (h2c_mux_busy(h2c, NULL)) {
1521 h2c->flags |= H2_CF_DEM_MBUSY;
1522 return 0;
1523 }
1524
Willy Tarreau44e973f2018-03-01 17:49:30 +01001525 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001526 if (!res) {
1527 h2c->flags |= H2_CF_MUX_MALLOC;
1528 h2c->flags |= H2_CF_DEM_MROOM;
1529 return 0;
1530 }
1531
1532 memcpy(str,
1533 "\x00\x00\x08" /* length : 8 (same payload) */
1534 "\x06" "\x01" /* type : 6, flags : ACK */
1535 "\x00\x00\x00\x00" /* stream ID */, 9);
1536
1537 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001538 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001539
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001540 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001541 if (unlikely(ret <= 0)) {
1542 if (!ret) {
1543 h2c->flags |= H2_CF_MUX_MFULL;
1544 h2c->flags |= H2_CF_DEM_MROOM;
1545 return 0;
1546 }
1547 else {
1548 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1549 return 0;
1550 }
1551 }
1552 return ret;
1553}
1554
Willy Tarreau26f95952017-07-27 17:18:30 +02001555/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1556 * Returns > 0 on success or zero on missing data. It may return an error in
1557 * h2c or h2s. Described in RFC7540#6.9.
1558 */
1559static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1560{
1561 int32_t inc;
1562 int error;
1563
1564 if (h2c->dfl != 4) {
1565 error = H2_ERR_FRAME_SIZE_ERROR;
1566 goto conn_err;
1567 }
1568
1569 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001570 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001571 return 0;
1572
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001573 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001574
1575 if (h2c->dsi != 0) {
1576 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001577
1578 /* it's not an error to receive WU on a closed stream */
1579 if (h2s->st == H2_SS_CLOSED)
1580 return 1;
1581
1582 if (!inc) {
1583 error = H2_ERR_PROTOCOL_ERROR;
1584 goto strm_err;
1585 }
1586
1587 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1588 error = H2_ERR_FLOW_CONTROL_ERROR;
1589 goto strm_err;
1590 }
1591
1592 h2s->mws += inc;
1593 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1594 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001595 if (h2s->send_wait)
1596 LIST_ADDQ(&h2c->send_list, &h2s->list);
1597
Willy Tarreau26f95952017-07-27 17:18:30 +02001598 }
1599 }
1600 else {
1601 /* connection window update */
1602 if (!inc) {
1603 error = H2_ERR_PROTOCOL_ERROR;
1604 goto conn_err;
1605 }
1606
1607 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1608 error = H2_ERR_FLOW_CONTROL_ERROR;
1609 goto conn_err;
1610 }
1611
1612 h2c->mws += inc;
1613 }
1614
1615 return 1;
1616
1617 conn_err:
1618 h2c_error(h2c, error);
1619 return 0;
1620
1621 strm_err:
1622 if (h2s) {
1623 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001624 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001625 }
1626 else
1627 h2c_error(h2c, error);
1628 return 0;
1629}
1630
Willy Tarreaue96b0922017-10-30 00:28:29 +01001631/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1632 * the last ID. Returns > 0 on success or zero on missing data. It may return
1633 * an error in h2c. Described in RFC7540#6.8.
1634 */
1635static int h2c_handle_goaway(struct h2c *h2c)
1636{
1637 int error;
1638 int last;
1639
1640 if (h2c->dsi != 0) {
1641 error = H2_ERR_PROTOCOL_ERROR;
1642 goto conn_err;
1643 }
1644
1645 if (h2c->dfl < 8) {
1646 error = H2_ERR_FRAME_SIZE_ERROR;
1647 goto conn_err;
1648 }
1649
1650 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001651 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001652 return 0;
1653
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001654 last = h2_get_n32(&h2c->dbuf, 0);
1655 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001656 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001657 if (h2c->last_sid < 0)
1658 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001659 return 1;
1660
1661 conn_err:
1662 h2c_error(h2c, error);
1663 return 0;
1664}
1665
Willy Tarreau92153fc2017-12-03 19:46:19 +01001666/* processes a PRIORITY frame, and either skips it or rejects if it is
1667 * invalid. Returns > 0 on success or zero on missing data. It may return
1668 * an error in h2c. Described in RFC7540#6.3.
1669 */
1670static int h2c_handle_priority(struct h2c *h2c)
1671{
1672 int error;
1673
1674 if (h2c->dsi == 0) {
1675 error = H2_ERR_PROTOCOL_ERROR;
1676 goto conn_err;
1677 }
1678
1679 if (h2c->dfl != 5) {
1680 error = H2_ERR_FRAME_SIZE_ERROR;
1681 goto conn_err;
1682 }
1683
1684 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001685 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001686 return 0;
1687
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001688 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001689 /* 7540#5.3 : can't depend on itself */
1690 error = H2_ERR_PROTOCOL_ERROR;
1691 goto conn_err;
1692 }
1693 return 1;
1694
1695 conn_err:
1696 h2c_error(h2c, error);
1697 return 0;
1698}
1699
Willy Tarreaucd234e92017-08-18 10:59:39 +02001700/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1701 * Returns > 0 on success or zero on missing data. It may return an error in
1702 * h2c. Described in RFC7540#6.4.
1703 */
1704static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1705{
1706 int error;
1707
1708 if (h2c->dsi == 0) {
1709 error = H2_ERR_PROTOCOL_ERROR;
1710 goto conn_err;
1711 }
1712
Willy Tarreaucd234e92017-08-18 10:59:39 +02001713 if (h2c->dfl != 4) {
1714 error = H2_ERR_FRAME_SIZE_ERROR;
1715 goto conn_err;
1716 }
1717
1718 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001720 return 0;
1721
1722 /* late RST, already handled */
1723 if (h2s->st == H2_SS_CLOSED)
1724 return 1;
1725
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001726 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001727 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001728
1729 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001730 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001731 if (h2s->recv_wait) {
1732 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001733
1734 sw->wait_reason &= ~SUB_CAN_RECV;
1735 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001736 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001737 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001738 }
1739
1740 h2s->flags |= H2_SF_RST_RCVD;
1741 return 1;
1742
1743 conn_err:
1744 h2c_error(h2c, error);
1745 return 0;
1746}
1747
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001748/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1749 * It may return an error in h2c or h2s. The caller must consider that the
1750 * return value is the new h2s in case one was allocated (most common case).
1751 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001752 * errors here are reported as connection errors since it's impossible to
1753 * recover from such errors after the compression context has been altered.
1754 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001755static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001756{
1757 int error;
1758
1759 if (!h2c->dfl) {
1760 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001761 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001762 goto strm_err;
1763 }
1764
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001765 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001766 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001767
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001768 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001769 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001770
Willy Tarreauf2101912018-07-19 10:11:38 +02001771 if (h2c->flags & H2_CF_DEM_TOOMANY)
1772 return 0; // too many cs still present
1773
Willy Tarreau13278b42017-10-13 19:23:14 +02001774 /* now either the frame is complete or the buffer is complete */
1775 if (h2s->st != H2_SS_IDLE) {
1776 /* FIXME: stream already exists, this is only allowed for
1777 * trailers (not supported for now).
1778 */
1779 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001780 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001781 goto conn_err;
1782 }
1783 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1784 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1785 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001786 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001787 goto conn_err;
1788 }
1789
Willy Tarreau22de8d32018-09-05 19:55:58 +02001790 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001791 * positively from h2c_frt_stream_new(), the stream will report the error,
1792 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001793 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001794 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001795 if (!h2s) {
1796 error = H2_ERR_INTERNAL_ERROR;
1797 goto conn_err;
1798 }
1799
1800 h2s->st = H2_SS_OPEN;
1801 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1802 h2s->st = H2_SS_HREM;
1803 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001804 /* note: cs cannot be null for now (just created above) */
1805 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001806 }
1807
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001808 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001809 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001810
Willy Tarreau8f650c32017-11-21 19:36:21 +01001811 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001812 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001813
Willy Tarreau721c9742017-11-07 11:05:42 +01001814 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001815 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001816 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001817 }
1818 else {
1819 /* update the max stream ID if the request is being processed */
1820 if (h2s->id > h2c->max_id)
1821 h2c->max_id = h2s->id;
1822 }
1823
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001824 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001825
1826 conn_err:
1827 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001828 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001829
1830 strm_err:
1831 if (h2s) {
1832 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001833 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001834 }
1835 else
1836 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001837 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001838}
1839
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001840/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1841 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1842 * errors here are reported as connection errors since it's impossible to
1843 * recover from such errors after the compression context has been altered.
1844 */
1845static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1846{
1847 int error;
1848
1849 if (!h2c->dfl) {
1850 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1851 sess_log(h2c->conn->owner);
1852 goto strm_err;
1853 }
1854
1855 if (!b_size(&h2c->dbuf))
1856 return NULL; // empty buffer
1857
1858 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1859 return NULL; // incomplete frame
1860
1861 if (h2c->flags & H2_CF_DEM_TOOMANY)
1862 return 0; // too many cs still present
1863
1864 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1865 h2s->flags |= H2_SF_ES_RCVD;
1866 h2s->cs->flags |= CS_FL_REOS;
1867 }
1868
1869 if (!h2s_decode_headers(h2s))
1870 return NULL;
1871
1872 if (h2c->st0 >= H2_CS_ERROR)
1873 return NULL;
1874
1875 if (h2s->st >= H2_SS_ERROR) {
1876 /* stream error : send RST_STREAM */
1877 h2c->st0 = H2_CS_FRAME_E;
1878 }
1879
1880 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1881 h2s->st = H2_SS_ERROR;
1882 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1883 h2s->st = H2_SS_HREM;
1884 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1885 h2s_close(h2s);
1886
1887 return h2s;
1888
1889 conn_err:
1890 h2c_error(h2c, error);
1891 return NULL;
1892
1893 strm_err:
1894 if (h2s) {
1895 h2s_error(h2s, error);
1896 h2c->st0 = H2_CS_FRAME_E;
1897 }
1898 else
1899 h2c_error(h2c, error);
1900 return NULL;
1901}
1902
Willy Tarreau454f9052017-10-26 19:40:35 +02001903/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1904 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1905 */
1906static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1907{
1908 int error;
1909
1910 /* note that empty DATA frames are perfectly valid and sometimes used
1911 * to signal an end of stream (with the ES flag).
1912 */
1913
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001914 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001915 return 0; // empty buffer
1916
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001917 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001918 return 0; // incomplete frame
1919
1920 /* now either the frame is complete or the buffer is complete */
1921
1922 if (!h2c->dsi) {
1923 /* RFC7540#6.1 */
1924 error = H2_ERR_PROTOCOL_ERROR;
1925 goto conn_err;
1926 }
1927
1928 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1929 /* RFC7540#6.1 */
1930 error = H2_ERR_STREAM_CLOSED;
1931 goto strm_err;
1932 }
1933
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001934 if (!h2_frt_transfer_data(h2s))
1935 return 0;
1936
Willy Tarreau454f9052017-10-26 19:40:35 +02001937 /* call the upper layers to process the frame, then let the upper layer
1938 * notify the stream about any change.
1939 */
1940 if (!h2s->cs) {
1941 error = H2_ERR_STREAM_CLOSED;
1942 goto strm_err;
1943 }
1944
Willy Tarreau8f650c32017-11-21 19:36:21 +01001945 if (h2c->st0 >= H2_CS_ERROR)
1946 return 0;
1947
Willy Tarreau721c9742017-11-07 11:05:42 +01001948 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001949 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001950 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001951 }
1952
1953 /* check for completion : the callee will change this to FRAME_A or
1954 * FRAME_H once done.
1955 */
1956 if (h2c->st0 == H2_CS_FRAME_P)
1957 return 0;
1958
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001959
1960 /* last frame */
1961 if (h2c->dff & H2_F_DATA_END_STREAM) {
1962 h2s->st = H2_SS_HREM;
1963 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001964 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001965 }
1966
Willy Tarreau454f9052017-10-26 19:40:35 +02001967 return 1;
1968
1969 conn_err:
1970 h2c_error(h2c, error);
1971 return 0;
1972
1973 strm_err:
1974 if (h2s) {
1975 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001976 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001977 }
1978 else
1979 h2c_error(h2c, error);
1980 return 0;
1981}
1982
Willy Tarreaubc933932017-10-09 16:21:43 +02001983/* process Rx frames to be demultiplexed */
1984static void h2_process_demux(struct h2c *h2c)
1985{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001986 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001987
Willy Tarreau081d4722017-05-16 21:51:05 +02001988 if (h2c->st0 >= H2_CS_ERROR)
1989 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001990
1991 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1992 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001993 if (h2c->flags & H2_CF_IS_BACK)
1994 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001995 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1996 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001997 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001998 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001999 sess_log(h2c->conn->owner);
2000 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002001 goto fail;
2002 }
2003
2004 h2c->max_id = 0;
2005 h2c->st0 = H2_CS_SETTINGS1;
2006 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002007
2008 if (h2c->st0 == H2_CS_SETTINGS1) {
2009 struct h2_fh hdr;
2010
2011 /* ensure that what is pending is a valid SETTINGS frame
2012 * without an ACK.
2013 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002014 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002015 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002016 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002017 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002018 sess_log(h2c->conn->owner);
2019 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002020 goto fail;
2021 }
2022
2023 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2024 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2025 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2026 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002027 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002028 goto fail;
2029 }
2030
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002031 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002032 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2033 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2034 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002035 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002036 goto fail;
2037 }
2038
2039 /* that's OK, switch to FRAME_P to process it */
2040 h2c->dfl = hdr.len;
2041 h2c->dsi = hdr.sid;
2042 h2c->dft = hdr.ft;
2043 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002044 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002045 h2c->st0 = H2_CS_FRAME_P;
2046 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002047 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002048
2049 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002050 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002051 int ret = 0;
2052
2053 if (h2c->st0 >= H2_CS_ERROR)
2054 break;
2055
2056 if (h2c->st0 == H2_CS_FRAME_H) {
2057 struct h2_fh hdr;
2058
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002059 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002060 break;
2061
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002062 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002063 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2064 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002065 if (!h2c->nb_streams) {
2066 /* only log if no other stream can report the error */
2067 sess_log(h2c->conn->owner);
2068 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002069 break;
2070 }
2071
2072 h2c->dfl = hdr.len;
2073 h2c->dsi = hdr.sid;
2074 h2c->dft = hdr.ft;
2075 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002076 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002077 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002078 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002079 }
2080
2081 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002082 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2083
Olivier Houchard638b7992018-08-16 15:41:52 +02002084 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002085 /* we may have to signal the upper layers */
2086 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002087 if (h2s->recv_wait) {
2088 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2089 tasklet_wakeup(h2s->recv_wait->task);
2090 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002091 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002092 }
2093 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002094
Willy Tarreaud7901432017-12-29 11:34:40 +01002095 if (h2c->st0 == H2_CS_FRAME_E)
2096 goto strm_err;
2097
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002098 if (h2s->st == H2_SS_IDLE &&
2099 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2100 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2101 * this state MUST be treated as a connection error
2102 */
2103 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2104 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002105 if (!h2c->nb_streams) {
2106 /* only log if no other stream can report the error */
2107 sess_log(h2c->conn->owner);
2108 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002109 break;
2110 }
2111
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002112 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2113 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2114 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2115 * this state MUST be treated as a stream error
2116 */
2117 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2118 goto strm_err;
2119 }
2120
Willy Tarreauab837502017-12-27 15:07:30 +01002121 /* Below the management of frames received in closed state is a
2122 * bit hackish because the spec makes strong differences between
2123 * streams closed by receiving RST, sending RST, and seeing ES
2124 * in both directions. In addition to this, the creation of a
2125 * new stream reusing the identifier of a closed one will be
2126 * detected here. Given that we cannot keep track of all closed
2127 * streams forever, we consider that unknown closed streams were
2128 * closed on RST received, which allows us to respond with an
2129 * RST without breaking the connection (eg: to abort a transfer).
2130 * Some frames have to be silently ignored as well.
2131 */
2132 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2133 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2134 /* #5.1.1: The identifier of a newly
2135 * established stream MUST be numerically
2136 * greater than all streams that the initiating
2137 * endpoint has opened or reserved. This
2138 * governs streams that are opened using a
2139 * HEADERS frame and streams that are reserved
2140 * using PUSH_PROMISE. An endpoint that
2141 * receives an unexpected stream identifier
2142 * MUST respond with a connection error.
2143 */
2144 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2145 goto strm_err;
2146 }
2147
2148 if (h2s->flags & H2_SF_RST_RCVD) {
2149 /* RFC7540#5.1:closed: an endpoint that
2150 * receives any frame other than PRIORITY after
2151 * receiving a RST_STREAM MUST treat that as a
2152 * stream error of type STREAM_CLOSED.
2153 *
2154 * Note that old streams fall into this category
2155 * and will lead to an RST being sent.
2156 */
2157 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2158 h2c->st0 = H2_CS_FRAME_E;
2159 goto strm_err;
2160 }
2161
2162 /* RFC7540#5.1:closed: if this state is reached as a
2163 * result of sending a RST_STREAM frame, the peer that
2164 * receives the RST_STREAM might have already sent
2165 * frames on the stream that cannot be withdrawn. An
2166 * endpoint MUST ignore frames that it receives on
2167 * closed streams after it has sent a RST_STREAM
2168 * frame. An endpoint MAY choose to limit the period
2169 * over which it ignores frames and treat frames that
2170 * arrive after this time as being in error.
2171 */
2172 if (!(h2s->flags & H2_SF_RST_SENT)) {
2173 /* RFC7540#5.1:closed: any frame other than
2174 * PRIO/WU/RST in this state MUST be treated as
2175 * a connection error
2176 */
2177 if (h2c->dft != H2_FT_RST_STREAM &&
2178 h2c->dft != H2_FT_PRIORITY &&
2179 h2c->dft != H2_FT_WINDOW_UPDATE) {
2180 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2181 goto strm_err;
2182 }
2183 }
2184 }
2185
Willy Tarreauc0da1962017-10-30 18:38:00 +01002186#if 0
2187 // problem below: it is not possible to completely ignore such
2188 // streams as we need to maintain the compression state as well
2189 // and for this we need to completely process these frames (eg:
2190 // HEADERS frames) as well as counting DATA frames to emit
2191 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2192 // This is a typical case of layer violation where the
2193 // transported contents are critical to the connection's
2194 // validity and must be ignored at the same time :-(
2195
2196 /* graceful shutdown, ignore streams whose ID is higher than
2197 * the one advertised in GOAWAY. RFC7540#6.8.
2198 */
2199 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002200 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2201 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002202 h2c->dfl -= ret;
2203 ret = h2c->dfl == 0;
2204 goto strm_err;
2205 }
2206#endif
2207
Willy Tarreau7e98c052017-10-10 15:56:59 +02002208 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002209 case H2_FT_SETTINGS:
2210 if (h2c->st0 == H2_CS_FRAME_P)
2211 ret = h2c_handle_settings(h2c);
2212
2213 if (h2c->st0 == H2_CS_FRAME_A)
2214 ret = h2c_ack_settings(h2c);
2215 break;
2216
Willy Tarreaucf68c782017-10-10 17:11:41 +02002217 case H2_FT_PING:
2218 if (h2c->st0 == H2_CS_FRAME_P)
2219 ret = h2c_handle_ping(h2c);
2220
2221 if (h2c->st0 == H2_CS_FRAME_A)
2222 ret = h2c_ack_ping(h2c);
2223 break;
2224
Willy Tarreau26f95952017-07-27 17:18:30 +02002225 case H2_FT_WINDOW_UPDATE:
2226 if (h2c->st0 == H2_CS_FRAME_P)
2227 ret = h2c_handle_window_update(h2c, h2s);
2228 break;
2229
Willy Tarreau61290ec2017-10-17 08:19:21 +02002230 case H2_FT_CONTINUATION:
2231 /* we currently don't support CONTINUATION frames since
2232 * we have nowhere to store the partial HEADERS frame.
2233 * Let's abort the stream on an INTERNAL_ERROR here.
2234 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002235 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002236 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002237 h2c->st0 = H2_CS_FRAME_E;
2238 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002239 break;
2240
Willy Tarreau13278b42017-10-13 19:23:14 +02002241 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002242 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002243 if (h2c->flags & H2_CF_IS_BACK)
2244 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2245 else
2246 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002247 if (tmp_h2s) {
2248 h2s = tmp_h2s;
2249 ret = 1;
2250 }
2251 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002252 break;
2253
Willy Tarreau454f9052017-10-26 19:40:35 +02002254 case H2_FT_DATA:
2255 if (h2c->st0 == H2_CS_FRAME_P)
2256 ret = h2c_frt_handle_data(h2c, h2s);
2257
2258 if (h2c->st0 == H2_CS_FRAME_A)
2259 ret = h2c_send_strm_wu(h2c);
2260 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002261
Willy Tarreau92153fc2017-12-03 19:46:19 +01002262 case H2_FT_PRIORITY:
2263 if (h2c->st0 == H2_CS_FRAME_P)
2264 ret = h2c_handle_priority(h2c);
2265 break;
2266
Willy Tarreaucd234e92017-08-18 10:59:39 +02002267 case H2_FT_RST_STREAM:
2268 if (h2c->st0 == H2_CS_FRAME_P)
2269 ret = h2c_handle_rst_stream(h2c, h2s);
2270 break;
2271
Willy Tarreaue96b0922017-10-30 00:28:29 +01002272 case H2_FT_GOAWAY:
2273 if (h2c->st0 == H2_CS_FRAME_P)
2274 ret = h2c_handle_goaway(h2c);
2275 break;
2276
Willy Tarreau1c661982017-10-30 13:52:01 +01002277 case H2_FT_PUSH_PROMISE:
2278 /* not permitted here, RFC7540#5.1 */
2279 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002280 if (!h2c->nb_streams) {
2281 /* only log if no other stream can report the error */
2282 sess_log(h2c->conn->owner);
2283 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002284 break;
2285
2286 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002287 default:
2288 /* drop frames that we ignore. They may be larger than
2289 * the buffer so we drain all of their contents until
2290 * we reach the end.
2291 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002292 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2293 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002294 h2c->dfl -= ret;
2295 ret = h2c->dfl == 0;
2296 }
2297
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002298 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002299 /* We may have to send an RST if not done yet */
2300 if (h2s->st == H2_SS_ERROR)
2301 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002302
Willy Tarreaua20a5192017-12-27 11:02:06 +01002303 if (h2c->st0 == H2_CS_FRAME_E)
2304 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002305
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002307 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002308 break;
2309
2310 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002311 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002312 h2c->st0 = H2_CS_FRAME_H;
2313 }
2314 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002315
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002316 if (h2c->rcvd_c > 0 &&
2317 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2318 h2c_send_conn_wu(h2c);
2319
Willy Tarreau52eed752017-09-22 15:05:09 +02002320 fail:
2321 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002322 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002323 /* we may have to signal the upper layers */
2324 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002325 if (h2s->recv_wait) {
2326 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2327 tasklet_wakeup(h2s->recv_wait->task);
2328 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002329 }
2330 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002331
2332 if (h2_recv_allowed(h2c))
2333 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002334}
2335
2336/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2337 * the end.
2338 */
2339static int h2_process_mux(struct h2c *h2c)
2340{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002341 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002342
Willy Tarreau01b44822018-10-03 14:26:37 +02002343 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2344 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2345 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2346 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2347 if (h2c->st0 == H2_CS_ERROR) {
2348 h2c->st0 = H2_CS_ERROR2;
2349 sess_log(h2c->conn->owner);
2350 }
2351 goto fail;
2352 }
2353 h2c->st0 = H2_CS_SETTINGS1;
2354 }
2355 /* need to wait for the other side */
2356 if (h2c->st0 == H2_CS_SETTINGS1)
2357 return 1;
2358 }
2359
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002360 /* start by sending possibly pending window updates */
2361 if (h2c->rcvd_c > 0 &&
2362 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2363 h2c_send_conn_wu(h2c) < 0)
2364 goto fail;
2365
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002366 /* First we always process the flow control list because the streams
2367 * waiting there were already elected for immediate emission but were
2368 * blocked just on this.
2369 */
2370
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002371 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002372 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2373 h2c->st0 >= H2_CS_ERROR)
2374 break;
2375
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002376 h2s->flags &= ~H2_SF_BLK_ANY;
2377 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002378 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002379 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002380 LIST_DEL(&h2s->list);
2381 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002382 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002383 }
2384
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002385 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002386 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2387 break;
2388
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002389 h2s->flags &= ~H2_SF_BLK_ANY;
2390 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002391 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002392 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002393 LIST_DEL(&h2s->list);
2394 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002395 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002396 }
2397
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002398 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002399 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002400 if (h2c->st0 == H2_CS_ERROR) {
2401 if (h2c->max_id >= 0) {
2402 h2c_send_goaway_error(h2c, NULL);
2403 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2404 return 0;
2405 }
2406
2407 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2408 }
2409 return 1;
2410 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002411 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002412}
2413
Willy Tarreau62f52692017-10-08 23:01:42 +02002414
Willy Tarreau479998a2018-11-18 06:30:59 +01002415/* Attempt to read data, and subscribe if none available.
2416 * The function returns 1 if data has been received, otherwise zero.
2417 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002418static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002419{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002420 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002421 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002422 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002423 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002424
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002425 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002426 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002427
Willy Tarreau315d8072017-12-10 22:17:57 +01002428 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002429 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002430
Willy Tarreau44e973f2018-03-01 17:49:30 +01002431 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002432 if (!buf) {
2433 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002434 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002435 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002436
Olivier Houchard7505f942018-08-21 18:10:44 +02002437 do {
2438 max = buf->size - b_data(buf);
2439 if (max)
2440 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2441 else
2442 ret = 0;
2443 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002444
Olivier Houchard53216e72018-10-10 15:46:36 +02002445 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002446 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002447
Olivier Houcharda1411e62018-08-17 18:42:48 +02002448 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002449 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002450 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002451 }
2452
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002453 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002454 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002455 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002456}
2457
Willy Tarreau479998a2018-11-18 06:30:59 +01002458/* Try to send data if possible.
2459 * The function returns 1 if data have been sent, otherwise zero.
2460 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002461static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002462{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002463 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002464 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002465 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002466
2467 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002468 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002469
Olivier Houchard7505f942018-08-21 18:10:44 +02002470
Willy Tarreaua2af5122017-10-09 11:56:46 +02002471 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2472 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002473 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002474 }
2475
Willy Tarreaubc933932017-10-09 16:21:43 +02002476 /* This loop is quite simple : it tries to fill as much as it can from
2477 * pending streams into the existing buffer until it's reportedly full
2478 * or the end of send requests is reached. Then it tries to send this
2479 * buffer's contents out, marks it not full if at least one byte could
2480 * be sent, and tries again.
2481 *
2482 * The snd_buf() function normally takes a "flags" argument which may
2483 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2484 * data immediately comes and CO_SFL_STREAMER to indicate that the
2485 * connection is streaming lots of data (used to increase TLS record
2486 * size at the expense of latency). The former can be sent any time
2487 * there's a buffer full flag, as it indicates at least one stream
2488 * attempted to send and failed so there are pending data. An
2489 * alternative would be to set it as long as there's an active stream
2490 * but that would be problematic for ACKs until we have an absolute
2491 * guarantee that all waiters have at least one byte to send. The
2492 * latter should possibly not be set for now.
2493 */
2494
2495 done = 0;
2496 while (!done) {
2497 unsigned int flags = 0;
2498
2499 /* fill as much as we can into the current buffer */
2500 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2501 done = h2_process_mux(h2c);
2502
2503 if (conn->flags & CO_FL_ERROR)
2504 break;
2505
2506 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2507 flags |= CO_SFL_MSG_MORE;
2508
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002509 if (b_data(&h2c->mbuf)) {
2510 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002511 if (!ret)
2512 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002513 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 b_del(&h2c->mbuf, ret);
2515 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002516 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002517
2518 /* wrote at least one byte, the buffer is not full anymore */
2519 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2520 }
2521
Willy Tarreaua2af5122017-10-09 11:56:46 +02002522 if (conn->flags & CO_FL_SOCK_WR_SH) {
2523 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002525 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002526 /* We're not full anymore, so we can wake any task that are waiting
2527 * for us.
2528 */
2529 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002530 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002531 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2532 struct h2s *, list);
2533 LIST_DEL(&h2s->list);
2534 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002535 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002536 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002537 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002538 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002539 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002540 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002541 /* We're done, no more to send */
2542 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002543 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002544schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002545 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2546 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002547 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002548}
2549
2550static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2551{
2552 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002553 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002554
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002555 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002556 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002557 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002558 ret |= h2_recv(h2c);
2559 if (ret)
2560 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002561 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002562}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002563
Willy Tarreau62f52692017-10-08 23:01:42 +02002564/* callback called on any event by the connection handler.
2565 * It applies changes and returns zero, or < 0 if it wants immediate
2566 * destruction of the connection (which normally doesn not happen in h2).
2567 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002568static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002569{
Olivier Houchard7505f942018-08-21 18:10:44 +02002570 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002571
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002572 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002573 h2_process_demux(h2c);
2574
2575 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002576 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002577
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002578 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002579 h2c->flags &= ~H2_CF_DEM_DFULL;
2580 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002581 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002582
Willy Tarreau0b37d652018-10-03 10:33:02 +02002583 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002584 /* frontend is stopping, reload likely in progress, let's try
2585 * to announce a graceful shutdown if not yet done. We don't
2586 * care if it fails, it will be tried again later.
2587 */
2588 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2589 if (h2c->last_sid < 0)
2590 h2c->last_sid = (1U << 31) - 1;
2591 h2c_send_goaway_error(h2c, NULL);
2592 }
2593 }
2594
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002595 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002596 * If we received early data, and the handshake is done, wake
2597 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002598 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002599 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2600 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2601 struct eb32_node *node;
2602 struct h2s *h2s;
2603
2604 h2c->flags |= H2_CF_WAIT_FOR_HS;
2605 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2606
2607 while (node) {
2608 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002609 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002610 h2s->recv_wait) {
2611 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002612 sw->wait_reason &= ~SUB_CAN_RECV;
2613 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002614 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002615 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002616 node = eb32_next(node);
2617 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002618 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002619
Willy Tarreau26bd7612017-10-09 16:47:04 +02002620 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002621 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2622 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2623 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002624 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002625
2626 if (eb_is_empty(&h2c->streams_by_id)) {
2627 /* no more stream, kill the connection now */
2628 h2_release(conn);
2629 return -1;
2630 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002631 }
2632
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002633 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002634 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002635
Olivier Houchard53216e72018-10-10 15:46:36 +02002636 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2637 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2638 (h2c->st0 != H2_CS_ERROR &&
2639 !b_data(&h2c->mbuf) &&
2640 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2641 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002642 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002643
Willy Tarreau3f133572017-10-31 19:21:06 +01002644 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002645 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002646 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002647 task_queue(h2c->task);
2648 }
2649 else
2650 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002651 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002652
Olivier Houchard7505f942018-08-21 18:10:44 +02002653 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002654 return 0;
2655}
2656
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002657static int h2_wake(struct connection *conn)
2658{
2659 struct h2c *h2c = conn->mux_ctx;
2660
2661 return (h2_process(h2c));
2662}
2663
Willy Tarreauea392822017-10-31 10:02:25 +01002664/* Connection timeout management. The principle is that if there's no receipt
2665 * nor sending for a certain amount of time, the connection is closed. If the
2666 * MUX buffer still has lying data or is not allocatable, the connection is
2667 * immediately killed. If it's allocatable and empty, we attempt to send a
2668 * GOAWAY frame.
2669 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002670static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002671{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002672 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002673 int expired = tick_is_expired(t->expire, now_ms);
2674
Willy Tarreau0975f112018-03-29 15:22:59 +02002675 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002676 return t;
2677
Willy Tarreau0975f112018-03-29 15:22:59 +02002678 task_delete(t);
2679 task_free(t);
2680
2681 if (!h2c) {
2682 /* resources were already deleted */
2683 return NULL;
2684 }
2685
2686 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002687 h2c_error(h2c, H2_ERR_NO_ERROR);
2688 h2_wake_some_streams(h2c, 0, 0);
2689
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002690 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002691 /* don't even try to send a GOAWAY, the buffer is stuck */
2692 h2c->flags |= H2_CF_GOAWAY_FAILED;
2693 }
2694
2695 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002696 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002697 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2698 h2c->flags |= H2_CF_GOAWAY_FAILED;
2699
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002700 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2701 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002702 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002703 b_del(&h2c->mbuf, ret);
2704 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002705 }
2706 }
Willy Tarreauea392822017-10-31 10:02:25 +01002707
Willy Tarreau0975f112018-03-29 15:22:59 +02002708 /* either we can release everything now or it will be done later once
2709 * the last stream closes.
2710 */
2711 if (eb_is_empty(&h2c->streams_by_id))
2712 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002713
Willy Tarreauea392822017-10-31 10:02:25 +01002714 return NULL;
2715}
2716
2717
Willy Tarreau62f52692017-10-08 23:01:42 +02002718/*******************************************/
2719/* functions below are used by the streams */
2720/*******************************************/
2721
2722/*
2723 * Attach a new stream to a connection
2724 * (Used for outgoing connections)
2725 */
2726static struct conn_stream *h2_attach(struct connection *conn)
2727{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002728 struct conn_stream *cs;
2729 struct h2s *h2s;
2730 struct h2c *h2c = conn->mux_ctx;
2731
2732 cs = cs_new(conn);
2733 if (!cs)
2734 return NULL;
2735 h2s = h2c_bck_stream_new(h2c, cs);
2736 if (!h2s) {
2737 cs_free(cs);
2738 return NULL;
2739 }
2740 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002741}
2742
Willy Tarreaufafd3982018-11-18 21:29:20 +01002743/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2744 * We have to scan because we may have some orphan streams. It might be
2745 * beneficial to scan backwards from the end to reduce the likeliness to find
2746 * orphans.
2747 */
2748static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2749{
2750 struct h2c *h2c = conn->mux_ctx;
2751 struct h2s *h2s;
2752 struct eb32_node *node;
2753
2754 node = eb32_first(&h2c->streams_by_id);
2755 while (node) {
2756 h2s = container_of(node, struct h2s, by_id);
2757 if (h2s->cs)
2758 return h2s->cs;
2759 node = eb32_next(node);
2760 }
2761 return NULL;
2762}
2763
Willy Tarreau62f52692017-10-08 23:01:42 +02002764/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002765 * Destroy the mux and the associated connection, if it is no longer used
2766 */
2767static void h2_destroy(struct connection *conn)
2768{
2769 struct h2c *h2c = conn->mux_ctx;
2770
2771 if (eb_is_empty(&h2c->streams_by_id))
2772 h2_release(h2c->conn);
2773}
2774
2775/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002776 * Detach the stream from the connection and possibly release the connection.
2777 */
2778static void h2_detach(struct conn_stream *cs)
2779{
Willy Tarreau60935142017-10-16 18:11:19 +02002780 struct h2s *h2s = cs->ctx;
2781 struct h2c *h2c;
2782
2783 cs->ctx = NULL;
2784 if (!h2s)
2785 return;
2786
2787 h2c = h2s->h2c;
2788 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002789 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002790 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2791 !h2_has_too_many_cs(h2c)) {
2792 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002793 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002794 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002795 }
Willy Tarreau60935142017-10-16 18:11:19 +02002796
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002797 /* this stream may be blocked waiting for some data to leave (possibly
2798 * an ES or RST frame), so orphan it in this case.
2799 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002800 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002801 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002802 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002803 return;
2804
Willy Tarreau45f752e2017-10-30 15:44:59 +01002805 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2806 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2807 /* unblock the connection if it was blocked on this
2808 * stream.
2809 */
2810 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2811 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002812 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002813 }
2814
Willy Tarreau71049cc2018-03-28 13:56:39 +02002815 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002816
Willy Tarreaue323f342018-03-28 13:51:45 +02002817 /* We don't want to close right now unless we're removing the
2818 * last stream, and either the connection is in error, or it
2819 * reached the ID already specified in a GOAWAY frame received
2820 * or sent (as seen by last_sid >= 0).
2821 */
2822 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2823 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002824 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002825 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002826 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002827 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002828 (conn_xprt_read0_pending(h2c->conn) ||
2829 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2830 /* no more stream will come, kill it now */
2831 h2_release(h2c->conn);
2832 }
2833 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002835 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2836 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002837 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002838 else
2839 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002840 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002841}
2842
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002843static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002844{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002845 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002846 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002847
Willy Tarreau721c9742017-11-07 11:05:42 +01002848 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002849 return;
2850
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002851 /* if no outgoing data was seen on this stream, it means it was
2852 * closed with a "tcp-request content" rule that is normally
2853 * used to kill the connection ASAP (eg: limit abuse). In this
2854 * case we send a goaway to close the connection.
2855 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002856 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002857 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002858 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002859
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002860 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2861 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002862 h2c_send_goaway_error(h2c, h2s) <= 0)
2863 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002864
Willy Tarreau00dd0782018-03-01 16:31:34 +01002865 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002866
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002867 return;
2868add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002869 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002870 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002871 if (h2s->flags & H2_SF_BLK_MFCTL) {
2872 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2873 h2s->send_wait = sw;
2874 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2875 h2s->send_wait = sw;
2876 LIST_ADDQ(&h2c->send_list, &h2s->list);
2877 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002878 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002879 /* Let the handler know we want shutr */
2880 sw->handle = (void *)((long)sw->handle | 1);
2881
Willy Tarreau62f52692017-10-08 23:01:42 +02002882}
2883
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002884static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002885{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002886 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002887 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002888
Willy Tarreau721c9742017-11-07 11:05:42 +01002889 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002890 return;
2891
Willy Tarreau67434202017-11-06 20:20:51 +01002892 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002893 /* we can cleanly close using an empty data frame only after headers */
2894
2895 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2896 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002897 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002898
2899 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002900 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002901 else
2902 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002903 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002904 /* if no outgoing data was seen on this stream, it means it was
2905 * closed with a "tcp-request content" rule that is normally
2906 * used to kill the connection ASAP (eg: limit abuse). In this
2907 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002908 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002909 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002910 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002911 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002912
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002913 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2914 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002915 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002916 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002917
Willy Tarreau00dd0782018-03-01 16:31:34 +01002918 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002919 }
2920
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002921
2922 add_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 Houchardfa8aa862018-10-10 18:25:41 +02002933 /* let the handler know we want to shutw */
2934 sw->handle = (void *)((long)(sw->handle) | 2);
2935
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002936}
2937
2938static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2939{
2940 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002941 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002942
2943 if (reason & 1)
2944 h2_do_shutr(h2s);
2945 if (reason & 2)
2946 h2_do_shutw(h2s);
2947
2948 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002949}
2950
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002951static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2952{
2953 struct h2s *h2s = cs->ctx;
2954
2955 if (!mode)
2956 return;
2957
2958 h2_do_shutr(h2s);
2959}
2960
2961static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2962{
2963 struct h2s *h2s = cs->ctx;
2964
2965 h2_do_shutw(h2s);
2966}
2967
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002968/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002969 * HTX request or response depending on the connection's side. Returns the
2970 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
2971 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002972 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002973static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002974{
2975 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002976 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002977 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002978 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002979 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002980 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002981 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002982 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002983 int flen = h2c->dfl;
2984 int outlen = 0;
2985 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002986 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002987
2988 if (!h2c->dfl) {
2989 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002990 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002991 return 0;
2992 }
2993
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002994 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002995 return 0; // incomplete input frame
2996
Willy Tarreau13278b42017-10-13 19:23:14 +02002997 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002998 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002999 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003000 copy = alloc_trash_chunk();
3001 if (!copy) {
3002 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3003 goto fail;
3004 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3006 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3007 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003008 }
3009
3010 /* The padlen is the first byte before data, and the padding appears
3011 * after data. padlen+data+padding are included in flen.
3012 */
3013 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003014 h2c->dpl = *hdrs;
3015 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003016 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3017 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003018 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003019 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003020 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003021 hdrs += 1; // skip Pad Length
3022 }
3023
3024 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3025 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003026 if (read_n32(hdrs) == h2s->id) {
3027 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3028 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003029 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003030 }
3031
Willy Tarreau13278b42017-10-13 19:23:14 +02003032 hdrs += 5; // stream dep = 4, weight = 1
3033 flen -= 5;
3034 }
3035
3036 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3037 * don't support this for now and can't even decompress so we have to
3038 * break the connection.
3039 */
3040 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3041 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003042 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003043 }
3044
Olivier Houchard638b7992018-08-16 15:41:52 +02003045 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003046 if (!csbuf) {
3047 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003048 goto fail;
3049 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003050
Willy Tarreau937f7602018-02-26 15:22:17 +01003051 /* we can't retry a failed decompression operation so we must be very
3052 * careful not to take any risks. In practice the output buffer is
3053 * always empty except maybe for trailers, in which case we simply have
3054 * to wait for the upper layer to finish consuming what is available.
3055 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003056
3057 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3058 htx = htx_from_buf(&h2s->rxbuf);
3059 if (!htx_is_empty(htx))
3060 goto fail;
3061 } else {
3062 if (b_data(csbuf))
3063 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003064
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003065 csbuf->head = 0;
3066 try = b_size(csbuf);
3067 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003068
3069 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3070 sizeof(list)/sizeof(list[0]), tmp);
3071 if (outlen < 0) {
3072 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3073 goto fail;
3074 }
3075
3076 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003077 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003078
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003079 if (htx) {
3080 /* HTX mode */
3081 if (h2c->flags & H2_CF_IS_BACK)
3082 outlen = h2_make_htx_response(list, htx, &msgf);
3083 else
3084 outlen = h2_make_htx_request(list, htx, &msgf);
3085 } else {
3086 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003087 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003088 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003089
3090 if (outlen < 0) {
3091 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3092 goto fail;
3093 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003094
Willy Tarreau174b06a2018-04-25 18:13:58 +02003095 if (msgf & H2_MSGF_BODY) {
3096 /* a payload is present */
3097 if (msgf & H2_MSGF_BODY_CL)
3098 h2s->flags |= H2_SF_DATA_CLEN;
3099 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
3100 h2s->flags |= H2_SF_DATA_CHNK;
3101 }
3102
Willy Tarreau13278b42017-10-13 19:23:14 +02003103 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003104 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003105 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003106 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003107
Willy Tarreau39d68502018-03-02 12:26:37 +01003108 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003109 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003110 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003111 if (htx)
3112 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003113 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003114
Willy Tarreau68dd9852017-07-03 14:44:26 +02003115 leave:
3116 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003117 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003118 fail:
3119 outlen = 0;
3120 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003121}
3122
Willy Tarreau454f9052017-10-26 19:40:35 +02003123/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3124 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3125 * in use, a new chunk is emitted for each frame. This is supposed to fit
3126 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3127 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3128 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003129 * parser state is automatically updated. Returns > 0 if it could completely
3130 * send the current frame, 0 if it couldn't complete, in which case
3131 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3132 * DATA frame can return 0 as a valid result). Stream errors are reported in
3133 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3134 * have checked the frame header and ensured that the frame was complete or the
3135 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003136 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003137static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003138{
3139 struct h2c *h2c = h2s->h2c;
3140 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003141 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003142 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003143 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003144 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003145
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003146 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003147
3148 /* The padlen is the first byte before data, and the padding appears
3149 * after data. padlen+data+padding are included in flen.
3150 */
Willy Tarreau79127812017-12-03 21:06:59 +01003151 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003152 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003153 return 0;
3154
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003155 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003156 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003157 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3158 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003159 return 0;
3160 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003161
3162 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003163 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003164 h2c->dfl--;
3165 h2c->rcvd_c++; h2c->rcvd_s++;
3166 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003167 }
3168
Olivier Houchard638b7992018-08-16 15:41:52 +02003169 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003170 if (!csbuf) {
3171 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003172 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003173 }
3174
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003175try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003176 flen = h2c->dfl - h2c->dpl;
3177 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003178 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003179
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003180 if (flen > b_data(&h2c->dbuf)) {
3181 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003182 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003183 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003184 }
3185
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003186 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3187 htx = htx_from_buf(csbuf);
3188 block1 = htx_free_data_space(htx);
3189 if (!block1) {
3190 h2c->flags |= H2_CF_DEM_SFULL;
3191 goto fail;
3192 }
3193 if (flen > block1)
3194 flen = block1;
3195
3196 /* here, flen is the max we can copy into the output buffer */
3197 block1 = b_contig_data(&h2c->dbuf, 0);
3198 if (flen > block1)
3199 flen = block1;
3200
3201 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3202 h2c->flags |= H2_CF_DEM_SFULL;
3203 goto fail;
3204 }
3205
3206 b_del(&h2c->dbuf, flen);
3207 h2c->dfl -= flen;
3208 h2c->rcvd_c += flen;
3209 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3210 goto try_again;
3211 }
3212 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003213 /* it doesn't fit and the buffer is fragmented,
3214 * so let's defragment it and try again.
3215 */
3216 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003217 }
3218
Willy Tarreaueba10f22018-04-25 20:44:22 +02003219 /* chunked-encoding requires more room */
3220 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003221 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003222 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3223 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3224 (chklen < 1048576) ? 4 : 8;
3225 chklen += 4; // CRLF, CRLF
3226 }
3227
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003228 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003229 if (flen + chklen > b_room(csbuf)) {
3230 if (chklen >= b_room(csbuf)) {
3231 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003232 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003233 }
3234 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003235 }
3236
3237 if (h2s->flags & H2_SF_DATA_CHNK) {
3238 /* emit the chunk size */
3239 unsigned int chksz = flen;
3240 char str[10];
3241 char *beg;
3242
3243 beg = str + sizeof(str);
3244 *--beg = '\n';
3245 *--beg = '\r';
3246 do {
3247 *--beg = hextab[chksz & 0xF];
3248 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003249 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003250 }
3251
Willy Tarreau454f9052017-10-26 19:40:35 +02003252 /* Block1 is the length of the first block before the buffer wraps,
3253 * block2 is the optional second block to reach the end of the frame.
3254 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003255 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003256 if (block1 > flen)
3257 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003258 block2 = flen - block1;
3259
3260 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003261 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003262
3263 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003264 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003265
Willy Tarreaueba10f22018-04-25 20:44:22 +02003266 if (h2s->flags & H2_SF_DATA_CHNK) {
3267 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003268 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003269 }
3270
Willy Tarreau454f9052017-10-26 19:40:35 +02003271 /* now mark the input data as consumed (will be deleted from the buffer
3272 * by the caller when seeing FRAME_A after sending the window update).
3273 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003274 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003275 h2c->dfl -= flen;
3276 h2c->rcvd_c += flen;
3277 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3278
3279 if (h2c->dfl > h2c->dpl) {
3280 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003281 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003282 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003283 }
3284
Willy Tarreau4a28da12018-01-04 14:41:00 +01003285 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003286 /* here we're done with the frame, all the payload (except padding) was
3287 * transferred.
3288 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003289
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003290 if (h2c->dff & H2_F_DATA_END_STREAM) {
3291 if (htx) {
3292 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3293 h2c->flags |= H2_CF_DEM_SFULL;
3294 goto fail;
3295 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003296 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003297 else if (h2s->flags & H2_SF_DATA_CHNK) {
3298 /* emit the trailing 0 CRLF CRLF */
3299 if (b_room(csbuf) < 5) {
3300 h2c->flags |= H2_CF_DEM_SFULL;
3301 goto fail;
3302 }
3303 chklen += 5;
3304 b_putblk(csbuf, "0\r\n\r\n", 5);
3305 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003306 }
3307
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003308 h2c->rcvd_c += h2c->dpl;
3309 h2c->rcvd_s += h2c->dpl;
3310 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003311 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3312
Willy Tarreau39d68502018-03-02 12:26:37 +01003313 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003314 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003315 h2s->cs->flags |= CS_FL_REOS;
3316 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003317
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003318 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003319 fail:
3320 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003321}
3322
Willy Tarreau5dd17352018-06-14 13:33:30 +02003323/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3324 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3325 * number of bytes sent. The caller must check the stream's status to detect
3326 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003327 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003328static 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 +02003329{
3330 struct http_hdr list[MAX_HTTP_HDR];
3331 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003332 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003333 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003334 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003335 int es_now = 0;
3336 int ret = 0;
3337 int hdr;
3338
3339 if (h2c_mux_busy(h2c, h2s)) {
3340 h2s->flags |= H2_SF_BLK_MBUSY;
3341 return 0;
3342 }
3343
Willy Tarreau44e973f2018-03-01 17:49:30 +01003344 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003345 h2c->flags |= H2_CF_MUX_MALLOC;
3346 h2s->flags |= H2_SF_BLK_MROOM;
3347 return 0;
3348 }
3349
3350 /* First, try to parse the H1 response and index it into <list>.
3351 * NOTE! Since it comes from haproxy, we *know* that a response header
3352 * block does not wrap and we can safely read it this way without
3353 * having to realign the buffer.
3354 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003355 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003356 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003357 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003358 /* incomplete or invalid response, this is abnormal coming from
3359 * haproxy and may only result in a bad errorfile or bad Lua code
3360 * so that won't be fixed, raise an error now.
3361 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003362 * FIXME: we should instead add the ability to only return a
3363 * 502 bad gateway. But in theory this is not supposed to
3364 * happen.
3365 */
3366 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3367 ret = 0;
3368 goto end;
3369 }
3370
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003371 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003372
3373 /* certain statuses have no body or an empty one, regardless of
3374 * what the headers say.
3375 */
3376 if (sl.st.status >= 100 && sl.st.status < 200) {
3377 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3378 h1m->curr_len = h1m->body_len = 0;
3379 }
3380 else if (sl.st.status == 204 || sl.st.status == 304) {
3381 /* no contents, claim c-len is present and set to zero */
3382 h1m->flags &= ~H1_MF_CHNK;
3383 h1m->flags |= H1_MF_CLEN;
3384 h1m->curr_len = h1m->body_len = 0;
3385 }
3386
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003387 chunk_reset(&outbuf);
3388
3389 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003390 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003391 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003392 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003393
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003394 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003395 break;
3396 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003397 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003398 }
3399
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003400 if (outbuf.size < 9)
3401 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003402
3403 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003404 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3405 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3406 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003407
3408 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003409 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003410 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003411 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003412 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003413 else if (unlikely(list[0].v.len != 3)) {
3414 /* this is an unparsable response */
3415 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3416 ret = 0;
3417 goto end;
3418 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003419 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003420 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003421 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3422 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3423 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3424 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3425 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003426 }
3427 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003428 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003429 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003430 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003431 }
3432
3433 /* encode all headers, stop at empty name */
3434 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003435 /* these ones do not exist in H2 and must be dropped. */
3436 if (isteq(list[hdr].n, ist("connection")) ||
3437 isteq(list[hdr].n, ist("proxy-connection")) ||
3438 isteq(list[hdr].n, ist("keep-alive")) ||
3439 isteq(list[hdr].n, ist("upgrade")) ||
3440 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003441 continue;
3442
3443 if (isteq(list[hdr].n, ist("")))
3444 break; // end
3445
3446 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3447 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003448 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003449 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003450 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003451 }
3452 }
3453
3454 /* we may need to add END_STREAM */
3455 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3456 es_now = 1;
3457
3458 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003459 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003460
3461 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003462 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003463
3464 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003465 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003466
3467 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003468 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003469 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003470
3471 /* for now we don't implemented CONTINUATION, so we wait for a
3472 * body or directly end in TRL2.
3473 */
3474 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003475 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003476 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003477
Willy Tarreau801250e2018-09-11 11:45:04 +02003478 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003479 h2s->flags |= H2_SF_ES_SENT;
3480 if (h2s->st == H2_SS_OPEN)
3481 h2s->st = H2_SS_HLOC;
3482 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003483 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003484 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003485 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003486 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003487 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003488 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003489 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003490 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003491 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003492
3493 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003494
3495 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003496 //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 +02003497 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003498 full:
3499 h1m_init_res(h1m);
3500 h1m->err_pos = -1; // don't care about errors on the response path
3501 h2c->flags |= H2_CF_MUX_MFULL;
3502 h2s->flags |= H2_SF_BLK_MROOM;
3503 ret = 0;
3504 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003505}
3506
Willy Tarreau5dd17352018-06-14 13:33:30 +02003507/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3508 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3509 * the number of bytes sent. The caller must check the stream's status to
3510 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003511 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003512static 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 +02003513{
3514 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003515 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003516 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003517 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003518 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003519 int es_now = 0;
3520 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003521 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003522 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523
3524 if (h2c_mux_busy(h2c, h2s)) {
3525 h2s->flags |= H2_SF_BLK_MBUSY;
3526 goto end;
3527 }
3528
Willy Tarreau44e973f2018-03-01 17:49:30 +01003529 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003530 h2c->flags |= H2_CF_MUX_MALLOC;
3531 h2s->flags |= H2_SF_BLK_MROOM;
3532 goto end;
3533 }
3534
3535 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003536 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003537 goto end;
3538
3539 chunk_reset(&outbuf);
3540
3541 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003542 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003543 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003544 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003545
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003546 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003547 break;
3548 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003549 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003550 }
3551
3552 if (outbuf.size < 9) {
3553 h2c->flags |= H2_CF_MUX_MFULL;
3554 h2s->flags |= H2_SF_BLK_MROOM;
3555 goto end;
3556 }
3557
3558 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003559 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3560 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3561 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003562
3563 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3564 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003565 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003566 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003567 break;
3568 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003569 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003570 if ((long long)size > h1m->curr_len)
3571 size = h1m->curr_len;
3572 break;
3573 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003574 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003575 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003576 if (!ret)
3577 goto end;
3578
3579 if (ret < 0) {
3580 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003581 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003582 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3583 goto end;
3584 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003585 max -= ret;
3586 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003587 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003588 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003589 }
3590
Willy Tarreau801250e2018-09-11 11:45:04 +02003591 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003592 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003593 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003594 if (!ret)
3595 goto end;
3596
3597 if (ret < 0) {
3598 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003599 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003600 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3601 goto end;
3602 }
3603
3604 size = chunk;
3605 h1m->curr_len = chunk;
3606 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003607 max -= ret;
3608 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003609 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003610 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003611 if (!size)
3612 goto send_empty;
3613 }
3614
3615 /* in MSG_DATA state, continue below */
3616 size = h1m->curr_len;
3617 break;
3618 }
3619
3620 /* we have in <size> the exact number of bytes we need to copy from
3621 * the H1 buffer. We need to check this against the connection's and
3622 * the stream's send windows, and to ensure that this fits in the max
3623 * frame size and in the buffer's available space minus 9 bytes (for
3624 * the frame header). The connection's flow control is applied last so
3625 * that we can use a separate list of streams which are immediately
3626 * unblocked on window opening. Note: we don't implement padding.
3627 */
3628
Willy Tarreau5dd17352018-06-14 13:33:30 +02003629 if (size > max)
3630 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003631
3632 if (size > h2s->mws)
3633 size = h2s->mws;
3634
3635 if (size <= 0) {
3636 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003637 if (h2s->send_wait) {
3638 LIST_DEL(&h2s->list);
3639 LIST_INIT(&h2s->list);
3640 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003641 goto end;
3642 }
3643
3644 if (h2c->mfs && size > h2c->mfs)
3645 size = h2c->mfs;
3646
3647 if (size + 9 > outbuf.size) {
3648 /* we have an opportunity for enlarging the too small
3649 * available space, let's try.
3650 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003651 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003652 goto realign_again;
3653 size = outbuf.size - 9;
3654 }
3655
3656 if (size <= 0) {
3657 h2c->flags |= H2_CF_MUX_MFULL;
3658 h2s->flags |= H2_SF_BLK_MROOM;
3659 goto end;
3660 }
3661
3662 if (size > h2c->mws)
3663 size = h2c->mws;
3664
3665 if (size <= 0) {
3666 h2s->flags |= H2_SF_BLK_MFCTL;
3667 goto end;
3668 }
3669
3670 /* copy whatever we can */
3671 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003672 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003673 if (ret == 1)
3674 len2 = 0;
3675
3676 if (!ret || len1 + len2 < size) {
3677 /* FIXME: must normally never happen */
3678 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3679 goto end;
3680 }
3681
3682 /* limit len1/len2 to size */
3683 if (len1 + len2 > size) {
3684 int sub = len1 + len2 - size;
3685
3686 if (len2 > sub)
3687 len2 -= sub;
3688 else {
3689 sub -= len2;
3690 len2 = 0;
3691 len1 -= sub;
3692 }
3693 }
3694
3695 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003696 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003697 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003698 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003699
3700 send_empty:
3701 /* we may need to add END_STREAM */
3702 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3703 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003704 *
3705 * FIXME: what we do here is not correct because we send end_stream
3706 * before knowing if we'll have to send a HEADERS frame for the
3707 * trailers. More importantly we're not consuming the trailing CRLF
3708 * after the end of trailers, so it will be left to the caller to
3709 * eat it. The right way to do it would be to measure trailers here
3710 * and to send ES only if there are no trailers.
3711 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003712 */
3713 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003714 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003715 es_now = 1;
3716
3717 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003718 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003719
3720 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003721 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003722
3723 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003724 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003725
3726 /* consume incoming H1 response */
3727 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003728 max -= size;
3729 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003730 total += size;
3731 h1m->curr_len -= size;
3732 h2s->mws -= size;
3733 h2c->mws -= size;
3734
3735 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003736 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003737 goto new_frame;
3738 }
3739 }
3740
3741 if (es_now) {
3742 if (h2s->st == H2_SS_OPEN)
3743 h2s->st = H2_SS_HLOC;
3744 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003745 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003746
Willy Tarreau35a62702018-02-27 15:37:25 +01003747 if (!(h1m->flags & H1_MF_CHNK)) {
3748 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003749 total += max;
3750 ofs += max;
3751 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003752
Willy Tarreau801250e2018-09-11 11:45:04 +02003753 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003754 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003755
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003756 h2s->flags |= H2_SF_ES_SENT;
3757 }
3758
3759 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003760 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 +02003761 return total;
3762}
3763
Willy Tarreau115e83b2018-12-01 19:17:53 +01003764/* Try to send a HEADERS frame matching HTX response present in HTX message
3765 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3766 * must check the stream's status to detect any error which might have happened
3767 * subsequently to a successful send. The htx blocks are automatically removed
3768 * from the message. The htx message is assumed to be valid since produced from
3769 * the internal code, hence it contains a start line, an optional series of
3770 * header blocks and an end of header, otherwise an invalid frame could be
3771 * emitted and the resulting htx message could be left in an inconsistent state.
3772 */
3773static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3774{
3775 struct http_hdr list[MAX_HTTP_HDR];
3776 struct h2c *h2c = h2s->h2c;
3777 struct htx_blk *blk;
3778 struct htx_blk *blk_end;
3779 struct buffer outbuf;
3780 struct htx_sl *sl;
3781 enum htx_blk_type type;
3782 int es_now = 0;
3783 int ret = 0;
3784 int hdr;
3785 int idx;
3786
3787 if (h2c_mux_busy(h2c, h2s)) {
3788 h2s->flags |= H2_SF_BLK_MBUSY;
3789 return 0;
3790 }
3791
3792 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3793 h2c->flags |= H2_CF_MUX_MALLOC;
3794 h2s->flags |= H2_SF_BLK_MROOM;
3795 return 0;
3796 }
3797
3798 /* determine the first block which must not be deleted, blk_end may
3799 * be NULL if all blocks have to be deleted.
3800 */
3801 idx = htx_get_head(htx);
3802 blk_end = NULL;
3803 while (idx != -1) {
3804 type = htx_get_blk_type(htx_get_blk(htx, idx));
3805 idx = htx_get_next(htx, idx);
3806 if (type == HTX_BLK_EOH) {
3807 if (idx != -1)
3808 blk_end = htx_get_blk(htx, idx);
3809 break;
3810 }
3811 }
3812
3813 /* get the start line, we do have one */
3814 blk = htx_get_blk(htx, htx->sl_off);
3815 sl = htx_get_blk_ptr(htx, blk);
3816 h2s->status = sl->info.res.status;
3817
3818 /* and the rest of the headers, that we dump starting at header 0 */
3819 hdr = 0;
3820
Willy Tarreaufab9bb02018-12-02 12:11:16 +01003821 idx = htx->sl_off;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003822 while ((idx = htx_get_next(htx, idx)) != -1) {
3823 blk = htx_get_blk(htx, idx);
3824 type = htx_get_blk_type(blk);
3825
3826 if (type == HTX_BLK_UNUSED)
3827 continue;
3828
3829 if (type != HTX_BLK_HDR)
3830 break;
3831
3832 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3833 goto fail;
3834
3835 list[hdr].n = htx_get_blk_name(htx, blk);
3836 list[hdr].v = htx_get_blk_value(htx, blk);
3837
3838#if 1
3839 {
3840 /* FIXME: header names MUST be lower case in H2. For now it's
3841 * not granted by HTX so let's force them now.
3842 */
3843 char *p;
3844 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
3845 if (unlikely(isupper(*p)))
3846 *p = tolower(*p);
3847 }
3848#endif
3849 hdr++;
3850 }
3851
3852 /* marker for end of headers */
3853 list[hdr].n = ist("");
3854
3855 if (h2s->status == 204 || h2s->status == 304) {
3856 /* no contents, claim c-len is present and set to zero */
3857 es_now = 1;
3858 }
3859
3860 chunk_reset(&outbuf);
3861
3862 while (1) {
3863 outbuf.area = b_tail(&h2c->mbuf);
3864 outbuf.size = b_contig_space(&h2c->mbuf);
3865 outbuf.data = 0;
3866
3867 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3868 break;
3869 realign_again:
3870 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3871 }
3872
3873 if (outbuf.size < 9)
3874 goto full;
3875
3876 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3877 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3878 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3879 outbuf.data = 9;
3880
3881 /* encode status, which necessarily is the first one */
3882 if (outbuf.data < outbuf.size && h2s->status == 200)
3883 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3884 else if (outbuf.data < outbuf.size && h2s->status == 304)
3885 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
3886 else if (unlikely(h2s->status < 100 || h2s->status > 999)) {
3887 /* this is an unparsable response */
3888 goto fail;
3889 }
3890 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
3891 /* basic encoding of the status code */
3892 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3893 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3894 outbuf.area[outbuf.data++] = '0' + h2s->status / 100;
3895 outbuf.area[outbuf.data++] = '0' + h2s->status / 10 % 10;
3896 outbuf.area[outbuf.data++] = '0' + h2s->status % 10;
3897 }
3898 else {
3899 if (b_space_wraps(&h2c->mbuf))
3900 goto realign_again;
3901 goto full;
3902 }
3903
3904 /* encode all headers, stop at empty name */
3905 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3906 /* these ones do not exist in H2 and must be dropped. */
3907 if (isteq(list[hdr].n, ist("connection")) ||
3908 isteq(list[hdr].n, ist("proxy-connection")) ||
3909 isteq(list[hdr].n, ist("keep-alive")) ||
3910 isteq(list[hdr].n, ist("upgrade")) ||
3911 isteq(list[hdr].n, ist("transfer-encoding")))
3912 continue;
3913
3914 if (isteq(list[hdr].n, ist("")))
3915 break; // end
3916
3917 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3918 /* output full */
3919 if (b_space_wraps(&h2c->mbuf))
3920 goto realign_again;
3921 goto full;
3922 }
3923 }
3924
3925 /* we may need to add END_STREAM.
3926 * FIXME: we should also set it when we know for sure that the
3927 * content-length is zero as well as on 204/304
3928 */
3929 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3930 es_now = 1;
3931
3932 if (h2s->cs->flags & CS_FL_SHW)
3933 es_now = 1;
3934
3935 /* update the frame's size */
3936 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3937
3938 if (es_now)
3939 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3940
3941 /* commit the H2 response */
3942 b_add(&h2c->mbuf, outbuf.data);
3943 h2s->flags |= H2_SF_HEADERS_SENT;
3944
3945 /* for now we don't implemented CONTINUATION, so we wait for a
3946 * body or directly end in TRL2.
3947 */
3948 if (es_now) {
3949 h2s->flags |= H2_SF_ES_SENT;
3950 if (h2s->st == H2_SS_OPEN)
3951 h2s->st = H2_SS_HLOC;
3952 else
3953 h2s_close(h2s);
3954 }
3955
3956 /* OK we could properly deliver the response */
3957
3958 /* remove all header blocks including the EOH and compute the
3959 * corresponding size.
3960 *
3961 * FIXME: We should remove everything when es_now is set.
3962 */
3963 ret = 0;
3964 idx = htx_get_head(htx);
3965 blk = htx_get_blk(htx, idx);
3966 while (blk != blk_end) {
3967 ret += htx_get_blksz(blk);
3968 blk = htx_remove_blk(htx, blk);
3969 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003970
3971 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3972 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003973 end:
3974 return ret;
3975 full:
3976 h2c->flags |= H2_CF_MUX_MFULL;
3977 h2s->flags |= H2_SF_BLK_MROOM;
3978 ret = 0;
3979 goto end;
3980 fail:
3981 /* unparsable HTX messages, too large ones to be produced in the local
3982 * list etc go here (unrecoverable errors).
3983 */
3984 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3985 ret = 0;
3986 goto end;
3987}
3988
Willy Tarreau80739692018-10-05 11:35:57 +02003989/* Try to send a HEADERS frame matching HTX request present in HTX message
3990 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3991 * must check the stream's status to detect any error which might have happened
3992 * subsequently to a successful send. The htx blocks are automatically removed
3993 * from the message. The htx message is assumed to be valid since produced from
3994 * the internal code, hence it contains a start line, an optional series of
3995 * header blocks and an end of header, otherwise an invalid frame could be
3996 * emitted and the resulting htx message could be left in an inconsistent state.
3997 */
3998static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
3999{
4000 struct http_hdr list[MAX_HTTP_HDR];
4001 struct h2c *h2c = h2s->h2c;
4002 struct htx_blk *blk;
4003 struct htx_blk *blk_end;
4004 struct buffer outbuf;
4005 struct htx_sl *sl;
4006 struct ist meth, path;
4007 enum htx_blk_type type;
4008 int es_now = 0;
4009 int ret = 0;
4010 int hdr;
4011 int idx;
4012
4013 if (h2c_mux_busy(h2c, h2s)) {
4014 h2s->flags |= H2_SF_BLK_MBUSY;
4015 return 0;
4016 }
4017
4018 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4019 h2c->flags |= H2_CF_MUX_MALLOC;
4020 h2s->flags |= H2_SF_BLK_MROOM;
4021 return 0;
4022 }
4023
4024 /* determine the first block which must not be deleted, blk_end may
4025 * be NULL if all blocks have to be deleted.
4026 */
4027 idx = htx_get_head(htx);
4028 blk_end = NULL;
4029 while (idx != -1) {
4030 type = htx_get_blk_type(htx_get_blk(htx, idx));
4031 idx = htx_get_next(htx, idx);
4032 if (type == HTX_BLK_EOH) {
4033 if (idx != -1)
4034 blk_end = htx_get_blk(htx, idx);
4035 break;
4036 }
4037 }
4038
4039 /* get the start line, we do have one */
4040 blk = htx_get_blk(htx, htx->sl_off);
4041 sl = htx_get_blk_ptr(htx, blk);
4042 meth = htx_sl_req_meth(sl);
4043 path = htx_sl_req_uri(sl);
4044
4045 /* and the rest of the headers, that we dump starting at header 0 */
4046 hdr = 0;
4047
4048 idx = htx->sl_off;
4049 while ((idx = htx_get_next(htx, idx)) != -1) {
4050 blk = htx_get_blk(htx, idx);
4051 type = htx_get_blk_type(blk);
4052
4053 if (type == HTX_BLK_UNUSED)
4054 continue;
4055
4056 if (type != HTX_BLK_HDR)
4057 break;
4058
4059 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4060 goto fail;
4061
4062 list[hdr].n = htx_get_blk_name(htx, blk);
4063 list[hdr].v = htx_get_blk_value(htx, blk);
4064
4065#if 1
4066 {
4067 /* FIXME: header names MUST be lower case in H2. For now it's
4068 * not granted by HTX so let's force them now.
4069 */
4070 char *p;
4071 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
4072 if (unlikely(isupper(*p)))
4073 *p = tolower(*p);
4074 }
4075#endif
4076 hdr++;
4077 }
4078
4079 /* marker for end of headers */
4080 list[hdr].n = ist("");
4081
4082 chunk_reset(&outbuf);
4083
4084 while (1) {
4085 outbuf.area = b_tail(&h2c->mbuf);
4086 outbuf.size = b_contig_space(&h2c->mbuf);
4087 outbuf.data = 0;
4088
4089 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4090 break;
4091 realign_again:
4092 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4093 }
4094
4095 if (outbuf.size < 9)
4096 goto full;
4097
4098 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4099 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4100 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4101 outbuf.data = 9;
4102
4103 /* encode the method, which necessarily is the first one */
4104 if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_GET)
4105 outbuf.area[outbuf.data++] = 0x82; // indexed field : idx[02]=(":method", "GET")
4106 else if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_POST)
4107 outbuf.area[outbuf.data++] = 0x83; // indexed field : idx[03]=(":method", "POST")
4108 else if (unlikely(outbuf.data + 2 + meth.len <= outbuf.size)) {
4109 /* basic encoding of the method code */
4110 outbuf.area[outbuf.data++] = 0x42; // indexed name -- name=":method" (idx 2)
4111 outbuf.area[outbuf.data++] = meth.len; // method length
4112 memcpy(&outbuf.area[outbuf.data], meth.ptr, meth.len);
4113 }
4114 else {
4115 if (b_space_wraps(&h2c->mbuf))
4116 goto realign_again;
4117 goto full;
4118 }
4119
4120 /* encode the scheme which is always "https" (or 0x86 for "http") */
4121 if (outbuf.data < outbuf.size)
4122 outbuf.area[outbuf.data++] = 0x87; // indexed field : idx[02]=(":scheme", "https")
4123
4124 /* encode the path, which necessarily is the second one */
4125 if (outbuf.data < outbuf.size && isteq(path, ist("/"))) {
4126 outbuf.area[outbuf.data++] = 0x84; // indexed field : idx[04]=(":path", "/")
4127 }
4128 else if (outbuf.data < outbuf.size && isteq(path, ist("/index.html"))) {
4129 outbuf.area[outbuf.data++] = 0x85; // indexed field : idx[04]=(":path", "/index.html")
4130 }
4131 else if (!hpack_encode_header(&outbuf, ist(":path"), path)) {
4132 /* output full */
4133 if (b_space_wraps(&h2c->mbuf))
4134 goto realign_again;
4135 goto full;
4136 }
4137
4138 /* encode all headers, stop at empty name */
4139 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4140 /* these ones do not exist in H2 and must be dropped. */
4141 if (isteq(list[hdr].n, ist("connection")) ||
4142 isteq(list[hdr].n, ist("proxy-connection")) ||
4143 isteq(list[hdr].n, ist("keep-alive")) ||
4144 isteq(list[hdr].n, ist("upgrade")) ||
4145 isteq(list[hdr].n, ist("transfer-encoding")))
4146 continue;
4147
4148 if (isteq(list[hdr].n, ist("")))
4149 break; // end
4150
4151 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4152 /* output full */
4153 if (b_space_wraps(&h2c->mbuf))
4154 goto realign_again;
4155 goto full;
4156 }
4157 }
4158
4159 /* we may need to add END_STREAM if we have no body :
4160 * - request already closed, or :
4161 * - no transfer-encoding, and :
4162 * - no content-length or content-length:0
4163 * Fixme: this doesn't take into account CONNECT requests.
4164 */
4165 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4166 es_now = 1;
4167
4168 if (sl->flags & HTX_SL_F_BODYLESS)
4169 es_now = 1;
4170
4171 if (h2s->cs->flags & CS_FL_SHW)
4172 es_now = 1;
4173
4174 /* update the frame's size */
4175 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4176
4177 if (es_now)
4178 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4179
4180 /* commit the H2 response */
4181 b_add(&h2c->mbuf, outbuf.data);
4182 h2s->flags |= H2_SF_HEADERS_SENT;
4183 h2s->st = H2_SS_OPEN;
4184
4185 /* for now we don't implemented CONTINUATION, so we wait for a
4186 * body or directly end in TRL2.
4187 */
4188 if (es_now) {
4189 // trim any possibly pending data (eg: inconsistent content-length)
4190 h2s->flags |= H2_SF_ES_SENT;
4191 h2s->st = H2_SS_HLOC;
4192 }
4193
4194 /* remove all header blocks including the EOH and compute the
4195 * corresponding size.
4196 *
4197 * FIXME: We should remove everything when es_now is set.
4198 */
4199 ret = 0;
4200 idx = htx_get_head(htx);
4201 blk = htx_get_blk(htx, idx);
4202 while (blk != blk_end) {
4203 ret += htx_get_blksz(blk);
4204 blk = htx_remove_blk(htx, blk);
4205 }
4206
4207 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4208 htx_remove_blk(htx, blk_end);
4209
4210 end:
4211 return ret;
4212 full:
4213 h2c->flags |= H2_CF_MUX_MFULL;
4214 h2s->flags |= H2_SF_BLK_MROOM;
4215 ret = 0;
4216 goto end;
4217 fail:
4218 /* unparsable HTX messages, too large ones to be produced in the local
4219 * list etc go here (unrecoverable errors).
4220 */
4221 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4222 ret = 0;
4223 goto end;
4224}
4225
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004226/* Try to send a DATA frame matching HTTP response present in HTX structure
4227 * <htx>, for stream <h2s>. Returns the number of bytes sent. The caller must
4228 * check the stream's status to detect any error which might have happened
4229 * subsequently to a successful send. Returns the number of data bytes
4230 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4231 */
4232static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct htx *htx)
4233{
4234 struct h2c *h2c = h2s->h2c;
4235 struct buffer outbuf;
4236 size_t total = 0;
4237 int es_now = 0;
4238 int bsize; /* htx block size */
4239 int fsize; /* h2 frame size */
4240 struct htx_blk *blk;
4241 enum htx_blk_type type;
4242 int idx;
4243
4244 if (h2c_mux_busy(h2c, h2s)) {
4245 h2s->flags |= H2_SF_BLK_MBUSY;
4246 goto end;
4247 }
4248
4249 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4250 h2c->flags |= H2_CF_MUX_MALLOC;
4251 h2s->flags |= H2_SF_BLK_MROOM;
4252 goto end;
4253 }
4254
4255 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4256 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4257 * the caller to handle.
4258 */
4259
4260 new_frame:
4261 if (htx_is_empty(htx))
4262 goto end;
4263
4264 idx = htx_get_head(htx);
4265 blk = htx_get_blk(htx, idx);
4266 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4267 bsize = htx_get_blksz(blk);
4268 fsize = bsize;
4269
4270 if (type == HTX_BLK_EOD) {
4271 /* if we have an EOD, we're dealing with chunked data. We may
4272 * have a set of trailers after us that the caller will want to
4273 * deal with. Let's simply remove the EOD and return.
4274 */
4275 htx_remove_blk(htx, blk);
4276 // FIXME, it seems we must not return it in the total bytes count?
4277 //total++; // EOD counts as one byte
4278 goto end;
4279 }
4280
4281 /* for DATA and EOM we'll have to emit a frame, even if empty */
4282
4283 while (1) {
4284 outbuf.area = b_tail(&h2c->mbuf);
4285 outbuf.size = b_contig_space(&h2c->mbuf);
4286 outbuf.data = 0;
4287
4288 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4289 break;
4290 realign_again:
4291 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4292 }
4293
4294 if (outbuf.size < 9) {
4295 h2c->flags |= H2_CF_MUX_MFULL;
4296 h2s->flags |= H2_SF_BLK_MROOM;
4297 goto end;
4298 }
4299
4300 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4301 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4302 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4303 outbuf.data = 9;
4304
4305 /* we have in <fsize> the exact number of bytes we need to copy from
4306 * the HTX buffer. We need to check this against the connection's and
4307 * the stream's send windows, and to ensure that this fits in the max
4308 * frame size and in the buffer's available space minus 9 bytes (for
4309 * the frame header). The connection's flow control is applied last so
4310 * that we can use a separate list of streams which are immediately
4311 * unblocked on window opening. Note: we don't implement padding.
4312 */
4313
4314 /* EOM is presented with bsize==1 but would lead to the emission of an
4315 * empty frame, thus we force it to zero here.
4316 */
4317 if (type == HTX_BLK_EOM)
4318 bsize = fsize = 0;
4319
4320 if (!fsize)
4321 goto send_empty;
4322
4323 if (h2s->mws <= 0) {
4324 h2s->flags |= H2_SF_BLK_SFCTL;
4325 if (h2s->send_wait) {
4326 LIST_DEL(&h2s->list);
4327 LIST_INIT(&h2s->list);
4328 }
4329 goto end;
4330 }
4331
4332 if (fsize > h2s->mws)
4333 fsize = h2s->mws; // >0
4334
4335 if (h2c->mfs && fsize > h2c->mfs)
4336 fsize = h2c->mfs; // >0
4337
4338 if (fsize + 9 > outbuf.size) {
4339 /* we have an opportunity for enlarging the too small
4340 * available space, let's try.
4341 * FIXME: is this really interesting to do? Maybe we'll
4342 * spend lots of time realigning instead of using two
4343 * frames.
4344 */
4345 if (b_space_wraps(&h2c->mbuf))
4346 goto realign_again;
4347 fsize = outbuf.size - 9;
4348
4349 if (fsize <= 0) {
4350 /* no need to send an empty frame here */
4351 h2c->flags |= H2_CF_MUX_MFULL;
4352 h2s->flags |= H2_SF_BLK_MROOM;
4353 goto end;
4354 }
4355 }
4356
4357 if (h2c->mws <= 0) {
4358 h2s->flags |= H2_SF_BLK_MFCTL;
4359 goto end;
4360 }
4361
4362 if (fsize > h2c->mws)
4363 fsize = h2c->mws;
4364
4365 /* now let's copy this this into the output buffer */
4366 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
4367
4368 send_empty:
4369 /* update the frame's size */
4370 h2_set_frame_size(outbuf.area, fsize);
4371
4372 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4373 * meeting EOM. We should optimize this later.
4374 */
4375 if (type == HTX_BLK_EOM) {
4376 // FIXME, it seems we must not return it in the total bytes count?
4377 // total++; // EOM counts as one byte
4378 es_now = 1;
4379 }
4380
4381 if (es_now)
4382 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4383
4384 /* commit the H2 response */
4385 b_add(&h2c->mbuf, fsize + 9);
4386
4387 /* consume incoming HTX block, including EOM */
4388 total += fsize;
4389 if (fsize == bsize) {
4390 htx_remove_blk(htx, blk);
4391 if (fsize)
4392 goto new_frame;
4393 } else {
4394 /* we've truncated this block */
4395 htx_cut_data_blk(htx, blk, fsize);
4396 }
4397
4398 if (es_now) {
4399 if (h2s->st == H2_SS_OPEN)
4400 h2s->st = H2_SS_HLOC;
4401 else
4402 h2s_close(h2s);
4403
4404 h2s->flags |= H2_SF_ES_SENT;
4405 }
4406
4407 end:
4408 return total;
4409}
4410
Olivier Houchard6ff20392018-07-17 18:46:31 +02004411/* Called from the upper layer, to subscribe to events, such as being able to send */
4412static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4413{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004414 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004415 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004416 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004417
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004418 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004419 sw = param;
4420 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4421 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004422 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004423 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004424 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004425 event_type &= ~SUB_CAN_RECV;
4426 }
4427 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004428 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004429 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004430 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004431 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004432 h2s->send_wait = sw;
4433 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4434 if (h2s->flags & H2_SF_BLK_MFCTL)
4435 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4436 else
4437 LIST_ADDQ(&h2c->send_list, &h2s->list);
4438 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004439 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004440 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004441 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004442 if (event_type != 0)
4443 return -1;
4444 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004445
4446
4447}
4448
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004449static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4450{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004451 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004452 struct h2s *h2s = cs->ctx;
4453
4454 if (event_type & SUB_CAN_RECV) {
4455 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004456 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004457 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004458 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004459 }
4460 }
4461 if (event_type & SUB_CAN_SEND) {
4462 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004463 if (h2s->send_wait == sw) {
4464 LIST_DEL(&h2s->list);
4465 LIST_INIT(&h2s->list);
4466 sw->wait_reason &= ~SUB_CAN_SEND;
4467 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004468 }
4469 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004470 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4471 sw = param;
4472 if (h2s->send_wait == sw) {
4473 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4474 h2s->send_wait = NULL;
4475 }
4476 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004477 return 0;
4478}
4479
4480
Olivier Houchard511efea2018-08-16 15:30:32 +02004481/* Called from the upper layer, to receive data */
4482static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4483{
Olivier Houchard638b7992018-08-16 15:41:52 +02004484 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004485 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004486 struct htx *h2s_htx = NULL;
4487 struct htx *buf_htx = NULL;
4488 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004489 size_t ret = 0;
4490
4491 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004492 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4493 /* in HTX mode we ignore the count argument */
4494 h2s_htx = htx_from_buf(&h2s->rxbuf);
4495 if (htx_is_empty(h2s_htx))
4496 goto end;
4497
4498 buf_htx = htx_from_buf(buf);
4499 count = htx_free_space(buf_htx);
4500
4501 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, (h2s_htx->sl_off != -1) ? HTX_BLK_EOH : HTX_BLK_EOM);
4502
4503 buf_htx->extra = h2s_htx->extra;
4504 if (htx_is_not_empty(buf_htx))
4505 b_set_data(buf, b_size(buf));
4506 ret = htx_ret.ret;
4507 }
4508 else {
4509 ret = b_xfer(buf, &h2s->rxbuf, count);
4510 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004511
Olivier Houchard638b7992018-08-16 15:41:52 +02004512 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02004513 cs->flags |= CS_FL_RCV_MORE;
4514 else {
4515 cs->flags &= ~CS_FL_RCV_MORE;
4516 if (cs->flags & CS_FL_REOS)
4517 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004518 if (b_size(&h2s->rxbuf)) {
4519 b_free(&h2s->rxbuf);
4520 offer_buffers(NULL, tasks_run_queue);
4521 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004522 }
4523
Willy Tarreau082f5592018-11-25 08:03:32 +01004524 if (ret && h2c->dsi == h2s->id) {
4525 /* demux is blocking on this stream's buffer */
4526 h2c->flags &= ~H2_CF_DEM_SFULL;
4527 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4528 if (h2_recv_allowed(h2c))
4529 tasklet_wakeup(h2c->wait_event.task);
4530 }
4531 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004532end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004533 return ret;
4534}
4535
Olivier Houchardd846c262018-10-19 17:24:29 +02004536static void h2_stop_senders(struct h2c *h2c)
4537{
4538 struct h2s *h2s, *h2s_back;
4539
4540 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4541 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4542 if (h2c->msi == h2s_id(h2s))
4543 continue;
4544 LIST_DEL(&h2s->list);
4545 LIST_INIT(&h2s->list);
4546 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4547 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4548 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4549 LIST_ADD(&h2c->send_list, &h2s->list);
4550 }
4551}
4552
Willy Tarreau62f52692017-10-08 23:01:42 +02004553/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004554static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004555{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004556 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004557 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004558 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004559 struct htx *htx;
4560 struct htx_blk *blk;
4561 enum htx_blk_type btype;
4562 uint32_t bsize;
4563 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004564
Olivier Houchardd846c262018-10-19 17:24:29 +02004565 if (h2s->send_wait) {
4566 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4567 h2s->send_wait = NULL;
4568 LIST_DEL(&h2s->list);
4569 LIST_INIT(&h2s->list);
4570 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004571 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4572 return 0;
4573
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004574 /* htx will be enough to decide if we're using HTX or legacy */
4575 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4576
Willy Tarreau0bad0432018-06-14 16:54:01 +02004577 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004578 h2s->flags |= H2_SF_OUTGOING_DATA;
4579
Willy Tarreau751f2d02018-10-05 09:35:00 +02004580 if (h2s->id == 0) {
4581 int32_t id = h2c_get_next_sid(h2s->h2c);
4582
4583 if (id < 0) {
4584 cs->ctx = NULL;
4585 cs->flags |= CS_FL_ERROR;
4586 h2s_destroy(h2s);
4587 return 0;
4588 }
4589
4590 eb32_delete(&h2s->by_id);
4591 h2s->by_id.key = h2s->id = id;
4592 h2s->h2c->max_id = id;
4593 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4594 }
4595
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004596 if (htx) {
4597 while (count && !htx_is_empty(htx)) {
4598 idx = htx_get_head(htx);
4599 blk = htx_get_blk(htx, idx);
4600 btype = htx_get_blk_type(blk);
4601 bsize = htx_get_blksz(blk);
4602
4603 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004604 case HTX_BLK_REQ_SL:
4605 /* start-line before headers */
4606 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4607 if (ret > 0) {
4608 total += ret;
4609 count -= ret;
4610 if (ret < bsize)
4611 goto done;
4612 }
4613 break;
4614
Willy Tarreau115e83b2018-12-01 19:17:53 +01004615 case HTX_BLK_RES_SL:
4616 /* start-line before headers */
4617 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4618 if (ret > 0) {
4619 total += ret;
4620 count -= ret;
4621 if (ret < bsize)
4622 goto done;
4623 }
4624 break;
4625
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004626 case HTX_BLK_DATA:
4627 case HTX_BLK_EOD:
4628 case HTX_BLK_EOM:
4629 /* all these cause the emission of a DATA frame (possibly empty) */
4630 ret = h2s_htx_frt_make_resp_data(h2s, htx);
4631 if (ret > 0) {
4632 total += ret;
4633 count -= ret;
4634 if (ret < bsize)
4635 goto done;
4636 }
4637 break;
4638
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004639 default:
4640 htx_remove_blk(htx, blk);
4641 total += bsize;
4642 count -= bsize;
4643 break;
4644 }
4645 }
4646 goto done;
4647 }
4648
4649 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004650 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004651 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004652 if (h2s->h2c->flags & H2_CF_IS_BACK)
4653 ret = -1;
4654 else
4655 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004656 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004657 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004658 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004659 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004660 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004661 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004662 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004663
Willy Tarreau5dd17352018-06-14 13:33:30 +02004664 if (unlikely((int)ret <= 0)) {
4665 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004666 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4667 break;
4668 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004669 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004670 total += count;
4671 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004672 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004673 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004674 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004675 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004676 cs->flags |= CS_FL_ERROR;
4677 break;
4678 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004679
4680 total += ret;
4681 count -= ret;
4682
4683 if (h2s->st >= H2_SS_ERROR)
4684 break;
4685
4686 if (h2s->flags & H2_SF_BLK_ANY)
4687 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004688 }
4689
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004690 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004691 if (h2s->st >= H2_SS_ERROR) {
4692 /* trim any possibly pending data after we close (extra CR-LF,
4693 * unprocessed trailers, abnormal extra data, ...)
4694 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004695 total += count;
4696 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004697 }
4698
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004699 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004700 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004701 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004702 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004703 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004704 }
4705
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004706 if (htx) {
4707 if (htx_is_empty(htx)) {
4708 htx_reset(htx);
4709 b_set_data(buf, 0);
4710 }
4711 } else {
4712 b_del(buf, total);
4713 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004714
4715 /* The mux is full, cancel the pending tasks */
4716 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4717 (h2s->flags & H2_SF_BLK_MBUSY))
4718 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004719
Olivier Houchard7505f942018-08-21 18:10:44 +02004720 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004721 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4722 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004723
Olivier Houchard7505f942018-08-21 18:10:44 +02004724 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004725 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004726}
4727
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004728/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004729static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004730{
4731 struct h2c *h2c = conn->mux_ctx;
4732 struct h2s *h2s;
4733 struct eb32_node *node;
4734 int fctl_cnt = 0;
4735 int send_cnt = 0;
4736 int tree_cnt = 0;
4737 int orph_cnt = 0;
4738
4739 if (!h2c)
4740 return;
4741
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004742 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004743 fctl_cnt++;
4744
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004745 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004746 send_cnt++;
4747
4748 node = eb32_first(&h2c->streams_by_id);
4749 while (node) {
4750 h2s = container_of(node, struct h2s, by_id);
4751 tree_cnt++;
4752 if (!h2s->cs)
4753 orph_cnt++;
4754 node = eb32_next(node);
4755 }
4756
Willy Tarreau616ac812018-07-24 14:12:42 +02004757 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4758 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4759 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4760 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4761 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4762 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004763}
Willy Tarreau62f52692017-10-08 23:01:42 +02004764
4765/*******************************************************/
4766/* functions below are dedicated to the config parsers */
4767/*******************************************************/
4768
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004769/* config parser for global "tune.h2.header-table-size" */
4770static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4771 struct proxy *defpx, const char *file, int line,
4772 char **err)
4773{
4774 if (too_many_args(1, args, err, NULL))
4775 return -1;
4776
4777 h2_settings_header_table_size = atoi(args[1]);
4778 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4779 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4780 return -1;
4781 }
4782 return 0;
4783}
Willy Tarreau62f52692017-10-08 23:01:42 +02004784
Willy Tarreaue6baec02017-07-27 11:45:11 +02004785/* config parser for global "tune.h2.initial-window-size" */
4786static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4787 struct proxy *defpx, const char *file, int line,
4788 char **err)
4789{
4790 if (too_many_args(1, args, err, NULL))
4791 return -1;
4792
4793 h2_settings_initial_window_size = atoi(args[1]);
4794 if (h2_settings_initial_window_size < 0) {
4795 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4796 return -1;
4797 }
4798 return 0;
4799}
4800
Willy Tarreau5242ef82017-07-27 11:47:28 +02004801/* config parser for global "tune.h2.max-concurrent-streams" */
4802static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4803 struct proxy *defpx, const char *file, int line,
4804 char **err)
4805{
4806 if (too_many_args(1, args, err, NULL))
4807 return -1;
4808
4809 h2_settings_max_concurrent_streams = atoi(args[1]);
4810 if (h2_settings_max_concurrent_streams < 0) {
4811 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4812 return -1;
4813 }
4814 return 0;
4815}
4816
Willy Tarreau62f52692017-10-08 23:01:42 +02004817
4818/****************************************/
4819/* MUX initialization and instanciation */
4820/***************************************/
4821
4822/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004823static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004824 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004825 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004826 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004827 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004828 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004829 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004830 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004831 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004832 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004833 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004834 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004835 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004836 .shutr = h2_shutr,
4837 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004838 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004839 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004840 .name = "H2",
4841};
4842
Christopher Faulet32f61c02018-04-10 14:33:41 +02004843/* PROTO selection : this mux registers PROTO token "h2" */
4844static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004845 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004846
Willy Tarreau0108d902018-11-25 19:14:37 +01004847INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4848
Willy Tarreauf8957272018-10-03 10:25:20 +02004849static struct mux_proto_list mux_proto_h2_htx =
4850 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4851
4852INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4853
Willy Tarreau62f52692017-10-08 23:01:42 +02004854/* config keyword parsers */
4855static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004856 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004857 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004858 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004859 { 0, NULL, NULL }
4860}};
4861
Willy Tarreau0108d902018-11-25 19:14:37 +01004862INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);