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