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