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