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