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