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