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) |
| 160 | #define H2_SS_EOS_BITS (H2_SS_CLOSED_BIT|H2_SS_ERROR_BIT|H2_SS_HREM_BIT) |
| 161 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 162 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 163 | #define H2_SF_NONE 0x00000000 |
| 164 | #define H2_SF_ES_RCVD 0x00000001 |
| 165 | #define H2_SF_ES_SENT 0x00000002 |
| 166 | |
| 167 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 168 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 169 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 170 | /* stream flags indicating the reason the stream is blocked */ |
| 171 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 172 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 173 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 174 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 175 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 176 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 177 | /* stream flags indicating how data is supposed to be sent */ |
| 178 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 179 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 180 | |
| 181 | /* step we're currently in when sending chunks. This is needed because we may |
| 182 | * have to transfer chunks as large as a full buffer so there's no room left |
| 183 | * for size nor crlf around. |
| 184 | */ |
| 185 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 186 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 187 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 188 | |
| 189 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 190 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 191 | #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] | 192 | #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] | 193 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 194 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 195 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 196 | #define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy) |
| 197 | #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] | 198 | #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] | 199 | |
| 200 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 201 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 202 | * it is being processed in the internal HTTP representation (H1 for now). |
| 203 | */ |
| 204 | struct h2s { |
| 205 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 206 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 207 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 208 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 209 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 210 | int32_t id; /* stream ID */ |
| 211 | uint32_t flags; /* H2_SF_* */ |
| 212 | int mws; /* mux window size for this stream */ |
| 213 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 214 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 215 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 216 | 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] | 217 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 218 | 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] | 219 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 220 | 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] | 221 | 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] | 222 | 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] | 223 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 224 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 225 | /* descriptor for an h2 frame header */ |
| 226 | struct h2_fh { |
| 227 | uint32_t len; /* length, host order, 24 bits */ |
| 228 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 229 | uint8_t ft; /* frame type */ |
| 230 | uint8_t ff; /* frame flags */ |
| 231 | }; |
| 232 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 233 | /* the h2c connection pool */ |
| 234 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 235 | |
| 236 | /* the h2s stream pool */ |
| 237 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 238 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 239 | /* The default connection window size is 65535, it may only be enlarged using |
| 240 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 241 | * we'll pretend we already received the difference between the two to send |
| 242 | * an equivalent window update to enlarge it to 2G-1. |
| 243 | */ |
| 244 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 245 | |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 246 | /* maximum amount of data we're OK with re-aligning for buffer optimizations */ |
| 247 | #define MAX_DATA_REALIGN 1024 |
| 248 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 249 | /* a few settings from the global section */ |
| 250 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 251 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 252 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 253 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 254 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 255 | /* a dmumy closed stream */ |
| 256 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 257 | .cs = NULL, |
| 258 | .h2c = NULL, |
| 259 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 260 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 261 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 262 | .id = 0, |
| 263 | }; |
| 264 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 265 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 266 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 267 | .cs = NULL, |
| 268 | .h2c = NULL, |
| 269 | .st = H2_SS_CLOSED, |
| 270 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 271 | .flags = 0, |
| 272 | .id = 0, |
| 273 | }; |
| 274 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 275 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 276 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 277 | .cs = NULL, |
| 278 | .h2c = NULL, |
| 279 | .st = H2_SS_CLOSED, |
| 280 | .errcode = H2_ERR_REFUSED_STREAM, |
| 281 | .flags = 0, |
| 282 | .id = 0, |
| 283 | }; |
| 284 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 285 | /* and a dummy idle stream for use with any unannounced stream */ |
| 286 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 287 | .cs = NULL, |
| 288 | .h2c = NULL, |
| 289 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 290 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 291 | .id = 0, |
| 292 | }; |
| 293 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 294 | 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] | 295 | static int h2_send(struct h2c *h2c); |
| 296 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 297 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 298 | 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] | 299 | 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] | 300 | 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] | 301 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 302 | 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] | 303 | 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] | 304 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 305 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 306 | static __inline int |
| 307 | h2c_is_dead(struct h2c *h2c) |
| 308 | { |
| 309 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 310 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 311 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 312 | (!(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] | 313 | (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 314 | (conn_xprt_read0_pending(h2c->conn) || |
| 315 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 316 | return 1; |
| 317 | |
| 318 | return 0; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 321 | /*****************************************************/ |
| 322 | /* functions below are for dynamic buffer management */ |
| 323 | /*****************************************************/ |
| 324 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 325 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 326 | * to receive data into the buffer and/or demux pending data. The condition is |
| 327 | * a bit complex due to some API limits for now. The rules are the following : |
| 328 | * - if an error or a shutdown was detected on the connection and the buffer |
| 329 | * is empty, we must not attempt to receive |
| 330 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 331 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 332 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 333 | * regardless of whether the demux buffer is full or not, so that only |
| 334 | * de demux part decides whether or not to block. This is needed because |
| 335 | * the connection API indeed prevents us from re-enabling receipt that is |
| 336 | * already enabled in a polled state, so we must always immediately stop |
| 337 | * as soon as the demux can't proceed so as never to hit an end of read |
| 338 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 339 | * - otherwise must may not attempt |
| 340 | */ |
| 341 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 342 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 343 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 344 | (h2c->st0 >= H2_CS_ERROR || |
| 345 | h2c->conn->flags & CO_FL_ERROR || |
| 346 | conn_xprt_read0_pending(h2c->conn))) |
| 347 | return 0; |
| 348 | |
| 349 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 350 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 351 | return 1; |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 356 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | if (!h2_recv_allowed(h2c)) |
| 360 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 361 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 362 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 363 | return; |
| 364 | tasklet_wakeup(h2c->wait_event.task); |
| 365 | } |
| 366 | |
| 367 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 368 | /* returns true if the front connection has too many conn_streams attached */ |
| 369 | 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] | 370 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 371 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 374 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 375 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 376 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 377 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 378 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 379 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 380 | { |
| 381 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 382 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 383 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 384 | 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] | 385 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 386 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 387 | return 1; |
| 388 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 389 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 390 | 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] | 391 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 392 | |
| 393 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 394 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 395 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 396 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 397 | return 1; |
| 398 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 399 | |
| 400 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 401 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 402 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 403 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 404 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 405 | return 1; |
| 406 | } |
| 407 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 411 | 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] | 412 | { |
| 413 | struct buffer *buf = NULL; |
| 414 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 415 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 416 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 417 | h2c->buf_wait.target = h2c; |
| 418 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 419 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 420 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 421 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 422 | __conn_xprt_stop_recv(h2c->conn); |
| 423 | } |
| 424 | return buf; |
| 425 | } |
| 426 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 427 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 428 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 429 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 430 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 431 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 435 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 436 | { |
| 437 | struct buffer *buf; |
| 438 | unsigned int count = 0; |
| 439 | |
| 440 | while (b_size(buf = br_head_pick(h2c->mbuf))) { |
| 441 | b_free(buf); |
| 442 | count++; |
| 443 | } |
| 444 | if (count) |
| 445 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
| 446 | } |
| 447 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 448 | /* returns the number of allocatable outgoing streams for the connection taking |
| 449 | * the last_sid and the reserved ones into account. |
| 450 | */ |
| 451 | static inline int h2_streams_left(const struct h2c *h2c) |
| 452 | { |
| 453 | int ret; |
| 454 | |
| 455 | /* consider the number of outgoing streams we're allowed to create before |
| 456 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 457 | * nb_reserved is the number of streams which don't yet have an ID. |
| 458 | */ |
| 459 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 460 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 461 | if (ret < 0) |
| 462 | ret = 0; |
| 463 | return ret; |
| 464 | } |
| 465 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 466 | /* returns the number of streams in use on a connection to figure if it's |
| 467 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 468 | * to know if it was the last one after a detach(). |
| 469 | */ |
| 470 | static int h2_used_streams(struct connection *conn) |
| 471 | { |
| 472 | struct h2c *h2c = conn->ctx; |
| 473 | |
| 474 | return h2c->nb_cs; |
| 475 | } |
| 476 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 477 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 478 | static int h2_avail_streams(struct connection *conn) |
| 479 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 480 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 481 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 482 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 483 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 484 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 485 | * streams on the connection. |
| 486 | */ |
| 487 | if (h2c->last_sid >= 0) |
| 488 | return 0; |
| 489 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 490 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 491 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 492 | |
| 493 | /* we must also consider the limit imposed by stream IDs */ |
| 494 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 495 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 496 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 497 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 498 | ret1 = MIN(ret1, ret2); |
| 499 | } |
| 500 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 503 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 504 | /*****************************************************************/ |
| 505 | /* functions below are dedicated to the mux setup and management */ |
| 506 | /*****************************************************************/ |
| 507 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 508 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 509 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 510 | * connections from the fact that the context is still NULL (even during mux |
| 511 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 512 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 513 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 514 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 515 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 516 | { |
| 517 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 518 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 519 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 520 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 521 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 522 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 523 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 524 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 525 | h2c->flags = H2_CF_IS_BACK; |
| 526 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 527 | if (tick_isset(prx->timeout.serverfin)) |
| 528 | h2c->shut_timeout = prx->timeout.serverfin; |
| 529 | } else { |
| 530 | h2c->flags = H2_CF_NONE; |
| 531 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 532 | if (tick_isset(prx->timeout.clientfin)) |
| 533 | h2c->shut_timeout = prx->timeout.clientfin; |
| 534 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 535 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 536 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 537 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 538 | if (tick_isset(h2c->timeout)) { |
| 539 | t = task_new(tid_bit); |
| 540 | if (!t) |
| 541 | goto fail; |
| 542 | |
| 543 | h2c->task = t; |
| 544 | t->process = h2_timeout_task; |
| 545 | t->context = h2c; |
| 546 | t->expire = tick_add(now_ms, h2c->timeout); |
| 547 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 548 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 549 | h2c->wait_event.task = tasklet_new(); |
| 550 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 551 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 552 | h2c->wait_event.task->process = h2_io_cb; |
| 553 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 554 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 555 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 556 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 557 | if (!h2c->ddht) |
| 558 | goto fail; |
| 559 | |
| 560 | /* Initialise the context. */ |
| 561 | h2c->st0 = H2_CS_PREFACE; |
| 562 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 563 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 564 | h2c->max_id = -1; |
| 565 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 566 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 567 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 568 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 569 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 570 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 571 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 572 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 573 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 574 | h2c->dsi = -1; |
| 575 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 576 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 577 | h2c->last_sid = -1; |
| 578 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 579 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 580 | h2c->miw = 65535; /* mux initial window size */ |
| 581 | h2c->mws = 65535; /* mux window size */ |
| 582 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 583 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 584 | LIST_INIT(&h2c->send_list); |
| 585 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 586 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 587 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 588 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 589 | if (t) |
| 590 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 591 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 592 | if (h2c->flags & H2_CF_IS_BACK) { |
| 593 | /* FIXME: this is temporary, for outgoing connections we need |
| 594 | * to immediately allocate a stream until the code is modified |
| 595 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 596 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 597 | */ |
| 598 | struct h2s *h2s; |
| 599 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 600 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 601 | if (!h2s) |
| 602 | goto fail_stream; |
| 603 | } |
| 604 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 605 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 606 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 607 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 608 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 609 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 610 | fail_stream: |
| 611 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 612 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 613 | task_destroy(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 614 | if (h2c->wait_event.task) |
| 615 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 616 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 617 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 618 | return -1; |
| 619 | } |
| 620 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 621 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 622 | * -1 if no more is allocatable. |
| 623 | */ |
| 624 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 625 | { |
| 626 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 627 | |
| 628 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 629 | id = -1; |
| 630 | return id; |
| 631 | } |
| 632 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 633 | /* returns the stream associated with id <id> or NULL if not found */ |
| 634 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 635 | { |
| 636 | struct eb32_node *node; |
| 637 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 638 | if (id == 0) |
| 639 | return (struct h2s *)h2_closed_stream; |
| 640 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 641 | if (id > h2c->max_id) |
| 642 | return (struct h2s *)h2_idle_stream; |
| 643 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 644 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 645 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 646 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 647 | |
| 648 | return container_of(node, struct h2s, by_id); |
| 649 | } |
| 650 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 651 | /* release function. This one should be called to free all resources allocated |
| 652 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 653 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 654 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 655 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 656 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 657 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 658 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 659 | /* The connection must be aattached to this mux to be released */ |
| 660 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 661 | conn = h2c->conn; |
| 662 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 663 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 664 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame^] | 665 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 666 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 667 | LIST_DEL(&h2c->buf_wait.list); |
| 668 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 669 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 670 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 671 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 672 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 673 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 674 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 675 | h2c->task->context = NULL; |
| 676 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 677 | h2c->task = NULL; |
| 678 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 679 | if (h2c->wait_event.task) |
| 680 | tasklet_free(h2c->wait_event.task); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 681 | if (h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 682 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 683 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 684 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 685 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 688 | if (conn) { |
| 689 | conn->mux = NULL; |
| 690 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 691 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 692 | conn_stop_tracking(conn); |
| 693 | conn_full_close(conn); |
| 694 | if (conn->destroy_cb) |
| 695 | conn->destroy_cb(conn); |
| 696 | conn_free(conn); |
| 697 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 701 | /******************************************************/ |
| 702 | /* functions below are for the H2 protocol processing */ |
| 703 | /******************************************************/ |
| 704 | |
| 705 | /* 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] | 706 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 707 | { |
| 708 | return h2s ? h2s->id : 0; |
| 709 | } |
| 710 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 711 | /* 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] | 712 | 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] | 713 | { |
| 714 | if (h2c->msi < 0) |
| 715 | return 0; |
| 716 | |
| 717 | if (h2c->msi == h2s_id(h2s)) |
| 718 | return 0; |
| 719 | |
| 720 | return 1; |
| 721 | } |
| 722 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 723 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 724 | 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] | 725 | { |
| 726 | h2c->errcode = err; |
| 727 | h2c->st0 = H2_CS_ERROR; |
| 728 | } |
| 729 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 730 | /* marks an error on the stream. It may also update an already closed stream |
| 731 | * (e.g. to report an error after an RST was received). |
| 732 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 733 | 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] | 734 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 735 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 736 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 737 | if (h2s->st < H2_SS_ERROR) |
| 738 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 739 | if (h2s->cs) |
| 740 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 744 | /* attempt to notify the data layer of recv availability */ |
| 745 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 746 | { |
| 747 | struct wait_event *sw; |
| 748 | |
| 749 | if (h2s->recv_wait) { |
| 750 | sw = h2s->recv_wait; |
| 751 | sw->events &= ~SUB_RETRY_RECV; |
| 752 | tasklet_wakeup(sw->task); |
| 753 | h2s->recv_wait = NULL; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* attempt to notify the data layer of send availability */ |
| 758 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 759 | { |
| 760 | struct wait_event *sw; |
| 761 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 762 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 763 | sw = h2s->send_wait; |
| 764 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 765 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 766 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 770 | /* alerts the data layer, trying to wake it up by all means, following |
| 771 | * this sequence : |
| 772 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 773 | * - if its subscribed to send, then it's woken up for send |
| 774 | * - if it was subscribed to neither, its ->wake() callback is called |
| 775 | * It is safe to call this function with a closed stream which doesn't have a |
| 776 | * conn_stream anymore. |
| 777 | */ |
| 778 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 779 | { |
| 780 | if (h2s->recv_wait || h2s->send_wait) { |
| 781 | h2s_notify_recv(h2s); |
| 782 | h2s_notify_send(h2s); |
| 783 | } |
| 784 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 785 | h2s->cs->data_cb->wake(h2s->cs); |
| 786 | } |
| 787 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 788 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 789 | 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] | 790 | { |
| 791 | uint8_t *out = frame; |
| 792 | |
| 793 | *out = len >> 16; |
| 794 | write_n16(out + 1, len); |
| 795 | } |
| 796 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 797 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 798 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 799 | * 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] | 800 | * available in the buffer's input prior to calling this function. The buffer |
| 801 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 802 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 803 | 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] | 804 | const struct buffer *b, int o) |
| 805 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 806 | 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] | 807 | } |
| 808 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 809 | 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] | 810 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 811 | 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] | 812 | } |
| 813 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 814 | 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] | 815 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 816 | 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] | 817 | } |
| 818 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 819 | 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] | 820 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 821 | 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] | 822 | } |
| 823 | |
| 824 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 825 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 826 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 827 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 828 | * 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] | 829 | * |
| 830 | * b0 b1 b2 b3 b4 b5..b8 |
| 831 | * +----------+---------+--------+----+----+----------------------+ |
| 832 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 833 | * +----------+---------+--------+----+----+----------------------+ |
| 834 | * |
| 835 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 836 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 837 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 838 | * 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] | 839 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 840 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 841 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 842 | 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] | 843 | { |
| 844 | uint64_t w; |
| 845 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 846 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 847 | return 0; |
| 848 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 849 | w = h2_get_n64(b, o + 1); |
| 850 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 851 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 852 | h->ff = w >> 32; |
| 853 | h->ft = w >> 40; |
| 854 | h->len += w >> 48; |
| 855 | return 1; |
| 856 | } |
| 857 | |
| 858 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 859 | * h2_peek_frame_hdr() above. |
| 860 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 861 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 862 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 863 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 867 | 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] | 868 | { |
| 869 | int ret; |
| 870 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 871 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 872 | if (ret > 0) |
| 873 | h2_skip_frame_hdr(b); |
| 874 | return ret; |
| 875 | } |
| 876 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 877 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 878 | * its connection if the stream was not yet closed. Please use this exclusively |
| 879 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 880 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 881 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 882 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 883 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 884 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 885 | if (!h2s->id) |
| 886 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 887 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 888 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 889 | h2s_notify_recv(h2s); |
| 890 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 891 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 892 | h2s->st = H2_SS_CLOSED; |
| 893 | } |
| 894 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 895 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 896 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 897 | { |
| 898 | h2s_close(h2s); |
| 899 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 900 | if (b_size(&h2s->rxbuf)) { |
| 901 | b_free(&h2s->rxbuf); |
| 902 | offer_buffers(NULL, tasks_run_queue); |
| 903 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 904 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 905 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 906 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 907 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 908 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 909 | * reference left would be in the h2c send_list/fctl_list, and if |
| 910 | * we're in it, we're getting out anyway |
| 911 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 912 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 913 | if (LIST_ADDED(&h2s->sending_list)) { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 914 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 915 | LIST_DEL_INIT(&h2s->sending_list); |
| 916 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 917 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 918 | pool_free(pool_head_h2s, h2s); |
| 919 | } |
| 920 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 921 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 922 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 923 | * causes of errors can be any failed memory allocation. The caller is |
| 924 | * responsible for checking if the connection may support an extra stream |
| 925 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 926 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 927 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 928 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 929 | struct h2s *h2s; |
| 930 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 931 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 932 | if (!h2s) |
| 933 | goto out; |
| 934 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 935 | h2s->wait_event.task = tasklet_new(); |
| 936 | if (!h2s->wait_event.task) { |
| 937 | pool_free(pool_head_h2s, h2s); |
| 938 | goto out; |
| 939 | } |
| 940 | h2s->send_wait = NULL; |
| 941 | h2s->recv_wait = NULL; |
| 942 | h2s->wait_event.task->process = h2_deferred_shut; |
| 943 | h2s->wait_event.task->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 944 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 945 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 946 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 947 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 948 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 949 | h2s->mws = h2c->miw; |
| 950 | h2s->flags = H2_SF_NONE; |
| 951 | h2s->errcode = H2_ERR_NO_ERROR; |
| 952 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 953 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 954 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 955 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 956 | |
| 957 | if (h2c->flags & H2_CF_IS_BACK) { |
| 958 | h1m_init_req(&h2s->h1m); |
| 959 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 960 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 961 | } else { |
| 962 | h1m_init_res(&h2s->h1m); |
| 963 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 964 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 965 | } |
| 966 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 967 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 968 | if (id > 0) |
| 969 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 970 | else |
| 971 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 972 | |
| 973 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 974 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 975 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 976 | |
| 977 | return h2s; |
| 978 | |
| 979 | out_free_h2s: |
| 980 | pool_free(pool_head_h2s, h2s); |
| 981 | out: |
| 982 | return NULL; |
| 983 | } |
| 984 | |
| 985 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 986 | * case of memory allocation error. |
| 987 | */ |
| 988 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 989 | { |
| 990 | struct session *sess = h2c->conn->owner; |
| 991 | struct conn_stream *cs; |
| 992 | struct h2s *h2s; |
| 993 | |
| 994 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 995 | goto out; |
| 996 | |
| 997 | h2s = h2s_new(h2c, id); |
| 998 | if (!h2s) |
| 999 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1000 | |
| 1001 | cs = cs_new(h2c->conn); |
| 1002 | if (!cs) |
| 1003 | goto out_close; |
| 1004 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1005 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1006 | h2s->cs = cs; |
| 1007 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1008 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1009 | |
| 1010 | if (stream_create_from_cs(cs) < 0) |
| 1011 | goto out_free_cs; |
| 1012 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1013 | /* We want the accept date presented to the next stream to be the one |
| 1014 | * we have now, the handshake time to be null (since the next stream |
| 1015 | * is not delayed by a handshake), and the idle time to count since |
| 1016 | * right now. |
| 1017 | */ |
| 1018 | sess->accept_date = date; |
| 1019 | sess->tv_accept = now; |
| 1020 | sess->t_handshake = 0; |
| 1021 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1022 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1023 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1024 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1025 | return h2s; |
| 1026 | |
| 1027 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1028 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1029 | cs_free(cs); |
| 1030 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1031 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1032 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1033 | sess_log(sess); |
| 1034 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1037 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1038 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1039 | * possible stream ID was reached. |
| 1040 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1041 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1042 | { |
| 1043 | struct h2s *h2s = NULL; |
| 1044 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1045 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1046 | goto out; |
| 1047 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1048 | if (h2_streams_left(h2c) < 1) |
| 1049 | goto out; |
| 1050 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1051 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1052 | h2s = h2s_new(h2c, 0); |
| 1053 | if (!h2s) |
| 1054 | goto out; |
| 1055 | |
| 1056 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1057 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1058 | cs->ctx = h2s; |
| 1059 | h2c->nb_cs++; |
| 1060 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1061 | out: |
| 1062 | return h2s; |
| 1063 | } |
| 1064 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1065 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1066 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1067 | * the various settings codes. |
| 1068 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1069 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1070 | { |
| 1071 | struct buffer *res; |
| 1072 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1073 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1074 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1075 | int ret; |
| 1076 | |
| 1077 | if (h2c_mux_busy(h2c, NULL)) { |
| 1078 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1082 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1083 | chunk_memcpy(&buf, |
| 1084 | "\x00\x00\x00" /* length : 0 for now */ |
| 1085 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1086 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1087 | 9); |
| 1088 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1089 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1090 | /* send settings_enable_push=0 */ |
| 1091 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1092 | } |
| 1093 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1094 | if (h2_settings_header_table_size != 4096) { |
| 1095 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1096 | |
| 1097 | write_n32(str + 2, h2_settings_header_table_size); |
| 1098 | chunk_memcat(&buf, str, 6); |
| 1099 | } |
| 1100 | |
| 1101 | if (h2_settings_initial_window_size != 65535) { |
| 1102 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1103 | |
| 1104 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1105 | chunk_memcat(&buf, str, 6); |
| 1106 | } |
| 1107 | |
| 1108 | if (h2_settings_max_concurrent_streams != 0) { |
| 1109 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1110 | |
| 1111 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1112 | * the protocol, so never send this value! |
| 1113 | */ |
| 1114 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1115 | chunk_memcat(&buf, str, 6); |
| 1116 | } |
| 1117 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1118 | mfs = h2_settings_max_frame_size; |
| 1119 | if (mfs > global.tune.bufsize) |
| 1120 | mfs = global.tune.bufsize; |
| 1121 | |
| 1122 | if (!mfs) |
| 1123 | mfs = global.tune.bufsize; |
| 1124 | |
| 1125 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1126 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1127 | |
| 1128 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1129 | * match bufsize - rewrite size, but at the moment it seems |
| 1130 | * that clients don't take care of it. |
| 1131 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1132 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1133 | chunk_memcat(&buf, str, 6); |
| 1134 | } |
| 1135 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1136 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1137 | |
| 1138 | res = br_tail(h2c->mbuf); |
| 1139 | retry: |
| 1140 | if (!h2_get_buf(h2c, res)) { |
| 1141 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1142 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1146 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1147 | if (unlikely(ret <= 0)) { |
| 1148 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1149 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1150 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1151 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1152 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | else { |
| 1156 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1157 | return 0; |
| 1158 | } |
| 1159 | } |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1163 | /* Try to receive a connection preface, then upon success try to send our |
| 1164 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1165 | * missing data. It may return an error in h2c. |
| 1166 | */ |
| 1167 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1168 | { |
| 1169 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1170 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1171 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1172 | ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE)); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1173 | |
| 1174 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1175 | if (ret1 < 0) |
| 1176 | sess_log(h2c->conn->owner); |
| 1177 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1178 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1179 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1183 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1184 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1185 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1186 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1187 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1190 | /* Try to send a connection preface, then upon success try to send our |
| 1191 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1192 | * missing data. It may return an error in h2c. |
| 1193 | */ |
| 1194 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1195 | { |
| 1196 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1197 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1198 | |
| 1199 | if (h2c_mux_busy(h2c, NULL)) { |
| 1200 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1204 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1205 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1206 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1207 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1208 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | if (!b_data(res)) { |
| 1213 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1214 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1215 | if (unlikely(ret <= 0)) { |
| 1216 | if (!ret) { |
| 1217 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1218 | goto retry; |
| 1219 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1220 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | else { |
| 1224 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1228 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1229 | return h2c_send_settings(h2c); |
| 1230 | } |
| 1231 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1232 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1233 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1234 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1235 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1236 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1237 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1238 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1239 | * case so that it is safe to use h2c_error() to report such errors. |
| 1240 | */ |
| 1241 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1242 | { |
| 1243 | struct buffer *res; |
| 1244 | char str[17]; |
| 1245 | int ret; |
| 1246 | |
| 1247 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1248 | return 1; // claim that it worked |
| 1249 | |
| 1250 | if (h2c_mux_busy(h2c, h2s)) { |
| 1251 | if (h2s) |
| 1252 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1253 | else |
| 1254 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1258 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1259 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1260 | |
| 1261 | if (h2c->last_sid < 0) |
| 1262 | h2c->last_sid = h2c->max_id; |
| 1263 | |
| 1264 | write_n32(str + 9, h2c->last_sid); |
| 1265 | write_n32(str + 13, h2c->errcode); |
| 1266 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1267 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1268 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1269 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1270 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1271 | if (h2s) |
| 1272 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1273 | else |
| 1274 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1278 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1279 | if (unlikely(ret <= 0)) { |
| 1280 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1281 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1282 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1283 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1284 | if (h2s) |
| 1285 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1286 | else |
| 1287 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | else { |
| 1291 | /* we cannot report this error using GOAWAY, so we mark |
| 1292 | * it and claim a success. |
| 1293 | */ |
| 1294 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1295 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1296 | return 1; |
| 1297 | } |
| 1298 | } |
| 1299 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1303 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1304 | * during mux operations. This stream must be valid and cannot be closed |
| 1305 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1306 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1307 | * not yet. |
| 1308 | * |
| 1309 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1310 | * to write the message, it subscribes the stream to future notifications. |
| 1311 | */ |
| 1312 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1313 | { |
| 1314 | struct buffer *res; |
| 1315 | char str[13]; |
| 1316 | int ret; |
| 1317 | |
| 1318 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1319 | return 1; |
| 1320 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1321 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1322 | * RST_STREAM in response to a RST_STREAM frame. |
| 1323 | */ |
| 1324 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1325 | ret = 1; |
| 1326 | goto ignore; |
| 1327 | } |
| 1328 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1329 | if (h2c_mux_busy(h2c, h2s)) { |
| 1330 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1334 | /* len: 4, type: 3, flags: none */ |
| 1335 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1336 | write_n32(str + 5, h2s->id); |
| 1337 | write_n32(str + 9, h2s->errcode); |
| 1338 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1339 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1340 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1341 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1342 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1343 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1347 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1348 | if (unlikely(ret <= 0)) { |
| 1349 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1350 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1351 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1352 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1353 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | else { |
| 1357 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1358 | return 0; |
| 1359 | } |
| 1360 | } |
| 1361 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1362 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1363 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1364 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1369 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1370 | * error code, even if the stream is one of the dummy ones, and will update |
| 1371 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1372 | * |
| 1373 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1374 | * to write the message, it blocks the demuxer and subscribes it to future |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 1375 | * notifications. It's worth mentioning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1376 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1377 | */ |
| 1378 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1379 | { |
| 1380 | struct buffer *res; |
| 1381 | char str[13]; |
| 1382 | int ret; |
| 1383 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1384 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1385 | * RST_STREAM in response to a RST_STREAM frame. |
| 1386 | */ |
| 1387 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1388 | ret = 1; |
| 1389 | goto ignore; |
| 1390 | } |
| 1391 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1392 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1393 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1397 | /* len: 4, type: 3, flags: none */ |
| 1398 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1399 | |
| 1400 | write_n32(str + 5, h2c->dsi); |
| 1401 | write_n32(str + 9, h2s->errcode); |
| 1402 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1403 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1404 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1405 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1406 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1407 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1408 | return 0; |
| 1409 | } |
| 1410 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1411 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1412 | if (unlikely(ret <= 0)) { |
| 1413 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1414 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1415 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1416 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1417 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | else { |
| 1421 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1422 | return 0; |
| 1423 | } |
| 1424 | } |
| 1425 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1426 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1427 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1428 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1429 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1430 | } |
| 1431 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1432 | return ret; |
| 1433 | } |
| 1434 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1435 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1436 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1437 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1438 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1439 | * message, it subscribes the requesting stream to future notifications. |
| 1440 | */ |
| 1441 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1442 | { |
| 1443 | struct h2c *h2c = h2s->h2c; |
| 1444 | struct buffer *res; |
| 1445 | char str[9]; |
| 1446 | int ret; |
| 1447 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1448 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1449 | return 1; |
| 1450 | |
| 1451 | if (h2c_mux_busy(h2c, h2s)) { |
| 1452 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1456 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1457 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1458 | write_n32(str + 5, h2s->id); |
| 1459 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1460 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1461 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1462 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1463 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1464 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1468 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1469 | if (likely(ret > 0)) { |
| 1470 | h2s->flags |= H2_SF_ES_SENT; |
| 1471 | } |
| 1472 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1473 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1474 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1475 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1476 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1477 | return 0; |
| 1478 | } |
| 1479 | else { |
| 1480 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1481 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1482 | } |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1486 | /* wake a specific stream and assign its conn_stream som CS_FL_* flags among |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 1487 | * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1488 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1489 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1490 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1491 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1492 | { |
| 1493 | if (!h2s->cs) { |
| 1494 | /* this stream was already orphaned */ |
| 1495 | h2s_destroy(h2s); |
| 1496 | return; |
| 1497 | } |
| 1498 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1499 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1500 | if (h2s->st == H2_SS_OPEN) |
| 1501 | h2s->st = H2_SS_HREM; |
| 1502 | else if (h2s->st == H2_SS_HLOC) |
| 1503 | h2s_close(h2s); |
| 1504 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1505 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1506 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1507 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1508 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1509 | if (h2s->cs->flags & CS_FL_EOS) |
| 1510 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1511 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1512 | if (h2s->st < H2_SS_ERROR) |
| 1513 | h2s->st = H2_SS_ERROR; |
| 1514 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1515 | |
| 1516 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1520 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1521 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1522 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1523 | { |
| 1524 | struct eb32_node *node; |
| 1525 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1526 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1527 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1528 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1529 | while (node) { |
| 1530 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1531 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1532 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1533 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1534 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1535 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1536 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1537 | while (node) { |
| 1538 | h2s = container_of(node, struct h2s, by_id); |
| 1539 | if (h2s->id > 0) |
| 1540 | break; |
| 1541 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1542 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1546 | /* Increase all streams' outgoing window size by the difference passed in |
| 1547 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1548 | * window size is different. The difference may be negative and the resulting |
| 1549 | * window size as well, for the time it takes to receive some window updates. |
| 1550 | */ |
| 1551 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1552 | { |
| 1553 | struct h2s *h2s; |
| 1554 | struct eb32_node *node; |
| 1555 | |
| 1556 | if (!diff) |
| 1557 | return; |
| 1558 | |
| 1559 | node = eb32_first(&h2c->streams_by_id); |
| 1560 | while (node) { |
| 1561 | h2s = container_of(node, struct h2s, by_id); |
| 1562 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1563 | |
| 1564 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1565 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1566 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1567 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1568 | } |
| 1569 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1570 | node = eb32_next(node); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1575 | * 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] | 1576 | * return an error in h2c. The caller must have already verified frame length |
| 1577 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1578 | */ |
| 1579 | static int h2c_handle_settings(struct h2c *h2c) |
| 1580 | { |
| 1581 | unsigned int offset; |
| 1582 | int error; |
| 1583 | |
| 1584 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1585 | if (h2c->dfl) { |
| 1586 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1587 | goto fail; |
| 1588 | } |
| 1589 | return 1; |
| 1590 | } |
| 1591 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1592 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1593 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1594 | return 0; |
| 1595 | |
| 1596 | /* parse the frame */ |
| 1597 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1598 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1599 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1600 | |
| 1601 | switch (type) { |
| 1602 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1603 | /* we need to update all existing streams with the |
| 1604 | * difference from the previous iws. |
| 1605 | */ |
| 1606 | if (arg < 0) { // RFC7540#6.5.2 |
| 1607 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1608 | goto fail; |
| 1609 | } |
| 1610 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1611 | h2c->miw = arg; |
| 1612 | break; |
| 1613 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1614 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1615 | error = H2_ERR_PROTOCOL_ERROR; |
| 1616 | goto fail; |
| 1617 | } |
| 1618 | h2c->mfs = arg; |
| 1619 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1620 | case H2_SETTINGS_ENABLE_PUSH: |
| 1621 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1622 | error = H2_ERR_PROTOCOL_ERROR; |
| 1623 | goto fail; |
| 1624 | } |
| 1625 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1626 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1627 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1628 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1629 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1630 | arg = h2_settings_max_concurrent_streams; |
| 1631 | h2c->streams_limit = arg; |
| 1632 | } |
| 1633 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | /* need to ACK this frame now */ |
| 1638 | h2c->st0 = H2_CS_FRAME_A; |
| 1639 | return 1; |
| 1640 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1641 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1642 | h2c_error(h2c, error); |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1647 | * success or one of the h2_status values. |
| 1648 | */ |
| 1649 | static int h2c_ack_settings(struct h2c *h2c) |
| 1650 | { |
| 1651 | struct buffer *res; |
| 1652 | char str[9]; |
| 1653 | int ret = -1; |
| 1654 | |
| 1655 | if (h2c_mux_busy(h2c, NULL)) { |
| 1656 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1660 | memcpy(str, |
| 1661 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1662 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1663 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1664 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1665 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1666 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1667 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1668 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1669 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1673 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1674 | if (unlikely(ret <= 0)) { |
| 1675 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1676 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1677 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1678 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1679 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1680 | return 0; |
| 1681 | } |
| 1682 | else { |
| 1683 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1684 | return 0; |
| 1685 | } |
| 1686 | } |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1690 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1691 | * 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] | 1692 | * missing data. The caller must have already verified frame length |
| 1693 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1694 | */ |
| 1695 | static int h2c_handle_ping(struct h2c *h2c) |
| 1696 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1697 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1698 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1699 | h2c->st0 = H2_CS_FRAME_A; |
| 1700 | return 1; |
| 1701 | } |
| 1702 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1703 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1704 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1705 | * error in h2c. |
| 1706 | */ |
| 1707 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1708 | { |
| 1709 | struct buffer *res; |
| 1710 | char str[13]; |
| 1711 | int ret = -1; |
| 1712 | |
| 1713 | if (h2c_mux_busy(h2c, NULL)) { |
| 1714 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1718 | /* length: 4, type: 8, flags: none */ |
| 1719 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1720 | write_n32(str + 5, sid); |
| 1721 | write_n32(str + 9, increment); |
| 1722 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1723 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1724 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1725 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1726 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1727 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1731 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1732 | if (unlikely(ret <= 0)) { |
| 1733 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1734 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1735 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1736 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1737 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | else { |
| 1741 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1742 | return 0; |
| 1743 | } |
| 1744 | } |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
| 1748 | /* try to send pending window update for the connection. It's safe to call it |
| 1749 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1750 | * failure. It may return an error in h2c. |
| 1751 | */ |
| 1752 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1753 | { |
| 1754 | int ret = 1; |
| 1755 | |
| 1756 | if (h2c->rcvd_c <= 0) |
| 1757 | return 1; |
| 1758 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1759 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1760 | /* increase the advertised connection window to 2G on |
| 1761 | * first update. |
| 1762 | */ |
| 1763 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1764 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1765 | } |
| 1766 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1767 | /* send WU for the connection */ |
| 1768 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1769 | if (ret > 0) |
| 1770 | h2c->rcvd_c = 0; |
| 1771 | |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
| 1775 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1776 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1777 | * room or failure. It may return an error in h2c. |
| 1778 | */ |
| 1779 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1780 | { |
| 1781 | int ret = 1; |
| 1782 | |
| 1783 | if (h2c->rcvd_s <= 0) |
| 1784 | return 1; |
| 1785 | |
| 1786 | /* send WU for the stream */ |
| 1787 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1788 | if (ret > 0) |
| 1789 | h2c->rcvd_s = 0; |
| 1790 | |
| 1791 | return ret; |
| 1792 | } |
| 1793 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1794 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1795 | * success, 0 on missing data or one of the h2_status values. |
| 1796 | */ |
| 1797 | static int h2c_ack_ping(struct h2c *h2c) |
| 1798 | { |
| 1799 | struct buffer *res; |
| 1800 | char str[17]; |
| 1801 | int ret = -1; |
| 1802 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1803 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1804 | return 0; |
| 1805 | |
| 1806 | if (h2c_mux_busy(h2c, NULL)) { |
| 1807 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1811 | memcpy(str, |
| 1812 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1813 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1814 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1815 | |
| 1816 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1817 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1818 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1819 | res = br_tail(h2c->mbuf); |
| 1820 | retry: |
| 1821 | if (!h2_get_buf(h2c, res)) { |
| 1822 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1823 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1827 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1828 | if (unlikely(ret <= 0)) { |
| 1829 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1830 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1831 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1832 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1833 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1834 | return 0; |
| 1835 | } |
| 1836 | else { |
| 1837 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1838 | return 0; |
| 1839 | } |
| 1840 | } |
| 1841 | return ret; |
| 1842 | } |
| 1843 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1844 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1845 | * 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] | 1846 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1847 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1848 | */ |
| 1849 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1850 | { |
| 1851 | int32_t inc; |
| 1852 | int error; |
| 1853 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1854 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1855 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1856 | return 0; |
| 1857 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1858 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1859 | |
| 1860 | if (h2c->dsi != 0) { |
| 1861 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1862 | |
| 1863 | /* it's not an error to receive WU on a closed stream */ |
| 1864 | if (h2s->st == H2_SS_CLOSED) |
| 1865 | return 1; |
| 1866 | |
| 1867 | if (!inc) { |
| 1868 | error = H2_ERR_PROTOCOL_ERROR; |
| 1869 | goto strm_err; |
| 1870 | } |
| 1871 | |
| 1872 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1873 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1874 | goto strm_err; |
| 1875 | } |
| 1876 | |
| 1877 | h2s->mws += inc; |
| 1878 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1879 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1880 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1881 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | else { |
| 1885 | /* connection window update */ |
| 1886 | if (!inc) { |
| 1887 | error = H2_ERR_PROTOCOL_ERROR; |
| 1888 | goto conn_err; |
| 1889 | } |
| 1890 | |
| 1891 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1892 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1893 | goto conn_err; |
| 1894 | } |
| 1895 | |
| 1896 | h2c->mws += inc; |
| 1897 | } |
| 1898 | |
| 1899 | return 1; |
| 1900 | |
| 1901 | conn_err: |
| 1902 | h2c_error(h2c, error); |
| 1903 | return 0; |
| 1904 | |
| 1905 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1906 | h2s_error(h2s, error); |
| 1907 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1908 | return 0; |
| 1909 | } |
| 1910 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1911 | /* 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] | 1912 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1913 | * have already verified frame length and stream ID validity. Described in |
| 1914 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1915 | */ |
| 1916 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1917 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1918 | int last; |
| 1919 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1920 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1921 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1922 | return 0; |
| 1923 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1924 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1925 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1926 | if (h2c->last_sid < 0) |
| 1927 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1928 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1929 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1932 | /* 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] | 1933 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1934 | * error in h2c. The caller must have already verified frame length and stream |
| 1935 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1936 | */ |
| 1937 | static int h2c_handle_priority(struct h2c *h2c) |
| 1938 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1939 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1940 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1941 | return 0; |
| 1942 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1943 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1944 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1945 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1946 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1947 | } |
| 1948 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1951 | /* 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] | 1952 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1953 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1954 | */ |
| 1955 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1956 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1957 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1958 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1959 | return 0; |
| 1960 | |
| 1961 | /* late RST, already handled */ |
| 1962 | if (h2s->st == H2_SS_CLOSED) |
| 1963 | return 1; |
| 1964 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1965 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1966 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1967 | |
| 1968 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1969 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1970 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | h2s->flags |= H2_SF_RST_RCVD; |
| 1974 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1977 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1978 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1979 | * return value is the new h2s in case one was allocated (most common case). |
| 1980 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1981 | * errors here are reported as connection errors since it's impossible to |
| 1982 | * recover from such errors after the compression context has been altered. |
| 1983 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1984 | 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] | 1985 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1986 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1987 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1988 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1989 | int error; |
| 1990 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1991 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1992 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1993 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1994 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1995 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1996 | |
| 1997 | /* now either the frame is complete or the buffer is complete */ |
| 1998 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1999 | /* The stream exists/existed, this must be a trailers frame */ |
| 2000 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2001 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2002 | /* unrecoverable error ? */ |
| 2003 | if (h2c->st0 >= H2_CS_ERROR) |
| 2004 | goto out; |
| 2005 | |
| 2006 | if (error == 0) |
| 2007 | goto out; // missing data |
| 2008 | |
| 2009 | if (error < 0) { |
| 2010 | /* Failed to decode this frame (e.g. too large request) |
| 2011 | * but the HPACK decompressor is still synchronized. |
| 2012 | */ |
| 2013 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2014 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2015 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2016 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2017 | goto done; |
| 2018 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2019 | /* the connection was already killed by an RST, let's consume |
| 2020 | * the data and send another RST. |
| 2021 | */ |
| 2022 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2023 | h2s = (struct h2s*)h2_error_stream; |
| 2024 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2025 | } |
| 2026 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2027 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2028 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2029 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2030 | goto conn_err; |
| 2031 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2032 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2033 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2034 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2035 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2036 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2037 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2038 | if (h2c->st0 >= H2_CS_ERROR) |
| 2039 | goto out; |
| 2040 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2041 | if (error <= 0) { |
| 2042 | if (error == 0) |
| 2043 | goto out; // missing data |
| 2044 | |
| 2045 | /* Failed to decode this stream (e.g. too large request) |
| 2046 | * but the HPACK decompressor is still synchronized. |
| 2047 | */ |
| 2048 | h2s = (struct h2s*)h2_error_stream; |
| 2049 | goto send_rst; |
| 2050 | } |
| 2051 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2052 | /* 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] | 2053 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2054 | * 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] | 2055 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2056 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2057 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2058 | h2s = (struct h2s*)h2_refused_stream; |
| 2059 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2063 | h2s->rxbuf = rxbuf; |
| 2064 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2065 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2066 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2067 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2068 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2069 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2070 | |
| 2071 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2072 | if (h2s->st == H2_SS_OPEN) |
| 2073 | h2s->st = H2_SS_HREM; |
| 2074 | else |
| 2075 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2078 | /* update the max stream ID if the request is being processed */ |
| 2079 | if (h2s->id > h2c->max_id) |
| 2080 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2081 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2082 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2083 | |
| 2084 | conn_err: |
| 2085 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2086 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2087 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2088 | out: |
| 2089 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2090 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2091 | |
| 2092 | send_rst: |
| 2093 | /* make the demux send an RST for the current stream. We may only |
| 2094 | * do this if we're certain that the HEADERS frame was properly |
| 2095 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2096 | */ |
| 2097 | h2_release_buf(h2c, &rxbuf); |
| 2098 | h2c->st0 = H2_CS_FRAME_E; |
| 2099 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2102 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2103 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2104 | * errors here are reported as connection errors since it's impossible to |
| 2105 | * recover from such errors after the compression context has been altered. |
| 2106 | */ |
| 2107 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2108 | { |
| 2109 | int error; |
| 2110 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2111 | if (!b_size(&h2c->dbuf)) |
| 2112 | return NULL; // empty buffer |
| 2113 | |
| 2114 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2115 | return NULL; // incomplete frame |
| 2116 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2117 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2118 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2119 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2120 | if (h2c->st0 >= H2_CS_ERROR) |
| 2121 | return NULL; |
| 2122 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2123 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2124 | /* RFC7540#5.1 */ |
| 2125 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2126 | h2c->st0 = H2_CS_FRAME_E; |
| 2127 | return NULL; |
| 2128 | } |
| 2129 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2130 | if (error <= 0) { |
| 2131 | if (error == 0) |
| 2132 | return NULL; // missing data |
| 2133 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2134 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2135 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2136 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2137 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2138 | } |
| 2139 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2140 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2141 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2142 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2143 | 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] | 2144 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2145 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2146 | if (h2s->st == H2_SS_OPEN) |
| 2147 | h2s->st = H2_SS_HREM; |
| 2148 | else if (h2s->st == H2_SS_HLOC) |
| 2149 | h2s_close(h2s); |
| 2150 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2151 | |
| 2152 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2155 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2156 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2157 | */ |
| 2158 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2159 | { |
| 2160 | int error; |
| 2161 | |
| 2162 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2163 | * to signal an end of stream (with the ES flag). |
| 2164 | */ |
| 2165 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2166 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2167 | return 0; // empty buffer |
| 2168 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2169 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2170 | return 0; // incomplete frame |
| 2171 | |
| 2172 | /* now either the frame is complete or the buffer is complete */ |
| 2173 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2174 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2175 | /* RFC7540#6.1 */ |
| 2176 | error = H2_ERR_STREAM_CLOSED; |
| 2177 | goto strm_err; |
| 2178 | } |
| 2179 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2180 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2181 | /* RFC7540#8.1.2 */ |
| 2182 | error = H2_ERR_PROTOCOL_ERROR; |
| 2183 | goto strm_err; |
| 2184 | } |
| 2185 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2186 | if (!h2_frt_transfer_data(h2s)) |
| 2187 | return 0; |
| 2188 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2189 | /* call the upper layers to process the frame, then let the upper layer |
| 2190 | * notify the stream about any change. |
| 2191 | */ |
| 2192 | if (!h2s->cs) { |
| 2193 | error = H2_ERR_STREAM_CLOSED; |
| 2194 | goto strm_err; |
| 2195 | } |
| 2196 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2197 | if (h2c->st0 >= H2_CS_ERROR) |
| 2198 | return 0; |
| 2199 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2200 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2201 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2202 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | /* check for completion : the callee will change this to FRAME_A or |
| 2206 | * FRAME_H once done. |
| 2207 | */ |
| 2208 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2209 | return 0; |
| 2210 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2211 | /* last frame */ |
| 2212 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2213 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2214 | if (h2s->st == H2_SS_OPEN) |
| 2215 | h2s->st = H2_SS_HREM; |
| 2216 | else |
| 2217 | h2s_close(h2s); |
| 2218 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2219 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2220 | /* RFC7540#8.1.2 */ |
| 2221 | error = H2_ERR_PROTOCOL_ERROR; |
| 2222 | goto strm_err; |
| 2223 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2226 | return 1; |
| 2227 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2228 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2229 | h2s_error(h2s, error); |
| 2230 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2234 | /* process Rx frames to be demultiplexed */ |
| 2235 | static void h2_process_demux(struct h2c *h2c) |
| 2236 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2237 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2238 | struct h2_fh hdr; |
| 2239 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2240 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2241 | if (h2c->st0 >= H2_CS_ERROR) |
| 2242 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2243 | |
| 2244 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2245 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2246 | if (h2c->flags & H2_CF_IS_BACK) |
| 2247 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2248 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2249 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2250 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2251 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2252 | sess_log(h2c->conn->owner); |
| 2253 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2254 | goto fail; |
| 2255 | } |
| 2256 | |
| 2257 | h2c->max_id = 0; |
| 2258 | h2c->st0 = H2_CS_SETTINGS1; |
| 2259 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2260 | |
| 2261 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2262 | /* ensure that what is pending is a valid SETTINGS frame |
| 2263 | * without an ACK. |
| 2264 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2265 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2266 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2267 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2268 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2269 | sess_log(h2c->conn->owner); |
| 2270 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2271 | goto fail; |
| 2272 | } |
| 2273 | |
| 2274 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2275 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2276 | h2c_error(h2c, H2_ERR_PROTOCOL_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 | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2282 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2283 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2284 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2285 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2286 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2287 | goto fail; |
| 2288 | } |
| 2289 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2290 | /* that's OK, switch to FRAME_P to process it. This is |
| 2291 | * a SETTINGS frame whose header has already been |
| 2292 | * deleted above. |
| 2293 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2294 | padlen = 0; |
| 2295 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2296 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2297 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2298 | |
| 2299 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2300 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2301 | int ret = 0; |
| 2302 | |
| 2303 | if (h2c->st0 >= H2_CS_ERROR) |
| 2304 | break; |
| 2305 | |
| 2306 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2307 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2308 | break; |
| 2309 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2310 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2311 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2312 | if (!h2c->nb_streams) { |
| 2313 | /* only log if no other stream can report the error */ |
| 2314 | sess_log(h2c->conn->owner); |
| 2315 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2316 | break; |
| 2317 | } |
| 2318 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2319 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2320 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2321 | * we read the pad length and drop it from the remaining |
| 2322 | * payload (one byte + the 9 remaining ones = 10 total |
| 2323 | * removed), so we have a frame payload starting after the |
| 2324 | * pad len. Flow controlled frames (DATA) also count the |
| 2325 | * padlen in the flow control, so it must be adjusted. |
| 2326 | */ |
| 2327 | if (hdr.len < 1) { |
| 2328 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2329 | sess_log(h2c->conn->owner); |
| 2330 | goto fail; |
| 2331 | } |
| 2332 | hdr.len--; |
| 2333 | |
| 2334 | if (b_data(&h2c->dbuf) < 10) |
| 2335 | break; // missing padlen |
| 2336 | |
| 2337 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2338 | |
| 2339 | if (padlen > hdr.len) { |
| 2340 | /* RFC7540#6.1 : pad length = length of |
| 2341 | * frame payload or greater => error. |
| 2342 | */ |
| 2343 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2344 | sess_log(h2c->conn->owner); |
| 2345 | goto fail; |
| 2346 | } |
| 2347 | |
| 2348 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2349 | h2c->rcvd_c++; |
| 2350 | h2c->rcvd_s++; |
| 2351 | } |
| 2352 | b_del(&h2c->dbuf, 1); |
| 2353 | } |
| 2354 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2355 | |
| 2356 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2357 | h2c->dfl = hdr.len; |
| 2358 | h2c->dsi = hdr.sid; |
| 2359 | h2c->dft = hdr.ft; |
| 2360 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2361 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2362 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2363 | |
| 2364 | /* check for minimum basic frame format validity */ |
| 2365 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2366 | if (ret != H2_ERR_NO_ERROR) { |
| 2367 | h2c_error(h2c, ret); |
| 2368 | sess_log(h2c->conn->owner); |
| 2369 | goto fail; |
| 2370 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2374 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2375 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2376 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2377 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 2378 | (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2379 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2380 | (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] | 2381 | /* we may have to signal the upper layers */ |
| 2382 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2383 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2384 | } |
| 2385 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2386 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2387 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2388 | goto strm_err; |
| 2389 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2390 | if (h2s->st == H2_SS_IDLE && |
| 2391 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2392 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2393 | * this state MUST be treated as a connection error |
| 2394 | */ |
| 2395 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2396 | if (!h2c->nb_streams) { |
| 2397 | /* only log if no other stream can report the error */ |
| 2398 | sess_log(h2c->conn->owner); |
| 2399 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2400 | break; |
| 2401 | } |
| 2402 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2403 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2404 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2405 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2406 | * this state MUST be treated as a stream error. |
| 2407 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2408 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2409 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2410 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2411 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2412 | else |
| 2413 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2414 | goto strm_err; |
| 2415 | } |
| 2416 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2417 | /* Below the management of frames received in closed state is a |
| 2418 | * bit hackish because the spec makes strong differences between |
| 2419 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2420 | * in both directions. In addition to this, the creation of a |
| 2421 | * new stream reusing the identifier of a closed one will be |
| 2422 | * detected here. Given that we cannot keep track of all closed |
| 2423 | * streams forever, we consider that unknown closed streams were |
| 2424 | * closed on RST received, which allows us to respond with an |
| 2425 | * RST without breaking the connection (eg: to abort a transfer). |
| 2426 | * Some frames have to be silently ignored as well. |
| 2427 | */ |
| 2428 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2429 | 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] | 2430 | /* #5.1.1: The identifier of a newly |
| 2431 | * established stream MUST be numerically |
| 2432 | * greater than all streams that the initiating |
| 2433 | * endpoint has opened or reserved. This |
| 2434 | * governs streams that are opened using a |
| 2435 | * HEADERS frame and streams that are reserved |
| 2436 | * using PUSH_PROMISE. An endpoint that |
| 2437 | * receives an unexpected stream identifier |
| 2438 | * MUST respond with a connection error. |
| 2439 | */ |
| 2440 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2441 | goto strm_err; |
| 2442 | } |
| 2443 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2444 | 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] | 2445 | /* RFC7540#5.1:closed: an endpoint that |
| 2446 | * receives any frame other than PRIORITY after |
| 2447 | * receiving a RST_STREAM MUST treat that as a |
| 2448 | * stream error of type STREAM_CLOSED. |
| 2449 | * |
| 2450 | * Note that old streams fall into this category |
| 2451 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2452 | * |
| 2453 | * However, we cannot generalize this to all frame types. Those |
| 2454 | * carrying compression state must still be processed before |
| 2455 | * being dropped or we'll desynchronize the decoder. This can |
| 2456 | * happen with request trailers received after sending an |
| 2457 | * RST_STREAM, or with header/trailers responses received after |
| 2458 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2459 | */ |
| 2460 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2461 | h2c->st0 = H2_CS_FRAME_E; |
| 2462 | goto strm_err; |
| 2463 | } |
| 2464 | |
| 2465 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2466 | * result of sending a RST_STREAM frame, the peer that |
| 2467 | * receives the RST_STREAM might have already sent |
| 2468 | * frames on the stream that cannot be withdrawn. An |
| 2469 | * endpoint MUST ignore frames that it receives on |
| 2470 | * closed streams after it has sent a RST_STREAM |
| 2471 | * frame. An endpoint MAY choose to limit the period |
| 2472 | * over which it ignores frames and treat frames that |
| 2473 | * arrive after this time as being in error. |
| 2474 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2475 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2476 | /* RFC7540#5.1:closed: any frame other than |
| 2477 | * PRIO/WU/RST in this state MUST be treated as |
| 2478 | * a connection error |
| 2479 | */ |
| 2480 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2481 | h2c->dft != H2_FT_PRIORITY && |
| 2482 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2483 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2484 | goto strm_err; |
| 2485 | } |
| 2486 | } |
| 2487 | } |
| 2488 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2489 | #if 0 |
| 2490 | // problem below: it is not possible to completely ignore such |
| 2491 | // streams as we need to maintain the compression state as well |
| 2492 | // and for this we need to completely process these frames (eg: |
| 2493 | // HEADERS frames) as well as counting DATA frames to emit |
| 2494 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2495 | // This is a typical case of layer violation where the |
| 2496 | // transported contents are critical to the connection's |
| 2497 | // validity and must be ignored at the same time :-( |
| 2498 | |
| 2499 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2500 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2501 | */ |
| 2502 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2503 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2504 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2505 | h2c->dfl -= ret; |
| 2506 | ret = h2c->dfl == 0; |
| 2507 | goto strm_err; |
| 2508 | } |
| 2509 | #endif |
| 2510 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2511 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2512 | case H2_FT_SETTINGS: |
| 2513 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2514 | ret = h2c_handle_settings(h2c); |
| 2515 | |
| 2516 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2517 | ret = h2c_ack_settings(h2c); |
| 2518 | break; |
| 2519 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2520 | case H2_FT_PING: |
| 2521 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2522 | ret = h2c_handle_ping(h2c); |
| 2523 | |
| 2524 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2525 | ret = h2c_ack_ping(h2c); |
| 2526 | break; |
| 2527 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2528 | case H2_FT_WINDOW_UPDATE: |
| 2529 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2530 | ret = h2c_handle_window_update(h2c, h2s); |
| 2531 | break; |
| 2532 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2533 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2534 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2535 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2536 | * frames' parsers consume all following CONTINUATION |
| 2537 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2538 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2539 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2540 | sess_log(h2c->conn->owner); |
| 2541 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2542 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2543 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2544 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2545 | if (h2c->flags & H2_CF_IS_BACK) |
| 2546 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2547 | else |
| 2548 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2549 | if (tmp_h2s) { |
| 2550 | h2s = tmp_h2s; |
| 2551 | ret = 1; |
| 2552 | } |
| 2553 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2554 | break; |
| 2555 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2556 | case H2_FT_DATA: |
| 2557 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2558 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2559 | |
| 2560 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2561 | ret = h2c_send_strm_wu(h2c); |
| 2562 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2563 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2564 | case H2_FT_PRIORITY: |
| 2565 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2566 | ret = h2c_handle_priority(h2c); |
| 2567 | break; |
| 2568 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2569 | case H2_FT_RST_STREAM: |
| 2570 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2571 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2572 | break; |
| 2573 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2574 | case H2_FT_GOAWAY: |
| 2575 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2576 | ret = h2c_handle_goaway(h2c); |
| 2577 | break; |
| 2578 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2579 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2580 | default: |
| 2581 | /* drop frames that we ignore. They may be larger than |
| 2582 | * the buffer so we drain all of their contents until |
| 2583 | * we reach the end. |
| 2584 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2585 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2586 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2587 | h2c->dfl -= ret; |
| 2588 | ret = h2c->dfl == 0; |
| 2589 | } |
| 2590 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2591 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2592 | /* We may have to send an RST if not done yet */ |
| 2593 | if (h2s->st == H2_SS_ERROR) |
| 2594 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2595 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2596 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2597 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2598 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2599 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2600 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2601 | break; |
| 2602 | |
| 2603 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2604 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2605 | h2c->st0 = H2_CS_FRAME_H; |
| 2606 | } |
| 2607 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2609 | if (h2c->rcvd_c > 0 && |
| 2610 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2611 | h2c_send_conn_wu(h2c); |
| 2612 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2613 | fail: |
| 2614 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2615 | if (h2s && h2s->cs && |
| 2616 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 2617 | (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2618 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2619 | (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] | 2620 | /* we may have to signal the upper layers */ |
| 2621 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2622 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2623 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2624 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2625 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2629 | * the end. |
| 2630 | */ |
| 2631 | static int h2_process_mux(struct h2c *h2c) |
| 2632 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2633 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2634 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2635 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2636 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2637 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2638 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2639 | if (h2c->st0 == H2_CS_ERROR) { |
| 2640 | h2c->st0 = H2_CS_ERROR2; |
| 2641 | sess_log(h2c->conn->owner); |
| 2642 | } |
| 2643 | goto fail; |
| 2644 | } |
| 2645 | h2c->st0 = H2_CS_SETTINGS1; |
| 2646 | } |
| 2647 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2648 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2649 | return 1; |
| 2650 | } |
| 2651 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2652 | /* start by sending possibly pending window updates */ |
| 2653 | if (h2c->rcvd_c > 0 && |
| 2654 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2655 | h2c_send_conn_wu(h2c) < 0) |
| 2656 | goto fail; |
| 2657 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2658 | /* First we always process the flow control list because the streams |
| 2659 | * waiting there were already elected for immediate emission but were |
| 2660 | * blocked just on this. |
| 2661 | */ |
| 2662 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2663 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2664 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2665 | h2c->st0 >= H2_CS_ERROR) |
| 2666 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2667 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2668 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2669 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2670 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2671 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2672 | /* For some reason, the upper layer failed to subsribe again, |
| 2673 | * so remove it from the send_list |
| 2674 | */ |
| 2675 | if (!h2s->send_wait) { |
| 2676 | LIST_DEL_INIT(&h2s->list); |
| 2677 | continue; |
| 2678 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2679 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2680 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2681 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2684 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2685 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2686 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2687 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2688 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2689 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2690 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2691 | /* For some reason, the upper layer failed to subsribe again, |
| 2692 | * so remove it from the send_list |
| 2693 | */ |
| 2694 | if (!h2s->send_wait) { |
| 2695 | LIST_DEL_INIT(&h2s->list); |
| 2696 | continue; |
| 2697 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2698 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2699 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2700 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2701 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2702 | } |
| 2703 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2704 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2705 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2706 | if (h2c->st0 == H2_CS_ERROR) { |
| 2707 | if (h2c->max_id >= 0) { |
| 2708 | h2c_send_goaway_error(h2c, NULL); |
| 2709 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
| 2713 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2714 | } |
| 2715 | return 1; |
| 2716 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2717 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2718 | } |
| 2719 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2720 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2721 | /* Attempt to read data, and subscribe if none available. |
| 2722 | * The function returns 1 if data has been received, otherwise zero. |
| 2723 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2724 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2725 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2726 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2727 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2728 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2729 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2731 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2732 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2733 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2734 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2735 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2736 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2737 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2738 | if (!buf) { |
| 2739 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2740 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2741 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2742 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2743 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2744 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2745 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2746 | /* HTX in use : try to pre-align the buffer like the |
| 2747 | * rxbufs will be to optimize memory copies. We'll make |
| 2748 | * sure that the frame header lands at the end of the |
| 2749 | * HTX block to alias it upon recv. We cannot use the |
| 2750 | * head because rcv_buf() will realign the buffer if |
| 2751 | * it's empty. Thus we cheat and pretend we already |
| 2752 | * have a few bytes there. |
| 2753 | */ |
| 2754 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2755 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2756 | } |
| 2757 | else |
| 2758 | max = b_room(buf); |
| 2759 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2760 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2761 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2762 | else |
| 2763 | ret = 0; |
| 2764 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2765 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2766 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2767 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2768 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2769 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2770 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2771 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2774 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2775 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2776 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2779 | /* Try to send data if possible. |
| 2780 | * The function returns 1 if data have been sent, otherwise zero. |
| 2781 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2782 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2783 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2784 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2785 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2786 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2787 | |
| 2788 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2789 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2790 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2791 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2792 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2793 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2794 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2797 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2798 | * pending streams into the existing buffer until it's reportedly full |
| 2799 | * or the end of send requests is reached. Then it tries to send this |
| 2800 | * buffer's contents out, marks it not full if at least one byte could |
| 2801 | * be sent, and tries again. |
| 2802 | * |
| 2803 | * The snd_buf() function normally takes a "flags" argument which may |
| 2804 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2805 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2806 | * connection is streaming lots of data (used to increase TLS record |
| 2807 | * size at the expense of latency). The former can be sent any time |
| 2808 | * there's a buffer full flag, as it indicates at least one stream |
| 2809 | * attempted to send and failed so there are pending data. An |
| 2810 | * alternative would be to set it as long as there's an active stream |
| 2811 | * but that would be problematic for ACKs until we have an absolute |
| 2812 | * guarantee that all waiters have at least one byte to send. The |
| 2813 | * latter should possibly not be set for now. |
| 2814 | */ |
| 2815 | |
| 2816 | done = 0; |
| 2817 | while (!done) { |
| 2818 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2819 | unsigned int released = 0; |
| 2820 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2821 | |
| 2822 | /* fill as much as we can into the current buffer */ |
| 2823 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2824 | done = h2_process_mux(h2c); |
| 2825 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2826 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2827 | break; |
| 2828 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2829 | if (conn->flags & CO_FL_ERROR) |
| 2830 | break; |
| 2831 | |
| 2832 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2833 | flags |= CO_SFL_MSG_MORE; |
| 2834 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2835 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2836 | if (b_data(buf)) { |
| 2837 | int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
| 2838 | if (!ret) |
| 2839 | break; |
| 2840 | sent = 1; |
| 2841 | b_del(buf, ret); |
| 2842 | if (b_data(buf)) |
| 2843 | break; |
| 2844 | } |
| 2845 | b_free(buf); |
| 2846 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2847 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2848 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2849 | if (released) |
| 2850 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
| 2851 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2852 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2853 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2854 | } |
| 2855 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2856 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2857 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2858 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2859 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2860 | /* We're not full anymore, so we can wake any task that are waiting |
| 2861 | * for us. |
| 2862 | */ |
| 2863 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2864 | struct h2s *h2s; |
| 2865 | |
| 2866 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2867 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2868 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2869 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2870 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2871 | continue; |
| 2872 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2873 | /* For some reason, the upper layer failed to subsribe again, |
| 2874 | * so remove it from the send_list |
| 2875 | */ |
| 2876 | if (!h2s->send_wait) { |
| 2877 | LIST_DEL_INIT(&h2s->list); |
| 2878 | continue; |
| 2879 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2880 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2881 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2882 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2883 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2884 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2885 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2886 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2887 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2888 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2889 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2890 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2891 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event); |
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 | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2895 | /* this is the tasklet referenced in h2c->wait_event.task */ |
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 | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2986 | if (eb_is_empty(&h2c->streams_by_id) || br_data(h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2987 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2988 | task_queue(h2c->task); |
| 2989 | } |
| 2990 | else |
| 2991 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2992 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2993 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2994 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2995 | return 0; |
| 2996 | } |
| 2997 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2998 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2999 | static int h2_wake(struct connection *conn) |
| 3000 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3001 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3002 | |
| 3003 | return (h2_process(h2c)); |
| 3004 | } |
| 3005 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3006 | /* Connection timeout management. The principle is that if there's no receipt |
| 3007 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3008 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3009 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3010 | * GOAWAY frame. |
| 3011 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3012 | 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] | 3013 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3014 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3015 | int expired = tick_is_expired(t->expire, now_ms); |
| 3016 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3017 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3018 | return t; |
| 3019 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3020 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3021 | |
| 3022 | if (!h2c) { |
| 3023 | /* resources were already deleted */ |
| 3024 | return NULL; |
| 3025 | } |
| 3026 | |
| 3027 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3028 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3029 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3030 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3031 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3032 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3033 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3034 | } |
| 3035 | |
| 3036 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3037 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3038 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3039 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3040 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3041 | 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] | 3042 | unsigned int released = 0; |
| 3043 | struct buffer *buf; |
| 3044 | |
| 3045 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3046 | if (b_data(buf)) { |
| 3047 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3048 | if (!ret) |
| 3049 | break; |
| 3050 | b_del(buf, ret); |
| 3051 | if (b_data(buf)) |
| 3052 | break; |
| 3053 | b_free(buf); |
| 3054 | released++; |
| 3055 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3056 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3057 | |
| 3058 | if (released) |
| 3059 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3060 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3061 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3062 | /* either we can release everything now or it will be done later once |
| 3063 | * the last stream closes. |
| 3064 | */ |
| 3065 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3066 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3067 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3068 | return NULL; |
| 3069 | } |
| 3070 | |
| 3071 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3072 | /*******************************************/ |
| 3073 | /* functions below are used by the streams */ |
| 3074 | /*******************************************/ |
| 3075 | |
| 3076 | /* |
| 3077 | * Attach a new stream to a connection |
| 3078 | * (Used for outgoing connections) |
| 3079 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3080 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3081 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3082 | struct conn_stream *cs; |
| 3083 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3084 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3085 | |
| 3086 | cs = cs_new(conn); |
| 3087 | if (!cs) |
| 3088 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3089 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3090 | if (!h2s) { |
| 3091 | cs_free(cs); |
| 3092 | return NULL; |
| 3093 | } |
| 3094 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3095 | } |
| 3096 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3097 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3098 | * We have to scan because we may have some orphan streams. It might be |
| 3099 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3100 | * orphans. |
| 3101 | */ |
| 3102 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3103 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3104 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3105 | struct h2s *h2s; |
| 3106 | struct eb32_node *node; |
| 3107 | |
| 3108 | node = eb32_first(&h2c->streams_by_id); |
| 3109 | while (node) { |
| 3110 | h2s = container_of(node, struct h2s, by_id); |
| 3111 | if (h2s->cs) |
| 3112 | return h2s->cs; |
| 3113 | node = eb32_next(node); |
| 3114 | } |
| 3115 | return NULL; |
| 3116 | } |
| 3117 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3118 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3119 | * Destroy the mux and the associated connection, if it is no longer used |
| 3120 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3121 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3122 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3123 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3124 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3125 | 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] | 3126 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3130 | * Detach the stream from the connection and possibly release the connection. |
| 3131 | */ |
| 3132 | static void h2_detach(struct conn_stream *cs) |
| 3133 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3134 | struct h2s *h2s = cs->ctx; |
| 3135 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3136 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3137 | |
| 3138 | cs->ctx = NULL; |
| 3139 | if (!h2s) |
| 3140 | return; |
| 3141 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3142 | /* 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] | 3143 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3144 | h2s->send_wait != &h2s->wait_event) { |
| 3145 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 3146 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3147 | /* |
| 3148 | * At this point, the stream_interface is supposed to have called |
| 3149 | * h2_unsubscribe(), so the only way there's still a |
| 3150 | * subscription that came from the stream_interface (as we |
| 3151 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3152 | * without the stream_interface involved) is that we subscribed |
| 3153 | * for sending, we woke the tasklet up and removed the |
| 3154 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3155 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3156 | * run yet. Make sure to handle that by explicitely setting |
| 3157 | * send_wait to NULL, as nothing else will do it for us. |
| 3158 | */ |
| 3159 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3160 | } |
| 3161 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3162 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3163 | h2c = h2s->h2c; |
| 3164 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3165 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3166 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3167 | !h2_frt_has_too_many_cs(h2c)) { |
| 3168 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3169 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3170 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3171 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3172 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3173 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3174 | * an ES or RST frame), so orphan it in this case. |
| 3175 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3176 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3177 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3178 | (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] | 3179 | return; |
| 3180 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3181 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3182 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3183 | /* unblock the connection if it was blocked on this |
| 3184 | * stream. |
| 3185 | */ |
| 3186 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3187 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3188 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3189 | } |
| 3190 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3191 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3192 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3193 | if (h2c->flags & H2_CF_IS_BACK && |
| 3194 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3195 | if (!(h2c->conn->flags & |
| 3196 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3197 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3198 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3199 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3200 | h2c->conn->owner = NULL; |
| 3201 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3202 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3203 | /* The server doesn't want it, let's kill the connection right away */ |
| 3204 | h2c->conn->mux->destroy(h2c->conn); |
| 3205 | return; |
| 3206 | } |
| 3207 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3208 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3209 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3210 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3211 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3212 | return; |
| 3213 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3214 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3215 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3216 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3217 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3218 | struct server *srv = objt_server(h2c->conn->target); |
| 3219 | |
| 3220 | if (srv) { |
| 3221 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3222 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3223 | else |
| 3224 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3225 | } |
| 3226 | |
| 3227 | } |
| 3228 | } |
| 3229 | } |
| 3230 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3231 | /* We don't want to close right now unless we're removing the |
| 3232 | * last stream, and either the connection is in error, or it |
| 3233 | * reached the ID already specified in a GOAWAY frame received |
| 3234 | * or sent (as seen by last_sid >= 0). |
| 3235 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3236 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3237 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3238 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3239 | } |
| 3240 | else if (h2c->task) { |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3241 | if (eb_is_empty(&h2c->streams_by_id) || br_data(h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3242 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3243 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3244 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3245 | else |
| 3246 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3247 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3248 | } |
| 3249 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3250 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3251 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3252 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3253 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3254 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3255 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3256 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3257 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3258 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3259 | /* a connstream may require us to immediately kill the whole connection |
| 3260 | * for example because of a "tcp-request content reject" rule that is |
| 3261 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3262 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3263 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3264 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3265 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3266 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3267 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3268 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3269 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3270 | /* Nothing was never sent for this stream, so reset with |
| 3271 | * REFUSED_STREAM error to let the client retry the |
| 3272 | * request. |
| 3273 | */ |
| 3274 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3275 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3276 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3277 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3278 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3279 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3280 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3281 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3282 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3283 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3284 | done: |
| 3285 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3286 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3287 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3288 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3289 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3290 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3291 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3292 | h2s->send_wait = sw; |
| 3293 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3294 | h2s->send_wait = sw; |
| 3295 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3296 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3297 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3298 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3299 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3300 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3301 | } |
| 3302 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3303 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3304 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3305 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3306 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3307 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3308 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3309 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3310 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3311 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3312 | if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR && |
| 3313 | (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3314 | /* we can cleanly close using an empty data frame only after headers */ |
| 3315 | |
| 3316 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3317 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3318 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3319 | |
| 3320 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3321 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3322 | else |
| 3323 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3324 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3325 | /* a connstream may require us to immediately kill the whole connection |
| 3326 | * for example because of a "tcp-request content reject" rule that is |
| 3327 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3328 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3329 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3330 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3331 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3332 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3333 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3334 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3335 | else { |
| 3336 | /* Nothing was never sent for this stream, so reset with |
| 3337 | * REFUSED_STREAM error to let the client retry the |
| 3338 | * request. |
| 3339 | */ |
| 3340 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3341 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3342 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3343 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3344 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3345 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3346 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3347 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3348 | } |
| 3349 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3350 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3351 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3352 | done: |
| 3353 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3354 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3355 | |
| 3356 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3357 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3358 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3359 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3360 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3361 | h2s->send_wait = sw; |
| 3362 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3363 | h2s->send_wait = sw; |
| 3364 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3365 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3366 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3367 | /* let the handler know we want to shutw */ |
| 3368 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3369 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3370 | } |
| 3371 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3372 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3373 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3374 | * and prevented the last frame from being emitted. |
| 3375 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3376 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3377 | { |
| 3378 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3379 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3380 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3381 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3382 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3383 | h2_do_shutw(h2s); |
| 3384 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3385 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3386 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3387 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3388 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3389 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3390 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3391 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3392 | if (!h2s->cs) { |
| 3393 | h2s_destroy(h2s); |
| 3394 | if (h2c_is_dead(h2c)) |
| 3395 | h2_release(h2c); |
| 3396 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3399 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3400 | } |
| 3401 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3402 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3403 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3404 | { |
| 3405 | struct h2s *h2s = cs->ctx; |
| 3406 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3407 | if (cs->flags & CS_FL_KILL_CONN) |
| 3408 | h2s->flags |= H2_SF_KILL_CONN; |
| 3409 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3410 | if (!mode) |
| 3411 | return; |
| 3412 | |
| 3413 | h2_do_shutr(h2s); |
| 3414 | } |
| 3415 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3416 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3417 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3418 | { |
| 3419 | struct h2s *h2s = cs->ctx; |
| 3420 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3421 | if (cs->flags & CS_FL_KILL_CONN) |
| 3422 | h2s->flags |= H2_SF_KILL_CONN; |
| 3423 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3424 | h2_do_shutw(h2s); |
| 3425 | } |
| 3426 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3427 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3428 | * HTX request or response depending on the connection's side. Returns a |
| 3429 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3430 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3431 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3432 | * error when a failure is reported, the caller must assume a stream error. |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3433 | * |
| 3434 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3435 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3436 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3437 | * leaving a hole between the main frame and the beginning of the next one. |
| 3438 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3439 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3440 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3441 | * |
| 3442 | * A buffer at the beginning of processing may look like this : |
| 3443 | * |
| 3444 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3445 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3446 | * `---^---------^-----^--------------^--------------^------^---' |
| 3447 | * | | <-----> | | |
| 3448 | * area | dpl | wrap |
| 3449 | * |<--------------> | |
| 3450 | * | dfl | |
| 3451 | * |<-------------------------------------------------->| |
| 3452 | * head data |
| 3453 | * |
| 3454 | * Padding is automatically overwritten when folding, participating to the |
| 3455 | * hole size after dfl : |
| 3456 | * |
| 3457 | * ,---.------------------------.-----.--------------.------.---. |
| 3458 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3459 | * `---^------------------------^-----^--------------^------^---' |
| 3460 | * | | <-----> | | |
| 3461 | * area | hole | wrap |
| 3462 | * |<-----------------------> | |
| 3463 | * | dfl | |
| 3464 | * |<-------------------------------------------------->| |
| 3465 | * head data |
| 3466 | * |
| 3467 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3468 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3469 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3470 | * |
| 3471 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3472 | * so that the function can update the following flags : |
| 3473 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3474 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3475 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3476 | * |
| 3477 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3478 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3479 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3480 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3481 | 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] | 3482 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3483 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3484 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3485 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3486 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3487 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3488 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3489 | int flen; // header frame len |
| 3490 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3491 | int ret = 0; |
| 3492 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3493 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3494 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3495 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3496 | next_frame: |
| 3497 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3498 | goto leave; // incomplete input frame |
| 3499 | |
| 3500 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3501 | * this case, we'll try to paste it immediately after the initial |
| 3502 | * HEADERS frame payload and kill any possible padding. The initial |
| 3503 | * frame's length will be increased to represent the concatenation |
| 3504 | * of the two frames. The next frame is read from position <tlen> |
| 3505 | * and written at position <flen> (minus padding if some is present). |
| 3506 | */ |
| 3507 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3508 | struct h2_fh hdr; |
| 3509 | int clen; // CONTINUATION frame's payload length |
| 3510 | |
| 3511 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3512 | /* no more data, the buffer may be full, either due to |
| 3513 | * too large a frame or because of too large a hole that |
| 3514 | * we're going to compact at the end. |
| 3515 | */ |
| 3516 | goto leave; |
| 3517 | } |
| 3518 | |
| 3519 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3520 | /* RFC7540#6.10: frame of unexpected type */ |
| 3521 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3522 | goto fail; |
| 3523 | } |
| 3524 | |
| 3525 | if (hdr.sid != h2c->dsi) { |
| 3526 | /* RFC7540#6.10: frame of different stream */ |
| 3527 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3528 | goto fail; |
| 3529 | } |
| 3530 | |
| 3531 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3532 | /* RFC7540#4.2: invalid frame length */ |
| 3533 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3534 | goto fail; |
| 3535 | } |
| 3536 | |
| 3537 | /* detect when we must stop aggragating frames */ |
| 3538 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3539 | |
| 3540 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3541 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3542 | if (clen > hdr.len) |
| 3543 | clen = hdr.len; |
| 3544 | |
| 3545 | /* Move the frame's payload over the padding, hole and frame |
| 3546 | * header. At least one of hole or dpl is null (see diagrams |
| 3547 | * above). The hole moves after the new aggragated frame. |
| 3548 | */ |
| 3549 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3550 | h2c->dfl += clen - h2c->dpl; |
| 3551 | hole += h2c->dpl + 9; |
| 3552 | h2c->dpl = 0; |
| 3553 | goto next_frame; |
| 3554 | } |
| 3555 | |
| 3556 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3557 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3558 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3559 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3560 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3561 | copy = alloc_trash_chunk(); |
| 3562 | if (!copy) { |
| 3563 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3564 | goto fail; |
| 3565 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3566 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3567 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3568 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3569 | } |
| 3570 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3571 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3572 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3573 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3574 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3575 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3576 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3577 | } |
| 3578 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3579 | if (flen < 5) { |
| 3580 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3581 | goto fail; |
| 3582 | } |
| 3583 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3584 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3585 | flen -= 5; |
| 3586 | } |
| 3587 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3588 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3589 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3590 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3591 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3592 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3593 | /* we can't retry a failed decompression operation so we must be very |
| 3594 | * careful not to take any risks. In practice the output buffer is |
| 3595 | * always empty except maybe for trailers, in which case we simply have |
| 3596 | * to wait for the upper layer to finish consuming what is available. |
| 3597 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3598 | |
| 3599 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3600 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3601 | if (!htx_is_empty(htx)) { |
| 3602 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3603 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3604 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3605 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3606 | if (b_data(rxbuf)) { |
| 3607 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3608 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3609 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3610 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3611 | rxbuf->head = 0; |
| 3612 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3613 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3614 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3615 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3616 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3617 | sizeof(list)/sizeof(list[0]), tmp); |
| 3618 | if (outlen < 0) { |
| 3619 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3620 | goto fail; |
| 3621 | } |
| 3622 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3623 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3624 | * the parser's state to commit these changes and allow us to later |
| 3625 | * fail solely on the stream if needed. |
| 3626 | */ |
| 3627 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3628 | h2c->dfl = hole = 0; |
| 3629 | h2c->st0 = H2_CS_FRAME_H; |
| 3630 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3631 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3632 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3633 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3634 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3635 | goto trailers; |
| 3636 | |
| 3637 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3638 | if (htx) { |
| 3639 | /* HTX mode */ |
| 3640 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3641 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3642 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3643 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3644 | } else { |
| 3645 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3646 | outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len); |
Willy Tarreau | 8319593 | 2019-01-03 10:26:23 +0100 | [diff] [blame] | 3647 | if (outlen > 0) |
| 3648 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3649 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3650 | |
| 3651 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3652 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3653 | goto fail; |
| 3654 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3655 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3656 | if (msgf & H2_MSGF_BODY) { |
| 3657 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3658 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3659 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3660 | if (htx) |
| 3661 | htx->extra = *body_len; |
| 3662 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3663 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3664 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3665 | } |
| 3666 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3667 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3668 | /* indicate that a HEADERS frame was received for this stream, except |
| 3669 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3670 | * expected. |
| 3671 | */ |
| 3672 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3673 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3674 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3675 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3676 | /* Mark the end of message, either using EOM in HTX or with the |
| 3677 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3678 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3679 | * we must not leave an HTX trailers block not followed by an |
| 3680 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3681 | * the EOM block we must remove the TLR block we've just added. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3682 | */ |
| 3683 | if (htx) { |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3684 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3685 | struct htx_blk *tail = htx_get_tail_blk(htx); |
| 3686 | |
| 3687 | if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR) |
| 3688 | htx_remove_blk(htx, tail); |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3689 | goto fail; |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3690 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3691 | } |
| 3692 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3693 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3694 | goto fail; |
| 3695 | } |
| 3696 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3697 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3698 | /* success */ |
| 3699 | ret = 1; |
| 3700 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3701 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3702 | /* 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] | 3703 | * move the remaining data over it. |
| 3704 | */ |
| 3705 | if (hole) { |
| 3706 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3707 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3708 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3709 | b_sub(&h2c->dbuf, hole); |
| 3710 | } |
| 3711 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3712 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3713 | /* too large frames */ |
| 3714 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3715 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3716 | } |
| 3717 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3718 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3719 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3720 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3721 | return ret; |
| 3722 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3723 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3724 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3725 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3726 | |
| 3727 | trailers: |
| 3728 | /* This is the last HEADERS frame hence a trailer */ |
| 3729 | |
| 3730 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3731 | /* It's a trailer but it's missing ES flag */ |
| 3732 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3733 | goto fail; |
| 3734 | } |
| 3735 | |
| 3736 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3737 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3738 | * other modes, the trailers are silently dropped. |
| 3739 | */ |
| 3740 | if (htx) { |
| 3741 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3742 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3743 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3744 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3745 | } |
| 3746 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3747 | /* Legacy mode with chunked encoding : we must finalize the |
| 3748 | * data block message emit the trailing CRLF */ |
| 3749 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3750 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3751 | |
| 3752 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3753 | if (outlen > 0) |
| 3754 | b_add(rxbuf, outlen); |
| 3755 | else |
| 3756 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3760 | } |
| 3761 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3762 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3763 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3764 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3765 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3766 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3767 | * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3768 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3769 | * send the current frame, 0 if it couldn't complete, in which case |
| 3770 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3771 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3772 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3773 | * have checked the frame header and ensured that the frame was complete or the |
| 3774 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3775 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3776 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3777 | { |
| 3778 | struct h2c *h2c = h2s->h2c; |
| 3779 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3780 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3781 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3782 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3783 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3784 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3785 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3786 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3787 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3788 | if (!csbuf) { |
| 3789 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3790 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3791 | } |
| 3792 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3793 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3794 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3795 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3796 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3797 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3798 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3799 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3800 | if (flen > b_data(&h2c->dbuf)) { |
| 3801 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3802 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3803 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3804 | } |
| 3805 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3806 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3807 | unsigned int sent; |
| 3808 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3809 | block1 = htx_free_data_space(htx); |
| 3810 | if (!block1) { |
| 3811 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3812 | goto fail; |
| 3813 | } |
| 3814 | if (flen > block1) |
| 3815 | flen = block1; |
| 3816 | |
| 3817 | /* here, flen is the max we can copy into the output buffer */ |
| 3818 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3819 | if (flen > block1) |
| 3820 | flen = block1; |
| 3821 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3822 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3823 | |
| 3824 | b_del(&h2c->dbuf, flen); |
| 3825 | h2c->dfl -= flen; |
| 3826 | h2c->rcvd_c += flen; |
| 3827 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3828 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3829 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3830 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3831 | htx->extra = h2s->body_len; |
| 3832 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3833 | |
| 3834 | if (sent < flen) { |
| 3835 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3836 | goto fail; |
| 3837 | } |
| 3838 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3839 | goto try_again; |
| 3840 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3841 | else if (unlikely(b_space_wraps(csbuf) && |
| 3842 | flen + chklen <= b_room(csbuf) && |
| 3843 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3844 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3845 | * not too many data in the buffer, let's defragment it and try |
| 3846 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3847 | */ |
| 3848 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3849 | } |
| 3850 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3851 | /* chunked-encoding requires more room */ |
| 3852 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3853 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3854 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3855 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3856 | (chklen < 1048576) ? 4 : 8; |
| 3857 | chklen += 4; // CRLF, CRLF |
| 3858 | } |
| 3859 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3860 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3861 | if (flen + chklen > b_room(csbuf)) { |
| 3862 | if (chklen >= b_room(csbuf)) { |
| 3863 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3864 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3865 | } |
| 3866 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3870 | /* emit the chunk size */ |
| 3871 | unsigned int chksz = flen; |
| 3872 | char str[10]; |
| 3873 | char *beg; |
| 3874 | |
| 3875 | beg = str + sizeof(str); |
| 3876 | *--beg = '\n'; |
| 3877 | *--beg = '\r'; |
| 3878 | do { |
| 3879 | *--beg = hextab[chksz & 0xF]; |
| 3880 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3881 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3882 | } |
| 3883 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3884 | /* Block1 is the length of the first block before the buffer wraps, |
| 3885 | * block2 is the optional second block to reach the end of the frame. |
| 3886 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3887 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3888 | if (block1 > flen) |
| 3889 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3890 | block2 = flen - block1; |
| 3891 | |
| 3892 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3893 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3894 | |
| 3895 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3896 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3897 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3898 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3899 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3900 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3901 | } |
| 3902 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3903 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3904 | * by the caller when seeing FRAME_A after sending the window update). |
| 3905 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3906 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3907 | h2c->dfl -= flen; |
| 3908 | h2c->rcvd_c += flen; |
| 3909 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3910 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3911 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3912 | h2s->body_len -= flen; |
| 3913 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3914 | if (h2c->dfl > h2c->dpl) { |
| 3915 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3916 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3917 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3918 | } |
| 3919 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3920 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3921 | /* here we're done with the frame, all the payload (except padding) was |
| 3922 | * transferred. |
| 3923 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3924 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3925 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3926 | if (htx) { |
| 3927 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3928 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3929 | goto fail; |
| 3930 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3931 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3932 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3933 | /* emit the trailing 0 CRLF CRLF */ |
| 3934 | if (b_room(csbuf) < 5) { |
| 3935 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3936 | goto fail; |
| 3937 | } |
| 3938 | chklen += 5; |
| 3939 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3940 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3941 | } |
| 3942 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3943 | h2c->rcvd_c += h2c->dpl; |
| 3944 | h2c->rcvd_s += h2c->dpl; |
| 3945 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3946 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3947 | if (htx) |
| 3948 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3949 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3950 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3951 | if (htx) |
| 3952 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3953 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3954 | } |
| 3955 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3956 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3957 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3958 | * number of bytes sent. The caller must check the stream's status to detect |
| 3959 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3960 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3961 | static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3962 | { |
| 3963 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3964 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3965 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3966 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3967 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3968 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3969 | int es_now = 0; |
| 3970 | int ret = 0; |
| 3971 | int hdr; |
| 3972 | |
| 3973 | if (h2c_mux_busy(h2c, h2s)) { |
| 3974 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3975 | return 0; |
| 3976 | } |
| 3977 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3978 | /* First, try to parse the H1 response and index it into <list>. |
| 3979 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3980 | * block does not wrap and we can safely read it this way without |
| 3981 | * having to realign the buffer. |
| 3982 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3983 | ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max, |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3984 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3985 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3986 | /* incomplete or invalid response, this is abnormal coming from |
| 3987 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3988 | * so that won't be fixed, raise an error now. |
| 3989 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3990 | * FIXME: we should instead add the ability to only return a |
| 3991 | * 502 bad gateway. But in theory this is not supposed to |
| 3992 | * happen. |
| 3993 | */ |
| 3994 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3995 | ret = 0; |
| 3996 | goto end; |
| 3997 | } |
| 3998 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3999 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4000 | |
| 4001 | /* certain statuses have no body or an empty one, regardless of |
| 4002 | * what the headers say. |
| 4003 | */ |
| 4004 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4005 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4006 | h1m->curr_len = h1m->body_len = 0; |
| 4007 | } |
| 4008 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4009 | /* no contents, claim c-len is present and set to zero */ |
| 4010 | h1m->flags &= ~H1_MF_CHNK; |
| 4011 | h1m->flags |= H1_MF_CLEN; |
| 4012 | h1m->curr_len = h1m->body_len = 0; |
| 4013 | } |
| 4014 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4015 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4016 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4017 | if (!h2_get_buf(h2c, mbuf)) { |
| 4018 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4019 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4020 | return 0; |
| 4021 | } |
| 4022 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4023 | chunk_reset(&outbuf); |
| 4024 | |
| 4025 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4026 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4027 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4028 | break; |
| 4029 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4030 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4031 | } |
| 4032 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4033 | if (outbuf.size < 9) |
| 4034 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4035 | |
| 4036 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4037 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4038 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4039 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4040 | |
| 4041 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4042 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4043 | /* this is an unparsable response */ |
| 4044 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4045 | ret = 0; |
| 4046 | goto end; |
| 4047 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4048 | |
| 4049 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4050 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4051 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4052 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | /* encode all headers, stop at empty name */ |
| 4056 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4057 | /* these ones do not exist in H2 and must be dropped. */ |
| 4058 | if (isteq(list[hdr].n, ist("connection")) || |
| 4059 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4060 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4061 | isteq(list[hdr].n, ist("upgrade")) || |
| 4062 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4063 | continue; |
| 4064 | |
| 4065 | if (isteq(list[hdr].n, ist(""))) |
| 4066 | break; // end |
| 4067 | |
| 4068 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4069 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4070 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4071 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4072 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4077 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4078 | es_now = 1; |
| 4079 | |
| 4080 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4081 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4082 | |
| 4083 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4084 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4085 | |
| 4086 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4087 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4088 | |
| 4089 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4090 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4091 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4092 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4093 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4094 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4095 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4096 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4097 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4098 | h2s->flags |= H2_SF_ES_SENT; |
| 4099 | if (h2s->st == H2_SS_OPEN) |
| 4100 | h2s->st = H2_SS_HLOC; |
| 4101 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4102 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4103 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4104 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4105 | /* we'll let the caller check if it has more headers to send */ |
Willy Tarreau | 7f437ff | 2018-09-11 13:51:19 +0200 | [diff] [blame] | 4106 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4107 | h1m->err_pos = -1; // don't care about errors on the response path |
Willy Tarreau | eb528db | 2018-09-12 09:54:00 +0200 | [diff] [blame] | 4108 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4109 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4110 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4111 | |
| 4112 | /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */ |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4113 | |
| 4114 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4115 | //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1m_state_str(h1m->err_state)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4116 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4117 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4118 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4119 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4120 | h1m_init_res(h1m); |
| 4121 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4122 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4123 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4124 | ret = 0; |
| 4125 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4128 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4129 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4130 | * the number of bytes sent. The caller must check the stream's status to |
| 4131 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4132 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4133 | static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4134 | { |
| 4135 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4136 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4137 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4138 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4139 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4140 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4141 | int es_now = 0; |
| 4142 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4143 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4144 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4145 | |
| 4146 | if (h2c_mux_busy(h2c, h2s)) { |
| 4147 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4148 | goto end; |
| 4149 | } |
| 4150 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4151 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4152 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4153 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4154 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4155 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4156 | goto end; |
| 4157 | } |
| 4158 | |
| 4159 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4160 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4161 | goto end; |
| 4162 | |
| 4163 | chunk_reset(&outbuf); |
| 4164 | |
| 4165 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4166 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4167 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4168 | break; |
| 4169 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4170 | /* If there are pending data in the output buffer, and we have |
| 4171 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4172 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4173 | * is full and wait, to save some slow realign calls. |
| 4174 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4175 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4176 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4177 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4178 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4179 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4180 | goto end; |
| 4181 | } |
| 4182 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4183 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4187 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4188 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4189 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4190 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4191 | goto end; |
| 4192 | } |
| 4193 | |
| 4194 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4195 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4196 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4197 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4198 | |
| 4199 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4200 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4201 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4202 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4203 | break; |
| 4204 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4205 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4206 | if ((long long)size > h1m->curr_len) |
| 4207 | size = h1m->curr_len; |
| 4208 | break; |
| 4209 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4210 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4211 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4212 | if (!ret) |
| 4213 | goto end; |
| 4214 | |
| 4215 | if (ret < 0) { |
| 4216 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4217 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4218 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4219 | goto end; |
| 4220 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4221 | max -= ret; |
| 4222 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4223 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4224 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4227 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4228 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4229 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4230 | if (!ret) |
| 4231 | goto end; |
| 4232 | |
| 4233 | if (ret < 0) { |
| 4234 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4235 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4236 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4237 | goto end; |
| 4238 | } |
| 4239 | |
| 4240 | size = chunk; |
| 4241 | h1m->curr_len = chunk; |
| 4242 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4243 | max -= ret; |
| 4244 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4245 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4246 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4247 | if (!size) |
| 4248 | goto send_empty; |
| 4249 | } |
| 4250 | |
| 4251 | /* in MSG_DATA state, continue below */ |
| 4252 | size = h1m->curr_len; |
| 4253 | break; |
| 4254 | } |
| 4255 | |
| 4256 | /* we have in <size> the exact number of bytes we need to copy from |
| 4257 | * the H1 buffer. We need to check this against the connection's and |
| 4258 | * the stream's send windows, and to ensure that this fits in the max |
| 4259 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4260 | * the frame header). The connection's flow control is applied last so |
| 4261 | * that we can use a separate list of streams which are immediately |
| 4262 | * unblocked on window opening. Note: we don't implement padding. |
| 4263 | */ |
| 4264 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4265 | if (size > max) |
| 4266 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4267 | |
| 4268 | if (size > h2s->mws) |
| 4269 | size = h2s->mws; |
| 4270 | |
| 4271 | if (size <= 0) { |
| 4272 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4273 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4274 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4275 | goto end; |
| 4276 | } |
| 4277 | |
| 4278 | if (h2c->mfs && size > h2c->mfs) |
| 4279 | size = h2c->mfs; |
| 4280 | |
| 4281 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4282 | /* It doesn't fit at once. If it at least fits once split and |
| 4283 | * the amount of data to move is low, let's defragment the |
| 4284 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4285 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4286 | if (b_space_wraps(mbuf) && |
| 4287 | (size + 9 <= b_room(mbuf)) && |
| 4288 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4289 | goto realign_again; |
| 4290 | size = outbuf.size - 9; |
| 4291 | } |
| 4292 | |
| 4293 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4294 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4295 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4296 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4297 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4298 | goto end; |
| 4299 | } |
| 4300 | |
| 4301 | if (size > h2c->mws) |
| 4302 | size = h2c->mws; |
| 4303 | |
| 4304 | if (size <= 0) { |
| 4305 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4306 | goto end; |
| 4307 | } |
| 4308 | |
| 4309 | /* copy whatever we can */ |
| 4310 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4311 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4312 | if (ret == 1) |
| 4313 | len2 = 0; |
| 4314 | |
| 4315 | if (!ret || len1 + len2 < size) { |
| 4316 | /* FIXME: must normally never happen */ |
| 4317 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4318 | goto end; |
| 4319 | } |
| 4320 | |
| 4321 | /* limit len1/len2 to size */ |
| 4322 | if (len1 + len2 > size) { |
| 4323 | int sub = len1 + len2 - size; |
| 4324 | |
| 4325 | if (len2 > sub) |
| 4326 | len2 -= sub; |
| 4327 | else { |
| 4328 | sub -= len2; |
| 4329 | len2 = 0; |
| 4330 | len1 -= sub; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4335 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4336 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4337 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4338 | |
| 4339 | send_empty: |
| 4340 | /* we may need to add END_STREAM */ |
| 4341 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4342 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4343 | * |
| 4344 | * FIXME: what we do here is not correct because we send end_stream |
| 4345 | * before knowing if we'll have to send a HEADERS frame for the |
| 4346 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4347 | * after the end of trailers, so it will be left to the caller to |
| 4348 | * eat it. The right way to do it would be to measure trailers here |
| 4349 | * and to send ES only if there are no trailers. |
| 4350 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4351 | */ |
| 4352 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4353 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4354 | es_now = 1; |
| 4355 | |
| 4356 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4357 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4358 | |
| 4359 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4360 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4361 | |
| 4362 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4363 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4364 | |
| 4365 | /* consume incoming H1 response */ |
| 4366 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4367 | max -= size; |
| 4368 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4369 | total += size; |
| 4370 | h1m->curr_len -= size; |
| 4371 | h2s->mws -= size; |
| 4372 | h2c->mws -= size; |
| 4373 | |
| 4374 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4375 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4376 | goto new_frame; |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | if (es_now) { |
| 4381 | if (h2s->st == H2_SS_OPEN) |
| 4382 | h2s->st = H2_SS_HLOC; |
| 4383 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4384 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4385 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4386 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4387 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4388 | total += max; |
| 4389 | ofs += max; |
| 4390 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4391 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4392 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4393 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4394 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4395 | h2s->flags |= H2_SF_ES_SENT; |
| 4396 | } |
| 4397 | |
| 4398 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4399 | trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4400 | return total; |
| 4401 | } |
| 4402 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4403 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4404 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4405 | * must check the stream's status to detect any error which might have happened |
| 4406 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4407 | * from the message. The htx message is assumed to be valid since produced from |
| 4408 | * the internal code, hence it contains a start line, an optional series of |
| 4409 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4410 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4411 | */ |
| 4412 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4413 | { |
| 4414 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4415 | struct h2c *h2c = h2s->h2c; |
| 4416 | struct htx_blk *blk; |
| 4417 | struct htx_blk *blk_end; |
| 4418 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4419 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4420 | struct htx_sl *sl; |
| 4421 | enum htx_blk_type type; |
| 4422 | int es_now = 0; |
| 4423 | int ret = 0; |
| 4424 | int hdr; |
| 4425 | int idx; |
| 4426 | |
| 4427 | if (h2c_mux_busy(h2c, h2s)) { |
| 4428 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4429 | return 0; |
| 4430 | } |
| 4431 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4432 | /* determine the first block which must not be deleted, blk_end may |
| 4433 | * be NULL if all blocks have to be deleted. |
| 4434 | */ |
| 4435 | idx = htx_get_head(htx); |
| 4436 | blk_end = NULL; |
| 4437 | while (idx != -1) { |
| 4438 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4439 | idx = htx_get_next(htx, idx); |
| 4440 | if (type == HTX_BLK_EOH) { |
| 4441 | if (idx != -1) |
| 4442 | blk_end = htx_get_blk(htx, idx); |
| 4443 | break; |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4448 | blk = htx_get_head_blk(htx); |
| 4449 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
| 4450 | ALREADY_CHECKED(blk); |
| 4451 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4452 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4453 | if (h2s->status < 100 || h2s->status > 999) |
| 4454 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4455 | |
| 4456 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4457 | hdr = 0; |
| 4458 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4459 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4460 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4461 | blk = htx_get_blk(htx, idx); |
| 4462 | type = htx_get_blk_type(blk); |
| 4463 | |
| 4464 | if (type == HTX_BLK_UNUSED) |
| 4465 | continue; |
| 4466 | |
| 4467 | if (type != HTX_BLK_HDR) |
| 4468 | break; |
| 4469 | |
| 4470 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4471 | goto fail; |
| 4472 | |
| 4473 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4474 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4475 | hdr++; |
| 4476 | } |
| 4477 | |
| 4478 | /* marker for end of headers */ |
| 4479 | list[hdr].n = ist(""); |
| 4480 | |
| 4481 | if (h2s->status == 204 || h2s->status == 304) { |
| 4482 | /* no contents, claim c-len is present and set to zero */ |
| 4483 | es_now = 1; |
| 4484 | } |
| 4485 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4486 | mbuf = br_tail(h2c->mbuf); |
| 4487 | retry: |
| 4488 | if (!h2_get_buf(h2c, mbuf)) { |
| 4489 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4490 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4491 | return 0; |
| 4492 | } |
| 4493 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4494 | chunk_reset(&outbuf); |
| 4495 | |
| 4496 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4497 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4498 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4499 | break; |
| 4500 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4501 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | if (outbuf.size < 9) |
| 4505 | goto full; |
| 4506 | |
| 4507 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4508 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4509 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4510 | outbuf.data = 9; |
| 4511 | |
| 4512 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4513 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4514 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4515 | goto realign_again; |
| 4516 | goto full; |
| 4517 | } |
| 4518 | |
| 4519 | /* encode all headers, stop at empty name */ |
| 4520 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4521 | /* these ones do not exist in H2 and must be dropped. */ |
| 4522 | if (isteq(list[hdr].n, ist("connection")) || |
| 4523 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4524 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4525 | isteq(list[hdr].n, ist("upgrade")) || |
| 4526 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4527 | continue; |
| 4528 | |
| 4529 | if (isteq(list[hdr].n, ist(""))) |
| 4530 | break; // end |
| 4531 | |
| 4532 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4533 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4534 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4535 | goto realign_again; |
| 4536 | goto full; |
| 4537 | } |
| 4538 | } |
| 4539 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4540 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4541 | * FIXME: we should also set it when we know for sure that the |
| 4542 | * content-length is zero as well as on 204/304 |
| 4543 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4544 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4545 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4546 | es_now = 1; |
| 4547 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4548 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4549 | es_now = 1; |
| 4550 | |
| 4551 | /* update the frame's size */ |
| 4552 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4553 | |
| 4554 | if (es_now) |
| 4555 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4556 | |
| 4557 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4558 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4559 | |
| 4560 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4561 | * 1xx responses, another HEADERS frame is expected. |
| 4562 | */ |
| 4563 | if (h2s->status >= 200 || h2s->status == 101) |
| 4564 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4565 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4566 | if (es_now) { |
| 4567 | h2s->flags |= H2_SF_ES_SENT; |
| 4568 | if (h2s->st == H2_SS_OPEN) |
| 4569 | h2s->st = H2_SS_HLOC; |
| 4570 | else |
| 4571 | h2s_close(h2s); |
| 4572 | } |
| 4573 | |
| 4574 | /* OK we could properly deliver the response */ |
| 4575 | |
| 4576 | /* remove all header blocks including the EOH and compute the |
| 4577 | * corresponding size. |
| 4578 | * |
| 4579 | * FIXME: We should remove everything when es_now is set. |
| 4580 | */ |
| 4581 | ret = 0; |
| 4582 | idx = htx_get_head(htx); |
| 4583 | blk = htx_get_blk(htx, idx); |
| 4584 | while (blk != blk_end) { |
| 4585 | ret += htx_get_blksz(blk); |
| 4586 | blk = htx_remove_blk(htx, blk); |
| 4587 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4588 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4589 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4590 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4591 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4592 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4593 | end: |
| 4594 | return ret; |
| 4595 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4596 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4597 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4598 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4599 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4600 | ret = 0; |
| 4601 | goto end; |
| 4602 | fail: |
| 4603 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4604 | * list etc go here (unrecoverable errors). |
| 4605 | */ |
| 4606 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4607 | ret = 0; |
| 4608 | goto end; |
| 4609 | } |
| 4610 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4611 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4612 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4613 | * must check the stream's status to detect any error which might have happened |
| 4614 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4615 | * from the message. The htx message is assumed to be valid since produced from |
| 4616 | * the internal code, hence it contains a start line, an optional series of |
| 4617 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4618 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4619 | */ |
| 4620 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4621 | { |
| 4622 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4623 | struct h2c *h2c = h2s->h2c; |
| 4624 | struct htx_blk *blk; |
| 4625 | struct htx_blk *blk_end; |
| 4626 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4627 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4628 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4629 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4630 | enum htx_blk_type type; |
| 4631 | int es_now = 0; |
| 4632 | int ret = 0; |
| 4633 | int hdr; |
| 4634 | int idx; |
| 4635 | |
| 4636 | if (h2c_mux_busy(h2c, h2s)) { |
| 4637 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4638 | return 0; |
| 4639 | } |
| 4640 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4641 | /* determine the first block which must not be deleted, blk_end may |
| 4642 | * be NULL if all blocks have to be deleted. |
| 4643 | */ |
| 4644 | idx = htx_get_head(htx); |
| 4645 | blk_end = NULL; |
| 4646 | while (idx != -1) { |
| 4647 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4648 | idx = htx_get_next(htx, idx); |
| 4649 | if (type == HTX_BLK_EOH) { |
| 4650 | if (idx != -1) |
| 4651 | blk_end = htx_get_blk(htx, idx); |
| 4652 | break; |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4657 | blk = htx_get_head_blk(htx); |
| 4658 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 4659 | ALREADY_CHECKED(blk); |
| 4660 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4661 | meth = htx_sl_req_meth(sl); |
| 4662 | path = htx_sl_req_uri(sl); |
| 4663 | |
| 4664 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4665 | hdr = 0; |
| 4666 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4667 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4668 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4669 | blk = htx_get_blk(htx, idx); |
| 4670 | type = htx_get_blk_type(blk); |
| 4671 | |
| 4672 | if (type == HTX_BLK_UNUSED) |
| 4673 | continue; |
| 4674 | |
| 4675 | if (type != HTX_BLK_HDR) |
| 4676 | break; |
| 4677 | |
| 4678 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4679 | goto fail; |
| 4680 | |
| 4681 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4682 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4683 | hdr++; |
| 4684 | } |
| 4685 | |
| 4686 | /* marker for end of headers */ |
| 4687 | list[hdr].n = ist(""); |
| 4688 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4689 | mbuf = br_tail(h2c->mbuf); |
| 4690 | retry: |
| 4691 | if (!h2_get_buf(h2c, mbuf)) { |
| 4692 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4693 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4694 | return 0; |
| 4695 | } |
| 4696 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4697 | chunk_reset(&outbuf); |
| 4698 | |
| 4699 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4700 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4701 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4702 | break; |
| 4703 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4704 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | if (outbuf.size < 9) |
| 4708 | goto full; |
| 4709 | |
| 4710 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4711 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4712 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4713 | outbuf.data = 9; |
| 4714 | |
| 4715 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4716 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4717 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4718 | goto realign_again; |
| 4719 | goto full; |
| 4720 | } |
| 4721 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4722 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4723 | * - :authority set to the URI part (host:port) |
| 4724 | * - :method set to CONNECT |
| 4725 | * - :scheme and :path omitted |
| 4726 | */ |
| 4727 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4728 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4729 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4730 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4731 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4732 | goto realign_again; |
| 4733 | goto full; |
| 4734 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4735 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4736 | /* encode the path, which necessarily is the second one */ |
| 4737 | if (!hpack_encode_path(&outbuf, path)) { |
| 4738 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4739 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4740 | goto realign_again; |
| 4741 | goto full; |
| 4742 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4743 | |
| 4744 | /* look for the Host header and place it in :authority */ |
| 4745 | auth = ist2(NULL, 0); |
| 4746 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4747 | if (isteq(list[hdr].n, ist(""))) |
| 4748 | break; // end |
| 4749 | |
| 4750 | if (isteq(list[hdr].n, ist("host"))) { |
| 4751 | auth = list[hdr].v; |
| 4752 | break; |
| 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | else { |
| 4757 | /* for CONNECT, :authority is taken from the path */ |
| 4758 | auth = path; |
| 4759 | } |
| 4760 | |
| 4761 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4762 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4763 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4764 | goto realign_again; |
| 4765 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | /* encode all headers, stop at empty name */ |
| 4769 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4770 | /* these ones do not exist in H2 and must be dropped. */ |
| 4771 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4772 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4773 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4774 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4775 | isteq(list[hdr].n, ist("upgrade")) || |
| 4776 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4777 | continue; |
| 4778 | |
| 4779 | if (isteq(list[hdr].n, ist(""))) |
| 4780 | break; // end |
| 4781 | |
| 4782 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4783 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4784 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4785 | goto realign_again; |
| 4786 | goto full; |
| 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | /* we may need to add END_STREAM if we have no body : |
| 4791 | * - request already closed, or : |
| 4792 | * - no transfer-encoding, and : |
| 4793 | * - no content-length or content-length:0 |
| 4794 | * Fixme: this doesn't take into account CONNECT requests. |
| 4795 | */ |
| 4796 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4797 | es_now = 1; |
| 4798 | |
| 4799 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4800 | es_now = 1; |
| 4801 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4802 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4803 | es_now = 1; |
| 4804 | |
| 4805 | /* update the frame's size */ |
| 4806 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4807 | |
| 4808 | if (es_now) |
| 4809 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4810 | |
| 4811 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4812 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4813 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4814 | h2s->st = H2_SS_OPEN; |
| 4815 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4816 | if (es_now) { |
| 4817 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4818 | h2s->flags |= H2_SF_ES_SENT; |
| 4819 | h2s->st = H2_SS_HLOC; |
| 4820 | } |
| 4821 | |
| 4822 | /* remove all header blocks including the EOH and compute the |
| 4823 | * corresponding size. |
| 4824 | * |
| 4825 | * FIXME: We should remove everything when es_now is set. |
| 4826 | */ |
| 4827 | ret = 0; |
| 4828 | idx = htx_get_head(htx); |
| 4829 | blk = htx_get_blk(htx, idx); |
| 4830 | while (blk != blk_end) { |
| 4831 | ret += htx_get_blksz(blk); |
| 4832 | blk = htx_remove_blk(htx, blk); |
| 4833 | } |
| 4834 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4835 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4836 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4837 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4838 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4839 | |
| 4840 | end: |
| 4841 | return ret; |
| 4842 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4843 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4844 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4845 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4846 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4847 | ret = 0; |
| 4848 | goto end; |
| 4849 | fail: |
| 4850 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4851 | * list etc go here (unrecoverable errors). |
| 4852 | */ |
| 4853 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4854 | ret = 0; |
| 4855 | goto end; |
| 4856 | } |
| 4857 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4858 | /* 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] | 4859 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4860 | * caller must check the stream's status to detect any error which might have |
| 4861 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4862 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4863 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4864 | static size_t h2s_htx_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] | 4865 | { |
| 4866 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4867 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4868 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4869 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4870 | size_t total = 0; |
| 4871 | int es_now = 0; |
| 4872 | int bsize; /* htx block size */ |
| 4873 | int fsize; /* h2 frame size */ |
| 4874 | struct htx_blk *blk; |
| 4875 | enum htx_blk_type type; |
| 4876 | int idx; |
| 4877 | |
| 4878 | if (h2c_mux_busy(h2c, h2s)) { |
| 4879 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4880 | goto end; |
| 4881 | } |
| 4882 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4883 | htx = htx_from_buf(buf); |
| 4884 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4885 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4886 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4887 | * the caller to handle. |
| 4888 | */ |
| 4889 | |
| 4890 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4891 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4892 | goto end; |
| 4893 | |
| 4894 | idx = htx_get_head(htx); |
| 4895 | blk = htx_get_blk(htx, idx); |
| 4896 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4897 | bsize = htx_get_blksz(blk); |
| 4898 | fsize = bsize; |
| 4899 | |
| 4900 | if (type == HTX_BLK_EOD) { |
| 4901 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4902 | * have a set of trailers after us that the caller will want to |
| 4903 | * deal with. Let's simply remove the EOD and return. |
| 4904 | */ |
| 4905 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4906 | total++; // EOD counts as one byte |
| 4907 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4908 | goto end; |
| 4909 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4910 | else if (type == HTX_BLK_EOM) { |
| 4911 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4912 | /* ES already sent */ |
| 4913 | htx_remove_blk(htx, blk); |
| 4914 | total++; // EOM counts as one byte |
| 4915 | count--; |
| 4916 | goto end; |
| 4917 | } |
| 4918 | } |
| 4919 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4920 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4921 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4922 | mbuf = br_tail(h2c->mbuf); |
| 4923 | retry: |
| 4924 | if (!h2_get_buf(h2c, mbuf)) { |
| 4925 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4926 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4927 | goto end; |
| 4928 | } |
| 4929 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4930 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4931 | * First, if the mux's buffer is empty and the htx area contains |
| 4932 | * exactly one data block of the same size as the requested count, and |
| 4933 | * this count fits within the frame size, the stream's window size, and |
| 4934 | * the connection's window size, then it's possible to simply swap the |
| 4935 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4936 | * length to match the entire DATA HTX block in the middle. In this |
| 4937 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4938 | * the situation that happens all the time with large files. Second, if |
| 4939 | * this is not possible, but the mux's output buffer is empty, we still |
| 4940 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4941 | * making the intermediary buffer's area point to the output buffer's |
| 4942 | * area. In this case we want to skip the HTX header to make sure that |
| 4943 | * copies remain aligned and that this operation remains possible all |
| 4944 | * the time. This goes for headers, data blocks and any data extracted |
| 4945 | * from the HTX blocks. |
| 4946 | */ |
| 4947 | if (unlikely(fsize == count && |
| 4948 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4949 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4950 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4951 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4952 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4953 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4954 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4955 | * frame into a buffer containing few data if it needs to be realigned, |
| 4956 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4957 | * 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] | 4958 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4959 | if (fsize + 9 <= b_room(mbuf) && |
| 4960 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 4961 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4962 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4963 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4964 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4965 | goto retry; |
| 4966 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4967 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4968 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4969 | goto end; |
| 4970 | } |
| 4971 | |
| 4972 | /* map an H2 frame to the HTX block so that we can put the |
| 4973 | * frame header there. |
| 4974 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4975 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 4976 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4977 | |
| 4978 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4979 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4980 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4981 | h2_set_frame_size(outbuf.area, fsize); |
| 4982 | |
| 4983 | /* update windows */ |
| 4984 | h2s->mws -= fsize; |
| 4985 | h2c->mws -= fsize; |
| 4986 | |
| 4987 | /* and exchange with our old area */ |
| 4988 | buf->area = old_area; |
| 4989 | buf->data = buf->head = 0; |
| 4990 | total += fsize; |
| 4991 | goto end; |
| 4992 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4993 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4994 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4995 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4996 | |
| 4997 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4998 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4999 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5000 | break; |
| 5001 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5002 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5006 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5007 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5008 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5009 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5010 | goto end; |
| 5011 | } |
| 5012 | |
| 5013 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5014 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5015 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5016 | outbuf.data = 9; |
| 5017 | |
| 5018 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5019 | * the HTX buffer. We need to check this against the connection's and |
| 5020 | * the stream's send windows, and to ensure that this fits in the max |
| 5021 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5022 | * the frame header). The connection's flow control is applied last so |
| 5023 | * that we can use a separate list of streams which are immediately |
| 5024 | * unblocked on window opening. Note: we don't implement padding. |
| 5025 | */ |
| 5026 | |
| 5027 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5028 | * empty frame, thus we force it to zero here. |
| 5029 | */ |
| 5030 | if (type == HTX_BLK_EOM) |
| 5031 | bsize = fsize = 0; |
| 5032 | |
| 5033 | if (!fsize) |
| 5034 | goto send_empty; |
| 5035 | |
| 5036 | if (h2s->mws <= 0) { |
| 5037 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5038 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5039 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5040 | goto end; |
| 5041 | } |
| 5042 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5043 | if (fsize > count) |
| 5044 | fsize = count; |
| 5045 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5046 | if (fsize > h2s->mws) |
| 5047 | fsize = h2s->mws; // >0 |
| 5048 | |
| 5049 | if (h2c->mfs && fsize > h2c->mfs) |
| 5050 | fsize = h2c->mfs; // >0 |
| 5051 | |
| 5052 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5053 | /* It doesn't fit at once. If it at least fits once split and |
| 5054 | * the amount of data to move is low, let's defragment the |
| 5055 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5056 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5057 | if (b_space_wraps(mbuf) && |
| 5058 | (fsize + 9 <= b_room(mbuf)) && |
| 5059 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5060 | goto realign_again; |
| 5061 | fsize = outbuf.size - 9; |
| 5062 | |
| 5063 | if (fsize <= 0) { |
| 5064 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5065 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5066 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5067 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5068 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5069 | goto end; |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | if (h2c->mws <= 0) { |
| 5074 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5075 | goto end; |
| 5076 | } |
| 5077 | |
| 5078 | if (fsize > h2c->mws) |
| 5079 | fsize = h2c->mws; |
| 5080 | |
| 5081 | /* now let's copy this this into the output buffer */ |
| 5082 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5083 | h2s->mws -= fsize; |
| 5084 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5085 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5086 | |
| 5087 | send_empty: |
| 5088 | /* update the frame's size */ |
| 5089 | h2_set_frame_size(outbuf.area, fsize); |
| 5090 | |
| 5091 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5092 | * meeting EOM. We should optimize this later. |
| 5093 | */ |
| 5094 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5095 | total++; // EOM counts as one byte |
| 5096 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5097 | es_now = 1; |
| 5098 | } |
| 5099 | |
| 5100 | if (es_now) |
| 5101 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5102 | |
| 5103 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5104 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5105 | |
| 5106 | /* consume incoming HTX block, including EOM */ |
| 5107 | total += fsize; |
| 5108 | if (fsize == bsize) { |
| 5109 | htx_remove_blk(htx, blk); |
| 5110 | if (fsize) |
| 5111 | goto new_frame; |
| 5112 | } else { |
| 5113 | /* we've truncated this block */ |
| 5114 | htx_cut_data_blk(htx, blk, fsize); |
| 5115 | } |
| 5116 | |
| 5117 | if (es_now) { |
| 5118 | if (h2s->st == H2_SS_OPEN) |
| 5119 | h2s->st = H2_SS_HLOC; |
| 5120 | else |
| 5121 | h2s_close(h2s); |
| 5122 | |
| 5123 | h2s->flags |= H2_SF_ES_SENT; |
| 5124 | } |
| 5125 | |
| 5126 | end: |
| 5127 | return total; |
| 5128 | } |
| 5129 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5130 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5131 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5132 | * processed. The caller must check the stream's status to detect any error |
| 5133 | * which might have happened subsequently to a successful send. The htx blocks |
| 5134 | * are automatically removed from the message. The htx message is assumed to be |
| 5135 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5136 | * 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] | 5137 | * as a single frame. The ES flag is always set. |
| 5138 | */ |
| 5139 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5140 | { |
| 5141 | struct http_hdr list[MAX_HTTP_HDR]; |
| 5142 | struct h2c *h2c = h2s->h2c; |
| 5143 | struct htx_blk *blk; |
| 5144 | struct htx_blk *blk_end; |
| 5145 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5146 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5147 | struct h1m h1m; |
| 5148 | enum htx_blk_type type; |
| 5149 | uint32_t size; |
| 5150 | int ret = 0; |
| 5151 | int hdr; |
| 5152 | int idx; |
| 5153 | void *start; |
| 5154 | |
| 5155 | if (h2c_mux_busy(h2c, h2s)) { |
| 5156 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5157 | goto end; |
| 5158 | } |
| 5159 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5160 | /* The principle is that we parse each and every trailers block using |
| 5161 | * the H1 headers parser, and append it to the list. We don't proceed |
| 5162 | * until EOM is met. blk_end will point to the EOM block. |
| 5163 | */ |
| 5164 | hdr = 0; |
| 5165 | memset(list, 0, sizeof(list)); |
| 5166 | blk_end = NULL; |
| 5167 | |
| 5168 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 5169 | blk = htx_get_blk(htx, idx); |
| 5170 | type = htx_get_blk_type(blk); |
| 5171 | |
| 5172 | if (type == HTX_BLK_UNUSED) |
| 5173 | continue; |
| 5174 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5175 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5176 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5177 | |
| 5178 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5179 | goto fail; |
| 5180 | |
| 5181 | size = htx_get_blksz(blk); |
| 5182 | start = htx_get_blk_ptr(htx, blk); |
| 5183 | |
| 5184 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 5185 | h1m.err_pos = 0; |
| 5186 | ret = h1_headers_to_hdr_list(start, start + size, |
| 5187 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 5188 | &h1m, NULL); |
| 5189 | if (ret < 0) |
| 5190 | goto fail; |
| 5191 | |
| 5192 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5193 | * empty line), or > 0 if it was found. We have to continue on |
| 5194 | * incomplete messages because the trailers block might be |
| 5195 | * incomplete. |
| 5196 | */ |
| 5197 | |
| 5198 | /* search the new end */ |
| 5199 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5200 | if (!list[hdr].n.len) |
| 5201 | break; |
| 5202 | hdr++; |
| 5203 | } |
| 5204 | } |
| 5205 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5206 | if (list[hdr].n.len != 0) |
| 5207 | goto fail; // empty trailer not found: internal error |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5208 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5209 | mbuf = br_tail(h2c->mbuf); |
| 5210 | retry: |
| 5211 | if (!h2_get_buf(h2c, mbuf)) { |
| 5212 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5213 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5214 | goto end; |
| 5215 | } |
| 5216 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5217 | chunk_reset(&outbuf); |
| 5218 | |
| 5219 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5220 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5221 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5222 | break; |
| 5223 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5224 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | if (outbuf.size < 9) |
| 5228 | goto full; |
| 5229 | |
| 5230 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5231 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5232 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5233 | outbuf.data = 9; |
| 5234 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5235 | /* encode all headers */ |
| 5236 | for (idx = 0; idx < hdr; idx++) { |
| 5237 | /* these ones do not exist in H2 or must not appear in |
| 5238 | * trailers and must be dropped. |
| 5239 | */ |
| 5240 | if (isteq(list[idx].n, ist("host")) || |
| 5241 | isteq(list[idx].n, ist("content-length")) || |
| 5242 | isteq(list[idx].n, ist("connection")) || |
| 5243 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5244 | isteq(list[idx].n, ist("keep-alive")) || |
| 5245 | isteq(list[idx].n, ist("upgrade")) || |
| 5246 | isteq(list[idx].n, ist("te")) || |
| 5247 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5248 | continue; |
| 5249 | |
| 5250 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5251 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5252 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5253 | goto realign_again; |
| 5254 | goto full; |
| 5255 | } |
| 5256 | } |
| 5257 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5258 | if (outbuf.data == 9) { |
| 5259 | /* here we have a problem, we have nothing to emit (either we |
| 5260 | * received an empty trailers block followed or we removed its |
| 5261 | * contents above). Because of this we can't send a HEADERS |
| 5262 | * frame, so we have to cheat and instead send an empty DATA |
| 5263 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5264 | */ |
| 5265 | outbuf.area[3] = H2_FT_DATA; |
| 5266 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5267 | } |
| 5268 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5269 | /* update the frame's size */ |
| 5270 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5271 | |
| 5272 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5273 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5274 | h2s->flags |= H2_SF_ES_SENT; |
| 5275 | |
| 5276 | if (h2s->st == H2_SS_OPEN) |
| 5277 | h2s->st = H2_SS_HLOC; |
| 5278 | else |
| 5279 | h2s_close(h2s); |
| 5280 | |
| 5281 | /* OK we could properly deliver the response */ |
| 5282 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5283 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5284 | ret = 0; |
| 5285 | idx = htx_get_head(htx); |
| 5286 | blk = htx_get_blk(htx, idx); |
| 5287 | while (blk != blk_end) { |
| 5288 | ret += htx_get_blksz(blk); |
| 5289 | blk = htx_remove_blk(htx, blk); |
| 5290 | } |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5291 | end: |
| 5292 | return ret; |
| 5293 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5294 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5295 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5296 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5297 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5298 | ret = 0; |
| 5299 | goto end; |
| 5300 | fail: |
| 5301 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5302 | * list etc go here (unrecoverable errors). |
| 5303 | */ |
| 5304 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5305 | ret = 0; |
| 5306 | goto end; |
| 5307 | } |
| 5308 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5309 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5310 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5311 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5312 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5313 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5314 | * returns 0 except for the error above. |
| 5315 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5316 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5317 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5318 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5319 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5320 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5321 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5322 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5323 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5324 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5325 | sw->events |= SUB_RETRY_RECV; |
| 5326 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5327 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5328 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5329 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5330 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5331 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5332 | sw->events |= SUB_RETRY_SEND; |
| 5333 | h2s->send_wait = sw; |
| 5334 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5335 | !LIST_ADDED(&h2s->list)) { |
| 5336 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5337 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5338 | else |
| 5339 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5340 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5341 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5342 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5343 | if (event_type != 0) |
| 5344 | return -1; |
| 5345 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5346 | } |
| 5347 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5348 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5349 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5350 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5351 | * It always returns zero. |
| 5352 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5353 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5354 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5355 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5356 | struct h2s *h2s = cs->ctx; |
| 5357 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5358 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5359 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5360 | BUG_ON(h2s->recv_wait != sw); |
| 5361 | sw->events &= ~SUB_RETRY_RECV; |
| 5362 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5363 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5364 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5365 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5366 | BUG_ON(h2s->send_wait != sw); |
| 5367 | LIST_DEL(&h2s->list); |
| 5368 | LIST_INIT(&h2s->list); |
| 5369 | sw->events &= ~SUB_RETRY_SEND; |
| 5370 | /* We were about to send, make sure it does not happen */ |
| 5371 | if (LIST_ADDED(&h2s->sending_list) && |
| 5372 | h2s->send_wait != &h2s->wait_event) { |
| 5373 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 5374 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5375 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5376 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5377 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5378 | return 0; |
| 5379 | } |
| 5380 | |
| 5381 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5382 | /* Called from the upper layer, to receive data */ |
| 5383 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5384 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5385 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5386 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5387 | struct htx *h2s_htx = NULL; |
| 5388 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5389 | size_t ret = 0; |
| 5390 | |
| 5391 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5392 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5393 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5394 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5395 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5396 | * the HTX is empty. |
| 5397 | */ |
| 5398 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5399 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5400 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5401 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5402 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5403 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5404 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5405 | /* <buf> is empty and the message is small enough, swap the |
| 5406 | * buffers. */ |
| 5407 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5408 | htx_to_buf(buf_htx, buf); |
| 5409 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5410 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5411 | goto end; |
| 5412 | } |
| 5413 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5414 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5415 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5416 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5417 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5418 | if (htx_is_empty(buf_htx)) |
| 5419 | cs->flags |= CS_FL_EOI; |
| 5420 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5421 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5422 | buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5423 | htx_to_buf(buf_htx, buf); |
| 5424 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5425 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5426 | } |
| 5427 | else { |
| 5428 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5429 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5430 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5431 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5432 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5433 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5434 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5435 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5436 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5437 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 5438 | if (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5439 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5440 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5441 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5442 | if (b_size(&h2s->rxbuf)) { |
| 5443 | b_free(&h2s->rxbuf); |
| 5444 | offer_buffers(NULL, tasks_run_queue); |
| 5445 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5446 | } |
| 5447 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5448 | if (ret && h2c->dsi == h2s->id) { |
| 5449 | /* demux is blocking on this stream's buffer */ |
| 5450 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5451 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5452 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5453 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5454 | return ret; |
| 5455 | } |
| 5456 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5457 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5458 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5459 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5460 | static void h2_stop_senders(struct h2c *h2c) |
| 5461 | { |
| 5462 | struct h2s *h2s, *h2s_back; |
| 5463 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5464 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5465 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5466 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5467 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5468 | } |
| 5469 | } |
| 5470 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5471 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5472 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5473 | * flags may be updated on the conn_stream. |
| 5474 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5475 | 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] | 5476 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5477 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5478 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5479 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5480 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5481 | struct htx *htx; |
| 5482 | struct htx_blk *blk; |
| 5483 | enum htx_blk_type btype; |
| 5484 | uint32_t bsize; |
| 5485 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5486 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5487 | /* If we were not just woken because we wanted to send but couldn't, |
| 5488 | * and there's somebody else that is waiting to send, do nothing, |
| 5489 | * we will subscribe later and be put at the end of the list |
| 5490 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5491 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5492 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5493 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5494 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5495 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5496 | /* We couldn't set it to NULL before, because we needed it in case |
| 5497 | * we had to cancel the tasklet |
| 5498 | */ |
| 5499 | h2s->send_wait = NULL; |
| 5500 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5501 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5502 | return 0; |
| 5503 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5504 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5505 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5506 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5507 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5508 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5509 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5510 | if (h2s->id == 0) { |
| 5511 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5512 | |
| 5513 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5514 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | eb32_delete(&h2s->by_id); |
| 5519 | h2s->by_id.key = h2s->id = id; |
| 5520 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5521 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5522 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5523 | } |
| 5524 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5525 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5526 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5527 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5528 | idx = htx_get_head(htx); |
| 5529 | blk = htx_get_blk(htx, idx); |
| 5530 | btype = htx_get_blk_type(blk); |
| 5531 | bsize = htx_get_blksz(blk); |
| 5532 | |
| 5533 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5534 | case HTX_BLK_REQ_SL: |
| 5535 | /* start-line before headers */ |
| 5536 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5537 | if (ret > 0) { |
| 5538 | total += ret; |
| 5539 | count -= ret; |
| 5540 | if (ret < bsize) |
| 5541 | goto done; |
| 5542 | } |
| 5543 | break; |
| 5544 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5545 | case HTX_BLK_RES_SL: |
| 5546 | /* start-line before headers */ |
| 5547 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5548 | if (ret > 0) { |
| 5549 | total += ret; |
| 5550 | count -= ret; |
| 5551 | if (ret < bsize) |
| 5552 | goto done; |
| 5553 | } |
| 5554 | break; |
| 5555 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5556 | case HTX_BLK_DATA: |
| 5557 | case HTX_BLK_EOD: |
| 5558 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5559 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5560 | * This EOM necessarily is one before trailers, as the EOM following |
| 5561 | * trailers would have been consumed by the trailers parser. |
| 5562 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5563 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5564 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5565 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5566 | total += ret; |
| 5567 | count -= ret; |
| 5568 | if (ret < bsize) |
| 5569 | goto done; |
| 5570 | } |
| 5571 | break; |
| 5572 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5573 | case HTX_BLK_TLR: |
| 5574 | /* This is the first trailers block, all the subsequent ones AND |
| 5575 | * the EOM will be swallowed by the parser. |
| 5576 | */ |
| 5577 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5578 | if (ret > 0) { |
| 5579 | total += ret; |
| 5580 | count -= ret; |
| 5581 | if (ret < bsize) |
| 5582 | goto done; |
| 5583 | } |
| 5584 | break; |
| 5585 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5586 | default: |
| 5587 | htx_remove_blk(htx, blk); |
| 5588 | total += bsize; |
| 5589 | count -= bsize; |
| 5590 | break; |
| 5591 | } |
| 5592 | } |
| 5593 | goto done; |
| 5594 | } |
| 5595 | |
| 5596 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5597 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5598 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5599 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5600 | ret = -1; |
| 5601 | else |
| 5602 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5603 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5604 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5605 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5606 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5607 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5608 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5609 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5610 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5611 | if (unlikely((int)ret <= 0)) { |
| 5612 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5613 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5614 | break; |
| 5615 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5616 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5617 | total += count; |
| 5618 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5619 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5620 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5621 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5622 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5623 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5624 | break; |
| 5625 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5626 | |
| 5627 | total += ret; |
| 5628 | count -= ret; |
| 5629 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5630 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5631 | break; |
| 5632 | |
| 5633 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5634 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5635 | } |
| 5636 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5637 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5638 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5639 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5640 | * unprocessed trailers, abnormal extra data, ...) |
| 5641 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5642 | total += count; |
| 5643 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5644 | } |
| 5645 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5646 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5647 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5648 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5649 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5650 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5651 | } |
| 5652 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5653 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5654 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5655 | } else { |
| 5656 | b_del(buf, total); |
| 5657 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5658 | |
| 5659 | /* The mux is full, cancel the pending tasks */ |
| 5660 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5661 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5662 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5663 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5664 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5665 | * we read exactly what the caller specified, as with HTX the caller |
| 5666 | * will always give the buffer size, instead of the amount of data |
| 5667 | * available. |
| 5668 | */ |
| 5669 | if (htx && !b_data(buf)) |
| 5670 | total = orig_count; |
| 5671 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5672 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5673 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5674 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5675 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5676 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5677 | /* If we're waiting for flow control, and we got a shutr on the |
| 5678 | * connection, we will never be unlocked, so add an error on |
| 5679 | * the conn_stream. |
| 5680 | */ |
| 5681 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5682 | !b_data(&h2s->h2c->dbuf) && |
| 5683 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5684 | if (cs->flags & CS_FL_EOS) |
| 5685 | cs->flags |= CS_FL_ERROR; |
| 5686 | else |
| 5687 | cs->flags |= CS_FL_ERR_PENDING; |
| 5688 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5689 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5690 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5691 | LIST_DEL_INIT(&h2s->list); |
| 5692 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5693 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5694 | } |
| 5695 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5696 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5697 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5698 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5699 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5700 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5701 | struct eb32_node *node; |
| 5702 | int fctl_cnt = 0; |
| 5703 | int send_cnt = 0; |
| 5704 | int tree_cnt = 0; |
| 5705 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5706 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5707 | |
| 5708 | if (!h2c) |
| 5709 | return; |
| 5710 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5711 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5712 | fctl_cnt++; |
| 5713 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5714 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5715 | send_cnt++; |
| 5716 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5717 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5718 | node = eb32_first(&h2c->streams_by_id); |
| 5719 | while (node) { |
| 5720 | h2s = container_of(node, struct h2s, by_id); |
| 5721 | tree_cnt++; |
| 5722 | if (!h2s->cs) |
| 5723 | orph_cnt++; |
| 5724 | node = eb32_next(node); |
| 5725 | } |
| 5726 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5727 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5728 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5729 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5730 | " .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] | 5731 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5732 | " .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] | 5733 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5734 | 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] | 5735 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5736 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5737 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5738 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5739 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5740 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5741 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5742 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5743 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5744 | |
| 5745 | if (h2s) { |
| 5746 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5747 | h2s, h2s->id, h2s->flags, |
| 5748 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5749 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5750 | h2s->cs); |
| 5751 | if (h2s->cs) |
| 5752 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5753 | h2s->cs->flags, h2s->cs->data); |
| 5754 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5755 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5756 | |
| 5757 | /*******************************************************/ |
| 5758 | /* functions below are dedicated to the config parsers */ |
| 5759 | /*******************************************************/ |
| 5760 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5761 | /* config parser for global "tune.h2.header-table-size" */ |
| 5762 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5763 | struct proxy *defpx, const char *file, int line, |
| 5764 | char **err) |
| 5765 | { |
| 5766 | if (too_many_args(1, args, err, NULL)) |
| 5767 | return -1; |
| 5768 | |
| 5769 | h2_settings_header_table_size = atoi(args[1]); |
| 5770 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5771 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5772 | return -1; |
| 5773 | } |
| 5774 | return 0; |
| 5775 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5776 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5777 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5778 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5779 | struct proxy *defpx, const char *file, int line, |
| 5780 | char **err) |
| 5781 | { |
| 5782 | if (too_many_args(1, args, err, NULL)) |
| 5783 | return -1; |
| 5784 | |
| 5785 | h2_settings_initial_window_size = atoi(args[1]); |
| 5786 | if (h2_settings_initial_window_size < 0) { |
| 5787 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5788 | return -1; |
| 5789 | } |
| 5790 | return 0; |
| 5791 | } |
| 5792 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5793 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5794 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5795 | struct proxy *defpx, const char *file, int line, |
| 5796 | char **err) |
| 5797 | { |
| 5798 | if (too_many_args(1, args, err, NULL)) |
| 5799 | return -1; |
| 5800 | |
| 5801 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5802 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5803 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5804 | return -1; |
| 5805 | } |
| 5806 | return 0; |
| 5807 | } |
| 5808 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5809 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5810 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5811 | struct proxy *defpx, const char *file, int line, |
| 5812 | char **err) |
| 5813 | { |
| 5814 | if (too_many_args(1, args, err, NULL)) |
| 5815 | return -1; |
| 5816 | |
| 5817 | h2_settings_max_frame_size = atoi(args[1]); |
| 5818 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5819 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5820 | return -1; |
| 5821 | } |
| 5822 | return 0; |
| 5823 | } |
| 5824 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5825 | |
| 5826 | /****************************************/ |
| 5827 | /* MUX initialization and instanciation */ |
| 5828 | /***************************************/ |
| 5829 | |
| 5830 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5831 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5832 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5833 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5834 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5835 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5836 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5837 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5838 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5839 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5840 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5841 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5842 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5843 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5844 | .shutr = h2_shutr, |
| 5845 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5846 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5847 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5848 | .name = "H2", |
| 5849 | }; |
| 5850 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5851 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5852 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5853 | { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops }; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5854 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5855 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5856 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5857 | |
| 5858 | /* The mux operations */ |
| 5859 | static const struct mux_ops h2_htx_ops = { |
| 5860 | .init = h2_init, |
| 5861 | .wake = h2_wake, |
| 5862 | .snd_buf = h2_snd_buf, |
| 5863 | .rcv_buf = h2_rcv_buf, |
| 5864 | .subscribe = h2_subscribe, |
| 5865 | .unsubscribe = h2_unsubscribe, |
| 5866 | .attach = h2_attach, |
| 5867 | .get_first_cs = h2_get_first_cs, |
| 5868 | .detach = h2_detach, |
| 5869 | .destroy = h2_destroy, |
| 5870 | .avail_streams = h2_avail_streams, |
| 5871 | .used_streams = h2_used_streams, |
| 5872 | .shutr = h2_shutr, |
| 5873 | .shutw = h2_shutw, |
| 5874 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5875 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5876 | .name = "H2", |
| 5877 | }; |
| 5878 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5879 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5880 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops }; |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5881 | |
| 5882 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5883 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5884 | /* config keyword parsers */ |
| 5885 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5886 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5887 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5888 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5889 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5890 | { 0, NULL, NULL } |
| 5891 | }}; |
| 5892 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5893 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |