Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 16 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 17 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 18 | #include <common/hpack-tbl.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 19 | #include <common/net_helper.h> |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 20 | #include <proto/applet.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 21 | #include <proto/connection.h> |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 22 | #include <proto/h1.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/stream.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 24 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 25 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | |
| 27 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 28 | /* dummy streams returned for idle and closed states */ |
| 29 | static const struct h2s *h2_closed_stream; |
| 30 | static const struct h2s *h2_idle_stream; |
| 31 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 32 | /* the h2c connection pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 33 | static struct pool_head *pool_head_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 34 | /* the h2s stream pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 35 | static struct pool_head *pool_head_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 36 | |
| 37 | /* Connection flags (32 bit), in h2c->flags */ |
| 38 | #define H2_CF_NONE 0x00000000 |
| 39 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 40 | /* Flags indicating why writing to the mux is blocked. */ |
| 41 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 42 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 43 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 44 | |
| 45 | /* Flags indicating why writing to the demux is blocked. */ |
| 46 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 47 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
| 48 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 49 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 50 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 51 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
| 52 | #define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above |
| 53 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 54 | /* other flags */ |
| 55 | #define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent |
| 56 | #define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent |
| 57 | |
| 58 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 59 | /* H2 connection state, in h2c->st0 */ |
| 60 | enum h2_cs { |
| 61 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 62 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 63 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 64 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
| 65 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame |
| 66 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 67 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 68 | H2_CS_ENTRIES // must be last |
| 69 | } __attribute__((packed)); |
| 70 | |
| 71 | /* H2 connection descriptor */ |
| 72 | struct h2c { |
| 73 | struct connection *conn; |
| 74 | |
| 75 | enum h2_cs st0; /* mux state */ |
| 76 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 77 | |
| 78 | /* 16 bit hole here */ |
| 79 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 80 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 81 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 82 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 83 | |
| 84 | /* states for the demux direction */ |
| 85 | struct hpack_dht *ddht; /* demux dynamic header table */ |
| 86 | struct buffer *dbuf; /* demux buffer */ |
| 87 | |
| 88 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 89 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 90 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 91 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
| 92 | /* 16 bit hole here */ |
| 93 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 94 | |
| 95 | /* states for the mux direction */ |
| 96 | struct buffer *mbuf; /* mux buffer */ |
| 97 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 98 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 99 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 100 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 101 | /* 16 bit hole here */ |
| 102 | int32_t miw; /* mux initial window size for all new streams */ |
| 103 | int32_t mws; /* mux window size. Can be negative. */ |
| 104 | int32_t mfs; /* mux's max frame size */ |
| 105 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 106 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 107 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 108 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 110 | struct list send_list; /* list of blocked streams requesting to send */ |
| 111 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 112 | struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */ |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 113 | struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 116 | /* H2 stream state, in h2s->st */ |
| 117 | enum h2_ss { |
| 118 | H2_SS_IDLE = 0, // idle |
| 119 | H2_SS_RLOC, // reserved(local) |
| 120 | H2_SS_RREM, // reserved(remote) |
| 121 | H2_SS_OPEN, // open |
| 122 | H2_SS_HREM, // half-closed(remote) |
| 123 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 124 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 125 | H2_SS_CLOSED, // closed |
| 126 | H2_SS_ENTRIES // must be last |
| 127 | } __attribute__((packed)); |
| 128 | |
| 129 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 130 | #define H2_SF_NONE 0x00000000 |
| 131 | #define H2_SF_ES_RCVD 0x00000001 |
| 132 | #define H2_SF_ES_SENT 0x00000002 |
| 133 | |
| 134 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 135 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 136 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 137 | /* stream flags indicating the reason the stream is blocked */ |
| 138 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 139 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 140 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 141 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 142 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 143 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 144 | /* stream flags indicating how data is supposed to be sent */ |
| 145 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 146 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 147 | |
| 148 | /* step we're currently in when sending chunks. This is needed because we may |
| 149 | * have to transfer chunks as large as a full buffer so there's no room left |
| 150 | * for size nor crlf around. |
| 151 | */ |
| 152 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 153 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 154 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 155 | |
| 156 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 157 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 158 | #define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 159 | #define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 160 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 161 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 162 | * it is being processed in the internal HTTP representation (H1 for now). |
| 163 | */ |
| 164 | struct h2s { |
| 165 | struct conn_stream *cs; |
| 166 | struct h2c *h2c; |
| 167 | struct h1m req, res; /* request and response parser state for H1 */ |
| 168 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 169 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 170 | int32_t id; /* stream ID */ |
| 171 | uint32_t flags; /* H2_SF_* */ |
| 172 | int mws; /* mux window size for this stream */ |
| 173 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 174 | enum h2_ss st; |
| 175 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 176 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 177 | /* descriptor for an h2 frame header */ |
| 178 | struct h2_fh { |
| 179 | uint32_t len; /* length, host order, 24 bits */ |
| 180 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 181 | uint8_t ft; /* frame type */ |
| 182 | uint8_t ff; /* frame flags */ |
| 183 | }; |
| 184 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 185 | /* a few settings from the global section */ |
| 186 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 187 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 188 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 189 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 190 | /* a dmumy closed stream */ |
| 191 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 192 | .cs = NULL, |
| 193 | .h2c = NULL, |
| 194 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 195 | .errcode = H2_ERR_STREAM_CLOSED, |
| 196 | .flags = H2_SF_RST_SENT, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 197 | .id = 0, |
| 198 | }; |
| 199 | |
| 200 | /* and a dummy idle stream for use with any unannounced stream */ |
| 201 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 202 | .cs = NULL, |
| 203 | .h2c = NULL, |
| 204 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 205 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 206 | .id = 0, |
| 207 | }; |
| 208 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 209 | static struct task *h2_timeout_task(struct task *t); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 211 | /*****************************************************/ |
| 212 | /* functions below are for dynamic buffer management */ |
| 213 | /*****************************************************/ |
| 214 | |
| 215 | /* re-enables receiving on mux <target> after a buffer was allocated. It returns |
| 216 | * 1 if the allocation succeeds, in which case the connection is woken up, or 0 |
| 217 | * if it's impossible to wake up and we prefer to be woken up later. |
| 218 | */ |
| 219 | static int h2_dbuf_available(void *target) |
| 220 | { |
| 221 | struct h2c *h2c = target; |
| 222 | |
| 223 | /* take the buffer now as we'll get scheduled waiting for ->wake() */ |
| 224 | if (b_alloc_margin(&h2c->dbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 225 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
| 226 | if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
| 227 | conn_xprt_want_recv(h2c->conn); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 228 | return 1; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static inline struct buffer *h2_get_dbuf(struct h2c *h2c) |
| 234 | { |
| 235 | struct buffer *buf = NULL; |
| 236 | |
| 237 | if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) && |
| 238 | unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) { |
| 239 | h2c->dbuf_wait.target = h2c->conn; |
| 240 | h2c->dbuf_wait.wakeup_cb = h2_dbuf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 241 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 242 | LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 243 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 244 | __conn_xprt_stop_recv(h2c->conn); |
| 245 | } |
| 246 | return buf; |
| 247 | } |
| 248 | |
| 249 | static inline void h2_release_dbuf(struct h2c *h2c) |
| 250 | { |
| 251 | if (h2c->dbuf->size) { |
| 252 | b_free(&h2c->dbuf); |
| 253 | offer_buffers(h2c->dbuf_wait.target, |
| 254 | tasks_run_queue + applets_active_queue); |
| 255 | } |
| 256 | } |
| 257 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 258 | /* re-enables sending on mux <target> after a buffer was allocated. It returns |
| 259 | * 1 if the allocation succeeds, in which case the connection is woken up, or 0 |
| 260 | * if it's impossible to wake up and we prefer to be woken up later. |
| 261 | */ |
| 262 | static int h2_mbuf_available(void *target) |
| 263 | { |
| 264 | struct h2c *h2c = target; |
| 265 | |
| 266 | /* take the buffer now as we'll get scheduled waiting for ->wake(). */ |
| 267 | if (b_alloc_margin(&h2c->mbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 268 | if (h2c->flags & H2_CF_MUX_MALLOC) { |
| 269 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
| 270 | if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY)) |
| 271 | conn_xprt_want_send(h2c->conn); |
| 272 | } |
| 273 | |
| 274 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 275 | h2c->flags &= ~H2_CF_DEM_MROOM; |
| 276 | if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
| 277 | conn_xprt_want_recv(h2c->conn); |
| 278 | } |
| 279 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 280 | /* FIXME: we should in fact call something like h2_update_poll() |
| 281 | * now to recompte the polling. For now it will be enough like |
| 282 | * this. |
| 283 | */ |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 284 | return 1; |
| 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static inline struct buffer *h2_get_mbuf(struct h2c *h2c) |
| 290 | { |
| 291 | struct buffer *buf = NULL; |
| 292 | |
| 293 | if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) && |
| 294 | unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) { |
| 295 | h2c->mbuf_wait.target = h2c; |
| 296 | h2c->mbuf_wait.wakeup_cb = h2_mbuf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 297 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 298 | LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 299 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 300 | |
| 301 | /* FIXME: we should in fact only block the direction being |
| 302 | * currently used. For now it will be enough like this. |
| 303 | */ |
| 304 | __conn_xprt_stop_send(h2c->conn); |
| 305 | __conn_xprt_stop_recv(h2c->conn); |
| 306 | } |
| 307 | return buf; |
| 308 | } |
| 309 | |
| 310 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 311 | { |
| 312 | if (h2c->mbuf->size) { |
| 313 | b_free(&h2c->mbuf); |
| 314 | offer_buffers(h2c->mbuf_wait.target, |
| 315 | tasks_run_queue + applets_active_queue); |
| 316 | } |
| 317 | } |
| 318 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 319 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 320 | /*****************************************************************/ |
| 321 | /* functions below are dedicated to the mux setup and management */ |
| 322 | /*****************************************************************/ |
| 323 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 324 | /* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */ |
| 325 | static int h2c_frt_init(struct connection *conn) |
| 326 | { |
| 327 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 328 | struct task *t = NULL; |
| 329 | struct session *sess = conn->owner; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 330 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 331 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 332 | if (!h2c) |
| 333 | goto fail; |
| 334 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 335 | |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 336 | h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client; |
| 337 | if (tick_isset(sess->fe->timeout.clientfin)) |
| 338 | h2c->shut_timeout = sess->fe->timeout.clientfin; |
| 339 | |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 340 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 341 | if (tick_isset(h2c->timeout)) { |
| 342 | t = task_new(tid_bit); |
| 343 | if (!t) |
| 344 | goto fail; |
| 345 | |
| 346 | h2c->task = t; |
| 347 | t->process = h2_timeout_task; |
| 348 | t->context = h2c; |
| 349 | t->expire = tick_add(now_ms, h2c->timeout); |
| 350 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 351 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 352 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 353 | if (!h2c->ddht) |
| 354 | goto fail; |
| 355 | |
| 356 | /* Initialise the context. */ |
| 357 | h2c->st0 = H2_CS_PREFACE; |
| 358 | h2c->conn = conn; |
| 359 | h2c->max_id = -1; |
| 360 | h2c->errcode = H2_ERR_NO_ERROR; |
| 361 | h2c->flags = H2_CF_NONE; |
| 362 | h2c->rcvd_c = 0; |
| 363 | h2c->rcvd_s = 0; |
| 364 | |
| 365 | h2c->dbuf = &buf_empty; |
| 366 | h2c->dsi = -1; |
| 367 | h2c->msi = -1; |
| 368 | h2c->last_sid = -1; |
| 369 | |
| 370 | h2c->mbuf = &buf_empty; |
| 371 | h2c->miw = 65535; /* mux initial window size */ |
| 372 | h2c->mws = 65535; /* mux window size */ |
| 373 | h2c->mfs = 16384; /* initial max frame size */ |
| 374 | h2c->streams_by_id = EB_ROOT_UNIQUE; |
| 375 | LIST_INIT(&h2c->send_list); |
| 376 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 377 | LIST_INIT(&h2c->dbuf_wait.list); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 378 | LIST_INIT(&h2c->mbuf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 379 | conn->mux_ctx = h2c; |
| 380 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 381 | if (t) |
| 382 | task_queue(t); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 383 | conn_xprt_want_recv(conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 384 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 385 | /* mux->wake will be called soon to complete the operation */ |
| 386 | return 0; |
| 387 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 388 | if (t) |
| 389 | task_free(t); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 390 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 394 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 395 | * is already initialized before installing the mux, so we detect incoming |
| 396 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 397 | * error. |
| 398 | */ |
| 399 | static int h2_init(struct connection *conn) |
| 400 | { |
| 401 | if (conn->mux_ctx) { |
| 402 | /* we don't support outgoing connections for now */ |
| 403 | return -1; |
| 404 | } |
| 405 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 406 | return h2c_frt_init(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 409 | /* returns the stream associated with id <id> or NULL if not found */ |
| 410 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 411 | { |
| 412 | struct eb32_node *node; |
| 413 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 414 | if (id > h2c->max_id) |
| 415 | return (struct h2s *)h2_idle_stream; |
| 416 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 417 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 418 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 419 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 420 | |
| 421 | return container_of(node, struct h2s, by_id); |
| 422 | } |
| 423 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 424 | /* release function for a connection. This one should be called to free all |
| 425 | * resources allocated to the mux. |
| 426 | */ |
| 427 | static void h2_release(struct connection *conn) |
| 428 | { |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 429 | struct h2c *h2c = conn->mux_ctx; |
| 430 | |
| 431 | LIST_DEL(&conn->list); |
| 432 | |
| 433 | if (h2c) { |
| 434 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 435 | h2_release_dbuf(h2c); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 436 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 437 | LIST_DEL(&h2c->dbuf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 438 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 439 | |
| 440 | h2_release_mbuf(h2c); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 441 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 442 | LIST_DEL(&h2c->mbuf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 443 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 444 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 445 | if (h2c->task) { |
| 446 | task_delete(h2c->task); |
| 447 | task_free(h2c->task); |
| 448 | h2c->task = NULL; |
| 449 | } |
| 450 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 451 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | conn->mux = NULL; |
| 455 | conn->mux_ctx = NULL; |
| 456 | |
| 457 | conn_stop_tracking(conn); |
| 458 | conn_full_close(conn); |
| 459 | if (conn->destroy_cb) |
| 460 | conn->destroy_cb(conn); |
| 461 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 465 | /******************************************************/ |
| 466 | /* functions below are for the H2 protocol processing */ |
| 467 | /******************************************************/ |
| 468 | |
| 469 | /* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 470 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 471 | { |
| 472 | return h2s ? h2s->id : 0; |
| 473 | } |
| 474 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 475 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 476 | static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 477 | { |
| 478 | if (h2c->msi < 0) |
| 479 | return 0; |
| 480 | |
| 481 | if (h2c->msi == h2s_id(h2s)) |
| 482 | return 0; |
| 483 | |
| 484 | return 1; |
| 485 | } |
| 486 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 487 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 488 | static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err) |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 489 | { |
| 490 | h2c->errcode = err; |
| 491 | h2c->st0 = H2_CS_ERROR; |
| 492 | } |
| 493 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 494 | /* marks an error on the stream */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 495 | static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err) |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 496 | { |
| 497 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) { |
| 498 | h2s->errcode = err; |
| 499 | h2s->st = H2_SS_ERROR; |
| 500 | if (h2s->cs) |
| 501 | h2s->cs->flags |= CS_FL_ERROR; |
| 502 | } |
| 503 | } |
| 504 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 505 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 506 | static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len) |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 507 | { |
| 508 | uint8_t *out = frame; |
| 509 | |
| 510 | *out = len >> 16; |
| 511 | write_n16(out + 1, len); |
| 512 | } |
| 513 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 514 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 515 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 516 | * the caller's responsibility to verify that there are at least <bytes> bytes |
| 517 | * available in the buffer's input prior to calling this function. |
| 518 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 519 | static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes, |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 520 | const struct buffer *b, int o) |
| 521 | { |
| 522 | readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 523 | } |
| 524 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 525 | static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 526 | { |
| 527 | return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 528 | } |
| 529 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 530 | static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 531 | { |
| 532 | return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 533 | } |
| 534 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 535 | static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 536 | { |
| 537 | return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 538 | } |
| 539 | |
| 540 | |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 541 | /* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm |
| 542 | * is not obvious. It turns out that H2 headers are neither aligned nor do they |
| 543 | * use regular sizes. And to add to the trouble, the buffer may wrap so each |
| 544 | * byte read must be checked. The header is formed like this : |
| 545 | * |
| 546 | * b0 b1 b2 b3 b4 b5..b8 |
| 547 | * +----------+---------+--------+----+----+----------------------+ |
| 548 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 549 | * +----------+---------+--------+----+----+----------------------+ |
| 550 | * |
| 551 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 552 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 553 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 554 | * the word, and only one extra read is needed to fetch len[16:23]. |
| 555 | * Returns zero if some bytes are missing, otherwise non-zero on success. |
| 556 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 557 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 558 | { |
| 559 | uint64_t w; |
| 560 | |
| 561 | if (b->i < 9) |
| 562 | return 0; |
| 563 | |
| 564 | w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data); |
| 565 | h->len = *b->p << 16; |
| 566 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 567 | h->ff = w >> 32; |
| 568 | h->ft = w >> 40; |
| 569 | h->len += w >> 48; |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 574 | * h2_peek_frame_hdr() above. |
| 575 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 576 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 577 | { |
| 578 | bi_del(b, 9); |
| 579 | } |
| 580 | |
| 581 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 582 | static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 583 | { |
| 584 | int ret; |
| 585 | |
| 586 | ret = h2_peek_frame_hdr(b, h); |
| 587 | if (ret > 0) |
| 588 | h2_skip_frame_hdr(b); |
| 589 | return ret; |
| 590 | } |
| 591 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 592 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 593 | * case of memory allocation error. |
| 594 | */ |
| 595 | static struct h2s *h2c_stream_new(struct h2c *h2c, int id) |
| 596 | { |
| 597 | struct conn_stream *cs; |
| 598 | struct h2s *h2s; |
| 599 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 600 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 601 | if (!h2s) |
| 602 | goto out; |
| 603 | |
| 604 | h2s->h2c = h2c; |
| 605 | h2s->mws = h2c->miw; |
| 606 | h2s->flags = H2_SF_NONE; |
| 607 | h2s->errcode = H2_ERR_NO_ERROR; |
| 608 | h2s->st = H2_SS_IDLE; |
| 609 | h1m_init(&h2s->req); |
| 610 | h1m_init(&h2s->res); |
| 611 | h2s->by_id.key = h2s->id = id; |
| 612 | h2c->max_id = id; |
| 613 | LIST_INIT(&h2s->list); |
| 614 | |
| 615 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
| 616 | |
| 617 | cs = cs_new(h2c->conn); |
| 618 | if (!cs) |
| 619 | goto out_close; |
| 620 | |
| 621 | h2s->cs = cs; |
| 622 | cs->ctx = h2s; |
| 623 | |
| 624 | if (stream_create_from_cs(cs) < 0) |
| 625 | goto out_free_cs; |
| 626 | |
| 627 | /* OK done, the stream lives its own life now */ |
| 628 | return h2s; |
| 629 | |
| 630 | out_free_cs: |
| 631 | cs_free(cs); |
| 632 | out_close: |
| 633 | eb32_delete(&h2s->by_id); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 634 | pool_free(pool_head_h2s, h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 635 | h2s = NULL; |
| 636 | out: |
| 637 | return h2s; |
| 638 | } |
| 639 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 640 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 641 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 642 | * the various settings codes. |
| 643 | */ |
| 644 | static int h2c_snd_settings(struct h2c *h2c) |
| 645 | { |
| 646 | struct buffer *res; |
| 647 | char buf_data[100]; // enough for 15 settings |
| 648 | struct chunk buf; |
| 649 | int ret; |
| 650 | |
| 651 | if (h2c_mux_busy(h2c, NULL)) { |
| 652 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | res = h2_get_mbuf(h2c); |
| 657 | if (!res) { |
| 658 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 659 | h2c->flags |= H2_CF_DEM_MROOM; |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 664 | chunk_memcpy(&buf, |
| 665 | "\x00\x00\x00" /* length : 0 for now */ |
| 666 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 667 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 668 | 9); |
| 669 | |
| 670 | if (h2_settings_header_table_size != 4096) { |
| 671 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 672 | |
| 673 | write_n32(str + 2, h2_settings_header_table_size); |
| 674 | chunk_memcat(&buf, str, 6); |
| 675 | } |
| 676 | |
| 677 | if (h2_settings_initial_window_size != 65535) { |
| 678 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 679 | |
| 680 | write_n32(str + 2, h2_settings_initial_window_size); |
| 681 | chunk_memcat(&buf, str, 6); |
| 682 | } |
| 683 | |
| 684 | if (h2_settings_max_concurrent_streams != 0) { |
| 685 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 686 | |
| 687 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 688 | * the protocol, so never send this value! |
| 689 | */ |
| 690 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 691 | chunk_memcat(&buf, str, 6); |
| 692 | } |
| 693 | |
| 694 | if (global.tune.bufsize != 16384) { |
| 695 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 696 | |
| 697 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 698 | * match bufsize - rewrite size, but at the moment it seems |
| 699 | * that clients don't take care of it. |
| 700 | */ |
| 701 | write_n32(str + 2, global.tune.bufsize); |
| 702 | chunk_memcat(&buf, str, 6); |
| 703 | } |
| 704 | |
| 705 | h2_set_frame_size(buf.str, buf.len - 9); |
| 706 | ret = bo_istput(res, ist2(buf.str, buf.len)); |
| 707 | if (unlikely(ret <= 0)) { |
| 708 | if (!ret) { |
| 709 | h2c->flags |= H2_CF_MUX_MFULL; |
| 710 | h2c->flags |= H2_CF_DEM_MROOM; |
| 711 | return 0; |
| 712 | } |
| 713 | else { |
| 714 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 715 | return 0; |
| 716 | } |
| 717 | } |
| 718 | return ret; |
| 719 | } |
| 720 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 721 | /* Try to receive a connection preface, then upon success try to send our |
| 722 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 723 | * missing data. It may return an error in h2c. |
| 724 | */ |
| 725 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 726 | { |
| 727 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 728 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 729 | |
| 730 | ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE)); |
| 731 | |
| 732 | if (unlikely(ret1 <= 0)) { |
| 733 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 734 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 735 | return 0; |
| 736 | } |
| 737 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 738 | ret2 = h2c_snd_settings(h2c); |
| 739 | if (ret2 > 0) |
| 740 | bi_del(h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 741 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 742 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 745 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 746 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 747 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 748 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 749 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 750 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 751 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 752 | * case so that it is safe to use h2c_error() to report such errors. |
| 753 | */ |
| 754 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 755 | { |
| 756 | struct buffer *res; |
| 757 | char str[17]; |
| 758 | int ret; |
| 759 | |
| 760 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 761 | return 1; // claim that it worked |
| 762 | |
| 763 | if (h2c_mux_busy(h2c, h2s)) { |
| 764 | if (h2s) |
| 765 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 766 | else |
| 767 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | res = h2_get_mbuf(h2c); |
| 772 | if (!res) { |
| 773 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 774 | if (h2s) |
| 775 | h2s->flags |= H2_SF_BLK_MROOM; |
| 776 | else |
| 777 | h2c->flags |= H2_CF_DEM_MROOM; |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 782 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 783 | |
| 784 | if (h2c->last_sid < 0) |
| 785 | h2c->last_sid = h2c->max_id; |
| 786 | |
| 787 | write_n32(str + 9, h2c->last_sid); |
| 788 | write_n32(str + 13, h2c->errcode); |
| 789 | ret = bo_istput(res, ist2(str, 17)); |
| 790 | if (unlikely(ret <= 0)) { |
| 791 | if (!ret) { |
| 792 | h2c->flags |= H2_CF_MUX_MFULL; |
| 793 | if (h2s) |
| 794 | h2s->flags |= H2_SF_BLK_MROOM; |
| 795 | else |
| 796 | h2c->flags |= H2_CF_DEM_MROOM; |
| 797 | return 0; |
| 798 | } |
| 799 | else { |
| 800 | /* we cannot report this error using GOAWAY, so we mark |
| 801 | * it and claim a success. |
| 802 | */ |
| 803 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 804 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 805 | return 1; |
| 806 | } |
| 807 | } |
| 808 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 809 | return ret; |
| 810 | } |
| 811 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 812 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 813 | * during mux operations. This stream must be valid and cannot be closed |
| 814 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 815 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 816 | * not yet. |
| 817 | * |
| 818 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 819 | * to write the message, it subscribes the stream to future notifications. |
| 820 | */ |
| 821 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 822 | { |
| 823 | struct buffer *res; |
| 824 | char str[13]; |
| 825 | int ret; |
| 826 | |
| 827 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 828 | return 1; |
| 829 | |
| 830 | if (h2c_mux_busy(h2c, h2s)) { |
| 831 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | res = h2_get_mbuf(h2c); |
| 836 | if (!res) { |
| 837 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 838 | h2s->flags |= H2_SF_BLK_MROOM; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /* len: 4, type: 3, flags: none */ |
| 843 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 844 | write_n32(str + 5, h2s->id); |
| 845 | write_n32(str + 9, h2s->errcode); |
| 846 | ret = bo_istput(res, ist2(str, 13)); |
| 847 | |
| 848 | if (unlikely(ret <= 0)) { |
| 849 | if (!ret) { |
| 850 | h2c->flags |= H2_CF_MUX_MFULL; |
| 851 | h2s->flags |= H2_SF_BLK_MROOM; |
| 852 | return 0; |
| 853 | } |
| 854 | else { |
| 855 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 856 | return 0; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | h2s->flags |= H2_SF_RST_SENT; |
| 861 | h2s->st = H2_SS_CLOSED; |
| 862 | return ret; |
| 863 | } |
| 864 | |
| 865 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 866 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
| 867 | * error code unless the stream's state already is IDLE or CLOSED in which |
| 868 | * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if |
| 869 | * it was not yet. |
| 870 | * |
| 871 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 872 | * to write the message, it blocks the demuxer and subscribes it to future |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 873 | * notifications. It's worth mentionning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 874 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 875 | */ |
| 876 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 877 | { |
| 878 | struct buffer *res; |
| 879 | char str[13]; |
| 880 | int ret; |
| 881 | |
| 882 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 883 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | res = h2_get_mbuf(h2c); |
| 888 | if (!res) { |
| 889 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 890 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | /* len: 4, type: 3, flags: none */ |
| 895 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 896 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 897 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 898 | write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ? |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 899 | h2s->errcode : H2_ERR_STREAM_CLOSED); |
| 900 | ret = bo_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 901 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 902 | if (unlikely(ret <= 0)) { |
| 903 | if (!ret) { |
| 904 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 905 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 906 | return 0; |
| 907 | } |
| 908 | else { |
| 909 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 910 | return 0; |
| 911 | } |
| 912 | } |
| 913 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 914 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 915 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 916 | h2s->st = H2_SS_CLOSED; |
| 917 | } |
| 918 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 919 | return ret; |
| 920 | } |
| 921 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 922 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 923 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 924 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 925 | * on success or zero if nothing was done. In case of lack of room to write the |
| 926 | * message, it subscribes the requesting stream to future notifications. |
| 927 | */ |
| 928 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 929 | { |
| 930 | struct h2c *h2c = h2s->h2c; |
| 931 | struct buffer *res; |
| 932 | char str[9]; |
| 933 | int ret; |
| 934 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 935 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 936 | return 1; |
| 937 | |
| 938 | if (h2c_mux_busy(h2c, h2s)) { |
| 939 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | res = h2_get_mbuf(h2c); |
| 944 | if (!res) { |
| 945 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 946 | h2s->flags |= H2_SF_BLK_MROOM; |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 951 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 952 | write_n32(str + 5, h2s->id); |
| 953 | ret = bo_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 954 | if (likely(ret > 0)) { |
| 955 | h2s->flags |= H2_SF_ES_SENT; |
| 956 | } |
| 957 | else if (!ret) { |
| 958 | h2c->flags |= H2_CF_MUX_MFULL; |
| 959 | h2s->flags |= H2_SF_BLK_MROOM; |
| 960 | return 0; |
| 961 | } |
| 962 | else { |
| 963 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 964 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 965 | } |
| 966 | return ret; |
| 967 | } |
| 968 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 969 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 970 | * and assign their conn_stream the CS_FL_* flags <flags> in addition to |
| 971 | * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The |
| 972 | * stream's state is automatically updated accordingly. |
| 973 | */ |
| 974 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 975 | { |
| 976 | struct eb32_node *node; |
| 977 | struct h2s *h2s; |
| 978 | |
| 979 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
| 980 | flags |= CS_FL_ERROR; |
| 981 | |
| 982 | if (conn_xprt_read0_pending(h2c->conn)) |
| 983 | flags |= CS_FL_EOS; |
| 984 | |
| 985 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 986 | while (node) { |
| 987 | h2s = container_of(node, struct h2s, by_id); |
| 988 | if (h2s->id <= last) |
| 989 | break; |
| 990 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 991 | |
| 992 | if (!h2s->cs) { |
| 993 | /* this stream was already orphaned */ |
| 994 | eb32_delete(&h2s->by_id); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 995 | pool_free(pool_head_h2s, h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 996 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 997 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 998 | |
| 999 | h2s->cs->flags |= flags; |
| 1000 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1001 | * doesn't handle in the stream int code. |
| 1002 | */ |
| 1003 | h2s->cs->data_cb->recv(h2s->cs); |
| 1004 | h2s->cs->data_cb->wake(h2s->cs); |
| 1005 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1006 | if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
| 1007 | h2s->st = H2_SS_ERROR; |
| 1008 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN) |
| 1009 | h2s->st = H2_SS_HREM; |
| 1010 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC) |
| 1011 | h2s->st = H2_SS_CLOSED; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1015 | /* Increase all streams' outgoing window size by the difference passed in |
| 1016 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1017 | * window size is different. The difference may be negative and the resulting |
| 1018 | * window size as well, for the time it takes to receive some window updates. |
| 1019 | */ |
| 1020 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1021 | { |
| 1022 | struct h2s *h2s; |
| 1023 | struct eb32_node *node; |
| 1024 | |
| 1025 | if (!diff) |
| 1026 | return; |
| 1027 | |
| 1028 | node = eb32_first(&h2c->streams_by_id); |
| 1029 | while (node) { |
| 1030 | h2s = container_of(node, struct h2s, by_id); |
| 1031 | h2s->mws += diff; |
| 1032 | node = eb32_next(node); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1037 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
| 1038 | * return an error in h2c. Described in RFC7540#6.5. |
| 1039 | */ |
| 1040 | static int h2c_handle_settings(struct h2c *h2c) |
| 1041 | { |
| 1042 | unsigned int offset; |
| 1043 | int error; |
| 1044 | |
| 1045 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1046 | if (h2c->dfl) { |
| 1047 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1048 | goto fail; |
| 1049 | } |
| 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | if (h2c->dsi != 0) { |
| 1054 | error = H2_ERR_PROTOCOL_ERROR; |
| 1055 | goto fail; |
| 1056 | } |
| 1057 | |
| 1058 | if (h2c->dfl % 6) { |
| 1059 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1060 | goto fail; |
| 1061 | } |
| 1062 | |
| 1063 | /* that's the limit we can process */ |
| 1064 | if (h2c->dfl > global.tune.bufsize) { |
| 1065 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1066 | goto fail; |
| 1067 | } |
| 1068 | |
| 1069 | /* process full frame only */ |
| 1070 | if (h2c->dbuf->i < h2c->dfl) |
| 1071 | return 0; |
| 1072 | |
| 1073 | /* parse the frame */ |
| 1074 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
| 1075 | uint16_t type = h2_get_n16(h2c->dbuf, offset); |
| 1076 | int32_t arg = h2_get_n32(h2c->dbuf, offset + 2); |
| 1077 | |
| 1078 | switch (type) { |
| 1079 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1080 | /* we need to update all existing streams with the |
| 1081 | * difference from the previous iws. |
| 1082 | */ |
| 1083 | if (arg < 0) { // RFC7540#6.5.2 |
| 1084 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1085 | goto fail; |
| 1086 | } |
| 1087 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1088 | h2c->miw = arg; |
| 1089 | break; |
| 1090 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1091 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1092 | error = H2_ERR_PROTOCOL_ERROR; |
| 1093 | goto fail; |
| 1094 | } |
| 1095 | h2c->mfs = arg; |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | /* need to ACK this frame now */ |
| 1101 | h2c->st0 = H2_CS_FRAME_A; |
| 1102 | return 1; |
| 1103 | fail: |
| 1104 | h2c_error(h2c, error); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1109 | * success or one of the h2_status values. |
| 1110 | */ |
| 1111 | static int h2c_ack_settings(struct h2c *h2c) |
| 1112 | { |
| 1113 | struct buffer *res; |
| 1114 | char str[9]; |
| 1115 | int ret = -1; |
| 1116 | |
| 1117 | if (h2c_mux_busy(h2c, NULL)) { |
| 1118 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | res = h2_get_mbuf(h2c); |
| 1123 | if (!res) { |
| 1124 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1125 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | memcpy(str, |
| 1130 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1131 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1132 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1133 | |
| 1134 | ret = bo_istput(res, ist2(str, 9)); |
| 1135 | if (unlikely(ret <= 0)) { |
| 1136 | if (!ret) { |
| 1137 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1138 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1139 | return 0; |
| 1140 | } |
| 1141 | else { |
| 1142 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1143 | return 0; |
| 1144 | } |
| 1145 | } |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1149 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1150 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
| 1151 | * missing data. It may return an error in h2c. |
| 1152 | */ |
| 1153 | static int h2c_handle_ping(struct h2c *h2c) |
| 1154 | { |
| 1155 | /* frame length must be exactly 8 */ |
| 1156 | if (h2c->dfl != 8) { |
| 1157 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | /* schedule a response */ |
| 1162 | if (!(h2c->dft & H2_F_PING_ACK)) |
| 1163 | h2c->st0 = H2_CS_FRAME_A; |
| 1164 | return 1; |
| 1165 | } |
| 1166 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1167 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1168 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1169 | * error in h2c. |
| 1170 | */ |
| 1171 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1172 | { |
| 1173 | struct buffer *res; |
| 1174 | char str[13]; |
| 1175 | int ret = -1; |
| 1176 | |
| 1177 | if (h2c_mux_busy(h2c, NULL)) { |
| 1178 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | res = h2_get_mbuf(h2c); |
| 1183 | if (!res) { |
| 1184 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1185 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /* length: 4, type: 8, flags: none */ |
| 1190 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1191 | write_n32(str + 5, sid); |
| 1192 | write_n32(str + 9, increment); |
| 1193 | |
| 1194 | ret = bo_istput(res, ist2(str, 13)); |
| 1195 | |
| 1196 | if (unlikely(ret <= 0)) { |
| 1197 | if (!ret) { |
| 1198 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1199 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | else { |
| 1203 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | } |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | /* try to send pending window update for the connection. It's safe to call it |
| 1211 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1212 | * failure. It may return an error in h2c. |
| 1213 | */ |
| 1214 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1215 | { |
| 1216 | int ret = 1; |
| 1217 | |
| 1218 | if (h2c->rcvd_c <= 0) |
| 1219 | return 1; |
| 1220 | |
| 1221 | /* send WU for the connection */ |
| 1222 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1223 | if (ret > 0) |
| 1224 | h2c->rcvd_c = 0; |
| 1225 | |
| 1226 | return ret; |
| 1227 | } |
| 1228 | |
| 1229 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1230 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1231 | * room or failure. It may return an error in h2c. |
| 1232 | */ |
| 1233 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1234 | { |
| 1235 | int ret = 1; |
| 1236 | |
| 1237 | if (h2c->rcvd_s <= 0) |
| 1238 | return 1; |
| 1239 | |
| 1240 | /* send WU for the stream */ |
| 1241 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1242 | if (ret > 0) |
| 1243 | h2c->rcvd_s = 0; |
| 1244 | |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1248 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1249 | * success, 0 on missing data or one of the h2_status values. |
| 1250 | */ |
| 1251 | static int h2c_ack_ping(struct h2c *h2c) |
| 1252 | { |
| 1253 | struct buffer *res; |
| 1254 | char str[17]; |
| 1255 | int ret = -1; |
| 1256 | |
| 1257 | if (h2c->dbuf->i < 8) |
| 1258 | return 0; |
| 1259 | |
| 1260 | if (h2c_mux_busy(h2c, NULL)) { |
| 1261 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | res = h2_get_mbuf(h2c); |
| 1266 | if (!res) { |
| 1267 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1268 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | memcpy(str, |
| 1273 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1274 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1275 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1276 | |
| 1277 | /* copy the original payload */ |
| 1278 | h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0); |
| 1279 | |
| 1280 | ret = bo_istput(res, ist2(str, 17)); |
| 1281 | if (unlikely(ret <= 0)) { |
| 1282 | if (!ret) { |
| 1283 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1284 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1285 | return 0; |
| 1286 | } |
| 1287 | else { |
| 1288 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1289 | return 0; |
| 1290 | } |
| 1291 | } |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1295 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1296 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1297 | * h2c or h2s. Described in RFC7540#6.9. |
| 1298 | */ |
| 1299 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1300 | { |
| 1301 | int32_t inc; |
| 1302 | int error; |
| 1303 | |
| 1304 | if (h2c->dfl != 4) { |
| 1305 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1306 | goto conn_err; |
| 1307 | } |
| 1308 | |
| 1309 | /* process full frame only */ |
| 1310 | if (h2c->dbuf->i < h2c->dfl) |
| 1311 | return 0; |
| 1312 | |
| 1313 | inc = h2_get_n32(h2c->dbuf, 0); |
| 1314 | |
| 1315 | if (h2c->dsi != 0) { |
| 1316 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1317 | |
| 1318 | /* it's not an error to receive WU on a closed stream */ |
| 1319 | if (h2s->st == H2_SS_CLOSED) |
| 1320 | return 1; |
| 1321 | |
| 1322 | if (!inc) { |
| 1323 | error = H2_ERR_PROTOCOL_ERROR; |
| 1324 | goto strm_err; |
| 1325 | } |
| 1326 | |
| 1327 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1328 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1329 | goto strm_err; |
| 1330 | } |
| 1331 | |
| 1332 | h2s->mws += inc; |
| 1333 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1334 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1335 | if (h2s->cs && LIST_ISEMPTY(&h2s->list) && |
| 1336 | (h2s->cs->flags & CS_FL_DATA_WR_ENA)) { |
| 1337 | /* This stream wanted to send but could not due to its |
| 1338 | * own flow control. We can put it back into the send |
| 1339 | * list now, it will be handled upon next send() call. |
| 1340 | */ |
| 1341 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | else { |
| 1346 | /* connection window update */ |
| 1347 | if (!inc) { |
| 1348 | error = H2_ERR_PROTOCOL_ERROR; |
| 1349 | goto conn_err; |
| 1350 | } |
| 1351 | |
| 1352 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1353 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1354 | goto conn_err; |
| 1355 | } |
| 1356 | |
| 1357 | h2c->mws += inc; |
| 1358 | } |
| 1359 | |
| 1360 | return 1; |
| 1361 | |
| 1362 | conn_err: |
| 1363 | h2c_error(h2c, error); |
| 1364 | return 0; |
| 1365 | |
| 1366 | strm_err: |
| 1367 | if (h2s) { |
| 1368 | h2s_error(h2s, error); |
| 1369 | h2c->st0 = H2_CS_FRAME_A; |
| 1370 | } |
| 1371 | else |
| 1372 | h2c_error(h2c, error); |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1376 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
| 1377 | * the last ID. Returns > 0 on success or zero on missing data. It may return |
| 1378 | * an error in h2c. Described in RFC7540#6.8. |
| 1379 | */ |
| 1380 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1381 | { |
| 1382 | int error; |
| 1383 | int last; |
| 1384 | |
| 1385 | if (h2c->dsi != 0) { |
| 1386 | error = H2_ERR_PROTOCOL_ERROR; |
| 1387 | goto conn_err; |
| 1388 | } |
| 1389 | |
| 1390 | if (h2c->dfl < 8) { |
| 1391 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1392 | goto conn_err; |
| 1393 | } |
| 1394 | |
| 1395 | /* process full frame only */ |
| 1396 | if (h2c->dbuf->i < h2c->dfl) |
| 1397 | return 0; |
| 1398 | |
| 1399 | last = h2_get_n32(h2c->dbuf, 0); |
| 1400 | h2c->errcode = h2_get_n32(h2c->dbuf, 4); |
| 1401 | h2_wake_some_streams(h2c, last, CS_FL_ERROR); |
| 1402 | return 1; |
| 1403 | |
| 1404 | conn_err: |
| 1405 | h2c_error(h2c, error); |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1409 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
| 1410 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1411 | * h2c. Described in RFC7540#6.4. |
| 1412 | */ |
| 1413 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1414 | { |
| 1415 | int error; |
| 1416 | |
| 1417 | if (h2c->dsi == 0) { |
| 1418 | error = H2_ERR_PROTOCOL_ERROR; |
| 1419 | goto conn_err; |
| 1420 | } |
| 1421 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1422 | if (h2c->dfl != 4) { |
| 1423 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1424 | goto conn_err; |
| 1425 | } |
| 1426 | |
| 1427 | /* process full frame only */ |
| 1428 | if (h2c->dbuf->i < h2c->dfl) |
| 1429 | return 0; |
| 1430 | |
| 1431 | /* late RST, already handled */ |
| 1432 | if (h2s->st == H2_SS_CLOSED) |
| 1433 | return 1; |
| 1434 | |
| 1435 | h2s->errcode = h2_get_n32(h2c->dbuf, 0); |
| 1436 | h2s->st = H2_SS_CLOSED; |
| 1437 | |
| 1438 | if (h2s->cs) { |
| 1439 | h2s->cs->flags |= CS_FL_EOS; |
| 1440 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1441 | * doesn't handle in the stream-int code. |
| 1442 | */ |
| 1443 | h2s->cs->data_cb->recv(h2s->cs); |
| 1444 | h2s->cs->data_cb->wake(h2s->cs); |
| 1445 | } |
| 1446 | |
| 1447 | h2s->flags |= H2_SF_RST_RCVD; |
| 1448 | return 1; |
| 1449 | |
| 1450 | conn_err: |
| 1451 | h2c_error(h2c, error); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1455 | /* processes a HEADERS frame. Returns > 0 on success or zero on missing data. |
| 1456 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1457 | * errors here are reported as connection errors since it's impossible to |
| 1458 | * recover from such errors after the compression context has been altered. |
| 1459 | */ |
| 1460 | static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1461 | { |
| 1462 | int error; |
| 1463 | |
| 1464 | if (!h2c->dfl) { |
| 1465 | error = H2_ERR_PROTOCOL_ERROR; // empty headers frame! |
| 1466 | goto strm_err; |
| 1467 | } |
| 1468 | |
| 1469 | if (!h2c->dbuf->size) |
| 1470 | return 0; // empty buffer |
| 1471 | |
| 1472 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 1473 | return 0; // incomplete frame |
| 1474 | |
| 1475 | /* now either the frame is complete or the buffer is complete */ |
| 1476 | if (h2s->st != H2_SS_IDLE) { |
| 1477 | /* FIXME: stream already exists, this is only allowed for |
| 1478 | * trailers (not supported for now). |
| 1479 | */ |
| 1480 | error = H2_ERR_PROTOCOL_ERROR; |
| 1481 | goto conn_err; |
| 1482 | } |
| 1483 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1484 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1485 | error = H2_ERR_PROTOCOL_ERROR; |
| 1486 | goto conn_err; |
| 1487 | } |
| 1488 | |
| 1489 | h2s = h2c_stream_new(h2c, h2c->dsi); |
| 1490 | if (!h2s) { |
| 1491 | error = H2_ERR_INTERNAL_ERROR; |
| 1492 | goto conn_err; |
| 1493 | } |
| 1494 | |
| 1495 | h2s->st = H2_SS_OPEN; |
| 1496 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1497 | h2s->st = H2_SS_HREM; |
| 1498 | h2s->flags |= H2_SF_ES_RCVD; |
| 1499 | } |
| 1500 | |
| 1501 | /* call the upper layers to process the frame, then let the upper layer |
| 1502 | * notify the stream about any change. |
| 1503 | */ |
| 1504 | h2s->cs->data_cb->recv(h2s->cs); |
| 1505 | |
| 1506 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1507 | /* FIXME: cs has already been destroyed, but we have to kill h2s. */ |
| 1508 | error = H2_ERR_INTERNAL_ERROR; |
| 1509 | goto conn_err; |
| 1510 | } |
| 1511 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1512 | if (h2c->st0 >= H2_CS_ERROR) |
| 1513 | return 0; |
| 1514 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1515 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1516 | /* stream error : send RST_STREAM */ |
| 1517 | h2c->st0 = H2_CS_FRAME_A; |
| 1518 | } |
| 1519 | else { |
| 1520 | /* update the max stream ID if the request is being processed */ |
| 1521 | if (h2s->id > h2c->max_id) |
| 1522 | h2c->max_id = h2s->id; |
| 1523 | } |
| 1524 | |
| 1525 | return 1; |
| 1526 | |
| 1527 | conn_err: |
| 1528 | h2c_error(h2c, error); |
| 1529 | return 0; |
| 1530 | |
| 1531 | strm_err: |
| 1532 | if (h2s) { |
| 1533 | h2s_error(h2s, error); |
| 1534 | h2c->st0 = H2_CS_FRAME_A; |
| 1535 | } |
| 1536 | else |
| 1537 | h2c_error(h2c, error); |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1541 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 1542 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 1543 | */ |
| 1544 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 1545 | { |
| 1546 | int error; |
| 1547 | |
| 1548 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 1549 | * to signal an end of stream (with the ES flag). |
| 1550 | */ |
| 1551 | |
| 1552 | if (!h2c->dbuf->size && h2c->dfl) |
| 1553 | return 0; // empty buffer |
| 1554 | |
| 1555 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 1556 | return 0; // incomplete frame |
| 1557 | |
| 1558 | /* now either the frame is complete or the buffer is complete */ |
| 1559 | |
| 1560 | if (!h2c->dsi) { |
| 1561 | /* RFC7540#6.1 */ |
| 1562 | error = H2_ERR_PROTOCOL_ERROR; |
| 1563 | goto conn_err; |
| 1564 | } |
| 1565 | |
| 1566 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 1567 | /* RFC7540#6.1 */ |
| 1568 | error = H2_ERR_STREAM_CLOSED; |
| 1569 | goto strm_err; |
| 1570 | } |
| 1571 | |
| 1572 | /* last frame */ |
| 1573 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1574 | h2s->st = H2_SS_HREM; |
| 1575 | h2s->flags |= H2_SF_ES_RCVD; |
| 1576 | } |
| 1577 | |
| 1578 | /* call the upper layers to process the frame, then let the upper layer |
| 1579 | * notify the stream about any change. |
| 1580 | */ |
| 1581 | if (!h2s->cs) { |
| 1582 | error = H2_ERR_STREAM_CLOSED; |
| 1583 | goto strm_err; |
| 1584 | } |
| 1585 | |
| 1586 | h2s->cs->data_cb->recv(h2s->cs); |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1588 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1589 | /* cs has just been destroyed, we have to kill h2s. */ |
| 1590 | error = H2_ERR_STREAM_CLOSED; |
| 1591 | goto strm_err; |
| 1592 | } |
| 1593 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1594 | if (h2c->st0 >= H2_CS_ERROR) |
| 1595 | return 0; |
| 1596 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1597 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1598 | /* stream error : send RST_STREAM */ |
| 1599 | h2c->st0 = H2_CS_FRAME_A; |
| 1600 | } |
| 1601 | |
| 1602 | /* check for completion : the callee will change this to FRAME_A or |
| 1603 | * FRAME_H once done. |
| 1604 | */ |
| 1605 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1606 | return 0; |
| 1607 | |
| 1608 | return 1; |
| 1609 | |
| 1610 | conn_err: |
| 1611 | h2c_error(h2c, error); |
| 1612 | return 0; |
| 1613 | |
| 1614 | strm_err: |
| 1615 | if (h2s) { |
| 1616 | h2s_error(h2s, error); |
| 1617 | h2c->st0 = H2_CS_FRAME_A; |
| 1618 | } |
| 1619 | else |
| 1620 | h2c_error(h2c, error); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1624 | /* process Rx frames to be demultiplexed */ |
| 1625 | static void h2_process_demux(struct h2c *h2c) |
| 1626 | { |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1627 | struct h2s *h2s; |
| 1628 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1629 | if (h2c->st0 >= H2_CS_ERROR) |
| 1630 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1631 | |
| 1632 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 1633 | if (h2c->st0 == H2_CS_PREFACE) { |
| 1634 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 1635 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1636 | if (h2c->st0 == H2_CS_ERROR) |
| 1637 | h2c->st0 = H2_CS_ERROR2; |
| 1638 | goto fail; |
| 1639 | } |
| 1640 | |
| 1641 | h2c->max_id = 0; |
| 1642 | h2c->st0 = H2_CS_SETTINGS1; |
| 1643 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1644 | |
| 1645 | if (h2c->st0 == H2_CS_SETTINGS1) { |
| 1646 | struct h2_fh hdr; |
| 1647 | |
| 1648 | /* ensure that what is pending is a valid SETTINGS frame |
| 1649 | * without an ACK. |
| 1650 | */ |
| 1651 | if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) { |
| 1652 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1653 | if (h2c->st0 == H2_CS_ERROR) |
| 1654 | h2c->st0 = H2_CS_ERROR2; |
| 1655 | goto fail; |
| 1656 | } |
| 1657 | |
| 1658 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 1659 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1660 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1661 | h2c->st0 = H2_CS_ERROR2; |
| 1662 | goto fail; |
| 1663 | } |
| 1664 | |
| 1665 | if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) { |
| 1666 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1667 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1668 | h2c->st0 = H2_CS_ERROR2; |
| 1669 | goto fail; |
| 1670 | } |
| 1671 | |
| 1672 | /* that's OK, switch to FRAME_P to process it */ |
| 1673 | h2c->dfl = hdr.len; |
| 1674 | h2c->dsi = hdr.sid; |
| 1675 | h2c->dft = hdr.ft; |
| 1676 | h2c->dff = hdr.ff; |
| 1677 | h2c->st0 = H2_CS_FRAME_P; |
| 1678 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1679 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1680 | |
| 1681 | /* process as many incoming frames as possible below */ |
| 1682 | while (h2c->dbuf->i) { |
| 1683 | int ret = 0; |
| 1684 | |
| 1685 | if (h2c->st0 >= H2_CS_ERROR) |
| 1686 | break; |
| 1687 | |
| 1688 | if (h2c->st0 == H2_CS_FRAME_H) { |
| 1689 | struct h2_fh hdr; |
| 1690 | |
| 1691 | if (!h2_peek_frame_hdr(h2c->dbuf, &hdr)) |
| 1692 | break; |
| 1693 | |
| 1694 | if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) { |
| 1695 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1696 | h2c->st0 = H2_CS_ERROR; |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | h2c->dfl = hdr.len; |
| 1701 | h2c->dsi = hdr.sid; |
| 1702 | h2c->dft = hdr.ft; |
| 1703 | h2c->dff = hdr.ff; |
| 1704 | h2c->st0 = H2_CS_FRAME_P; |
| 1705 | h2_skip_frame_hdr(h2c->dbuf); |
| 1706 | } |
| 1707 | |
| 1708 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1709 | h2s = h2c_st_by_id(h2c, h2c->dsi); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1710 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 1711 | if (h2s->st == H2_SS_IDLE && |
| 1712 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 1713 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 1714 | * this state MUST be treated as a connection error |
| 1715 | */ |
| 1716 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1717 | h2c->st0 = H2_CS_ERROR; |
| 1718 | break; |
| 1719 | } |
| 1720 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1721 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 1722 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 1723 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 1724 | * this state MUST be treated as a stream error |
| 1725 | */ |
| 1726 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 1727 | goto strm_err; |
| 1728 | } |
| 1729 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 1730 | #if 0 |
| 1731 | // problem below: it is not possible to completely ignore such |
| 1732 | // streams as we need to maintain the compression state as well |
| 1733 | // and for this we need to completely process these frames (eg: |
| 1734 | // HEADERS frames) as well as counting DATA frames to emit |
| 1735 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 1736 | // This is a typical case of layer violation where the |
| 1737 | // transported contents are critical to the connection's |
| 1738 | // validity and must be ignored at the same time :-( |
| 1739 | |
| 1740 | /* graceful shutdown, ignore streams whose ID is higher than |
| 1741 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 1742 | */ |
| 1743 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
| 1744 | ret = MIN(h2c->dbuf->i, h2c->dfl); |
| 1745 | bi_del(h2c->dbuf, ret); |
| 1746 | h2c->dfl -= ret; |
| 1747 | ret = h2c->dfl == 0; |
| 1748 | goto strm_err; |
| 1749 | } |
| 1750 | #endif |
| 1751 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1752 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1753 | case H2_FT_SETTINGS: |
| 1754 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1755 | ret = h2c_handle_settings(h2c); |
| 1756 | |
| 1757 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1758 | ret = h2c_ack_settings(h2c); |
| 1759 | break; |
| 1760 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1761 | case H2_FT_PING: |
| 1762 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1763 | ret = h2c_handle_ping(h2c); |
| 1764 | |
| 1765 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1766 | ret = h2c_ack_ping(h2c); |
| 1767 | break; |
| 1768 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1769 | case H2_FT_WINDOW_UPDATE: |
| 1770 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1771 | ret = h2c_handle_window_update(h2c, h2s); |
| 1772 | break; |
| 1773 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1774 | case H2_FT_CONTINUATION: |
| 1775 | /* we currently don't support CONTINUATION frames since |
| 1776 | * we have nowhere to store the partial HEADERS frame. |
| 1777 | * Let's abort the stream on an INTERNAL_ERROR here. |
| 1778 | */ |
| 1779 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1780 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 1781 | break; |
| 1782 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1783 | case H2_FT_HEADERS: |
| 1784 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1785 | ret = h2c_frt_handle_headers(h2c, h2s); |
| 1786 | break; |
| 1787 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1788 | case H2_FT_DATA: |
| 1789 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1790 | ret = h2c_frt_handle_data(h2c, h2s); |
| 1791 | |
| 1792 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1793 | ret = h2c_send_strm_wu(h2c); |
| 1794 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1795 | |
| 1796 | case H2_FT_RST_STREAM: |
| 1797 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1798 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 1799 | break; |
| 1800 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1801 | case H2_FT_GOAWAY: |
| 1802 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1803 | ret = h2c_handle_goaway(h2c); |
| 1804 | break; |
| 1805 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1806 | case H2_FT_PUSH_PROMISE: |
| 1807 | /* not permitted here, RFC7540#5.1 */ |
| 1808 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1809 | break; |
| 1810 | |
| 1811 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1812 | default: |
| 1813 | /* drop frames that we ignore. They may be larger than |
| 1814 | * the buffer so we drain all of their contents until |
| 1815 | * we reach the end. |
| 1816 | */ |
| 1817 | ret = MIN(h2c->dbuf->i, h2c->dfl); |
| 1818 | bi_del(h2c->dbuf, ret); |
| 1819 | h2c->dfl -= ret; |
| 1820 | ret = h2c->dfl == 0; |
| 1821 | } |
| 1822 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1823 | strm_err: |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1824 | /* RST are sent similarly to frame acks */ |
| 1825 | if (h2s->st == H2_SS_ERROR) { |
| 1826 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1827 | h2c->st0 = H2_CS_FRAME_A; |
| 1828 | |
| 1829 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1830 | ret = h2c_send_rst_stream(h2c, h2s); |
| 1831 | } |
| 1832 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1833 | /* error or missing data condition met above ? */ |
| 1834 | if (ret <= 0) |
| 1835 | break; |
| 1836 | |
| 1837 | if (h2c->st0 != H2_CS_FRAME_H) { |
| 1838 | bi_del(h2c->dbuf, h2c->dfl); |
| 1839 | h2c->st0 = H2_CS_FRAME_H; |
| 1840 | } |
| 1841 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1842 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1843 | if (h2c->rcvd_c > 0 && |
| 1844 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 1845 | h2c_send_conn_wu(h2c); |
| 1846 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1847 | fail: |
| 1848 | /* we can go here on missing data, blocked response or error */ |
| 1849 | return; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 1853 | * the end. |
| 1854 | */ |
| 1855 | static int h2_process_mux(struct h2c *h2c) |
| 1856 | { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1857 | struct h2s *h2s, *h2s_back; |
| 1858 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1859 | /* start by sending possibly pending window updates */ |
| 1860 | if (h2c->rcvd_c > 0 && |
| 1861 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 1862 | h2c_send_conn_wu(h2c) < 0) |
| 1863 | goto fail; |
| 1864 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1865 | /* First we always process the flow control list because the streams |
| 1866 | * waiting there were already elected for immediate emission but were |
| 1867 | * blocked just on this. |
| 1868 | */ |
| 1869 | |
| 1870 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
| 1871 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 1872 | h2c->st0 >= H2_CS_ERROR) |
| 1873 | break; |
| 1874 | |
| 1875 | /* In theory it's possible that h2s->cs == NULL here : |
| 1876 | * - client sends crap that causes a parse error |
| 1877 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 1878 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 1879 | * - stream gets notified, detaches and quits |
| 1880 | * - mux buffer gets ready and wakes pending streams up |
| 1881 | * - bam! |
| 1882 | */ |
| 1883 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 1884 | |
| 1885 | if (h2s->cs) { |
| 1886 | h2s->cs->data_cb->send(h2s->cs); |
| 1887 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1888 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1889 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /* depending on callee's blocking reasons, we may queue in send |
| 1893 | * list or completely dequeue. |
| 1894 | */ |
| 1895 | if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) { |
| 1896 | if (h2s->flags & H2_SF_BLK_ANY) { |
| 1897 | LIST_DEL(&h2s->list); |
| 1898 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1899 | } |
| 1900 | else { |
| 1901 | LIST_DEL(&h2s->list); |
| 1902 | LIST_INIT(&h2s->list); |
| 1903 | if (h2s->cs) |
| 1904 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1905 | else { |
| 1906 | /* just sent the last frame for this orphaned stream */ |
| 1907 | eb32_delete(&h2s->by_id); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1908 | pool_free(pool_head_h2s, h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1909 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
| 1915 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 1916 | break; |
| 1917 | |
| 1918 | /* In theory it's possible that h2s->cs == NULL here : |
| 1919 | * - client sends crap that causes a parse error |
| 1920 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 1921 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 1922 | * - stream gets notified, detaches and quits |
| 1923 | * - mux buffer gets ready and wakes pending streams up |
| 1924 | * - bam! |
| 1925 | */ |
| 1926 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 1927 | |
| 1928 | if (h2s->cs) { |
| 1929 | h2s->cs->data_cb->send(h2s->cs); |
| 1930 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1931 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1932 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1933 | } |
| 1934 | /* depending on callee's blocking reasons, we may queue in fctl |
| 1935 | * list or completely dequeue. |
| 1936 | */ |
| 1937 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 1938 | /* stream hit the connection's flow control */ |
| 1939 | LIST_DEL(&h2s->list); |
| 1940 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 1941 | } |
| 1942 | else if (!(h2s->flags & H2_SF_BLK_ANY)) { |
| 1943 | LIST_DEL(&h2s->list); |
| 1944 | LIST_INIT(&h2s->list); |
| 1945 | if (h2s->cs) |
| 1946 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1947 | else { |
| 1948 | /* just sent the last frame for this orphaned stream */ |
| 1949 | eb32_delete(&h2s->by_id); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1950 | pool_free(pool_head_h2s, h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1951 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1955 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 1956 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1957 | if (h2c->st0 == H2_CS_ERROR) { |
| 1958 | if (h2c->max_id >= 0) { |
| 1959 | h2c_send_goaway_error(h2c, NULL); |
| 1960 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 1965 | } |
| 1966 | return 1; |
| 1967 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1968 | return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 1971 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1972 | /*********************************************************/ |
| 1973 | /* functions below are I/O callbacks from the connection */ |
| 1974 | /*********************************************************/ |
| 1975 | |
| 1976 | /* callback called on recv event by the connection handler */ |
| 1977 | static void h2_recv(struct connection *conn) |
| 1978 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 1979 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 1980 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 1981 | int max; |
| 1982 | |
| 1983 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 1984 | return; |
| 1985 | |
| 1986 | if (h2c->flags & H2_CF_DEM_BLOCK_ANY) |
| 1987 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 1988 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 1989 | buf = h2_get_dbuf(h2c); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 1990 | if (!buf) { |
| 1991 | h2c->flags |= H2_CF_DEM_DALLOC; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 1992 | return; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 1993 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 1995 | /* note: buf->o == 0 */ |
| 1996 | max = buf->size - buf->i; |
| 1997 | if (!max) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 1998 | h2c->flags |= H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | conn->xprt->rcv_buf(conn, buf, max); |
| 2003 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2004 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2005 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2006 | if (!buf->i) { |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2007 | h2_release_dbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2008 | return; |
| 2009 | } |
| 2010 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2011 | if (buf->i == buf->size) |
| 2012 | h2c->flags |= H2_CF_DEM_DFULL; |
| 2013 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2014 | h2_process_demux(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2015 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2016 | /* after streams have been processed, we should have made some room */ |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2017 | if (h2c->st0 >= H2_CS_ERROR) |
| 2018 | buf->i = 0; |
| 2019 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2020 | if (buf->i != buf->size) |
| 2021 | h2c->flags &= ~H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2022 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* callback called on send event by the connection handler */ |
| 2026 | static void h2_send(struct connection *conn) |
| 2027 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2028 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2029 | int done; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2030 | |
| 2031 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2032 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2033 | |
| 2034 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2035 | /* a handshake was requested */ |
| 2036 | return; |
| 2037 | } |
| 2038 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2039 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2040 | * pending streams into the existing buffer until it's reportedly full |
| 2041 | * or the end of send requests is reached. Then it tries to send this |
| 2042 | * buffer's contents out, marks it not full if at least one byte could |
| 2043 | * be sent, and tries again. |
| 2044 | * |
| 2045 | * The snd_buf() function normally takes a "flags" argument which may |
| 2046 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2047 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2048 | * connection is streaming lots of data (used to increase TLS record |
| 2049 | * size at the expense of latency). The former can be sent any time |
| 2050 | * there's a buffer full flag, as it indicates at least one stream |
| 2051 | * attempted to send and failed so there are pending data. An |
| 2052 | * alternative would be to set it as long as there's an active stream |
| 2053 | * but that would be problematic for ACKs until we have an absolute |
| 2054 | * guarantee that all waiters have at least one byte to send. The |
| 2055 | * latter should possibly not be set for now. |
| 2056 | */ |
| 2057 | |
| 2058 | done = 0; |
| 2059 | while (!done) { |
| 2060 | unsigned int flags = 0; |
| 2061 | |
| 2062 | /* fill as much as we can into the current buffer */ |
| 2063 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2064 | done = h2_process_mux(h2c); |
| 2065 | |
| 2066 | if (conn->flags & CO_FL_ERROR) |
| 2067 | break; |
| 2068 | |
| 2069 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2070 | flags |= CO_SFL_MSG_MORE; |
| 2071 | |
Willy Tarreau | 319994a | 2017-11-07 11:03:56 +0100 | [diff] [blame] | 2072 | if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0) |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2073 | break; |
| 2074 | |
| 2075 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2076 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2077 | } |
| 2078 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2079 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2080 | /* output closed, nothing to send, clear the buffer to release it */ |
| 2081 | h2c->mbuf->o = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2082 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2083 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2084 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2085 | /* callback called on any event by the connection handler. |
| 2086 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2087 | * destruction of the connection (which normally doesn not happen in h2). |
| 2088 | */ |
| 2089 | static int h2_wake(struct connection *conn) |
| 2090 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2091 | struct h2c *h2c = conn->mux_ctx; |
| 2092 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2093 | /* |
| 2094 | * If we received early data, try to wake any stream, just in case |
| 2095 | * at least one of them was waiting for the handshake |
| 2096 | */ |
| 2097 | if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) == |
| 2098 | CO_FL_EARLY_DATA) { |
| 2099 | h2_wake_some_streams(h2c, 0, 0); |
| 2100 | conn->flags &= ~CO_FL_EARLY_DATA; |
| 2101 | } |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2102 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2103 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2104 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2105 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2106 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2107 | |
| 2108 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2109 | /* no more stream, kill the connection now */ |
| 2110 | h2_release(conn); |
| 2111 | return -1; |
| 2112 | } |
| 2113 | else { |
| 2114 | /* some streams still there, we need to signal them all and |
| 2115 | * wait for their departure. |
| 2116 | */ |
| 2117 | __conn_xprt_stop_recv(conn); |
| 2118 | __conn_xprt_stop_send(conn); |
| 2119 | return 0; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | if (!h2c->dbuf->i) |
| 2124 | h2_release_dbuf(h2c); |
| 2125 | |
| 2126 | /* stop being notified of incoming data if we can't process them */ |
| 2127 | if (h2c->st0 >= H2_CS_ERROR || |
| 2128 | (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2129 | __conn_xprt_stop_recv(conn); |
| 2130 | } |
| 2131 | else { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2132 | __conn_xprt_want_recv(conn); |
| 2133 | } |
| 2134 | |
| 2135 | /* adjust output polling */ |
Willy Tarreau | 5160683 | 2017-10-17 15:30:07 +0200 | [diff] [blame] | 2136 | if (!(conn->flags & CO_FL_SOCK_WR_SH) && |
| 2137 | (h2c->st0 == H2_CS_ERROR || |
| 2138 | h2c->mbuf->o || |
| 2139 | (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) || |
| 2140 | (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2141 | __conn_xprt_want_send(conn); |
| 2142 | } |
| 2143 | else { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2144 | h2_release_mbuf(h2c); |
| 2145 | __conn_xprt_stop_send(conn); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2146 | } |
| 2147 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2148 | if (h2c->task) { |
| 2149 | if (eb_is_empty(&h2c->streams_by_id)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2150 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2151 | task_queue(h2c->task); |
| 2152 | } |
| 2153 | else |
| 2154 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2155 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2156 | return 0; |
| 2157 | } |
| 2158 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2159 | /* Connection timeout management. The principle is that if there's no receipt |
| 2160 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2161 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2162 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2163 | * GOAWAY frame. |
| 2164 | */ |
| 2165 | static struct task *h2_timeout_task(struct task *t) |
| 2166 | { |
| 2167 | struct h2c *h2c = t->context; |
| 2168 | int expired = tick_is_expired(t->expire, now_ms); |
| 2169 | |
| 2170 | if (!expired) |
| 2171 | return t; |
| 2172 | |
| 2173 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2174 | h2_wake_some_streams(h2c, 0, 0); |
| 2175 | |
| 2176 | if (h2c->mbuf->o) { |
| 2177 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2178 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2179 | } |
| 2180 | |
| 2181 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2182 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2183 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2184 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2185 | |
| 2186 | if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) |
| 2187 | h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0); |
| 2188 | |
| 2189 | if (!eb_is_empty(&h2c->streams_by_id)) |
| 2190 | goto wait; |
| 2191 | |
| 2192 | h2_release(h2c->conn); |
| 2193 | return NULL; |
| 2194 | |
| 2195 | wait: |
| 2196 | /* the streams have been notified, we must let them finish and close */ |
| 2197 | h2c->task = NULL; |
| 2198 | task_delete(t); |
| 2199 | task_free(t); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | |
| 2203 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2204 | /*******************************************/ |
| 2205 | /* functions below are used by the streams */ |
| 2206 | /*******************************************/ |
| 2207 | |
| 2208 | /* |
| 2209 | * Attach a new stream to a connection |
| 2210 | * (Used for outgoing connections) |
| 2211 | */ |
| 2212 | static struct conn_stream *h2_attach(struct connection *conn) |
| 2213 | { |
| 2214 | return NULL; |
| 2215 | } |
| 2216 | |
| 2217 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 2218 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 2219 | * to the transport layer. |
| 2220 | */ |
| 2221 | static void h2_update_poll(struct conn_stream *cs) |
| 2222 | { |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2223 | struct h2s *h2s = cs->ctx; |
| 2224 | |
| 2225 | if (!h2s) |
| 2226 | return; |
| 2227 | |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2228 | /* we may unblock a blocked read */ |
| 2229 | |
| 2230 | if (cs->flags & CS_FL_DATA_RD_ENA && |
| 2231 | h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) { |
| 2232 | h2s->h2c->flags &= ~H2_CF_DEM_SFULL; |
| 2233 | conn_xprt_want_recv(cs->conn); |
| 2234 | } |
| 2235 | |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2236 | /* Note: the stream and stream-int code doesn't allow us to perform a |
| 2237 | * synchronous send() here unfortunately, because this code is called |
| 2238 | * as si_update() from the process_stream() context. This means that |
| 2239 | * we have to queue the current cs and defer its processing after the |
| 2240 | * connection's cs list is processed anyway. |
| 2241 | */ |
| 2242 | |
| 2243 | if (cs->flags & CS_FL_DATA_WR_ENA) { |
| 2244 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2245 | if (LIST_ISEMPTY(&h2s->h2c->send_list) && |
| 2246 | !h2s->h2c->mbuf->o && // not yet subscribed |
| 2247 | !(cs->conn->flags & CO_FL_SOCK_WR_SH)) |
| 2248 | conn_xprt_want_send(cs->conn); |
| 2249 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2250 | } |
| 2251 | } |
| 2252 | else if (!LIST_ISEMPTY(&h2s->list)) { |
| 2253 | LIST_DEL(&h2s->list); |
| 2254 | LIST_INIT(&h2s->list); |
| 2255 | h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL); |
| 2256 | } |
| 2257 | |
| 2258 | /* this can happen from within si_chk_snd() */ |
| 2259 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2260 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * Detach the stream from the connection and possibly release the connection. |
| 2265 | */ |
| 2266 | static void h2_detach(struct conn_stream *cs) |
| 2267 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2268 | struct h2s *h2s = cs->ctx; |
| 2269 | struct h2c *h2c; |
| 2270 | |
| 2271 | cs->ctx = NULL; |
| 2272 | if (!h2s) |
| 2273 | return; |
| 2274 | |
| 2275 | h2c = h2s->h2c; |
| 2276 | h2s->cs = NULL; |
| 2277 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2278 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2279 | * an ES or RST frame), so orphan it in this case. |
| 2280 | */ |
| 2281 | if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) |
| 2282 | return; |
| 2283 | |
Willy Tarreau | 541dd82 | 2017-11-23 18:12:50 +0100 | [diff] [blame] | 2284 | /* the stream could be in the send list */ |
| 2285 | LIST_DEL(&h2s->list); |
| 2286 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2287 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2288 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2289 | /* unblock the connection if it was blocked on this |
| 2290 | * stream. |
| 2291 | */ |
| 2292 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2293 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
| 2294 | conn_xprt_want_recv(cs->conn); |
| 2295 | conn_xprt_want_send(cs->conn); |
| 2296 | } |
| 2297 | |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2298 | if (h2s->by_id.node.leaf_p) { |
| 2299 | /* h2s still attached to the h2c */ |
| 2300 | eb32_delete(&h2s->by_id); |
| 2301 | |
| 2302 | /* We don't want to close right now unless we're removing the |
| 2303 | * last stream, and either the connection is in error, or it |
| 2304 | * reached the ID already specified in a GOAWAY frame received |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2305 | * or sent (as seen by last_sid >= 0). |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2306 | */ |
Willy Tarreau | 83906c2 | 2017-11-07 11:48:46 +0100 | [diff] [blame] | 2307 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 2308 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2309 | (h2c->flags & H2_CF_GOAWAY_FAILED) || |
Willy Tarreau | 83906c2 | 2017-11-07 11:48:46 +0100 | [diff] [blame] | 2310 | (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */ |
| 2311 | (conn_xprt_read0_pending(h2c->conn) || |
| 2312 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2313 | /* no more stream will come, kill it now */ |
| 2314 | h2_release(h2c->conn); |
| 2315 | } |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2316 | else if (h2c->task) { |
| 2317 | if (eb_is_empty(&h2c->streams_by_id)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2318 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2319 | task_queue(h2c->task); |
| 2320 | } |
| 2321 | else |
| 2322 | h2c->task->expire = TICK_ETERNITY; |
| 2323 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2324 | } |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2325 | pool_free(pool_head_h2s, h2s); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2329 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2330 | struct h2s *h2s = cs->ctx; |
| 2331 | |
| 2332 | if (!mode) |
| 2333 | return; |
| 2334 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2335 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2336 | return; |
| 2337 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2338 | /* if no outgoing data was seen on this stream, it means it was |
| 2339 | * closed with a "tcp-request content" rule that is normally |
| 2340 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2341 | * case we send a goaway to close the connection. |
| 2342 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2343 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2344 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
| 2345 | return; |
| 2346 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2347 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2348 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
| 2349 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
| 2350 | return; |
| 2351 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2352 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2353 | conn_xprt_want_send(cs->conn); |
| 2354 | |
| 2355 | h2s->st = H2_SS_CLOSED; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2359 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2360 | struct h2s *h2s = cs->ctx; |
| 2361 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2362 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2363 | return; |
| 2364 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2365 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2366 | /* we can cleanly close using an empty data frame only after headers */ |
| 2367 | |
| 2368 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 2369 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2370 | return; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2371 | |
| 2372 | if (h2s->st == H2_SS_HREM) |
| 2373 | h2s->st = H2_SS_CLOSED; |
| 2374 | else |
| 2375 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2376 | } else { |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2377 | /* if no outgoing data was seen on this stream, it means it was |
| 2378 | * closed with a "tcp-request content" rule that is normally |
| 2379 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2380 | * case we send a goaway to close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2381 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2382 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2383 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
| 2384 | return; |
| 2385 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2386 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2387 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2388 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
| 2389 | return; |
| 2390 | |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2391 | h2s->st = H2_SS_CLOSED; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2395 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2396 | } |
| 2397 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2398 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 |
| 2399 | * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't |
| 2400 | * proceed. Stream errors are reported in h2s->errcode and connection errors |
| 2401 | * in h2c->errcode. The caller must already have checked the frame header and |
| 2402 | * ensured that the frame was complete or the buffer full. |
| 2403 | */ |
| 2404 | static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count) |
| 2405 | { |
| 2406 | struct h2c *h2c = h2s->h2c; |
| 2407 | const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2408 | struct chunk *tmp = get_trash_chunk(); |
| 2409 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2410 | struct chunk *copy = NULL; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2411 | int flen = h2c->dfl; |
| 2412 | int outlen = 0; |
| 2413 | int wrap; |
| 2414 | int try; |
| 2415 | |
| 2416 | if (!h2c->dfl) { |
| 2417 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame! |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
| 2422 | wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p; |
| 2423 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2424 | copy = alloc_trash_chunk(); |
| 2425 | if (!copy) { |
| 2426 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 2427 | goto fail; |
| 2428 | } |
| 2429 | memcpy(copy->str, h2c->dbuf->p, wrap); |
| 2430 | memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap); |
| 2431 | hdrs = (uint8_t *)copy->str; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /* The padlen is the first byte before data, and the padding appears |
| 2435 | * after data. padlen+data+padding are included in flen. |
| 2436 | */ |
| 2437 | if (h2c->dff & H2_F_HEADERS_PADDED) { |
| 2438 | if (*hdrs >= flen) { |
| 2439 | /* RFC7540#6.2 : pad length = length of frame payload or greater */ |
| 2440 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2441 | return 0; |
| 2442 | } |
| 2443 | flen -= *hdrs + 1; |
| 2444 | hdrs += 1; // skip Pad Length |
| 2445 | } |
| 2446 | |
| 2447 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 2448 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
| 2449 | hdrs += 5; // stream dep = 4, weight = 1 |
| 2450 | flen -= 5; |
| 2451 | } |
| 2452 | |
| 2453 | /* FIXME: lack of END_HEADERS means there's a continuation frame, we |
| 2454 | * don't support this for now and can't even decompress so we have to |
| 2455 | * break the connection. |
| 2456 | */ |
| 2457 | if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) { |
| 2458 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2459 | goto fail; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2460 | } |
| 2461 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2462 | /* we can't retry a failed decompression operation so we must be very |
| 2463 | * careful not to take any risks. In practice the output buffer is |
| 2464 | * always empty except maybe for trailers, so these operations almost |
| 2465 | * never happen. |
| 2466 | */ |
| 2467 | if (unlikely(buf->o)) { |
| 2468 | /* need to let the output buffer flush and |
| 2469 | * mark the buffer for later wake up. |
| 2470 | */ |
| 2471 | goto fail; |
| 2472 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2474 | if (unlikely(buffer_space_wraps(buf))) { |
| 2475 | /* it doesn't fit and the buffer is fragmented, |
| 2476 | * so let's defragment it and try again. |
| 2477 | */ |
| 2478 | buffer_slow_realign(buf); |
| 2479 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2480 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2481 | /* first check if we have some room after p+i */ |
| 2482 | try = buf->data + buf->size - (buf->p + buf->i); |
| 2483 | |
| 2484 | /* otherwise continue between data and p-o */ |
| 2485 | if (try <= 0) { |
| 2486 | try = buf->p - (buf->data + buf->o); |
| 2487 | if (try <= 0) |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2488 | goto fail; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2489 | } |
| 2490 | if (try > count) |
| 2491 | try = count; |
| 2492 | |
| 2493 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 2494 | sizeof(list)/sizeof(list[0]), tmp); |
| 2495 | if (outlen < 0) { |
| 2496 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2497 | goto fail; |
| 2498 | } |
| 2499 | |
| 2500 | /* OK now we have our header list in <list> */ |
| 2501 | outlen = h2_make_h1_request(list, bi_end(buf), try); |
| 2502 | |
| 2503 | if (outlen < 0) { |
| 2504 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2505 | goto fail; |
| 2506 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2507 | |
| 2508 | /* now consume the input data */ |
| 2509 | bi_del(h2c->dbuf, h2c->dfl); |
| 2510 | h2c->st0 = H2_CS_FRAME_H; |
| 2511 | buf->i += outlen; |
| 2512 | |
| 2513 | /* don't send it before returning data! |
| 2514 | * FIXME: should we instead try to send it much later, after the |
| 2515 | * response ? This would require that we keep a copy of it in h2s. |
| 2516 | */ |
| 2517 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2518 | h2s->cs->flags |= CS_FL_EOS; |
| 2519 | h2s->flags |= H2_SF_ES_RCVD; |
| 2520 | } |
| 2521 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2522 | leave: |
| 2523 | free_trash_chunk(copy); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2524 | return outlen; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2525 | fail: |
| 2526 | outlen = 0; |
| 2527 | goto leave; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2528 | } |
| 2529 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2530 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 2531 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 2532 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 2533 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 2534 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 2535 | * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame |
| 2536 | * parser state is automatically updated. Returns the number of bytes emitted |
| 2537 | * if > 0, or 0 if it couldn't proceed. Stream errors are reported in |
| 2538 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 2539 | * have checked the frame header and ensured that the frame was complete or the |
| 2540 | * buffer full. It changes the frame state to FRAME_A once done. |
| 2541 | */ |
| 2542 | static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count) |
| 2543 | { |
| 2544 | struct h2c *h2c = h2s->h2c; |
| 2545 | int block1, block2; |
| 2546 | unsigned int flen = h2c->dfl; |
| 2547 | unsigned int padlen = 0; |
| 2548 | int offset = 0; |
| 2549 | |
| 2550 | if (h2c->dbuf->i < flen) |
| 2551 | return 0; |
| 2552 | |
| 2553 | /* The padlen is the first byte before data, and the padding appears |
| 2554 | * after data. padlen+data+padding are included in flen. |
| 2555 | */ |
| 2556 | if (h2c->dff & H2_F_HEADERS_PADDED) { |
| 2557 | padlen = *(uint8_t *)bi_ptr(h2c->dbuf); |
| 2558 | if (padlen >= flen) { |
| 2559 | /* RFC7540#6.1 : pad length = length of frame payload or greater */ |
| 2560 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2561 | return 0; |
| 2562 | } |
| 2563 | flen -= padlen + 1; |
| 2564 | offset = 1; // skip Pad Length |
| 2565 | } |
| 2566 | |
| 2567 | /* does it fit in output buffer or should we wait ? */ |
| 2568 | if (buf->i + buf->o + flen > buf->size) { |
| 2569 | h2c->flags |= H2_CF_DEM_SFULL; |
| 2570 | return 0; |
| 2571 | } |
| 2572 | |
| 2573 | /* Block1 is the length of the first block before the buffer wraps, |
| 2574 | * block2 is the optional second block to reach the end of the frame. |
| 2575 | */ |
| 2576 | block1 = bi_contig_data(h2c->dbuf); |
| 2577 | if (block1 > offset + flen) |
| 2578 | block1 = offset + flen; |
| 2579 | block1 -= offset; // skip Pad Length |
| 2580 | block2 = flen - block1; |
| 2581 | |
| 2582 | if (block1) |
| 2583 | bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1); |
| 2584 | |
| 2585 | if (block2) |
| 2586 | bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2); |
| 2587 | |
| 2588 | /* now mark the input data as consumed (will be deleted from the buffer |
| 2589 | * by the caller when seeing FRAME_A after sending the window update). |
| 2590 | */ |
| 2591 | h2c->rcvd_c += h2c->dfl; |
| 2592 | h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams! |
| 2593 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 2594 | |
| 2595 | /* don't send it before returning data! |
| 2596 | * FIXME: should we instead try to send it much later, after the |
| 2597 | * response ? This would require that we keep a copy of it in h2s. |
| 2598 | */ |
| 2599 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2600 | h2s->cs->flags |= CS_FL_EOS; |
| 2601 | h2s->flags |= H2_SF_ES_RCVD; |
| 2602 | } |
| 2603 | |
| 2604 | return flen; |
| 2605 | } |
| 2606 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2607 | /* |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2608 | * Called from the upper layer to get more data, up to <count> bytes. The |
| 2609 | * caller is responsible for never asking for more data than what is available |
| 2610 | * in the buffer. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2611 | */ |
| 2612 | static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count) |
| 2613 | { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2614 | struct h2s *h2s = cs->ctx; |
| 2615 | struct h2c *h2c = h2s->h2c; |
| 2616 | int ret = 0; |
| 2617 | |
| 2618 | if (h2c->st0 != H2_CS_FRAME_P) |
| 2619 | return 0; // no pre-parsed frame yet |
| 2620 | |
| 2621 | if (h2c->dsi != h2s->id) |
| 2622 | return 0; // not for us |
| 2623 | |
| 2624 | if (!h2c->dbuf->size) |
| 2625 | return 0; // empty buffer |
| 2626 | |
| 2627 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 2628 | return 0; // incomplete input frame |
| 2629 | |
| 2630 | switch (h2c->dft) { |
| 2631 | case H2_FT_HEADERS: |
| 2632 | ret = h2_frt_decode_headers(h2s, buf, count); |
| 2633 | break; |
| 2634 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2635 | case H2_FT_DATA: |
| 2636 | ret = h2_frt_transfer_data(h2s, buf, count); |
| 2637 | break; |
| 2638 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2639 | default: |
| 2640 | ret = 0; |
| 2641 | } |
| 2642 | return ret; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2643 | } |
| 2644 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2645 | /* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf> |
| 2646 | * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of |
| 2647 | * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds |
| 2648 | * to the number of buffer bytes consumed. |
| 2649 | */ |
| 2650 | static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf) |
| 2651 | { |
| 2652 | struct http_hdr list[MAX_HTTP_HDR]; |
| 2653 | struct h2c *h2c = h2s->h2c; |
| 2654 | struct h1m *h1m = &h2s->res; |
| 2655 | struct chunk outbuf; |
| 2656 | int es_now = 0; |
| 2657 | int ret = 0; |
| 2658 | int hdr; |
| 2659 | |
| 2660 | if (h2c_mux_busy(h2c, h2s)) { |
| 2661 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | if (!h2_get_mbuf(h2c)) { |
| 2666 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 2667 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
| 2671 | /* First, try to parse the H1 response and index it into <list>. |
| 2672 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 2673 | * block does not wrap and we can safely read it this way without |
| 2674 | * having to realign the buffer. |
| 2675 | */ |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 2676 | next_header_block: |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2677 | ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o, |
| 2678 | list, sizeof(list)/sizeof(list[0]), h1m); |
| 2679 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 2680 | /* incomplete or invalid response, this is abnormal coming from |
| 2681 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 2682 | * so that won't be fixed, raise an error now. |
| 2683 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2684 | * FIXME: we should instead add the ability to only return a |
| 2685 | * 502 bad gateway. But in theory this is not supposed to |
| 2686 | * happen. |
| 2687 | */ |
| 2688 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2689 | ret = 0; |
| 2690 | goto end; |
| 2691 | } |
| 2692 | |
| 2693 | chunk_reset(&outbuf); |
| 2694 | |
Willy Tarreau | af1e4f5 | 2017-10-30 21:54:49 +0100 | [diff] [blame] | 2695 | try_again: |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2696 | while (1) { |
| 2697 | outbuf.str = bo_end(h2c->mbuf); |
| 2698 | outbuf.size = bo_contig_space(h2c->mbuf); |
| 2699 | outbuf.len = 0; |
| 2700 | |
| 2701 | if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf)) |
| 2702 | break; |
| 2703 | realign_again: |
| 2704 | buffer_slow_realign(h2c->mbuf); |
| 2705 | } |
| 2706 | |
| 2707 | if (outbuf.size < 9) { |
| 2708 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2709 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2710 | ret = 0; |
| 2711 | goto end; |
| 2712 | } |
| 2713 | |
| 2714 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 2715 | memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5); |
| 2716 | write_n32(outbuf.str + 5, h2s->id); // 4 bytes |
| 2717 | outbuf.len = 9; |
| 2718 | |
| 2719 | /* encode status, which necessarily is the first one */ |
| 2720 | if (outbuf.len < outbuf.size && h1m->status == 200) |
| 2721 | outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200") |
| 2722 | else if (outbuf.len < outbuf.size && h1m->status == 304) |
| 2723 | outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304") |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 2724 | else if (unlikely(list[0].v.len != 3)) { |
| 2725 | /* this is an unparsable response */ |
| 2726 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2727 | ret = 0; |
| 2728 | goto end; |
| 2729 | } |
| 2730 | else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2731 | /* basic encoding of the status code */ |
| 2732 | outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8) |
| 2733 | outbuf.str[outbuf.len++] = 0x03; // 3 bytes status |
| 2734 | outbuf.str[outbuf.len++] = list[0].v.ptr[0]; |
| 2735 | outbuf.str[outbuf.len++] = list[0].v.ptr[1]; |
| 2736 | outbuf.str[outbuf.len++] = list[0].v.ptr[2]; |
| 2737 | } |
| 2738 | else { |
| 2739 | if (buffer_space_wraps(h2c->mbuf)) |
| 2740 | goto realign_again; |
| 2741 | |
| 2742 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2743 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2744 | ret = 0; |
| 2745 | goto end; |
| 2746 | } |
| 2747 | |
| 2748 | /* encode all headers, stop at empty name */ |
| 2749 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 2750 | /* these ones do not exist in H2 and must be dropped. */ |
| 2751 | if (isteq(list[hdr].n, ist("connection")) || |
| 2752 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 2753 | isteq(list[hdr].n, ist("keep-alive")) || |
| 2754 | isteq(list[hdr].n, ist("upgrade")) || |
| 2755 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2756 | continue; |
| 2757 | |
| 2758 | if (isteq(list[hdr].n, ist(""))) |
| 2759 | break; // end |
| 2760 | |
| 2761 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 2762 | /* output full */ |
| 2763 | if (buffer_space_wraps(h2c->mbuf)) |
| 2764 | goto realign_again; |
| 2765 | |
| 2766 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2767 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2768 | ret = 0; |
| 2769 | goto end; |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | /* we may need to add END_STREAM */ |
| 2774 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 2775 | es_now = 1; |
| 2776 | |
| 2777 | /* update the frame's size */ |
| 2778 | h2_set_frame_size(outbuf.str, outbuf.len - 9); |
| 2779 | |
| 2780 | if (es_now) |
| 2781 | outbuf.str[4] |= H2_F_HEADERS_END_STREAM; |
| 2782 | |
| 2783 | /* consume incoming H1 response */ |
| 2784 | bo_del(buf, ret); |
| 2785 | |
| 2786 | /* commit the H2 response */ |
| 2787 | h2c->mbuf->o += outbuf.len; |
| 2788 | h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2789 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2790 | |
| 2791 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 2792 | * body or directly end in TRL2. |
| 2793 | */ |
| 2794 | if (es_now) { |
| 2795 | h1m->state = HTTP_MSG_DONE; |
| 2796 | h2s->flags |= H2_SF_ES_SENT; |
| 2797 | if (h2s->st == H2_SS_OPEN) |
| 2798 | h2s->st = H2_SS_HLOC; |
| 2799 | else |
| 2800 | h2s->st = H2_SS_CLOSED; |
| 2801 | } |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 2802 | else if (h1m->status >= 100 && h1m->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 2803 | /* we'll let the caller check if it has more headers to send */ |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 2804 | h1m->state = HTTP_MSG_RPBEFORE; |
| 2805 | h1m->status = 0; |
| 2806 | h1m->flags = 0; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 2807 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 2808 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2809 | else |
| 2810 | h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE; |
| 2811 | |
| 2812 | end: |
| 2813 | //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)); |
| 2814 | return ret; |
| 2815 | } |
| 2816 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 2817 | /* Try to send a DATA frame matching HTTP/1 response present in the response |
| 2818 | * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error |
| 2819 | * (one of the H2_ERR* or h2_status codes), >0 on success in which case it |
| 2820 | * corresponds to the number of buffer bytes consumed. |
| 2821 | */ |
| 2822 | static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf) |
| 2823 | { |
| 2824 | struct h2c *h2c = h2s->h2c; |
| 2825 | struct h1m *h1m = &h2s->res; |
| 2826 | struct chunk outbuf; |
| 2827 | int ret = 0; |
| 2828 | int total = 0; |
| 2829 | int es_now = 0; |
| 2830 | int size = 0; |
| 2831 | char *blk1, *blk2; |
| 2832 | int len1, len2; |
| 2833 | |
| 2834 | if (h2c_mux_busy(h2c, h2s)) { |
| 2835 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 2836 | goto end; |
| 2837 | } |
| 2838 | |
| 2839 | if (!h2_get_mbuf(h2c)) { |
| 2840 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 2841 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2842 | goto end; |
| 2843 | } |
| 2844 | |
| 2845 | new_frame: |
| 2846 | if (!buf->o) |
| 2847 | goto end; |
| 2848 | |
| 2849 | chunk_reset(&outbuf); |
| 2850 | |
| 2851 | while (1) { |
| 2852 | outbuf.str = bo_end(h2c->mbuf); |
| 2853 | outbuf.size = bo_contig_space(h2c->mbuf); |
| 2854 | outbuf.len = 0; |
| 2855 | |
| 2856 | if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf)) |
| 2857 | break; |
| 2858 | realign_again: |
| 2859 | buffer_slow_realign(h2c->mbuf); |
| 2860 | } |
| 2861 | |
| 2862 | if (outbuf.size < 9) { |
| 2863 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2864 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2865 | goto end; |
| 2866 | } |
| 2867 | |
| 2868 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 2869 | memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5); |
| 2870 | write_n32(outbuf.str + 5, h2s->id); // 4 bytes |
| 2871 | outbuf.len = 9; |
| 2872 | |
| 2873 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 2874 | case 0: /* no content length, read till SHUTW */ |
| 2875 | size = buf->o; |
| 2876 | break; |
| 2877 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
| 2878 | size = buf->o; |
| 2879 | if ((long long)size > h1m->curr_len) |
| 2880 | size = h1m->curr_len; |
| 2881 | break; |
| 2882 | default: /* te:chunked : parse chunks */ |
| 2883 | if (h1m->state == HTTP_MSG_CHUNK_CRLF) { |
| 2884 | ret = h1_skip_chunk_crlf(buf, -buf->o, 0); |
| 2885 | if (!ret) |
| 2886 | goto end; |
| 2887 | |
| 2888 | if (ret < 0) { |
| 2889 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 2890 | h1m->err_pos = ret; |
| 2891 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2892 | goto end; |
| 2893 | } |
| 2894 | bo_del(buf, ret); |
| 2895 | total += ret; |
| 2896 | h1m->state = HTTP_MSG_CHUNK_SIZE; |
| 2897 | } |
| 2898 | |
| 2899 | if (h1m->state == HTTP_MSG_CHUNK_SIZE) { |
| 2900 | unsigned int chunk; |
| 2901 | |
| 2902 | ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk); |
| 2903 | if (!ret) |
| 2904 | goto end; |
| 2905 | |
| 2906 | if (ret < 0) { |
| 2907 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 2908 | h1m->err_pos = ret; |
| 2909 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2910 | goto end; |
| 2911 | } |
| 2912 | |
| 2913 | size = chunk; |
| 2914 | h1m->curr_len = chunk; |
| 2915 | h1m->body_len += chunk; |
| 2916 | bo_del(buf, ret); |
| 2917 | total += ret; |
| 2918 | h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
| 2919 | if (!size) |
| 2920 | goto send_empty; |
| 2921 | } |
| 2922 | |
| 2923 | /* in MSG_DATA state, continue below */ |
| 2924 | size = h1m->curr_len; |
| 2925 | break; |
| 2926 | } |
| 2927 | |
| 2928 | /* we have in <size> the exact number of bytes we need to copy from |
| 2929 | * the H1 buffer. We need to check this against the connection's and |
| 2930 | * the stream's send windows, and to ensure that this fits in the max |
| 2931 | * frame size and in the buffer's available space minus 9 bytes (for |
| 2932 | * the frame header). The connection's flow control is applied last so |
| 2933 | * that we can use a separate list of streams which are immediately |
| 2934 | * unblocked on window opening. Note: we don't implement padding. |
| 2935 | */ |
| 2936 | |
| 2937 | if (size > buf->o) |
| 2938 | size = buf->o; |
| 2939 | |
| 2940 | if (size > h2s->mws) |
| 2941 | size = h2s->mws; |
| 2942 | |
| 2943 | if (size <= 0) { |
| 2944 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 2945 | goto end; |
| 2946 | } |
| 2947 | |
| 2948 | if (h2c->mfs && size > h2c->mfs) |
| 2949 | size = h2c->mfs; |
| 2950 | |
| 2951 | if (size + 9 > outbuf.size) { |
| 2952 | /* we have an opportunity for enlarging the too small |
| 2953 | * available space, let's try. |
| 2954 | */ |
| 2955 | if (buffer_space_wraps(h2c->mbuf)) |
| 2956 | goto realign_again; |
| 2957 | size = outbuf.size - 9; |
| 2958 | } |
| 2959 | |
| 2960 | if (size <= 0) { |
| 2961 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2962 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2963 | goto end; |
| 2964 | } |
| 2965 | |
| 2966 | if (size > h2c->mws) |
| 2967 | size = h2c->mws; |
| 2968 | |
| 2969 | if (size <= 0) { |
| 2970 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 2971 | goto end; |
| 2972 | } |
| 2973 | |
| 2974 | /* copy whatever we can */ |
| 2975 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
| 2976 | ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2); |
| 2977 | if (ret == 1) |
| 2978 | len2 = 0; |
| 2979 | |
| 2980 | if (!ret || len1 + len2 < size) { |
| 2981 | /* FIXME: must normally never happen */ |
| 2982 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2983 | goto end; |
| 2984 | } |
| 2985 | |
| 2986 | /* limit len1/len2 to size */ |
| 2987 | if (len1 + len2 > size) { |
| 2988 | int sub = len1 + len2 - size; |
| 2989 | |
| 2990 | if (len2 > sub) |
| 2991 | len2 -= sub; |
| 2992 | else { |
| 2993 | sub -= len2; |
| 2994 | len2 = 0; |
| 2995 | len1 -= sub; |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | /* now let's copy this this into the output buffer */ |
| 3000 | memcpy(outbuf.str + 9, blk1, len1); |
| 3001 | if (len2) |
| 3002 | memcpy(outbuf.str + 9 + len1, blk2, len2); |
| 3003 | |
| 3004 | send_empty: |
| 3005 | /* we may need to add END_STREAM */ |
| 3006 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 3007 | * could rely on the MSG_MORE flag as a hint for this ? |
| 3008 | */ |
| 3009 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
| 3010 | !h1m->curr_len || h1m->state >= HTTP_MSG_DONE) |
| 3011 | es_now = 1; |
| 3012 | |
| 3013 | /* update the frame's size */ |
| 3014 | h2_set_frame_size(outbuf.str, size); |
| 3015 | |
| 3016 | if (es_now) |
| 3017 | outbuf.str[4] |= H2_F_DATA_END_STREAM; |
| 3018 | |
| 3019 | /* commit the H2 response */ |
| 3020 | h2c->mbuf->o += size + 9; |
| 3021 | h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9); |
| 3022 | |
| 3023 | /* consume incoming H1 response */ |
| 3024 | if (size > 0) { |
| 3025 | bo_del(buf, size); |
| 3026 | total += size; |
| 3027 | h1m->curr_len -= size; |
| 3028 | h2s->mws -= size; |
| 3029 | h2c->mws -= size; |
| 3030 | |
| 3031 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
| 3032 | h1m->state = HTTP_MSG_CHUNK_CRLF; |
| 3033 | goto new_frame; |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | if (es_now) { |
| 3038 | if (h2s->st == H2_SS_OPEN) |
| 3039 | h2s->st = H2_SS_HLOC; |
| 3040 | else |
| 3041 | h2s->st = H2_SS_CLOSED; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3042 | |
| 3043 | if (!(h1m->flags & H1_MF_CHNK)) |
| 3044 | h1m->state = HTTP_MSG_DONE; |
| 3045 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3046 | h2s->flags |= H2_SF_ES_SENT; |
| 3047 | } |
| 3048 | |
| 3049 | end: |
| 3050 | trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%d", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, buf->o); |
| 3051 | return total; |
| 3052 | } |
| 3053 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3054 | /* Called from the upper layer, to send data */ |
| 3055 | static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags) |
| 3056 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3057 | struct h2s *h2s = cs->ctx; |
| 3058 | int total = 0; |
| 3059 | |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 3060 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o) |
| 3061 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 3062 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3063 | while (h2s->res.state < HTTP_MSG_DONE && buf->o) { |
| 3064 | if (h2s->res.state < HTTP_MSG_BODY) { |
| 3065 | total += h2s_frt_make_resp_headers(h2s, buf); |
| 3066 | |
| 3067 | if (h2s->st == H2_SS_ERROR) |
| 3068 | break; |
| 3069 | |
| 3070 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3071 | break; |
| 3072 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3073 | else if (h2s->res.state < HTTP_MSG_TRAILERS) { |
| 3074 | total += h2s_frt_make_resp_data(h2s, buf); |
| 3075 | |
| 3076 | if (h2s->st == H2_SS_ERROR) |
| 3077 | break; |
| 3078 | |
| 3079 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3080 | break; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3081 | } |
| 3082 | else if (h2s->res.state == HTTP_MSG_TRAILERS) { |
| 3083 | /* consume the trailers if any (we don't forward them for now) */ |
| 3084 | int count = h1_measure_trailers(buf); |
| 3085 | |
| 3086 | if (unlikely(count <= 0)) { |
| 3087 | if (count < 0) |
| 3088 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3089 | break; |
| 3090 | } |
| 3091 | total += count; |
| 3092 | bo_del(buf, count); |
| 3093 | h2s->res.state = HTTP_MSG_DONE; |
| 3094 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3095 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3096 | else { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3097 | cs->flags |= CS_FL_ERROR; |
| 3098 | break; |
| 3099 | } |
| 3100 | } |
| 3101 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3102 | /* RST are sent similarly to frame acks */ |
| 3103 | if (h2s->st == H2_SS_ERROR) { |
| 3104 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 3105 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3106 | h2s->st = H2_SS_CLOSED; |
| 3107 | } |
| 3108 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3109 | if (h2s->flags & H2_SF_BLK_SFCTL) { |
| 3110 | /* stream flow control, quit the list */ |
| 3111 | LIST_DEL(&h2s->list); |
| 3112 | LIST_INIT(&h2s->list); |
| 3113 | } |
| 3114 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3115 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | |
| 3119 | /*******************************************************/ |
| 3120 | /* functions below are dedicated to the config parsers */ |
| 3121 | /*******************************************************/ |
| 3122 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3123 | /* config parser for global "tune.h2.header-table-size" */ |
| 3124 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 3125 | struct proxy *defpx, const char *file, int line, |
| 3126 | char **err) |
| 3127 | { |
| 3128 | if (too_many_args(1, args, err, NULL)) |
| 3129 | return -1; |
| 3130 | |
| 3131 | h2_settings_header_table_size = atoi(args[1]); |
| 3132 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 3133 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 3134 | return -1; |
| 3135 | } |
| 3136 | return 0; |
| 3137 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3138 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3139 | /* config parser for global "tune.h2.initial-window-size" */ |
| 3140 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 3141 | struct proxy *defpx, const char *file, int line, |
| 3142 | char **err) |
| 3143 | { |
| 3144 | if (too_many_args(1, args, err, NULL)) |
| 3145 | return -1; |
| 3146 | |
| 3147 | h2_settings_initial_window_size = atoi(args[1]); |
| 3148 | if (h2_settings_initial_window_size < 0) { |
| 3149 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3150 | return -1; |
| 3151 | } |
| 3152 | return 0; |
| 3153 | } |
| 3154 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3155 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 3156 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 3157 | struct proxy *defpx, const char *file, int line, |
| 3158 | char **err) |
| 3159 | { |
| 3160 | if (too_many_args(1, args, err, NULL)) |
| 3161 | return -1; |
| 3162 | |
| 3163 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 3164 | if (h2_settings_max_concurrent_streams < 0) { |
| 3165 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3166 | return -1; |
| 3167 | } |
| 3168 | return 0; |
| 3169 | } |
| 3170 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3171 | |
| 3172 | /****************************************/ |
| 3173 | /* MUX initialization and instanciation */ |
| 3174 | /***************************************/ |
| 3175 | |
| 3176 | /* The mux operations */ |
| 3177 | const struct mux_ops h2_ops = { |
| 3178 | .init = h2_init, |
| 3179 | .recv = h2_recv, |
| 3180 | .send = h2_send, |
| 3181 | .wake = h2_wake, |
| 3182 | .update_poll = h2_update_poll, |
| 3183 | .rcv_buf = h2_rcv_buf, |
| 3184 | .snd_buf = h2_snd_buf, |
| 3185 | .attach = h2_attach, |
| 3186 | .detach = h2_detach, |
| 3187 | .shutr = h2_shutr, |
| 3188 | .shutw = h2_shutw, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3189 | .name = "H2", |
| 3190 | }; |
| 3191 | |
| 3192 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 3193 | static struct alpn_mux_list alpn_mux_h2 = |
| 3194 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 3195 | |
| 3196 | /* config keyword parsers */ |
| 3197 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3198 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3199 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3200 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3201 | { 0, NULL, NULL } |
| 3202 | }}; |
| 3203 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3204 | static void __h2_deinit(void) |
| 3205 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3206 | pool_destroy(pool_head_h2s); |
| 3207 | pool_destroy(pool_head_h2c); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3208 | } |
| 3209 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3210 | __attribute__((constructor)) |
| 3211 | static void __h2_init(void) |
| 3212 | { |
| 3213 | alpn_register_mux(&alpn_mux_h2); |
| 3214 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3215 | hap_register_post_deinit(__h2_deinit); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3216 | pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
| 3217 | pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3218 | } |