blob: 4da20070f76eeac66c1e3790a9f2459627637f79 [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 Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200123 struct wait_list wait_list; /* We're in a wait list, to send */
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;
177 struct h1m req, res; /* request and response parser state for H1 */
178 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;
Olivier Houchard638b7992018-08-16 15:41:52 +0200184 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200185 struct wait_list wait_list; /* Wait list, when we're attempting to send a RST but we can't send */
186 struct wait_list *recv_wait_list; /* Address of the wait_list the conn_stream associated is waiting on */
Willy Tarreau18312642017-10-11 07:57:07 +0200187};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200188
Willy Tarreauc6405142017-09-21 20:23:50 +0200189/* descriptor for an h2 frame header */
190struct h2_fh {
191 uint32_t len; /* length, host order, 24 bits */
192 uint32_t sid; /* stream id, host order, 31 bits */
193 uint8_t ft; /* frame type */
194 uint8_t ff; /* frame flags */
195};
196
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200197/* a few settings from the global section */
198static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200199static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200200static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200201
Willy Tarreau2a856182017-05-16 15:20:39 +0200202/* a dmumy closed stream */
203static const struct h2s *h2_closed_stream = &(const struct h2s){
204 .cs = NULL,
205 .h2c = NULL,
206 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100207 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100208 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200209 .id = 0,
210};
211
212/* and a dummy idle stream for use with any unannounced stream */
213static const struct h2s *h2_idle_stream = &(const struct h2s){
214 .cs = NULL,
215 .h2c = NULL,
216 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100217 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
Olivier Houchard9f6af332018-05-25 14:04:04 +0200221static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200222static int h2_send(struct h2c *h2c);
223static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200224static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200225static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100226static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100227static int h2_frt_decode_headers(struct h2s *h2s);
228static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200229static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200230
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200231/*****************************************************/
232/* functions below are for dynamic buffer management */
233/*****************************************************/
234
Willy Tarreau315d8072017-12-10 22:17:57 +0100235/* indicates whether or not the we may call the h2_recv() function to attempt
236 * to receive data into the buffer and/or demux pending data. The condition is
237 * a bit complex due to some API limits for now. The rules are the following :
238 * - if an error or a shutdown was detected on the connection and the buffer
239 * is empty, we must not attempt to receive
240 * - if the demux buf failed to be allocated, we must not try to receive and
241 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100242 * - if no flag indicates a blocking condition, we may attempt to receive,
243 * regardless of whether the demux buffer is full or not, so that only
244 * de demux part decides whether or not to block. This is needed because
245 * the connection API indeed prevents us from re-enabling receipt that is
246 * already enabled in a polled state, so we must always immediately stop
247 * as soon as the demux can't proceed so as never to hit an end of read
248 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100249 * - otherwise must may not attempt
250 */
251static inline int h2_recv_allowed(const struct h2c *h2c)
252{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200253 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100254 (h2c->st0 >= H2_CS_ERROR ||
255 h2c->conn->flags & CO_FL_ERROR ||
256 conn_xprt_read0_pending(h2c->conn)))
257 return 0;
258
259 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100260 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100261 return 1;
262
263 return 0;
264}
265
Willy Tarreauf2101912018-07-19 10:11:38 +0200266/* returns true if the connection has too many conn_streams attached */
267static inline int h2_has_too_many_cs(const struct h2c *h2c)
268{
269 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
270}
271
Willy Tarreau44e973f2018-03-01 17:49:30 +0100272/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
273 * flags are used to figure what buffer was requested. It returns 1 if the
274 * allocation succeeds, in which case the connection is woken up, or 0 if it's
275 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200276 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100277static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200278{
279 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100280 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281
Willy Tarreau44e973f2018-03-01 17:49:30 +0100282 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200283 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200284 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200285 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200286 if (h2_recv(h2c))
287 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200288 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200289 return 1;
290 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
293 h2c->flags &= ~H2_CF_MUX_MALLOC;
294 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
295 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200296
297 if (h2c->flags & H2_CF_DEM_MROOM) {
298 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200299 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200300 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200301 if (h2_recv(h2c))
302 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200303 }
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200304 }
Willy Tarreau14398122017-09-22 14:26:04 +0200305 return 1;
306 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100307
308 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
309 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200310 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100311 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200312 if (h2_recv_allowed(h2c)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200314 if (h2_recv(h2c))
315 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200316 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100317 return 1;
318 }
319
Willy Tarreau14398122017-09-22 14:26:04 +0200320 return 0;
321}
322
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200323static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200324{
325 struct buffer *buf = NULL;
326
Willy Tarreau44e973f2018-03-01 17:49:30 +0100327 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
328 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
329 h2c->buf_wait.target = h2c;
330 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100332 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200334 __conn_xprt_stop_recv(h2c->conn);
335 }
336 return buf;
337}
338
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200339static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200340{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100342 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200343 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200344 }
345}
346
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200347
Willy Tarreau62f52692017-10-08 23:01:42 +0200348/*****************************************************************/
349/* functions below are dedicated to the mux setup and management */
350/*****************************************************************/
351
Willy Tarreau32218eb2017-09-22 08:07:25 +0200352/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
353static int h2c_frt_init(struct connection *conn)
354{
355 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100356 struct task *t = NULL;
357 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200358
Willy Tarreaubafbe012017-11-24 17:34:44 +0100359 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200360 if (!h2c)
361 goto fail;
362
Willy Tarreau3f133572017-10-31 19:21:06 +0100363
Willy Tarreau599391a2017-11-24 10:16:00 +0100364 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
365 if (tick_isset(sess->fe->timeout.clientfin))
366 h2c->shut_timeout = sess->fe->timeout.clientfin;
367
Willy Tarreau33400292017-11-05 11:23:40 +0100368 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100369 if (tick_isset(h2c->timeout)) {
370 t = task_new(tid_bit);
371 if (!t)
372 goto fail;
373
374 h2c->task = t;
375 t->process = h2_timeout_task;
376 t->context = h2c;
377 t->expire = tick_add(now_ms, h2c->timeout);
378 }
Willy Tarreauea392822017-10-31 10:02:25 +0100379
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200380 h2c->wait_list.task = tasklet_new();
381 if (!h2c->wait_list.task)
382 goto fail;
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200383 h2c->wait_list.task->process = h2_io_cb;
384 h2c->wait_list.task->context = h2c;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200385 h2c->wait_list.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200386
Willy Tarreau32218eb2017-09-22 08:07:25 +0200387 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
388 if (!h2c->ddht)
389 goto fail;
390
391 /* Initialise the context. */
392 h2c->st0 = H2_CS_PREFACE;
393 h2c->conn = conn;
394 h2c->max_id = -1;
395 h2c->errcode = H2_ERR_NO_ERROR;
396 h2c->flags = H2_CF_NONE;
397 h2c->rcvd_c = 0;
398 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100399 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200400 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200401
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200402 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200403 h2c->dsi = -1;
404 h2c->msi = -1;
405 h2c->last_sid = -1;
406
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200408 h2c->miw = 65535; /* mux initial window size */
409 h2c->mws = 65535; /* mux window size */
410 h2c->mfs = 16384; /* initial max frame size */
411 h2c->streams_by_id = EB_ROOT_UNIQUE;
412 LIST_INIT(&h2c->send_list);
413 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100414 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200415 conn->mux_ctx = h2c;
416
Willy Tarreau3f133572017-10-31 19:21:06 +0100417 if (t)
418 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200419 conn_xprt_want_recv(conn);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200420 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100421
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200422 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard7505f942018-08-21 18:10:44 +0200423 if (h2_recv(h2c))
424 h2_process(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 return 0;
426 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100427 if (t)
428 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200429 if (h2c->wait_list.task)
430 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100431 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 return -1;
433}
434
Willy Tarreau62f52692017-10-08 23:01:42 +0200435/* Initialize the mux once it's attached. For outgoing connections, the context
436 * is already initialized before installing the mux, so we detect incoming
437 * connections from the fact that the context is still NULL. Returns < 0 on
438 * error.
439 */
440static int h2_init(struct connection *conn)
441{
442 if (conn->mux_ctx) {
443 /* we don't support outgoing connections for now */
444 return -1;
445 }
446
Willy Tarreau32218eb2017-09-22 08:07:25 +0200447 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200448}
449
Willy Tarreau2373acc2017-10-12 17:35:14 +0200450/* returns the stream associated with id <id> or NULL if not found */
451static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
452{
453 struct eb32_node *node;
454
Willy Tarreau2a856182017-05-16 15:20:39 +0200455 if (id > h2c->max_id)
456 return (struct h2s *)h2_idle_stream;
457
Willy Tarreau2373acc2017-10-12 17:35:14 +0200458 node = eb32_lookup(&h2c->streams_by_id, id);
459 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200460 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200461
462 return container_of(node, struct h2s, by_id);
463}
464
Willy Tarreau62f52692017-10-08 23:01:42 +0200465/* release function for a connection. This one should be called to free all
466 * resources allocated to the mux.
467 */
468static void h2_release(struct connection *conn)
469{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470 struct h2c *h2c = conn->mux_ctx;
471
472 LIST_DEL(&conn->list);
473
474 if (h2c) {
475 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200476
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100477 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100478 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100479 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200480
Willy Tarreau44e973f2018-03-01 17:49:30 +0100481 h2_release_buf(h2c, &h2c->dbuf);
482 h2_release_buf(h2c, &h2c->mbuf);
483
Willy Tarreauea392822017-10-31 10:02:25 +0100484 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200485 h2c->task->context = NULL;
486 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100487 h2c->task = NULL;
488 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200489 if (h2c->wait_list.task)
490 tasklet_free(h2c->wait_list.task);
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200491 LIST_DEL(&h2c->wait_list.list);
492 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100493
Willy Tarreaubafbe012017-11-24 17:34:44 +0100494 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200495 }
496
497 conn->mux = NULL;
498 conn->mux_ctx = NULL;
499
500 conn_stop_tracking(conn);
501 conn_full_close(conn);
502 if (conn->destroy_cb)
503 conn->destroy_cb(conn);
504 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200505}
506
507
Willy Tarreau71681172017-10-23 14:39:06 +0200508/******************************************************/
509/* functions below are for the H2 protocol processing */
510/******************************************************/
511
512/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100513static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200514{
515 return h2s ? h2s->id : 0;
516}
517
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200518/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100519static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200520{
521 if (h2c->msi < 0)
522 return 0;
523
524 if (h2c->msi == h2s_id(h2s))
525 return 0;
526
527 return 1;
528}
529
Willy Tarreau741d6df2017-10-17 08:00:59 +0200530/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200532{
533 h2c->errcode = err;
534 h2c->st0 = H2_CS_ERROR;
535}
536
Willy Tarreau2e43f082017-10-17 08:03:59 +0200537/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100538static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200539{
540 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
541 h2s->errcode = err;
542 h2s->st = H2_SS_ERROR;
543 if (h2s->cs)
544 h2s->cs->flags |= CS_FL_ERROR;
545 }
546}
547
Willy Tarreaue4820742017-07-27 13:37:23 +0200548/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100549static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200550{
551 uint8_t *out = frame;
552
553 *out = len >> 16;
554 write_n16(out + 1, len);
555}
556
Willy Tarreau54c15062017-10-10 17:10:03 +0200557/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
558 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
559 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200560 * available in the buffer's input prior to calling this function. The buffer
561 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200562 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100563static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200564 const struct buffer *b, int o)
565{
Willy Tarreau591d4452018-06-15 17:21:00 +0200566 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 +0200567}
568
Willy Tarreau1f094672017-11-20 21:27:45 +0100569static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200570{
Willy Tarreau591d4452018-06-15 17:21:00 +0200571 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200572}
573
Willy Tarreau1f094672017-11-20 21:27:45 +0100574static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200575{
Willy Tarreau591d4452018-06-15 17:21:00 +0200576 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200577}
578
Willy Tarreau1f094672017-11-20 21:27:45 +0100579static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200580{
Willy Tarreau591d4452018-06-15 17:21:00 +0200581 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200582}
583
584
Willy Tarreau715d5312017-07-11 15:20:24 +0200585/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
586 * is not obvious. It turns out that H2 headers are neither aligned nor do they
587 * use regular sizes. And to add to the trouble, the buffer may wrap so each
588 * byte read must be checked. The header is formed like this :
589 *
590 * b0 b1 b2 b3 b4 b5..b8
591 * +----------+---------+--------+----+----+----------------------+
592 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
593 * +----------+---------+--------+----+----+----------------------+
594 *
595 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
596 * we get the sid properly aligned and ordered, and 16 bits of len properly
597 * ordered as well. The type and flags can be extracted using bit shifts from
598 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200599 * Returns zero if some bytes are missing, otherwise non-zero on success. The
600 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200601 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100602static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200603{
604 uint64_t w;
605
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200606 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200607 return 0;
608
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200609 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200610 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200611 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
612 h->ff = w >> 32;
613 h->ft = w >> 40;
614 h->len += w >> 48;
615 return 1;
616}
617
618/* skip the next 9 bytes corresponding to the frame header possibly parsed by
619 * h2_peek_frame_hdr() above.
620 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100621static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200622{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200623 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200624}
625
626/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100627static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200628{
629 int ret;
630
631 ret = h2_peek_frame_hdr(b, h);
632 if (ret > 0)
633 h2_skip_frame_hdr(b);
634 return ret;
635}
636
Willy Tarreau00dd0782018-03-01 16:31:34 +0100637/* marks stream <h2s> as CLOSED and decrement the number of active streams for
638 * its connection if the stream was not yet closed. Please use this exclusively
639 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100640 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100641static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100642{
643 if (h2s->st != H2_SS_CLOSED)
644 h2s->h2c->nb_streams--;
645 h2s->st = H2_SS_CLOSED;
646}
647
Willy Tarreau71049cc2018-03-28 13:56:39 +0200648/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
649static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100650{
651 h2s_close(h2s);
652 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200653 if (b_size(&h2s->rxbuf)) {
654 b_free(&h2s->rxbuf);
655 offer_buffers(NULL, tasks_run_queue);
656 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200657 LIST_DEL(&h2s->wait_list.list);
658 LIST_INIT(&h2s->wait_list.list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200659 tasklet_free(h2s->wait_list.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100660 pool_free(pool_head_h2s, h2s);
661}
662
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200663/* creates a new stream <id> on the h2c connection and returns it, or NULL in
664 * case of memory allocation error.
665 */
666static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
667{
Willy Tarreau590a0512018-09-05 11:56:48 +0200668 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200669 struct conn_stream *cs;
670 struct h2s *h2s;
671
Willy Tarreaubafbe012017-11-24 17:34:44 +0100672 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673 if (!h2s)
674 goto out;
675
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200676 h2s->wait_list.task = tasklet_new();
677 if (!h2s->wait_list.task) {
678 pool_free(pool_head_h2s, h2s);
679 goto out;
680 }
681 LIST_INIT(&h2s->wait_list.list);
682 h2s->recv_wait_list = NULL;
683 h2s->wait_list.task->process = h2_deferred_shut;
684 h2s->wait_list.task->context = h2s;
685 h2s->wait_list.handle = NULL;
686 h2s->wait_list.wait_reason = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200687 h2s->h2c = h2c;
688 h2s->mws = h2c->miw;
689 h2s->flags = H2_SF_NONE;
690 h2s->errcode = H2_ERR_NO_ERROR;
691 h2s->st = H2_SS_IDLE;
Olivier Houchard638b7992018-08-16 15:41:52 +0200692 h2s->rxbuf = BUF_NULL;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200693 h1m_init_req(&h2s->req);
694 h1m_init_res(&h2s->res);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200695 h2s->by_id.key = h2s->id = id;
696 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200697
698 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100699 h2c->nb_streams++;
700 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
701 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200702
703 cs = cs_new(h2c->conn);
704 if (!cs)
705 goto out_close;
706
707 h2s->cs = cs;
708 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200709 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200710
711 if (stream_create_from_cs(cs) < 0)
712 goto out_free_cs;
713
Willy Tarreau590a0512018-09-05 11:56:48 +0200714 /* We want the accept date presented to the next stream to be the one
715 * we have now, the handshake time to be null (since the next stream
716 * is not delayed by a handshake), and the idle time to count since
717 * right now.
718 */
719 sess->accept_date = date;
720 sess->tv_accept = now;
721 sess->t_handshake = 0;
722
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200723 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200724 if (h2_has_too_many_cs(h2c))
725 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200726 return h2s;
727
728 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200729 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200730 cs_free(cs);
731 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200732 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200733 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200734 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200735 out:
736 return h2s;
737}
738
Willy Tarreaube5b7152017-09-25 16:25:39 +0200739/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
740 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
741 * the various settings codes.
742 */
743static int h2c_snd_settings(struct h2c *h2c)
744{
745 struct buffer *res;
746 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200747 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200748 int ret;
749
750 if (h2c_mux_busy(h2c, NULL)) {
751 h2c->flags |= H2_CF_DEM_MBUSY;
752 return 0;
753 }
754
Willy Tarreau44e973f2018-03-01 17:49:30 +0100755 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200756 if (!res) {
757 h2c->flags |= H2_CF_MUX_MALLOC;
758 h2c->flags |= H2_CF_DEM_MROOM;
759 return 0;
760 }
761
762 chunk_init(&buf, buf_data, sizeof(buf_data));
763 chunk_memcpy(&buf,
764 "\x00\x00\x00" /* length : 0 for now */
765 "\x04\x00" /* type : 4 (settings), flags : 0 */
766 "\x00\x00\x00\x00", /* stream ID : 0 */
767 9);
768
769 if (h2_settings_header_table_size != 4096) {
770 char str[6] = "\x00\x01"; /* header_table_size */
771
772 write_n32(str + 2, h2_settings_header_table_size);
773 chunk_memcat(&buf, str, 6);
774 }
775
776 if (h2_settings_initial_window_size != 65535) {
777 char str[6] = "\x00\x04"; /* initial_window_size */
778
779 write_n32(str + 2, h2_settings_initial_window_size);
780 chunk_memcat(&buf, str, 6);
781 }
782
783 if (h2_settings_max_concurrent_streams != 0) {
784 char str[6] = "\x00\x03"; /* max_concurrent_streams */
785
786 /* Note: 0 means "unlimited" for haproxy's config but not for
787 * the protocol, so never send this value!
788 */
789 write_n32(str + 2, h2_settings_max_concurrent_streams);
790 chunk_memcat(&buf, str, 6);
791 }
792
793 if (global.tune.bufsize != 16384) {
794 char str[6] = "\x00\x05"; /* max_frame_size */
795
796 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
797 * match bufsize - rewrite size, but at the moment it seems
798 * that clients don't take care of it.
799 */
800 write_n32(str + 2, global.tune.bufsize);
801 chunk_memcat(&buf, str, 6);
802 }
803
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200804 h2_set_frame_size(buf.area, buf.data - 9);
805 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200806 if (unlikely(ret <= 0)) {
807 if (!ret) {
808 h2c->flags |= H2_CF_MUX_MFULL;
809 h2c->flags |= H2_CF_DEM_MROOM;
810 return 0;
811 }
812 else {
813 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
814 return 0;
815 }
816 }
817 return ret;
818}
819
Willy Tarreau52eed752017-09-22 15:05:09 +0200820/* Try to receive a connection preface, then upon success try to send our
821 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
822 * missing data. It may return an error in h2c.
823 */
824static int h2c_frt_recv_preface(struct h2c *h2c)
825{
826 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200827 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200828
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200829 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200830
831 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200832 if (ret1 < 0)
833 sess_log(h2c->conn->owner);
834
Willy Tarreau52eed752017-09-22 15:05:09 +0200835 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
836 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
837 return 0;
838 }
839
Willy Tarreaube5b7152017-09-25 16:25:39 +0200840 ret2 = h2c_snd_settings(h2c);
841 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200842 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200843
Willy Tarreaube5b7152017-09-25 16:25:39 +0200844 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200845}
846
Willy Tarreau081d4722017-05-16 21:51:05 +0200847/* try to send a GOAWAY frame on the connection to report an error or a graceful
848 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
849 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
850 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
851 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
852 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
853 * on unrecoverable failure. It will not attempt to send one again in this last
854 * case so that it is safe to use h2c_error() to report such errors.
855 */
856static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
857{
858 struct buffer *res;
859 char str[17];
860 int ret;
861
862 if (h2c->flags & H2_CF_GOAWAY_FAILED)
863 return 1; // claim that it worked
864
865 if (h2c_mux_busy(h2c, h2s)) {
866 if (h2s)
867 h2s->flags |= H2_SF_BLK_MBUSY;
868 else
869 h2c->flags |= H2_CF_DEM_MBUSY;
870 return 0;
871 }
872
Willy Tarreau44e973f2018-03-01 17:49:30 +0100873 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200874 if (!res) {
875 h2c->flags |= H2_CF_MUX_MALLOC;
876 if (h2s)
877 h2s->flags |= H2_SF_BLK_MROOM;
878 else
879 h2c->flags |= H2_CF_DEM_MROOM;
880 return 0;
881 }
882
883 /* len: 8, type: 7, flags: none, sid: 0 */
884 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
885
886 if (h2c->last_sid < 0)
887 h2c->last_sid = h2c->max_id;
888
889 write_n32(str + 9, h2c->last_sid);
890 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200891 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200892 if (unlikely(ret <= 0)) {
893 if (!ret) {
894 h2c->flags |= H2_CF_MUX_MFULL;
895 if (h2s)
896 h2s->flags |= H2_SF_BLK_MROOM;
897 else
898 h2c->flags |= H2_CF_DEM_MROOM;
899 return 0;
900 }
901 else {
902 /* we cannot report this error using GOAWAY, so we mark
903 * it and claim a success.
904 */
905 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
906 h2c->flags |= H2_CF_GOAWAY_FAILED;
907 return 1;
908 }
909 }
910 h2c->flags |= H2_CF_GOAWAY_SENT;
911 return ret;
912}
913
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100914/* Try to send an RST_STREAM frame on the connection for the indicated stream
915 * during mux operations. This stream must be valid and cannot be closed
916 * already. h2s->id will be used for the stream ID and h2s->errcode will be
917 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
918 * not yet.
919 *
920 * Returns > 0 on success or zero if nothing was done. In case of lack of room
921 * to write the message, it subscribes the stream to future notifications.
922 */
923static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
924{
925 struct buffer *res;
926 char str[13];
927 int ret;
928
929 if (!h2s || h2s->st == H2_SS_CLOSED)
930 return 1;
931
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100932 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
933 * RST_STREAM in response to a RST_STREAM frame.
934 */
935 if (h2c->dft == H2_FT_RST_STREAM) {
936 ret = 1;
937 goto ignore;
938 }
939
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100940 if (h2c_mux_busy(h2c, h2s)) {
941 h2s->flags |= H2_SF_BLK_MBUSY;
942 return 0;
943 }
944
Willy Tarreau44e973f2018-03-01 17:49:30 +0100945 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100946 if (!res) {
947 h2c->flags |= H2_CF_MUX_MALLOC;
948 h2s->flags |= H2_SF_BLK_MROOM;
949 return 0;
950 }
951
952 /* len: 4, type: 3, flags: none */
953 memcpy(str, "\x00\x00\x04\x03\x00", 5);
954 write_n32(str + 5, h2s->id);
955 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200956 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100957
958 if (unlikely(ret <= 0)) {
959 if (!ret) {
960 h2c->flags |= H2_CF_MUX_MFULL;
961 h2s->flags |= H2_SF_BLK_MROOM;
962 return 0;
963 }
964 else {
965 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
966 return 0;
967 }
968 }
969
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100970 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100971 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100972 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100973 return ret;
974}
975
976/* Try to send an RST_STREAM frame on the connection for the stream being
977 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
978 * error code unless the stream's state already is IDLE or CLOSED in which
979 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
980 * it was not yet.
981 *
982 * Returns > 0 on success or zero if nothing was done. In case of lack of room
983 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200984 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100985 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200986 */
987static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
988{
989 struct buffer *res;
990 char str[13];
991 int ret;
992
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100993 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
994 * RST_STREAM in response to a RST_STREAM frame.
995 */
996 if (h2c->dft == H2_FT_RST_STREAM) {
997 ret = 1;
998 goto ignore;
999 }
1000
Willy Tarreau27a84c92017-10-17 08:10:17 +02001001 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001002 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001003 return 0;
1004 }
1005
Willy Tarreau44e973f2018-03-01 17:49:30 +01001006 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001007 if (!res) {
1008 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001009 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001010 return 0;
1011 }
1012
1013 /* len: 4, type: 3, flags: none */
1014 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001015
Willy Tarreau27a84c92017-10-17 08:10:17 +02001016 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +01001017 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +02001018 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001019 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001020
Willy Tarreau27a84c92017-10-17 08:10:17 +02001021 if (unlikely(ret <= 0)) {
1022 if (!ret) {
1023 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001024 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001025 return 0;
1026 }
1027 else {
1028 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1029 return 0;
1030 }
1031 }
1032
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001033 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001034 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001035 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001036 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001037 }
1038
Willy Tarreau27a84c92017-10-17 08:10:17 +02001039 return ret;
1040}
1041
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001042/* try to send an empty DATA frame with the ES flag set to notify about the
1043 * end of stream and match a shutdown(write). If an ES was already sent as
1044 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1045 * on success or zero if nothing was done. In case of lack of room to write the
1046 * message, it subscribes the requesting stream to future notifications.
1047 */
1048static int h2_send_empty_data_es(struct h2s *h2s)
1049{
1050 struct h2c *h2c = h2s->h2c;
1051 struct buffer *res;
1052 char str[9];
1053 int ret;
1054
Willy Tarreau721c9742017-11-07 11:05:42 +01001055 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001056 return 1;
1057
1058 if (h2c_mux_busy(h2c, h2s)) {
1059 h2s->flags |= H2_SF_BLK_MBUSY;
1060 return 0;
1061 }
1062
Willy Tarreau44e973f2018-03-01 17:49:30 +01001063 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001064 if (!res) {
1065 h2c->flags |= H2_CF_MUX_MALLOC;
1066 h2s->flags |= H2_SF_BLK_MROOM;
1067 return 0;
1068 }
1069
1070 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1071 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1072 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001073 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001074 if (likely(ret > 0)) {
1075 h2s->flags |= H2_SF_ES_SENT;
1076 }
1077 else if (!ret) {
1078 h2c->flags |= H2_CF_MUX_MFULL;
1079 h2s->flags |= H2_SF_BLK_MROOM;
1080 return 0;
1081 }
1082 else {
1083 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1084 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001085 }
1086 return ret;
1087}
1088
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001089/* wake the streams attached to the connection, whose id is greater than <last>,
1090 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001091 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1092 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001093 */
1094static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1095{
1096 struct eb32_node *node;
1097 struct h2s *h2s;
1098
1099 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1100 flags |= CS_FL_ERROR;
1101
1102 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001103 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001104
1105 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1106 while (node) {
1107 h2s = container_of(node, struct h2s, by_id);
1108 if (h2s->id <= last)
1109 break;
1110 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001111
1112 if (!h2s->cs) {
1113 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001114 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001115 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001116 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001117
1118 h2s->cs->flags |= flags;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001119 if (h2s->recv_wait_list) {
1120 struct wait_list *sw = h2s->recv_wait_list;
1121 sw->wait_reason &= ~SUB_CAN_RECV;
1122 tasklet_wakeup(sw->task);
1123 h2s->recv_wait_list = NULL;
1124 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001125
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001126 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1127 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001128 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001129 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001130 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001131 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001132 }
1133}
1134
Willy Tarreau3421aba2017-07-27 15:41:03 +02001135/* Increase all streams' outgoing window size by the difference passed in
1136 * argument. This is needed upon receipt of the settings frame if the initial
1137 * window size is different. The difference may be negative and the resulting
1138 * window size as well, for the time it takes to receive some window updates.
1139 */
1140static void h2c_update_all_ws(struct h2c *h2c, int diff)
1141{
1142 struct h2s *h2s;
1143 struct eb32_node *node;
1144
1145 if (!diff)
1146 return;
1147
1148 node = eb32_first(&h2c->streams_by_id);
1149 while (node) {
1150 h2s = container_of(node, struct h2s, by_id);
1151 h2s->mws += diff;
1152 node = eb32_next(node);
1153 }
1154}
1155
1156/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1157 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1158 * return an error in h2c. Described in RFC7540#6.5.
1159 */
1160static int h2c_handle_settings(struct h2c *h2c)
1161{
1162 unsigned int offset;
1163 int error;
1164
1165 if (h2c->dff & H2_F_SETTINGS_ACK) {
1166 if (h2c->dfl) {
1167 error = H2_ERR_FRAME_SIZE_ERROR;
1168 goto fail;
1169 }
1170 return 1;
1171 }
1172
1173 if (h2c->dsi != 0) {
1174 error = H2_ERR_PROTOCOL_ERROR;
1175 goto fail;
1176 }
1177
1178 if (h2c->dfl % 6) {
1179 error = H2_ERR_FRAME_SIZE_ERROR;
1180 goto fail;
1181 }
1182
1183 /* that's the limit we can process */
1184 if (h2c->dfl > global.tune.bufsize) {
1185 error = H2_ERR_FRAME_SIZE_ERROR;
1186 goto fail;
1187 }
1188
1189 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001190 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001191 return 0;
1192
1193 /* parse the frame */
1194 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001195 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1196 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001197
1198 switch (type) {
1199 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1200 /* we need to update all existing streams with the
1201 * difference from the previous iws.
1202 */
1203 if (arg < 0) { // RFC7540#6.5.2
1204 error = H2_ERR_FLOW_CONTROL_ERROR;
1205 goto fail;
1206 }
1207 h2c_update_all_ws(h2c, arg - h2c->miw);
1208 h2c->miw = arg;
1209 break;
1210 case H2_SETTINGS_MAX_FRAME_SIZE:
1211 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1212 error = H2_ERR_PROTOCOL_ERROR;
1213 goto fail;
1214 }
1215 h2c->mfs = arg;
1216 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001217 case H2_SETTINGS_ENABLE_PUSH:
1218 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1219 error = H2_ERR_PROTOCOL_ERROR;
1220 goto fail;
1221 }
1222 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001223 }
1224 }
1225
1226 /* need to ACK this frame now */
1227 h2c->st0 = H2_CS_FRAME_A;
1228 return 1;
1229 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001230 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001231 h2c_error(h2c, error);
1232 return 0;
1233}
1234
1235/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1236 * success or one of the h2_status values.
1237 */
1238static int h2c_ack_settings(struct h2c *h2c)
1239{
1240 struct buffer *res;
1241 char str[9];
1242 int ret = -1;
1243
1244 if (h2c_mux_busy(h2c, NULL)) {
1245 h2c->flags |= H2_CF_DEM_MBUSY;
1246 return 0;
1247 }
1248
Willy Tarreau44e973f2018-03-01 17:49:30 +01001249 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001250 if (!res) {
1251 h2c->flags |= H2_CF_MUX_MALLOC;
1252 h2c->flags |= H2_CF_DEM_MROOM;
1253 return 0;
1254 }
1255
1256 memcpy(str,
1257 "\x00\x00\x00" /* length : 0 (no data) */
1258 "\x04" "\x01" /* type : 4, flags : ACK */
1259 "\x00\x00\x00\x00" /* stream ID */, 9);
1260
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001261 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001262 if (unlikely(ret <= 0)) {
1263 if (!ret) {
1264 h2c->flags |= H2_CF_MUX_MFULL;
1265 h2c->flags |= H2_CF_DEM_MROOM;
1266 return 0;
1267 }
1268 else {
1269 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1270 return 0;
1271 }
1272 }
1273 return ret;
1274}
1275
Willy Tarreaucf68c782017-10-10 17:11:41 +02001276/* processes a PING frame and schedules an ACK if needed. The caller must pass
1277 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1278 * missing data. It may return an error in h2c.
1279 */
1280static int h2c_handle_ping(struct h2c *h2c)
1281{
1282 /* frame length must be exactly 8 */
1283 if (h2c->dfl != 8) {
1284 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1285 return 0;
1286 }
1287
1288 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001289 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001290 h2c->st0 = H2_CS_FRAME_A;
1291 return 1;
1292}
1293
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001294/* Try to send a window update for stream id <sid> and value <increment>.
1295 * Returns > 0 on success or zero on missing room or failure. It may return an
1296 * error in h2c.
1297 */
1298static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1299{
1300 struct buffer *res;
1301 char str[13];
1302 int ret = -1;
1303
1304 if (h2c_mux_busy(h2c, NULL)) {
1305 h2c->flags |= H2_CF_DEM_MBUSY;
1306 return 0;
1307 }
1308
Willy Tarreau44e973f2018-03-01 17:49:30 +01001309 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001310 if (!res) {
1311 h2c->flags |= H2_CF_MUX_MALLOC;
1312 h2c->flags |= H2_CF_DEM_MROOM;
1313 return 0;
1314 }
1315
1316 /* length: 4, type: 8, flags: none */
1317 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1318 write_n32(str + 5, sid);
1319 write_n32(str + 9, increment);
1320
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001321 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001322
1323 if (unlikely(ret <= 0)) {
1324 if (!ret) {
1325 h2c->flags |= H2_CF_MUX_MFULL;
1326 h2c->flags |= H2_CF_DEM_MROOM;
1327 return 0;
1328 }
1329 else {
1330 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1331 return 0;
1332 }
1333 }
1334 return ret;
1335}
1336
1337/* try to send pending window update for the connection. It's safe to call it
1338 * with no pending updates. Returns > 0 on success or zero on missing room or
1339 * failure. It may return an error in h2c.
1340 */
1341static int h2c_send_conn_wu(struct h2c *h2c)
1342{
1343 int ret = 1;
1344
1345 if (h2c->rcvd_c <= 0)
1346 return 1;
1347
1348 /* send WU for the connection */
1349 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1350 if (ret > 0)
1351 h2c->rcvd_c = 0;
1352
1353 return ret;
1354}
1355
1356/* try to send pending window update for the current dmux stream. It's safe to
1357 * call it with no pending updates. Returns > 0 on success or zero on missing
1358 * room or failure. It may return an error in h2c.
1359 */
1360static int h2c_send_strm_wu(struct h2c *h2c)
1361{
1362 int ret = 1;
1363
1364 if (h2c->rcvd_s <= 0)
1365 return 1;
1366
1367 /* send WU for the stream */
1368 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1369 if (ret > 0)
1370 h2c->rcvd_s = 0;
1371
1372 return ret;
1373}
1374
Willy Tarreaucf68c782017-10-10 17:11:41 +02001375/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1376 * success, 0 on missing data or one of the h2_status values.
1377 */
1378static int h2c_ack_ping(struct h2c *h2c)
1379{
1380 struct buffer *res;
1381 char str[17];
1382 int ret = -1;
1383
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001384 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001385 return 0;
1386
1387 if (h2c_mux_busy(h2c, NULL)) {
1388 h2c->flags |= H2_CF_DEM_MBUSY;
1389 return 0;
1390 }
1391
Willy Tarreau44e973f2018-03-01 17:49:30 +01001392 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001393 if (!res) {
1394 h2c->flags |= H2_CF_MUX_MALLOC;
1395 h2c->flags |= H2_CF_DEM_MROOM;
1396 return 0;
1397 }
1398
1399 memcpy(str,
1400 "\x00\x00\x08" /* length : 8 (same payload) */
1401 "\x06" "\x01" /* type : 6, flags : ACK */
1402 "\x00\x00\x00\x00" /* stream ID */, 9);
1403
1404 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001405 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001406
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001407 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001408 if (unlikely(ret <= 0)) {
1409 if (!ret) {
1410 h2c->flags |= H2_CF_MUX_MFULL;
1411 h2c->flags |= H2_CF_DEM_MROOM;
1412 return 0;
1413 }
1414 else {
1415 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1416 return 0;
1417 }
1418 }
1419 return ret;
1420}
1421
Willy Tarreau26f95952017-07-27 17:18:30 +02001422/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1423 * Returns > 0 on success or zero on missing data. It may return an error in
1424 * h2c or h2s. Described in RFC7540#6.9.
1425 */
1426static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1427{
1428 int32_t inc;
1429 int error;
1430
1431 if (h2c->dfl != 4) {
1432 error = H2_ERR_FRAME_SIZE_ERROR;
1433 goto conn_err;
1434 }
1435
1436 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001437 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001438 return 0;
1439
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001440 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001441
1442 if (h2c->dsi != 0) {
1443 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001444
1445 /* it's not an error to receive WU on a closed stream */
1446 if (h2s->st == H2_SS_CLOSED)
1447 return 1;
1448
1449 if (!inc) {
1450 error = H2_ERR_PROTOCOL_ERROR;
1451 goto strm_err;
1452 }
1453
1454 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1455 error = H2_ERR_FLOW_CONTROL_ERROR;
1456 goto strm_err;
1457 }
1458
1459 h2s->mws += inc;
1460 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1461 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02001462 /* The task will be waken up later */
Willy Tarreau26f95952017-07-27 17:18:30 +02001463 }
1464 }
1465 else {
1466 /* connection window update */
1467 if (!inc) {
1468 error = H2_ERR_PROTOCOL_ERROR;
1469 goto conn_err;
1470 }
1471
1472 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1473 error = H2_ERR_FLOW_CONTROL_ERROR;
1474 goto conn_err;
1475 }
1476
1477 h2c->mws += inc;
1478 }
1479
1480 return 1;
1481
1482 conn_err:
1483 h2c_error(h2c, error);
1484 return 0;
1485
1486 strm_err:
1487 if (h2s) {
1488 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001489 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001490 }
1491 else
1492 h2c_error(h2c, error);
1493 return 0;
1494}
1495
Willy Tarreaue96b0922017-10-30 00:28:29 +01001496/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1497 * the last ID. Returns > 0 on success or zero on missing data. It may return
1498 * an error in h2c. Described in RFC7540#6.8.
1499 */
1500static int h2c_handle_goaway(struct h2c *h2c)
1501{
1502 int error;
1503 int last;
1504
1505 if (h2c->dsi != 0) {
1506 error = H2_ERR_PROTOCOL_ERROR;
1507 goto conn_err;
1508 }
1509
1510 if (h2c->dfl < 8) {
1511 error = H2_ERR_FRAME_SIZE_ERROR;
1512 goto conn_err;
1513 }
1514
1515 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001516 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001517 return 0;
1518
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001519 last = h2_get_n32(&h2c->dbuf, 0);
1520 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001521 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001522 if (h2c->last_sid < 0)
1523 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001524 return 1;
1525
1526 conn_err:
1527 h2c_error(h2c, error);
1528 return 0;
1529}
1530
Willy Tarreau92153fc2017-12-03 19:46:19 +01001531/* processes a PRIORITY frame, and either skips it or rejects if it is
1532 * invalid. Returns > 0 on success or zero on missing data. It may return
1533 * an error in h2c. Described in RFC7540#6.3.
1534 */
1535static int h2c_handle_priority(struct h2c *h2c)
1536{
1537 int error;
1538
1539 if (h2c->dsi == 0) {
1540 error = H2_ERR_PROTOCOL_ERROR;
1541 goto conn_err;
1542 }
1543
1544 if (h2c->dfl != 5) {
1545 error = H2_ERR_FRAME_SIZE_ERROR;
1546 goto conn_err;
1547 }
1548
1549 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001550 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001551 return 0;
1552
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001553 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001554 /* 7540#5.3 : can't depend on itself */
1555 error = H2_ERR_PROTOCOL_ERROR;
1556 goto conn_err;
1557 }
1558 return 1;
1559
1560 conn_err:
1561 h2c_error(h2c, error);
1562 return 0;
1563}
1564
Willy Tarreaucd234e92017-08-18 10:59:39 +02001565/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1566 * Returns > 0 on success or zero on missing data. It may return an error in
1567 * h2c. Described in RFC7540#6.4.
1568 */
1569static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1570{
1571 int error;
1572
1573 if (h2c->dsi == 0) {
1574 error = H2_ERR_PROTOCOL_ERROR;
1575 goto conn_err;
1576 }
1577
Willy Tarreaucd234e92017-08-18 10:59:39 +02001578 if (h2c->dfl != 4) {
1579 error = H2_ERR_FRAME_SIZE_ERROR;
1580 goto conn_err;
1581 }
1582
1583 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001584 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001585 return 0;
1586
1587 /* late RST, already handled */
1588 if (h2s->st == H2_SS_CLOSED)
1589 return 1;
1590
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001591 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001592 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001593
1594 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001595 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001596 if (h2s->recv_wait_list) {
1597 struct wait_list *sw = h2s->recv_wait_list;
1598
1599 sw->wait_reason &= ~SUB_CAN_RECV;
1600 tasklet_wakeup(sw->task);
1601 h2s->recv_wait_list = NULL;
1602 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001603 }
1604
1605 h2s->flags |= H2_SF_RST_RCVD;
1606 return 1;
1607
1608 conn_err:
1609 h2c_error(h2c, error);
1610 return 0;
1611}
1612
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001613/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1614 * It may return an error in h2c or h2s. The caller must consider that the
1615 * return value is the new h2s in case one was allocated (most common case).
1616 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001617 * errors here are reported as connection errors since it's impossible to
1618 * recover from such errors after the compression context has been altered.
1619 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001620static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001621{
1622 int error;
1623
1624 if (!h2c->dfl) {
1625 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001626 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001627 goto strm_err;
1628 }
1629
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001630 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001631 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001632
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001633 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001634 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001635
Willy Tarreauf2101912018-07-19 10:11:38 +02001636 if (h2c->flags & H2_CF_DEM_TOOMANY)
1637 return 0; // too many cs still present
1638
Willy Tarreau13278b42017-10-13 19:23:14 +02001639 /* now either the frame is complete or the buffer is complete */
1640 if (h2s->st != H2_SS_IDLE) {
1641 /* FIXME: stream already exists, this is only allowed for
1642 * trailers (not supported for now).
1643 */
1644 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001645 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001646 goto conn_err;
1647 }
1648 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1649 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1650 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001651 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001652 goto conn_err;
1653 }
1654
Willy Tarreau22de8d32018-09-05 19:55:58 +02001655 /* Note: we don't emit any other logs below because ff we return
1656 * positively from h2c_stream_new(), the stream will report the error,
1657 * and if we return in error, h2c_stream_new() will emit the error.
1658 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001659 h2s = h2c_stream_new(h2c, h2c->dsi);
1660 if (!h2s) {
1661 error = H2_ERR_INTERNAL_ERROR;
1662 goto conn_err;
1663 }
1664
1665 h2s->st = H2_SS_OPEN;
1666 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1667 h2s->st = H2_SS_HREM;
1668 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001669 /* note: cs cannot be null for now (just created above) */
1670 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001671 }
1672
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001673 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001674 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001675
Willy Tarreau8f650c32017-11-21 19:36:21 +01001676 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001677 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001678
Willy Tarreau721c9742017-11-07 11:05:42 +01001679 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001680 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001681 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001682 }
1683 else {
1684 /* update the max stream ID if the request is being processed */
1685 if (h2s->id > h2c->max_id)
1686 h2c->max_id = h2s->id;
1687 }
1688
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001689 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001690
1691 conn_err:
1692 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001693 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001694
1695 strm_err:
1696 if (h2s) {
1697 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001698 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001699 }
1700 else
1701 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001702 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001703}
1704
Willy Tarreau454f9052017-10-26 19:40:35 +02001705/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1706 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1707 */
1708static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1709{
1710 int error;
1711
1712 /* note that empty DATA frames are perfectly valid and sometimes used
1713 * to signal an end of stream (with the ES flag).
1714 */
1715
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001716 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001717 return 0; // empty buffer
1718
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001720 return 0; // incomplete frame
1721
1722 /* now either the frame is complete or the buffer is complete */
1723
1724 if (!h2c->dsi) {
1725 /* RFC7540#6.1 */
1726 error = H2_ERR_PROTOCOL_ERROR;
1727 goto conn_err;
1728 }
1729
1730 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1731 /* RFC7540#6.1 */
1732 error = H2_ERR_STREAM_CLOSED;
1733 goto strm_err;
1734 }
1735
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001736 if (!h2_frt_transfer_data(h2s))
1737 return 0;
1738
Willy Tarreau454f9052017-10-26 19:40:35 +02001739 /* call the upper layers to process the frame, then let the upper layer
1740 * notify the stream about any change.
1741 */
1742 if (!h2s->cs) {
1743 error = H2_ERR_STREAM_CLOSED;
1744 goto strm_err;
1745 }
1746
Willy Tarreau8f650c32017-11-21 19:36:21 +01001747 if (h2c->st0 >= H2_CS_ERROR)
1748 return 0;
1749
Willy Tarreau721c9742017-11-07 11:05:42 +01001750 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001751 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001752 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001753 }
1754
1755 /* check for completion : the callee will change this to FRAME_A or
1756 * FRAME_H once done.
1757 */
1758 if (h2c->st0 == H2_CS_FRAME_P)
1759 return 0;
1760
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001761
1762 /* last frame */
1763 if (h2c->dff & H2_F_DATA_END_STREAM) {
1764 h2s->st = H2_SS_HREM;
1765 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001766 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001767 }
1768
Willy Tarreau454f9052017-10-26 19:40:35 +02001769 return 1;
1770
1771 conn_err:
1772 h2c_error(h2c, error);
1773 return 0;
1774
1775 strm_err:
1776 if (h2s) {
1777 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001778 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001779 }
1780 else
1781 h2c_error(h2c, error);
1782 return 0;
1783}
1784
Willy Tarreaubc933932017-10-09 16:21:43 +02001785/* process Rx frames to be demultiplexed */
1786static void h2_process_demux(struct h2c *h2c)
1787{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001788 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001789
Willy Tarreau081d4722017-05-16 21:51:05 +02001790 if (h2c->st0 >= H2_CS_ERROR)
1791 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001792
1793 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1794 if (h2c->st0 == H2_CS_PREFACE) {
1795 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1796 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001797 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001798 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001799 sess_log(h2c->conn->owner);
1800 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001801 goto fail;
1802 }
1803
1804 h2c->max_id = 0;
1805 h2c->st0 = H2_CS_SETTINGS1;
1806 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001807
1808 if (h2c->st0 == H2_CS_SETTINGS1) {
1809 struct h2_fh hdr;
1810
1811 /* ensure that what is pending is a valid SETTINGS frame
1812 * without an ACK.
1813 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001814 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001815 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001816 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001817 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001818 sess_log(h2c->conn->owner);
1819 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001820 goto fail;
1821 }
1822
1823 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1824 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1825 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1826 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001827 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001828 goto fail;
1829 }
1830
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001831 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001832 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1833 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1834 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001835 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001836 goto fail;
1837 }
1838
1839 /* that's OK, switch to FRAME_P to process it */
1840 h2c->dfl = hdr.len;
1841 h2c->dsi = hdr.sid;
1842 h2c->dft = hdr.ft;
1843 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001844 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001845 h2c->st0 = H2_CS_FRAME_P;
1846 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001847 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001848
1849 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001850 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001851 int ret = 0;
1852
1853 if (h2c->st0 >= H2_CS_ERROR)
1854 break;
1855
1856 if (h2c->st0 == H2_CS_FRAME_H) {
1857 struct h2_fh hdr;
1858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001860 break;
1861
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001862 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001863 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1864 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001865 if (!h2c->nb_streams) {
1866 /* only log if no other stream can report the error */
1867 sess_log(h2c->conn->owner);
1868 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001869 break;
1870 }
1871
1872 h2c->dfl = hdr.len;
1873 h2c->dsi = hdr.sid;
1874 h2c->dft = hdr.ft;
1875 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001876 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001877 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001878 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001879 }
1880
1881 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001882 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1883
Olivier Houchard638b7992018-08-16 15:41:52 +02001884 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001885 /* we may have to signal the upper layers */
1886 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001887 if (h2s->recv_wait_list) {
1888 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
1889 tasklet_wakeup(h2s->recv_wait_list->task);
1890 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001891 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001892 if (h2c->st0 >= H2_CS_ERROR)
1893 goto strm_err;
1894
1895 if (h2s->st >= H2_SS_ERROR) {
1896 /* stream error : send RST_STREAM */
1897 h2c->st0 = H2_CS_FRAME_E;
1898 }
1899 }
1900 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001901
Willy Tarreaud7901432017-12-29 11:34:40 +01001902 if (h2c->st0 == H2_CS_FRAME_E)
1903 goto strm_err;
1904
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001905 if (h2s->st == H2_SS_IDLE &&
1906 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1907 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1908 * this state MUST be treated as a connection error
1909 */
1910 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1911 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001912 if (!h2c->nb_streams) {
1913 /* only log if no other stream can report the error */
1914 sess_log(h2c->conn->owner);
1915 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001916 break;
1917 }
1918
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001919 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1920 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1921 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1922 * this state MUST be treated as a stream error
1923 */
1924 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1925 goto strm_err;
1926 }
1927
Willy Tarreauab837502017-12-27 15:07:30 +01001928 /* Below the management of frames received in closed state is a
1929 * bit hackish because the spec makes strong differences between
1930 * streams closed by receiving RST, sending RST, and seeing ES
1931 * in both directions. In addition to this, the creation of a
1932 * new stream reusing the identifier of a closed one will be
1933 * detected here. Given that we cannot keep track of all closed
1934 * streams forever, we consider that unknown closed streams were
1935 * closed on RST received, which allows us to respond with an
1936 * RST without breaking the connection (eg: to abort a transfer).
1937 * Some frames have to be silently ignored as well.
1938 */
1939 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1940 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1941 /* #5.1.1: The identifier of a newly
1942 * established stream MUST be numerically
1943 * greater than all streams that the initiating
1944 * endpoint has opened or reserved. This
1945 * governs streams that are opened using a
1946 * HEADERS frame and streams that are reserved
1947 * using PUSH_PROMISE. An endpoint that
1948 * receives an unexpected stream identifier
1949 * MUST respond with a connection error.
1950 */
1951 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1952 goto strm_err;
1953 }
1954
1955 if (h2s->flags & H2_SF_RST_RCVD) {
1956 /* RFC7540#5.1:closed: an endpoint that
1957 * receives any frame other than PRIORITY after
1958 * receiving a RST_STREAM MUST treat that as a
1959 * stream error of type STREAM_CLOSED.
1960 *
1961 * Note that old streams fall into this category
1962 * and will lead to an RST being sent.
1963 */
1964 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1965 h2c->st0 = H2_CS_FRAME_E;
1966 goto strm_err;
1967 }
1968
1969 /* RFC7540#5.1:closed: if this state is reached as a
1970 * result of sending a RST_STREAM frame, the peer that
1971 * receives the RST_STREAM might have already sent
1972 * frames on the stream that cannot be withdrawn. An
1973 * endpoint MUST ignore frames that it receives on
1974 * closed streams after it has sent a RST_STREAM
1975 * frame. An endpoint MAY choose to limit the period
1976 * over which it ignores frames and treat frames that
1977 * arrive after this time as being in error.
1978 */
1979 if (!(h2s->flags & H2_SF_RST_SENT)) {
1980 /* RFC7540#5.1:closed: any frame other than
1981 * PRIO/WU/RST in this state MUST be treated as
1982 * a connection error
1983 */
1984 if (h2c->dft != H2_FT_RST_STREAM &&
1985 h2c->dft != H2_FT_PRIORITY &&
1986 h2c->dft != H2_FT_WINDOW_UPDATE) {
1987 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1988 goto strm_err;
1989 }
1990 }
1991 }
1992
Willy Tarreauc0da1962017-10-30 18:38:00 +01001993#if 0
1994 // problem below: it is not possible to completely ignore such
1995 // streams as we need to maintain the compression state as well
1996 // and for this we need to completely process these frames (eg:
1997 // HEADERS frames) as well as counting DATA frames to emit
1998 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1999 // This is a typical case of layer violation where the
2000 // transported contents are critical to the connection's
2001 // validity and must be ignored at the same time :-(
2002
2003 /* graceful shutdown, ignore streams whose ID is higher than
2004 * the one advertised in GOAWAY. RFC7540#6.8.
2005 */
2006 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002007 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2008 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002009 h2c->dfl -= ret;
2010 ret = h2c->dfl == 0;
2011 goto strm_err;
2012 }
2013#endif
2014
Willy Tarreau7e98c052017-10-10 15:56:59 +02002015 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002016 case H2_FT_SETTINGS:
2017 if (h2c->st0 == H2_CS_FRAME_P)
2018 ret = h2c_handle_settings(h2c);
2019
2020 if (h2c->st0 == H2_CS_FRAME_A)
2021 ret = h2c_ack_settings(h2c);
2022 break;
2023
Willy Tarreaucf68c782017-10-10 17:11:41 +02002024 case H2_FT_PING:
2025 if (h2c->st0 == H2_CS_FRAME_P)
2026 ret = h2c_handle_ping(h2c);
2027
2028 if (h2c->st0 == H2_CS_FRAME_A)
2029 ret = h2c_ack_ping(h2c);
2030 break;
2031
Willy Tarreau26f95952017-07-27 17:18:30 +02002032 case H2_FT_WINDOW_UPDATE:
2033 if (h2c->st0 == H2_CS_FRAME_P)
2034 ret = h2c_handle_window_update(h2c, h2s);
2035 break;
2036
Willy Tarreau61290ec2017-10-17 08:19:21 +02002037 case H2_FT_CONTINUATION:
2038 /* we currently don't support CONTINUATION frames since
2039 * we have nowhere to store the partial HEADERS frame.
2040 * Let's abort the stream on an INTERNAL_ERROR here.
2041 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002042 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002043 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002044 h2c->st0 = H2_CS_FRAME_E;
2045 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002046 break;
2047
Willy Tarreau13278b42017-10-13 19:23:14 +02002048 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002049 if (h2c->st0 == H2_CS_FRAME_P) {
2050 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2051 if (tmp_h2s) {
2052 h2s = tmp_h2s;
2053 ret = 1;
2054 }
2055 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002056 break;
2057
Willy Tarreau454f9052017-10-26 19:40:35 +02002058 case H2_FT_DATA:
2059 if (h2c->st0 == H2_CS_FRAME_P)
2060 ret = h2c_frt_handle_data(h2c, h2s);
2061
2062 if (h2c->st0 == H2_CS_FRAME_A)
2063 ret = h2c_send_strm_wu(h2c);
2064 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002065
Willy Tarreau92153fc2017-12-03 19:46:19 +01002066 case H2_FT_PRIORITY:
2067 if (h2c->st0 == H2_CS_FRAME_P)
2068 ret = h2c_handle_priority(h2c);
2069 break;
2070
Willy Tarreaucd234e92017-08-18 10:59:39 +02002071 case H2_FT_RST_STREAM:
2072 if (h2c->st0 == H2_CS_FRAME_P)
2073 ret = h2c_handle_rst_stream(h2c, h2s);
2074 break;
2075
Willy Tarreaue96b0922017-10-30 00:28:29 +01002076 case H2_FT_GOAWAY:
2077 if (h2c->st0 == H2_CS_FRAME_P)
2078 ret = h2c_handle_goaway(h2c);
2079 break;
2080
Willy Tarreau1c661982017-10-30 13:52:01 +01002081 case H2_FT_PUSH_PROMISE:
2082 /* not permitted here, RFC7540#5.1 */
2083 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002084 if (!h2c->nb_streams) {
2085 /* only log if no other stream can report the error */
2086 sess_log(h2c->conn->owner);
2087 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002088 break;
2089
2090 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002091 default:
2092 /* drop frames that we ignore. They may be larger than
2093 * the buffer so we drain all of their contents until
2094 * we reach the end.
2095 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002096 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2097 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002098 h2c->dfl -= ret;
2099 ret = h2c->dfl == 0;
2100 }
2101
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002102 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002103 /* We may have to send an RST if not done yet */
2104 if (h2s->st == H2_SS_ERROR)
2105 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002106
Willy Tarreaua20a5192017-12-27 11:02:06 +01002107 if (h2c->st0 == H2_CS_FRAME_E)
2108 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002109
Willy Tarreau7e98c052017-10-10 15:56:59 +02002110 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002111 if (ret <= 0) {
2112 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002113 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002114 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002115
2116 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002117 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002118 h2c->st0 = H2_CS_FRAME_H;
2119 }
2120 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002121
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002122 if (h2c->rcvd_c > 0 &&
2123 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2124 h2c_send_conn_wu(h2c);
2125
Willy Tarreau52eed752017-09-22 15:05:09 +02002126 fail:
2127 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002128 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002129 /* we may have to signal the upper layers */
2130 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002131 if (h2s->recv_wait_list) {
2132 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
2133 tasklet_wakeup(h2s->recv_wait_list->task);
2134 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002135 }
2136 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002137 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002138}
2139
2140/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2141 * the end.
2142 */
2143static int h2_process_mux(struct h2c *h2c)
2144{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002145 struct h2s *h2s;
2146 struct wait_list *sw, *sw_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002147
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002148 /* start by sending possibly pending window updates */
2149 if (h2c->rcvd_c > 0 &&
2150 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2151 h2c_send_conn_wu(h2c) < 0)
2152 goto fail;
2153
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002154 /* First we always process the flow control list because the streams
2155 * waiting there were already elected for immediate emission but were
2156 * blocked just on this.
2157 */
2158
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002159 list_for_each_entry_safe(sw, sw_back, &h2c->fctl_list, list) {
2160 h2s = sw->handle;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002161 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2162 h2c->st0 >= H2_CS_ERROR)
2163 break;
2164
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002165 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2166 if ((long)(h2s) & 3) {
2167 sw->wait_reason &= ~SUB_CAN_SEND;
2168 LIST_DEL(&sw->list);
2169 LIST_INIT(&sw->list);
2170 tasklet_wakeup(sw->task);
2171 } else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2172 h2s->flags &= ~H2_SF_BLK_ANY;
2173 LIST_DEL(&sw->list);
2174 LIST_INIT(&sw->list);
2175 sw->wait_reason &= ~SUB_CAN_SEND;
2176 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002177 }
2178 }
2179
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002180 list_for_each_entry_safe(sw, sw_back, &h2c->send_list, list) {
2181 h2s = sw->handle;
2182
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002183 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2184 break;
2185
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002186 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2187 if ((long)(h2s) & 3) {
2188 sw->wait_reason &= ~SUB_CAN_SEND;
2189 LIST_DEL(&sw->list);
2190 LIST_INIT(&sw->list);
2191 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002192 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002193 else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2194 h2s->flags &= ~H2_SF_BLK_ANY;
2195
2196 LIST_DEL(&sw->list);
2197 LIST_INIT(&sw->list);
2198 sw->wait_reason &= ~SUB_CAN_SEND;
2199 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002200 }
2201 }
2202
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002203 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002204 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002205 if (h2c->st0 == H2_CS_ERROR) {
2206 if (h2c->max_id >= 0) {
2207 h2c_send_goaway_error(h2c, NULL);
2208 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2209 return 0;
2210 }
2211
2212 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2213 }
2214 return 1;
2215 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002216 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002217}
2218
Willy Tarreau62f52692017-10-08 23:01:42 +02002219
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002220/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002221static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002222{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002223 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002224 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002225 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002226 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002227
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002228 if (h2c->wait_list.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002229 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002230
Willy Tarreau315d8072017-12-10 22:17:57 +01002231 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002232 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002233
Willy Tarreau44e973f2018-03-01 17:49:30 +01002234 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002235 if (!buf) {
2236 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002237 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002238 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002239
Olivier Houchard7505f942018-08-21 18:10:44 +02002240 do {
2241 max = buf->size - b_data(buf);
2242 if (max)
2243 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2244 else
2245 ret = 0;
2246 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002247
Olivier Houchard7505f942018-08-21 18:10:44 +02002248 if (h2_recv_allowed(h2c)) {
2249 conn_xprt_want_recv(conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002250 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_list);
Olivier Houchard7505f942018-08-21 18:10:44 +02002251 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002252 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002253 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002254 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002255 }
2256
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002257 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002258 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002259 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002260}
2261
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002262/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002263static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002264{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002265 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002266 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002267 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002268
2269 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002270 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002271
Olivier Houchard7505f942018-08-21 18:10:44 +02002272
Willy Tarreaua2af5122017-10-09 11:56:46 +02002273 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2274 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002275 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002276 }
2277
Willy Tarreaubc933932017-10-09 16:21:43 +02002278 /* This loop is quite simple : it tries to fill as much as it can from
2279 * pending streams into the existing buffer until it's reportedly full
2280 * or the end of send requests is reached. Then it tries to send this
2281 * buffer's contents out, marks it not full if at least one byte could
2282 * be sent, and tries again.
2283 *
2284 * The snd_buf() function normally takes a "flags" argument which may
2285 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2286 * data immediately comes and CO_SFL_STREAMER to indicate that the
2287 * connection is streaming lots of data (used to increase TLS record
2288 * size at the expense of latency). The former can be sent any time
2289 * there's a buffer full flag, as it indicates at least one stream
2290 * attempted to send and failed so there are pending data. An
2291 * alternative would be to set it as long as there's an active stream
2292 * but that would be problematic for ACKs until we have an absolute
2293 * guarantee that all waiters have at least one byte to send. The
2294 * latter should possibly not be set for now.
2295 */
2296
2297 done = 0;
2298 while (!done) {
2299 unsigned int flags = 0;
2300
2301 /* fill as much as we can into the current buffer */
2302 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2303 done = h2_process_mux(h2c);
2304
2305 if (conn->flags & CO_FL_ERROR)
2306 break;
2307
2308 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2309 flags |= CO_SFL_MSG_MORE;
2310
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002311 if (b_data(&h2c->mbuf)) {
2312 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002313 if (!ret)
2314 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002315 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002316 b_del(&h2c->mbuf, ret);
2317 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002318 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002319
2320 /* wrote at least one byte, the buffer is not full anymore */
2321 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2322 }
2323
Willy Tarreaua2af5122017-10-09 11:56:46 +02002324 if (conn->flags & CO_FL_SOCK_WR_SH) {
2325 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002326 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002327 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002328 /* We're not full anymore, so we can wake any task that are waiting
2329 * for us.
2330 */
2331 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002332 while (!LIST_ISEMPTY(&h2c->send_list)) {
2333 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
Olivier Houchard6ff20392018-07-17 18:46:31 +02002334 struct wait_list *, list);
2335 LIST_DEL(&sw->list);
2336 LIST_INIT(&sw->list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002337 sw->wait_reason &= ~SUB_CAN_SEND;
2338 tasklet_wakeup(sw->task);
2339 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002340 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002341 /* We're done, no more to send */
2342 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002343 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002344schedule:
2345 if (LIST_ISEMPTY(&h2c->wait_list.list))
2346 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002347 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002348}
2349
2350static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2351{
2352 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002353 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002354
2355 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002356 ret = h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002357 if (!(h2c->wait_list.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002358 ret |= h2_recv(h2c);
2359 if (ret)
2360 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002361 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002362}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002363
Willy Tarreau62f52692017-10-08 23:01:42 +02002364/* callback called on any event by the connection handler.
2365 * It applies changes and returns zero, or < 0 if it wants immediate
2366 * destruction of the connection (which normally doesn not happen in h2).
2367 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002368static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002369{
Olivier Houchard7505f942018-08-21 18:10:44 +02002370 struct connection *conn = h2c->conn;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002371 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002372
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002373 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002374 h2_process_demux(h2c);
2375
2376 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002377 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002378
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002379 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002380 h2c->flags &= ~H2_CF_DEM_DFULL;
2381 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002382 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002383
Willy Tarreau8ec14062017-12-30 18:08:13 +01002384 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2385 /* frontend is stopping, reload likely in progress, let's try
2386 * to announce a graceful shutdown if not yet done. We don't
2387 * care if it fails, it will be tried again later.
2388 */
2389 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2390 if (h2c->last_sid < 0)
2391 h2c->last_sid = (1U << 31) - 1;
2392 h2c_send_goaway_error(h2c, NULL);
2393 }
2394 }
2395
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002396 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002397 * If we received early data, and the handshake is done, wake
2398 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002399 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002400 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2401 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2402 struct eb32_node *node;
2403 struct h2s *h2s;
2404
2405 h2c->flags |= H2_CF_WAIT_FOR_HS;
2406 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2407
2408 while (node) {
2409 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002410 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
2411 h2s->recv_wait_list) {
2412 struct wait_list *sw = h2s->recv_wait_list;
2413 sw->wait_reason &= ~SUB_CAN_RECV;
2414 tasklet_wakeup(sw->task);
2415 h2s->recv_wait_list = NULL;
2416 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002417 node = eb32_next(node);
2418 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002419 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002420
Willy Tarreau26bd7612017-10-09 16:47:04 +02002421 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002422 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2423 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2424 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002425 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002426
2427 if (eb_is_empty(&h2c->streams_by_id)) {
2428 /* no more stream, kill the connection now */
2429 h2_release(conn);
2430 return -1;
2431 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002432 }
2433
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002434 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002435 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002436
2437 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002438 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002439 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002440 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002441 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442
2443 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002444 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002445 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002446 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002447 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002448 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2449 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002450 __conn_xprt_want_send(conn);
2451 }
2452 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002453 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002454 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002455 }
2456
Willy Tarreau3f133572017-10-31 19:21:06 +01002457 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002458 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002459 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002460 task_queue(h2c->task);
2461 }
2462 else
2463 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002464 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002465
Olivier Houchard7505f942018-08-21 18:10:44 +02002466 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002467 return 0;
2468}
2469
Willy Tarreauea392822017-10-31 10:02:25 +01002470/* Connection timeout management. The principle is that if there's no receipt
2471 * nor sending for a certain amount of time, the connection is closed. If the
2472 * MUX buffer still has lying data or is not allocatable, the connection is
2473 * immediately killed. If it's allocatable and empty, we attempt to send a
2474 * GOAWAY frame.
2475 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002476static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002477{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002478 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002479 int expired = tick_is_expired(t->expire, now_ms);
2480
Willy Tarreau0975f112018-03-29 15:22:59 +02002481 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002482 return t;
2483
Willy Tarreau0975f112018-03-29 15:22:59 +02002484 task_delete(t);
2485 task_free(t);
2486
2487 if (!h2c) {
2488 /* resources were already deleted */
2489 return NULL;
2490 }
2491
2492 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002493 h2c_error(h2c, H2_ERR_NO_ERROR);
2494 h2_wake_some_streams(h2c, 0, 0);
2495
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002496 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002497 /* don't even try to send a GOAWAY, the buffer is stuck */
2498 h2c->flags |= H2_CF_GOAWAY_FAILED;
2499 }
2500
2501 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002502 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002503 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2504 h2c->flags |= H2_CF_GOAWAY_FAILED;
2505
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002506 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2507 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002508 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002509 b_del(&h2c->mbuf, ret);
2510 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002511 }
2512 }
Willy Tarreauea392822017-10-31 10:02:25 +01002513
Willy Tarreau0975f112018-03-29 15:22:59 +02002514 /* either we can release everything now or it will be done later once
2515 * the last stream closes.
2516 */
2517 if (eb_is_empty(&h2c->streams_by_id))
2518 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002519
Willy Tarreauea392822017-10-31 10:02:25 +01002520 return NULL;
2521}
2522
2523
Willy Tarreau62f52692017-10-08 23:01:42 +02002524/*******************************************/
2525/* functions below are used by the streams */
2526/*******************************************/
2527
2528/*
2529 * Attach a new stream to a connection
2530 * (Used for outgoing connections)
2531 */
2532static struct conn_stream *h2_attach(struct connection *conn)
2533{
2534 return NULL;
2535}
2536
2537/* callback used to update the mux's polling flags after changing a cs' status.
2538 * The caller (cs_update_mux_polling) will take care of propagating any changes
2539 * to the transport layer.
2540 */
2541static void h2_update_poll(struct conn_stream *cs)
2542{
Willy Tarreau1d393222017-10-17 10:26:19 +02002543 struct h2s *h2s = cs->ctx;
2544
2545 if (!h2s)
2546 return;
2547
Willy Tarreaud7739c82017-10-30 15:38:23 +01002548 /* we may unblock a blocked read */
2549
Willy Tarreau315d8072017-12-10 22:17:57 +01002550 if (cs->flags & CS_FL_DATA_RD_ENA) {
2551 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002552 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002553 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002554 conn_xprt_want_recv(cs->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002555 if (h2_recv(h2s->h2c))
2556 h2_process(h2s->h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002557 conn_xprt_want_send(cs->conn);
2558 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002559 }
2560
Willy Tarreau1d393222017-10-17 10:26:19 +02002561 /* Note: the stream and stream-int code doesn't allow us to perform a
2562 * synchronous send() here unfortunately, because this code is called
2563 * as si_update() from the process_stream() context. This means that
2564 * we have to queue the current cs and defer its processing after the
2565 * connection's cs list is processed anyway.
2566 */
2567
2568 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002569 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2570 conn_xprt_want_send(cs->conn);
2571 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002572 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002573 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002574
2575 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002576 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002577 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002578}
2579
2580/*
2581 * Detach the stream from the connection and possibly release the connection.
2582 */
2583static void h2_detach(struct conn_stream *cs)
2584{
Willy Tarreau60935142017-10-16 18:11:19 +02002585 struct h2s *h2s = cs->ctx;
2586 struct h2c *h2c;
2587
2588 cs->ctx = NULL;
2589 if (!h2s)
2590 return;
2591
2592 h2c = h2s->h2c;
2593 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002594 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002595 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2596 !h2_has_too_many_cs(h2c)) {
2597 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2598 if (h2_recv_allowed(h2c)) {
2599 __conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002600 h2_recv(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002601 conn_xprt_want_send(h2c->conn);
2602 }
2603 }
Willy Tarreau60935142017-10-16 18:11:19 +02002604
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002605 /* this stream may be blocked waiting for some data to leave (possibly
2606 * an ES or RST frame), so orphan it in this case.
2607 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002608 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002609 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002610 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002611 return;
2612
Willy Tarreau45f752e2017-10-30 15:44:59 +01002613 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2614 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2615 /* unblock the connection if it was blocked on this
2616 * stream.
2617 */
2618 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2619 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2620 conn_xprt_want_recv(cs->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002621 h2_recv(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002622 conn_xprt_want_send(cs->conn);
2623 }
2624
Willy Tarreau71049cc2018-03-28 13:56:39 +02002625 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002626
Willy Tarreaue323f342018-03-28 13:51:45 +02002627 /* We don't want to close right now unless we're removing the
2628 * last stream, and either the connection is in error, or it
2629 * reached the ID already specified in a GOAWAY frame received
2630 * or sent (as seen by last_sid >= 0).
2631 */
2632 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2633 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002634 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002635 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002636 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002637 (conn_xprt_read0_pending(h2c->conn) ||
2638 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2639 /* no more stream will come, kill it now */
2640 h2_release(h2c->conn);
2641 }
2642 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002643 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002644 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2645 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002646 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002647 else
2648 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002649 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002650}
2651
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002652static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002653{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002654 struct h2c *h2c = h2s->h2c;
2655 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002656
Willy Tarreau721c9742017-11-07 11:05:42 +01002657 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002658 return;
2659
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002660 /* if no outgoing data was seen on this stream, it means it was
2661 * closed with a "tcp-request content" rule that is normally
2662 * used to kill the connection ASAP (eg: limit abuse). In this
2663 * case we send a goaway to close the connection.
2664 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002665 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002666 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002667 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002668
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002669 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2670 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671 h2c_send_goaway_error(h2c, h2s) <= 0)
2672 return;
2673 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2674 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002675
Willy Tarreau00dd0782018-03-01 16:31:34 +01002676 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002677
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002678 return;
2679add_to_list:
2680 if (LIST_ISEMPTY(&sw->list)) {
2681 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002682 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002683 LIST_ADDQ(&h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002684 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002685 LIST_ADDQ(&h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002686 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002687 /* Let the handler know we want shutr */
2688 sw->handle = (void *)((long)sw->handle | 1);
2689
Willy Tarreau62f52692017-10-08 23:01:42 +02002690}
2691
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002692static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002693{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002694 struct h2c *h2c = h2s->h2c;
2695 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002696
Willy Tarreau721c9742017-11-07 11:05:42 +01002697 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002698 return;
2699
Willy Tarreau67434202017-11-06 20:20:51 +01002700 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002701 /* we can cleanly close using an empty data frame only after headers */
2702
2703 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2704 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002705 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002706
2707 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002708 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002709 else
2710 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002711 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002712 /* if no outgoing data was seen on this stream, it means it was
2713 * closed with a "tcp-request content" rule that is normally
2714 * used to kill the connection ASAP (eg: limit abuse). In this
2715 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002716 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002717 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002718 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002719 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002720
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002721 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2722 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002723 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002724 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002725
Willy Tarreau00dd0782018-03-01 16:31:34 +01002726 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002727 }
2728
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002729 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2730 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002731
2732 add_to_list:
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002733 sw = &h2s->wait_list;
2734
2735 if (LIST_ISEMPTY(&sw->list)) {
2736 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002737 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002738 LIST_ADDQ(&h2s->h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002739 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002740 LIST_ADDQ(&h2s->h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002741 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002742 /* let the handler know we want to shutr */
2743 sw->handle = (void *)((long)(sw->handle) | 2);
2744}
2745
2746static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2747{
2748 struct h2s *h2s = ctx;
2749 long reason = (long)h2s->wait_list.handle;
2750
2751 if (reason & 1)
2752 h2_do_shutr(h2s);
2753 if (reason & 2)
2754 h2_do_shutw(h2s);
2755
2756 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002757}
2758
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002759static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2760{
2761 struct h2s *h2s = cs->ctx;
2762
2763 if (!mode)
2764 return;
2765
2766 h2_do_shutr(h2s);
2767}
2768
2769static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2770{
2771 struct h2s *h2s = cs->ctx;
2772
2773 h2_do_shutw(h2s);
2774}
2775
Willy Tarreau13278b42017-10-13 19:23:14 +02002776/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2777 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2778 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002779 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002780 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002781static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002782{
2783 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002784 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002785 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002786 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002787 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002788 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002789 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002790 int flen = h2c->dfl;
2791 int outlen = 0;
2792 int wrap;
2793 int try;
2794
2795 if (!h2c->dfl) {
2796 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002797 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002798 return 0;
2799 }
2800
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002801 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002802 return 0; // incomplete input frame
2803
Willy Tarreau13278b42017-10-13 19:23:14 +02002804 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002805 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002806 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002807 copy = alloc_trash_chunk();
2808 if (!copy) {
2809 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2810 goto fail;
2811 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002812 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2813 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2814 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002815 }
2816
2817 /* The padlen is the first byte before data, and the padding appears
2818 * after data. padlen+data+padding are included in flen.
2819 */
2820 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002821 h2c->dpl = *hdrs;
2822 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002823 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2824 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002825 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002826 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002827 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002828 hdrs += 1; // skip Pad Length
2829 }
2830
2831 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2832 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002833 if (read_n32(hdrs) == h2s->id) {
2834 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2835 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002836 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002837 }
2838
Willy Tarreau13278b42017-10-13 19:23:14 +02002839 hdrs += 5; // stream dep = 4, weight = 1
2840 flen -= 5;
2841 }
2842
2843 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2844 * don't support this for now and can't even decompress so we have to
2845 * break the connection.
2846 */
2847 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2848 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002849 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002850 }
2851
Olivier Houchard638b7992018-08-16 15:41:52 +02002852 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002853 if (!csbuf) {
2854 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002855 goto fail;
2856 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002857
Willy Tarreau937f7602018-02-26 15:22:17 +01002858 /* we can't retry a failed decompression operation so we must be very
2859 * careful not to take any risks. In practice the output buffer is
2860 * always empty except maybe for trailers, in which case we simply have
2861 * to wait for the upper layer to finish consuming what is available.
2862 */
2863 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002864 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002865
Willy Tarreau937f7602018-02-26 15:22:17 +01002866 csbuf->head = 0;
2867 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002868
2869 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2870 sizeof(list)/sizeof(list[0]), tmp);
2871 if (outlen < 0) {
2872 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2873 goto fail;
2874 }
2875
2876 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002877 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002878 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002879
2880 if (outlen < 0) {
2881 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2882 goto fail;
2883 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002884
Willy Tarreau174b06a2018-04-25 18:13:58 +02002885 if (msgf & H2_MSGF_BODY) {
2886 /* a payload is present */
2887 if (msgf & H2_MSGF_BODY_CL)
2888 h2s->flags |= H2_SF_DATA_CLEN;
2889 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2890 h2s->flags |= H2_SF_DATA_CHNK;
2891 }
2892
Willy Tarreau13278b42017-10-13 19:23:14 +02002893 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002895 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002896 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002897
Willy Tarreau39d68502018-03-02 12:26:37 +01002898 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002899 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002900 h2s->cs->flags |= CS_FL_REOS;
2901 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002902
Willy Tarreau68dd9852017-07-03 14:44:26 +02002903 leave:
2904 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002905 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002906 fail:
2907 outlen = 0;
2908 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002909}
2910
Willy Tarreau454f9052017-10-26 19:40:35 +02002911/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2912 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2913 * in use, a new chunk is emitted for each frame. This is supposed to fit
2914 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2915 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2916 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2917 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002918 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2919 * checked to know if some data remain pending (an empty DATA frame can return
2920 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2921 * connection errors in h2c->errcode. The caller must already have checked the
2922 * frame header and ensured that the frame was complete or the buffer full. It
2923 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002924 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002925static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002926{
2927 struct h2c *h2c = h2s->h2c;
2928 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002929 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002930 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002931 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002932
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002933 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002934
2935 /* The padlen is the first byte before data, and the padding appears
2936 * after data. padlen+data+padding are included in flen.
2937 */
Willy Tarreau79127812017-12-03 21:06:59 +01002938 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002940 return 0;
2941
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002942 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002943 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002944 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2945 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002946 return 0;
2947 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002948
2949 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002951 h2c->dfl--;
2952 h2c->rcvd_c++; h2c->rcvd_s++;
2953 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002954 }
2955
Olivier Houchard638b7992018-08-16 15:41:52 +02002956 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002957 if (!csbuf) {
2958 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002959 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002960 }
2961
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002962 flen = h2c->dfl - h2c->dpl;
2963 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002964 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002965
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002966 if (flen > b_data(&h2c->dbuf)) {
2967 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002968 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002969 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002970 }
2971
2972 if (unlikely(b_space_wraps(csbuf))) {
2973 /* it doesn't fit and the buffer is fragmented,
2974 * so let's defragment it and try again.
2975 */
2976 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002977 }
2978
Willy Tarreaueba10f22018-04-25 20:44:22 +02002979 /* chunked-encoding requires more room */
2980 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002981 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002982 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2983 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2984 (chklen < 1048576) ? 4 : 8;
2985 chklen += 4; // CRLF, CRLF
2986 }
2987
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002988 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002989 if (flen + chklen > b_room(csbuf)) {
2990 if (chklen >= b_room(csbuf)) {
2991 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002992 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002993 }
2994 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002995 }
2996
2997 if (h2s->flags & H2_SF_DATA_CHNK) {
2998 /* emit the chunk size */
2999 unsigned int chksz = flen;
3000 char str[10];
3001 char *beg;
3002
3003 beg = str + sizeof(str);
3004 *--beg = '\n';
3005 *--beg = '\r';
3006 do {
3007 *--beg = hextab[chksz & 0xF];
3008 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003009 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003010 }
3011
Willy Tarreau454f9052017-10-26 19:40:35 +02003012 /* Block1 is the length of the first block before the buffer wraps,
3013 * block2 is the optional second block to reach the end of the frame.
3014 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003015 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003016 if (block1 > flen)
3017 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003018 block2 = flen - block1;
3019
3020 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003021 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003022
3023 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003024 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003025
Willy Tarreaueba10f22018-04-25 20:44:22 +02003026 if (h2s->flags & H2_SF_DATA_CHNK) {
3027 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003028 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003029 }
3030
Willy Tarreau454f9052017-10-26 19:40:35 +02003031 /* now mark the input data as consumed (will be deleted from the buffer
3032 * by the caller when seeing FRAME_A after sending the window update).
3033 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003034 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003035 h2c->dfl -= flen;
3036 h2c->rcvd_c += flen;
3037 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3038
3039 if (h2c->dfl > h2c->dpl) {
3040 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003041 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003042 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003043 }
3044
Willy Tarreau4a28da12018-01-04 14:41:00 +01003045 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003046 /* here we're done with the frame, all the payload (except padding) was
3047 * transferred.
3048 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003049
3050 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3051 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003052 if (b_room(csbuf) < 5) {
3053 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003054 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003055 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003056 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003057 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003058 }
3059
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003060 h2c->rcvd_c += h2c->dpl;
3061 h2c->rcvd_s += h2c->dpl;
3062 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003063 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3064
Willy Tarreau39d68502018-03-02 12:26:37 +01003065 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003066 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003067 h2s->cs->flags |= CS_FL_REOS;
3068 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003069
Willy Tarreau454b57b2018-02-26 15:50:05 +01003070 return flen + chklen;
3071 fail:
3072 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003073}
3074
Willy Tarreau5dd17352018-06-14 13:33:30 +02003075/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3076 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3077 * number of bytes sent. The caller must check the stream's status to detect
3078 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003079 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003080static 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 +02003081{
3082 struct http_hdr list[MAX_HTTP_HDR];
3083 struct h2c *h2c = h2s->h2c;
3084 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003085 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003086 int es_now = 0;
3087 int ret = 0;
3088 int hdr;
3089
3090 if (h2c_mux_busy(h2c, h2s)) {
3091 h2s->flags |= H2_SF_BLK_MBUSY;
3092 return 0;
3093 }
3094
Willy Tarreau44e973f2018-03-01 17:49:30 +01003095 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003096 h2c->flags |= H2_CF_MUX_MALLOC;
3097 h2s->flags |= H2_SF_BLK_MROOM;
3098 return 0;
3099 }
3100
3101 /* First, try to parse the H1 response and index it into <list>.
3102 * NOTE! Since it comes from haproxy, we *know* that a response header
3103 * block does not wrap and we can safely read it this way without
3104 * having to realign the buffer.
3105 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003106 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003107 list, sizeof(list)/sizeof(list[0]), h1m);
3108 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003109 /* incomplete or invalid response, this is abnormal coming from
3110 * haproxy and may only result in a bad errorfile or bad Lua code
3111 * so that won't be fixed, raise an error now.
3112 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003113 * FIXME: we should instead add the ability to only return a
3114 * 502 bad gateway. But in theory this is not supposed to
3115 * happen.
3116 */
3117 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3118 ret = 0;
3119 goto end;
3120 }
3121
3122 chunk_reset(&outbuf);
3123
3124 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003125 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003126 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003127 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003128
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003129 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003130 break;
3131 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003132 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003133 }
3134
3135 if (outbuf.size < 9) {
3136 h2c->flags |= H2_CF_MUX_MFULL;
3137 h2s->flags |= H2_SF_BLK_MROOM;
3138 ret = 0;
3139 goto end;
3140 }
3141
3142 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003143 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3144 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3145 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003146
3147 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003148 if (outbuf.data < outbuf.size && h1m->status == 200)
3149 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3150 else if (outbuf.data < outbuf.size && h1m->status == 304)
3151 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003152 else if (unlikely(list[0].v.len != 3)) {
3153 /* this is an unparsable response */
3154 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3155 ret = 0;
3156 goto end;
3157 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003158 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003159 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003160 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3161 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3162 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3163 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3164 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003165 }
3166 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003167 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003168 goto realign_again;
3169
3170 h2c->flags |= H2_CF_MUX_MFULL;
3171 h2s->flags |= H2_SF_BLK_MROOM;
3172 ret = 0;
3173 goto end;
3174 }
3175
3176 /* encode all headers, stop at empty name */
3177 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003178 /* these ones do not exist in H2 and must be dropped. */
3179 if (isteq(list[hdr].n, ist("connection")) ||
3180 isteq(list[hdr].n, ist("proxy-connection")) ||
3181 isteq(list[hdr].n, ist("keep-alive")) ||
3182 isteq(list[hdr].n, ist("upgrade")) ||
3183 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003184 continue;
3185
3186 if (isteq(list[hdr].n, ist("")))
3187 break; // end
3188
3189 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3190 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003191 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003192 goto realign_again;
3193
3194 h2c->flags |= H2_CF_MUX_MFULL;
3195 h2s->flags |= H2_SF_BLK_MROOM;
3196 ret = 0;
3197 goto end;
3198 }
3199 }
3200
3201 /* we may need to add END_STREAM */
3202 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3203 es_now = 1;
3204
3205 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003206 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003207
3208 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003209 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003210
3211 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003212 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003213
3214 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003215 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003216 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003217
3218 /* for now we don't implemented CONTINUATION, so we wait for a
3219 * body or directly end in TRL2.
3220 */
3221 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003222 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003223 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003224
Willy Tarreau801250e2018-09-11 11:45:04 +02003225 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003226 h2s->flags |= H2_SF_ES_SENT;
3227 if (h2s->st == H2_SS_OPEN)
3228 h2s->st = H2_SS_HLOC;
3229 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003230 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003231 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003232 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003233 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003234 h1m_init_res(h1m);
Willy Tarreau87285592017-11-29 15:41:32 +01003235 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003236 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003237 else
Willy Tarreau801250e2018-09-11 11:45:04 +02003238 h1m->state = (h1m->flags & H1_MF_CHNK) ? H1_MSG_CHUNK_SIZE : H1_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003239
3240 end:
3241 //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, h1_msg_state_str(h1m->err_state));
3242 return ret;
3243}
3244
Willy Tarreau5dd17352018-06-14 13:33:30 +02003245/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3246 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3247 * the number of bytes sent. The caller must check the stream's status to
3248 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003249 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003250static 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 +02003251{
3252 struct h2c *h2c = h2s->h2c;
3253 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003254 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003255 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003256 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003257 int es_now = 0;
3258 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003259 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003260 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003261
3262 if (h2c_mux_busy(h2c, h2s)) {
3263 h2s->flags |= H2_SF_BLK_MBUSY;
3264 goto end;
3265 }
3266
Willy Tarreau44e973f2018-03-01 17:49:30 +01003267 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268 h2c->flags |= H2_CF_MUX_MALLOC;
3269 h2s->flags |= H2_SF_BLK_MROOM;
3270 goto end;
3271 }
3272
3273 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003274 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003275 goto end;
3276
3277 chunk_reset(&outbuf);
3278
3279 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003281 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003282 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003283
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003284 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003285 break;
3286 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288 }
3289
3290 if (outbuf.size < 9) {
3291 h2c->flags |= H2_CF_MUX_MFULL;
3292 h2s->flags |= H2_SF_BLK_MROOM;
3293 goto end;
3294 }
3295
3296 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003297 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3298 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3299 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003300
3301 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3302 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003303 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003304 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003305 break;
3306 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003307 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003308 if ((long long)size > h1m->curr_len)
3309 size = h1m->curr_len;
3310 break;
3311 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003312 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003313 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003314 if (!ret)
3315 goto end;
3316
3317 if (ret < 0) {
3318 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003319 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3321 goto end;
3322 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003323 max -= ret;
3324 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003325 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003326 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003327 }
3328
Willy Tarreau801250e2018-09-11 11:45:04 +02003329 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003330 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003331 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003332 if (!ret)
3333 goto end;
3334
3335 if (ret < 0) {
3336 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003337 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3339 goto end;
3340 }
3341
3342 size = chunk;
3343 h1m->curr_len = chunk;
3344 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003345 max -= ret;
3346 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003347 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003348 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003349 if (!size)
3350 goto send_empty;
3351 }
3352
3353 /* in MSG_DATA state, continue below */
3354 size = h1m->curr_len;
3355 break;
3356 }
3357
3358 /* we have in <size> the exact number of bytes we need to copy from
3359 * the H1 buffer. We need to check this against the connection's and
3360 * the stream's send windows, and to ensure that this fits in the max
3361 * frame size and in the buffer's available space minus 9 bytes (for
3362 * the frame header). The connection's flow control is applied last so
3363 * that we can use a separate list of streams which are immediately
3364 * unblocked on window opening. Note: we don't implement padding.
3365 */
3366
Willy Tarreau5dd17352018-06-14 13:33:30 +02003367 if (size > max)
3368 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003369
3370 if (size > h2s->mws)
3371 size = h2s->mws;
3372
3373 if (size <= 0) {
3374 h2s->flags |= H2_SF_BLK_SFCTL;
3375 goto end;
3376 }
3377
3378 if (h2c->mfs && size > h2c->mfs)
3379 size = h2c->mfs;
3380
3381 if (size + 9 > outbuf.size) {
3382 /* we have an opportunity for enlarging the too small
3383 * available space, let's try.
3384 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003385 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003386 goto realign_again;
3387 size = outbuf.size - 9;
3388 }
3389
3390 if (size <= 0) {
3391 h2c->flags |= H2_CF_MUX_MFULL;
3392 h2s->flags |= H2_SF_BLK_MROOM;
3393 goto end;
3394 }
3395
3396 if (size > h2c->mws)
3397 size = h2c->mws;
3398
3399 if (size <= 0) {
3400 h2s->flags |= H2_SF_BLK_MFCTL;
3401 goto end;
3402 }
3403
3404 /* copy whatever we can */
3405 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003406 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003407 if (ret == 1)
3408 len2 = 0;
3409
3410 if (!ret || len1 + len2 < size) {
3411 /* FIXME: must normally never happen */
3412 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3413 goto end;
3414 }
3415
3416 /* limit len1/len2 to size */
3417 if (len1 + len2 > size) {
3418 int sub = len1 + len2 - size;
3419
3420 if (len2 > sub)
3421 len2 -= sub;
3422 else {
3423 sub -= len2;
3424 len2 = 0;
3425 len1 -= sub;
3426 }
3427 }
3428
3429 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003430 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003431 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003432 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003433
3434 send_empty:
3435 /* we may need to add END_STREAM */
3436 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3437 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003438 *
3439 * FIXME: what we do here is not correct because we send end_stream
3440 * before knowing if we'll have to send a HEADERS frame for the
3441 * trailers. More importantly we're not consuming the trailing CRLF
3442 * after the end of trailers, so it will be left to the caller to
3443 * eat it. The right way to do it would be to measure trailers here
3444 * and to send ES only if there are no trailers.
3445 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003446 */
3447 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003448 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003449 es_now = 1;
3450
3451 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003452 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003453
3454 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003455 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003456
3457 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003458 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003459
3460 /* consume incoming H1 response */
3461 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003462 max -= size;
3463 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003464 total += size;
3465 h1m->curr_len -= size;
3466 h2s->mws -= size;
3467 h2c->mws -= size;
3468
3469 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003470 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003471 goto new_frame;
3472 }
3473 }
3474
3475 if (es_now) {
3476 if (h2s->st == H2_SS_OPEN)
3477 h2s->st = H2_SS_HLOC;
3478 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003479 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003480
Willy Tarreau35a62702018-02-27 15:37:25 +01003481 if (!(h1m->flags & H1_MF_CHNK)) {
3482 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003483 total += max;
3484 ofs += max;
3485 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003486
Willy Tarreau801250e2018-09-11 11:45:04 +02003487 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003488 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003489
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003490 h2s->flags |= H2_SF_ES_SENT;
3491 }
3492
3493 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003494 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, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003495 return total;
3496}
3497
Olivier Houchard6ff20392018-07-17 18:46:31 +02003498/* Called from the upper layer, to subscribe to events, such as being able to send */
3499static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3500{
3501 struct wait_list *sw;
3502 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003503 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003504
3505 switch (event_type) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003506 case SUB_CAN_RECV:
3507 sw = param;
3508 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3509 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003510 sw->handle = h2s;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003511 h2s->recv_wait_list = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003512 }
3513 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003514 case SUB_CAN_SEND:
3515 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003516 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003517 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003518 sw->handle = h2s;
3519 if (h2s->flags & H2_SF_BLK_MFCTL)
3520 LIST_ADDQ(&h2c->fctl_list, &sw->list);
3521 else
3522 LIST_ADDQ(&h2c->send_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003523 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003524 return 0;
3525 default:
3526 break;
3527 }
3528 return -1;
3529
3530
3531}
3532
Olivier Houchard511efea2018-08-16 15:30:32 +02003533/* Called from the upper layer, to receive data */
3534static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3535{
Olivier Houchard638b7992018-08-16 15:41:52 +02003536 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003537 size_t ret = 0;
3538
3539 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003540 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003541
Olivier Houchard638b7992018-08-16 15:41:52 +02003542 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003543 cs->flags |= CS_FL_RCV_MORE;
3544 else {
3545 cs->flags &= ~CS_FL_RCV_MORE;
3546 if (cs->flags & CS_FL_REOS)
3547 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003548 if (b_size(&h2s->rxbuf)) {
3549 b_free(&h2s->rxbuf);
3550 offer_buffers(NULL, tasks_run_queue);
3551 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003552 }
3553
3554 return ret;
3555}
3556
Willy Tarreau62f52692017-10-08 23:01:42 +02003557/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003558static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003559{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003560 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003561 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003562 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003563
Willy Tarreau0bad0432018-06-14 16:54:01 +02003564 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003565 h2s->flags |= H2_SF_OUTGOING_DATA;
3566
Willy Tarreau801250e2018-09-11 11:45:04 +02003567 while (h2s->res.state < H1_MSG_DONE && count) {
3568 if (h2s->res.state < H1_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003569 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003570 }
Willy Tarreau801250e2018-09-11 11:45:04 +02003571 else if (h2s->res.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003572 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003573 }
Willy Tarreau801250e2018-09-11 11:45:04 +02003574 else if (h2s->res.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003575 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003576 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003577
Willy Tarreau5dd17352018-06-14 13:33:30 +02003578 if (unlikely((int)ret <= 0)) {
3579 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003580 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3581 break;
3582 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003583 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003584 total += count;
3585 count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +02003586 h2s->res.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003587 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003588 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003589 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003590 cs->flags |= CS_FL_ERROR;
3591 break;
3592 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003593
3594 total += ret;
3595 count -= ret;
3596
3597 if (h2s->st >= H2_SS_ERROR)
3598 break;
3599
3600 if (h2s->flags & H2_SF_BLK_ANY)
3601 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003602 }
3603
Willy Tarreau00610962018-07-19 10:58:28 +02003604 if (h2s->st >= H2_SS_ERROR) {
3605 /* trim any possibly pending data after we close (extra CR-LF,
3606 * unprocessed trailers, abnormal extra data, ...)
3607 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003608 total += count;
3609 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003610 }
3611
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003612 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003613 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003614 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003615 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003616 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003617 }
3618
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003619 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003620 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003621 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02003622 if (!(h2s->h2c->wait_list.wait_reason & SUB_CAN_SEND))
3623 tasklet_wakeup(h2s->h2c->wait_list.task);
3624 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003625 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003626}
3627
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003628/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003629static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003630{
3631 struct h2c *h2c = conn->mux_ctx;
3632 struct h2s *h2s;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003633 struct wait_list *sw;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003634 struct eb32_node *node;
3635 int fctl_cnt = 0;
3636 int send_cnt = 0;
3637 int tree_cnt = 0;
3638 int orph_cnt = 0;
3639
3640 if (!h2c)
3641 return;
3642
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003643 list_for_each_entry(sw, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003644 fctl_cnt++;
3645
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003646 list_for_each_entry(sw, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003647 send_cnt++;
3648
3649 node = eb32_first(&h2c->streams_by_id);
3650 while (node) {
3651 h2s = container_of(node, struct h2s, by_id);
3652 tree_cnt++;
3653 if (!h2s->cs)
3654 orph_cnt++;
3655 node = eb32_next(node);
3656 }
3657
Willy Tarreau616ac812018-07-24 14:12:42 +02003658 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3659 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3660 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3661 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3662 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3663 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003664}
Willy Tarreau62f52692017-10-08 23:01:42 +02003665
3666/*******************************************************/
3667/* functions below are dedicated to the config parsers */
3668/*******************************************************/
3669
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003670/* config parser for global "tune.h2.header-table-size" */
3671static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3672 struct proxy *defpx, const char *file, int line,
3673 char **err)
3674{
3675 if (too_many_args(1, args, err, NULL))
3676 return -1;
3677
3678 h2_settings_header_table_size = atoi(args[1]);
3679 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3680 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3681 return -1;
3682 }
3683 return 0;
3684}
Willy Tarreau62f52692017-10-08 23:01:42 +02003685
Willy Tarreaue6baec02017-07-27 11:45:11 +02003686/* config parser for global "tune.h2.initial-window-size" */
3687static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3688 struct proxy *defpx, const char *file, int line,
3689 char **err)
3690{
3691 if (too_many_args(1, args, err, NULL))
3692 return -1;
3693
3694 h2_settings_initial_window_size = atoi(args[1]);
3695 if (h2_settings_initial_window_size < 0) {
3696 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3697 return -1;
3698 }
3699 return 0;
3700}
3701
Willy Tarreau5242ef82017-07-27 11:47:28 +02003702/* config parser for global "tune.h2.max-concurrent-streams" */
3703static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3704 struct proxy *defpx, const char *file, int line,
3705 char **err)
3706{
3707 if (too_many_args(1, args, err, NULL))
3708 return -1;
3709
3710 h2_settings_max_concurrent_streams = atoi(args[1]);
3711 if (h2_settings_max_concurrent_streams < 0) {
3712 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3713 return -1;
3714 }
3715 return 0;
3716}
3717
Willy Tarreau62f52692017-10-08 23:01:42 +02003718
3719/****************************************/
3720/* MUX initialization and instanciation */
3721/***************************************/
3722
3723/* The mux operations */
3724const struct mux_ops h2_ops = {
3725 .init = h2_init,
Willy Tarreau62f52692017-10-08 23:01:42 +02003726 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003727 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003728 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003729 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003730 .attach = h2_attach,
3731 .detach = h2_detach,
3732 .shutr = h2_shutr,
3733 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003734 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003735 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003736 .name = "H2",
3737};
3738
Christopher Faulet32f61c02018-04-10 14:33:41 +02003739/* PROTO selection : this mux registers PROTO token "h2" */
3740static struct mux_proto_list mux_proto_h2 =
3741 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003742
3743/* config keyword parsers */
3744static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003745 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003746 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003747 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003748 { 0, NULL, NULL }
3749}};
3750
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003751static void __h2_deinit(void)
3752{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003753 pool_destroy(pool_head_h2s);
3754 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003755}
3756
Willy Tarreau62f52692017-10-08 23:01:42 +02003757__attribute__((constructor))
3758static void __h2_init(void)
3759{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003760 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003761 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003762 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003763 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3764 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003765}