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 | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 15 | #include <common/h1.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 16 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 17 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 18 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <common/hpack-tbl.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 20 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 21 | #include <common/initcall.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 22 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/connection.h> |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 24 | #include <proto/http_htx.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 25 | #include <proto/session.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | #include <proto/stream.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 27 | #include <proto/stream_interface.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 28 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 32 | /* dummy streams returned for closed, error, refused, idle and states */ |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 33 | static const struct h2s *h2_closed_stream; |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 34 | static const struct h2s *h2_error_stream; |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 35 | static const struct h2s *h2_refused_stream; |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 36 | static const struct h2s *h2_idle_stream; |
| 37 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 38 | /* Connection flags (32 bit), in h2c->flags */ |
| 39 | #define H2_CF_NONE 0x00000000 |
| 40 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 41 | /* Flags indicating why writing to the mux is blocked. */ |
| 42 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 43 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 44 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 45 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 46 | /* Flags indicating why writing to the demux is blocked. |
| 47 | * The first two ones directly affect the ability for the mux to receive data |
| 48 | * from the connection. The other ones affect the mux's ability to demux |
| 49 | * received data. |
| 50 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 51 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 52 | #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] | 53 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 54 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 55 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 56 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 57 | #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] | 58 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 59 | #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] | 60 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 61 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 62 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 63 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 64 | #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] | 65 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 68 | /* H2 connection state, in h2c->st0 */ |
| 69 | enum h2_cs { |
| 70 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 71 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 72 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 73 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 74 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 75 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 76 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 77 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 78 | H2_CS_ENTRIES // must be last |
| 79 | } __attribute__((packed)); |
| 80 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 82 | /* 32 buffers: one for the ring's root, rest for the mbuf itself */ |
| 83 | #define H2C_MBUF_CNT 32 |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 85 | /* H2 connection descriptor */ |
| 86 | struct h2c { |
| 87 | struct connection *conn; |
| 88 | |
| 89 | enum h2_cs st0; /* mux state */ |
| 90 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 91 | |
| 92 | /* 16 bit hole here */ |
| 93 | uint32_t flags; /* connection flags: H2_CF_* */ |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 94 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 95 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 96 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 97 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 98 | |
| 99 | /* states for the demux direction */ |
| 100 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 101 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 102 | |
| 103 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 104 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 105 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 106 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 107 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 108 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 110 | |
| 111 | /* states for the mux direction */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 112 | struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 113 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 114 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 115 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 116 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 117 | /* 16 bit hole here */ |
| 118 | int32_t miw; /* mux initial window size for all new streams */ |
| 119 | int32_t mws; /* mux window size. Can be negative. */ |
| 120 | int32_t mfs; /* mux's max frame size */ |
| 121 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 122 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 123 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 124 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 125 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 126 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 127 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 128 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 129 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 130 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 131 | struct list send_list; /* list of blocked streams requesting to send */ |
| 132 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 133 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 134 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 135 | 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] | 136 | }; |
| 137 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 138 | /* H2 stream state, in h2s->st */ |
| 139 | enum h2_ss { |
| 140 | H2_SS_IDLE = 0, // idle |
| 141 | H2_SS_RLOC, // reserved(local) |
| 142 | H2_SS_RREM, // reserved(remote) |
| 143 | H2_SS_OPEN, // open |
| 144 | H2_SS_HREM, // half-closed(remote) |
| 145 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 146 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 147 | H2_SS_CLOSED, // closed |
| 148 | H2_SS_ENTRIES // must be last |
| 149 | } __attribute__((packed)); |
| 150 | |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 151 | #define H2_SS_MASK(state) (1UL << (state)) |
| 152 | #define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE) |
| 153 | #define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC) |
| 154 | #define H2_SS_RREM_BIT (1UL << H2_SS_RREM) |
| 155 | #define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN) |
| 156 | #define H2_SS_HREM_BIT (1UL << H2_SS_HREM) |
| 157 | #define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC) |
| 158 | #define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR) |
| 159 | #define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED) |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 161 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 162 | #define H2_SF_NONE 0x00000000 |
| 163 | #define H2_SF_ES_RCVD 0x00000001 |
| 164 | #define H2_SF_ES_SENT 0x00000002 |
| 165 | |
| 166 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 167 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 168 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 169 | /* stream flags indicating the reason the stream is blocked */ |
| 170 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 171 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 172 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 173 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 174 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 175 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 176 | /* stream flags indicating how data is supposed to be sent */ |
| 177 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 178 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 179 | |
Christopher Faulet | 4da0547 | 2019-07-18 15:26:47 +0200 | [diff] [blame] | 180 | /* unused flags: 0x00000400, 0x00000800 */ |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 181 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 182 | #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] | 183 | #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] | 184 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 185 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 186 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 187 | #define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy) |
| 188 | #define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy) |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 189 | #define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 190 | |
| 191 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 192 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 193 | * it is being processed in the internal HTTP representation (H1 for now). |
| 194 | */ |
| 195 | struct h2s { |
| 196 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 197 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 198 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 199 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 200 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 201 | int32_t id; /* stream ID */ |
| 202 | uint32_t flags; /* H2_SF_* */ |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 203 | int sws; /* stream window size, to be added to the mux's initial window size */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 204 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 205 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 206 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 207 | unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */ |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 208 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 209 | struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */ |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 210 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 211 | struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 212 | struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 213 | struct list sending_list; /* To be used when adding in h2c->sending_list */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 214 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 216 | /* descriptor for an h2 frame header */ |
| 217 | struct h2_fh { |
| 218 | uint32_t len; /* length, host order, 24 bits */ |
| 219 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 220 | uint8_t ft; /* frame type */ |
| 221 | uint8_t ff; /* frame flags */ |
| 222 | }; |
| 223 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 224 | /* the h2c connection pool */ |
| 225 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 226 | |
| 227 | /* the h2s stream pool */ |
| 228 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 229 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 230 | /* The default connection window size is 65535, it may only be enlarged using |
| 231 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 232 | * we'll pretend we already received the difference between the two to send |
| 233 | * an equivalent window update to enlarge it to 2G-1. |
| 234 | */ |
| 235 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 236 | |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 237 | /* maximum amount of data we're OK with re-aligning for buffer optimizations */ |
| 238 | #define MAX_DATA_REALIGN 1024 |
| 239 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 240 | /* a few settings from the global section */ |
| 241 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 242 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 243 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 244 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 245 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 246 | /* a dmumy closed stream */ |
| 247 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 248 | .cs = NULL, |
| 249 | .h2c = NULL, |
| 250 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 251 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 252 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 253 | .id = 0, |
| 254 | }; |
| 255 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 256 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 257 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 258 | .cs = NULL, |
| 259 | .h2c = NULL, |
| 260 | .st = H2_SS_CLOSED, |
| 261 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 262 | .flags = 0, |
| 263 | .id = 0, |
| 264 | }; |
| 265 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 266 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 267 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 268 | .cs = NULL, |
| 269 | .h2c = NULL, |
| 270 | .st = H2_SS_CLOSED, |
| 271 | .errcode = H2_ERR_REFUSED_STREAM, |
| 272 | .flags = 0, |
| 273 | .id = 0, |
| 274 | }; |
| 275 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 276 | /* and a dummy idle stream for use with any unannounced stream */ |
| 277 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 278 | .cs = NULL, |
| 279 | .h2c = NULL, |
| 280 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 281 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 282 | .id = 0, |
| 283 | }; |
| 284 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 285 | 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] | 286 | static int h2_send(struct h2c *h2c); |
| 287 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 288 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 289 | 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] | 290 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id); |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 291 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len); |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 292 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 293 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state); |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 294 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess); |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 295 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 296 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 297 | static __inline int |
| 298 | h2c_is_dead(struct h2c *h2c) |
| 299 | { |
| 300 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 301 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 302 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 303 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 304 | (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 305 | (conn_xprt_read0_pending(h2c->conn) || |
| 306 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 307 | return 1; |
| 308 | |
| 309 | return 0; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 312 | /*****************************************************/ |
| 313 | /* functions below are for dynamic buffer management */ |
| 314 | /*****************************************************/ |
| 315 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 316 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 317 | * to receive data into the buffer and/or demux pending data. The condition is |
| 318 | * a bit complex due to some API limits for now. The rules are the following : |
| 319 | * - if an error or a shutdown was detected on the connection and the buffer |
| 320 | * is empty, we must not attempt to receive |
| 321 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 322 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 323 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 324 | * regardless of whether the demux buffer is full or not, so that only |
| 325 | * de demux part decides whether or not to block. This is needed because |
| 326 | * the connection API indeed prevents us from re-enabling receipt that is |
| 327 | * already enabled in a polled state, so we must always immediately stop |
| 328 | * as soon as the demux can't proceed so as never to hit an end of read |
| 329 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 330 | * - otherwise must may not attempt |
| 331 | */ |
| 332 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 333 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 334 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 335 | (h2c->st0 >= H2_CS_ERROR || |
| 336 | h2c->conn->flags & CO_FL_ERROR || |
| 337 | conn_xprt_read0_pending(h2c->conn))) |
| 338 | return 0; |
| 339 | |
| 340 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 341 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 342 | return 1; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 347 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 348 | static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 349 | { |
| 350 | if (!h2_recv_allowed(h2c)) |
| 351 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 352 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 353 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 354 | return; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 355 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 359 | /* returns true if the front connection has too many conn_streams attached */ |
| 360 | static inline int h2_frt_has_too_many_cs(const struct h2c *h2c) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 361 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 362 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 365 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 366 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 367 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 368 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 369 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 370 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 371 | { |
| 372 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 373 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 374 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 375 | 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] | 376 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 377 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 378 | return 1; |
| 379 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 380 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 381 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 382 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 383 | |
| 384 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 385 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 386 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 387 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 388 | return 1; |
| 389 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 390 | |
| 391 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 392 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 393 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 394 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 395 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 396 | return 1; |
| 397 | } |
| 398 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 402 | 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] | 403 | { |
| 404 | struct buffer *buf = NULL; |
| 405 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 406 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 407 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 408 | h2c->buf_wait.target = h2c; |
| 409 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 410 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 411 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 412 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 413 | __conn_xprt_stop_recv(h2c->conn); |
| 414 | } |
| 415 | return buf; |
| 416 | } |
| 417 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 418 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 419 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 420 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 421 | b_free(bptr); |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 422 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 426 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 427 | { |
| 428 | struct buffer *buf; |
| 429 | unsigned int count = 0; |
| 430 | |
| 431 | while (b_size(buf = br_head_pick(h2c->mbuf))) { |
| 432 | b_free(buf); |
| 433 | count++; |
| 434 | } |
| 435 | if (count) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 436 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 439 | /* returns the number of allocatable outgoing streams for the connection taking |
| 440 | * the last_sid and the reserved ones into account. |
| 441 | */ |
| 442 | static inline int h2_streams_left(const struct h2c *h2c) |
| 443 | { |
| 444 | int ret; |
| 445 | |
| 446 | /* consider the number of outgoing streams we're allowed to create before |
| 447 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 448 | * nb_reserved is the number of streams which don't yet have an ID. |
| 449 | */ |
| 450 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 451 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 452 | if (ret < 0) |
| 453 | ret = 0; |
| 454 | return ret; |
| 455 | } |
| 456 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 457 | /* returns the number of streams in use on a connection to figure if it's |
| 458 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 459 | * to know if it was the last one after a detach(). |
| 460 | */ |
| 461 | static int h2_used_streams(struct connection *conn) |
| 462 | { |
| 463 | struct h2c *h2c = conn->ctx; |
| 464 | |
| 465 | return h2c->nb_cs; |
| 466 | } |
| 467 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 468 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 469 | static int h2_avail_streams(struct connection *conn) |
| 470 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 471 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 472 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 473 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 474 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 475 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 476 | * streams on the connection. |
| 477 | */ |
| 478 | if (h2c->last_sid >= 0) |
| 479 | return 0; |
| 480 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 481 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 482 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 483 | |
| 484 | /* we must also consider the limit imposed by stream IDs */ |
| 485 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 486 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 487 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 488 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 489 | ret1 = MIN(ret1, ret2); |
| 490 | } |
| 491 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 494 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 495 | /*****************************************************************/ |
| 496 | /* functions below are dedicated to the mux setup and management */ |
| 497 | /*****************************************************************/ |
| 498 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 499 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 500 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 501 | * connections from the fact that the context is still NULL (even during mux |
| 502 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 503 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 504 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 505 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 506 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 507 | { |
| 508 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 509 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 510 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 511 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 512 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 513 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 514 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 515 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 516 | h2c->flags = H2_CF_IS_BACK; |
| 517 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 518 | if (tick_isset(prx->timeout.serverfin)) |
| 519 | h2c->shut_timeout = prx->timeout.serverfin; |
| 520 | } else { |
| 521 | h2c->flags = H2_CF_NONE; |
| 522 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 523 | if (tick_isset(prx->timeout.clientfin)) |
| 524 | h2c->shut_timeout = prx->timeout.clientfin; |
| 525 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 526 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 527 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 528 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 529 | if (tick_isset(h2c->timeout)) { |
| 530 | t = task_new(tid_bit); |
| 531 | if (!t) |
| 532 | goto fail; |
| 533 | |
| 534 | h2c->task = t; |
| 535 | t->process = h2_timeout_task; |
| 536 | t->context = h2c; |
| 537 | t->expire = tick_add(now_ms, h2c->timeout); |
| 538 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 539 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 540 | h2c->wait_event.tasklet = tasklet_new(); |
| 541 | if (!h2c->wait_event.tasklet) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 542 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 543 | h2c->wait_event.tasklet->process = h2_io_cb; |
| 544 | h2c->wait_event.tasklet->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 545 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 546 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 547 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 548 | if (!h2c->ddht) |
| 549 | goto fail; |
| 550 | |
| 551 | /* Initialise the context. */ |
| 552 | h2c->st0 = H2_CS_PREFACE; |
| 553 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 554 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 555 | h2c->max_id = -1; |
| 556 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 557 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 558 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 559 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 560 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 561 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 562 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 563 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 564 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 565 | h2c->dsi = -1; |
| 566 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 567 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 568 | h2c->last_sid = -1; |
| 569 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 570 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 571 | h2c->miw = 65535; /* mux initial window size */ |
| 572 | h2c->mws = 65535; /* mux window size */ |
| 573 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 574 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 575 | LIST_INIT(&h2c->send_list); |
| 576 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 577 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 578 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 579 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 580 | if (t) |
| 581 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 582 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 583 | if (h2c->flags & H2_CF_IS_BACK) { |
| 584 | /* FIXME: this is temporary, for outgoing connections we need |
| 585 | * to immediately allocate a stream until the code is modified |
| 586 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 587 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 588 | */ |
| 589 | struct h2s *h2s; |
| 590 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 591 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 592 | if (!h2s) |
| 593 | goto fail_stream; |
| 594 | } |
| 595 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 596 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 597 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 598 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 599 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 600 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 601 | fail_stream: |
| 602 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 603 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 604 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 605 | if (h2c->wait_event.tasklet) |
| 606 | tasklet_free(h2c->wait_event.tasklet); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 607 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 608 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 609 | return -1; |
| 610 | } |
| 611 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 612 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 613 | * -1 if no more is allocatable. |
| 614 | */ |
| 615 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 616 | { |
| 617 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 618 | |
| 619 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 620 | id = -1; |
| 621 | return id; |
| 622 | } |
| 623 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 624 | /* returns the stream associated with id <id> or NULL if not found */ |
| 625 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 626 | { |
| 627 | struct eb32_node *node; |
| 628 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 629 | if (id == 0) |
| 630 | return (struct h2s *)h2_closed_stream; |
| 631 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 632 | if (id > h2c->max_id) |
| 633 | return (struct h2s *)h2_idle_stream; |
| 634 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 635 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 636 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 637 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 638 | |
| 639 | return container_of(node, struct h2s, by_id); |
| 640 | } |
| 641 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 642 | /* release function. This one should be called to free all resources allocated |
| 643 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 644 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 645 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 646 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 647 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 648 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 649 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 650 | /* The connection must be aattached to this mux to be released */ |
| 651 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 652 | conn = h2c->conn; |
| 653 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 654 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 655 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 656 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 657 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 658 | LIST_DEL(&h2c->buf_wait.list); |
| 659 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 660 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 662 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 663 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 664 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 665 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 666 | h2c->task->context = NULL; |
| 667 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 668 | h2c->task = NULL; |
| 669 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 670 | if (h2c->wait_event.tasklet) |
| 671 | tasklet_free(h2c->wait_event.tasklet); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 672 | if (h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 673 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 674 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 675 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 676 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 679 | if (conn) { |
| 680 | conn->mux = NULL; |
| 681 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 682 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 683 | conn_stop_tracking(conn); |
| 684 | conn_full_close(conn); |
| 685 | if (conn->destroy_cb) |
| 686 | conn->destroy_cb(conn); |
| 687 | conn_free(conn); |
| 688 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 692 | /******************************************************/ |
| 693 | /* functions below are for the H2 protocol processing */ |
| 694 | /******************************************************/ |
| 695 | |
| 696 | /* 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] | 697 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 698 | { |
| 699 | return h2s ? h2s->id : 0; |
| 700 | } |
| 701 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 702 | /* returns the sum of the stream's own window size and the mux's initial |
| 703 | * window, which together form the stream's effective window size. |
| 704 | */ |
| 705 | static inline int h2s_mws(const struct h2s *h2s) |
| 706 | { |
| 707 | return h2s->sws + h2s->h2c->miw; |
| 708 | } |
| 709 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 710 | /* 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] | 711 | 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] | 712 | { |
| 713 | if (h2c->msi < 0) |
| 714 | return 0; |
| 715 | |
| 716 | if (h2c->msi == h2s_id(h2s)) |
| 717 | return 0; |
| 718 | |
| 719 | return 1; |
| 720 | } |
| 721 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 722 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 723 | 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] | 724 | { |
| 725 | h2c->errcode = err; |
| 726 | h2c->st0 = H2_CS_ERROR; |
| 727 | } |
| 728 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 729 | /* marks an error on the stream. It may also update an already closed stream |
| 730 | * (e.g. to report an error after an RST was received). |
| 731 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 732 | 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] | 733 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 734 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 735 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 736 | if (h2s->st < H2_SS_ERROR) |
| 737 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 738 | if (h2s->cs) |
| 739 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 743 | /* attempt to notify the data layer of recv availability */ |
| 744 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 745 | { |
| 746 | struct wait_event *sw; |
| 747 | |
| 748 | if (h2s->recv_wait) { |
| 749 | sw = h2s->recv_wait; |
| 750 | sw->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 751 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 752 | h2s->recv_wait = NULL; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | /* attempt to notify the data layer of send availability */ |
| 757 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 758 | { |
| 759 | struct wait_event *sw; |
| 760 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 761 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 762 | sw = h2s->send_wait; |
| 763 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 764 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 765 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 769 | /* alerts the data layer, trying to wake it up by all means, following |
| 770 | * this sequence : |
| 771 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 772 | * - if its subscribed to send, then it's woken up for send |
| 773 | * - if it was subscribed to neither, its ->wake() callback is called |
| 774 | * It is safe to call this function with a closed stream which doesn't have a |
| 775 | * conn_stream anymore. |
| 776 | */ |
| 777 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 778 | { |
| 779 | if (h2s->recv_wait || h2s->send_wait) { |
| 780 | h2s_notify_recv(h2s); |
| 781 | h2s_notify_send(h2s); |
| 782 | } |
| 783 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 784 | h2s->cs->data_cb->wake(h2s->cs); |
| 785 | } |
| 786 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 787 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 788 | 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] | 789 | { |
| 790 | uint8_t *out = frame; |
| 791 | |
| 792 | *out = len >> 16; |
| 793 | write_n16(out + 1, len); |
| 794 | } |
| 795 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 796 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 797 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 798 | * 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] | 799 | * available in the buffer's input prior to calling this function. The buffer |
| 800 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 801 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 802 | 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] | 803 | const struct buffer *b, int o) |
| 804 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 805 | 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] | 806 | } |
| 807 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 808 | 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] | 809 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 810 | 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] | 811 | } |
| 812 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 813 | 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] | 814 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 815 | 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] | 816 | } |
| 817 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 818 | 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] | 819 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 820 | 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] | 821 | } |
| 822 | |
| 823 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 824 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 825 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 826 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 827 | * may wrap so each byte read must be checked. The header is formed like this : |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 828 | * |
| 829 | * b0 b1 b2 b3 b4 b5..b8 |
| 830 | * +----------+---------+--------+----+----+----------------------+ |
| 831 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 832 | * +----------+---------+--------+----+----+----------------------+ |
| 833 | * |
| 834 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 835 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 836 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 837 | * 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] | 838 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 839 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 840 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 841 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 842 | { |
| 843 | uint64_t w; |
| 844 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 845 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 846 | return 0; |
| 847 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 848 | w = h2_get_n64(b, o + 1); |
| 849 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 850 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 851 | h->ff = w >> 32; |
| 852 | h->ft = w >> 40; |
| 853 | h->len += w >> 48; |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 858 | * h2_peek_frame_hdr() above. |
| 859 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 860 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 861 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 862 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 866 | 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] | 867 | { |
| 868 | int ret; |
| 869 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 870 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 871 | if (ret > 0) |
| 872 | h2_skip_frame_hdr(b); |
| 873 | return ret; |
| 874 | } |
| 875 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 876 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 877 | * its connection if the stream was not yet closed. Please use this exclusively |
| 878 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 879 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 880 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 881 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 882 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 883 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 884 | if (!h2s->id) |
| 885 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 886 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 887 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 888 | h2s_notify_recv(h2s); |
| 889 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 890 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 891 | h2s->st = H2_SS_CLOSED; |
| 892 | } |
| 893 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 894 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 895 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 896 | { |
| 897 | h2s_close(h2s); |
| 898 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 899 | if (b_size(&h2s->rxbuf)) { |
| 900 | b_free(&h2s->rxbuf); |
| 901 | offer_buffers(NULL, tasks_run_queue); |
| 902 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 903 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 904 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 905 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 906 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 907 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 908 | * reference left would be in the h2c send_list/fctl_list, and if |
| 909 | * we're in it, we're getting out anyway |
| 910 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 911 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 912 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 913 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 914 | LIST_DEL_INIT(&h2s->sending_list); |
| 915 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 916 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 917 | pool_free(pool_head_h2s, h2s); |
| 918 | } |
| 919 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 920 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 921 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 922 | * causes of errors can be any failed memory allocation. The caller is |
| 923 | * responsible for checking if the connection may support an extra stream |
| 924 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 925 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 926 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 927 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 928 | struct h2s *h2s; |
| 929 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 930 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 931 | if (!h2s) |
| 932 | goto out; |
| 933 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 934 | h2s->wait_event.tasklet = tasklet_new(); |
| 935 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 936 | pool_free(pool_head_h2s, h2s); |
| 937 | goto out; |
| 938 | } |
| 939 | h2s->send_wait = NULL; |
| 940 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 941 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 942 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 943 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 944 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 945 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 946 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 947 | h2s->cs = NULL; |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 948 | h2s->sws = 0; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 949 | h2s->flags = H2_SF_NONE; |
| 950 | h2s->errcode = H2_ERR_NO_ERROR; |
| 951 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 952 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 953 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 954 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 955 | |
| 956 | if (h2c->flags & H2_CF_IS_BACK) { |
| 957 | h1m_init_req(&h2s->h1m); |
| 958 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 959 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 960 | } else { |
| 961 | h1m_init_res(&h2s->h1m); |
| 962 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 963 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 964 | } |
| 965 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 966 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 967 | if (id > 0) |
| 968 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 969 | else |
| 970 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 971 | |
| 972 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 973 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 974 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 975 | |
| 976 | return h2s; |
| 977 | |
| 978 | out_free_h2s: |
| 979 | pool_free(pool_head_h2s, h2s); |
| 980 | out: |
| 981 | return NULL; |
| 982 | } |
| 983 | |
| 984 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 985 | * case of memory allocation error. |
| 986 | */ |
| 987 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 988 | { |
| 989 | struct session *sess = h2c->conn->owner; |
| 990 | struct conn_stream *cs; |
| 991 | struct h2s *h2s; |
| 992 | |
| 993 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 994 | goto out; |
| 995 | |
| 996 | h2s = h2s_new(h2c, id); |
| 997 | if (!h2s) |
| 998 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 999 | |
| 1000 | cs = cs_new(h2c->conn); |
| 1001 | if (!cs) |
| 1002 | goto out_close; |
| 1003 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1004 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1005 | h2s->cs = cs; |
| 1006 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1007 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1008 | |
| 1009 | if (stream_create_from_cs(cs) < 0) |
| 1010 | goto out_free_cs; |
| 1011 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1012 | /* We want the accept date presented to the next stream to be the one |
| 1013 | * we have now, the handshake time to be null (since the next stream |
| 1014 | * is not delayed by a handshake), and the idle time to count since |
| 1015 | * right now. |
| 1016 | */ |
| 1017 | sess->accept_date = date; |
| 1018 | sess->tv_accept = now; |
| 1019 | sess->t_handshake = 0; |
| 1020 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1021 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1022 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1023 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1024 | return h2s; |
| 1025 | |
| 1026 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1027 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1028 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1029 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1030 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1031 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1032 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1033 | sess_log(sess); |
| 1034 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1037 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1038 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1039 | * possible stream ID was reached. |
| 1040 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1041 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1042 | { |
| 1043 | struct h2s *h2s = NULL; |
| 1044 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1045 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1046 | goto out; |
| 1047 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1048 | if (h2_streams_left(h2c) < 1) |
| 1049 | goto out; |
| 1050 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1051 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1052 | h2s = h2s_new(h2c, 0); |
| 1053 | if (!h2s) |
| 1054 | goto out; |
| 1055 | |
| 1056 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1057 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1058 | cs->ctx = h2s; |
| 1059 | h2c->nb_cs++; |
| 1060 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1061 | out: |
| 1062 | return h2s; |
| 1063 | } |
| 1064 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1065 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1066 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1067 | * the various settings codes. |
| 1068 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1069 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1070 | { |
| 1071 | struct buffer *res; |
| 1072 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1073 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1074 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1075 | int ret; |
| 1076 | |
| 1077 | if (h2c_mux_busy(h2c, NULL)) { |
| 1078 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1082 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1083 | chunk_memcpy(&buf, |
| 1084 | "\x00\x00\x00" /* length : 0 for now */ |
| 1085 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1086 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1087 | 9); |
| 1088 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1089 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1090 | /* send settings_enable_push=0 */ |
| 1091 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1092 | } |
| 1093 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1094 | if (h2_settings_header_table_size != 4096) { |
| 1095 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1096 | |
| 1097 | write_n32(str + 2, h2_settings_header_table_size); |
| 1098 | chunk_memcat(&buf, str, 6); |
| 1099 | } |
| 1100 | |
| 1101 | if (h2_settings_initial_window_size != 65535) { |
| 1102 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1103 | |
| 1104 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1105 | chunk_memcat(&buf, str, 6); |
| 1106 | } |
| 1107 | |
| 1108 | if (h2_settings_max_concurrent_streams != 0) { |
| 1109 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1110 | |
| 1111 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1112 | * the protocol, so never send this value! |
| 1113 | */ |
| 1114 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1115 | chunk_memcat(&buf, str, 6); |
| 1116 | } |
| 1117 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1118 | mfs = h2_settings_max_frame_size; |
| 1119 | if (mfs > global.tune.bufsize) |
| 1120 | mfs = global.tune.bufsize; |
| 1121 | |
| 1122 | if (!mfs) |
| 1123 | mfs = global.tune.bufsize; |
| 1124 | |
| 1125 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1126 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1127 | |
| 1128 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1129 | * match bufsize - rewrite size, but at the moment it seems |
| 1130 | * that clients don't take care of it. |
| 1131 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1132 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1133 | chunk_memcat(&buf, str, 6); |
| 1134 | } |
| 1135 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1136 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1137 | |
| 1138 | res = br_tail(h2c->mbuf); |
| 1139 | retry: |
| 1140 | if (!h2_get_buf(h2c, res)) { |
| 1141 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1142 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1146 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1147 | if (unlikely(ret <= 0)) { |
| 1148 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1149 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1150 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1151 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1152 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | else { |
| 1156 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1157 | return 0; |
| 1158 | } |
| 1159 | } |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1163 | /* Try to receive a connection preface, then upon success try to send our |
| 1164 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1165 | * missing data. It may return an error in h2c. |
| 1166 | */ |
| 1167 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1168 | { |
| 1169 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1170 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1171 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1172 | 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] | 1173 | |
| 1174 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1175 | if (ret1 < 0) |
| 1176 | sess_log(h2c->conn->owner); |
| 1177 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1178 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1179 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1183 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1184 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1185 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1186 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1187 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1190 | /* Try to send a connection preface, then upon success try to send our |
| 1191 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1192 | * missing data. It may return an error in h2c. |
| 1193 | */ |
| 1194 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1195 | { |
| 1196 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1197 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1198 | |
| 1199 | if (h2c_mux_busy(h2c, NULL)) { |
| 1200 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1204 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1205 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1206 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1207 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1208 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | if (!b_data(res)) { |
| 1213 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1214 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1215 | if (unlikely(ret <= 0)) { |
| 1216 | if (!ret) { |
| 1217 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1218 | goto retry; |
| 1219 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1220 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | else { |
| 1224 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1228 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1229 | return h2c_send_settings(h2c); |
| 1230 | } |
| 1231 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1232 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1233 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1234 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1235 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1236 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1237 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1238 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1239 | * case so that it is safe to use h2c_error() to report such errors. |
| 1240 | */ |
| 1241 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1242 | { |
| 1243 | struct buffer *res; |
| 1244 | char str[17]; |
| 1245 | int ret; |
| 1246 | |
| 1247 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1248 | return 1; // claim that it worked |
| 1249 | |
| 1250 | if (h2c_mux_busy(h2c, h2s)) { |
| 1251 | if (h2s) |
| 1252 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1253 | else |
| 1254 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1258 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1259 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1260 | |
| 1261 | if (h2c->last_sid < 0) |
| 1262 | h2c->last_sid = h2c->max_id; |
| 1263 | |
| 1264 | write_n32(str + 9, h2c->last_sid); |
| 1265 | write_n32(str + 13, h2c->errcode); |
| 1266 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1267 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1268 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1269 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1270 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1271 | if (h2s) |
| 1272 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1273 | else |
| 1274 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1278 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1279 | if (unlikely(ret <= 0)) { |
| 1280 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1281 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1282 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1283 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1284 | if (h2s) |
| 1285 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1286 | else |
| 1287 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | else { |
| 1291 | /* we cannot report this error using GOAWAY, so we mark |
| 1292 | * it and claim a success. |
| 1293 | */ |
| 1294 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1295 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1296 | return 1; |
| 1297 | } |
| 1298 | } |
| 1299 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1303 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1304 | * during mux operations. This stream must be valid and cannot be closed |
| 1305 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1306 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1307 | * not yet. |
| 1308 | * |
| 1309 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1310 | * to write the message, it subscribes the stream to future notifications. |
| 1311 | */ |
| 1312 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1313 | { |
| 1314 | struct buffer *res; |
| 1315 | char str[13]; |
| 1316 | int ret; |
| 1317 | |
| 1318 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1319 | return 1; |
| 1320 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1321 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1322 | * RST_STREAM in response to a RST_STREAM frame. |
| 1323 | */ |
Willy Tarreau | 231f616 | 2019-08-06 10:01:40 +0200 | [diff] [blame] | 1324 | if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) { |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1325 | ret = 1; |
| 1326 | goto ignore; |
| 1327 | } |
| 1328 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1329 | if (h2c_mux_busy(h2c, h2s)) { |
| 1330 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1334 | /* len: 4, type: 3, flags: none */ |
| 1335 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1336 | write_n32(str + 5, h2s->id); |
| 1337 | write_n32(str + 9, h2s->errcode); |
| 1338 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1339 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1340 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1341 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1342 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1343 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1347 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1348 | if (unlikely(ret <= 0)) { |
| 1349 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1350 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1351 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1352 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1353 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | else { |
| 1357 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1358 | return 0; |
| 1359 | } |
| 1360 | } |
| 1361 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1362 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1363 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1364 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1369 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1370 | * error code, even if the stream is one of the dummy ones, and will update |
| 1371 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1372 | * |
| 1373 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1374 | * to write the message, it blocks the demuxer and subscribes it to future |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 1375 | * notifications. It's worth mentioning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1376 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1377 | */ |
| 1378 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1379 | { |
| 1380 | struct buffer *res; |
| 1381 | char str[13]; |
| 1382 | int ret; |
| 1383 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1384 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1385 | * RST_STREAM in response to a RST_STREAM frame. |
| 1386 | */ |
| 1387 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1388 | ret = 1; |
| 1389 | goto ignore; |
| 1390 | } |
| 1391 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1392 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1393 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1397 | /* len: 4, type: 3, flags: none */ |
| 1398 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1399 | |
| 1400 | write_n32(str + 5, h2c->dsi); |
| 1401 | write_n32(str + 9, h2s->errcode); |
| 1402 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1403 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1404 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1405 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1406 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1407 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1408 | return 0; |
| 1409 | } |
| 1410 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1411 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1412 | if (unlikely(ret <= 0)) { |
| 1413 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1414 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1415 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1416 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1417 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | else { |
| 1421 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1422 | return 0; |
| 1423 | } |
| 1424 | } |
| 1425 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1426 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1427 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1428 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1429 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1430 | } |
| 1431 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1432 | return ret; |
| 1433 | } |
| 1434 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1435 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1436 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1437 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1438 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1439 | * message, it subscribes the requesting stream to future notifications. |
| 1440 | */ |
| 1441 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1442 | { |
| 1443 | struct h2c *h2c = h2s->h2c; |
| 1444 | struct buffer *res; |
| 1445 | char str[9]; |
| 1446 | int ret; |
| 1447 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1448 | 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] | 1449 | return 1; |
| 1450 | |
| 1451 | if (h2c_mux_busy(h2c, h2s)) { |
| 1452 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1456 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1457 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1458 | write_n32(str + 5, h2s->id); |
| 1459 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1460 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1461 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1462 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1463 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1464 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1468 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1469 | if (likely(ret > 0)) { |
| 1470 | h2s->flags |= H2_SF_ES_SENT; |
| 1471 | } |
| 1472 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1473 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1474 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1475 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1476 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1477 | return 0; |
| 1478 | } |
| 1479 | else { |
| 1480 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1481 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1482 | } |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1486 | /* wake a specific stream and assign its conn_stream som CS_FL_* flags among |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 1487 | * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1488 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1489 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1490 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1491 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1492 | { |
| 1493 | if (!h2s->cs) { |
| 1494 | /* this stream was already orphaned */ |
| 1495 | h2s_destroy(h2s); |
| 1496 | return; |
| 1497 | } |
| 1498 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1499 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1500 | if (h2s->st == H2_SS_OPEN) |
| 1501 | h2s->st = H2_SS_HREM; |
| 1502 | else if (h2s->st == H2_SS_HLOC) |
| 1503 | h2s_close(h2s); |
| 1504 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1505 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1506 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1507 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1508 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1509 | if (h2s->cs->flags & CS_FL_EOS) |
| 1510 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1511 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1512 | if (h2s->st < H2_SS_ERROR) |
| 1513 | h2s->st = H2_SS_ERROR; |
| 1514 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1515 | |
| 1516 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1520 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1521 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1522 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1523 | { |
| 1524 | struct eb32_node *node; |
| 1525 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1526 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1527 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1528 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1529 | while (node) { |
| 1530 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1531 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1532 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1533 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1534 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1535 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1536 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1537 | while (node) { |
| 1538 | h2s = container_of(node, struct h2s, by_id); |
| 1539 | if (h2s->id > 0) |
| 1540 | break; |
| 1541 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1542 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1546 | /* Wake up all blocked streams whose window size has become positive after the |
| 1547 | * mux's initial window was adjusted. This should be done after having processed |
| 1548 | * SETTINGS frames which have updated the mux's initial window size. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1549 | */ |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1550 | static void h2c_unblock_sfctl(struct h2c *h2c) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1551 | { |
| 1552 | struct h2s *h2s; |
| 1553 | struct eb32_node *node; |
| 1554 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1555 | node = eb32_first(&h2c->streams_by_id); |
| 1556 | while (node) { |
| 1557 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1558 | if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) { |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1559 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1560 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1561 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1562 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1563 | node = eb32_next(node); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1568 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1569 | * return an error in h2c. The caller must have already verified frame length |
| 1570 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1571 | */ |
| 1572 | static int h2c_handle_settings(struct h2c *h2c) |
| 1573 | { |
| 1574 | unsigned int offset; |
| 1575 | int error; |
| 1576 | |
| 1577 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1578 | if (h2c->dfl) { |
| 1579 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1580 | goto fail; |
| 1581 | } |
| 1582 | return 1; |
| 1583 | } |
| 1584 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1585 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1586 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1587 | return 0; |
| 1588 | |
| 1589 | /* parse the frame */ |
| 1590 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1591 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1592 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1593 | |
| 1594 | switch (type) { |
| 1595 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1596 | /* we need to update all existing streams with the |
| 1597 | * difference from the previous iws. |
| 1598 | */ |
| 1599 | if (arg < 0) { // RFC7540#6.5.2 |
| 1600 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1601 | goto fail; |
| 1602 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1603 | h2c->miw = arg; |
| 1604 | break; |
| 1605 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1606 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1607 | error = H2_ERR_PROTOCOL_ERROR; |
| 1608 | goto fail; |
| 1609 | } |
| 1610 | h2c->mfs = arg; |
| 1611 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1612 | case H2_SETTINGS_ENABLE_PUSH: |
| 1613 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1614 | error = H2_ERR_PROTOCOL_ERROR; |
| 1615 | goto fail; |
| 1616 | } |
| 1617 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1618 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1619 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1620 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1621 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1622 | arg = h2_settings_max_concurrent_streams; |
| 1623 | h2c->streams_limit = arg; |
| 1624 | } |
| 1625 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /* need to ACK this frame now */ |
| 1630 | h2c->st0 = H2_CS_FRAME_A; |
| 1631 | return 1; |
| 1632 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1633 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1634 | h2c_error(h2c, error); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1639 | * success or one of the h2_status values. |
| 1640 | */ |
| 1641 | static int h2c_ack_settings(struct h2c *h2c) |
| 1642 | { |
| 1643 | struct buffer *res; |
| 1644 | char str[9]; |
| 1645 | int ret = -1; |
| 1646 | |
| 1647 | if (h2c_mux_busy(h2c, NULL)) { |
| 1648 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1652 | memcpy(str, |
| 1653 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1654 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1655 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1656 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1657 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1658 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1659 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1660 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1661 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1665 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1666 | if (unlikely(ret <= 0)) { |
| 1667 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1668 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1669 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1670 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1671 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1672 | return 0; |
| 1673 | } |
| 1674 | else { |
| 1675 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | } |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1682 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1683 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1684 | * missing data. The caller must have already verified frame length |
| 1685 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1686 | */ |
| 1687 | static int h2c_handle_ping(struct h2c *h2c) |
| 1688 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1689 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1690 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1691 | h2c->st0 = H2_CS_FRAME_A; |
| 1692 | return 1; |
| 1693 | } |
| 1694 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1695 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1696 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1697 | * error in h2c. |
| 1698 | */ |
| 1699 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1700 | { |
| 1701 | struct buffer *res; |
| 1702 | char str[13]; |
| 1703 | int ret = -1; |
| 1704 | |
| 1705 | if (h2c_mux_busy(h2c, NULL)) { |
| 1706 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1710 | /* length: 4, type: 8, flags: none */ |
| 1711 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1712 | write_n32(str + 5, sid); |
| 1713 | write_n32(str + 9, increment); |
| 1714 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1715 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1716 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1717 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1718 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1719 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1723 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1724 | if (unlikely(ret <= 0)) { |
| 1725 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1726 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1727 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1728 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1729 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1730 | return 0; |
| 1731 | } |
| 1732 | else { |
| 1733 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | } |
| 1737 | return ret; |
| 1738 | } |
| 1739 | |
| 1740 | /* try to send pending window update for the connection. It's safe to call it |
| 1741 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1742 | * failure. It may return an error in h2c. |
| 1743 | */ |
| 1744 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1745 | { |
| 1746 | int ret = 1; |
| 1747 | |
| 1748 | if (h2c->rcvd_c <= 0) |
| 1749 | return 1; |
| 1750 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1751 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1752 | /* increase the advertised connection window to 2G on |
| 1753 | * first update. |
| 1754 | */ |
| 1755 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1756 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1757 | } |
| 1758 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1759 | /* send WU for the connection */ |
| 1760 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1761 | if (ret > 0) |
| 1762 | h2c->rcvd_c = 0; |
| 1763 | |
| 1764 | return ret; |
| 1765 | } |
| 1766 | |
| 1767 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1768 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1769 | * room or failure. It may return an error in h2c. |
| 1770 | */ |
| 1771 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1772 | { |
| 1773 | int ret = 1; |
| 1774 | |
| 1775 | if (h2c->rcvd_s <= 0) |
| 1776 | return 1; |
| 1777 | |
| 1778 | /* send WU for the stream */ |
| 1779 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1780 | if (ret > 0) |
| 1781 | h2c->rcvd_s = 0; |
| 1782 | |
| 1783 | return ret; |
| 1784 | } |
| 1785 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1786 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1787 | * success, 0 on missing data or one of the h2_status values. |
| 1788 | */ |
| 1789 | static int h2c_ack_ping(struct h2c *h2c) |
| 1790 | { |
| 1791 | struct buffer *res; |
| 1792 | char str[17]; |
| 1793 | int ret = -1; |
| 1794 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1795 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1796 | return 0; |
| 1797 | |
| 1798 | if (h2c_mux_busy(h2c, NULL)) { |
| 1799 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1800 | return 0; |
| 1801 | } |
| 1802 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1803 | memcpy(str, |
| 1804 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1805 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1806 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1807 | |
| 1808 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1809 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1810 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1811 | res = br_tail(h2c->mbuf); |
| 1812 | retry: |
| 1813 | if (!h2_get_buf(h2c, res)) { |
| 1814 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1815 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1819 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1820 | if (unlikely(ret <= 0)) { |
| 1821 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1822 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1823 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1824 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1825 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1826 | return 0; |
| 1827 | } |
| 1828 | else { |
| 1829 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1830 | return 0; |
| 1831 | } |
| 1832 | } |
| 1833 | return ret; |
| 1834 | } |
| 1835 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1836 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1837 | * Returns > 0 on success or zero on missing data. It may return an error in |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1838 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1839 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1840 | */ |
| 1841 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1842 | { |
| 1843 | int32_t inc; |
| 1844 | int error; |
| 1845 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1846 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1847 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1848 | return 0; |
| 1849 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1850 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1851 | |
| 1852 | if (h2c->dsi != 0) { |
| 1853 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1854 | |
| 1855 | /* it's not an error to receive WU on a closed stream */ |
| 1856 | if (h2s->st == H2_SS_CLOSED) |
| 1857 | return 1; |
| 1858 | |
| 1859 | if (!inc) { |
| 1860 | error = H2_ERR_PROTOCOL_ERROR; |
| 1861 | goto strm_err; |
| 1862 | } |
| 1863 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1864 | if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1865 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1866 | goto strm_err; |
| 1867 | } |
| 1868 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1869 | h2s->sws += inc; |
| 1870 | if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1871 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1872 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1873 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | else { |
| 1877 | /* connection window update */ |
| 1878 | if (!inc) { |
| 1879 | error = H2_ERR_PROTOCOL_ERROR; |
| 1880 | goto conn_err; |
| 1881 | } |
| 1882 | |
| 1883 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1884 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1885 | goto conn_err; |
| 1886 | } |
| 1887 | |
| 1888 | h2c->mws += inc; |
| 1889 | } |
| 1890 | |
| 1891 | return 1; |
| 1892 | |
| 1893 | conn_err: |
| 1894 | h2c_error(h2c, error); |
| 1895 | return 0; |
| 1896 | |
| 1897 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1898 | h2s_error(h2s, error); |
| 1899 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1900 | return 0; |
| 1901 | } |
| 1902 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1903 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1904 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1905 | * have already verified frame length and stream ID validity. Described in |
| 1906 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1907 | */ |
| 1908 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1909 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1910 | int last; |
| 1911 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1912 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1913 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1914 | return 0; |
| 1915 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1916 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1917 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1918 | if (h2c->last_sid < 0) |
| 1919 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1920 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1921 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1922 | } |
| 1923 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1924 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1925 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1926 | * error in h2c. The caller must have already verified frame length and stream |
| 1927 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1928 | */ |
| 1929 | static int h2c_handle_priority(struct h2c *h2c) |
| 1930 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1931 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1932 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1933 | return 0; |
| 1934 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1935 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1936 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1937 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1938 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1939 | } |
| 1940 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1941 | } |
| 1942 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1943 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1944 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1945 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1946 | */ |
| 1947 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1948 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1949 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1950 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1951 | return 0; |
| 1952 | |
| 1953 | /* late RST, already handled */ |
| 1954 | if (h2s->st == H2_SS_CLOSED) |
| 1955 | return 1; |
| 1956 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1957 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1958 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1959 | |
| 1960 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1961 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1962 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | h2s->flags |= H2_SF_RST_RCVD; |
| 1966 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1969 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1970 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1971 | * return value is the new h2s in case one was allocated (most common case). |
| 1972 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1973 | * errors here are reported as connection errors since it's impossible to |
| 1974 | * recover from such errors after the compression context has been altered. |
| 1975 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1978 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1979 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1980 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1981 | int error; |
| 1982 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1983 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1984 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1985 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1986 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1987 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1988 | |
| 1989 | /* now either the frame is complete or the buffer is complete */ |
| 1990 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1991 | /* The stream exists/existed, this must be a trailers frame */ |
| 1992 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 1993 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 1994 | /* unrecoverable error ? */ |
| 1995 | if (h2c->st0 >= H2_CS_ERROR) |
| 1996 | goto out; |
| 1997 | |
| 1998 | if (error == 0) |
| 1999 | goto out; // missing data |
| 2000 | |
| 2001 | if (error < 0) { |
| 2002 | /* Failed to decode this frame (e.g. too large request) |
| 2003 | * but the HPACK decompressor is still synchronized. |
| 2004 | */ |
| 2005 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2006 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2007 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2008 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2009 | goto done; |
| 2010 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2011 | /* the connection was already killed by an RST, let's consume |
| 2012 | * the data and send another RST. |
| 2013 | */ |
| 2014 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2015 | h2s = (struct h2s*)h2_error_stream; |
| 2016 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2017 | } |
| 2018 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2019 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2020 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2021 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2022 | goto conn_err; |
| 2023 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2024 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2025 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2026 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2027 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2028 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2029 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2030 | if (h2c->st0 >= H2_CS_ERROR) |
| 2031 | goto out; |
| 2032 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2033 | if (error <= 0) { |
| 2034 | if (error == 0) |
| 2035 | goto out; // missing data |
| 2036 | |
| 2037 | /* Failed to decode this stream (e.g. too large request) |
| 2038 | * but the HPACK decompressor is still synchronized. |
| 2039 | */ |
| 2040 | h2s = (struct h2s*)h2_error_stream; |
| 2041 | goto send_rst; |
| 2042 | } |
| 2043 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2044 | /* 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] | 2045 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2046 | * 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] | 2047 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2048 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2049 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2050 | h2s = (struct h2s*)h2_refused_stream; |
| 2051 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2055 | h2s->rxbuf = rxbuf; |
| 2056 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2057 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2058 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2059 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2060 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2061 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2062 | |
| 2063 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2064 | if (h2s->st == H2_SS_OPEN) |
| 2065 | h2s->st = H2_SS_HREM; |
| 2066 | else |
| 2067 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2070 | /* update the max stream ID if the request is being processed */ |
| 2071 | if (h2s->id > h2c->max_id) |
| 2072 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2073 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2074 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2075 | |
| 2076 | conn_err: |
| 2077 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2078 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2079 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2080 | out: |
| 2081 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2082 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2083 | |
| 2084 | send_rst: |
| 2085 | /* make the demux send an RST for the current stream. We may only |
| 2086 | * do this if we're certain that the HEADERS frame was properly |
| 2087 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2088 | */ |
| 2089 | h2_release_buf(h2c, &rxbuf); |
| 2090 | h2c->st0 = H2_CS_FRAME_E; |
| 2091 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2092 | } |
| 2093 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2094 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2095 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2096 | * errors here are reported as connection errors since it's impossible to |
| 2097 | * recover from such errors after the compression context has been altered. |
| 2098 | */ |
| 2099 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2100 | { |
| 2101 | int error; |
| 2102 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2103 | if (!b_size(&h2c->dbuf)) |
| 2104 | return NULL; // empty buffer |
| 2105 | |
| 2106 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2107 | return NULL; // incomplete frame |
| 2108 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2109 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2110 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2111 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2112 | if (h2c->st0 >= H2_CS_ERROR) |
| 2113 | return NULL; |
| 2114 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2115 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2116 | /* RFC7540#5.1 */ |
| 2117 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2118 | h2c->st0 = H2_CS_FRAME_E; |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2122 | if (error <= 0) { |
| 2123 | if (error == 0) |
| 2124 | return NULL; // missing data |
| 2125 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2126 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2127 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2128 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2129 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2130 | } |
| 2131 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2132 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2133 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2134 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2135 | if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2136 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2137 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2138 | if (h2s->st == H2_SS_OPEN) |
| 2139 | h2s->st = H2_SS_HREM; |
| 2140 | else if (h2s->st == H2_SS_HLOC) |
| 2141 | h2s_close(h2s); |
| 2142 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2143 | |
| 2144 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2145 | } |
| 2146 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2147 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2148 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2149 | */ |
| 2150 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2151 | { |
| 2152 | int error; |
| 2153 | |
| 2154 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2155 | * to signal an end of stream (with the ES flag). |
| 2156 | */ |
| 2157 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2158 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2159 | return 0; // empty buffer |
| 2160 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2161 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2162 | return 0; // incomplete frame |
| 2163 | |
| 2164 | /* now either the frame is complete or the buffer is complete */ |
| 2165 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2166 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2167 | /* RFC7540#6.1 */ |
| 2168 | error = H2_ERR_STREAM_CLOSED; |
| 2169 | goto strm_err; |
| 2170 | } |
| 2171 | |
Christopher Faulet | 4f09ec8 | 2019-06-19 09:25:58 +0200 | [diff] [blame] | 2172 | if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2173 | /* RFC7540#8.1.2 */ |
| 2174 | error = H2_ERR_PROTOCOL_ERROR; |
| 2175 | goto strm_err; |
| 2176 | } |
| 2177 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2178 | if (!h2_frt_transfer_data(h2s)) |
| 2179 | return 0; |
| 2180 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2181 | /* call the upper layers to process the frame, then let the upper layer |
| 2182 | * notify the stream about any change. |
| 2183 | */ |
| 2184 | if (!h2s->cs) { |
Willy Tarreau | 082c457 | 2019-08-06 10:11:02 +0200 | [diff] [blame] | 2185 | /* The upper layer has already closed, this may happen on |
| 2186 | * 4xx/redirects during POST, or when receiving a response |
| 2187 | * from an H2 server after the client has aborted. |
| 2188 | */ |
| 2189 | error = H2_ERR_CANCEL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2190 | goto strm_err; |
| 2191 | } |
| 2192 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2193 | if (h2c->st0 >= H2_CS_ERROR) |
| 2194 | return 0; |
| 2195 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2196 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2197 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2198 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | /* check for completion : the callee will change this to FRAME_A or |
| 2202 | * FRAME_H once done. |
| 2203 | */ |
| 2204 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2205 | return 0; |
| 2206 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2207 | /* last frame */ |
| 2208 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2209 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2210 | if (h2s->st == H2_SS_OPEN) |
| 2211 | h2s->st = H2_SS_HREM; |
| 2212 | else |
| 2213 | h2s_close(h2s); |
| 2214 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2215 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2216 | /* RFC7540#8.1.2 */ |
| 2217 | error = H2_ERR_PROTOCOL_ERROR; |
| 2218 | goto strm_err; |
| 2219 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2220 | } |
| 2221 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2222 | return 1; |
| 2223 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2224 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2225 | h2s_error(h2s, error); |
| 2226 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2227 | return 0; |
| 2228 | } |
| 2229 | |
Willy Tarreau | 6386481 | 2019-08-07 14:25:20 +0200 | [diff] [blame] | 2230 | /* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is |
| 2231 | * valid for the current stream state. This is needed only after parsing the |
| 2232 | * frame header but in practice it can be performed at any time during |
| 2233 | * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success |
| 2234 | * or 0 in case of error, in which case either h2s or h2c will carry an error. |
| 2235 | */ |
| 2236 | static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s) |
| 2237 | { |
| 2238 | if (h2s->st == H2_SS_IDLE && |
| 2239 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2240 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2241 | * this state MUST be treated as a connection error |
| 2242 | */ |
| 2243 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2244 | if (!h2c->nb_streams) { |
| 2245 | /* only log if no other stream can report the error */ |
| 2246 | sess_log(h2c->conn->owner); |
| 2247 | } |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
| 2251 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2252 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2253 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 2254 | * this state MUST be treated as a stream error. |
| 2255 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2256 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
| 2257 | */ |
| 2258 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2259 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2260 | else |
| 2261 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | /* Below the management of frames received in closed state is a |
| 2266 | * bit hackish because the spec makes strong differences between |
| 2267 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2268 | * in both directions. In addition to this, the creation of a |
| 2269 | * new stream reusing the identifier of a closed one will be |
| 2270 | * detected here. Given that we cannot keep track of all closed |
| 2271 | * streams forever, we consider that unknown closed streams were |
| 2272 | * closed on RST received, which allows us to respond with an |
| 2273 | * RST without breaking the connection (eg: to abort a transfer). |
| 2274 | * Some frames have to be silently ignored as well. |
| 2275 | */ |
| 2276 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
| 2277 | if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) { |
| 2278 | /* #5.1.1: The identifier of a newly |
| 2279 | * established stream MUST be numerically |
| 2280 | * greater than all streams that the initiating |
| 2281 | * endpoint has opened or reserved. This |
| 2282 | * governs streams that are opened using a |
| 2283 | * HEADERS frame and streams that are reserved |
| 2284 | * using PUSH_PROMISE. An endpoint that |
| 2285 | * receives an unexpected stream identifier |
| 2286 | * MUST respond with a connection error. |
| 2287 | */ |
| 2288 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| 2292 | if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) { |
| 2293 | /* RFC7540#5.1:closed: an endpoint that |
| 2294 | * receives any frame other than PRIORITY after |
| 2295 | * receiving a RST_STREAM MUST treat that as a |
| 2296 | * stream error of type STREAM_CLOSED. |
| 2297 | * |
| 2298 | * Note that old streams fall into this category |
| 2299 | * and will lead to an RST being sent. |
| 2300 | * |
| 2301 | * However, we cannot generalize this to all frame types. Those |
| 2302 | * carrying compression state must still be processed before |
| 2303 | * being dropped or we'll desynchronize the decoder. This can |
| 2304 | * happen with request trailers received after sending an |
| 2305 | * RST_STREAM, or with header/trailers responses received after |
| 2306 | * sending RST_STREAM (aborted stream). |
| 2307 | */ |
| 2308 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2309 | h2c->st0 = H2_CS_FRAME_E; |
| 2310 | return 0; |
| 2311 | } |
| 2312 | |
| 2313 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2314 | * result of sending a RST_STREAM frame, the peer that |
| 2315 | * receives the RST_STREAM might have already sent |
| 2316 | * frames on the stream that cannot be withdrawn. An |
| 2317 | * endpoint MUST ignore frames that it receives on |
| 2318 | * closed streams after it has sent a RST_STREAM |
| 2319 | * frame. An endpoint MAY choose to limit the period |
| 2320 | * over which it ignores frames and treat frames that |
| 2321 | * arrive after this time as being in error. |
| 2322 | */ |
| 2323 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
| 2324 | /* RFC7540#5.1:closed: any frame other than |
| 2325 | * PRIO/WU/RST in this state MUST be treated as |
| 2326 | * a connection error |
| 2327 | */ |
| 2328 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2329 | h2c->dft != H2_FT_PRIORITY && |
| 2330 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2331 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2332 | return 0; |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | return 1; |
| 2337 | } |
| 2338 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2339 | /* process Rx frames to be demultiplexed */ |
| 2340 | static void h2_process_demux(struct h2c *h2c) |
| 2341 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2342 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2343 | struct h2_fh hdr; |
| 2344 | unsigned int padlen = 0; |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2345 | int32_t old_iw = h2c->miw; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2346 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2347 | if (h2c->st0 >= H2_CS_ERROR) |
| 2348 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2349 | |
| 2350 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2351 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2352 | if (h2c->flags & H2_CF_IS_BACK) |
| 2353 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2354 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2355 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2356 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2357 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2358 | sess_log(h2c->conn->owner); |
| 2359 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2360 | goto fail; |
| 2361 | } |
| 2362 | |
| 2363 | h2c->max_id = 0; |
| 2364 | h2c->st0 = H2_CS_SETTINGS1; |
| 2365 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2366 | |
| 2367 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2368 | /* ensure that what is pending is a valid SETTINGS frame |
| 2369 | * without an ACK. |
| 2370 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2371 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2372 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2373 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2374 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2375 | sess_log(h2c->conn->owner); |
| 2376 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2377 | goto fail; |
| 2378 | } |
| 2379 | |
| 2380 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2381 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2382 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2383 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2384 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2385 | goto fail; |
| 2386 | } |
| 2387 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2388 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2389 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2390 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2391 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2392 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2393 | goto fail; |
| 2394 | } |
| 2395 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2396 | /* that's OK, switch to FRAME_P to process it. This is |
| 2397 | * a SETTINGS frame whose header has already been |
| 2398 | * deleted above. |
| 2399 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2400 | padlen = 0; |
| 2401 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2402 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2403 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2404 | |
| 2405 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2406 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2407 | int ret = 0; |
| 2408 | |
| 2409 | if (h2c->st0 >= H2_CS_ERROR) |
| 2410 | break; |
| 2411 | |
| 2412 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | 30d05f3 | 2019-08-06 15:49:51 +0200 | [diff] [blame] | 2413 | h2c->rcvd_s = 0; |
| 2414 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2415 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2416 | break; |
| 2417 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2418 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2419 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2420 | if (!h2c->nb_streams) { |
| 2421 | /* only log if no other stream can report the error */ |
| 2422 | sess_log(h2c->conn->owner); |
| 2423 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2424 | break; |
| 2425 | } |
| 2426 | |
Christopher Faulet | dd2a562 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2427 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2428 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2429 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2430 | * we read the pad length and drop it from the remaining |
| 2431 | * payload (one byte + the 9 remaining ones = 10 total |
| 2432 | * removed), so we have a frame payload starting after the |
| 2433 | * pad len. Flow controlled frames (DATA) also count the |
| 2434 | * padlen in the flow control, so it must be adjusted. |
| 2435 | */ |
| 2436 | if (hdr.len < 1) { |
| 2437 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2438 | sess_log(h2c->conn->owner); |
| 2439 | goto fail; |
| 2440 | } |
| 2441 | hdr.len--; |
| 2442 | |
| 2443 | if (b_data(&h2c->dbuf) < 10) |
| 2444 | break; // missing padlen |
| 2445 | |
| 2446 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2447 | |
| 2448 | if (padlen > hdr.len) { |
| 2449 | /* RFC7540#6.1 : pad length = length of |
| 2450 | * frame payload or greater => error. |
| 2451 | */ |
| 2452 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2453 | sess_log(h2c->conn->owner); |
| 2454 | goto fail; |
| 2455 | } |
| 2456 | |
| 2457 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2458 | h2c->rcvd_c++; |
| 2459 | h2c->rcvd_s++; |
| 2460 | } |
| 2461 | b_del(&h2c->dbuf, 1); |
| 2462 | } |
| 2463 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2464 | |
| 2465 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2466 | h2c->dfl = hdr.len; |
| 2467 | h2c->dsi = hdr.sid; |
| 2468 | h2c->dft = hdr.ft; |
| 2469 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2470 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2471 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2472 | |
| 2473 | /* check for minimum basic frame format validity */ |
| 2474 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2475 | if (ret != H2_ERR_NO_ERROR) { |
| 2476 | h2c_error(h2c, ret); |
| 2477 | sess_log(h2c->conn->owner); |
| 2478 | goto fail; |
| 2479 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2480 | } |
| 2481 | |
Willy Tarreau | 9fd5aa8 | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2482 | /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here. |
| 2483 | * H2_CS_FRAME_P indicates an incomplete previous operation |
| 2484 | * (most often the first attempt) and requires some validity |
| 2485 | * checks for the frame and the current state. The two other |
| 2486 | * ones are set after completion (or abortion) and must skip |
| 2487 | * validity checks. |
| 2488 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2489 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2490 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2491 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2492 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2493 | conn_xprt_read0_pending(h2c->conn) || |
| 2494 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2495 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2496 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2497 | /* we may have to signal the upper layers */ |
| 2498 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2499 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2500 | } |
| 2501 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2502 | |
Willy Tarreau | 6386481 | 2019-08-07 14:25:20 +0200 | [diff] [blame] | 2503 | if (h2c->st0 == H2_CS_FRAME_E || |
| 2504 | (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2505 | goto strm_err; |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2506 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2507 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2508 | case H2_FT_SETTINGS: |
| 2509 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2510 | ret = h2c_handle_settings(h2c); |
| 2511 | |
| 2512 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2513 | ret = h2c_ack_settings(h2c); |
| 2514 | break; |
| 2515 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2516 | case H2_FT_PING: |
| 2517 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2518 | ret = h2c_handle_ping(h2c); |
| 2519 | |
| 2520 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2521 | ret = h2c_ack_ping(h2c); |
| 2522 | break; |
| 2523 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2524 | case H2_FT_WINDOW_UPDATE: |
| 2525 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2526 | ret = h2c_handle_window_update(h2c, h2s); |
| 2527 | break; |
| 2528 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2529 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2530 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2531 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2532 | * frames' parsers consume all following CONTINUATION |
| 2533 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2534 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2535 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2536 | sess_log(h2c->conn->owner); |
| 2537 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2538 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2539 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2540 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2541 | if (h2c->flags & H2_CF_IS_BACK) |
| 2542 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2543 | else |
| 2544 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2545 | if (tmp_h2s) { |
| 2546 | h2s = tmp_h2s; |
| 2547 | ret = 1; |
| 2548 | } |
| 2549 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2550 | break; |
| 2551 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2552 | case H2_FT_DATA: |
| 2553 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2554 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2555 | |
| 2556 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2557 | ret = h2c_send_strm_wu(h2c); |
| 2558 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2559 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2560 | case H2_FT_PRIORITY: |
| 2561 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2562 | ret = h2c_handle_priority(h2c); |
| 2563 | break; |
| 2564 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2565 | case H2_FT_RST_STREAM: |
| 2566 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2567 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2568 | break; |
| 2569 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2570 | case H2_FT_GOAWAY: |
| 2571 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2572 | ret = h2c_handle_goaway(h2c); |
| 2573 | break; |
| 2574 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2575 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2576 | default: |
| 2577 | /* drop frames that we ignore. They may be larger than |
| 2578 | * the buffer so we drain all of their contents until |
| 2579 | * we reach the end. |
| 2580 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2581 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2582 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2583 | h2c->dfl -= ret; |
| 2584 | ret = h2c->dfl == 0; |
| 2585 | } |
| 2586 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2587 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2588 | /* We may have to send an RST if not done yet */ |
| 2589 | if (h2s->st == H2_SS_ERROR) |
| 2590 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2591 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2592 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2593 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2594 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2595 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2596 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2597 | break; |
| 2598 | |
| 2599 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2600 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2601 | h2c->st0 = H2_CS_FRAME_H; |
| 2602 | } |
| 2603 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2604 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2605 | if (h2c->rcvd_c > 0 && |
| 2606 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2607 | h2c_send_conn_wu(h2c); |
| 2608 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2609 | fail: |
| 2610 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2611 | if (h2s && h2s->cs && |
| 2612 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2613 | conn_xprt_read0_pending(h2c->conn) || |
| 2614 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2615 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2616 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2617 | /* we may have to signal the upper layers */ |
| 2618 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2619 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2620 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2621 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2622 | if (old_iw != h2c->miw) |
| 2623 | h2c_unblock_sfctl(h2c); |
| 2624 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2625 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2629 | * the end. |
| 2630 | */ |
| 2631 | static int h2_process_mux(struct h2c *h2c) |
| 2632 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2633 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2634 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2635 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2636 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2637 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2638 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2639 | if (h2c->st0 == H2_CS_ERROR) { |
| 2640 | h2c->st0 = H2_CS_ERROR2; |
| 2641 | sess_log(h2c->conn->owner); |
| 2642 | } |
| 2643 | goto fail; |
| 2644 | } |
| 2645 | h2c->st0 = H2_CS_SETTINGS1; |
| 2646 | } |
| 2647 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2648 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2649 | return 1; |
| 2650 | } |
| 2651 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2652 | /* start by sending possibly pending window updates */ |
Willy Tarreau | e74679a | 2019-08-06 15:39:32 +0200 | [diff] [blame] | 2653 | if (h2c->rcvd_s > 0 && |
| 2654 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2655 | h2c_send_strm_wu(h2c) < 0) |
| 2656 | goto fail; |
| 2657 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2658 | if (h2c->rcvd_c > 0 && |
| 2659 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2660 | h2c_send_conn_wu(h2c) < 0) |
| 2661 | goto fail; |
| 2662 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2663 | /* First we always process the flow control list because the streams |
| 2664 | * waiting there were already elected for immediate emission but were |
| 2665 | * blocked just on this. |
| 2666 | */ |
| 2667 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2668 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2669 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2670 | h2c->st0 >= H2_CS_ERROR) |
| 2671 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2672 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2673 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2674 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2675 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2676 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2677 | /* For some reason, the upper layer failed to subsribe again, |
| 2678 | * so remove it from the send_list |
| 2679 | */ |
| 2680 | if (!h2s->send_wait) { |
| 2681 | LIST_DEL_INIT(&h2s->list); |
| 2682 | continue; |
| 2683 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2684 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2685 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2686 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2687 | } |
| 2688 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2689 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2690 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2691 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2692 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2693 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2694 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2695 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2696 | /* For some reason, the upper layer failed to subsribe again, |
| 2697 | * so remove it from the send_list |
| 2698 | */ |
| 2699 | if (!h2s->send_wait) { |
| 2700 | LIST_DEL_INIT(&h2s->list); |
| 2701 | continue; |
| 2702 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2703 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2704 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2705 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2706 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2707 | } |
| 2708 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2709 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2710 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2711 | if (h2c->st0 == H2_CS_ERROR) { |
| 2712 | if (h2c->max_id >= 0) { |
| 2713 | h2c_send_goaway_error(h2c, NULL); |
| 2714 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2715 | return 0; |
| 2716 | } |
| 2717 | |
| 2718 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2719 | } |
| 2720 | return 1; |
| 2721 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2722 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2726 | /* Attempt to read data, and subscribe if none available. |
| 2727 | * The function returns 1 if data has been received, otherwise zero. |
| 2728 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2729 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2730 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2731 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2732 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2733 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2734 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2735 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2736 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2737 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2738 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2739 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2740 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2741 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2742 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2743 | if (!buf) { |
| 2744 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2745 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2746 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2747 | |
Willy Tarreau | 4d7a884 | 2019-07-31 16:00:48 +0200 | [diff] [blame] | 2748 | b_realign_if_empty(buf); |
| 2749 | if (!b_data(buf)) { |
| 2750 | /* try to pre-align the buffer like the |
| 2751 | * rxbufs will be to optimize memory copies. We'll make |
| 2752 | * sure that the frame header lands at the end of the |
| 2753 | * HTX block to alias it upon recv. We cannot use the |
| 2754 | * head because rcv_buf() will realign the buffer if |
| 2755 | * it's empty. Thus we cheat and pretend we already |
| 2756 | * have a few bytes there. |
| 2757 | */ |
| 2758 | max = buf_room_for_htx_data(buf) + 9; |
| 2759 | buf->head = sizeof(struct htx) - 9; |
| 2760 | } |
| 2761 | else |
| 2762 | max = b_room(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2763 | |
Willy Tarreau | 4d7a884 | 2019-07-31 16:00:48 +0200 | [diff] [blame] | 2764 | ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2765 | |
Willy Tarreau | 9bc1c95 | 2019-08-02 07:48:47 +0200 | [diff] [blame] | 2766 | if (max && !ret && h2_recv_allowed(h2c)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2767 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2768 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2769 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2770 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2771 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2774 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2775 | h2c->flags |= H2_CF_DEM_DFULL; |
Willy Tarreau | 4d7a884 | 2019-07-31 16:00:48 +0200 | [diff] [blame] | 2776 | return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2779 | /* Try to send data if possible. |
| 2780 | * The function returns 1 if data have been sent, otherwise zero. |
| 2781 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2782 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2783 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2784 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2785 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2786 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2787 | |
| 2788 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2789 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2790 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2791 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2792 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2793 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2794 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2797 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2798 | * pending streams into the existing buffer until it's reportedly full |
| 2799 | * or the end of send requests is reached. Then it tries to send this |
| 2800 | * buffer's contents out, marks it not full if at least one byte could |
| 2801 | * be sent, and tries again. |
| 2802 | * |
| 2803 | * The snd_buf() function normally takes a "flags" argument which may |
| 2804 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2805 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2806 | * connection is streaming lots of data (used to increase TLS record |
| 2807 | * size at the expense of latency). The former can be sent any time |
| 2808 | * there's a buffer full flag, as it indicates at least one stream |
| 2809 | * attempted to send and failed so there are pending data. An |
| 2810 | * alternative would be to set it as long as there's an active stream |
| 2811 | * but that would be problematic for ACKs until we have an absolute |
| 2812 | * guarantee that all waiters have at least one byte to send. The |
| 2813 | * latter should possibly not be set for now. |
| 2814 | */ |
| 2815 | |
| 2816 | done = 0; |
| 2817 | while (!done) { |
| 2818 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2819 | unsigned int released = 0; |
| 2820 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2821 | |
| 2822 | /* fill as much as we can into the current buffer */ |
| 2823 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2824 | done = h2_process_mux(h2c); |
| 2825 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2826 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2827 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2828 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2829 | if (conn->flags & CO_FL_ERROR) |
| 2830 | break; |
| 2831 | |
| 2832 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2833 | flags |= CO_SFL_MSG_MORE; |
| 2834 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2835 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2836 | if (b_data(buf)) { |
| 2837 | int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2838 | if (!ret) { |
| 2839 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2840 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2841 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2842 | sent = 1; |
| 2843 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2844 | if (b_data(buf)) { |
| 2845 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2846 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2847 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2848 | } |
| 2849 | b_free(buf); |
| 2850 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2851 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2852 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2853 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2854 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2855 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2856 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2857 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2858 | } |
| 2859 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2860 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2861 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2862 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2863 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2864 | /* We're not full anymore, so we can wake any task that are waiting |
| 2865 | * for us. |
| 2866 | */ |
| 2867 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2868 | struct h2s *h2s; |
| 2869 | |
| 2870 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2871 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2872 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2873 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2874 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2875 | continue; |
| 2876 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2877 | /* For some reason, the upper layer failed to subsribe again, |
| 2878 | * so remove it from the send_list |
| 2879 | */ |
| 2880 | if (!h2s->send_wait) { |
| 2881 | LIST_DEL_INIT(&h2s->list); |
| 2882 | continue; |
| 2883 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2884 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2885 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2886 | tasklet_wakeup(h2s->send_wait->tasklet); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2887 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2888 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2889 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2890 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2891 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2892 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2893 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2894 | if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2895 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2896 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2897 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2898 | } |
| 2899 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2900 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2901 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2902 | { |
| 2903 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2904 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2905 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2906 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2907 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2908 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2909 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2910 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2911 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2912 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2913 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2914 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2915 | /* callback called on any event by the connection handler. |
| 2916 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2917 | * destruction of the connection (which normally doesn not happen in h2). |
| 2918 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2919 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2920 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2921 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2922 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2923 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2924 | h2_process_demux(h2c); |
| 2925 | |
| 2926 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2927 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2929 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2930 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2931 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2932 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2933 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2934 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2935 | /* frontend is stopping, reload likely in progress, let's try |
| 2936 | * to announce a graceful shutdown if not yet done. We don't |
| 2937 | * care if it fails, it will be tried again later. |
| 2938 | */ |
| 2939 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2940 | if (h2c->last_sid < 0) |
| 2941 | h2c->last_sid = (1U << 31) - 1; |
| 2942 | h2c_send_goaway_error(h2c, NULL); |
| 2943 | } |
| 2944 | } |
| 2945 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2946 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2947 | * If we received early data, and the handshake is done, wake |
| 2948 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2949 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2950 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2951 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2952 | struct eb32_node *node; |
| 2953 | struct h2s *h2s; |
| 2954 | |
| 2955 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2956 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2957 | |
| 2958 | while (node) { |
| 2959 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2960 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2961 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2962 | node = eb32_next(node); |
| 2963 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2964 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2965 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2966 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2967 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2968 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2969 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 2970 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2971 | |
| 2972 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2973 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2974 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2975 | return -1; |
| 2976 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2979 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2980 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2981 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2982 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2983 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2984 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2985 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2986 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2987 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 2988 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2989 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2990 | if (h2c->task) { |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 2991 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 2992 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2993 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2994 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2995 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2996 | return 0; |
| 2997 | } |
| 2998 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2999 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3000 | static int h2_wake(struct connection *conn) |
| 3001 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3002 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3003 | |
| 3004 | return (h2_process(h2c)); |
| 3005 | } |
| 3006 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3007 | /* Connection timeout management. The principle is that if there's no receipt |
| 3008 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3009 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3010 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3011 | * GOAWAY frame. |
| 3012 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3013 | 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] | 3014 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3015 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3016 | int expired = tick_is_expired(t->expire, now_ms); |
| 3017 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3018 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3019 | return t; |
| 3020 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3021 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3022 | |
| 3023 | if (!h2c) { |
| 3024 | /* resources were already deleted */ |
| 3025 | return NULL; |
| 3026 | } |
| 3027 | |
| 3028 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3029 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3030 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3032 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3033 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3034 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3035 | } |
| 3036 | |
| 3037 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3038 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3039 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3040 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3041 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3042 | if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3043 | unsigned int released = 0; |
| 3044 | struct buffer *buf; |
| 3045 | |
| 3046 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3047 | if (b_data(buf)) { |
| 3048 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3049 | if (!ret) |
| 3050 | break; |
| 3051 | b_del(buf, ret); |
| 3052 | if (b_data(buf)) |
| 3053 | break; |
| 3054 | b_free(buf); |
| 3055 | released++; |
| 3056 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3057 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3058 | |
| 3059 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3060 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3061 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3062 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3063 | /* either we can release everything now or it will be done later once |
| 3064 | * the last stream closes. |
| 3065 | */ |
| 3066 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3067 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3068 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3069 | return NULL; |
| 3070 | } |
| 3071 | |
| 3072 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3073 | /*******************************************/ |
| 3074 | /* functions below are used by the streams */ |
| 3075 | /*******************************************/ |
| 3076 | |
| 3077 | /* |
| 3078 | * Attach a new stream to a connection |
| 3079 | * (Used for outgoing connections) |
| 3080 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3081 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3082 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3083 | struct conn_stream *cs; |
| 3084 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3085 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3086 | |
| 3087 | cs = cs_new(conn); |
| 3088 | if (!cs) |
| 3089 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3090 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3091 | if (!h2s) { |
| 3092 | cs_free(cs); |
| 3093 | return NULL; |
| 3094 | } |
| 3095 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3098 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3099 | * We have to scan because we may have some orphan streams. It might be |
| 3100 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3101 | * orphans. |
| 3102 | */ |
| 3103 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3104 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3105 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3106 | struct h2s *h2s; |
| 3107 | struct eb32_node *node; |
| 3108 | |
| 3109 | node = eb32_first(&h2c->streams_by_id); |
| 3110 | while (node) { |
| 3111 | h2s = container_of(node, struct h2s, by_id); |
| 3112 | if (h2s->cs) |
| 3113 | return h2s->cs; |
| 3114 | node = eb32_next(node); |
| 3115 | } |
| 3116 | return NULL; |
| 3117 | } |
| 3118 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3119 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3120 | * Destroy the mux and the associated connection, if it is no longer used |
| 3121 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3122 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3123 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3124 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3125 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3126 | if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3127 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3131 | * Detach the stream from the connection and possibly release the connection. |
| 3132 | */ |
| 3133 | static void h2_detach(struct conn_stream *cs) |
| 3134 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3135 | struct h2s *h2s = cs->ctx; |
| 3136 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3137 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3138 | |
| 3139 | cs->ctx = NULL; |
| 3140 | if (!h2s) |
| 3141 | return; |
| 3142 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3143 | /* The stream is about to die, so no need to attempt to run its task */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3144 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3145 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3146 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3147 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3148 | /* |
| 3149 | * At this point, the stream_interface is supposed to have called |
| 3150 | * h2_unsubscribe(), so the only way there's still a |
| 3151 | * subscription that came from the stream_interface (as we |
| 3152 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3153 | * without the stream_interface involved) is that we subscribed |
| 3154 | * for sending, we woke the tasklet up and removed the |
| 3155 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3156 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3157 | * run yet. Make sure to handle that by explicitely setting |
| 3158 | * send_wait to NULL, as nothing else will do it for us. |
| 3159 | */ |
| 3160 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3161 | } |
| 3162 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3163 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3164 | h2c = h2s->h2c; |
| 3165 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3166 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3167 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3168 | !h2_frt_has_too_many_cs(h2c)) { |
| 3169 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3170 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3171 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3172 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3173 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3174 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3175 | * an ES or RST frame), so orphan it in this case. |
| 3176 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3177 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3178 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3179 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait)) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3180 | return; |
| 3181 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3182 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3183 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3184 | /* unblock the connection if it was blocked on this |
| 3185 | * stream. |
| 3186 | */ |
| 3187 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3188 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3189 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3190 | } |
| 3191 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3192 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3193 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3194 | if (h2c->flags & H2_CF_IS_BACK) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3195 | if (!(h2c->conn->flags & |
| 3196 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3197 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3198 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3199 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3200 | h2c->conn->owner = NULL; |
| 3201 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3202 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3203 | /* The server doesn't want it, let's kill the connection right away */ |
| 3204 | h2c->conn->mux->destroy(h2c->conn); |
| 3205 | return; |
| 3206 | } |
| 3207 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3208 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3209 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3210 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3211 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3212 | return; |
| 3213 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3214 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3215 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3216 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3217 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3218 | struct server *srv = objt_server(h2c->conn->target); |
| 3219 | |
| 3220 | if (srv) { |
| 3221 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3222 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3223 | else |
| 3224 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3225 | } |
| 3226 | |
| 3227 | } |
| 3228 | } |
| 3229 | } |
| 3230 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3231 | /* We don't want to close right now unless we're removing the |
| 3232 | * last stream, and either the connection is in error, or it |
| 3233 | * reached the ID already specified in a GOAWAY frame received |
| 3234 | * or sent (as seen by last_sid >= 0). |
| 3235 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3236 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3237 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3238 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3239 | } |
| 3240 | else if (h2c->task) { |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3241 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3242 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3243 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3244 | } |
| 3245 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3246 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3247 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3248 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3249 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3250 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3251 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3252 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3253 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3254 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3255 | /* a connstream may require us to immediately kill the whole connection |
| 3256 | * for example because of a "tcp-request content reject" rule that is |
| 3257 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3258 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3259 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3260 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3261 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3262 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3263 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3264 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3265 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3266 | /* Nothing was never sent for this stream, so reset with |
| 3267 | * REFUSED_STREAM error to let the client retry the |
| 3268 | * request. |
| 3269 | */ |
| 3270 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3271 | } |
Willy Tarreau | cfba9d6 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3272 | else { |
| 3273 | /* a final response was already provided, we don't want this |
| 3274 | * stream anymore. This may happen when the server responds |
| 3275 | * before the end of an upload and closes quickly (redirect, |
| 3276 | * deny, ...) |
| 3277 | */ |
| 3278 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3279 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3280 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3281 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3282 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3283 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3284 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3285 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3286 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3287 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3288 | done: |
| 3289 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3290 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3291 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3292 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3293 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3294 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3295 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3296 | h2s->send_wait = sw; |
| 3297 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3298 | h2s->send_wait = sw; |
| 3299 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3300 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3301 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3302 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3303 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3304 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3305 | } |
| 3306 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3307 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3308 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3309 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3310 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3311 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3312 | |
Willy Tarreau | cfba9d6 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3313 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3314 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3315 | |
Willy Tarreau | cfba9d6 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3316 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3317 | /* we can cleanly close using an empty data frame only after headers */ |
| 3318 | |
| 3319 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3320 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3321 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3322 | |
| 3323 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3324 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3325 | else |
| 3326 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3327 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3328 | /* a connstream may require us to immediately kill the whole connection |
| 3329 | * for example because of a "tcp-request content reject" rule that is |
| 3330 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3331 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3332 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3333 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3334 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3335 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3336 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3337 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3338 | else { |
| 3339 | /* Nothing was never sent for this stream, so reset with |
| 3340 | * REFUSED_STREAM error to let the client retry the |
| 3341 | * request. |
| 3342 | */ |
| 3343 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3344 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3345 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3346 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3347 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3348 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3349 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3350 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3351 | } |
| 3352 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3353 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3354 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3355 | done: |
| 3356 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3357 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3358 | |
| 3359 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3360 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3361 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3362 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3363 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3364 | h2s->send_wait = sw; |
| 3365 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3366 | h2s->send_wait = sw; |
| 3367 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3368 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3369 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3370 | /* let the handler know we want to shutw */ |
| 3371 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3372 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3373 | } |
| 3374 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3375 | /* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3376 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3377 | * and prevented the last frame from being emitted. |
| 3378 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3379 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3380 | { |
| 3381 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3382 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3383 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3384 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3385 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3386 | h2_do_shutw(h2s); |
| 3387 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3388 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3389 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3390 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3391 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3392 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3393 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3394 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3395 | if (!h2s->cs) { |
| 3396 | h2s_destroy(h2s); |
| 3397 | if (h2c_is_dead(h2c)) |
| 3398 | h2_release(h2c); |
| 3399 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3400 | } |
| 3401 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3402 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3403 | } |
| 3404 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3405 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3406 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3407 | { |
| 3408 | struct h2s *h2s = cs->ctx; |
| 3409 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3410 | if (cs->flags & CS_FL_KILL_CONN) |
| 3411 | h2s->flags |= H2_SF_KILL_CONN; |
| 3412 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3413 | if (!mode) |
| 3414 | return; |
| 3415 | |
| 3416 | h2_do_shutr(h2s); |
| 3417 | } |
| 3418 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3419 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3420 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3421 | { |
| 3422 | struct h2s *h2s = cs->ctx; |
| 3423 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3424 | if (cs->flags & CS_FL_KILL_CONN) |
| 3425 | h2s->flags |= H2_SF_KILL_CONN; |
| 3426 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3427 | h2_do_shutw(h2s); |
| 3428 | } |
| 3429 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3430 | /* Decode the payload of a HEADERS frame and produce the HTX request or response |
| 3431 | * depending on the connection's side. Returns a positive value on success, a |
| 3432 | * negative value on failure, or 0 if it couldn't proceed. May report connection |
| 3433 | * errors in h2c->errcode if the frame is non-decodable and the connection |
| 3434 | * unrecoverable. In absence of connection error when a failure is reported, the |
| 3435 | * caller must assume a stream error. |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3436 | * |
| 3437 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3438 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3439 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3440 | * leaving a hole between the main frame and the beginning of the next one. |
| 3441 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3442 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3443 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3444 | * |
| 3445 | * A buffer at the beginning of processing may look like this : |
| 3446 | * |
| 3447 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3448 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3449 | * `---^---------^-----^--------------^--------------^------^---' |
| 3450 | * | | <-----> | | |
| 3451 | * area | dpl | wrap |
| 3452 | * |<--------------> | |
| 3453 | * | dfl | |
| 3454 | * |<-------------------------------------------------->| |
| 3455 | * head data |
| 3456 | * |
| 3457 | * Padding is automatically overwritten when folding, participating to the |
| 3458 | * hole size after dfl : |
| 3459 | * |
| 3460 | * ,---.------------------------.-----.--------------.------.---. |
| 3461 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3462 | * `---^------------------------^-----^--------------^------^---' |
| 3463 | * | | <-----> | | |
| 3464 | * area | hole | wrap |
| 3465 | * |<-----------------------> | |
| 3466 | * | dfl | |
| 3467 | * |<-------------------------------------------------->| |
| 3468 | * head data |
| 3469 | * |
| 3470 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3471 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3472 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3473 | * |
| 3474 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3475 | * so that the function can update the following flags : |
| 3476 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3477 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3478 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3479 | * |
| 3480 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3481 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3482 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3483 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3484 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3485 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3486 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3487 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3488 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3489 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3490 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3491 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3492 | int flen; // header frame len |
| 3493 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3494 | int ret = 0; |
| 3495 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3496 | int wrap; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3497 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3498 | next_frame: |
| 3499 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3500 | goto leave; // incomplete input frame |
| 3501 | |
| 3502 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3503 | * this case, we'll try to paste it immediately after the initial |
| 3504 | * HEADERS frame payload and kill any possible padding. The initial |
| 3505 | * frame's length will be increased to represent the concatenation |
| 3506 | * of the two frames. The next frame is read from position <tlen> |
| 3507 | * and written at position <flen> (minus padding if some is present). |
| 3508 | */ |
| 3509 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3510 | struct h2_fh hdr; |
| 3511 | int clen; // CONTINUATION frame's payload length |
| 3512 | |
| 3513 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3514 | /* no more data, the buffer may be full, either due to |
| 3515 | * too large a frame or because of too large a hole that |
| 3516 | * we're going to compact at the end. |
| 3517 | */ |
| 3518 | goto leave; |
| 3519 | } |
| 3520 | |
| 3521 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3522 | /* RFC7540#6.10: frame of unexpected type */ |
| 3523 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3524 | goto fail; |
| 3525 | } |
| 3526 | |
| 3527 | if (hdr.sid != h2c->dsi) { |
| 3528 | /* RFC7540#6.10: frame of different stream */ |
| 3529 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3530 | goto fail; |
| 3531 | } |
| 3532 | |
| 3533 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3534 | /* RFC7540#4.2: invalid frame length */ |
| 3535 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3536 | goto fail; |
| 3537 | } |
| 3538 | |
| 3539 | /* detect when we must stop aggragating frames */ |
| 3540 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3541 | |
| 3542 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3543 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3544 | if (clen > hdr.len) |
| 3545 | clen = hdr.len; |
| 3546 | |
| 3547 | /* Move the frame's payload over the padding, hole and frame |
| 3548 | * header. At least one of hole or dpl is null (see diagrams |
| 3549 | * above). The hole moves after the new aggragated frame. |
| 3550 | */ |
| 3551 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3552 | h2c->dfl += clen - h2c->dpl; |
| 3553 | hole += h2c->dpl + 9; |
| 3554 | h2c->dpl = 0; |
| 3555 | goto next_frame; |
| 3556 | } |
| 3557 | |
| 3558 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3559 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3560 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3561 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3562 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3563 | copy = alloc_trash_chunk(); |
| 3564 | if (!copy) { |
| 3565 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3566 | goto fail; |
| 3567 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3568 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3569 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3570 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3571 | } |
| 3572 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3573 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3574 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3575 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3576 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3577 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3578 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3579 | } |
| 3580 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3581 | if (flen < 5) { |
| 3582 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3583 | goto fail; |
| 3584 | } |
| 3585 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3586 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3587 | flen -= 5; |
| 3588 | } |
| 3589 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3590 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3591 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3592 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3593 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3594 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3595 | /* we can't retry a failed decompression operation so we must be very |
| 3596 | * careful not to take any risks. In practice the output buffer is |
| 3597 | * always empty except maybe for trailers, in which case we simply have |
| 3598 | * to wait for the upper layer to finish consuming what is available. |
| 3599 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3600 | htx = htx_from_buf(rxbuf); |
| 3601 | if (!htx_is_empty(htx)) { |
| 3602 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3603 | goto leave; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3604 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3605 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3606 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3607 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3608 | sizeof(list)/sizeof(list[0]), tmp); |
| 3609 | if (outlen < 0) { |
| 3610 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3611 | goto fail; |
| 3612 | } |
| 3613 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3614 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3615 | * the parser's state to commit these changes and allow us to later |
| 3616 | * fail solely on the stream if needed. |
| 3617 | */ |
| 3618 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3619 | h2c->dfl = hole = 0; |
| 3620 | h2c->st0 = H2_CS_FRAME_H; |
| 3621 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3622 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3623 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3625 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3626 | goto trailers; |
| 3627 | |
| 3628 | /* This is the first HEADERS frame so it's a headers block */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3629 | if (h2c->flags & H2_CF_IS_BACK) |
| 3630 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
| 3631 | else |
| 3632 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3633 | |
| 3634 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3635 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3636 | goto fail; |
| 3637 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3638 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3639 | if (msgf & H2_MSGF_BODY) { |
| 3640 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3641 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3642 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3643 | htx->extra = *body_len; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3644 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3645 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3646 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3647 | } |
| 3648 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3649 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3650 | /* indicate that a HEADERS frame was received for this stream, except |
| 3651 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3652 | * expected. |
| 3653 | */ |
| 3654 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3655 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3656 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3657 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3658 | /* Mark the end of message using EOM */ |
| 3659 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3660 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3661 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3662 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3663 | /* success */ |
| 3664 | ret = 1; |
| 3665 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3666 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3667 | /* If there is a hole left and it's not at the end, we are forced to |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3668 | * move the remaining data over it. |
| 3669 | */ |
| 3670 | if (hole) { |
| 3671 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3672 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3673 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3674 | b_sub(&h2c->dbuf, hole); |
| 3675 | } |
| 3676 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3677 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3678 | /* too large frames */ |
| 3679 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3680 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3683 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3684 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3685 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3686 | return ret; |
| 3687 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3688 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3689 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3690 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3691 | |
| 3692 | trailers: |
| 3693 | /* This is the last HEADERS frame hence a trailer */ |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3694 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3695 | /* It's a trailer but it's missing ES flag */ |
| 3696 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3697 | goto fail; |
| 3698 | } |
| 3699 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3700 | /* Trailers terminate a DATA sequence */ |
| 3701 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3702 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3703 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3704 | } |
| 3705 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3706 | /* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3707 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3708 | * send the current frame, 0 if it couldn't complete, in which case |
| 3709 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3710 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3711 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3712 | * have checked the frame header and ensured that the frame was complete or the |
| 3713 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3714 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3715 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3716 | { |
| 3717 | struct h2c *h2c = h2s->h2c; |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3718 | int block; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3719 | unsigned int flen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3720 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3721 | struct buffer *csbuf; |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3722 | unsigned int sent; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3723 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3724 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3725 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3726 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3727 | if (!csbuf) { |
| 3728 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3729 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3730 | } |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3731 | htx = htx_from_buf(csbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3732 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3733 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3734 | flen = h2c->dfl - h2c->dpl; |
| 3735 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3736 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3737 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3738 | if (flen > b_data(&h2c->dbuf)) { |
| 3739 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3740 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3741 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3742 | } |
| 3743 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3744 | block = htx_free_data_space(htx); |
| 3745 | if (!block) { |
| 3746 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3747 | goto fail; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3748 | } |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3749 | if (flen > block) |
| 3750 | flen = block; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3751 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3752 | /* here, flen is the max we can copy into the output buffer */ |
| 3753 | block = b_contig_data(&h2c->dbuf, 0); |
| 3754 | if (flen > block) |
| 3755 | flen = block; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3756 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3757 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3758 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3759 | b_del(&h2c->dbuf, sent); |
| 3760 | h2c->dfl -= sent; |
| 3761 | h2c->rcvd_c += sent; |
| 3762 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3763 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3764 | if (h2s->flags & H2_SF_DATA_CLEN) { |
| 3765 | h2s->body_len -= sent; |
| 3766 | htx->extra = h2s->body_len; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3769 | if (sent < flen) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3770 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3771 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3772 | } |
| 3773 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3774 | goto try_again; |
| 3775 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3776 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3777 | /* here we're done with the frame, all the payload (except padding) was |
| 3778 | * transferred. |
| 3779 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3780 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3781 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3782 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3783 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3784 | goto fail; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3785 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3786 | } |
| 3787 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3788 | h2c->rcvd_c += h2c->dpl; |
| 3789 | h2c->rcvd_s += h2c->dpl; |
| 3790 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3791 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3792 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3793 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3794 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3795 | if (htx) |
| 3796 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3797 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3798 | } |
| 3799 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3800 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 3801 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 3802 | * must check the stream's status to detect any error which might have happened |
| 3803 | * subsequently to a successful send. The htx blocks are automatically removed |
| 3804 | * from the message. The htx message is assumed to be valid since produced from |
| 3805 | * the internal code, hence it contains a start line, an optional series of |
| 3806 | * header blocks and an end of header, otherwise an invalid frame could be |
| 3807 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 3808 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3809 | static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3810 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3811 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3812 | struct h2c *h2c = h2s->h2c; |
| 3813 | struct htx_blk *blk; |
| 3814 | struct htx_blk *blk_end; |
| 3815 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3816 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3817 | struct htx_sl *sl; |
| 3818 | enum htx_blk_type type; |
| 3819 | int es_now = 0; |
| 3820 | int ret = 0; |
| 3821 | int hdr; |
| 3822 | int idx; |
| 3823 | |
| 3824 | if (h2c_mux_busy(h2c, h2s)) { |
| 3825 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3826 | return 0; |
| 3827 | } |
| 3828 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3829 | /* determine the first block which must not be deleted, blk_end may |
| 3830 | * be NULL if all blocks have to be deleted. |
| 3831 | */ |
| 3832 | idx = htx_get_head(htx); |
| 3833 | blk_end = NULL; |
| 3834 | while (idx != -1) { |
| 3835 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 3836 | idx = htx_get_next(htx, idx); |
| 3837 | if (type == HTX_BLK_EOH) { |
| 3838 | if (idx != -1) |
| 3839 | blk_end = htx_get_blk(htx, idx); |
| 3840 | break; |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 3845 | blk = htx_get_head_blk(htx); |
| 3846 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
| 3847 | ALREADY_CHECKED(blk); |
| 3848 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3849 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3850 | if (h2s->status < 100 || h2s->status > 999) |
| 3851 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3852 | |
| 3853 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 3854 | hdr = 0; |
| 3855 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 3856 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3857 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 3858 | blk = htx_get_blk(htx, idx); |
| 3859 | type = htx_get_blk_type(blk); |
| 3860 | |
| 3861 | if (type == HTX_BLK_UNUSED) |
| 3862 | continue; |
| 3863 | |
| 3864 | if (type != HTX_BLK_HDR) |
| 3865 | break; |
| 3866 | |
| 3867 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 3868 | goto fail; |
| 3869 | |
| 3870 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 3871 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3872 | hdr++; |
| 3873 | } |
| 3874 | |
| 3875 | /* marker for end of headers */ |
| 3876 | list[hdr].n = ist(""); |
| 3877 | |
| 3878 | if (h2s->status == 204 || h2s->status == 304) { |
| 3879 | /* no contents, claim c-len is present and set to zero */ |
| 3880 | es_now = 1; |
| 3881 | } |
| 3882 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 3883 | mbuf = br_tail(h2c->mbuf); |
| 3884 | retry: |
| 3885 | if (!h2_get_buf(h2c, mbuf)) { |
| 3886 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3887 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3888 | return 0; |
| 3889 | } |
| 3890 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3891 | chunk_reset(&outbuf); |
| 3892 | |
| 3893 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3894 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 3895 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3896 | break; |
| 3897 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3898 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | if (outbuf.size < 9) |
| 3902 | goto full; |
| 3903 | |
| 3904 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 3905 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3906 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3907 | outbuf.data = 9; |
| 3908 | |
| 3909 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3910 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3911 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3912 | goto realign_again; |
| 3913 | goto full; |
| 3914 | } |
| 3915 | |
| 3916 | /* encode all headers, stop at empty name */ |
| 3917 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 3918 | /* these ones do not exist in H2 and must be dropped. */ |
| 3919 | if (isteq(list[hdr].n, ist("connection")) || |
| 3920 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3921 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3922 | isteq(list[hdr].n, ist("upgrade")) || |
| 3923 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 3924 | continue; |
| 3925 | |
| 3926 | if (isteq(list[hdr].n, ist(""))) |
| 3927 | break; // end |
| 3928 | |
| 3929 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3930 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3931 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3932 | goto realign_again; |
| 3933 | goto full; |
| 3934 | } |
| 3935 | } |
| 3936 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3937 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3938 | * FIXME: we should also set it when we know for sure that the |
| 3939 | * content-length is zero as well as on 204/304 |
| 3940 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3941 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 3942 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3943 | es_now = 1; |
| 3944 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3945 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3946 | es_now = 1; |
| 3947 | |
| 3948 | /* update the frame's size */ |
| 3949 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 3950 | |
| 3951 | if (es_now) |
| 3952 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 3953 | |
| 3954 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3955 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3956 | |
| 3957 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 3958 | * 1xx responses, another HEADERS frame is expected. |
| 3959 | */ |
| 3960 | if (h2s->status >= 200 || h2s->status == 101) |
| 3961 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3962 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3963 | if (es_now) { |
| 3964 | h2s->flags |= H2_SF_ES_SENT; |
| 3965 | if (h2s->st == H2_SS_OPEN) |
| 3966 | h2s->st = H2_SS_HLOC; |
| 3967 | else |
| 3968 | h2s_close(h2s); |
| 3969 | } |
| 3970 | |
| 3971 | /* OK we could properly deliver the response */ |
| 3972 | |
| 3973 | /* remove all header blocks including the EOH and compute the |
| 3974 | * corresponding size. |
| 3975 | * |
| 3976 | * FIXME: We should remove everything when es_now is set. |
| 3977 | */ |
| 3978 | ret = 0; |
| 3979 | idx = htx_get_head(htx); |
| 3980 | blk = htx_get_blk(htx, idx); |
| 3981 | while (blk != blk_end) { |
| 3982 | ret += htx_get_blksz(blk); |
| 3983 | blk = htx_remove_blk(htx, blk); |
| 3984 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 3985 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 3986 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 3987 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 3988 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 3989 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3990 | end: |
| 3991 | return ret; |
| 3992 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 3993 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 3994 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3995 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3996 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3997 | ret = 0; |
| 3998 | goto end; |
| 3999 | fail: |
| 4000 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4001 | * list etc go here (unrecoverable errors). |
| 4002 | */ |
| 4003 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4004 | ret = 0; |
| 4005 | goto end; |
| 4006 | } |
| 4007 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4008 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4009 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4010 | * must check the stream's status to detect any error which might have happened |
| 4011 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4012 | * from the message. The htx message is assumed to be valid since produced from |
| 4013 | * the internal code, hence it contains a start line, an optional series of |
| 4014 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4015 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4016 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4017 | static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4018 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4019 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4020 | struct h2c *h2c = h2s->h2c; |
| 4021 | struct htx_blk *blk; |
| 4022 | struct htx_blk *blk_end; |
| 4023 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4024 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4025 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4026 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4027 | enum htx_blk_type type; |
| 4028 | int es_now = 0; |
| 4029 | int ret = 0; |
| 4030 | int hdr; |
| 4031 | int idx; |
| 4032 | |
| 4033 | if (h2c_mux_busy(h2c, h2s)) { |
| 4034 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4035 | return 0; |
| 4036 | } |
| 4037 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4038 | /* determine the first block which must not be deleted, blk_end may |
| 4039 | * be NULL if all blocks have to be deleted. |
| 4040 | */ |
| 4041 | idx = htx_get_head(htx); |
| 4042 | blk_end = NULL; |
| 4043 | while (idx != -1) { |
| 4044 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4045 | idx = htx_get_next(htx, idx); |
| 4046 | if (type == HTX_BLK_EOH) { |
| 4047 | if (idx != -1) |
| 4048 | blk_end = htx_get_blk(htx, idx); |
| 4049 | break; |
| 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4054 | blk = htx_get_head_blk(htx); |
| 4055 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 4056 | ALREADY_CHECKED(blk); |
| 4057 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4058 | meth = htx_sl_req_meth(sl); |
| 4059 | path = htx_sl_req_uri(sl); |
| 4060 | |
| 4061 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4062 | hdr = 0; |
| 4063 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4064 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4065 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4066 | blk = htx_get_blk(htx, idx); |
| 4067 | type = htx_get_blk_type(blk); |
| 4068 | |
| 4069 | if (type == HTX_BLK_UNUSED) |
| 4070 | continue; |
| 4071 | |
| 4072 | if (type != HTX_BLK_HDR) |
| 4073 | break; |
| 4074 | |
| 4075 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4076 | goto fail; |
| 4077 | |
| 4078 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4079 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4080 | hdr++; |
| 4081 | } |
| 4082 | |
| 4083 | /* marker for end of headers */ |
| 4084 | list[hdr].n = ist(""); |
| 4085 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4086 | mbuf = br_tail(h2c->mbuf); |
| 4087 | retry: |
| 4088 | if (!h2_get_buf(h2c, mbuf)) { |
| 4089 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4090 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4091 | return 0; |
| 4092 | } |
| 4093 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4094 | chunk_reset(&outbuf); |
| 4095 | |
| 4096 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4097 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4098 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4099 | break; |
| 4100 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4101 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | if (outbuf.size < 9) |
| 4105 | goto full; |
| 4106 | |
| 4107 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4108 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4109 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4110 | outbuf.data = 9; |
| 4111 | |
| 4112 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4113 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4114 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4115 | goto realign_again; |
| 4116 | goto full; |
| 4117 | } |
| 4118 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4119 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4120 | * - :authority set to the URI part (host:port) |
| 4121 | * - :method set to CONNECT |
| 4122 | * - :scheme and :path omitted |
| 4123 | */ |
| 4124 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4125 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4126 | struct ist scheme; |
| 4127 | |
| 4128 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4129 | scheme = ist("http"); |
| 4130 | else |
| 4131 | scheme = ist("https"); |
| 4132 | |
| 4133 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4134 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4135 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4136 | goto realign_again; |
| 4137 | goto full; |
| 4138 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4139 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4140 | /* encode the path, which necessarily is the second one */ |
| 4141 | if (!hpack_encode_path(&outbuf, path)) { |
| 4142 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4143 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4144 | goto realign_again; |
| 4145 | goto full; |
| 4146 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4147 | |
| 4148 | /* look for the Host header and place it in :authority */ |
| 4149 | auth = ist2(NULL, 0); |
| 4150 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4151 | if (isteq(list[hdr].n, ist(""))) |
| 4152 | break; // end |
| 4153 | |
| 4154 | if (isteq(list[hdr].n, ist("host"))) { |
| 4155 | auth = list[hdr].v; |
| 4156 | break; |
| 4157 | } |
| 4158 | } |
| 4159 | } |
| 4160 | else { |
| 4161 | /* for CONNECT, :authority is taken from the path */ |
| 4162 | auth = path; |
| 4163 | } |
| 4164 | |
| 4165 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4166 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4167 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4168 | goto realign_again; |
| 4169 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | /* encode all headers, stop at empty name */ |
| 4173 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4174 | /* these ones do not exist in H2 and must be dropped. */ |
| 4175 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4176 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4177 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4178 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4179 | isteq(list[hdr].n, ist("upgrade")) || |
| 4180 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4181 | continue; |
| 4182 | |
| 4183 | if (isteq(list[hdr].n, ist(""))) |
| 4184 | break; // end |
| 4185 | |
| 4186 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4187 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4188 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4189 | goto realign_again; |
| 4190 | goto full; |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | /* we may need to add END_STREAM if we have no body : |
| 4195 | * - request already closed, or : |
| 4196 | * - no transfer-encoding, and : |
| 4197 | * - no content-length or content-length:0 |
| 4198 | * Fixme: this doesn't take into account CONNECT requests. |
| 4199 | */ |
| 4200 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4201 | es_now = 1; |
| 4202 | |
| 4203 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4204 | es_now = 1; |
| 4205 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4206 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4207 | es_now = 1; |
| 4208 | |
| 4209 | /* update the frame's size */ |
| 4210 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4211 | |
| 4212 | if (es_now) |
| 4213 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4214 | |
| 4215 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4216 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4217 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4218 | h2s->st = H2_SS_OPEN; |
| 4219 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4220 | if (es_now) { |
| 4221 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4222 | h2s->flags |= H2_SF_ES_SENT; |
| 4223 | h2s->st = H2_SS_HLOC; |
| 4224 | } |
| 4225 | |
| 4226 | /* remove all header blocks including the EOH and compute the |
| 4227 | * corresponding size. |
| 4228 | * |
| 4229 | * FIXME: We should remove everything when es_now is set. |
| 4230 | */ |
| 4231 | ret = 0; |
| 4232 | idx = htx_get_head(htx); |
| 4233 | blk = htx_get_blk(htx, idx); |
| 4234 | while (blk != blk_end) { |
| 4235 | ret += htx_get_blksz(blk); |
| 4236 | blk = htx_remove_blk(htx, blk); |
| 4237 | } |
| 4238 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4239 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4240 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4241 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4242 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4243 | |
| 4244 | end: |
| 4245 | return ret; |
| 4246 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4247 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4248 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4249 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4250 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4251 | ret = 0; |
| 4252 | goto end; |
| 4253 | fail: |
| 4254 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4255 | * list etc go here (unrecoverable errors). |
| 4256 | */ |
| 4257 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4258 | ret = 0; |
| 4259 | goto end; |
| 4260 | } |
| 4261 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4262 | /* Try to send a DATA frame matching HTTP response present in HTX structure |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4263 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4264 | * caller must check the stream's status to detect any error which might have |
| 4265 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4266 | * consumed, or zero if nothing done. Note that EOM count for 1 byte. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4267 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4268 | static size_t h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4269 | { |
| 4270 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4271 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4272 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4273 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4274 | size_t total = 0; |
| 4275 | int es_now = 0; |
| 4276 | int bsize; /* htx block size */ |
| 4277 | int fsize; /* h2 frame size */ |
| 4278 | struct htx_blk *blk; |
| 4279 | enum htx_blk_type type; |
| 4280 | int idx; |
| 4281 | |
| 4282 | if (h2c_mux_busy(h2c, h2s)) { |
| 4283 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4284 | goto end; |
| 4285 | } |
| 4286 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4287 | htx = htx_from_buf(buf); |
| 4288 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4289 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 4290 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 4291 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4292 | */ |
| 4293 | |
| 4294 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4295 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4296 | goto end; |
| 4297 | |
| 4298 | idx = htx_get_head(htx); |
| 4299 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4300 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4301 | bsize = htx_get_blksz(blk); |
| 4302 | fsize = bsize; |
| 4303 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4304 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4305 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4306 | /* ES already sent */ |
| 4307 | htx_remove_blk(htx, blk); |
| 4308 | total++; // EOM counts as one byte |
| 4309 | count--; |
| 4310 | goto end; |
| 4311 | } |
| 4312 | } |
| 4313 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4314 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4315 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4316 | mbuf = br_tail(h2c->mbuf); |
| 4317 | retry: |
| 4318 | if (!h2_get_buf(h2c, mbuf)) { |
| 4319 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4320 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4321 | goto end; |
| 4322 | } |
| 4323 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4324 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4325 | * First, if the mux's buffer is empty and the htx area contains |
| 4326 | * exactly one data block of the same size as the requested count, and |
| 4327 | * this count fits within the frame size, the stream's window size, and |
| 4328 | * the connection's window size, then it's possible to simply swap the |
| 4329 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4330 | * length to match the entire DATA HTX block in the middle. In this |
| 4331 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4332 | * the situation that happens all the time with large files. Second, if |
| 4333 | * this is not possible, but the mux's output buffer is empty, we still |
| 4334 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4335 | * making the intermediary buffer's area point to the output buffer's |
| 4336 | * area. In this case we want to skip the HTX header to make sure that |
| 4337 | * copies remain aligned and that this operation remains possible all |
| 4338 | * the time. This goes for headers, data blocks and any data extracted |
| 4339 | * from the HTX blocks. |
| 4340 | */ |
| 4341 | if (unlikely(fsize == count && |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 4342 | htx_nbblks(htx) == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4343 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4344 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4345 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4346 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4347 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4348 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4349 | * frame into a buffer containing few data if it needs to be realigned, |
| 4350 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4351 | * we'll pretend the mbuf is full and wait for it to become empty. |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4352 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4353 | if (fsize + 9 <= b_room(mbuf) && |
| 4354 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 4355 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4356 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4357 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4358 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4359 | goto retry; |
| 4360 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4361 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4362 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4363 | goto end; |
| 4364 | } |
| 4365 | |
| 4366 | /* map an H2 frame to the HTX block so that we can put the |
| 4367 | * frame header there. |
| 4368 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4369 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 4370 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4371 | |
| 4372 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4373 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4374 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4375 | h2_set_frame_size(outbuf.area, fsize); |
| 4376 | |
| 4377 | /* update windows */ |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4378 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4379 | h2c->mws -= fsize; |
| 4380 | |
| 4381 | /* and exchange with our old area */ |
| 4382 | buf->area = old_area; |
| 4383 | buf->data = buf->head = 0; |
| 4384 | total += fsize; |
| 4385 | goto end; |
| 4386 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4387 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4388 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4389 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4390 | |
| 4391 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4392 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4393 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4394 | break; |
| 4395 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4396 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4400 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4401 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4402 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4403 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4404 | goto end; |
| 4405 | } |
| 4406 | |
| 4407 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4408 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4409 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4410 | outbuf.data = 9; |
| 4411 | |
| 4412 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4413 | * the HTX buffer. We need to check this against the connection's and |
| 4414 | * the stream's send windows, and to ensure that this fits in the max |
| 4415 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4416 | * the frame header). The connection's flow control is applied last so |
| 4417 | * that we can use a separate list of streams which are immediately |
| 4418 | * unblocked on window opening. Note: we don't implement padding. |
| 4419 | */ |
| 4420 | |
| 4421 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4422 | * empty frame, thus we force it to zero here. |
| 4423 | */ |
| 4424 | if (type == HTX_BLK_EOM) |
| 4425 | bsize = fsize = 0; |
| 4426 | |
| 4427 | if (!fsize) |
| 4428 | goto send_empty; |
| 4429 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4430 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4431 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4432 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4433 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4434 | goto end; |
| 4435 | } |
| 4436 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4437 | if (fsize > count) |
| 4438 | fsize = count; |
| 4439 | |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4440 | if (fsize > h2s_mws(h2s)) |
| 4441 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4442 | |
| 4443 | if (h2c->mfs && fsize > h2c->mfs) |
| 4444 | fsize = h2c->mfs; // >0 |
| 4445 | |
| 4446 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4447 | /* It doesn't fit at once. If it at least fits once split and |
| 4448 | * the amount of data to move is low, let's defragment the |
| 4449 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4450 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4451 | if (b_space_wraps(mbuf) && |
| 4452 | (fsize + 9 <= b_room(mbuf)) && |
| 4453 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4454 | goto realign_again; |
| 4455 | fsize = outbuf.size - 9; |
| 4456 | |
| 4457 | if (fsize <= 0) { |
| 4458 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4459 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4460 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4461 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4462 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4463 | goto end; |
| 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | if (h2c->mws <= 0) { |
| 4468 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4469 | goto end; |
| 4470 | } |
| 4471 | |
| 4472 | if (fsize > h2c->mws) |
| 4473 | fsize = h2c->mws; |
| 4474 | |
| 4475 | /* now let's copy this this into the output buffer */ |
| 4476 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 1d4a0f8 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4477 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4478 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4479 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4480 | |
| 4481 | send_empty: |
| 4482 | /* update the frame's size */ |
| 4483 | h2_set_frame_size(outbuf.area, fsize); |
| 4484 | |
| 4485 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4486 | * meeting EOM. We should optimize this later. |
| 4487 | */ |
| 4488 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4489 | total++; // EOM counts as one byte |
| 4490 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4491 | es_now = 1; |
| 4492 | } |
| 4493 | |
| 4494 | if (es_now) |
| 4495 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4496 | |
| 4497 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4498 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4499 | |
| 4500 | /* consume incoming HTX block, including EOM */ |
| 4501 | total += fsize; |
| 4502 | if (fsize == bsize) { |
| 4503 | htx_remove_blk(htx, blk); |
| 4504 | if (fsize) |
| 4505 | goto new_frame; |
| 4506 | } else { |
| 4507 | /* we've truncated this block */ |
| 4508 | htx_cut_data_blk(htx, blk, fsize); |
| 4509 | } |
| 4510 | |
| 4511 | if (es_now) { |
| 4512 | if (h2s->st == H2_SS_OPEN) |
| 4513 | h2s->st = H2_SS_HLOC; |
| 4514 | else |
| 4515 | h2s_close(h2s); |
| 4516 | |
| 4517 | h2s->flags |= H2_SF_ES_SENT; |
| 4518 | } |
| 4519 | |
| 4520 | end: |
| 4521 | return total; |
| 4522 | } |
| 4523 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4524 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4525 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4526 | * processed. The caller must check the stream's status to detect any error |
| 4527 | * which might have happened subsequently to a successful send. The htx blocks |
| 4528 | * are automatically removed from the message. The htx message is assumed to be |
| 4529 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 4530 | * the EOM, which is *not* removed. All trailers are processed at once and sent |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4531 | * as a single frame. The ES flag is always set. |
| 4532 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4533 | static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4534 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4535 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4536 | struct h2c *h2c = h2s->h2c; |
| 4537 | struct htx_blk *blk; |
| 4538 | struct htx_blk *blk_end; |
| 4539 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4540 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4541 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4542 | int ret = 0; |
| 4543 | int hdr; |
| 4544 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4545 | |
| 4546 | if (h2c_mux_busy(h2c, h2s)) { |
| 4547 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4548 | goto end; |
| 4549 | } |
| 4550 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4551 | /* determine the first block which must not be deleted, blk_end may |
| 4552 | * be NULL if all blocks have to be deleted. also get trailers. |
| 4553 | */ |
| 4554 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4555 | blk_end = NULL; |
| 4556 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4557 | hdr = 0; |
| 4558 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4559 | blk = htx_get_blk(htx, idx); |
| 4560 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4561 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4562 | if (type == HTX_BLK_UNUSED) |
| 4563 | continue; |
| 4564 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4565 | if (type == HTX_BLK_EOT) { |
| 4566 | if (idx != -1) |
| 4567 | blk_end = blk; |
| 4568 | break; |
| 4569 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 4570 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4571 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4572 | |
| 4573 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4574 | goto fail; |
| 4575 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4576 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4577 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 4578 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4579 | } |
| 4580 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4581 | /* marker for end of trailers */ |
| 4582 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4583 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4584 | mbuf = br_tail(h2c->mbuf); |
| 4585 | retry: |
| 4586 | if (!h2_get_buf(h2c, mbuf)) { |
| 4587 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4588 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4589 | goto end; |
| 4590 | } |
| 4591 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4592 | chunk_reset(&outbuf); |
| 4593 | |
| 4594 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4595 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4596 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4597 | break; |
| 4598 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4599 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | if (outbuf.size < 9) |
| 4603 | goto full; |
| 4604 | |
| 4605 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 4606 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 4607 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4608 | outbuf.data = 9; |
| 4609 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4610 | /* encode all headers */ |
| 4611 | for (idx = 0; idx < hdr; idx++) { |
| 4612 | /* these ones do not exist in H2 or must not appear in |
| 4613 | * trailers and must be dropped. |
| 4614 | */ |
| 4615 | if (isteq(list[idx].n, ist("host")) || |
| 4616 | isteq(list[idx].n, ist("content-length")) || |
| 4617 | isteq(list[idx].n, ist("connection")) || |
| 4618 | isteq(list[idx].n, ist("proxy-connection")) || |
| 4619 | isteq(list[idx].n, ist("keep-alive")) || |
| 4620 | isteq(list[idx].n, ist("upgrade")) || |
| 4621 | isteq(list[idx].n, ist("te")) || |
| 4622 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 4623 | continue; |
| 4624 | |
| 4625 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 4626 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4627 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4628 | goto realign_again; |
| 4629 | goto full; |
| 4630 | } |
| 4631 | } |
| 4632 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 4633 | if (outbuf.data == 9) { |
| 4634 | /* here we have a problem, we have nothing to emit (either we |
| 4635 | * received an empty trailers block followed or we removed its |
| 4636 | * contents above). Because of this we can't send a HEADERS |
| 4637 | * frame, so we have to cheat and instead send an empty DATA |
| 4638 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 4639 | */ |
| 4640 | outbuf.area[3] = H2_FT_DATA; |
| 4641 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 4642 | } |
| 4643 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4644 | /* update the frame's size */ |
| 4645 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4646 | |
| 4647 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4648 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4649 | h2s->flags |= H2_SF_ES_SENT; |
| 4650 | |
| 4651 | if (h2s->st == H2_SS_OPEN) |
| 4652 | h2s->st = H2_SS_HLOC; |
| 4653 | else |
| 4654 | h2s_close(h2s); |
| 4655 | |
| 4656 | /* OK we could properly deliver the response */ |
| 4657 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 4658 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4659 | ret = 0; |
| 4660 | idx = htx_get_head(htx); |
| 4661 | blk = htx_get_blk(htx, idx); |
| 4662 | while (blk != blk_end) { |
| 4663 | ret += htx_get_blksz(blk); |
| 4664 | blk = htx_remove_blk(htx, blk); |
| 4665 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4666 | |
| 4667 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4668 | ret += htx_get_blksz(blk_end); |
| 4669 | htx_remove_blk(htx, blk_end); |
| 4670 | } |
| 4671 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4672 | end: |
| 4673 | return ret; |
| 4674 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4675 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4676 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4677 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4678 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4679 | ret = 0; |
| 4680 | goto end; |
| 4681 | fail: |
| 4682 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4683 | * list etc go here (unrecoverable errors). |
| 4684 | */ |
| 4685 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4686 | ret = 0; |
| 4687 | goto end; |
| 4688 | } |
| 4689 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4690 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 4691 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 4692 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 4693 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 4694 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 4695 | * returns 0 except for the error above. |
| 4696 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4697 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 4698 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4699 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4700 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4701 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4702 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4703 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4704 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4705 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 4706 | sw->events |= SUB_RETRY_RECV; |
| 4707 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4708 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4709 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4710 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4711 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4712 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 4713 | sw->events |= SUB_RETRY_SEND; |
| 4714 | h2s->send_wait = sw; |
| 4715 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 4716 | !LIST_ADDED(&h2s->list)) { |
| 4717 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 4718 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 4719 | else |
| 4720 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 4721 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4722 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4723 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4724 | if (event_type != 0) |
| 4725 | return -1; |
| 4726 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4729 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 4730 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 4731 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 4732 | * It always returns zero. |
| 4733 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4734 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 4735 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4736 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4737 | struct h2s *h2s = cs->ctx; |
| 4738 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4739 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4740 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4741 | BUG_ON(h2s->recv_wait != sw); |
| 4742 | sw->events &= ~SUB_RETRY_RECV; |
| 4743 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4744 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4745 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4746 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4747 | BUG_ON(h2s->send_wait != sw); |
| 4748 | LIST_DEL(&h2s->list); |
| 4749 | LIST_INIT(&h2s->list); |
| 4750 | sw->events &= ~SUB_RETRY_SEND; |
| 4751 | /* We were about to send, make sure it does not happen */ |
| 4752 | if (LIST_ADDED(&h2s->sending_list) && |
| 4753 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 4754 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4755 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4756 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4757 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4758 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4759 | return 0; |
| 4760 | } |
| 4761 | |
| 4762 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4763 | /* Called from the upper layer, to receive data */ |
| 4764 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 4765 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4766 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4767 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4768 | struct htx *h2s_htx = NULL; |
| 4769 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4770 | size_t ret = 0; |
| 4771 | |
| 4772 | /* transfer possibly pending data to the upper layer */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4773 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
| 4774 | if (htx_is_empty(h2s_htx)) { |
| 4775 | /* Here htx_to_buf() will set buffer data to 0 because |
| 4776 | * the HTX is empty. |
| 4777 | */ |
| 4778 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 4779 | goto end; |
| 4780 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 4781 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4782 | ret = h2s_htx->data; |
| 4783 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 4784 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4785 | /* <buf> is empty and the message is small enough, swap the |
| 4786 | * buffers. */ |
| 4787 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4788 | htx_to_buf(buf_htx, buf); |
| 4789 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4790 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 4791 | goto end; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4792 | } |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4793 | |
| 4794 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
| 4795 | |
| 4796 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
| 4797 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 4798 | if (htx_is_empty(buf_htx)) |
| 4799 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4800 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4801 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4802 | buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0); |
| 4803 | htx_to_buf(buf_htx, buf); |
| 4804 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 4805 | ret -= h2s_htx->data; |
| 4806 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 4807 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4808 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 4809 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4810 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 4811 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 4812 | if (h2s->flags & H2_SF_ES_RCVD) |
| 4813 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 4814 | if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4815 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 4816 | if (cs->flags & CS_FL_ERR_PENDING) |
| 4817 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4818 | if (b_size(&h2s->rxbuf)) { |
| 4819 | b_free(&h2s->rxbuf); |
| 4820 | offer_buffers(NULL, tasks_run_queue); |
| 4821 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4822 | } |
| 4823 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4824 | if (ret && h2c->dsi == h2s->id) { |
| 4825 | /* demux is blocking on this stream's buffer */ |
| 4826 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 4827 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4828 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 4829 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4830 | return ret; |
| 4831 | } |
| 4832 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4833 | /* stops all senders of this connection for example when the mux buffer is full. |
| 4834 | * They are moved from the sending_list to either fctl_list or send_list. |
| 4835 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4836 | static void h2_stop_senders(struct h2c *h2c) |
| 4837 | { |
| 4838 | struct h2s *h2s, *h2s_back; |
| 4839 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4840 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 4841 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 4842 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4843 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4847 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 4848 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 4849 | * flags may be updated on the conn_stream. |
| 4850 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 4851 | 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] | 4852 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4853 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4854 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4855 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4856 | struct htx *htx; |
| 4857 | struct htx_blk *blk; |
| 4858 | enum htx_blk_type btype; |
| 4859 | uint32_t bsize; |
| 4860 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4861 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4862 | /* If we were not just woken because we wanted to send but couldn't, |
| 4863 | * and there's somebody else that is waiting to send, do nothing, |
| 4864 | * we will subscribe later and be put at the end of the list |
| 4865 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4866 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4867 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 4868 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 4869 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4870 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 4871 | /* We couldn't set it to NULL before, because we needed it in case |
| 4872 | * we had to cancel the tasklet |
| 4873 | */ |
| 4874 | h2s->send_wait = NULL; |
| 4875 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 4876 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 4877 | return 0; |
| 4878 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4879 | htx = htx_from_buf(buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4880 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4881 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 4882 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 4883 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4884 | if (h2s->id == 0) { |
| 4885 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 4886 | |
| 4887 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4888 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4889 | return 0; |
| 4890 | } |
| 4891 | |
| 4892 | eb32_delete(&h2s->by_id); |
| 4893 | h2s->by_id.key = h2s->id = id; |
| 4894 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 4895 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4896 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 4897 | } |
| 4898 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4899 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
| 4900 | count && !htx_is_empty(htx)) { |
| 4901 | idx = htx_get_head(htx); |
| 4902 | blk = htx_get_blk(htx, idx); |
| 4903 | btype = htx_get_blk_type(blk); |
| 4904 | bsize = htx_get_blksz(blk); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4905 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4906 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4907 | case HTX_BLK_REQ_SL: |
| 4908 | /* start-line before headers */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4909 | ret = h2s_bck_make_req_headers(h2s, htx); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4910 | if (ret > 0) { |
| 4911 | total += ret; |
| 4912 | count -= ret; |
| 4913 | if (ret < bsize) |
| 4914 | goto done; |
| 4915 | } |
| 4916 | break; |
| 4917 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4918 | case HTX_BLK_RES_SL: |
| 4919 | /* start-line before headers */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4920 | ret = h2s_frt_make_resp_headers(h2s, htx); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4921 | if (ret > 0) { |
| 4922 | total += ret; |
| 4923 | count -= ret; |
| 4924 | if (ret < bsize) |
| 4925 | goto done; |
| 4926 | } |
| 4927 | break; |
| 4928 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4929 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4930 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4931 | /* all these cause the emission of a DATA frame (possibly empty). |
| 4932 | * This EOM necessarily is one before trailers, as the EOM following |
| 4933 | * trailers would have been consumed by the trailers parser. |
| 4934 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4935 | ret = h2s_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4936 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4937 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4938 | total += ret; |
| 4939 | count -= ret; |
| 4940 | if (ret < bsize) |
| 4941 | goto done; |
| 4942 | } |
| 4943 | break; |
| 4944 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4945 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4946 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4947 | /* This is the first trailers block, all the subsequent ones AND |
| 4948 | * the EOM will be swallowed by the parser. |
| 4949 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4950 | ret = h2s_make_trailers(h2s, htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4951 | if (ret > 0) { |
| 4952 | total += ret; |
| 4953 | count -= ret; |
| 4954 | if (ret < bsize) |
| 4955 | goto done; |
| 4956 | } |
| 4957 | break; |
| 4958 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4959 | default: |
| 4960 | htx_remove_blk(htx, blk); |
| 4961 | total += bsize; |
| 4962 | count -= bsize; |
| 4963 | break; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4964 | } |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4965 | } |
| 4966 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4967 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 4968 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4969 | /* trim any possibly pending data after we close (extra CR-LF, |
| 4970 | * unprocessed trailers, abnormal extra data, ...) |
| 4971 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4972 | total += count; |
| 4973 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4974 | } |
| 4975 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 4976 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 4977 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 4978 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 4979 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4980 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 4981 | } |
| 4982 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4983 | htx_to_buf(htx, buf); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4984 | |
| 4985 | /* The mux is full, cancel the pending tasks */ |
| 4986 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 4987 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 4988 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4989 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 4990 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4991 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4992 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4993 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 4994 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 4995 | /* If we're waiting for flow control, and we got a shutr on the |
| 4996 | * connection, we will never be unlocked, so add an error on |
| 4997 | * the conn_stream. |
| 4998 | */ |
| 4999 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5000 | !b_data(&h2s->h2c->dbuf) && |
| 5001 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5002 | if (cs->flags & CS_FL_EOS) |
| 5003 | cs->flags |= CS_FL_ERROR; |
| 5004 | else |
| 5005 | cs->flags |= CS_FL_ERR_PENDING; |
| 5006 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5007 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5008 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5009 | LIST_DEL_INIT(&h2s->list); |
| 5010 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5011 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5012 | } |
| 5013 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5014 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5015 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5016 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5017 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5018 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5019 | struct eb32_node *node; |
| 5020 | int fctl_cnt = 0; |
| 5021 | int send_cnt = 0; |
| 5022 | int tree_cnt = 0; |
| 5023 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5024 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5025 | |
| 5026 | if (!h2c) |
| 5027 | return; |
| 5028 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5029 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5030 | fctl_cnt++; |
| 5031 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5032 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5033 | send_cnt++; |
| 5034 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5035 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5036 | node = eb32_first(&h2c->streams_by_id); |
| 5037 | while (node) { |
| 5038 | h2s = container_of(node, struct h2s, by_id); |
| 5039 | tree_cnt++; |
| 5040 | if (!h2s->cs) |
| 5041 | orph_cnt++; |
| 5042 | node = eb32_next(node); |
| 5043 | } |
| 5044 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5045 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5046 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5047 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5048 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5049 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5050 | " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]", |
Willy Tarreau | 616ac81 | 2018-07-24 14:12:42 +0200 | [diff] [blame] | 5051 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5052 | h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5053 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5054 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5055 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5056 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5057 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5058 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5059 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5060 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5061 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5062 | |
| 5063 | if (h2s) { |
| 5064 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5065 | h2s, h2s->id, h2s->flags, |
| 5066 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5067 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5068 | h2s->cs); |
| 5069 | if (h2s->cs) |
| 5070 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5071 | h2s->cs->flags, h2s->cs->data); |
| 5072 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5073 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5074 | |
| 5075 | /*******************************************************/ |
| 5076 | /* functions below are dedicated to the config parsers */ |
| 5077 | /*******************************************************/ |
| 5078 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5079 | /* config parser for global "tune.h2.header-table-size" */ |
| 5080 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5081 | struct proxy *defpx, const char *file, int line, |
| 5082 | char **err) |
| 5083 | { |
| 5084 | if (too_many_args(1, args, err, NULL)) |
| 5085 | return -1; |
| 5086 | |
| 5087 | h2_settings_header_table_size = atoi(args[1]); |
| 5088 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5089 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5090 | return -1; |
| 5091 | } |
| 5092 | return 0; |
| 5093 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5094 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5095 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5096 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5097 | struct proxy *defpx, const char *file, int line, |
| 5098 | char **err) |
| 5099 | { |
| 5100 | if (too_many_args(1, args, err, NULL)) |
| 5101 | return -1; |
| 5102 | |
| 5103 | h2_settings_initial_window_size = atoi(args[1]); |
| 5104 | if (h2_settings_initial_window_size < 0) { |
| 5105 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5106 | return -1; |
| 5107 | } |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5111 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5112 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5113 | struct proxy *defpx, const char *file, int line, |
| 5114 | char **err) |
| 5115 | { |
| 5116 | if (too_many_args(1, args, err, NULL)) |
| 5117 | return -1; |
| 5118 | |
| 5119 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5120 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5121 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5122 | return -1; |
| 5123 | } |
| 5124 | return 0; |
| 5125 | } |
| 5126 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5127 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5128 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5129 | struct proxy *defpx, const char *file, int line, |
| 5130 | char **err) |
| 5131 | { |
| 5132 | if (too_many_args(1, args, err, NULL)) |
| 5133 | return -1; |
| 5134 | |
| 5135 | h2_settings_max_frame_size = atoi(args[1]); |
| 5136 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5137 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5138 | return -1; |
| 5139 | } |
| 5140 | return 0; |
| 5141 | } |
| 5142 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5143 | |
| 5144 | /****************************************/ |
| 5145 | /* MUX initialization and instanciation */ |
| 5146 | /***************************************/ |
| 5147 | |
| 5148 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5149 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5150 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5151 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5152 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5153 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5154 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5155 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5156 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5157 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5158 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5159 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5160 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5161 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5162 | .shutr = h2_shutr, |
| 5163 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5164 | .show_fd = h2_show_fd, |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 5165 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5166 | .name = "H2", |
| 5167 | }; |
| 5168 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5169 | static struct mux_proto_list mux_proto_h2 = |
Christopher Faulet | c985f6c | 2019-07-15 11:42:52 +0200 | [diff] [blame] | 5170 | { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5171 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5172 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5173 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5174 | /* config keyword parsers */ |
| 5175 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5176 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5177 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5178 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5179 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5180 | { 0, NULL, NULL } |
| 5181 | }}; |
| 5182 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5183 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |