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