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_* */ |
| 203 | int mws; /* mux window size for this stream */ |
| 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 | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 702 | /* 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] | 703 | 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] | 704 | { |
| 705 | if (h2c->msi < 0) |
| 706 | return 0; |
| 707 | |
| 708 | if (h2c->msi == h2s_id(h2s)) |
| 709 | return 0; |
| 710 | |
| 711 | return 1; |
| 712 | } |
| 713 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 714 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 715 | 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] | 716 | { |
| 717 | h2c->errcode = err; |
| 718 | h2c->st0 = H2_CS_ERROR; |
| 719 | } |
| 720 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 721 | /* marks an error on the stream. It may also update an already closed stream |
| 722 | * (e.g. to report an error after an RST was received). |
| 723 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 724 | 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] | 725 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 726 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 727 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 728 | if (h2s->st < H2_SS_ERROR) |
| 729 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 730 | if (h2s->cs) |
| 731 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 735 | /* attempt to notify the data layer of recv availability */ |
| 736 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 737 | { |
| 738 | struct wait_event *sw; |
| 739 | |
| 740 | if (h2s->recv_wait) { |
| 741 | sw = h2s->recv_wait; |
| 742 | sw->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 743 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 744 | h2s->recv_wait = NULL; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | /* attempt to notify the data layer of send availability */ |
| 749 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 750 | { |
| 751 | struct wait_event *sw; |
| 752 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 753 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 754 | sw = h2s->send_wait; |
| 755 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 756 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 757 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 761 | /* alerts the data layer, trying to wake it up by all means, following |
| 762 | * this sequence : |
| 763 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 764 | * - if its subscribed to send, then it's woken up for send |
| 765 | * - if it was subscribed to neither, its ->wake() callback is called |
| 766 | * It is safe to call this function with a closed stream which doesn't have a |
| 767 | * conn_stream anymore. |
| 768 | */ |
| 769 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 770 | { |
| 771 | if (h2s->recv_wait || h2s->send_wait) { |
| 772 | h2s_notify_recv(h2s); |
| 773 | h2s_notify_send(h2s); |
| 774 | } |
| 775 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 776 | h2s->cs->data_cb->wake(h2s->cs); |
| 777 | } |
| 778 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 779 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 780 | 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] | 781 | { |
| 782 | uint8_t *out = frame; |
| 783 | |
| 784 | *out = len >> 16; |
| 785 | write_n16(out + 1, len); |
| 786 | } |
| 787 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 788 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 789 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 790 | * 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] | 791 | * available in the buffer's input prior to calling this function. The buffer |
| 792 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 793 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 794 | 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] | 795 | const struct buffer *b, int o) |
| 796 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 797 | 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] | 798 | } |
| 799 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 800 | 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] | 801 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 802 | 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] | 803 | } |
| 804 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 805 | 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] | 806 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 807 | 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] | 808 | } |
| 809 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 810 | 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] | 811 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 812 | 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] | 813 | } |
| 814 | |
| 815 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 816 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 817 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 818 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 819 | * 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] | 820 | * |
| 821 | * b0 b1 b2 b3 b4 b5..b8 |
| 822 | * +----------+---------+--------+----+----+----------------------+ |
| 823 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 824 | * +----------+---------+--------+----+----+----------------------+ |
| 825 | * |
| 826 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 827 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 828 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 829 | * 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] | 830 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 831 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 832 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 833 | 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] | 834 | { |
| 835 | uint64_t w; |
| 836 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 837 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 838 | return 0; |
| 839 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 840 | w = h2_get_n64(b, o + 1); |
| 841 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 842 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 843 | h->ff = w >> 32; |
| 844 | h->ft = w >> 40; |
| 845 | h->len += w >> 48; |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 850 | * h2_peek_frame_hdr() above. |
| 851 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 852 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 853 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 854 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 858 | 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] | 859 | { |
| 860 | int ret; |
| 861 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 862 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 863 | if (ret > 0) |
| 864 | h2_skip_frame_hdr(b); |
| 865 | return ret; |
| 866 | } |
| 867 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 868 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 869 | * its connection if the stream was not yet closed. Please use this exclusively |
| 870 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 871 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 872 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 873 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 874 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 875 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 876 | if (!h2s->id) |
| 877 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 878 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 879 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 880 | h2s_notify_recv(h2s); |
| 881 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 882 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 883 | h2s->st = H2_SS_CLOSED; |
| 884 | } |
| 885 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 886 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 887 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 888 | { |
| 889 | h2s_close(h2s); |
| 890 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 891 | if (b_size(&h2s->rxbuf)) { |
| 892 | b_free(&h2s->rxbuf); |
| 893 | offer_buffers(NULL, tasks_run_queue); |
| 894 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 895 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 896 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 897 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 898 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 899 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 900 | * reference left would be in the h2c send_list/fctl_list, and if |
| 901 | * we're in it, we're getting out anyway |
| 902 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 903 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 904 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 905 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 906 | LIST_DEL_INIT(&h2s->sending_list); |
| 907 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 908 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 909 | pool_free(pool_head_h2s, h2s); |
| 910 | } |
| 911 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 912 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 913 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 914 | * causes of errors can be any failed memory allocation. The caller is |
| 915 | * responsible for checking if the connection may support an extra stream |
| 916 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 917 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 918 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 919 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 920 | struct h2s *h2s; |
| 921 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 922 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 923 | if (!h2s) |
| 924 | goto out; |
| 925 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 926 | h2s->wait_event.tasklet = tasklet_new(); |
| 927 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 928 | pool_free(pool_head_h2s, h2s); |
| 929 | goto out; |
| 930 | } |
| 931 | h2s->send_wait = NULL; |
| 932 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 933 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 934 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 935 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 936 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 937 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 938 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 939 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 940 | h2s->mws = h2c->miw; |
| 941 | h2s->flags = H2_SF_NONE; |
| 942 | h2s->errcode = H2_ERR_NO_ERROR; |
| 943 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 944 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 945 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 946 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 947 | |
| 948 | if (h2c->flags & H2_CF_IS_BACK) { |
| 949 | h1m_init_req(&h2s->h1m); |
| 950 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 951 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 952 | } else { |
| 953 | h1m_init_res(&h2s->h1m); |
| 954 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 955 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 956 | } |
| 957 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 958 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 959 | if (id > 0) |
| 960 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 961 | else |
| 962 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 963 | |
| 964 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 965 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 966 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 967 | |
| 968 | return h2s; |
| 969 | |
| 970 | out_free_h2s: |
| 971 | pool_free(pool_head_h2s, h2s); |
| 972 | out: |
| 973 | return NULL; |
| 974 | } |
| 975 | |
| 976 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 977 | * case of memory allocation error. |
| 978 | */ |
| 979 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 980 | { |
| 981 | struct session *sess = h2c->conn->owner; |
| 982 | struct conn_stream *cs; |
| 983 | struct h2s *h2s; |
| 984 | |
| 985 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 986 | goto out; |
| 987 | |
| 988 | h2s = h2s_new(h2c, id); |
| 989 | if (!h2s) |
| 990 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 991 | |
| 992 | cs = cs_new(h2c->conn); |
| 993 | if (!cs) |
| 994 | goto out_close; |
| 995 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 996 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 997 | h2s->cs = cs; |
| 998 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 999 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1000 | |
| 1001 | if (stream_create_from_cs(cs) < 0) |
| 1002 | goto out_free_cs; |
| 1003 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1004 | /* We want the accept date presented to the next stream to be the one |
| 1005 | * we have now, the handshake time to be null (since the next stream |
| 1006 | * is not delayed by a handshake), and the idle time to count since |
| 1007 | * right now. |
| 1008 | */ |
| 1009 | sess->accept_date = date; |
| 1010 | sess->tv_accept = now; |
| 1011 | sess->t_handshake = 0; |
| 1012 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1013 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1014 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1015 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1016 | return h2s; |
| 1017 | |
| 1018 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1019 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1020 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1021 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1022 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1023 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1024 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1025 | sess_log(sess); |
| 1026 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1029 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1030 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1031 | * possible stream ID was reached. |
| 1032 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1033 | 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] | 1034 | { |
| 1035 | struct h2s *h2s = NULL; |
| 1036 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1037 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1038 | goto out; |
| 1039 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1040 | if (h2_streams_left(h2c) < 1) |
| 1041 | goto out; |
| 1042 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1043 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1044 | h2s = h2s_new(h2c, 0); |
| 1045 | if (!h2s) |
| 1046 | goto out; |
| 1047 | |
| 1048 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1049 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1050 | cs->ctx = h2s; |
| 1051 | h2c->nb_cs++; |
| 1052 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1053 | out: |
| 1054 | return h2s; |
| 1055 | } |
| 1056 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1057 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1058 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1059 | * the various settings codes. |
| 1060 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1061 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1062 | { |
| 1063 | struct buffer *res; |
| 1064 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1065 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1066 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1067 | int ret; |
| 1068 | |
| 1069 | if (h2c_mux_busy(h2c, NULL)) { |
| 1070 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1074 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1075 | chunk_memcpy(&buf, |
| 1076 | "\x00\x00\x00" /* length : 0 for now */ |
| 1077 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1078 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1079 | 9); |
| 1080 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1081 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1082 | /* send settings_enable_push=0 */ |
| 1083 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1084 | } |
| 1085 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1086 | if (h2_settings_header_table_size != 4096) { |
| 1087 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1088 | |
| 1089 | write_n32(str + 2, h2_settings_header_table_size); |
| 1090 | chunk_memcat(&buf, str, 6); |
| 1091 | } |
| 1092 | |
| 1093 | if (h2_settings_initial_window_size != 65535) { |
| 1094 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1095 | |
| 1096 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1097 | chunk_memcat(&buf, str, 6); |
| 1098 | } |
| 1099 | |
| 1100 | if (h2_settings_max_concurrent_streams != 0) { |
| 1101 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1102 | |
| 1103 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1104 | * the protocol, so never send this value! |
| 1105 | */ |
| 1106 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1107 | chunk_memcat(&buf, str, 6); |
| 1108 | } |
| 1109 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1110 | mfs = h2_settings_max_frame_size; |
| 1111 | if (mfs > global.tune.bufsize) |
| 1112 | mfs = global.tune.bufsize; |
| 1113 | |
| 1114 | if (!mfs) |
| 1115 | mfs = global.tune.bufsize; |
| 1116 | |
| 1117 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1118 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1119 | |
| 1120 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1121 | * match bufsize - rewrite size, but at the moment it seems |
| 1122 | * that clients don't take care of it. |
| 1123 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1124 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1125 | chunk_memcat(&buf, str, 6); |
| 1126 | } |
| 1127 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1128 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1129 | |
| 1130 | res = br_tail(h2c->mbuf); |
| 1131 | retry: |
| 1132 | if (!h2_get_buf(h2c, res)) { |
| 1133 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1134 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1138 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1139 | if (unlikely(ret <= 0)) { |
| 1140 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1141 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1142 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1143 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1144 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1145 | return 0; |
| 1146 | } |
| 1147 | else { |
| 1148 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | } |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1155 | /* Try to receive a connection preface, then upon success try to send our |
| 1156 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1157 | * missing data. It may return an error in h2c. |
| 1158 | */ |
| 1159 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1160 | { |
| 1161 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1162 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1163 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1164 | 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] | 1165 | |
| 1166 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1167 | if (ret1 < 0) |
| 1168 | sess_log(h2c->conn->owner); |
| 1169 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1170 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1171 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1175 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1176 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1177 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1178 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1179 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1182 | /* Try to send a connection preface, then upon success try to send our |
| 1183 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1184 | * missing data. It may return an error in h2c. |
| 1185 | */ |
| 1186 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1187 | { |
| 1188 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1189 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1190 | |
| 1191 | if (h2c_mux_busy(h2c, NULL)) { |
| 1192 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1196 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1197 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1198 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1199 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1200 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | if (!b_data(res)) { |
| 1205 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1206 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1207 | if (unlikely(ret <= 0)) { |
| 1208 | if (!ret) { |
| 1209 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1210 | goto retry; |
| 1211 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1212 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1213 | return 0; |
| 1214 | } |
| 1215 | else { |
| 1216 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1220 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1221 | return h2c_send_settings(h2c); |
| 1222 | } |
| 1223 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1224 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1225 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1226 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1227 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1228 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1229 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1230 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1231 | * case so that it is safe to use h2c_error() to report such errors. |
| 1232 | */ |
| 1233 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1234 | { |
| 1235 | struct buffer *res; |
| 1236 | char str[17]; |
| 1237 | int ret; |
| 1238 | |
| 1239 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1240 | return 1; // claim that it worked |
| 1241 | |
| 1242 | if (h2c_mux_busy(h2c, h2s)) { |
| 1243 | if (h2s) |
| 1244 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1245 | else |
| 1246 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1250 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1251 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1252 | |
| 1253 | if (h2c->last_sid < 0) |
| 1254 | h2c->last_sid = h2c->max_id; |
| 1255 | |
| 1256 | write_n32(str + 9, h2c->last_sid); |
| 1257 | write_n32(str + 13, h2c->errcode); |
| 1258 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1259 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1260 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1261 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1262 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1263 | if (h2s) |
| 1264 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1265 | else |
| 1266 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1270 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1271 | if (unlikely(ret <= 0)) { |
| 1272 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1273 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1274 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1275 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1276 | if (h2s) |
| 1277 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1278 | else |
| 1279 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1280 | return 0; |
| 1281 | } |
| 1282 | else { |
| 1283 | /* we cannot report this error using GOAWAY, so we mark |
| 1284 | * it and claim a success. |
| 1285 | */ |
| 1286 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1287 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1288 | return 1; |
| 1289 | } |
| 1290 | } |
| 1291 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1295 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1296 | * during mux operations. This stream must be valid and cannot be closed |
| 1297 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1298 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1299 | * not yet. |
| 1300 | * |
| 1301 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1302 | * to write the message, it subscribes the stream to future notifications. |
| 1303 | */ |
| 1304 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1305 | { |
| 1306 | struct buffer *res; |
| 1307 | char str[13]; |
| 1308 | int ret; |
| 1309 | |
| 1310 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1311 | return 1; |
| 1312 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1313 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1314 | * RST_STREAM in response to a RST_STREAM frame. |
| 1315 | */ |
| 1316 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1317 | ret = 1; |
| 1318 | goto ignore; |
| 1319 | } |
| 1320 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1321 | if (h2c_mux_busy(h2c, h2s)) { |
| 1322 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1326 | /* len: 4, type: 3, flags: none */ |
| 1327 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1328 | write_n32(str + 5, h2s->id); |
| 1329 | write_n32(str + 9, h2s->errcode); |
| 1330 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1331 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1332 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1333 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1334 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1335 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1339 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1340 | if (unlikely(ret <= 0)) { |
| 1341 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1342 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1343 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1344 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1345 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1346 | return 0; |
| 1347 | } |
| 1348 | else { |
| 1349 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1350 | return 0; |
| 1351 | } |
| 1352 | } |
| 1353 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1354 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1355 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1356 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1357 | return ret; |
| 1358 | } |
| 1359 | |
| 1360 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1361 | * 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] | 1362 | * error code, even if the stream is one of the dummy ones, and will update |
| 1363 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1364 | * |
| 1365 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1366 | * 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] | 1367 | * 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] | 1368 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1369 | */ |
| 1370 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1371 | { |
| 1372 | struct buffer *res; |
| 1373 | char str[13]; |
| 1374 | int ret; |
| 1375 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1376 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1377 | * RST_STREAM in response to a RST_STREAM frame. |
| 1378 | */ |
| 1379 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1380 | ret = 1; |
| 1381 | goto ignore; |
| 1382 | } |
| 1383 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1384 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1385 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1389 | /* len: 4, type: 3, flags: none */ |
| 1390 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1391 | |
| 1392 | write_n32(str + 5, h2c->dsi); |
| 1393 | write_n32(str + 9, h2s->errcode); |
| 1394 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1395 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1396 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1397 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1398 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1399 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1403 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1404 | if (unlikely(ret <= 0)) { |
| 1405 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1406 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1407 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1408 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1409 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1410 | return 0; |
| 1411 | } |
| 1412 | else { |
| 1413 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1414 | return 0; |
| 1415 | } |
| 1416 | } |
| 1417 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1418 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1419 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1420 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1421 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1424 | return ret; |
| 1425 | } |
| 1426 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1427 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1428 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1429 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1430 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1431 | * message, it subscribes the requesting stream to future notifications. |
| 1432 | */ |
| 1433 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1434 | { |
| 1435 | struct h2c *h2c = h2s->h2c; |
| 1436 | struct buffer *res; |
| 1437 | char str[9]; |
| 1438 | int ret; |
| 1439 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1440 | 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] | 1441 | return 1; |
| 1442 | |
| 1443 | if (h2c_mux_busy(h2c, h2s)) { |
| 1444 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1448 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1449 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1450 | write_n32(str + 5, h2s->id); |
| 1451 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1452 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1453 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1454 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1455 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1456 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1460 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1461 | if (likely(ret > 0)) { |
| 1462 | h2s->flags |= H2_SF_ES_SENT; |
| 1463 | } |
| 1464 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1465 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1466 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1467 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1468 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1469 | return 0; |
| 1470 | } |
| 1471 | else { |
| 1472 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1473 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1474 | } |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1478 | /* 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] | 1479 | * 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] | 1480 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1481 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1482 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1483 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1484 | { |
| 1485 | if (!h2s->cs) { |
| 1486 | /* this stream was already orphaned */ |
| 1487 | h2s_destroy(h2s); |
| 1488 | return; |
| 1489 | } |
| 1490 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1491 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1492 | if (h2s->st == H2_SS_OPEN) |
| 1493 | h2s->st = H2_SS_HREM; |
| 1494 | else if (h2s->st == H2_SS_HLOC) |
| 1495 | h2s_close(h2s); |
| 1496 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1497 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1498 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1499 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1500 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1501 | if (h2s->cs->flags & CS_FL_EOS) |
| 1502 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1503 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1504 | if (h2s->st < H2_SS_ERROR) |
| 1505 | h2s->st = H2_SS_ERROR; |
| 1506 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1507 | |
| 1508 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1512 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1513 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1514 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1515 | { |
| 1516 | struct eb32_node *node; |
| 1517 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1518 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1519 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1520 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1521 | while (node) { |
| 1522 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1523 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1524 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1525 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1526 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1527 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1528 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1529 | while (node) { |
| 1530 | h2s = container_of(node, struct h2s, by_id); |
| 1531 | if (h2s->id > 0) |
| 1532 | break; |
| 1533 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1534 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1538 | /* Increase all streams' outgoing window size by the difference passed in |
| 1539 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1540 | * window size is different. The difference may be negative and the resulting |
| 1541 | * window size as well, for the time it takes to receive some window updates. |
| 1542 | */ |
| 1543 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1544 | { |
| 1545 | struct h2s *h2s; |
| 1546 | struct eb32_node *node; |
| 1547 | |
| 1548 | if (!diff) |
| 1549 | return; |
| 1550 | |
| 1551 | node = eb32_first(&h2c->streams_by_id); |
| 1552 | while (node) { |
| 1553 | h2s = container_of(node, struct h2s, by_id); |
| 1554 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1555 | |
| 1556 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1557 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1558 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1559 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1562 | node = eb32_next(node); |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1567 | * 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] | 1568 | * return an error in h2c. The caller must have already verified frame length |
| 1569 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1570 | */ |
| 1571 | static int h2c_handle_settings(struct h2c *h2c) |
| 1572 | { |
| 1573 | unsigned int offset; |
| 1574 | int error; |
| 1575 | |
| 1576 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1577 | if (h2c->dfl) { |
| 1578 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1579 | goto fail; |
| 1580 | } |
| 1581 | return 1; |
| 1582 | } |
| 1583 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1584 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1585 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1586 | return 0; |
| 1587 | |
| 1588 | /* parse the frame */ |
| 1589 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1590 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1591 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1592 | |
| 1593 | switch (type) { |
| 1594 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1595 | /* we need to update all existing streams with the |
| 1596 | * difference from the previous iws. |
| 1597 | */ |
| 1598 | if (arg < 0) { // RFC7540#6.5.2 |
| 1599 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1600 | goto fail; |
| 1601 | } |
| 1602 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 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 | |
| 1864 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1865 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1866 | goto strm_err; |
| 1867 | } |
| 1868 | |
| 1869 | h2s->mws += inc; |
| 1870 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 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) { |
| 2185 | error = H2_ERR_STREAM_CLOSED; |
| 2186 | goto strm_err; |
| 2187 | } |
| 2188 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2189 | if (h2c->st0 >= H2_CS_ERROR) |
| 2190 | return 0; |
| 2191 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2192 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2193 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2194 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | /* check for completion : the callee will change this to FRAME_A or |
| 2198 | * FRAME_H once done. |
| 2199 | */ |
| 2200 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2201 | return 0; |
| 2202 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2203 | /* last frame */ |
| 2204 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2205 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2206 | if (h2s->st == H2_SS_OPEN) |
| 2207 | h2s->st = H2_SS_HREM; |
| 2208 | else |
| 2209 | h2s_close(h2s); |
| 2210 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2211 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2212 | /* RFC7540#8.1.2 */ |
| 2213 | error = H2_ERR_PROTOCOL_ERROR; |
| 2214 | goto strm_err; |
| 2215 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2218 | return 1; |
| 2219 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2220 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2221 | h2s_error(h2s, error); |
| 2222 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2223 | return 0; |
| 2224 | } |
| 2225 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2226 | /* process Rx frames to be demultiplexed */ |
| 2227 | static void h2_process_demux(struct h2c *h2c) |
| 2228 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2229 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2230 | struct h2_fh hdr; |
| 2231 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2232 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2233 | if (h2c->st0 >= H2_CS_ERROR) |
| 2234 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2235 | |
| 2236 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2237 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2238 | if (h2c->flags & H2_CF_IS_BACK) |
| 2239 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2240 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2241 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2242 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2243 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2244 | sess_log(h2c->conn->owner); |
| 2245 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2246 | goto fail; |
| 2247 | } |
| 2248 | |
| 2249 | h2c->max_id = 0; |
| 2250 | h2c->st0 = H2_CS_SETTINGS1; |
| 2251 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2252 | |
| 2253 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2254 | /* ensure that what is pending is a valid SETTINGS frame |
| 2255 | * without an ACK. |
| 2256 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2257 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2258 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2259 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2260 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2261 | sess_log(h2c->conn->owner); |
| 2262 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2263 | goto fail; |
| 2264 | } |
| 2265 | |
| 2266 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2267 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2268 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2269 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2270 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2271 | goto fail; |
| 2272 | } |
| 2273 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2274 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2275 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2276 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2277 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2278 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2279 | goto fail; |
| 2280 | } |
| 2281 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2282 | /* that's OK, switch to FRAME_P to process it. This is |
| 2283 | * a SETTINGS frame whose header has already been |
| 2284 | * deleted above. |
| 2285 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2286 | padlen = 0; |
| 2287 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2288 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2289 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2290 | |
| 2291 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2292 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2293 | int ret = 0; |
| 2294 | |
| 2295 | if (h2c->st0 >= H2_CS_ERROR) |
| 2296 | break; |
| 2297 | |
| 2298 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2299 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2300 | break; |
| 2301 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2302 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2303 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2304 | if (!h2c->nb_streams) { |
| 2305 | /* only log if no other stream can report the error */ |
| 2306 | sess_log(h2c->conn->owner); |
| 2307 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2308 | break; |
| 2309 | } |
| 2310 | |
Christopher Faulet | dd2a562 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2311 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2312 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2313 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2314 | * we read the pad length and drop it from the remaining |
| 2315 | * payload (one byte + the 9 remaining ones = 10 total |
| 2316 | * removed), so we have a frame payload starting after the |
| 2317 | * pad len. Flow controlled frames (DATA) also count the |
| 2318 | * padlen in the flow control, so it must be adjusted. |
| 2319 | */ |
| 2320 | if (hdr.len < 1) { |
| 2321 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2322 | sess_log(h2c->conn->owner); |
| 2323 | goto fail; |
| 2324 | } |
| 2325 | hdr.len--; |
| 2326 | |
| 2327 | if (b_data(&h2c->dbuf) < 10) |
| 2328 | break; // missing padlen |
| 2329 | |
| 2330 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2331 | |
| 2332 | if (padlen > hdr.len) { |
| 2333 | /* RFC7540#6.1 : pad length = length of |
| 2334 | * frame payload or greater => error. |
| 2335 | */ |
| 2336 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2337 | sess_log(h2c->conn->owner); |
| 2338 | goto fail; |
| 2339 | } |
| 2340 | |
| 2341 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2342 | h2c->rcvd_c++; |
| 2343 | h2c->rcvd_s++; |
| 2344 | } |
| 2345 | b_del(&h2c->dbuf, 1); |
| 2346 | } |
| 2347 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2348 | |
| 2349 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2350 | h2c->dfl = hdr.len; |
| 2351 | h2c->dsi = hdr.sid; |
| 2352 | h2c->dft = hdr.ft; |
| 2353 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2354 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2355 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2356 | |
| 2357 | /* check for minimum basic frame format validity */ |
| 2358 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2359 | if (ret != H2_ERR_NO_ERROR) { |
| 2360 | h2c_error(h2c, ret); |
| 2361 | sess_log(h2c->conn->owner); |
| 2362 | goto fail; |
| 2363 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2367 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2368 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2369 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2370 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2371 | conn_xprt_read0_pending(h2c->conn) || |
| 2372 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2373 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2374 | (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] | 2375 | /* we may have to signal the upper layers */ |
| 2376 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2377 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2378 | } |
| 2379 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2380 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2381 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2382 | goto strm_err; |
| 2383 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2384 | if (h2s->st == H2_SS_IDLE && |
| 2385 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2386 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2387 | * this state MUST be treated as a connection error |
| 2388 | */ |
| 2389 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2390 | if (!h2c->nb_streams) { |
| 2391 | /* only log if no other stream can report the error */ |
| 2392 | sess_log(h2c->conn->owner); |
| 2393 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2394 | break; |
| 2395 | } |
| 2396 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2397 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2398 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2399 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2400 | * this state MUST be treated as a stream error. |
| 2401 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2402 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2403 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2404 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2405 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2406 | else |
| 2407 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2408 | goto strm_err; |
| 2409 | } |
| 2410 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2411 | /* Below the management of frames received in closed state is a |
| 2412 | * bit hackish because the spec makes strong differences between |
| 2413 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2414 | * in both directions. In addition to this, the creation of a |
| 2415 | * new stream reusing the identifier of a closed one will be |
| 2416 | * detected here. Given that we cannot keep track of all closed |
| 2417 | * streams forever, we consider that unknown closed streams were |
| 2418 | * closed on RST received, which allows us to respond with an |
| 2419 | * RST without breaking the connection (eg: to abort a transfer). |
| 2420 | * Some frames have to be silently ignored as well. |
| 2421 | */ |
| 2422 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2423 | if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2424 | /* #5.1.1: The identifier of a newly |
| 2425 | * established stream MUST be numerically |
| 2426 | * greater than all streams that the initiating |
| 2427 | * endpoint has opened or reserved. This |
| 2428 | * governs streams that are opened using a |
| 2429 | * HEADERS frame and streams that are reserved |
| 2430 | * using PUSH_PROMISE. An endpoint that |
| 2431 | * receives an unexpected stream identifier |
| 2432 | * MUST respond with a connection error. |
| 2433 | */ |
| 2434 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2435 | goto strm_err; |
| 2436 | } |
| 2437 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2438 | if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2439 | /* RFC7540#5.1:closed: an endpoint that |
| 2440 | * receives any frame other than PRIORITY after |
| 2441 | * receiving a RST_STREAM MUST treat that as a |
| 2442 | * stream error of type STREAM_CLOSED. |
| 2443 | * |
| 2444 | * Note that old streams fall into this category |
| 2445 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2446 | * |
| 2447 | * However, we cannot generalize this to all frame types. Those |
| 2448 | * carrying compression state must still be processed before |
| 2449 | * being dropped or we'll desynchronize the decoder. This can |
| 2450 | * happen with request trailers received after sending an |
| 2451 | * RST_STREAM, or with header/trailers responses received after |
| 2452 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2453 | */ |
| 2454 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2455 | h2c->st0 = H2_CS_FRAME_E; |
| 2456 | goto strm_err; |
| 2457 | } |
| 2458 | |
| 2459 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2460 | * result of sending a RST_STREAM frame, the peer that |
| 2461 | * receives the RST_STREAM might have already sent |
| 2462 | * frames on the stream that cannot be withdrawn. An |
| 2463 | * endpoint MUST ignore frames that it receives on |
| 2464 | * closed streams after it has sent a RST_STREAM |
| 2465 | * frame. An endpoint MAY choose to limit the period |
| 2466 | * over which it ignores frames and treat frames that |
| 2467 | * arrive after this time as being in error. |
| 2468 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2469 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2470 | /* RFC7540#5.1:closed: any frame other than |
| 2471 | * PRIO/WU/RST in this state MUST be treated as |
| 2472 | * a connection error |
| 2473 | */ |
| 2474 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2475 | h2c->dft != H2_FT_PRIORITY && |
| 2476 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2477 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2478 | goto strm_err; |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2483 | #if 0 |
| 2484 | // problem below: it is not possible to completely ignore such |
| 2485 | // streams as we need to maintain the compression state as well |
| 2486 | // and for this we need to completely process these frames (eg: |
| 2487 | // HEADERS frames) as well as counting DATA frames to emit |
| 2488 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2489 | // This is a typical case of layer violation where the |
| 2490 | // transported contents are critical to the connection's |
| 2491 | // validity and must be ignored at the same time :-( |
| 2492 | |
| 2493 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2494 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2495 | */ |
| 2496 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2497 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2498 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2499 | h2c->dfl -= ret; |
| 2500 | ret = h2c->dfl == 0; |
| 2501 | goto strm_err; |
| 2502 | } |
| 2503 | #endif |
| 2504 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2505 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2506 | case H2_FT_SETTINGS: |
| 2507 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2508 | ret = h2c_handle_settings(h2c); |
| 2509 | |
| 2510 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2511 | ret = h2c_ack_settings(h2c); |
| 2512 | break; |
| 2513 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2514 | case H2_FT_PING: |
| 2515 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2516 | ret = h2c_handle_ping(h2c); |
| 2517 | |
| 2518 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2519 | ret = h2c_ack_ping(h2c); |
| 2520 | break; |
| 2521 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2522 | case H2_FT_WINDOW_UPDATE: |
| 2523 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2524 | ret = h2c_handle_window_update(h2c, h2s); |
| 2525 | break; |
| 2526 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2527 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2528 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2529 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2530 | * frames' parsers consume all following CONTINUATION |
| 2531 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2532 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2533 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2534 | sess_log(h2c->conn->owner); |
| 2535 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2536 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2537 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2538 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2539 | if (h2c->flags & H2_CF_IS_BACK) |
| 2540 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2541 | else |
| 2542 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2543 | if (tmp_h2s) { |
| 2544 | h2s = tmp_h2s; |
| 2545 | ret = 1; |
| 2546 | } |
| 2547 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2548 | break; |
| 2549 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2550 | case H2_FT_DATA: |
| 2551 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2552 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2553 | |
| 2554 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2555 | ret = h2c_send_strm_wu(h2c); |
| 2556 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2557 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2558 | case H2_FT_PRIORITY: |
| 2559 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2560 | ret = h2c_handle_priority(h2c); |
| 2561 | break; |
| 2562 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2563 | case H2_FT_RST_STREAM: |
| 2564 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2565 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2566 | break; |
| 2567 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2568 | case H2_FT_GOAWAY: |
| 2569 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2570 | ret = h2c_handle_goaway(h2c); |
| 2571 | break; |
| 2572 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2573 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2574 | default: |
| 2575 | /* drop frames that we ignore. They may be larger than |
| 2576 | * the buffer so we drain all of their contents until |
| 2577 | * we reach the end. |
| 2578 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2579 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2580 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2581 | h2c->dfl -= ret; |
| 2582 | ret = h2c->dfl == 0; |
| 2583 | } |
| 2584 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2585 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2586 | /* We may have to send an RST if not done yet */ |
| 2587 | if (h2s->st == H2_SS_ERROR) |
| 2588 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2589 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2590 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2591 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2592 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2593 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2594 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2595 | break; |
| 2596 | |
| 2597 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2598 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2599 | h2c->st0 = H2_CS_FRAME_H; |
| 2600 | } |
| 2601 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2602 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2603 | if (h2c->rcvd_c > 0 && |
| 2604 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2605 | h2c_send_conn_wu(h2c); |
| 2606 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2607 | fail: |
| 2608 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2609 | if (h2s && h2s->cs && |
| 2610 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2611 | conn_xprt_read0_pending(h2c->conn) || |
| 2612 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2613 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2614 | (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] | 2615 | /* we may have to signal the upper layers */ |
| 2616 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2617 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2618 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2619 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2620 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2624 | * the end. |
| 2625 | */ |
| 2626 | static int h2_process_mux(struct h2c *h2c) |
| 2627 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2628 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2630 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2631 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2632 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2633 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2634 | if (h2c->st0 == H2_CS_ERROR) { |
| 2635 | h2c->st0 = H2_CS_ERROR2; |
| 2636 | sess_log(h2c->conn->owner); |
| 2637 | } |
| 2638 | goto fail; |
| 2639 | } |
| 2640 | h2c->st0 = H2_CS_SETTINGS1; |
| 2641 | } |
| 2642 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2643 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2644 | return 1; |
| 2645 | } |
| 2646 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2647 | /* start by sending possibly pending window updates */ |
| 2648 | if (h2c->rcvd_c > 0 && |
| 2649 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2650 | h2c_send_conn_wu(h2c) < 0) |
| 2651 | goto fail; |
| 2652 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2653 | /* First we always process the flow control list because the streams |
| 2654 | * waiting there were already elected for immediate emission but were |
| 2655 | * blocked just on this. |
| 2656 | */ |
| 2657 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2658 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2659 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2660 | h2c->st0 >= H2_CS_ERROR) |
| 2661 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2663 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2664 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2665 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2666 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2667 | /* For some reason, the upper layer failed to subsribe again, |
| 2668 | * so remove it from the send_list |
| 2669 | */ |
| 2670 | if (!h2s->send_wait) { |
| 2671 | LIST_DEL_INIT(&h2s->list); |
| 2672 | continue; |
| 2673 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2674 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2675 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2676 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2679 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2680 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2681 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2682 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2683 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2684 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2685 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2686 | /* For some reason, the upper layer failed to subsribe again, |
| 2687 | * so remove it from the send_list |
| 2688 | */ |
| 2689 | if (!h2s->send_wait) { |
| 2690 | LIST_DEL_INIT(&h2s->list); |
| 2691 | continue; |
| 2692 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2693 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2694 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2695 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2696 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2697 | } |
| 2698 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2699 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2700 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2701 | if (h2c->st0 == H2_CS_ERROR) { |
| 2702 | if (h2c->max_id >= 0) { |
| 2703 | h2c_send_goaway_error(h2c, NULL); |
| 2704 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
| 2708 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2709 | } |
| 2710 | return 1; |
| 2711 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2712 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2715 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2716 | /* Attempt to read data, and subscribe if none available. |
| 2717 | * The function returns 1 if data has been received, otherwise zero. |
| 2718 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2719 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2720 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2721 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2722 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2723 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2724 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2726 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2727 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2728 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2729 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2730 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2731 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2732 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2733 | if (!buf) { |
| 2734 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2735 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2736 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2737 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2738 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2739 | b_realign_if_empty(buf); |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 2740 | if (!b_data(buf)) { |
| 2741 | /* try to pre-align the buffer like the |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2742 | * rxbufs will be to optimize memory copies. We'll make |
| 2743 | * sure that the frame header lands at the end of the |
| 2744 | * HTX block to alias it upon recv. We cannot use the |
| 2745 | * head because rcv_buf() will realign the buffer if |
| 2746 | * it's empty. Thus we cheat and pretend we already |
| 2747 | * have a few bytes there. |
| 2748 | */ |
| 2749 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2750 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2751 | } |
| 2752 | else |
| 2753 | max = b_room(buf); |
| 2754 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2755 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2756 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2757 | else |
| 2758 | ret = 0; |
| 2759 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2760 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2761 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2762 | 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] | 2763 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2764 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2765 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2766 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2767 | } |
| 2768 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2769 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2770 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2771 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2774 | /* Try to send data if possible. |
| 2775 | * The function returns 1 if data have been sent, otherwise zero. |
| 2776 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2777 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2778 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2779 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2780 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2781 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2782 | |
| 2783 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2784 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2785 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2787 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2788 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2789 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2792 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2793 | * pending streams into the existing buffer until it's reportedly full |
| 2794 | * or the end of send requests is reached. Then it tries to send this |
| 2795 | * buffer's contents out, marks it not full if at least one byte could |
| 2796 | * be sent, and tries again. |
| 2797 | * |
| 2798 | * The snd_buf() function normally takes a "flags" argument which may |
| 2799 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2800 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2801 | * connection is streaming lots of data (used to increase TLS record |
| 2802 | * size at the expense of latency). The former can be sent any time |
| 2803 | * there's a buffer full flag, as it indicates at least one stream |
| 2804 | * attempted to send and failed so there are pending data. An |
| 2805 | * alternative would be to set it as long as there's an active stream |
| 2806 | * but that would be problematic for ACKs until we have an absolute |
| 2807 | * guarantee that all waiters have at least one byte to send. The |
| 2808 | * latter should possibly not be set for now. |
| 2809 | */ |
| 2810 | |
| 2811 | done = 0; |
| 2812 | while (!done) { |
| 2813 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2814 | unsigned int released = 0; |
| 2815 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2816 | |
| 2817 | /* fill as much as we can into the current buffer */ |
| 2818 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2819 | done = h2_process_mux(h2c); |
| 2820 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2821 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2822 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2823 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2824 | if (conn->flags & CO_FL_ERROR) |
| 2825 | break; |
| 2826 | |
| 2827 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2828 | flags |= CO_SFL_MSG_MORE; |
| 2829 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2830 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2831 | if (b_data(buf)) { |
| 2832 | 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] | 2833 | if (!ret) { |
| 2834 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2835 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2836 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2837 | sent = 1; |
| 2838 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2839 | if (b_data(buf)) { |
| 2840 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2841 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2842 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2843 | } |
| 2844 | b_free(buf); |
| 2845 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2846 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2847 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2848 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2849 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2850 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2851 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2852 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2853 | } |
| 2854 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2855 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2856 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2857 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2858 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2859 | /* We're not full anymore, so we can wake any task that are waiting |
| 2860 | * for us. |
| 2861 | */ |
| 2862 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2863 | struct h2s *h2s; |
| 2864 | |
| 2865 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2866 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2867 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2868 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2869 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2870 | continue; |
| 2871 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2872 | /* For some reason, the upper layer failed to subsribe again, |
| 2873 | * so remove it from the send_list |
| 2874 | */ |
| 2875 | if (!h2s->send_wait) { |
| 2876 | LIST_DEL_INIT(&h2s->list); |
| 2877 | continue; |
| 2878 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2879 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2880 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2881 | tasklet_wakeup(h2s->send_wait->tasklet); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2882 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2883 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2884 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2885 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2886 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2887 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2888 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2889 | 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] | 2890 | 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] | 2891 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2892 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2895 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2896 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2897 | { |
| 2898 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2899 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2900 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2901 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2902 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2903 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2904 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2905 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2906 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2907 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2908 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2909 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2910 | /* callback called on any event by the connection handler. |
| 2911 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2912 | * destruction of the connection (which normally doesn not happen in h2). |
| 2913 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2914 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2915 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2916 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2918 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2919 | h2_process_demux(h2c); |
| 2920 | |
| 2921 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2922 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2923 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2924 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2925 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2926 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2927 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2929 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2930 | /* frontend is stopping, reload likely in progress, let's try |
| 2931 | * to announce a graceful shutdown if not yet done. We don't |
| 2932 | * care if it fails, it will be tried again later. |
| 2933 | */ |
| 2934 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2935 | if (h2c->last_sid < 0) |
| 2936 | h2c->last_sid = (1U << 31) - 1; |
| 2937 | h2c_send_goaway_error(h2c, NULL); |
| 2938 | } |
| 2939 | } |
| 2940 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2941 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2942 | * If we received early data, and the handshake is done, wake |
| 2943 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2944 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2945 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2946 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2947 | struct eb32_node *node; |
| 2948 | struct h2s *h2s; |
| 2949 | |
| 2950 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2951 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2952 | |
| 2953 | while (node) { |
| 2954 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2955 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2956 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2957 | node = eb32_next(node); |
| 2958 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2959 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2960 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2961 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2962 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2963 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2964 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 2965 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2966 | |
| 2967 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2968 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2969 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2970 | return -1; |
| 2971 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2974 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2975 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2976 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2977 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2978 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2979 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2980 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2981 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2982 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 2983 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2984 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2985 | if (h2c->task) { |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 2986 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 2987 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2988 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2989 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2990 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2991 | return 0; |
| 2992 | } |
| 2993 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2994 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2995 | static int h2_wake(struct connection *conn) |
| 2996 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2997 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2998 | |
| 2999 | return (h2_process(h2c)); |
| 3000 | } |
| 3001 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3002 | /* Connection timeout management. The principle is that if there's no receipt |
| 3003 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3004 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3005 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3006 | * GOAWAY frame. |
| 3007 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3008 | 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] | 3009 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3010 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3011 | int expired = tick_is_expired(t->expire, now_ms); |
| 3012 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3013 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3014 | return t; |
| 3015 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3016 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3017 | |
| 3018 | if (!h2c) { |
| 3019 | /* resources were already deleted */ |
| 3020 | return NULL; |
| 3021 | } |
| 3022 | |
| 3023 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3024 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3025 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3026 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3027 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3028 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3029 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3030 | } |
| 3031 | |
| 3032 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3033 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3034 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3035 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3036 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3037 | 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] | 3038 | unsigned int released = 0; |
| 3039 | struct buffer *buf; |
| 3040 | |
| 3041 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3042 | if (b_data(buf)) { |
| 3043 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3044 | if (!ret) |
| 3045 | break; |
| 3046 | b_del(buf, ret); |
| 3047 | if (b_data(buf)) |
| 3048 | break; |
| 3049 | b_free(buf); |
| 3050 | released++; |
| 3051 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3052 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3053 | |
| 3054 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3055 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3056 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3057 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3058 | /* either we can release everything now or it will be done later once |
| 3059 | * the last stream closes. |
| 3060 | */ |
| 3061 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3062 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3063 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3064 | return NULL; |
| 3065 | } |
| 3066 | |
| 3067 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3068 | /*******************************************/ |
| 3069 | /* functions below are used by the streams */ |
| 3070 | /*******************************************/ |
| 3071 | |
| 3072 | /* |
| 3073 | * Attach a new stream to a connection |
| 3074 | * (Used for outgoing connections) |
| 3075 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3076 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3077 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3078 | struct conn_stream *cs; |
| 3079 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3080 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3081 | |
| 3082 | cs = cs_new(conn); |
| 3083 | if (!cs) |
| 3084 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3085 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3086 | if (!h2s) { |
| 3087 | cs_free(cs); |
| 3088 | return NULL; |
| 3089 | } |
| 3090 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3091 | } |
| 3092 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3093 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3094 | * We have to scan because we may have some orphan streams. It might be |
| 3095 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3096 | * orphans. |
| 3097 | */ |
| 3098 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3099 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3100 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3101 | struct h2s *h2s; |
| 3102 | struct eb32_node *node; |
| 3103 | |
| 3104 | node = eb32_first(&h2c->streams_by_id); |
| 3105 | while (node) { |
| 3106 | h2s = container_of(node, struct h2s, by_id); |
| 3107 | if (h2s->cs) |
| 3108 | return h2s->cs; |
| 3109 | node = eb32_next(node); |
| 3110 | } |
| 3111 | return NULL; |
| 3112 | } |
| 3113 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3114 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3115 | * Destroy the mux and the associated connection, if it is no longer used |
| 3116 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3117 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3118 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3119 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3120 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3121 | 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] | 3122 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3126 | * Detach the stream from the connection and possibly release the connection. |
| 3127 | */ |
| 3128 | static void h2_detach(struct conn_stream *cs) |
| 3129 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3130 | struct h2s *h2s = cs->ctx; |
| 3131 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3132 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3133 | |
| 3134 | cs->ctx = NULL; |
| 3135 | if (!h2s) |
| 3136 | return; |
| 3137 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3138 | /* 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] | 3139 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3140 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3141 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3142 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3143 | /* |
| 3144 | * At this point, the stream_interface is supposed to have called |
| 3145 | * h2_unsubscribe(), so the only way there's still a |
| 3146 | * subscription that came from the stream_interface (as we |
| 3147 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3148 | * without the stream_interface involved) is that we subscribed |
| 3149 | * for sending, we woke the tasklet up and removed the |
| 3150 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3151 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3152 | * run yet. Make sure to handle that by explicitely setting |
| 3153 | * send_wait to NULL, as nothing else will do it for us. |
| 3154 | */ |
| 3155 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3156 | } |
| 3157 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3158 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3159 | h2c = h2s->h2c; |
| 3160 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3161 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3162 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3163 | !h2_frt_has_too_many_cs(h2c)) { |
| 3164 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3165 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3166 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3167 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3168 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3169 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3170 | * an ES or RST frame), so orphan it in this case. |
| 3171 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3172 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3173 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3174 | (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] | 3175 | return; |
| 3176 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3177 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3178 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3179 | /* unblock the connection if it was blocked on this |
| 3180 | * stream. |
| 3181 | */ |
| 3182 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3183 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3184 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3185 | } |
| 3186 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3187 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3188 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3189 | if (h2c->flags & H2_CF_IS_BACK) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3190 | if (!(h2c->conn->flags & |
| 3191 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3192 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3193 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3194 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3195 | h2c->conn->owner = NULL; |
| 3196 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3197 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3198 | /* The server doesn't want it, let's kill the connection right away */ |
| 3199 | h2c->conn->mux->destroy(h2c->conn); |
| 3200 | return; |
| 3201 | } |
| 3202 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3203 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3204 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3205 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3206 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3207 | return; |
| 3208 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3209 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3210 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3211 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3212 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3213 | struct server *srv = objt_server(h2c->conn->target); |
| 3214 | |
| 3215 | if (srv) { |
| 3216 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3217 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3218 | else |
| 3219 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3220 | } |
| 3221 | |
| 3222 | } |
| 3223 | } |
| 3224 | } |
| 3225 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3226 | /* We don't want to close right now unless we're removing the |
| 3227 | * last stream, and either the connection is in error, or it |
| 3228 | * reached the ID already specified in a GOAWAY frame received |
| 3229 | * or sent (as seen by last_sid >= 0). |
| 3230 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3231 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3232 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3233 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3234 | } |
| 3235 | else if (h2c->task) { |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3236 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3237 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3238 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3241 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3242 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3243 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3244 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3245 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3246 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3247 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3248 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3249 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3250 | /* a connstream may require us to immediately kill the whole connection |
| 3251 | * for example because of a "tcp-request content reject" rule that is |
| 3252 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3253 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3254 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3255 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3256 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3257 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3258 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3259 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3260 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3261 | /* Nothing was never sent for this stream, so reset with |
| 3262 | * REFUSED_STREAM error to let the client retry the |
| 3263 | * request. |
| 3264 | */ |
| 3265 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3266 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3267 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3268 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3269 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3270 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3271 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3272 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3273 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3274 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3275 | done: |
| 3276 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3277 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3278 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3279 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3280 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3281 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3282 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3283 | h2s->send_wait = sw; |
| 3284 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3285 | h2s->send_wait = sw; |
| 3286 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3287 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3288 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3289 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3290 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3291 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3294 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3295 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3296 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3297 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3298 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3299 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3300 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3301 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3302 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3303 | if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR && |
| 3304 | (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3305 | /* we can cleanly close using an empty data frame only after headers */ |
| 3306 | |
| 3307 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3308 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3309 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3310 | |
| 3311 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3312 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3313 | else |
| 3314 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3315 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3316 | /* a connstream may require us to immediately kill the whole connection |
| 3317 | * for example because of a "tcp-request content reject" rule that is |
| 3318 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3319 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3320 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3321 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3322 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3323 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3324 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3325 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3326 | else { |
| 3327 | /* Nothing was never sent for this stream, so reset with |
| 3328 | * REFUSED_STREAM error to let the client retry the |
| 3329 | * request. |
| 3330 | */ |
| 3331 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3332 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3333 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3334 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3335 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3336 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3337 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3338 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3339 | } |
| 3340 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3341 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3342 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3343 | done: |
| 3344 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3345 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3346 | |
| 3347 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3348 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3349 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3350 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3351 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3352 | h2s->send_wait = sw; |
| 3353 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3354 | h2s->send_wait = sw; |
| 3355 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3356 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3357 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3358 | /* let the handler know we want to shutw */ |
| 3359 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3360 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3363 | /* 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] | 3364 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3365 | * and prevented the last frame from being emitted. |
| 3366 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3367 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3368 | { |
| 3369 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3370 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3371 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3372 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3373 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3374 | h2_do_shutw(h2s); |
| 3375 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3376 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3377 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3378 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3379 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3380 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3381 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3382 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3383 | if (!h2s->cs) { |
| 3384 | h2s_destroy(h2s); |
| 3385 | if (h2c_is_dead(h2c)) |
| 3386 | h2_release(h2c); |
| 3387 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3390 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3391 | } |
| 3392 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3393 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3394 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3395 | { |
| 3396 | struct h2s *h2s = cs->ctx; |
| 3397 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3398 | if (cs->flags & CS_FL_KILL_CONN) |
| 3399 | h2s->flags |= H2_SF_KILL_CONN; |
| 3400 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3401 | if (!mode) |
| 3402 | return; |
| 3403 | |
| 3404 | h2_do_shutr(h2s); |
| 3405 | } |
| 3406 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3407 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3408 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3409 | { |
| 3410 | struct h2s *h2s = cs->ctx; |
| 3411 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3412 | if (cs->flags & CS_FL_KILL_CONN) |
| 3413 | h2s->flags |= H2_SF_KILL_CONN; |
| 3414 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3415 | h2_do_shutw(h2s); |
| 3416 | } |
| 3417 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3418 | /* Decode the payload of a HEADERS frame and produce the HTX request or response |
| 3419 | * depending on the connection's side. Returns a positive value on success, a |
| 3420 | * negative value on failure, or 0 if it couldn't proceed. May report connection |
| 3421 | * errors in h2c->errcode if the frame is non-decodable and the connection |
| 3422 | * unrecoverable. In absence of connection error when a failure is reported, the |
| 3423 | * caller must assume a stream error. |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3424 | * |
| 3425 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3426 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3427 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3428 | * leaving a hole between the main frame and the beginning of the next one. |
| 3429 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3430 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3431 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3432 | * |
| 3433 | * A buffer at the beginning of processing may look like this : |
| 3434 | * |
| 3435 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3436 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3437 | * `---^---------^-----^--------------^--------------^------^---' |
| 3438 | * | | <-----> | | |
| 3439 | * area | dpl | wrap |
| 3440 | * |<--------------> | |
| 3441 | * | dfl | |
| 3442 | * |<-------------------------------------------------->| |
| 3443 | * head data |
| 3444 | * |
| 3445 | * Padding is automatically overwritten when folding, participating to the |
| 3446 | * hole size after dfl : |
| 3447 | * |
| 3448 | * ,---.------------------------.-----.--------------.------.---. |
| 3449 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3450 | * `---^------------------------^-----^--------------^------^---' |
| 3451 | * | | <-----> | | |
| 3452 | * area | hole | wrap |
| 3453 | * |<-----------------------> | |
| 3454 | * | dfl | |
| 3455 | * |<-------------------------------------------------->| |
| 3456 | * head data |
| 3457 | * |
| 3458 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3459 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3460 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3461 | * |
| 3462 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3463 | * so that the function can update the following flags : |
| 3464 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3465 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3466 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3467 | * |
| 3468 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3469 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3470 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3471 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3472 | 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] | 3473 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3474 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3475 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3476 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3477 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3478 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3479 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3480 | int flen; // header frame len |
| 3481 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3482 | int ret = 0; |
| 3483 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3484 | int wrap; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3485 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3486 | next_frame: |
| 3487 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3488 | goto leave; // incomplete input frame |
| 3489 | |
| 3490 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3491 | * this case, we'll try to paste it immediately after the initial |
| 3492 | * HEADERS frame payload and kill any possible padding. The initial |
| 3493 | * frame's length will be increased to represent the concatenation |
| 3494 | * of the two frames. The next frame is read from position <tlen> |
| 3495 | * and written at position <flen> (minus padding if some is present). |
| 3496 | */ |
| 3497 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3498 | struct h2_fh hdr; |
| 3499 | int clen; // CONTINUATION frame's payload length |
| 3500 | |
| 3501 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3502 | /* no more data, the buffer may be full, either due to |
| 3503 | * too large a frame or because of too large a hole that |
| 3504 | * we're going to compact at the end. |
| 3505 | */ |
| 3506 | goto leave; |
| 3507 | } |
| 3508 | |
| 3509 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3510 | /* RFC7540#6.10: frame of unexpected type */ |
| 3511 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3512 | goto fail; |
| 3513 | } |
| 3514 | |
| 3515 | if (hdr.sid != h2c->dsi) { |
| 3516 | /* RFC7540#6.10: frame of different stream */ |
| 3517 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3518 | goto fail; |
| 3519 | } |
| 3520 | |
| 3521 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3522 | /* RFC7540#4.2: invalid frame length */ |
| 3523 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3524 | goto fail; |
| 3525 | } |
| 3526 | |
| 3527 | /* detect when we must stop aggragating frames */ |
| 3528 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3529 | |
| 3530 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3531 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3532 | if (clen > hdr.len) |
| 3533 | clen = hdr.len; |
| 3534 | |
| 3535 | /* Move the frame's payload over the padding, hole and frame |
| 3536 | * header. At least one of hole or dpl is null (see diagrams |
| 3537 | * above). The hole moves after the new aggragated frame. |
| 3538 | */ |
| 3539 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3540 | h2c->dfl += clen - h2c->dpl; |
| 3541 | hole += h2c->dpl + 9; |
| 3542 | h2c->dpl = 0; |
| 3543 | goto next_frame; |
| 3544 | } |
| 3545 | |
| 3546 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3547 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3548 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3549 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3550 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3551 | copy = alloc_trash_chunk(); |
| 3552 | if (!copy) { |
| 3553 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3554 | goto fail; |
| 3555 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3556 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3557 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3558 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3559 | } |
| 3560 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3561 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3562 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3563 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3564 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3565 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3566 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3567 | } |
| 3568 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3569 | if (flen < 5) { |
| 3570 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3571 | goto fail; |
| 3572 | } |
| 3573 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3574 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3575 | flen -= 5; |
| 3576 | } |
| 3577 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3578 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3579 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3580 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3581 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3582 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3583 | /* we can't retry a failed decompression operation so we must be very |
| 3584 | * careful not to take any risks. In practice the output buffer is |
| 3585 | * always empty except maybe for trailers, in which case we simply have |
| 3586 | * to wait for the upper layer to finish consuming what is available. |
| 3587 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3588 | htx = htx_from_buf(rxbuf); |
| 3589 | if (!htx_is_empty(htx)) { |
| 3590 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3591 | goto leave; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3592 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3593 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3594 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3595 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3596 | sizeof(list)/sizeof(list[0]), tmp); |
| 3597 | if (outlen < 0) { |
| 3598 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3599 | goto fail; |
| 3600 | } |
| 3601 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3602 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3603 | * the parser's state to commit these changes and allow us to later |
| 3604 | * fail solely on the stream if needed. |
| 3605 | */ |
| 3606 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3607 | h2c->dfl = hole = 0; |
| 3608 | h2c->st0 = H2_CS_FRAME_H; |
| 3609 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3610 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3611 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3612 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3613 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3614 | goto trailers; |
| 3615 | |
| 3616 | /* This is the first HEADERS frame so it's a headers block */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3617 | if (h2c->flags & H2_CF_IS_BACK) |
| 3618 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
| 3619 | else |
| 3620 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3621 | |
| 3622 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3623 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3624 | goto fail; |
| 3625 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3626 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3627 | if (msgf & H2_MSGF_BODY) { |
| 3628 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3629 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3630 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3631 | htx->extra = *body_len; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3632 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3633 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3634 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3635 | } |
| 3636 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3637 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3638 | /* indicate that a HEADERS frame was received for this stream, except |
| 3639 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3640 | * expected. |
| 3641 | */ |
| 3642 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3643 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3644 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3645 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3646 | /* Mark the end of message using EOM */ |
| 3647 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3648 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3649 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3650 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3651 | /* success */ |
| 3652 | ret = 1; |
| 3653 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3654 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3655 | /* 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] | 3656 | * move the remaining data over it. |
| 3657 | */ |
| 3658 | if (hole) { |
| 3659 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3660 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3661 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3662 | b_sub(&h2c->dbuf, hole); |
| 3663 | } |
| 3664 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3665 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3666 | /* too large frames */ |
| 3667 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3668 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3669 | } |
| 3670 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3671 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3672 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3673 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3674 | return ret; |
| 3675 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3676 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3677 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3678 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3679 | |
| 3680 | trailers: |
| 3681 | /* This is the last HEADERS frame hence a trailer */ |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3682 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3683 | /* It's a trailer but it's missing ES flag */ |
| 3684 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3685 | goto fail; |
| 3686 | } |
| 3687 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3688 | /* Trailers terminate a DATA sequence */ |
| 3689 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3690 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3691 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3692 | } |
| 3693 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3694 | /* 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] | 3695 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3696 | * send the current frame, 0 if it couldn't complete, in which case |
| 3697 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3698 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3699 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3700 | * have checked the frame header and ensured that the frame was complete or the |
| 3701 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3702 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3703 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3704 | { |
| 3705 | struct h2c *h2c = h2s->h2c; |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3706 | int block; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3707 | unsigned int flen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3708 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3709 | struct buffer *csbuf; |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3710 | unsigned int sent; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3711 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3712 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3713 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3714 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3715 | if (!csbuf) { |
| 3716 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3717 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3718 | } |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3719 | htx = htx_from_buf(csbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3720 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3721 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3722 | flen = h2c->dfl - h2c->dpl; |
| 3723 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3724 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3725 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3726 | if (flen > b_data(&h2c->dbuf)) { |
| 3727 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3728 | if (!flen) |
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 | } |
| 3731 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3732 | block = htx_free_data_space(htx); |
| 3733 | if (!block) { |
| 3734 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3735 | goto fail; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3736 | } |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3737 | if (flen > block) |
| 3738 | flen = block; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3739 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3740 | /* here, flen is the max we can copy into the output buffer */ |
| 3741 | block = b_contig_data(&h2c->dbuf, 0); |
| 3742 | if (flen > block) |
| 3743 | flen = block; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3744 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3745 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3746 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3747 | b_del(&h2c->dbuf, sent); |
| 3748 | h2c->dfl -= sent; |
| 3749 | h2c->rcvd_c += sent; |
| 3750 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3751 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3752 | if (h2s->flags & H2_SF_DATA_CLEN) { |
| 3753 | h2s->body_len -= sent; |
| 3754 | htx->extra = h2s->body_len; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3755 | } |
| 3756 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3757 | if (sent < flen) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3758 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3759 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3760 | } |
| 3761 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3762 | goto try_again; |
| 3763 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3764 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3765 | /* here we're done with the frame, all the payload (except padding) was |
| 3766 | * transferred. |
| 3767 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3768 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3769 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3770 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3771 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3772 | goto fail; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3773 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3774 | } |
| 3775 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3776 | h2c->rcvd_c += h2c->dpl; |
| 3777 | h2c->rcvd_s += h2c->dpl; |
| 3778 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3779 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3780 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3781 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3782 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3783 | if (htx) |
| 3784 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3785 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3786 | } |
| 3787 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3788 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 3789 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 3790 | * must check the stream's status to detect any error which might have happened |
| 3791 | * subsequently to a successful send. The htx blocks are automatically removed |
| 3792 | * from the message. The htx message is assumed to be valid since produced from |
| 3793 | * the internal code, hence it contains a start line, an optional series of |
| 3794 | * header blocks and an end of header, otherwise an invalid frame could be |
| 3795 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 3796 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 3797 | 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] | 3798 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3799 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3800 | struct h2c *h2c = h2s->h2c; |
| 3801 | struct htx_blk *blk; |
| 3802 | struct htx_blk *blk_end; |
| 3803 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3804 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3805 | struct htx_sl *sl; |
| 3806 | enum htx_blk_type type; |
| 3807 | int es_now = 0; |
| 3808 | int ret = 0; |
| 3809 | int hdr; |
| 3810 | int idx; |
| 3811 | |
| 3812 | if (h2c_mux_busy(h2c, h2s)) { |
| 3813 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3814 | return 0; |
| 3815 | } |
| 3816 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3817 | /* determine the first block which must not be deleted, blk_end may |
| 3818 | * be NULL if all blocks have to be deleted. |
| 3819 | */ |
| 3820 | idx = htx_get_head(htx); |
| 3821 | blk_end = NULL; |
| 3822 | while (idx != -1) { |
| 3823 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 3824 | idx = htx_get_next(htx, idx); |
| 3825 | if (type == HTX_BLK_EOH) { |
| 3826 | if (idx != -1) |
| 3827 | blk_end = htx_get_blk(htx, idx); |
| 3828 | break; |
| 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 3833 | blk = htx_get_head_blk(htx); |
| 3834 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
| 3835 | ALREADY_CHECKED(blk); |
| 3836 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3837 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3838 | if (h2s->status < 100 || h2s->status > 999) |
| 3839 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3840 | |
| 3841 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 3842 | hdr = 0; |
| 3843 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 3844 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3845 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 3846 | blk = htx_get_blk(htx, idx); |
| 3847 | type = htx_get_blk_type(blk); |
| 3848 | |
| 3849 | if (type == HTX_BLK_UNUSED) |
| 3850 | continue; |
| 3851 | |
| 3852 | if (type != HTX_BLK_HDR) |
| 3853 | break; |
| 3854 | |
| 3855 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 3856 | goto fail; |
| 3857 | |
| 3858 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 3859 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3860 | hdr++; |
| 3861 | } |
| 3862 | |
| 3863 | /* marker for end of headers */ |
| 3864 | list[hdr].n = ist(""); |
| 3865 | |
| 3866 | if (h2s->status == 204 || h2s->status == 304) { |
| 3867 | /* no contents, claim c-len is present and set to zero */ |
| 3868 | es_now = 1; |
| 3869 | } |
| 3870 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 3871 | mbuf = br_tail(h2c->mbuf); |
| 3872 | retry: |
| 3873 | if (!h2_get_buf(h2c, mbuf)) { |
| 3874 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3875 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3876 | return 0; |
| 3877 | } |
| 3878 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3879 | chunk_reset(&outbuf); |
| 3880 | |
| 3881 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3882 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 3883 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3884 | break; |
| 3885 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3886 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | if (outbuf.size < 9) |
| 3890 | goto full; |
| 3891 | |
| 3892 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 3893 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3894 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3895 | outbuf.data = 9; |
| 3896 | |
| 3897 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3898 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3899 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3900 | goto realign_again; |
| 3901 | goto full; |
| 3902 | } |
| 3903 | |
| 3904 | /* encode all headers, stop at empty name */ |
| 3905 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 3906 | /* these ones do not exist in H2 and must be dropped. */ |
| 3907 | if (isteq(list[hdr].n, ist("connection")) || |
| 3908 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3909 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3910 | isteq(list[hdr].n, ist("upgrade")) || |
| 3911 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 3912 | continue; |
| 3913 | |
| 3914 | if (isteq(list[hdr].n, ist(""))) |
| 3915 | break; // end |
| 3916 | |
| 3917 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3918 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3919 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3920 | goto realign_again; |
| 3921 | goto full; |
| 3922 | } |
| 3923 | } |
| 3924 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3925 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3926 | * FIXME: we should also set it when we know for sure that the |
| 3927 | * content-length is zero as well as on 204/304 |
| 3928 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3929 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 3930 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3931 | es_now = 1; |
| 3932 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3933 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3934 | es_now = 1; |
| 3935 | |
| 3936 | /* update the frame's size */ |
| 3937 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 3938 | |
| 3939 | if (es_now) |
| 3940 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 3941 | |
| 3942 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3943 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3944 | |
| 3945 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 3946 | * 1xx responses, another HEADERS frame is expected. |
| 3947 | */ |
| 3948 | if (h2s->status >= 200 || h2s->status == 101) |
| 3949 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3950 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3951 | if (es_now) { |
| 3952 | h2s->flags |= H2_SF_ES_SENT; |
| 3953 | if (h2s->st == H2_SS_OPEN) |
| 3954 | h2s->st = H2_SS_HLOC; |
| 3955 | else |
| 3956 | h2s_close(h2s); |
| 3957 | } |
| 3958 | |
| 3959 | /* OK we could properly deliver the response */ |
| 3960 | |
| 3961 | /* remove all header blocks including the EOH and compute the |
| 3962 | * corresponding size. |
| 3963 | * |
| 3964 | * FIXME: We should remove everything when es_now is set. |
| 3965 | */ |
| 3966 | ret = 0; |
| 3967 | idx = htx_get_head(htx); |
| 3968 | blk = htx_get_blk(htx, idx); |
| 3969 | while (blk != blk_end) { |
| 3970 | ret += htx_get_blksz(blk); |
| 3971 | blk = htx_remove_blk(htx, blk); |
| 3972 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 3973 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 3974 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 3975 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 3976 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 3977 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3978 | end: |
| 3979 | return ret; |
| 3980 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 3981 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 3982 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3983 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3984 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3985 | ret = 0; |
| 3986 | goto end; |
| 3987 | fail: |
| 3988 | /* unparsable HTX messages, too large ones to be produced in the local |
| 3989 | * list etc go here (unrecoverable errors). |
| 3990 | */ |
| 3991 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3992 | ret = 0; |
| 3993 | goto end; |
| 3994 | } |
| 3995 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 3996 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 3997 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 3998 | * must check the stream's status to detect any error which might have happened |
| 3999 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4000 | * from the message. The htx message is assumed to be valid since produced from |
| 4001 | * the internal code, hence it contains a start line, an optional series of |
| 4002 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4003 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4004 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4005 | 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] | 4006 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4007 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4008 | struct h2c *h2c = h2s->h2c; |
| 4009 | struct htx_blk *blk; |
| 4010 | struct htx_blk *blk_end; |
| 4011 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4012 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4013 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4014 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4015 | enum htx_blk_type type; |
| 4016 | int es_now = 0; |
| 4017 | int ret = 0; |
| 4018 | int hdr; |
| 4019 | int idx; |
| 4020 | |
| 4021 | if (h2c_mux_busy(h2c, h2s)) { |
| 4022 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4023 | return 0; |
| 4024 | } |
| 4025 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4026 | /* determine the first block which must not be deleted, blk_end may |
| 4027 | * be NULL if all blocks have to be deleted. |
| 4028 | */ |
| 4029 | idx = htx_get_head(htx); |
| 4030 | blk_end = NULL; |
| 4031 | while (idx != -1) { |
| 4032 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4033 | idx = htx_get_next(htx, idx); |
| 4034 | if (type == HTX_BLK_EOH) { |
| 4035 | if (idx != -1) |
| 4036 | blk_end = htx_get_blk(htx, idx); |
| 4037 | break; |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4042 | blk = htx_get_head_blk(htx); |
| 4043 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 4044 | ALREADY_CHECKED(blk); |
| 4045 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4046 | meth = htx_sl_req_meth(sl); |
| 4047 | path = htx_sl_req_uri(sl); |
| 4048 | |
| 4049 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4050 | hdr = 0; |
| 4051 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4052 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4053 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4054 | blk = htx_get_blk(htx, idx); |
| 4055 | type = htx_get_blk_type(blk); |
| 4056 | |
| 4057 | if (type == HTX_BLK_UNUSED) |
| 4058 | continue; |
| 4059 | |
| 4060 | if (type != HTX_BLK_HDR) |
| 4061 | break; |
| 4062 | |
| 4063 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4064 | goto fail; |
| 4065 | |
| 4066 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4067 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4068 | hdr++; |
| 4069 | } |
| 4070 | |
| 4071 | /* marker for end of headers */ |
| 4072 | list[hdr].n = ist(""); |
| 4073 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4074 | mbuf = br_tail(h2c->mbuf); |
| 4075 | retry: |
| 4076 | if (!h2_get_buf(h2c, mbuf)) { |
| 4077 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4078 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4079 | return 0; |
| 4080 | } |
| 4081 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4082 | chunk_reset(&outbuf); |
| 4083 | |
| 4084 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4085 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4086 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4087 | break; |
| 4088 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4089 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | if (outbuf.size < 9) |
| 4093 | goto full; |
| 4094 | |
| 4095 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4096 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4097 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4098 | outbuf.data = 9; |
| 4099 | |
| 4100 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4101 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4102 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4103 | goto realign_again; |
| 4104 | goto full; |
| 4105 | } |
| 4106 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4107 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4108 | * - :authority set to the URI part (host:port) |
| 4109 | * - :method set to CONNECT |
| 4110 | * - :scheme and :path omitted |
| 4111 | */ |
| 4112 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4113 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4114 | struct ist scheme; |
| 4115 | |
| 4116 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4117 | scheme = ist("http"); |
| 4118 | else |
| 4119 | scheme = ist("https"); |
| 4120 | |
| 4121 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4122 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4123 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4124 | goto realign_again; |
| 4125 | goto full; |
| 4126 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4127 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4128 | /* encode the path, which necessarily is the second one */ |
| 4129 | if (!hpack_encode_path(&outbuf, path)) { |
| 4130 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4131 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4132 | goto realign_again; |
| 4133 | goto full; |
| 4134 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4135 | |
| 4136 | /* look for the Host header and place it in :authority */ |
| 4137 | auth = ist2(NULL, 0); |
| 4138 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4139 | if (isteq(list[hdr].n, ist(""))) |
| 4140 | break; // end |
| 4141 | |
| 4142 | if (isteq(list[hdr].n, ist("host"))) { |
| 4143 | auth = list[hdr].v; |
| 4144 | break; |
| 4145 | } |
| 4146 | } |
| 4147 | } |
| 4148 | else { |
| 4149 | /* for CONNECT, :authority is taken from the path */ |
| 4150 | auth = path; |
| 4151 | } |
| 4152 | |
| 4153 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4154 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4155 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4156 | goto realign_again; |
| 4157 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | /* encode all headers, stop at empty name */ |
| 4161 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4162 | /* these ones do not exist in H2 and must be dropped. */ |
| 4163 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4164 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4165 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4166 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4167 | isteq(list[hdr].n, ist("upgrade")) || |
| 4168 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4169 | continue; |
| 4170 | |
| 4171 | if (isteq(list[hdr].n, ist(""))) |
| 4172 | break; // end |
| 4173 | |
| 4174 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4175 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4176 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4177 | goto realign_again; |
| 4178 | goto full; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | /* we may need to add END_STREAM if we have no body : |
| 4183 | * - request already closed, or : |
| 4184 | * - no transfer-encoding, and : |
| 4185 | * - no content-length or content-length:0 |
| 4186 | * Fixme: this doesn't take into account CONNECT requests. |
| 4187 | */ |
| 4188 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4189 | es_now = 1; |
| 4190 | |
| 4191 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4192 | es_now = 1; |
| 4193 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4194 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4195 | es_now = 1; |
| 4196 | |
| 4197 | /* update the frame's size */ |
| 4198 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4199 | |
| 4200 | if (es_now) |
| 4201 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4202 | |
| 4203 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4204 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4205 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4206 | h2s->st = H2_SS_OPEN; |
| 4207 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4208 | if (es_now) { |
| 4209 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4210 | h2s->flags |= H2_SF_ES_SENT; |
| 4211 | h2s->st = H2_SS_HLOC; |
| 4212 | } |
| 4213 | |
| 4214 | /* remove all header blocks including the EOH and compute the |
| 4215 | * corresponding size. |
| 4216 | * |
| 4217 | * FIXME: We should remove everything when es_now is set. |
| 4218 | */ |
| 4219 | ret = 0; |
| 4220 | idx = htx_get_head(htx); |
| 4221 | blk = htx_get_blk(htx, idx); |
| 4222 | while (blk != blk_end) { |
| 4223 | ret += htx_get_blksz(blk); |
| 4224 | blk = htx_remove_blk(htx, blk); |
| 4225 | } |
| 4226 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4227 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4228 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4229 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4230 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4231 | |
| 4232 | end: |
| 4233 | return ret; |
| 4234 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4235 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4236 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4237 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4238 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4239 | ret = 0; |
| 4240 | goto end; |
| 4241 | fail: |
| 4242 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4243 | * list etc go here (unrecoverable errors). |
| 4244 | */ |
| 4245 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4246 | ret = 0; |
| 4247 | goto end; |
| 4248 | } |
| 4249 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4250 | /* 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] | 4251 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4252 | * caller must check the stream's status to detect any error which might have |
| 4253 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4254 | * 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] | 4255 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4256 | 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] | 4257 | { |
| 4258 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4259 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4260 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4261 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4262 | size_t total = 0; |
| 4263 | int es_now = 0; |
| 4264 | int bsize; /* htx block size */ |
| 4265 | int fsize; /* h2 frame size */ |
| 4266 | struct htx_blk *blk; |
| 4267 | enum htx_blk_type type; |
| 4268 | int idx; |
| 4269 | |
| 4270 | if (h2c_mux_busy(h2c, h2s)) { |
| 4271 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4272 | goto end; |
| 4273 | } |
| 4274 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4275 | htx = htx_from_buf(buf); |
| 4276 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4277 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 4278 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 4279 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4280 | */ |
| 4281 | |
| 4282 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4283 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4284 | goto end; |
| 4285 | |
| 4286 | idx = htx_get_head(htx); |
| 4287 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4288 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4289 | bsize = htx_get_blksz(blk); |
| 4290 | fsize = bsize; |
| 4291 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4292 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4293 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4294 | /* ES already sent */ |
| 4295 | htx_remove_blk(htx, blk); |
| 4296 | total++; // EOM counts as one byte |
| 4297 | count--; |
| 4298 | goto end; |
| 4299 | } |
| 4300 | } |
| 4301 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4302 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4303 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4304 | mbuf = br_tail(h2c->mbuf); |
| 4305 | retry: |
| 4306 | if (!h2_get_buf(h2c, mbuf)) { |
| 4307 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4308 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4309 | goto end; |
| 4310 | } |
| 4311 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4312 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4313 | * First, if the mux's buffer is empty and the htx area contains |
| 4314 | * exactly one data block of the same size as the requested count, and |
| 4315 | * this count fits within the frame size, the stream's window size, and |
| 4316 | * the connection's window size, then it's possible to simply swap the |
| 4317 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4318 | * length to match the entire DATA HTX block in the middle. In this |
| 4319 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4320 | * the situation that happens all the time with large files. Second, if |
| 4321 | * this is not possible, but the mux's output buffer is empty, we still |
| 4322 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4323 | * making the intermediary buffer's area point to the output buffer's |
| 4324 | * area. In this case we want to skip the HTX header to make sure that |
| 4325 | * copies remain aligned and that this operation remains possible all |
| 4326 | * the time. This goes for headers, data blocks and any data extracted |
| 4327 | * from the HTX blocks. |
| 4328 | */ |
| 4329 | if (unlikely(fsize == count && |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 4330 | htx_nbblks(htx) == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4331 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4332 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4333 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4334 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4335 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4336 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4337 | * frame into a buffer containing few data if it needs to be realigned, |
| 4338 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4339 | * 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] | 4340 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4341 | if (fsize + 9 <= b_room(mbuf) && |
| 4342 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 4343 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4344 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4345 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4346 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4347 | goto retry; |
| 4348 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4349 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4350 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4351 | goto end; |
| 4352 | } |
| 4353 | |
| 4354 | /* map an H2 frame to the HTX block so that we can put the |
| 4355 | * frame header there. |
| 4356 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4357 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 4358 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4359 | |
| 4360 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4361 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4362 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4363 | h2_set_frame_size(outbuf.area, fsize); |
| 4364 | |
| 4365 | /* update windows */ |
| 4366 | h2s->mws -= fsize; |
| 4367 | h2c->mws -= fsize; |
| 4368 | |
| 4369 | /* and exchange with our old area */ |
| 4370 | buf->area = old_area; |
| 4371 | buf->data = buf->head = 0; |
| 4372 | total += fsize; |
| 4373 | goto end; |
| 4374 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4375 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4376 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4377 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4378 | |
| 4379 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4380 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4381 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4382 | break; |
| 4383 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4384 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4388 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4389 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4390 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4391 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4392 | goto end; |
| 4393 | } |
| 4394 | |
| 4395 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4396 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4397 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4398 | outbuf.data = 9; |
| 4399 | |
| 4400 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4401 | * the HTX buffer. We need to check this against the connection's and |
| 4402 | * the stream's send windows, and to ensure that this fits in the max |
| 4403 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4404 | * the frame header). The connection's flow control is applied last so |
| 4405 | * that we can use a separate list of streams which are immediately |
| 4406 | * unblocked on window opening. Note: we don't implement padding. |
| 4407 | */ |
| 4408 | |
| 4409 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4410 | * empty frame, thus we force it to zero here. |
| 4411 | */ |
| 4412 | if (type == HTX_BLK_EOM) |
| 4413 | bsize = fsize = 0; |
| 4414 | |
| 4415 | if (!fsize) |
| 4416 | goto send_empty; |
| 4417 | |
| 4418 | if (h2s->mws <= 0) { |
| 4419 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4420 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4421 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4422 | goto end; |
| 4423 | } |
| 4424 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4425 | if (fsize > count) |
| 4426 | fsize = count; |
| 4427 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4428 | if (fsize > h2s->mws) |
| 4429 | fsize = h2s->mws; // >0 |
| 4430 | |
| 4431 | if (h2c->mfs && fsize > h2c->mfs) |
| 4432 | fsize = h2c->mfs; // >0 |
| 4433 | |
| 4434 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4435 | /* It doesn't fit at once. If it at least fits once split and |
| 4436 | * the amount of data to move is low, let's defragment the |
| 4437 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4438 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4439 | if (b_space_wraps(mbuf) && |
| 4440 | (fsize + 9 <= b_room(mbuf)) && |
| 4441 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4442 | goto realign_again; |
| 4443 | fsize = outbuf.size - 9; |
| 4444 | |
| 4445 | if (fsize <= 0) { |
| 4446 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4447 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4448 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4449 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4450 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4451 | goto end; |
| 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | if (h2c->mws <= 0) { |
| 4456 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4457 | goto end; |
| 4458 | } |
| 4459 | |
| 4460 | if (fsize > h2c->mws) |
| 4461 | fsize = h2c->mws; |
| 4462 | |
| 4463 | /* now let's copy this this into the output buffer */ |
| 4464 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4465 | h2s->mws -= fsize; |
| 4466 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4467 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4468 | |
| 4469 | send_empty: |
| 4470 | /* update the frame's size */ |
| 4471 | h2_set_frame_size(outbuf.area, fsize); |
| 4472 | |
| 4473 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4474 | * meeting EOM. We should optimize this later. |
| 4475 | */ |
| 4476 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4477 | total++; // EOM counts as one byte |
| 4478 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4479 | es_now = 1; |
| 4480 | } |
| 4481 | |
| 4482 | if (es_now) |
| 4483 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4484 | |
| 4485 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4486 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4487 | |
| 4488 | /* consume incoming HTX block, including EOM */ |
| 4489 | total += fsize; |
| 4490 | if (fsize == bsize) { |
| 4491 | htx_remove_blk(htx, blk); |
| 4492 | if (fsize) |
| 4493 | goto new_frame; |
| 4494 | } else { |
| 4495 | /* we've truncated this block */ |
| 4496 | htx_cut_data_blk(htx, blk, fsize); |
| 4497 | } |
| 4498 | |
| 4499 | if (es_now) { |
| 4500 | if (h2s->st == H2_SS_OPEN) |
| 4501 | h2s->st = H2_SS_HLOC; |
| 4502 | else |
| 4503 | h2s_close(h2s); |
| 4504 | |
| 4505 | h2s->flags |= H2_SF_ES_SENT; |
| 4506 | } |
| 4507 | |
| 4508 | end: |
| 4509 | return total; |
| 4510 | } |
| 4511 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4512 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4513 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4514 | * processed. The caller must check the stream's status to detect any error |
| 4515 | * which might have happened subsequently to a successful send. The htx blocks |
| 4516 | * are automatically removed from the message. The htx message is assumed to be |
| 4517 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 4518 | * 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] | 4519 | * as a single frame. The ES flag is always set. |
| 4520 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4521 | static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4522 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4523 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4524 | struct h2c *h2c = h2s->h2c; |
| 4525 | struct htx_blk *blk; |
| 4526 | struct htx_blk *blk_end; |
| 4527 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4528 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4529 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4530 | int ret = 0; |
| 4531 | int hdr; |
| 4532 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4533 | |
| 4534 | if (h2c_mux_busy(h2c, h2s)) { |
| 4535 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4536 | goto end; |
| 4537 | } |
| 4538 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4539 | /* determine the first block which must not be deleted, blk_end may |
| 4540 | * be NULL if all blocks have to be deleted. also get trailers. |
| 4541 | */ |
| 4542 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4543 | blk_end = NULL; |
| 4544 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4545 | hdr = 0; |
| 4546 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4547 | blk = htx_get_blk(htx, idx); |
| 4548 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4549 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4550 | if (type == HTX_BLK_UNUSED) |
| 4551 | continue; |
| 4552 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4553 | if (type == HTX_BLK_EOT) { |
| 4554 | if (idx != -1) |
| 4555 | blk_end = blk; |
| 4556 | break; |
| 4557 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 4558 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4559 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4560 | |
| 4561 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4562 | goto fail; |
| 4563 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4564 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4565 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 4566 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4567 | } |
| 4568 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4569 | /* marker for end of trailers */ |
| 4570 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4571 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4572 | mbuf = br_tail(h2c->mbuf); |
| 4573 | retry: |
| 4574 | if (!h2_get_buf(h2c, mbuf)) { |
| 4575 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4576 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4577 | goto end; |
| 4578 | } |
| 4579 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4580 | chunk_reset(&outbuf); |
| 4581 | |
| 4582 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4583 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4584 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4585 | break; |
| 4586 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4587 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | if (outbuf.size < 9) |
| 4591 | goto full; |
| 4592 | |
| 4593 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 4594 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 4595 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4596 | outbuf.data = 9; |
| 4597 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4598 | /* encode all headers */ |
| 4599 | for (idx = 0; idx < hdr; idx++) { |
| 4600 | /* these ones do not exist in H2 or must not appear in |
| 4601 | * trailers and must be dropped. |
| 4602 | */ |
| 4603 | if (isteq(list[idx].n, ist("host")) || |
| 4604 | isteq(list[idx].n, ist("content-length")) || |
| 4605 | isteq(list[idx].n, ist("connection")) || |
| 4606 | isteq(list[idx].n, ist("proxy-connection")) || |
| 4607 | isteq(list[idx].n, ist("keep-alive")) || |
| 4608 | isteq(list[idx].n, ist("upgrade")) || |
| 4609 | isteq(list[idx].n, ist("te")) || |
| 4610 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 4611 | continue; |
| 4612 | |
| 4613 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 4614 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4615 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4616 | goto realign_again; |
| 4617 | goto full; |
| 4618 | } |
| 4619 | } |
| 4620 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 4621 | if (outbuf.data == 9) { |
| 4622 | /* here we have a problem, we have nothing to emit (either we |
| 4623 | * received an empty trailers block followed or we removed its |
| 4624 | * contents above). Because of this we can't send a HEADERS |
| 4625 | * frame, so we have to cheat and instead send an empty DATA |
| 4626 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 4627 | */ |
| 4628 | outbuf.area[3] = H2_FT_DATA; |
| 4629 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 4630 | } |
| 4631 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4632 | /* update the frame's size */ |
| 4633 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4634 | |
| 4635 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4636 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4637 | h2s->flags |= H2_SF_ES_SENT; |
| 4638 | |
| 4639 | if (h2s->st == H2_SS_OPEN) |
| 4640 | h2s->st = H2_SS_HLOC; |
| 4641 | else |
| 4642 | h2s_close(h2s); |
| 4643 | |
| 4644 | /* OK we could properly deliver the response */ |
| 4645 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 4646 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4647 | ret = 0; |
| 4648 | idx = htx_get_head(htx); |
| 4649 | blk = htx_get_blk(htx, idx); |
| 4650 | while (blk != blk_end) { |
| 4651 | ret += htx_get_blksz(blk); |
| 4652 | blk = htx_remove_blk(htx, blk); |
| 4653 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4654 | |
| 4655 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4656 | ret += htx_get_blksz(blk_end); |
| 4657 | htx_remove_blk(htx, blk_end); |
| 4658 | } |
| 4659 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4660 | end: |
| 4661 | return ret; |
| 4662 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4663 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4664 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4665 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4666 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4667 | ret = 0; |
| 4668 | goto end; |
| 4669 | fail: |
| 4670 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4671 | * list etc go here (unrecoverable errors). |
| 4672 | */ |
| 4673 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4674 | ret = 0; |
| 4675 | goto end; |
| 4676 | } |
| 4677 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4678 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 4679 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 4680 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 4681 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 4682 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 4683 | * returns 0 except for the error above. |
| 4684 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4685 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 4686 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4687 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4688 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4689 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4690 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4691 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4692 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4693 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 4694 | sw->events |= SUB_RETRY_RECV; |
| 4695 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4696 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4697 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4698 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4699 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4700 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 4701 | sw->events |= SUB_RETRY_SEND; |
| 4702 | h2s->send_wait = sw; |
| 4703 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 4704 | !LIST_ADDED(&h2s->list)) { |
| 4705 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 4706 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 4707 | else |
| 4708 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 4709 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4710 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4711 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4712 | if (event_type != 0) |
| 4713 | return -1; |
| 4714 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4715 | } |
| 4716 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4717 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 4718 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 4719 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 4720 | * It always returns zero. |
| 4721 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4722 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 4723 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4724 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4725 | struct h2s *h2s = cs->ctx; |
| 4726 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4727 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4728 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4729 | BUG_ON(h2s->recv_wait != sw); |
| 4730 | sw->events &= ~SUB_RETRY_RECV; |
| 4731 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4732 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4733 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4734 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4735 | BUG_ON(h2s->send_wait != sw); |
| 4736 | LIST_DEL(&h2s->list); |
| 4737 | LIST_INIT(&h2s->list); |
| 4738 | sw->events &= ~SUB_RETRY_SEND; |
| 4739 | /* We were about to send, make sure it does not happen */ |
| 4740 | if (LIST_ADDED(&h2s->sending_list) && |
| 4741 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 4742 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4743 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4744 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 4745 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4746 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4747 | return 0; |
| 4748 | } |
| 4749 | |
| 4750 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4751 | /* Called from the upper layer, to receive data */ |
| 4752 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 4753 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4754 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4755 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4756 | struct htx *h2s_htx = NULL; |
| 4757 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4758 | size_t ret = 0; |
| 4759 | |
| 4760 | /* transfer possibly pending data to the upper layer */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4761 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
| 4762 | if (htx_is_empty(h2s_htx)) { |
| 4763 | /* Here htx_to_buf() will set buffer data to 0 because |
| 4764 | * the HTX is empty. |
| 4765 | */ |
| 4766 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 4767 | goto end; |
| 4768 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 4769 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4770 | ret = h2s_htx->data; |
| 4771 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 4772 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4773 | /* <buf> is empty and the message is small enough, swap the |
| 4774 | * buffers. */ |
| 4775 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4776 | htx_to_buf(buf_htx, buf); |
| 4777 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4778 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 4779 | goto end; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4780 | } |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4781 | |
| 4782 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
| 4783 | |
| 4784 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
| 4785 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 4786 | if (htx_is_empty(buf_htx)) |
| 4787 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4788 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4789 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4790 | buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0); |
| 4791 | htx_to_buf(buf_htx, buf); |
| 4792 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 4793 | ret -= h2s_htx->data; |
| 4794 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 4795 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4796 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 4797 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4798 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 4799 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 4800 | if (h2s->flags & H2_SF_ES_RCVD) |
| 4801 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 4802 | if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4803 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 4804 | if (cs->flags & CS_FL_ERR_PENDING) |
| 4805 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4806 | if (b_size(&h2s->rxbuf)) { |
| 4807 | b_free(&h2s->rxbuf); |
| 4808 | offer_buffers(NULL, tasks_run_queue); |
| 4809 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4810 | } |
| 4811 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4812 | if (ret && h2c->dsi == h2s->id) { |
| 4813 | /* demux is blocking on this stream's buffer */ |
| 4814 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 4815 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4816 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 4817 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4818 | return ret; |
| 4819 | } |
| 4820 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4821 | /* stops all senders of this connection for example when the mux buffer is full. |
| 4822 | * They are moved from the sending_list to either fctl_list or send_list. |
| 4823 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4824 | static void h2_stop_senders(struct h2c *h2c) |
| 4825 | { |
| 4826 | struct h2s *h2s, *h2s_back; |
| 4827 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4828 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 4829 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 4830 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4831 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4832 | } |
| 4833 | } |
| 4834 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 4835 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 4836 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 4837 | * flags may be updated on the conn_stream. |
| 4838 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 4839 | 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] | 4840 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4841 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4842 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4843 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4844 | struct htx *htx; |
| 4845 | struct htx_blk *blk; |
| 4846 | enum htx_blk_type btype; |
| 4847 | uint32_t bsize; |
| 4848 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4849 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4850 | /* If we were not just woken because we wanted to send but couldn't, |
| 4851 | * and there's somebody else that is waiting to send, do nothing, |
| 4852 | * we will subscribe later and be put at the end of the list |
| 4853 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4854 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4855 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 4856 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 4857 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4858 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 4859 | /* We couldn't set it to NULL before, because we needed it in case |
| 4860 | * we had to cancel the tasklet |
| 4861 | */ |
| 4862 | h2s->send_wait = NULL; |
| 4863 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 4864 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 4865 | return 0; |
| 4866 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4867 | htx = htx_from_buf(buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4868 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4869 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 4870 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 4871 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4872 | if (h2s->id == 0) { |
| 4873 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 4874 | |
| 4875 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4876 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | eb32_delete(&h2s->by_id); |
| 4881 | h2s->by_id.key = h2s->id = id; |
| 4882 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 4883 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4884 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 4885 | } |
| 4886 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4887 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
| 4888 | count && !htx_is_empty(htx)) { |
| 4889 | idx = htx_get_head(htx); |
| 4890 | blk = htx_get_blk(htx, idx); |
| 4891 | btype = htx_get_blk_type(blk); |
| 4892 | bsize = htx_get_blksz(blk); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4893 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4894 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4895 | case HTX_BLK_REQ_SL: |
| 4896 | /* start-line before headers */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4897 | ret = h2s_bck_make_req_headers(h2s, htx); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4898 | if (ret > 0) { |
| 4899 | total += ret; |
| 4900 | count -= ret; |
| 4901 | if (ret < bsize) |
| 4902 | goto done; |
| 4903 | } |
| 4904 | break; |
| 4905 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4906 | case HTX_BLK_RES_SL: |
| 4907 | /* start-line before headers */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4908 | ret = h2s_frt_make_resp_headers(h2s, htx); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4909 | if (ret > 0) { |
| 4910 | total += ret; |
| 4911 | count -= ret; |
| 4912 | if (ret < bsize) |
| 4913 | goto done; |
| 4914 | } |
| 4915 | break; |
| 4916 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4917 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4918 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4919 | /* all these cause the emission of a DATA frame (possibly empty). |
| 4920 | * This EOM necessarily is one before trailers, as the EOM following |
| 4921 | * trailers would have been consumed by the trailers parser. |
| 4922 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4923 | ret = h2s_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4924 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4925 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4926 | total += ret; |
| 4927 | count -= ret; |
| 4928 | if (ret < bsize) |
| 4929 | goto done; |
| 4930 | } |
| 4931 | break; |
| 4932 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4933 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 4934 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4935 | /* This is the first trailers block, all the subsequent ones AND |
| 4936 | * the EOM will be swallowed by the parser. |
| 4937 | */ |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4938 | ret = h2s_make_trailers(h2s, htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4939 | if (ret > 0) { |
| 4940 | total += ret; |
| 4941 | count -= ret; |
| 4942 | if (ret < bsize) |
| 4943 | goto done; |
| 4944 | } |
| 4945 | break; |
| 4946 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4947 | default: |
| 4948 | htx_remove_blk(htx, blk); |
| 4949 | total += bsize; |
| 4950 | count -= bsize; |
| 4951 | break; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4952 | } |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4955 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 4956 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4957 | /* trim any possibly pending data after we close (extra CR-LF, |
| 4958 | * unprocessed trailers, abnormal extra data, ...) |
| 4959 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4960 | total += count; |
| 4961 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4962 | } |
| 4963 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 4964 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 4965 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 4966 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 4967 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4968 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 4969 | } |
| 4970 | |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 4971 | htx_to_buf(htx, buf); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4972 | |
| 4973 | /* The mux is full, cancel the pending tasks */ |
| 4974 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 4975 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 4976 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4977 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 4978 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4979 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4980 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4981 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 4982 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 4983 | /* If we're waiting for flow control, and we got a shutr on the |
| 4984 | * connection, we will never be unlocked, so add an error on |
| 4985 | * the conn_stream. |
| 4986 | */ |
| 4987 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 4988 | !b_data(&h2s->h2c->dbuf) && |
| 4989 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 4990 | if (cs->flags & CS_FL_EOS) |
| 4991 | cs->flags |= CS_FL_ERROR; |
| 4992 | else |
| 4993 | cs->flags |= CS_FL_ERR_PENDING; |
| 4994 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 4995 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4996 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 4997 | LIST_DEL_INIT(&h2s->list); |
| 4998 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4999 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5000 | } |
| 5001 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5002 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5003 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5004 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5005 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5006 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5007 | struct eb32_node *node; |
| 5008 | int fctl_cnt = 0; |
| 5009 | int send_cnt = 0; |
| 5010 | int tree_cnt = 0; |
| 5011 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5012 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5013 | |
| 5014 | if (!h2c) |
| 5015 | return; |
| 5016 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5017 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5018 | fctl_cnt++; |
| 5019 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5020 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5021 | send_cnt++; |
| 5022 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5023 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5024 | node = eb32_first(&h2c->streams_by_id); |
| 5025 | while (node) { |
| 5026 | h2s = container_of(node, struct h2s, by_id); |
| 5027 | tree_cnt++; |
| 5028 | if (!h2s->cs) |
| 5029 | orph_cnt++; |
| 5030 | node = eb32_next(node); |
| 5031 | } |
| 5032 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5033 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5034 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5035 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5036 | " .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] | 5037 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5038 | " .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] | 5039 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5040 | 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] | 5041 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5042 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5043 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5044 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5045 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5046 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5047 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5048 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5049 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5050 | |
| 5051 | if (h2s) { |
| 5052 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5053 | h2s, h2s->id, h2s->flags, |
| 5054 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5055 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5056 | h2s->cs); |
| 5057 | if (h2s->cs) |
| 5058 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5059 | h2s->cs->flags, h2s->cs->data); |
| 5060 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5061 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5062 | |
| 5063 | /*******************************************************/ |
| 5064 | /* functions below are dedicated to the config parsers */ |
| 5065 | /*******************************************************/ |
| 5066 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5067 | /* config parser for global "tune.h2.header-table-size" */ |
| 5068 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5069 | struct proxy *defpx, const char *file, int line, |
| 5070 | char **err) |
| 5071 | { |
| 5072 | if (too_many_args(1, args, err, NULL)) |
| 5073 | return -1; |
| 5074 | |
| 5075 | h2_settings_header_table_size = atoi(args[1]); |
| 5076 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5077 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5078 | return -1; |
| 5079 | } |
| 5080 | return 0; |
| 5081 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5082 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5083 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5084 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5085 | struct proxy *defpx, const char *file, int line, |
| 5086 | char **err) |
| 5087 | { |
| 5088 | if (too_many_args(1, args, err, NULL)) |
| 5089 | return -1; |
| 5090 | |
| 5091 | h2_settings_initial_window_size = atoi(args[1]); |
| 5092 | if (h2_settings_initial_window_size < 0) { |
| 5093 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5094 | return -1; |
| 5095 | } |
| 5096 | return 0; |
| 5097 | } |
| 5098 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5099 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5100 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5101 | struct proxy *defpx, const char *file, int line, |
| 5102 | char **err) |
| 5103 | { |
| 5104 | if (too_many_args(1, args, err, NULL)) |
| 5105 | return -1; |
| 5106 | |
| 5107 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5108 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5109 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5110 | return -1; |
| 5111 | } |
| 5112 | return 0; |
| 5113 | } |
| 5114 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5115 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5116 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5117 | struct proxy *defpx, const char *file, int line, |
| 5118 | char **err) |
| 5119 | { |
| 5120 | if (too_many_args(1, args, err, NULL)) |
| 5121 | return -1; |
| 5122 | |
| 5123 | h2_settings_max_frame_size = atoi(args[1]); |
| 5124 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5125 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5126 | return -1; |
| 5127 | } |
| 5128 | return 0; |
| 5129 | } |
| 5130 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5131 | |
| 5132 | /****************************************/ |
| 5133 | /* MUX initialization and instanciation */ |
| 5134 | /***************************************/ |
| 5135 | |
| 5136 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5137 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5138 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5139 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5140 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5141 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5142 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5143 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5144 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5145 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5146 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5147 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5148 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5149 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5150 | .shutr = h2_shutr, |
| 5151 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5152 | .show_fd = h2_show_fd, |
Christopher Faulet | 9b79a10 | 2019-07-15 11:22:56 +0200 | [diff] [blame] | 5153 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5154 | .name = "H2", |
| 5155 | }; |
| 5156 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5157 | static struct mux_proto_list mux_proto_h2 = |
Christopher Faulet | c985f6c | 2019-07-15 11:42:52 +0200 | [diff] [blame] | 5158 | { .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] | 5159 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5160 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5161 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5162 | /* config keyword parsers */ |
| 5163 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5164 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5165 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5166 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5167 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5168 | { 0, NULL, NULL } |
| 5169 | }}; |
| 5170 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5171 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |