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