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 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 665 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 666 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 667 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 668 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 669 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 670 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 671 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 672 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 673 | h2c->task->context = NULL; |
| 674 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 675 | h2c->task = NULL; |
| 676 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 677 | if (h2c->wait_event.task) |
| 678 | tasklet_free(h2c->wait_event.task); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 679 | if (h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 680 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 681 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 682 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 683 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 686 | if (conn) { |
| 687 | conn->mux = NULL; |
| 688 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 689 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 690 | conn_stop_tracking(conn); |
| 691 | conn_full_close(conn); |
| 692 | if (conn->destroy_cb) |
| 693 | conn->destroy_cb(conn); |
| 694 | conn_free(conn); |
| 695 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 699 | /******************************************************/ |
| 700 | /* functions below are for the H2 protocol processing */ |
| 701 | /******************************************************/ |
| 702 | |
| 703 | /* 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] | 704 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 705 | { |
| 706 | return h2s ? h2s->id : 0; |
| 707 | } |
| 708 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 709 | /* 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] | 710 | 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] | 711 | { |
| 712 | if (h2c->msi < 0) |
| 713 | return 0; |
| 714 | |
| 715 | if (h2c->msi == h2s_id(h2s)) |
| 716 | return 0; |
| 717 | |
| 718 | return 1; |
| 719 | } |
| 720 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 721 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 722 | 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] | 723 | { |
| 724 | h2c->errcode = err; |
| 725 | h2c->st0 = H2_CS_ERROR; |
| 726 | } |
| 727 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 728 | /* marks an error on the stream. It may also update an already closed stream |
| 729 | * (e.g. to report an error after an RST was received). |
| 730 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 731 | 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] | 732 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 733 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 734 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 735 | if (h2s->st < H2_SS_ERROR) |
| 736 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 737 | if (h2s->cs) |
| 738 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 742 | /* attempt to notify the data layer of recv availability */ |
| 743 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 744 | { |
| 745 | struct wait_event *sw; |
| 746 | |
| 747 | if (h2s->recv_wait) { |
| 748 | sw = h2s->recv_wait; |
| 749 | sw->events &= ~SUB_RETRY_RECV; |
| 750 | tasklet_wakeup(sw->task); |
| 751 | h2s->recv_wait = NULL; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* attempt to notify the data layer of send availability */ |
| 756 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 757 | { |
| 758 | struct wait_event *sw; |
| 759 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 760 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 761 | sw = h2s->send_wait; |
| 762 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 763 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 764 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 768 | /* alerts the data layer, trying to wake it up by all means, following |
| 769 | * this sequence : |
| 770 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 771 | * - if its subscribed to send, then it's woken up for send |
| 772 | * - if it was subscribed to neither, its ->wake() callback is called |
| 773 | * It is safe to call this function with a closed stream which doesn't have a |
| 774 | * conn_stream anymore. |
| 775 | */ |
| 776 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 777 | { |
| 778 | if (h2s->recv_wait || h2s->send_wait) { |
| 779 | h2s_notify_recv(h2s); |
| 780 | h2s_notify_send(h2s); |
| 781 | } |
| 782 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 783 | h2s->cs->data_cb->wake(h2s->cs); |
| 784 | } |
| 785 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 786 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 787 | 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] | 788 | { |
| 789 | uint8_t *out = frame; |
| 790 | |
| 791 | *out = len >> 16; |
| 792 | write_n16(out + 1, len); |
| 793 | } |
| 794 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 795 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 796 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 797 | * 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] | 798 | * available in the buffer's input prior to calling this function. The buffer |
| 799 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 800 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 801 | 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] | 802 | const struct buffer *b, int o) |
| 803 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 804 | 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] | 805 | } |
| 806 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 807 | 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] | 808 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 809 | 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] | 810 | } |
| 811 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 812 | 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] | 813 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 814 | 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] | 815 | } |
| 816 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 817 | 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] | 818 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 819 | 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] | 820 | } |
| 821 | |
| 822 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 823 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 824 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 825 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 826 | * 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] | 827 | * |
| 828 | * b0 b1 b2 b3 b4 b5..b8 |
| 829 | * +----------+---------+--------+----+----+----------------------+ |
| 830 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 831 | * +----------+---------+--------+----+----+----------------------+ |
| 832 | * |
| 833 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 834 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 835 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 836 | * 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] | 837 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 838 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 839 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 840 | 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] | 841 | { |
| 842 | uint64_t w; |
| 843 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 844 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 845 | return 0; |
| 846 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 847 | w = h2_get_n64(b, o + 1); |
| 848 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 849 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 850 | h->ff = w >> 32; |
| 851 | h->ft = w >> 40; |
| 852 | h->len += w >> 48; |
| 853 | return 1; |
| 854 | } |
| 855 | |
| 856 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 857 | * h2_peek_frame_hdr() above. |
| 858 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 859 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 860 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 861 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 865 | 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] | 866 | { |
| 867 | int ret; |
| 868 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 869 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 870 | if (ret > 0) |
| 871 | h2_skip_frame_hdr(b); |
| 872 | return ret; |
| 873 | } |
| 874 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 875 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 876 | * its connection if the stream was not yet closed. Please use this exclusively |
| 877 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 878 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 879 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 880 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 881 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 882 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 883 | if (!h2s->id) |
| 884 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 885 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 886 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 887 | h2s_notify_recv(h2s); |
| 888 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 889 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 890 | h2s->st = H2_SS_CLOSED; |
| 891 | } |
| 892 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 893 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 894 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 895 | { |
| 896 | h2s_close(h2s); |
| 897 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 898 | if (b_size(&h2s->rxbuf)) { |
| 899 | b_free(&h2s->rxbuf); |
| 900 | offer_buffers(NULL, tasks_run_queue); |
| 901 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 902 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 903 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 904 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 905 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 906 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 907 | * reference left would be in the h2c send_list/fctl_list, and if |
| 908 | * we're in it, we're getting out anyway |
| 909 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 910 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 911 | if (LIST_ADDED(&h2s->sending_list)) { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 912 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 913 | LIST_DEL_INIT(&h2s->sending_list); |
| 914 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 915 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 916 | pool_free(pool_head_h2s, h2s); |
| 917 | } |
| 918 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 919 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 920 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 921 | * causes of errors can be any failed memory allocation. The caller is |
| 922 | * responsible for checking if the connection may support an extra stream |
| 923 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 924 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 925 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 926 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 927 | struct h2s *h2s; |
| 928 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 929 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 930 | if (!h2s) |
| 931 | goto out; |
| 932 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 933 | h2s->wait_event.task = tasklet_new(); |
| 934 | if (!h2s->wait_event.task) { |
| 935 | pool_free(pool_head_h2s, h2s); |
| 936 | goto out; |
| 937 | } |
| 938 | h2s->send_wait = NULL; |
| 939 | h2s->recv_wait = NULL; |
| 940 | h2s->wait_event.task->process = h2_deferred_shut; |
| 941 | h2s->wait_event.task->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 942 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 943 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 944 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 945 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 946 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 947 | h2s->mws = h2c->miw; |
| 948 | h2s->flags = H2_SF_NONE; |
| 949 | h2s->errcode = H2_ERR_NO_ERROR; |
| 950 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 951 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 952 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 953 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 954 | |
| 955 | if (h2c->flags & H2_CF_IS_BACK) { |
| 956 | h1m_init_req(&h2s->h1m); |
| 957 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 958 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 959 | } else { |
| 960 | h1m_init_res(&h2s->h1m); |
| 961 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 962 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 963 | } |
| 964 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 965 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 966 | if (id > 0) |
| 967 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 968 | else |
| 969 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 970 | |
| 971 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 972 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 973 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 974 | |
| 975 | return h2s; |
| 976 | |
| 977 | out_free_h2s: |
| 978 | pool_free(pool_head_h2s, h2s); |
| 979 | out: |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 984 | * case of memory allocation error. |
| 985 | */ |
| 986 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 987 | { |
| 988 | struct session *sess = h2c->conn->owner; |
| 989 | struct conn_stream *cs; |
| 990 | struct h2s *h2s; |
| 991 | |
| 992 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 993 | goto out; |
| 994 | |
| 995 | h2s = h2s_new(h2c, id); |
| 996 | if (!h2s) |
| 997 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 998 | |
| 999 | cs = cs_new(h2c->conn); |
| 1000 | if (!cs) |
| 1001 | goto out_close; |
| 1002 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1003 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1004 | h2s->cs = cs; |
| 1005 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1006 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1007 | |
| 1008 | if (stream_create_from_cs(cs) < 0) |
| 1009 | goto out_free_cs; |
| 1010 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1011 | /* We want the accept date presented to the next stream to be the one |
| 1012 | * we have now, the handshake time to be null (since the next stream |
| 1013 | * is not delayed by a handshake), and the idle time to count since |
| 1014 | * right now. |
| 1015 | */ |
| 1016 | sess->accept_date = date; |
| 1017 | sess->tv_accept = now; |
| 1018 | sess->t_handshake = 0; |
| 1019 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1020 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1021 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1022 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1023 | return h2s; |
| 1024 | |
| 1025 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1026 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1027 | cs_free(cs); |
| 1028 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1029 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1030 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1031 | sess_log(sess); |
| 1032 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1035 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1036 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1037 | * possible stream ID was reached. |
| 1038 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1039 | 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] | 1040 | { |
| 1041 | struct h2s *h2s = NULL; |
| 1042 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1043 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1044 | goto out; |
| 1045 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1046 | if (h2_streams_left(h2c) < 1) |
| 1047 | goto out; |
| 1048 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1049 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1050 | h2s = h2s_new(h2c, 0); |
| 1051 | if (!h2s) |
| 1052 | goto out; |
| 1053 | |
| 1054 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1055 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1056 | cs->ctx = h2s; |
| 1057 | h2c->nb_cs++; |
| 1058 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1059 | out: |
| 1060 | return h2s; |
| 1061 | } |
| 1062 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1063 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1064 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1065 | * the various settings codes. |
| 1066 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1067 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1068 | { |
| 1069 | struct buffer *res; |
| 1070 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1071 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1072 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1073 | int ret; |
| 1074 | |
| 1075 | if (h2c_mux_busy(h2c, NULL)) { |
| 1076 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1080 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1081 | chunk_memcpy(&buf, |
| 1082 | "\x00\x00\x00" /* length : 0 for now */ |
| 1083 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1084 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1085 | 9); |
| 1086 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1087 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1088 | /* send settings_enable_push=0 */ |
| 1089 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1090 | } |
| 1091 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1092 | if (h2_settings_header_table_size != 4096) { |
| 1093 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1094 | |
| 1095 | write_n32(str + 2, h2_settings_header_table_size); |
| 1096 | chunk_memcat(&buf, str, 6); |
| 1097 | } |
| 1098 | |
| 1099 | if (h2_settings_initial_window_size != 65535) { |
| 1100 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1101 | |
| 1102 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1103 | chunk_memcat(&buf, str, 6); |
| 1104 | } |
| 1105 | |
| 1106 | if (h2_settings_max_concurrent_streams != 0) { |
| 1107 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1108 | |
| 1109 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1110 | * the protocol, so never send this value! |
| 1111 | */ |
| 1112 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1113 | chunk_memcat(&buf, str, 6); |
| 1114 | } |
| 1115 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1116 | mfs = h2_settings_max_frame_size; |
| 1117 | if (mfs > global.tune.bufsize) |
| 1118 | mfs = global.tune.bufsize; |
| 1119 | |
| 1120 | if (!mfs) |
| 1121 | mfs = global.tune.bufsize; |
| 1122 | |
| 1123 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1124 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1125 | |
| 1126 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1127 | * match bufsize - rewrite size, but at the moment it seems |
| 1128 | * that clients don't take care of it. |
| 1129 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1130 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1131 | chunk_memcat(&buf, str, 6); |
| 1132 | } |
| 1133 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1134 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1135 | |
| 1136 | res = br_tail(h2c->mbuf); |
| 1137 | retry: |
| 1138 | if (!h2_get_buf(h2c, res)) { |
| 1139 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1140 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1144 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1145 | if (unlikely(ret <= 0)) { |
| 1146 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1147 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1148 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1149 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1150 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | else { |
| 1154 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | } |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1161 | /* Try to receive a connection preface, then upon success try to send our |
| 1162 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1163 | * missing data. It may return an error in h2c. |
| 1164 | */ |
| 1165 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1166 | { |
| 1167 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1168 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1169 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1170 | 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] | 1171 | |
| 1172 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1173 | if (ret1 < 0) |
| 1174 | sess_log(h2c->conn->owner); |
| 1175 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1176 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1177 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1181 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1182 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1183 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1184 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1185 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1188 | /* Try to send a connection preface, then upon success try to send our |
| 1189 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1190 | * missing data. It may return an error in h2c. |
| 1191 | */ |
| 1192 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1193 | { |
| 1194 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1195 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1196 | |
| 1197 | if (h2c_mux_busy(h2c, NULL)) { |
| 1198 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1202 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1203 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1204 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1205 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1206 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | if (!b_data(res)) { |
| 1211 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1212 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1213 | if (unlikely(ret <= 0)) { |
| 1214 | if (!ret) { |
| 1215 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1216 | goto retry; |
| 1217 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1218 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1219 | return 0; |
| 1220 | } |
| 1221 | else { |
| 1222 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1226 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1227 | return h2c_send_settings(h2c); |
| 1228 | } |
| 1229 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1230 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1231 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1232 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1233 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1234 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1235 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1236 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1237 | * case so that it is safe to use h2c_error() to report such errors. |
| 1238 | */ |
| 1239 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1240 | { |
| 1241 | struct buffer *res; |
| 1242 | char str[17]; |
| 1243 | int ret; |
| 1244 | |
| 1245 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1246 | return 1; // claim that it worked |
| 1247 | |
| 1248 | if (h2c_mux_busy(h2c, h2s)) { |
| 1249 | if (h2s) |
| 1250 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1251 | else |
| 1252 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1256 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1257 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1258 | |
| 1259 | if (h2c->last_sid < 0) |
| 1260 | h2c->last_sid = h2c->max_id; |
| 1261 | |
| 1262 | write_n32(str + 9, h2c->last_sid); |
| 1263 | write_n32(str + 13, h2c->errcode); |
| 1264 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1265 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1266 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1267 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1268 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1269 | if (h2s) |
| 1270 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1271 | else |
| 1272 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1276 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1277 | if (unlikely(ret <= 0)) { |
| 1278 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1279 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1280 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1281 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1282 | if (h2s) |
| 1283 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1284 | else |
| 1285 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1286 | return 0; |
| 1287 | } |
| 1288 | else { |
| 1289 | /* we cannot report this error using GOAWAY, so we mark |
| 1290 | * it and claim a success. |
| 1291 | */ |
| 1292 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1293 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1294 | return 1; |
| 1295 | } |
| 1296 | } |
| 1297 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1298 | return ret; |
| 1299 | } |
| 1300 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1301 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1302 | * during mux operations. This stream must be valid and cannot be closed |
| 1303 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1304 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1305 | * not yet. |
| 1306 | * |
| 1307 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1308 | * to write the message, it subscribes the stream to future notifications. |
| 1309 | */ |
| 1310 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1311 | { |
| 1312 | struct buffer *res; |
| 1313 | char str[13]; |
| 1314 | int ret; |
| 1315 | |
| 1316 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1317 | return 1; |
| 1318 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1319 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1320 | * RST_STREAM in response to a RST_STREAM frame. |
| 1321 | */ |
| 1322 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1323 | ret = 1; |
| 1324 | goto ignore; |
| 1325 | } |
| 1326 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1327 | if (h2c_mux_busy(h2c, h2s)) { |
| 1328 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1332 | /* len: 4, type: 3, flags: none */ |
| 1333 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1334 | write_n32(str + 5, h2s->id); |
| 1335 | write_n32(str + 9, h2s->errcode); |
| 1336 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1337 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1338 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1339 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1340 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1341 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1345 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1346 | if (unlikely(ret <= 0)) { |
| 1347 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1348 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1349 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1350 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1351 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | else { |
| 1355 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1356 | return 0; |
| 1357 | } |
| 1358 | } |
| 1359 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1360 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1361 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1362 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1367 | * 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] | 1368 | * error code, even if the stream is one of the dummy ones, and will update |
| 1369 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1370 | * |
| 1371 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1372 | * 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] | 1373 | * 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] | 1374 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1375 | */ |
| 1376 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1377 | { |
| 1378 | struct buffer *res; |
| 1379 | char str[13]; |
| 1380 | int ret; |
| 1381 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1382 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1383 | * RST_STREAM in response to a RST_STREAM frame. |
| 1384 | */ |
| 1385 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1386 | ret = 1; |
| 1387 | goto ignore; |
| 1388 | } |
| 1389 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1390 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1391 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1392 | return 0; |
| 1393 | } |
| 1394 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1395 | /* len: 4, type: 3, flags: none */ |
| 1396 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1397 | |
| 1398 | write_n32(str + 5, h2c->dsi); |
| 1399 | write_n32(str + 9, h2s->errcode); |
| 1400 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1401 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1402 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1403 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1404 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1405 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1406 | return 0; |
| 1407 | } |
| 1408 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1409 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1410 | if (unlikely(ret <= 0)) { |
| 1411 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1412 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1413 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1414 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1415 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1416 | return 0; |
| 1417 | } |
| 1418 | else { |
| 1419 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | } |
| 1423 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1424 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1425 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1426 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1427 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1430 | return ret; |
| 1431 | } |
| 1432 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1433 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1434 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1435 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1436 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1437 | * message, it subscribes the requesting stream to future notifications. |
| 1438 | */ |
| 1439 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1440 | { |
| 1441 | struct h2c *h2c = h2s->h2c; |
| 1442 | struct buffer *res; |
| 1443 | char str[9]; |
| 1444 | int ret; |
| 1445 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1446 | 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] | 1447 | return 1; |
| 1448 | |
| 1449 | if (h2c_mux_busy(h2c, h2s)) { |
| 1450 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1454 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1455 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1456 | write_n32(str + 5, h2s->id); |
| 1457 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1458 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1459 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1460 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1461 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1462 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1466 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1467 | if (likely(ret > 0)) { |
| 1468 | h2s->flags |= H2_SF_ES_SENT; |
| 1469 | } |
| 1470 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1471 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1472 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1473 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1474 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1475 | return 0; |
| 1476 | } |
| 1477 | else { |
| 1478 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1479 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1480 | } |
| 1481 | return ret; |
| 1482 | } |
| 1483 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1484 | /* 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] | 1485 | * 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] | 1486 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1487 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1488 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1489 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1490 | { |
| 1491 | if (!h2s->cs) { |
| 1492 | /* this stream was already orphaned */ |
| 1493 | h2s_destroy(h2s); |
| 1494 | return; |
| 1495 | } |
| 1496 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1497 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1498 | if (h2s->st == H2_SS_OPEN) |
| 1499 | h2s->st = H2_SS_HREM; |
| 1500 | else if (h2s->st == H2_SS_HLOC) |
| 1501 | h2s_close(h2s); |
| 1502 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1503 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1504 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1505 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1506 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1507 | if (h2s->cs->flags & CS_FL_EOS) |
| 1508 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1509 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1510 | if (h2s->st < H2_SS_ERROR) |
| 1511 | h2s->st = H2_SS_ERROR; |
| 1512 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1513 | |
| 1514 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1518 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1519 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1520 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1521 | { |
| 1522 | struct eb32_node *node; |
| 1523 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1524 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1525 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1526 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1527 | while (node) { |
| 1528 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1529 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1530 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1531 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1532 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1533 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1534 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1535 | while (node) { |
| 1536 | h2s = container_of(node, struct h2s, by_id); |
| 1537 | if (h2s->id > 0) |
| 1538 | break; |
| 1539 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1540 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1544 | /* Increase all streams' outgoing window size by the difference passed in |
| 1545 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1546 | * window size is different. The difference may be negative and the resulting |
| 1547 | * window size as well, for the time it takes to receive some window updates. |
| 1548 | */ |
| 1549 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1550 | { |
| 1551 | struct h2s *h2s; |
| 1552 | struct eb32_node *node; |
| 1553 | |
| 1554 | if (!diff) |
| 1555 | return; |
| 1556 | |
| 1557 | node = eb32_first(&h2c->streams_by_id); |
| 1558 | while (node) { |
| 1559 | h2s = container_of(node, struct h2s, by_id); |
| 1560 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1561 | |
| 1562 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1563 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1564 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1565 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1568 | node = eb32_next(node); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1573 | * 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] | 1574 | * return an error in h2c. The caller must have already verified frame length |
| 1575 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1576 | */ |
| 1577 | static int h2c_handle_settings(struct h2c *h2c) |
| 1578 | { |
| 1579 | unsigned int offset; |
| 1580 | int error; |
| 1581 | |
| 1582 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1583 | if (h2c->dfl) { |
| 1584 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1585 | goto fail; |
| 1586 | } |
| 1587 | return 1; |
| 1588 | } |
| 1589 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1590 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1591 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1592 | return 0; |
| 1593 | |
| 1594 | /* parse the frame */ |
| 1595 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1596 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1597 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1598 | |
| 1599 | switch (type) { |
| 1600 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1601 | /* we need to update all existing streams with the |
| 1602 | * difference from the previous iws. |
| 1603 | */ |
| 1604 | if (arg < 0) { // RFC7540#6.5.2 |
| 1605 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1606 | goto fail; |
| 1607 | } |
| 1608 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1609 | h2c->miw = arg; |
| 1610 | break; |
| 1611 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1612 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1613 | error = H2_ERR_PROTOCOL_ERROR; |
| 1614 | goto fail; |
| 1615 | } |
| 1616 | h2c->mfs = arg; |
| 1617 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1618 | case H2_SETTINGS_ENABLE_PUSH: |
| 1619 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1620 | error = H2_ERR_PROTOCOL_ERROR; |
| 1621 | goto fail; |
| 1622 | } |
| 1623 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1624 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1625 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1626 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1627 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1628 | arg = h2_settings_max_concurrent_streams; |
| 1629 | h2c->streams_limit = arg; |
| 1630 | } |
| 1631 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /* need to ACK this frame now */ |
| 1636 | h2c->st0 = H2_CS_FRAME_A; |
| 1637 | return 1; |
| 1638 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1639 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1640 | h2c_error(h2c, error); |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1645 | * success or one of the h2_status values. |
| 1646 | */ |
| 1647 | static int h2c_ack_settings(struct h2c *h2c) |
| 1648 | { |
| 1649 | struct buffer *res; |
| 1650 | char str[9]; |
| 1651 | int ret = -1; |
| 1652 | |
| 1653 | if (h2c_mux_busy(h2c, NULL)) { |
| 1654 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1658 | memcpy(str, |
| 1659 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1660 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1661 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1662 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1663 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1664 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1665 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1666 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1667 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1671 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1672 | if (unlikely(ret <= 0)) { |
| 1673 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1674 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1675 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1676 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1677 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1678 | return 0; |
| 1679 | } |
| 1680 | else { |
| 1681 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1682 | return 0; |
| 1683 | } |
| 1684 | } |
| 1685 | return ret; |
| 1686 | } |
| 1687 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1688 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1689 | * 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] | 1690 | * missing data. The caller must have already verified frame length |
| 1691 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1692 | */ |
| 1693 | static int h2c_handle_ping(struct h2c *h2c) |
| 1694 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1695 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1696 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1697 | h2c->st0 = H2_CS_FRAME_A; |
| 1698 | return 1; |
| 1699 | } |
| 1700 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1701 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1702 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1703 | * error in h2c. |
| 1704 | */ |
| 1705 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1706 | { |
| 1707 | struct buffer *res; |
| 1708 | char str[13]; |
| 1709 | int ret = -1; |
| 1710 | |
| 1711 | if (h2c_mux_busy(h2c, NULL)) { |
| 1712 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1716 | /* length: 4, type: 8, flags: none */ |
| 1717 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1718 | write_n32(str + 5, sid); |
| 1719 | write_n32(str + 9, increment); |
| 1720 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1721 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1722 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1723 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1724 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1725 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1729 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1730 | if (unlikely(ret <= 0)) { |
| 1731 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1732 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1733 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1734 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1735 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1736 | return 0; |
| 1737 | } |
| 1738 | else { |
| 1739 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1740 | return 0; |
| 1741 | } |
| 1742 | } |
| 1743 | return ret; |
| 1744 | } |
| 1745 | |
| 1746 | /* try to send pending window update for the connection. It's safe to call it |
| 1747 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1748 | * failure. It may return an error in h2c. |
| 1749 | */ |
| 1750 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1751 | { |
| 1752 | int ret = 1; |
| 1753 | |
| 1754 | if (h2c->rcvd_c <= 0) |
| 1755 | return 1; |
| 1756 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1757 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1758 | /* increase the advertised connection window to 2G on |
| 1759 | * first update. |
| 1760 | */ |
| 1761 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1762 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1763 | } |
| 1764 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1765 | /* send WU for the connection */ |
| 1766 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1767 | if (ret > 0) |
| 1768 | h2c->rcvd_c = 0; |
| 1769 | |
| 1770 | return ret; |
| 1771 | } |
| 1772 | |
| 1773 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1774 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1775 | * room or failure. It may return an error in h2c. |
| 1776 | */ |
| 1777 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1778 | { |
| 1779 | int ret = 1; |
| 1780 | |
| 1781 | if (h2c->rcvd_s <= 0) |
| 1782 | return 1; |
| 1783 | |
| 1784 | /* send WU for the stream */ |
| 1785 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1786 | if (ret > 0) |
| 1787 | h2c->rcvd_s = 0; |
| 1788 | |
| 1789 | return ret; |
| 1790 | } |
| 1791 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1792 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1793 | * success, 0 on missing data or one of the h2_status values. |
| 1794 | */ |
| 1795 | static int h2c_ack_ping(struct h2c *h2c) |
| 1796 | { |
| 1797 | struct buffer *res; |
| 1798 | char str[17]; |
| 1799 | int ret = -1; |
| 1800 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1801 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1802 | return 0; |
| 1803 | |
| 1804 | if (h2c_mux_busy(h2c, NULL)) { |
| 1805 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1809 | memcpy(str, |
| 1810 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1811 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1812 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1813 | |
| 1814 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1815 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1816 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1817 | res = br_tail(h2c->mbuf); |
| 1818 | retry: |
| 1819 | if (!h2_get_buf(h2c, res)) { |
| 1820 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1821 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1825 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1826 | if (unlikely(ret <= 0)) { |
| 1827 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1828 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1829 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1830 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1831 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1832 | return 0; |
| 1833 | } |
| 1834 | else { |
| 1835 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1836 | return 0; |
| 1837 | } |
| 1838 | } |
| 1839 | return ret; |
| 1840 | } |
| 1841 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1842 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1843 | * 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] | 1844 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1845 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1846 | */ |
| 1847 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1848 | { |
| 1849 | int32_t inc; |
| 1850 | int error; |
| 1851 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1852 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1853 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1854 | return 0; |
| 1855 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1856 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1857 | |
| 1858 | if (h2c->dsi != 0) { |
| 1859 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1860 | |
| 1861 | /* it's not an error to receive WU on a closed stream */ |
| 1862 | if (h2s->st == H2_SS_CLOSED) |
| 1863 | return 1; |
| 1864 | |
| 1865 | if (!inc) { |
| 1866 | error = H2_ERR_PROTOCOL_ERROR; |
| 1867 | goto strm_err; |
| 1868 | } |
| 1869 | |
| 1870 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1871 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1872 | goto strm_err; |
| 1873 | } |
| 1874 | |
| 1875 | h2s->mws += inc; |
| 1876 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1877 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1878 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1879 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | else { |
| 1883 | /* connection window update */ |
| 1884 | if (!inc) { |
| 1885 | error = H2_ERR_PROTOCOL_ERROR; |
| 1886 | goto conn_err; |
| 1887 | } |
| 1888 | |
| 1889 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1890 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1891 | goto conn_err; |
| 1892 | } |
| 1893 | |
| 1894 | h2c->mws += inc; |
| 1895 | } |
| 1896 | |
| 1897 | return 1; |
| 1898 | |
| 1899 | conn_err: |
| 1900 | h2c_error(h2c, error); |
| 1901 | return 0; |
| 1902 | |
| 1903 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1904 | h2s_error(h2s, error); |
| 1905 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1906 | return 0; |
| 1907 | } |
| 1908 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1909 | /* 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] | 1910 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1911 | * have already verified frame length and stream ID validity. Described in |
| 1912 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1913 | */ |
| 1914 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1915 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1916 | int last; |
| 1917 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1918 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1919 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1920 | return 0; |
| 1921 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1922 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1923 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1924 | if (h2c->last_sid < 0) |
| 1925 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1926 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1927 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1930 | /* 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] | 1931 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1932 | * error in h2c. The caller must have already verified frame length and stream |
| 1933 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1934 | */ |
| 1935 | static int h2c_handle_priority(struct h2c *h2c) |
| 1936 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1937 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1938 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1939 | return 0; |
| 1940 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1941 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1942 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1943 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1944 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1945 | } |
| 1946 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1947 | } |
| 1948 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1949 | /* 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] | 1950 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1951 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1952 | */ |
| 1953 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1954 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1955 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1956 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1957 | return 0; |
| 1958 | |
| 1959 | /* late RST, already handled */ |
| 1960 | if (h2s->st == H2_SS_CLOSED) |
| 1961 | return 1; |
| 1962 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1963 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1964 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1965 | |
| 1966 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1967 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1968 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | h2s->flags |= H2_SF_RST_RCVD; |
| 1972 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1973 | } |
| 1974 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1975 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1976 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1977 | * return value is the new h2s in case one was allocated (most common case). |
| 1978 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1979 | * errors here are reported as connection errors since it's impossible to |
| 1980 | * recover from such errors after the compression context has been altered. |
| 1981 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1982 | 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] | 1983 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1984 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1985 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1986 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1987 | int error; |
| 1988 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1989 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1990 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1991 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1992 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1993 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1994 | |
| 1995 | /* now either the frame is complete or the buffer is complete */ |
| 1996 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1997 | /* The stream exists/existed, this must be a trailers frame */ |
| 1998 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 1999 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2000 | /* unrecoverable error ? */ |
| 2001 | if (h2c->st0 >= H2_CS_ERROR) |
| 2002 | goto out; |
| 2003 | |
| 2004 | if (error == 0) |
| 2005 | goto out; // missing data |
| 2006 | |
| 2007 | if (error < 0) { |
| 2008 | /* Failed to decode this frame (e.g. too large request) |
| 2009 | * but the HPACK decompressor is still synchronized. |
| 2010 | */ |
| 2011 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2012 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2013 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2014 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2015 | goto done; |
| 2016 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2017 | /* the connection was already killed by an RST, let's consume |
| 2018 | * the data and send another RST. |
| 2019 | */ |
| 2020 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2021 | h2s = (struct h2s*)h2_error_stream; |
| 2022 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2023 | } |
| 2024 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2025 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2026 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2027 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2028 | goto conn_err; |
| 2029 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2030 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2031 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2032 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2033 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2034 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2035 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2036 | if (h2c->st0 >= H2_CS_ERROR) |
| 2037 | goto out; |
| 2038 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2039 | if (error <= 0) { |
| 2040 | if (error == 0) |
| 2041 | goto out; // missing data |
| 2042 | |
| 2043 | /* Failed to decode this stream (e.g. too large request) |
| 2044 | * but the HPACK decompressor is still synchronized. |
| 2045 | */ |
| 2046 | h2s = (struct h2s*)h2_error_stream; |
| 2047 | goto send_rst; |
| 2048 | } |
| 2049 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2050 | /* 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] | 2051 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2052 | * 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] | 2053 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2054 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2055 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2056 | h2s = (struct h2s*)h2_refused_stream; |
| 2057 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2061 | h2s->rxbuf = rxbuf; |
| 2062 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2063 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2064 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2065 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2066 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2067 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2068 | |
| 2069 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2070 | if (h2s->st == H2_SS_OPEN) |
| 2071 | h2s->st = H2_SS_HREM; |
| 2072 | else |
| 2073 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2076 | /* update the max stream ID if the request is being processed */ |
| 2077 | if (h2s->id > h2c->max_id) |
| 2078 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2079 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2080 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2081 | |
| 2082 | conn_err: |
| 2083 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2084 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2085 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2086 | out: |
| 2087 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2088 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2089 | |
| 2090 | send_rst: |
| 2091 | /* make the demux send an RST for the current stream. We may only |
| 2092 | * do this if we're certain that the HEADERS frame was properly |
| 2093 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2094 | */ |
| 2095 | h2_release_buf(h2c, &rxbuf); |
| 2096 | h2c->st0 = H2_CS_FRAME_E; |
| 2097 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2100 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2101 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2102 | * errors here are reported as connection errors since it's impossible to |
| 2103 | * recover from such errors after the compression context has been altered. |
| 2104 | */ |
| 2105 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2106 | { |
| 2107 | int error; |
| 2108 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2109 | if (!b_size(&h2c->dbuf)) |
| 2110 | return NULL; // empty buffer |
| 2111 | |
| 2112 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2113 | return NULL; // incomplete frame |
| 2114 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2115 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2116 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2117 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2118 | if (h2c->st0 >= H2_CS_ERROR) |
| 2119 | return NULL; |
| 2120 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2121 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2122 | /* RFC7540#5.1 */ |
| 2123 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2124 | h2c->st0 = H2_CS_FRAME_E; |
| 2125 | return NULL; |
| 2126 | } |
| 2127 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2128 | if (error <= 0) { |
| 2129 | if (error == 0) |
| 2130 | return NULL; // missing data |
| 2131 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2132 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2133 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2134 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2135 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2138 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2139 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2140 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2141 | 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] | 2142 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2143 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2144 | if (h2s->st == H2_SS_OPEN) |
| 2145 | h2s->st = H2_SS_HREM; |
| 2146 | else if (h2s->st == H2_SS_HLOC) |
| 2147 | h2s_close(h2s); |
| 2148 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2149 | |
| 2150 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2151 | } |
| 2152 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2153 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2154 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2155 | */ |
| 2156 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2157 | { |
| 2158 | int error; |
| 2159 | |
| 2160 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2161 | * to signal an end of stream (with the ES flag). |
| 2162 | */ |
| 2163 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2164 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2165 | return 0; // empty buffer |
| 2166 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2167 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2168 | return 0; // incomplete frame |
| 2169 | |
| 2170 | /* now either the frame is complete or the buffer is complete */ |
| 2171 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2172 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2173 | /* RFC7540#6.1 */ |
| 2174 | error = H2_ERR_STREAM_CLOSED; |
| 2175 | goto strm_err; |
| 2176 | } |
| 2177 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2178 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2179 | /* RFC7540#8.1.2 */ |
| 2180 | error = H2_ERR_PROTOCOL_ERROR; |
| 2181 | goto strm_err; |
| 2182 | } |
| 2183 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2184 | if (!h2_frt_transfer_data(h2s)) |
| 2185 | return 0; |
| 2186 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2187 | /* call the upper layers to process the frame, then let the upper layer |
| 2188 | * notify the stream about any change. |
| 2189 | */ |
| 2190 | if (!h2s->cs) { |
| 2191 | error = H2_ERR_STREAM_CLOSED; |
| 2192 | goto strm_err; |
| 2193 | } |
| 2194 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2195 | if (h2c->st0 >= H2_CS_ERROR) |
| 2196 | return 0; |
| 2197 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2198 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2199 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2200 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* check for completion : the callee will change this to FRAME_A or |
| 2204 | * FRAME_H once done. |
| 2205 | */ |
| 2206 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2207 | return 0; |
| 2208 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2209 | /* last frame */ |
| 2210 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2211 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2212 | if (h2s->st == H2_SS_OPEN) |
| 2213 | h2s->st = H2_SS_HREM; |
| 2214 | else |
| 2215 | h2s_close(h2s); |
| 2216 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2217 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2218 | /* RFC7540#8.1.2 */ |
| 2219 | error = H2_ERR_PROTOCOL_ERROR; |
| 2220 | goto strm_err; |
| 2221 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2224 | return 1; |
| 2225 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2226 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2227 | h2s_error(h2s, error); |
| 2228 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2229 | return 0; |
| 2230 | } |
| 2231 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2232 | /* process Rx frames to be demultiplexed */ |
| 2233 | static void h2_process_demux(struct h2c *h2c) |
| 2234 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2235 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2236 | struct h2_fh hdr; |
| 2237 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2238 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2239 | if (h2c->st0 >= H2_CS_ERROR) |
| 2240 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2241 | |
| 2242 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2243 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2244 | if (h2c->flags & H2_CF_IS_BACK) |
| 2245 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2246 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2247 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2248 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2249 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2250 | sess_log(h2c->conn->owner); |
| 2251 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2252 | goto fail; |
| 2253 | } |
| 2254 | |
| 2255 | h2c->max_id = 0; |
| 2256 | h2c->st0 = H2_CS_SETTINGS1; |
| 2257 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2258 | |
| 2259 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2260 | /* ensure that what is pending is a valid SETTINGS frame |
| 2261 | * without an ACK. |
| 2262 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2263 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2264 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2265 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2266 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2267 | sess_log(h2c->conn->owner); |
| 2268 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2269 | goto fail; |
| 2270 | } |
| 2271 | |
| 2272 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2273 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2274 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2275 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2276 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2277 | goto fail; |
| 2278 | } |
| 2279 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2280 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2281 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2282 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2283 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2284 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2285 | goto fail; |
| 2286 | } |
| 2287 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2288 | /* that's OK, switch to FRAME_P to process it. This is |
| 2289 | * a SETTINGS frame whose header has already been |
| 2290 | * deleted above. |
| 2291 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2292 | padlen = 0; |
| 2293 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2294 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2295 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2296 | |
| 2297 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2298 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2299 | int ret = 0; |
| 2300 | |
| 2301 | if (h2c->st0 >= H2_CS_ERROR) |
| 2302 | break; |
| 2303 | |
| 2304 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2305 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2306 | break; |
| 2307 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2308 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2309 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2310 | if (!h2c->nb_streams) { |
| 2311 | /* only log if no other stream can report the error */ |
| 2312 | sess_log(h2c->conn->owner); |
| 2313 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2314 | break; |
| 2315 | } |
| 2316 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2317 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2318 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2319 | * we read the pad length and drop it from the remaining |
| 2320 | * payload (one byte + the 9 remaining ones = 10 total |
| 2321 | * removed), so we have a frame payload starting after the |
| 2322 | * pad len. Flow controlled frames (DATA) also count the |
| 2323 | * padlen in the flow control, so it must be adjusted. |
| 2324 | */ |
| 2325 | if (hdr.len < 1) { |
| 2326 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2327 | sess_log(h2c->conn->owner); |
| 2328 | goto fail; |
| 2329 | } |
| 2330 | hdr.len--; |
| 2331 | |
| 2332 | if (b_data(&h2c->dbuf) < 10) |
| 2333 | break; // missing padlen |
| 2334 | |
| 2335 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2336 | |
| 2337 | if (padlen > hdr.len) { |
| 2338 | /* RFC7540#6.1 : pad length = length of |
| 2339 | * frame payload or greater => error. |
| 2340 | */ |
| 2341 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2342 | sess_log(h2c->conn->owner); |
| 2343 | goto fail; |
| 2344 | } |
| 2345 | |
| 2346 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2347 | h2c->rcvd_c++; |
| 2348 | h2c->rcvd_s++; |
| 2349 | } |
| 2350 | b_del(&h2c->dbuf, 1); |
| 2351 | } |
| 2352 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2353 | |
| 2354 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2355 | h2c->dfl = hdr.len; |
| 2356 | h2c->dsi = hdr.sid; |
| 2357 | h2c->dft = hdr.ft; |
| 2358 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2359 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2360 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2361 | |
| 2362 | /* check for minimum basic frame format validity */ |
| 2363 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2364 | if (ret != H2_ERR_NO_ERROR) { |
| 2365 | h2c_error(h2c, ret); |
| 2366 | sess_log(h2c->conn->owner); |
| 2367 | goto fail; |
| 2368 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2372 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2373 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2374 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2375 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 2376 | (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2377 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2378 | (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] | 2379 | /* we may have to signal the upper layers */ |
| 2380 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2381 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2382 | } |
| 2383 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2384 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2385 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2386 | goto strm_err; |
| 2387 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2388 | if (h2s->st == H2_SS_IDLE && |
| 2389 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2390 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2391 | * this state MUST be treated as a connection error |
| 2392 | */ |
| 2393 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2394 | if (!h2c->nb_streams) { |
| 2395 | /* only log if no other stream can report the error */ |
| 2396 | sess_log(h2c->conn->owner); |
| 2397 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2398 | break; |
| 2399 | } |
| 2400 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2401 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2402 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2403 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2404 | * this state MUST be treated as a stream error. |
| 2405 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2406 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2407 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2408 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2409 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2410 | else |
| 2411 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2412 | goto strm_err; |
| 2413 | } |
| 2414 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2415 | /* Below the management of frames received in closed state is a |
| 2416 | * bit hackish because the spec makes strong differences between |
| 2417 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2418 | * in both directions. In addition to this, the creation of a |
| 2419 | * new stream reusing the identifier of a closed one will be |
| 2420 | * detected here. Given that we cannot keep track of all closed |
| 2421 | * streams forever, we consider that unknown closed streams were |
| 2422 | * closed on RST received, which allows us to respond with an |
| 2423 | * RST without breaking the connection (eg: to abort a transfer). |
| 2424 | * Some frames have to be silently ignored as well. |
| 2425 | */ |
| 2426 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2427 | 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] | 2428 | /* #5.1.1: The identifier of a newly |
| 2429 | * established stream MUST be numerically |
| 2430 | * greater than all streams that the initiating |
| 2431 | * endpoint has opened or reserved. This |
| 2432 | * governs streams that are opened using a |
| 2433 | * HEADERS frame and streams that are reserved |
| 2434 | * using PUSH_PROMISE. An endpoint that |
| 2435 | * receives an unexpected stream identifier |
| 2436 | * MUST respond with a connection error. |
| 2437 | */ |
| 2438 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2439 | goto strm_err; |
| 2440 | } |
| 2441 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2442 | 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] | 2443 | /* RFC7540#5.1:closed: an endpoint that |
| 2444 | * receives any frame other than PRIORITY after |
| 2445 | * receiving a RST_STREAM MUST treat that as a |
| 2446 | * stream error of type STREAM_CLOSED. |
| 2447 | * |
| 2448 | * Note that old streams fall into this category |
| 2449 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2450 | * |
| 2451 | * However, we cannot generalize this to all frame types. Those |
| 2452 | * carrying compression state must still be processed before |
| 2453 | * being dropped or we'll desynchronize the decoder. This can |
| 2454 | * happen with request trailers received after sending an |
| 2455 | * RST_STREAM, or with header/trailers responses received after |
| 2456 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2457 | */ |
| 2458 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2459 | h2c->st0 = H2_CS_FRAME_E; |
| 2460 | goto strm_err; |
| 2461 | } |
| 2462 | |
| 2463 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2464 | * result of sending a RST_STREAM frame, the peer that |
| 2465 | * receives the RST_STREAM might have already sent |
| 2466 | * frames on the stream that cannot be withdrawn. An |
| 2467 | * endpoint MUST ignore frames that it receives on |
| 2468 | * closed streams after it has sent a RST_STREAM |
| 2469 | * frame. An endpoint MAY choose to limit the period |
| 2470 | * over which it ignores frames and treat frames that |
| 2471 | * arrive after this time as being in error. |
| 2472 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2473 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2474 | /* RFC7540#5.1:closed: any frame other than |
| 2475 | * PRIO/WU/RST in this state MUST be treated as |
| 2476 | * a connection error |
| 2477 | */ |
| 2478 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2479 | h2c->dft != H2_FT_PRIORITY && |
| 2480 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2481 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2482 | goto strm_err; |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2487 | #if 0 |
| 2488 | // problem below: it is not possible to completely ignore such |
| 2489 | // streams as we need to maintain the compression state as well |
| 2490 | // and for this we need to completely process these frames (eg: |
| 2491 | // HEADERS frames) as well as counting DATA frames to emit |
| 2492 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2493 | // This is a typical case of layer violation where the |
| 2494 | // transported contents are critical to the connection's |
| 2495 | // validity and must be ignored at the same time :-( |
| 2496 | |
| 2497 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2498 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2499 | */ |
| 2500 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2501 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2502 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2503 | h2c->dfl -= ret; |
| 2504 | ret = h2c->dfl == 0; |
| 2505 | goto strm_err; |
| 2506 | } |
| 2507 | #endif |
| 2508 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2509 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2510 | case H2_FT_SETTINGS: |
| 2511 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2512 | ret = h2c_handle_settings(h2c); |
| 2513 | |
| 2514 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2515 | ret = h2c_ack_settings(h2c); |
| 2516 | break; |
| 2517 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2518 | case H2_FT_PING: |
| 2519 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2520 | ret = h2c_handle_ping(h2c); |
| 2521 | |
| 2522 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2523 | ret = h2c_ack_ping(h2c); |
| 2524 | break; |
| 2525 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2526 | case H2_FT_WINDOW_UPDATE: |
| 2527 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2528 | ret = h2c_handle_window_update(h2c, h2s); |
| 2529 | break; |
| 2530 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2531 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2532 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2533 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2534 | * frames' parsers consume all following CONTINUATION |
| 2535 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2536 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2537 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2538 | sess_log(h2c->conn->owner); |
| 2539 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2540 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2541 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2542 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2543 | if (h2c->flags & H2_CF_IS_BACK) |
| 2544 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2545 | else |
| 2546 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2547 | if (tmp_h2s) { |
| 2548 | h2s = tmp_h2s; |
| 2549 | ret = 1; |
| 2550 | } |
| 2551 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2552 | break; |
| 2553 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2554 | case H2_FT_DATA: |
| 2555 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2556 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2557 | |
| 2558 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2559 | ret = h2c_send_strm_wu(h2c); |
| 2560 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2561 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2562 | case H2_FT_PRIORITY: |
| 2563 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2564 | ret = h2c_handle_priority(h2c); |
| 2565 | break; |
| 2566 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2567 | case H2_FT_RST_STREAM: |
| 2568 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2569 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2570 | break; |
| 2571 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2572 | case H2_FT_GOAWAY: |
| 2573 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2574 | ret = h2c_handle_goaway(h2c); |
| 2575 | break; |
| 2576 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2577 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2578 | default: |
| 2579 | /* drop frames that we ignore. They may be larger than |
| 2580 | * the buffer so we drain all of their contents until |
| 2581 | * we reach the end. |
| 2582 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2583 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2584 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2585 | h2c->dfl -= ret; |
| 2586 | ret = h2c->dfl == 0; |
| 2587 | } |
| 2588 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2589 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2590 | /* We may have to send an RST if not done yet */ |
| 2591 | if (h2s->st == H2_SS_ERROR) |
| 2592 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2593 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2594 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2595 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2596 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2597 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2598 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2599 | break; |
| 2600 | |
| 2601 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2602 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2603 | h2c->st0 = H2_CS_FRAME_H; |
| 2604 | } |
| 2605 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2606 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2607 | if (h2c->rcvd_c > 0 && |
| 2608 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2609 | h2c_send_conn_wu(h2c); |
| 2610 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2611 | fail: |
| 2612 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2613 | if (h2s && h2s->cs && |
| 2614 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 2615 | (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2616 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2617 | (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] | 2618 | /* we may have to signal the upper layers */ |
| 2619 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2620 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2621 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2622 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2623 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2627 | * the end. |
| 2628 | */ |
| 2629 | static int h2_process_mux(struct h2c *h2c) |
| 2630 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2631 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2632 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2633 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2634 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2635 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2636 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2637 | if (h2c->st0 == H2_CS_ERROR) { |
| 2638 | h2c->st0 = H2_CS_ERROR2; |
| 2639 | sess_log(h2c->conn->owner); |
| 2640 | } |
| 2641 | goto fail; |
| 2642 | } |
| 2643 | h2c->st0 = H2_CS_SETTINGS1; |
| 2644 | } |
| 2645 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2646 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2647 | return 1; |
| 2648 | } |
| 2649 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2650 | /* start by sending possibly pending window updates */ |
| 2651 | if (h2c->rcvd_c > 0 && |
| 2652 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2653 | h2c_send_conn_wu(h2c) < 0) |
| 2654 | goto fail; |
| 2655 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2656 | /* First we always process the flow control list because the streams |
| 2657 | * waiting there were already elected for immediate emission but were |
| 2658 | * blocked just on this. |
| 2659 | */ |
| 2660 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2661 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2662 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2663 | h2c->st0 >= H2_CS_ERROR) |
| 2664 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2665 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2666 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2667 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2668 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2669 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2670 | /* For some reason, the upper layer failed to subsribe again, |
| 2671 | * so remove it from the send_list |
| 2672 | */ |
| 2673 | if (!h2s->send_wait) { |
| 2674 | LIST_DEL_INIT(&h2s->list); |
| 2675 | continue; |
| 2676 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2677 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2678 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2679 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2680 | } |
| 2681 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2682 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2683 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2684 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2685 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2686 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2687 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2688 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2689 | /* For some reason, the upper layer failed to subsribe again, |
| 2690 | * so remove it from the send_list |
| 2691 | */ |
| 2692 | if (!h2s->send_wait) { |
| 2693 | LIST_DEL_INIT(&h2s->list); |
| 2694 | continue; |
| 2695 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2696 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2697 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2698 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2699 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2700 | } |
| 2701 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2702 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2703 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2704 | if (h2c->st0 == H2_CS_ERROR) { |
| 2705 | if (h2c->max_id >= 0) { |
| 2706 | h2c_send_goaway_error(h2c, NULL); |
| 2707 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2708 | return 0; |
| 2709 | } |
| 2710 | |
| 2711 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2712 | } |
| 2713 | return 1; |
| 2714 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2715 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2718 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2719 | /* Attempt to read data, and subscribe if none available. |
| 2720 | * The function returns 1 if data has been received, otherwise zero. |
| 2721 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2722 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2723 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2724 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2725 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2726 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2727 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2728 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2729 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2730 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2731 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2732 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2733 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2734 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2735 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2736 | if (!buf) { |
| 2737 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2738 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2739 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2740 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2741 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2742 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2743 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2744 | /* HTX in use : try to pre-align the buffer like the |
| 2745 | * rxbufs will be to optimize memory copies. We'll make |
| 2746 | * sure that the frame header lands at the end of the |
| 2747 | * HTX block to alias it upon recv. We cannot use the |
| 2748 | * head because rcv_buf() will realign the buffer if |
| 2749 | * it's empty. Thus we cheat and pretend we already |
| 2750 | * have a few bytes there. |
| 2751 | */ |
| 2752 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2753 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2754 | } |
| 2755 | else |
| 2756 | max = b_room(buf); |
| 2757 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2758 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2759 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2760 | else |
| 2761 | ret = 0; |
| 2762 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2763 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2764 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2765 | 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] | 2766 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2767 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2768 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2769 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2770 | } |
| 2771 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2772 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2773 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2774 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2775 | } |
| 2776 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2777 | /* Try to send data if possible. |
| 2778 | * The function returns 1 if data have been sent, otherwise zero. |
| 2779 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2780 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2781 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2782 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2783 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2784 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2785 | |
| 2786 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2787 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2788 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2789 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2790 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2791 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2792 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2795 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2796 | * pending streams into the existing buffer until it's reportedly full |
| 2797 | * or the end of send requests is reached. Then it tries to send this |
| 2798 | * buffer's contents out, marks it not full if at least one byte could |
| 2799 | * be sent, and tries again. |
| 2800 | * |
| 2801 | * The snd_buf() function normally takes a "flags" argument which may |
| 2802 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2803 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2804 | * connection is streaming lots of data (used to increase TLS record |
| 2805 | * size at the expense of latency). The former can be sent any time |
| 2806 | * there's a buffer full flag, as it indicates at least one stream |
| 2807 | * attempted to send and failed so there are pending data. An |
| 2808 | * alternative would be to set it as long as there's an active stream |
| 2809 | * but that would be problematic for ACKs until we have an absolute |
| 2810 | * guarantee that all waiters have at least one byte to send. The |
| 2811 | * latter should possibly not be set for now. |
| 2812 | */ |
| 2813 | |
| 2814 | done = 0; |
| 2815 | while (!done) { |
| 2816 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2817 | unsigned int released = 0; |
| 2818 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2819 | |
| 2820 | /* fill as much as we can into the current buffer */ |
| 2821 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2822 | done = h2_process_mux(h2c); |
| 2823 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2824 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2825 | break; |
| 2826 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2827 | if (conn->flags & CO_FL_ERROR) |
| 2828 | break; |
| 2829 | |
| 2830 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2831 | flags |= CO_SFL_MSG_MORE; |
| 2832 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2833 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2834 | if (b_data(buf)) { |
| 2835 | int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
| 2836 | if (!ret) |
| 2837 | break; |
| 2838 | sent = 1; |
| 2839 | b_del(buf, ret); |
| 2840 | if (b_data(buf)) |
| 2841 | break; |
| 2842 | } |
| 2843 | b_free(buf); |
| 2844 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2845 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2847 | if (released) |
| 2848 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
| 2849 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2850 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2851 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2852 | } |
| 2853 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2854 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2855 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2856 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2857 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2858 | /* We're not full anymore, so we can wake any task that are waiting |
| 2859 | * for us. |
| 2860 | */ |
| 2861 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2862 | struct h2s *h2s; |
| 2863 | |
| 2864 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2865 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2866 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2867 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2868 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2869 | continue; |
| 2870 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2871 | /* For some reason, the upper layer failed to subsribe again, |
| 2872 | * so remove it from the send_list |
| 2873 | */ |
| 2874 | if (!h2s->send_wait) { |
| 2875 | LIST_DEL_INIT(&h2s->list); |
| 2876 | continue; |
| 2877 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2878 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2879 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2880 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2881 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2882 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2883 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2884 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2885 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2886 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2887 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2888 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2889 | 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] | 2890 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2891 | } |
| 2892 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2893 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2894 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2895 | { |
| 2896 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2897 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2898 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2899 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2900 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2901 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2902 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2903 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2904 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2905 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2906 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2907 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2908 | /* callback called on any event by the connection handler. |
| 2909 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2910 | * destruction of the connection (which normally doesn not happen in h2). |
| 2911 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2912 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2913 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2914 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2915 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2916 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2917 | h2_process_demux(h2c); |
| 2918 | |
| 2919 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2920 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2921 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2922 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2923 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2924 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2925 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2926 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2927 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2928 | /* frontend is stopping, reload likely in progress, let's try |
| 2929 | * to announce a graceful shutdown if not yet done. We don't |
| 2930 | * care if it fails, it will be tried again later. |
| 2931 | */ |
| 2932 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2933 | if (h2c->last_sid < 0) |
| 2934 | h2c->last_sid = (1U << 31) - 1; |
| 2935 | h2c_send_goaway_error(h2c, NULL); |
| 2936 | } |
| 2937 | } |
| 2938 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2939 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2940 | * If we received early data, and the handshake is done, wake |
| 2941 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2942 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2943 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2944 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2945 | struct eb32_node *node; |
| 2946 | struct h2s *h2s; |
| 2947 | |
| 2948 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2949 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2950 | |
| 2951 | while (node) { |
| 2952 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2953 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2954 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2955 | node = eb32_next(node); |
| 2956 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2957 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2958 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2959 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2960 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2961 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2962 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 2963 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2964 | |
| 2965 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2966 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2967 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2968 | return -1; |
| 2969 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2972 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2973 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2974 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2975 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2976 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2977 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2978 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2979 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2980 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 2981 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2982 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2983 | if (h2c->task) { |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2984 | if (eb_is_empty(&h2c->streams_by_id) || br_data(h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2985 | 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] | 2986 | task_queue(h2c->task); |
| 2987 | } |
| 2988 | else |
| 2989 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2990 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2991 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2992 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2993 | return 0; |
| 2994 | } |
| 2995 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2996 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2997 | static int h2_wake(struct connection *conn) |
| 2998 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2999 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3000 | |
| 3001 | return (h2_process(h2c)); |
| 3002 | } |
| 3003 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3004 | /* Connection timeout management. The principle is that if there's no receipt |
| 3005 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3006 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3007 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3008 | * GOAWAY frame. |
| 3009 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3010 | 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] | 3011 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3012 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3013 | int expired = tick_is_expired(t->expire, now_ms); |
| 3014 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3015 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3016 | return t; |
| 3017 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3018 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3019 | |
| 3020 | if (!h2c) { |
| 3021 | /* resources were already deleted */ |
| 3022 | return NULL; |
| 3023 | } |
| 3024 | |
| 3025 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3026 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3027 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3028 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3029 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3030 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3031 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3032 | } |
| 3033 | |
| 3034 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3035 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3036 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3037 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3038 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3039 | 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] | 3040 | unsigned int released = 0; |
| 3041 | struct buffer *buf; |
| 3042 | |
| 3043 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3044 | if (b_data(buf)) { |
| 3045 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3046 | if (!ret) |
| 3047 | break; |
| 3048 | b_del(buf, ret); |
| 3049 | if (b_data(buf)) |
| 3050 | break; |
| 3051 | b_free(buf); |
| 3052 | released++; |
| 3053 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3054 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3055 | |
| 3056 | if (released) |
| 3057 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3058 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3059 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3060 | /* either we can release everything now or it will be done later once |
| 3061 | * the last stream closes. |
| 3062 | */ |
| 3063 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3064 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3065 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3066 | return NULL; |
| 3067 | } |
| 3068 | |
| 3069 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3070 | /*******************************************/ |
| 3071 | /* functions below are used by the streams */ |
| 3072 | /*******************************************/ |
| 3073 | |
| 3074 | /* |
| 3075 | * Attach a new stream to a connection |
| 3076 | * (Used for outgoing connections) |
| 3077 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3078 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3079 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3080 | struct conn_stream *cs; |
| 3081 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3082 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3083 | |
| 3084 | cs = cs_new(conn); |
| 3085 | if (!cs) |
| 3086 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3087 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3088 | if (!h2s) { |
| 3089 | cs_free(cs); |
| 3090 | return NULL; |
| 3091 | } |
| 3092 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3093 | } |
| 3094 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3095 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3096 | * We have to scan because we may have some orphan streams. It might be |
| 3097 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3098 | * orphans. |
| 3099 | */ |
| 3100 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3101 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3102 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3103 | struct h2s *h2s; |
| 3104 | struct eb32_node *node; |
| 3105 | |
| 3106 | node = eb32_first(&h2c->streams_by_id); |
| 3107 | while (node) { |
| 3108 | h2s = container_of(node, struct h2s, by_id); |
| 3109 | if (h2s->cs) |
| 3110 | return h2s->cs; |
| 3111 | node = eb32_next(node); |
| 3112 | } |
| 3113 | return NULL; |
| 3114 | } |
| 3115 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3116 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3117 | * Destroy the mux and the associated connection, if it is no longer used |
| 3118 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3119 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3120 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3121 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3122 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3123 | 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] | 3124 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3128 | * Detach the stream from the connection and possibly release the connection. |
| 3129 | */ |
| 3130 | static void h2_detach(struct conn_stream *cs) |
| 3131 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3132 | struct h2s *h2s = cs->ctx; |
| 3133 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3134 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3135 | |
| 3136 | cs->ctx = NULL; |
| 3137 | if (!h2s) |
| 3138 | return; |
| 3139 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3140 | /* 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] | 3141 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3142 | h2s->send_wait != &h2s->wait_event) { |
| 3143 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 3144 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3145 | /* |
| 3146 | * At this point, the stream_interface is supposed to have called |
| 3147 | * h2_unsubscribe(), so the only way there's still a |
| 3148 | * subscription that came from the stream_interface (as we |
| 3149 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3150 | * without the stream_interface involved) is that we subscribed |
| 3151 | * for sending, we woke the tasklet up and removed the |
| 3152 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3153 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3154 | * run yet. Make sure to handle that by explicitely setting |
| 3155 | * send_wait to NULL, as nothing else will do it for us. |
| 3156 | */ |
| 3157 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3160 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3161 | h2c = h2s->h2c; |
| 3162 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3163 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3164 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3165 | !h2_frt_has_too_many_cs(h2c)) { |
| 3166 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3167 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3168 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3169 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3170 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3171 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3172 | * an ES or RST frame), so orphan it in this case. |
| 3173 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3174 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3175 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3176 | (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] | 3177 | return; |
| 3178 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3179 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3180 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3181 | /* unblock the connection if it was blocked on this |
| 3182 | * stream. |
| 3183 | */ |
| 3184 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3185 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3186 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3187 | } |
| 3188 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3189 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3190 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3191 | if (h2c->flags & H2_CF_IS_BACK && |
| 3192 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3193 | if (!(h2c->conn->flags & |
| 3194 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3195 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3196 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3197 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3198 | h2c->conn->owner = NULL; |
| 3199 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3200 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3201 | /* The server doesn't want it, let's kill the connection right away */ |
| 3202 | h2c->conn->mux->destroy(h2c->conn); |
| 3203 | return; |
| 3204 | } |
| 3205 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3206 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3207 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3208 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3209 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3210 | return; |
| 3211 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3212 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3213 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3214 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3215 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3216 | struct server *srv = objt_server(h2c->conn->target); |
| 3217 | |
| 3218 | if (srv) { |
| 3219 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3220 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3221 | else |
| 3222 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3223 | } |
| 3224 | |
| 3225 | } |
| 3226 | } |
| 3227 | } |
| 3228 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3229 | /* We don't want to close right now unless we're removing the |
| 3230 | * last stream, and either the connection is in error, or it |
| 3231 | * reached the ID already specified in a GOAWAY frame received |
| 3232 | * or sent (as seen by last_sid >= 0). |
| 3233 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3234 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3235 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3236 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3237 | } |
| 3238 | else if (h2c->task) { |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3239 | if (eb_is_empty(&h2c->streams_by_id) || br_data(h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3240 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3241 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3242 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3243 | else |
| 3244 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3245 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3246 | } |
| 3247 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3248 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3249 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3250 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3251 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3252 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3253 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3254 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3255 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3256 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3257 | /* a connstream may require us to immediately kill the whole connection |
| 3258 | * for example because of a "tcp-request content reject" rule that is |
| 3259 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3260 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3261 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3262 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3263 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3264 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3265 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3266 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3267 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3268 | /* Nothing was never sent for this stream, so reset with |
| 3269 | * REFUSED_STREAM error to let the client retry the |
| 3270 | * request. |
| 3271 | */ |
| 3272 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3273 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3274 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3275 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3276 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3277 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3278 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3279 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3280 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3281 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3282 | done: |
| 3283 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3284 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3285 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3286 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3287 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3288 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3289 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3290 | h2s->send_wait = sw; |
| 3291 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3292 | h2s->send_wait = sw; |
| 3293 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3294 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3295 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3296 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3297 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3298 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3301 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3302 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3303 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3304 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3305 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3306 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3307 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3308 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3309 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3310 | if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR && |
| 3311 | (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3312 | /* we can cleanly close using an empty data frame only after headers */ |
| 3313 | |
| 3314 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3315 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3316 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3317 | |
| 3318 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3319 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3320 | else |
| 3321 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3322 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3323 | /* a connstream may require us to immediately kill the whole connection |
| 3324 | * for example because of a "tcp-request content reject" rule that is |
| 3325 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3326 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3327 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3328 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3329 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3330 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3331 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3332 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3333 | else { |
| 3334 | /* Nothing was never sent for this stream, so reset with |
| 3335 | * REFUSED_STREAM error to let the client retry the |
| 3336 | * request. |
| 3337 | */ |
| 3338 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3339 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3340 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3341 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3342 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3343 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3344 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3345 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3346 | } |
| 3347 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3348 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3349 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3350 | done: |
| 3351 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3352 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3353 | |
| 3354 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3355 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3356 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3357 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3358 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3359 | h2s->send_wait = sw; |
| 3360 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3361 | h2s->send_wait = sw; |
| 3362 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3363 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3364 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3365 | /* let the handler know we want to shutw */ |
| 3366 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3367 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3368 | } |
| 3369 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3370 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3371 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3372 | * and prevented the last frame from being emitted. |
| 3373 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3374 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3375 | { |
| 3376 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3377 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3378 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3379 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3380 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3381 | h2_do_shutw(h2s); |
| 3382 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3383 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3384 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3385 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3386 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3387 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3388 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3389 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3390 | if (!h2s->cs) { |
| 3391 | h2s_destroy(h2s); |
| 3392 | if (h2c_is_dead(h2c)) |
| 3393 | h2_release(h2c); |
| 3394 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3395 | } |
| 3396 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3397 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3400 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3401 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3402 | { |
| 3403 | struct h2s *h2s = cs->ctx; |
| 3404 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3405 | if (cs->flags & CS_FL_KILL_CONN) |
| 3406 | h2s->flags |= H2_SF_KILL_CONN; |
| 3407 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3408 | if (!mode) |
| 3409 | return; |
| 3410 | |
| 3411 | h2_do_shutr(h2s); |
| 3412 | } |
| 3413 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3414 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3415 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3416 | { |
| 3417 | struct h2s *h2s = cs->ctx; |
| 3418 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3419 | if (cs->flags & CS_FL_KILL_CONN) |
| 3420 | h2s->flags |= H2_SF_KILL_CONN; |
| 3421 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3422 | h2_do_shutw(h2s); |
| 3423 | } |
| 3424 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3425 | /* 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] | 3426 | * HTX request or response depending on the connection's side. Returns a |
| 3427 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3428 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3429 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3430 | * 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] | 3431 | * |
| 3432 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3433 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3434 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3435 | * leaving a hole between the main frame and the beginning of the next one. |
| 3436 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3437 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3438 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3439 | * |
| 3440 | * A buffer at the beginning of processing may look like this : |
| 3441 | * |
| 3442 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3443 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3444 | * `---^---------^-----^--------------^--------------^------^---' |
| 3445 | * | | <-----> | | |
| 3446 | * area | dpl | wrap |
| 3447 | * |<--------------> | |
| 3448 | * | dfl | |
| 3449 | * |<-------------------------------------------------->| |
| 3450 | * head data |
| 3451 | * |
| 3452 | * Padding is automatically overwritten when folding, participating to the |
| 3453 | * hole size after dfl : |
| 3454 | * |
| 3455 | * ,---.------------------------.-----.--------------.------.---. |
| 3456 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3457 | * `---^------------------------^-----^--------------^------^---' |
| 3458 | * | | <-----> | | |
| 3459 | * area | hole | wrap |
| 3460 | * |<-----------------------> | |
| 3461 | * | dfl | |
| 3462 | * |<-------------------------------------------------->| |
| 3463 | * head data |
| 3464 | * |
| 3465 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3466 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3467 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3468 | * |
| 3469 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3470 | * so that the function can update the following flags : |
| 3471 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3472 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3473 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3474 | * |
| 3475 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3476 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3477 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3478 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3479 | 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] | 3480 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3481 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3482 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3483 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3484 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3485 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3486 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3487 | int flen; // header frame len |
| 3488 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3489 | int ret = 0; |
| 3490 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3491 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3492 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3493 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3494 | next_frame: |
| 3495 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3496 | goto leave; // incomplete input frame |
| 3497 | |
| 3498 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3499 | * this case, we'll try to paste it immediately after the initial |
| 3500 | * HEADERS frame payload and kill any possible padding. The initial |
| 3501 | * frame's length will be increased to represent the concatenation |
| 3502 | * of the two frames. The next frame is read from position <tlen> |
| 3503 | * and written at position <flen> (minus padding if some is present). |
| 3504 | */ |
| 3505 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3506 | struct h2_fh hdr; |
| 3507 | int clen; // CONTINUATION frame's payload length |
| 3508 | |
| 3509 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3510 | /* no more data, the buffer may be full, either due to |
| 3511 | * too large a frame or because of too large a hole that |
| 3512 | * we're going to compact at the end. |
| 3513 | */ |
| 3514 | goto leave; |
| 3515 | } |
| 3516 | |
| 3517 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3518 | /* RFC7540#6.10: frame of unexpected type */ |
| 3519 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3520 | goto fail; |
| 3521 | } |
| 3522 | |
| 3523 | if (hdr.sid != h2c->dsi) { |
| 3524 | /* RFC7540#6.10: frame of different stream */ |
| 3525 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3526 | goto fail; |
| 3527 | } |
| 3528 | |
| 3529 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3530 | /* RFC7540#4.2: invalid frame length */ |
| 3531 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3532 | goto fail; |
| 3533 | } |
| 3534 | |
| 3535 | /* detect when we must stop aggragating frames */ |
| 3536 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3537 | |
| 3538 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3539 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3540 | if (clen > hdr.len) |
| 3541 | clen = hdr.len; |
| 3542 | |
| 3543 | /* Move the frame's payload over the padding, hole and frame |
| 3544 | * header. At least one of hole or dpl is null (see diagrams |
| 3545 | * above). The hole moves after the new aggragated frame. |
| 3546 | */ |
| 3547 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3548 | h2c->dfl += clen - h2c->dpl; |
| 3549 | hole += h2c->dpl + 9; |
| 3550 | h2c->dpl = 0; |
| 3551 | goto next_frame; |
| 3552 | } |
| 3553 | |
| 3554 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3555 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3556 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3557 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3558 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3559 | copy = alloc_trash_chunk(); |
| 3560 | if (!copy) { |
| 3561 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3562 | goto fail; |
| 3563 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3564 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3565 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3566 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3569 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3570 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3571 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3572 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3573 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3574 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3577 | if (flen < 5) { |
| 3578 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3579 | goto fail; |
| 3580 | } |
| 3581 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3582 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3583 | flen -= 5; |
| 3584 | } |
| 3585 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3586 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3587 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3588 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3589 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3590 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3591 | /* we can't retry a failed decompression operation so we must be very |
| 3592 | * careful not to take any risks. In practice the output buffer is |
| 3593 | * always empty except maybe for trailers, in which case we simply have |
| 3594 | * to wait for the upper layer to finish consuming what is available. |
| 3595 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3596 | |
| 3597 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3598 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3599 | if (!htx_is_empty(htx)) { |
| 3600 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3601 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3602 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3603 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3604 | if (b_data(rxbuf)) { |
| 3605 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3606 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3607 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3608 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3609 | rxbuf->head = 0; |
| 3610 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3611 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3612 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3613 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3614 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3615 | sizeof(list)/sizeof(list[0]), tmp); |
| 3616 | if (outlen < 0) { |
| 3617 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3618 | goto fail; |
| 3619 | } |
| 3620 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3621 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3622 | * the parser's state to commit these changes and allow us to later |
| 3623 | * fail solely on the stream if needed. |
| 3624 | */ |
| 3625 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3626 | h2c->dfl = hole = 0; |
| 3627 | h2c->st0 = H2_CS_FRAME_H; |
| 3628 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3629 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3630 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3631 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3632 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3633 | goto trailers; |
| 3634 | |
| 3635 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3636 | if (htx) { |
| 3637 | /* HTX mode */ |
| 3638 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3639 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3640 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3641 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3642 | } else { |
| 3643 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3644 | 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] | 3645 | if (outlen > 0) |
| 3646 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3647 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3648 | |
| 3649 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3650 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3651 | goto fail; |
| 3652 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3653 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3654 | if (msgf & H2_MSGF_BODY) { |
| 3655 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3656 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3657 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3658 | if (htx) |
| 3659 | htx->extra = *body_len; |
| 3660 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3661 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3662 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3663 | } |
| 3664 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3665 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3666 | /* indicate that a HEADERS frame was received for this stream, except |
| 3667 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3668 | * expected. |
| 3669 | */ |
| 3670 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3671 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3672 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3673 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3674 | /* Mark the end of message, either using EOM in HTX or with the |
| 3675 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3676 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3677 | * we must not leave an HTX trailers block not followed by an |
| 3678 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3679 | * 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] | 3680 | */ |
| 3681 | if (htx) { |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3682 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3683 | struct htx_blk *tail = htx_get_tail_blk(htx); |
| 3684 | |
| 3685 | if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR) |
| 3686 | htx_remove_blk(htx, tail); |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3687 | goto fail; |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3688 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3689 | } |
| 3690 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3691 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3692 | goto fail; |
| 3693 | } |
| 3694 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3695 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3696 | /* success */ |
| 3697 | ret = 1; |
| 3698 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3699 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3700 | /* 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] | 3701 | * move the remaining data over it. |
| 3702 | */ |
| 3703 | if (hole) { |
| 3704 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3705 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3706 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3707 | b_sub(&h2c->dbuf, hole); |
| 3708 | } |
| 3709 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3710 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3711 | /* too large frames */ |
| 3712 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3713 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3714 | } |
| 3715 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3716 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3717 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3718 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3719 | return ret; |
| 3720 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3721 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3722 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3723 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3724 | |
| 3725 | trailers: |
| 3726 | /* This is the last HEADERS frame hence a trailer */ |
| 3727 | |
| 3728 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3729 | /* It's a trailer but it's missing ES flag */ |
| 3730 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3731 | goto fail; |
| 3732 | } |
| 3733 | |
| 3734 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3735 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3736 | * other modes, the trailers are silently dropped. |
| 3737 | */ |
| 3738 | if (htx) { |
| 3739 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3740 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3741 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3742 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3743 | } |
| 3744 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3745 | /* Legacy mode with chunked encoding : we must finalize the |
| 3746 | * data block message emit the trailing CRLF */ |
| 3747 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3748 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3749 | |
| 3750 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3751 | if (outlen > 0) |
| 3752 | b_add(rxbuf, outlen); |
| 3753 | else |
| 3754 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3758 | } |
| 3759 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3760 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3761 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3762 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3763 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3764 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3765 | * 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] | 3766 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3767 | * send the current frame, 0 if it couldn't complete, in which case |
| 3768 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3769 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3770 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3771 | * have checked the frame header and ensured that the frame was complete or the |
| 3772 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3773 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3774 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3775 | { |
| 3776 | struct h2c *h2c = h2s->h2c; |
| 3777 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3778 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3779 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3780 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3781 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3782 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3783 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3784 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3785 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3786 | if (!csbuf) { |
| 3787 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3788 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3791 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3792 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3793 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3794 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3795 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3796 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3797 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3798 | if (flen > b_data(&h2c->dbuf)) { |
| 3799 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3800 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3801 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3804 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame^] | 3805 | unsigned int sent; |
| 3806 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3807 | block1 = htx_free_data_space(htx); |
| 3808 | if (!block1) { |
| 3809 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3810 | goto fail; |
| 3811 | } |
| 3812 | if (flen > block1) |
| 3813 | flen = block1; |
| 3814 | |
| 3815 | /* here, flen is the max we can copy into the output buffer */ |
| 3816 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3817 | if (flen > block1) |
| 3818 | flen = block1; |
| 3819 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame^] | 3820 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3821 | |
| 3822 | b_del(&h2c->dbuf, flen); |
| 3823 | h2c->dfl -= flen; |
| 3824 | h2c->rcvd_c += flen; |
| 3825 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3826 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3827 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3828 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3829 | htx->extra = h2s->body_len; |
| 3830 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame^] | 3831 | |
| 3832 | if (sent < flen) { |
| 3833 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3834 | goto fail; |
| 3835 | } |
| 3836 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3837 | goto try_again; |
| 3838 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3839 | else if (unlikely(b_space_wraps(csbuf) && |
| 3840 | flen + chklen <= b_room(csbuf) && |
| 3841 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3842 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3843 | * not too many data in the buffer, let's defragment it and try |
| 3844 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3845 | */ |
| 3846 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3849 | /* chunked-encoding requires more room */ |
| 3850 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3851 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3852 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3853 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3854 | (chklen < 1048576) ? 4 : 8; |
| 3855 | chklen += 4; // CRLF, CRLF |
| 3856 | } |
| 3857 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3858 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3859 | if (flen + chklen > b_room(csbuf)) { |
| 3860 | if (chklen >= b_room(csbuf)) { |
| 3861 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3862 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3863 | } |
| 3864 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3868 | /* emit the chunk size */ |
| 3869 | unsigned int chksz = flen; |
| 3870 | char str[10]; |
| 3871 | char *beg; |
| 3872 | |
| 3873 | beg = str + sizeof(str); |
| 3874 | *--beg = '\n'; |
| 3875 | *--beg = '\r'; |
| 3876 | do { |
| 3877 | *--beg = hextab[chksz & 0xF]; |
| 3878 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3879 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3880 | } |
| 3881 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3882 | /* Block1 is the length of the first block before the buffer wraps, |
| 3883 | * block2 is the optional second block to reach the end of the frame. |
| 3884 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3885 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3886 | if (block1 > flen) |
| 3887 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3888 | block2 = flen - block1; |
| 3889 | |
| 3890 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3891 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3892 | |
| 3893 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3894 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3895 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3896 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3897 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3898 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3901 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3902 | * by the caller when seeing FRAME_A after sending the window update). |
| 3903 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3904 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3905 | h2c->dfl -= flen; |
| 3906 | h2c->rcvd_c += flen; |
| 3907 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3908 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3909 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3910 | h2s->body_len -= flen; |
| 3911 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3912 | if (h2c->dfl > h2c->dpl) { |
| 3913 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3914 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3915 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3916 | } |
| 3917 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3918 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3919 | /* here we're done with the frame, all the payload (except padding) was |
| 3920 | * transferred. |
| 3921 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3922 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3923 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3924 | if (htx) { |
| 3925 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3926 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3927 | goto fail; |
| 3928 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3929 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3930 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3931 | /* emit the trailing 0 CRLF CRLF */ |
| 3932 | if (b_room(csbuf) < 5) { |
| 3933 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3934 | goto fail; |
| 3935 | } |
| 3936 | chklen += 5; |
| 3937 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3938 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3939 | } |
| 3940 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3941 | h2c->rcvd_c += h2c->dpl; |
| 3942 | h2c->rcvd_s += h2c->dpl; |
| 3943 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3944 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3945 | if (htx) |
| 3946 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3947 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3948 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3949 | if (htx) |
| 3950 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3951 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3954 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3955 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3956 | * number of bytes sent. The caller must check the stream's status to detect |
| 3957 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3958 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3959 | 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] | 3960 | { |
| 3961 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3962 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3963 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3964 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 3965 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3966 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3967 | int es_now = 0; |
| 3968 | int ret = 0; |
| 3969 | int hdr; |
| 3970 | |
| 3971 | if (h2c_mux_busy(h2c, h2s)) { |
| 3972 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3976 | /* First, try to parse the H1 response and index it into <list>. |
| 3977 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3978 | * block does not wrap and we can safely read it this way without |
| 3979 | * having to realign the buffer. |
| 3980 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3981 | 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] | 3982 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3983 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3984 | /* incomplete or invalid response, this is abnormal coming from |
| 3985 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3986 | * so that won't be fixed, raise an error now. |
| 3987 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3988 | * FIXME: we should instead add the ability to only return a |
| 3989 | * 502 bad gateway. But in theory this is not supposed to |
| 3990 | * happen. |
| 3991 | */ |
| 3992 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3993 | ret = 0; |
| 3994 | goto end; |
| 3995 | } |
| 3996 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3997 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3998 | |
| 3999 | /* certain statuses have no body or an empty one, regardless of |
| 4000 | * what the headers say. |
| 4001 | */ |
| 4002 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4003 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4004 | h1m->curr_len = h1m->body_len = 0; |
| 4005 | } |
| 4006 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4007 | /* no contents, claim c-len is present and set to zero */ |
| 4008 | h1m->flags &= ~H1_MF_CHNK; |
| 4009 | h1m->flags |= H1_MF_CLEN; |
| 4010 | h1m->curr_len = h1m->body_len = 0; |
| 4011 | } |
| 4012 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4013 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4014 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4015 | if (!h2_get_buf(h2c, mbuf)) { |
| 4016 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4017 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4018 | return 0; |
| 4019 | } |
| 4020 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4021 | chunk_reset(&outbuf); |
| 4022 | |
| 4023 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4024 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4025 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4026 | break; |
| 4027 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4028 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4029 | } |
| 4030 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4031 | if (outbuf.size < 9) |
| 4032 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4033 | |
| 4034 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4035 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4036 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4037 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4038 | |
| 4039 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4040 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4041 | /* this is an unparsable response */ |
| 4042 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4043 | ret = 0; |
| 4044 | goto end; |
| 4045 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4046 | |
| 4047 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4048 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4049 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4050 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | /* encode all headers, stop at empty name */ |
| 4054 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4055 | /* these ones do not exist in H2 and must be dropped. */ |
| 4056 | if (isteq(list[hdr].n, ist("connection")) || |
| 4057 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4058 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4059 | isteq(list[hdr].n, ist("upgrade")) || |
| 4060 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4061 | continue; |
| 4062 | |
| 4063 | if (isteq(list[hdr].n, ist(""))) |
| 4064 | break; // end |
| 4065 | |
| 4066 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4067 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4068 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4069 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4070 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
| 4074 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4075 | 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] | 4076 | es_now = 1; |
| 4077 | |
| 4078 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4079 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4080 | |
| 4081 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4082 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4083 | |
| 4084 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4085 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4086 | |
| 4087 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4088 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4089 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4090 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4091 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4092 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4093 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4094 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4095 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4096 | h2s->flags |= H2_SF_ES_SENT; |
| 4097 | if (h2s->st == H2_SS_OPEN) |
| 4098 | h2s->st = H2_SS_HLOC; |
| 4099 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4100 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4101 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4102 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4103 | /* 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] | 4104 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4105 | 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] | 4106 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4107 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4108 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4109 | |
| 4110 | /* 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] | 4111 | |
| 4112 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4113 | //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] | 4114 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4115 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4116 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4117 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4118 | h1m_init_res(h1m); |
| 4119 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4120 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4121 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4122 | ret = 0; |
| 4123 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4124 | } |
| 4125 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4126 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4127 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4128 | * the number of bytes sent. The caller must check the stream's status to |
| 4129 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4130 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4131 | 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] | 4132 | { |
| 4133 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4134 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4135 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4136 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4137 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4138 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4139 | int es_now = 0; |
| 4140 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4141 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4142 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4143 | |
| 4144 | if (h2c_mux_busy(h2c, h2s)) { |
| 4145 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4146 | goto end; |
| 4147 | } |
| 4148 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4149 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4150 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4151 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4152 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4153 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4154 | goto end; |
| 4155 | } |
| 4156 | |
| 4157 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4158 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4159 | goto end; |
| 4160 | |
| 4161 | chunk_reset(&outbuf); |
| 4162 | |
| 4163 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4164 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4165 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4166 | break; |
| 4167 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4168 | /* If there are pending data in the output buffer, and we have |
| 4169 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4170 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4171 | * is full and wait, to save some slow realign calls. |
| 4172 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4173 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4174 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4175 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4176 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4177 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4178 | goto end; |
| 4179 | } |
| 4180 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4181 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4185 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4186 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4187 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4188 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4189 | goto end; |
| 4190 | } |
| 4191 | |
| 4192 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4193 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4194 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4195 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4196 | |
| 4197 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4198 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4199 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4200 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4201 | break; |
| 4202 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4203 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4204 | if ((long long)size > h1m->curr_len) |
| 4205 | size = h1m->curr_len; |
| 4206 | break; |
| 4207 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4208 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4209 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4210 | if (!ret) |
| 4211 | goto end; |
| 4212 | |
| 4213 | if (ret < 0) { |
| 4214 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4215 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4216 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4217 | goto end; |
| 4218 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4219 | max -= ret; |
| 4220 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4221 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4222 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4223 | } |
| 4224 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4225 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4226 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4227 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4228 | if (!ret) |
| 4229 | goto end; |
| 4230 | |
| 4231 | if (ret < 0) { |
| 4232 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4233 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4234 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4235 | goto end; |
| 4236 | } |
| 4237 | |
| 4238 | size = chunk; |
| 4239 | h1m->curr_len = chunk; |
| 4240 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4241 | max -= ret; |
| 4242 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4243 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4244 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4245 | if (!size) |
| 4246 | goto send_empty; |
| 4247 | } |
| 4248 | |
| 4249 | /* in MSG_DATA state, continue below */ |
| 4250 | size = h1m->curr_len; |
| 4251 | break; |
| 4252 | } |
| 4253 | |
| 4254 | /* we have in <size> the exact number of bytes we need to copy from |
| 4255 | * the H1 buffer. We need to check this against the connection's and |
| 4256 | * the stream's send windows, and to ensure that this fits in the max |
| 4257 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4258 | * the frame header). The connection's flow control is applied last so |
| 4259 | * that we can use a separate list of streams which are immediately |
| 4260 | * unblocked on window opening. Note: we don't implement padding. |
| 4261 | */ |
| 4262 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4263 | if (size > max) |
| 4264 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4265 | |
| 4266 | if (size > h2s->mws) |
| 4267 | size = h2s->mws; |
| 4268 | |
| 4269 | if (size <= 0) { |
| 4270 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4271 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4272 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4273 | goto end; |
| 4274 | } |
| 4275 | |
| 4276 | if (h2c->mfs && size > h2c->mfs) |
| 4277 | size = h2c->mfs; |
| 4278 | |
| 4279 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4280 | /* It doesn't fit at once. If it at least fits once split and |
| 4281 | * the amount of data to move is low, let's defragment the |
| 4282 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4283 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4284 | if (b_space_wraps(mbuf) && |
| 4285 | (size + 9 <= b_room(mbuf)) && |
| 4286 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4287 | goto realign_again; |
| 4288 | size = outbuf.size - 9; |
| 4289 | } |
| 4290 | |
| 4291 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4292 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4293 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4294 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4295 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4296 | goto end; |
| 4297 | } |
| 4298 | |
| 4299 | if (size > h2c->mws) |
| 4300 | size = h2c->mws; |
| 4301 | |
| 4302 | if (size <= 0) { |
| 4303 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4304 | goto end; |
| 4305 | } |
| 4306 | |
| 4307 | /* copy whatever we can */ |
| 4308 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4309 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4310 | if (ret == 1) |
| 4311 | len2 = 0; |
| 4312 | |
| 4313 | if (!ret || len1 + len2 < size) { |
| 4314 | /* FIXME: must normally never happen */ |
| 4315 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4316 | goto end; |
| 4317 | } |
| 4318 | |
| 4319 | /* limit len1/len2 to size */ |
| 4320 | if (len1 + len2 > size) { |
| 4321 | int sub = len1 + len2 - size; |
| 4322 | |
| 4323 | if (len2 > sub) |
| 4324 | len2 -= sub; |
| 4325 | else { |
| 4326 | sub -= len2; |
| 4327 | len2 = 0; |
| 4328 | len1 -= sub; |
| 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4333 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4334 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4335 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4336 | |
| 4337 | send_empty: |
| 4338 | /* we may need to add END_STREAM */ |
| 4339 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4340 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4341 | * |
| 4342 | * FIXME: what we do here is not correct because we send end_stream |
| 4343 | * before knowing if we'll have to send a HEADERS frame for the |
| 4344 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4345 | * after the end of trailers, so it will be left to the caller to |
| 4346 | * eat it. The right way to do it would be to measure trailers here |
| 4347 | * and to send ES only if there are no trailers. |
| 4348 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4349 | */ |
| 4350 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4351 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4352 | es_now = 1; |
| 4353 | |
| 4354 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4355 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4356 | |
| 4357 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4358 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4359 | |
| 4360 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4361 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4362 | |
| 4363 | /* consume incoming H1 response */ |
| 4364 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4365 | max -= size; |
| 4366 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4367 | total += size; |
| 4368 | h1m->curr_len -= size; |
| 4369 | h2s->mws -= size; |
| 4370 | h2c->mws -= size; |
| 4371 | |
| 4372 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4373 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4374 | goto new_frame; |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | if (es_now) { |
| 4379 | if (h2s->st == H2_SS_OPEN) |
| 4380 | h2s->st = H2_SS_HLOC; |
| 4381 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4382 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4383 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4384 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4385 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4386 | total += max; |
| 4387 | ofs += max; |
| 4388 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4389 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4390 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4391 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4392 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4393 | h2s->flags |= H2_SF_ES_SENT; |
| 4394 | } |
| 4395 | |
| 4396 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4397 | 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] | 4398 | return total; |
| 4399 | } |
| 4400 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4401 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4402 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4403 | * must check the stream's status to detect any error which might have happened |
| 4404 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4405 | * from the message. The htx message is assumed to be valid since produced from |
| 4406 | * the internal code, hence it contains a start line, an optional series of |
| 4407 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4408 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4409 | */ |
| 4410 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4411 | { |
| 4412 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4413 | struct h2c *h2c = h2s->h2c; |
| 4414 | struct htx_blk *blk; |
| 4415 | struct htx_blk *blk_end; |
| 4416 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4417 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4418 | struct htx_sl *sl; |
| 4419 | enum htx_blk_type type; |
| 4420 | int es_now = 0; |
| 4421 | int ret = 0; |
| 4422 | int hdr; |
| 4423 | int idx; |
| 4424 | |
| 4425 | if (h2c_mux_busy(h2c, h2s)) { |
| 4426 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4430 | /* determine the first block which must not be deleted, blk_end may |
| 4431 | * be NULL if all blocks have to be deleted. |
| 4432 | */ |
| 4433 | idx = htx_get_head(htx); |
| 4434 | blk_end = NULL; |
| 4435 | while (idx != -1) { |
| 4436 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4437 | idx = htx_get_next(htx, idx); |
| 4438 | if (type == HTX_BLK_EOH) { |
| 4439 | if (idx != -1) |
| 4440 | blk_end = htx_get_blk(htx, idx); |
| 4441 | break; |
| 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4446 | blk = htx_get_head_blk(htx); |
| 4447 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
| 4448 | ALREADY_CHECKED(blk); |
| 4449 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4450 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4451 | if (h2s->status < 100 || h2s->status > 999) |
| 4452 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4453 | |
| 4454 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4455 | hdr = 0; |
| 4456 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4457 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4458 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4459 | blk = htx_get_blk(htx, idx); |
| 4460 | type = htx_get_blk_type(blk); |
| 4461 | |
| 4462 | if (type == HTX_BLK_UNUSED) |
| 4463 | continue; |
| 4464 | |
| 4465 | if (type != HTX_BLK_HDR) |
| 4466 | break; |
| 4467 | |
| 4468 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4469 | goto fail; |
| 4470 | |
| 4471 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4472 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4473 | hdr++; |
| 4474 | } |
| 4475 | |
| 4476 | /* marker for end of headers */ |
| 4477 | list[hdr].n = ist(""); |
| 4478 | |
| 4479 | if (h2s->status == 204 || h2s->status == 304) { |
| 4480 | /* no contents, claim c-len is present and set to zero */ |
| 4481 | es_now = 1; |
| 4482 | } |
| 4483 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4484 | mbuf = br_tail(h2c->mbuf); |
| 4485 | retry: |
| 4486 | if (!h2_get_buf(h2c, mbuf)) { |
| 4487 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4488 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4489 | return 0; |
| 4490 | } |
| 4491 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4492 | chunk_reset(&outbuf); |
| 4493 | |
| 4494 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4495 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4496 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4497 | break; |
| 4498 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4499 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | if (outbuf.size < 9) |
| 4503 | goto full; |
| 4504 | |
| 4505 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4506 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4507 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4508 | outbuf.data = 9; |
| 4509 | |
| 4510 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4511 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4512 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4513 | goto realign_again; |
| 4514 | goto full; |
| 4515 | } |
| 4516 | |
| 4517 | /* encode all headers, stop at empty name */ |
| 4518 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4519 | /* these ones do not exist in H2 and must be dropped. */ |
| 4520 | if (isteq(list[hdr].n, ist("connection")) || |
| 4521 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4522 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4523 | isteq(list[hdr].n, ist("upgrade")) || |
| 4524 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4525 | continue; |
| 4526 | |
| 4527 | if (isteq(list[hdr].n, ist(""))) |
| 4528 | break; // end |
| 4529 | |
| 4530 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4531 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4532 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4533 | goto realign_again; |
| 4534 | goto full; |
| 4535 | } |
| 4536 | } |
| 4537 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4538 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4539 | * FIXME: we should also set it when we know for sure that the |
| 4540 | * content-length is zero as well as on 204/304 |
| 4541 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4542 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4543 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4544 | es_now = 1; |
| 4545 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4546 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4547 | es_now = 1; |
| 4548 | |
| 4549 | /* update the frame's size */ |
| 4550 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4551 | |
| 4552 | if (es_now) |
| 4553 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4554 | |
| 4555 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4556 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4557 | |
| 4558 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4559 | * 1xx responses, another HEADERS frame is expected. |
| 4560 | */ |
| 4561 | if (h2s->status >= 200 || h2s->status == 101) |
| 4562 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4563 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4564 | if (es_now) { |
| 4565 | h2s->flags |= H2_SF_ES_SENT; |
| 4566 | if (h2s->st == H2_SS_OPEN) |
| 4567 | h2s->st = H2_SS_HLOC; |
| 4568 | else |
| 4569 | h2s_close(h2s); |
| 4570 | } |
| 4571 | |
| 4572 | /* OK we could properly deliver the response */ |
| 4573 | |
| 4574 | /* remove all header blocks including the EOH and compute the |
| 4575 | * corresponding size. |
| 4576 | * |
| 4577 | * FIXME: We should remove everything when es_now is set. |
| 4578 | */ |
| 4579 | ret = 0; |
| 4580 | idx = htx_get_head(htx); |
| 4581 | blk = htx_get_blk(htx, idx); |
| 4582 | while (blk != blk_end) { |
| 4583 | ret += htx_get_blksz(blk); |
| 4584 | blk = htx_remove_blk(htx, blk); |
| 4585 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4586 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4587 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4588 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4589 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4590 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4591 | end: |
| 4592 | return ret; |
| 4593 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4594 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4595 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4596 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4597 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4598 | ret = 0; |
| 4599 | goto end; |
| 4600 | fail: |
| 4601 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4602 | * list etc go here (unrecoverable errors). |
| 4603 | */ |
| 4604 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4605 | ret = 0; |
| 4606 | goto end; |
| 4607 | } |
| 4608 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4609 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4610 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4611 | * must check the stream's status to detect any error which might have happened |
| 4612 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4613 | * from the message. The htx message is assumed to be valid since produced from |
| 4614 | * the internal code, hence it contains a start line, an optional series of |
| 4615 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4616 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4617 | */ |
| 4618 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4619 | { |
| 4620 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4621 | struct h2c *h2c = h2s->h2c; |
| 4622 | struct htx_blk *blk; |
| 4623 | struct htx_blk *blk_end; |
| 4624 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4625 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4626 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4627 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4628 | enum htx_blk_type type; |
| 4629 | int es_now = 0; |
| 4630 | int ret = 0; |
| 4631 | int hdr; |
| 4632 | int idx; |
| 4633 | |
| 4634 | if (h2c_mux_busy(h2c, h2s)) { |
| 4635 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4636 | return 0; |
| 4637 | } |
| 4638 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4639 | /* determine the first block which must not be deleted, blk_end may |
| 4640 | * be NULL if all blocks have to be deleted. |
| 4641 | */ |
| 4642 | idx = htx_get_head(htx); |
| 4643 | blk_end = NULL; |
| 4644 | while (idx != -1) { |
| 4645 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4646 | idx = htx_get_next(htx, idx); |
| 4647 | if (type == HTX_BLK_EOH) { |
| 4648 | if (idx != -1) |
| 4649 | blk_end = htx_get_blk(htx, idx); |
| 4650 | break; |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4655 | blk = htx_get_head_blk(htx); |
| 4656 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 4657 | ALREADY_CHECKED(blk); |
| 4658 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4659 | meth = htx_sl_req_meth(sl); |
| 4660 | path = htx_sl_req_uri(sl); |
| 4661 | |
| 4662 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4663 | hdr = 0; |
| 4664 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4665 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4666 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4667 | blk = htx_get_blk(htx, idx); |
| 4668 | type = htx_get_blk_type(blk); |
| 4669 | |
| 4670 | if (type == HTX_BLK_UNUSED) |
| 4671 | continue; |
| 4672 | |
| 4673 | if (type != HTX_BLK_HDR) |
| 4674 | break; |
| 4675 | |
| 4676 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4677 | goto fail; |
| 4678 | |
| 4679 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4680 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4681 | hdr++; |
| 4682 | } |
| 4683 | |
| 4684 | /* marker for end of headers */ |
| 4685 | list[hdr].n = ist(""); |
| 4686 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4687 | mbuf = br_tail(h2c->mbuf); |
| 4688 | retry: |
| 4689 | if (!h2_get_buf(h2c, mbuf)) { |
| 4690 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4691 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4692 | return 0; |
| 4693 | } |
| 4694 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4695 | chunk_reset(&outbuf); |
| 4696 | |
| 4697 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4698 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4699 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4700 | break; |
| 4701 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4702 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | if (outbuf.size < 9) |
| 4706 | goto full; |
| 4707 | |
| 4708 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4709 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4710 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4711 | outbuf.data = 9; |
| 4712 | |
| 4713 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4714 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4715 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4716 | goto realign_again; |
| 4717 | goto full; |
| 4718 | } |
| 4719 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4720 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4721 | * - :authority set to the URI part (host:port) |
| 4722 | * - :method set to CONNECT |
| 4723 | * - :scheme and :path omitted |
| 4724 | */ |
| 4725 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4726 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4727 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4728 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4729 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4730 | goto realign_again; |
| 4731 | goto full; |
| 4732 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4733 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4734 | /* encode the path, which necessarily is the second one */ |
| 4735 | if (!hpack_encode_path(&outbuf, path)) { |
| 4736 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4737 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4738 | goto realign_again; |
| 4739 | goto full; |
| 4740 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4741 | |
| 4742 | /* look for the Host header and place it in :authority */ |
| 4743 | auth = ist2(NULL, 0); |
| 4744 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4745 | if (isteq(list[hdr].n, ist(""))) |
| 4746 | break; // end |
| 4747 | |
| 4748 | if (isteq(list[hdr].n, ist("host"))) { |
| 4749 | auth = list[hdr].v; |
| 4750 | break; |
| 4751 | } |
| 4752 | } |
| 4753 | } |
| 4754 | else { |
| 4755 | /* for CONNECT, :authority is taken from the path */ |
| 4756 | auth = path; |
| 4757 | } |
| 4758 | |
| 4759 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4760 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4761 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4762 | goto realign_again; |
| 4763 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | /* encode all headers, stop at empty name */ |
| 4767 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4768 | /* these ones do not exist in H2 and must be dropped. */ |
| 4769 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4770 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4771 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4772 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4773 | isteq(list[hdr].n, ist("upgrade")) || |
| 4774 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4775 | continue; |
| 4776 | |
| 4777 | if (isteq(list[hdr].n, ist(""))) |
| 4778 | break; // end |
| 4779 | |
| 4780 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4781 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4782 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4783 | goto realign_again; |
| 4784 | goto full; |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | /* we may need to add END_STREAM if we have no body : |
| 4789 | * - request already closed, or : |
| 4790 | * - no transfer-encoding, and : |
| 4791 | * - no content-length or content-length:0 |
| 4792 | * Fixme: this doesn't take into account CONNECT requests. |
| 4793 | */ |
| 4794 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4795 | es_now = 1; |
| 4796 | |
| 4797 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4798 | es_now = 1; |
| 4799 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4800 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4801 | es_now = 1; |
| 4802 | |
| 4803 | /* update the frame's size */ |
| 4804 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4805 | |
| 4806 | if (es_now) |
| 4807 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4808 | |
| 4809 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4810 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4811 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4812 | h2s->st = H2_SS_OPEN; |
| 4813 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4814 | if (es_now) { |
| 4815 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4816 | h2s->flags |= H2_SF_ES_SENT; |
| 4817 | h2s->st = H2_SS_HLOC; |
| 4818 | } |
| 4819 | |
| 4820 | /* remove all header blocks including the EOH and compute the |
| 4821 | * corresponding size. |
| 4822 | * |
| 4823 | * FIXME: We should remove everything when es_now is set. |
| 4824 | */ |
| 4825 | ret = 0; |
| 4826 | idx = htx_get_head(htx); |
| 4827 | blk = htx_get_blk(htx, idx); |
| 4828 | while (blk != blk_end) { |
| 4829 | ret += htx_get_blksz(blk); |
| 4830 | blk = htx_remove_blk(htx, blk); |
| 4831 | } |
| 4832 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4833 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4834 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4835 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4836 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4837 | |
| 4838 | end: |
| 4839 | return ret; |
| 4840 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4841 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4842 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4843 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4844 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4845 | ret = 0; |
| 4846 | goto end; |
| 4847 | fail: |
| 4848 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4849 | * list etc go here (unrecoverable errors). |
| 4850 | */ |
| 4851 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4852 | ret = 0; |
| 4853 | goto end; |
| 4854 | } |
| 4855 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4856 | /* 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] | 4857 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4858 | * caller must check the stream's status to detect any error which might have |
| 4859 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4860 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4861 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4862 | 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] | 4863 | { |
| 4864 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4865 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4866 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4867 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4868 | size_t total = 0; |
| 4869 | int es_now = 0; |
| 4870 | int bsize; /* htx block size */ |
| 4871 | int fsize; /* h2 frame size */ |
| 4872 | struct htx_blk *blk; |
| 4873 | enum htx_blk_type type; |
| 4874 | int idx; |
| 4875 | |
| 4876 | if (h2c_mux_busy(h2c, h2s)) { |
| 4877 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4878 | goto end; |
| 4879 | } |
| 4880 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4881 | htx = htx_from_buf(buf); |
| 4882 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4883 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4884 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4885 | * the caller to handle. |
| 4886 | */ |
| 4887 | |
| 4888 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4889 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4890 | goto end; |
| 4891 | |
| 4892 | idx = htx_get_head(htx); |
| 4893 | blk = htx_get_blk(htx, idx); |
| 4894 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4895 | bsize = htx_get_blksz(blk); |
| 4896 | fsize = bsize; |
| 4897 | |
| 4898 | if (type == HTX_BLK_EOD) { |
| 4899 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4900 | * have a set of trailers after us that the caller will want to |
| 4901 | * deal with. Let's simply remove the EOD and return. |
| 4902 | */ |
| 4903 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4904 | total++; // EOD counts as one byte |
| 4905 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4906 | goto end; |
| 4907 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4908 | else if (type == HTX_BLK_EOM) { |
| 4909 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4910 | /* ES already sent */ |
| 4911 | htx_remove_blk(htx, blk); |
| 4912 | total++; // EOM counts as one byte |
| 4913 | count--; |
| 4914 | goto end; |
| 4915 | } |
| 4916 | } |
| 4917 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4918 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4919 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4920 | mbuf = br_tail(h2c->mbuf); |
| 4921 | retry: |
| 4922 | if (!h2_get_buf(h2c, mbuf)) { |
| 4923 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4924 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4925 | goto end; |
| 4926 | } |
| 4927 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4928 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4929 | * First, if the mux's buffer is empty and the htx area contains |
| 4930 | * exactly one data block of the same size as the requested count, and |
| 4931 | * this count fits within the frame size, the stream's window size, and |
| 4932 | * the connection's window size, then it's possible to simply swap the |
| 4933 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4934 | * length to match the entire DATA HTX block in the middle. In this |
| 4935 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4936 | * the situation that happens all the time with large files. Second, if |
| 4937 | * this is not possible, but the mux's output buffer is empty, we still |
| 4938 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4939 | * making the intermediary buffer's area point to the output buffer's |
| 4940 | * area. In this case we want to skip the HTX header to make sure that |
| 4941 | * copies remain aligned and that this operation remains possible all |
| 4942 | * the time. This goes for headers, data blocks and any data extracted |
| 4943 | * from the HTX blocks. |
| 4944 | */ |
| 4945 | if (unlikely(fsize == count && |
| 4946 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4947 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4948 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4949 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4950 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4951 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4952 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4953 | * frame into a buffer containing few data if it needs to be realigned, |
| 4954 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4955 | * 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] | 4956 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4957 | if (fsize + 9 <= b_room(mbuf) && |
| 4958 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 4959 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4960 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4961 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4962 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4963 | goto retry; |
| 4964 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4965 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4966 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4967 | goto end; |
| 4968 | } |
| 4969 | |
| 4970 | /* map an H2 frame to the HTX block so that we can put the |
| 4971 | * frame header there. |
| 4972 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4973 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 4974 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4975 | |
| 4976 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4977 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4978 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4979 | h2_set_frame_size(outbuf.area, fsize); |
| 4980 | |
| 4981 | /* update windows */ |
| 4982 | h2s->mws -= fsize; |
| 4983 | h2c->mws -= fsize; |
| 4984 | |
| 4985 | /* and exchange with our old area */ |
| 4986 | buf->area = old_area; |
| 4987 | buf->data = buf->head = 0; |
| 4988 | total += fsize; |
| 4989 | goto end; |
| 4990 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4991 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4992 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4993 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4994 | |
| 4995 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4996 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4997 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4998 | break; |
| 4999 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5000 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5001 | } |
| 5002 | |
| 5003 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5004 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5005 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5006 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5007 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5008 | goto end; |
| 5009 | } |
| 5010 | |
| 5011 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5012 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5013 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5014 | outbuf.data = 9; |
| 5015 | |
| 5016 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5017 | * the HTX buffer. We need to check this against the connection's and |
| 5018 | * the stream's send windows, and to ensure that this fits in the max |
| 5019 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5020 | * the frame header). The connection's flow control is applied last so |
| 5021 | * that we can use a separate list of streams which are immediately |
| 5022 | * unblocked on window opening. Note: we don't implement padding. |
| 5023 | */ |
| 5024 | |
| 5025 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5026 | * empty frame, thus we force it to zero here. |
| 5027 | */ |
| 5028 | if (type == HTX_BLK_EOM) |
| 5029 | bsize = fsize = 0; |
| 5030 | |
| 5031 | if (!fsize) |
| 5032 | goto send_empty; |
| 5033 | |
| 5034 | if (h2s->mws <= 0) { |
| 5035 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5036 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5037 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5038 | goto end; |
| 5039 | } |
| 5040 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5041 | if (fsize > count) |
| 5042 | fsize = count; |
| 5043 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5044 | if (fsize > h2s->mws) |
| 5045 | fsize = h2s->mws; // >0 |
| 5046 | |
| 5047 | if (h2c->mfs && fsize > h2c->mfs) |
| 5048 | fsize = h2c->mfs; // >0 |
| 5049 | |
| 5050 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5051 | /* It doesn't fit at once. If it at least fits once split and |
| 5052 | * the amount of data to move is low, let's defragment the |
| 5053 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5054 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5055 | if (b_space_wraps(mbuf) && |
| 5056 | (fsize + 9 <= b_room(mbuf)) && |
| 5057 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5058 | goto realign_again; |
| 5059 | fsize = outbuf.size - 9; |
| 5060 | |
| 5061 | if (fsize <= 0) { |
| 5062 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5063 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5064 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5065 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5066 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5067 | goto end; |
| 5068 | } |
| 5069 | } |
| 5070 | |
| 5071 | if (h2c->mws <= 0) { |
| 5072 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5073 | goto end; |
| 5074 | } |
| 5075 | |
| 5076 | if (fsize > h2c->mws) |
| 5077 | fsize = h2c->mws; |
| 5078 | |
| 5079 | /* now let's copy this this into the output buffer */ |
| 5080 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5081 | h2s->mws -= fsize; |
| 5082 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5083 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5084 | |
| 5085 | send_empty: |
| 5086 | /* update the frame's size */ |
| 5087 | h2_set_frame_size(outbuf.area, fsize); |
| 5088 | |
| 5089 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5090 | * meeting EOM. We should optimize this later. |
| 5091 | */ |
| 5092 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5093 | total++; // EOM counts as one byte |
| 5094 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5095 | es_now = 1; |
| 5096 | } |
| 5097 | |
| 5098 | if (es_now) |
| 5099 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5100 | |
| 5101 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5102 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5103 | |
| 5104 | /* consume incoming HTX block, including EOM */ |
| 5105 | total += fsize; |
| 5106 | if (fsize == bsize) { |
| 5107 | htx_remove_blk(htx, blk); |
| 5108 | if (fsize) |
| 5109 | goto new_frame; |
| 5110 | } else { |
| 5111 | /* we've truncated this block */ |
| 5112 | htx_cut_data_blk(htx, blk, fsize); |
| 5113 | } |
| 5114 | |
| 5115 | if (es_now) { |
| 5116 | if (h2s->st == H2_SS_OPEN) |
| 5117 | h2s->st = H2_SS_HLOC; |
| 5118 | else |
| 5119 | h2s_close(h2s); |
| 5120 | |
| 5121 | h2s->flags |= H2_SF_ES_SENT; |
| 5122 | } |
| 5123 | |
| 5124 | end: |
| 5125 | return total; |
| 5126 | } |
| 5127 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5128 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5129 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5130 | * processed. The caller must check the stream's status to detect any error |
| 5131 | * which might have happened subsequently to a successful send. The htx blocks |
| 5132 | * are automatically removed from the message. The htx message is assumed to be |
| 5133 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5134 | * 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] | 5135 | * as a single frame. The ES flag is always set. |
| 5136 | */ |
| 5137 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5138 | { |
| 5139 | struct http_hdr list[MAX_HTTP_HDR]; |
| 5140 | struct h2c *h2c = h2s->h2c; |
| 5141 | struct htx_blk *blk; |
| 5142 | struct htx_blk *blk_end; |
| 5143 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5144 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5145 | struct h1m h1m; |
| 5146 | enum htx_blk_type type; |
| 5147 | uint32_t size; |
| 5148 | int ret = 0; |
| 5149 | int hdr; |
| 5150 | int idx; |
| 5151 | void *start; |
| 5152 | |
| 5153 | if (h2c_mux_busy(h2c, h2s)) { |
| 5154 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5155 | goto end; |
| 5156 | } |
| 5157 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5158 | /* The principle is that we parse each and every trailers block using |
| 5159 | * the H1 headers parser, and append it to the list. We don't proceed |
| 5160 | * until EOM is met. blk_end will point to the EOM block. |
| 5161 | */ |
| 5162 | hdr = 0; |
| 5163 | memset(list, 0, sizeof(list)); |
| 5164 | blk_end = NULL; |
| 5165 | |
| 5166 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 5167 | blk = htx_get_blk(htx, idx); |
| 5168 | type = htx_get_blk_type(blk); |
| 5169 | |
| 5170 | if (type == HTX_BLK_UNUSED) |
| 5171 | continue; |
| 5172 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5173 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5174 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5175 | |
| 5176 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5177 | goto fail; |
| 5178 | |
| 5179 | size = htx_get_blksz(blk); |
| 5180 | start = htx_get_blk_ptr(htx, blk); |
| 5181 | |
| 5182 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 5183 | h1m.err_pos = 0; |
| 5184 | ret = h1_headers_to_hdr_list(start, start + size, |
| 5185 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 5186 | &h1m, NULL); |
| 5187 | if (ret < 0) |
| 5188 | goto fail; |
| 5189 | |
| 5190 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5191 | * empty line), or > 0 if it was found. We have to continue on |
| 5192 | * incomplete messages because the trailers block might be |
| 5193 | * incomplete. |
| 5194 | */ |
| 5195 | |
| 5196 | /* search the new end */ |
| 5197 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5198 | if (!list[hdr].n.len) |
| 5199 | break; |
| 5200 | hdr++; |
| 5201 | } |
| 5202 | } |
| 5203 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5204 | if (list[hdr].n.len != 0) |
| 5205 | goto fail; // empty trailer not found: internal error |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5206 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5207 | mbuf = br_tail(h2c->mbuf); |
| 5208 | retry: |
| 5209 | if (!h2_get_buf(h2c, mbuf)) { |
| 5210 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5211 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5212 | goto end; |
| 5213 | } |
| 5214 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5215 | chunk_reset(&outbuf); |
| 5216 | |
| 5217 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5218 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5219 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5220 | break; |
| 5221 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5222 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | if (outbuf.size < 9) |
| 5226 | goto full; |
| 5227 | |
| 5228 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5229 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5230 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5231 | outbuf.data = 9; |
| 5232 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5233 | /* encode all headers */ |
| 5234 | for (idx = 0; idx < hdr; idx++) { |
| 5235 | /* these ones do not exist in H2 or must not appear in |
| 5236 | * trailers and must be dropped. |
| 5237 | */ |
| 5238 | if (isteq(list[idx].n, ist("host")) || |
| 5239 | isteq(list[idx].n, ist("content-length")) || |
| 5240 | isteq(list[idx].n, ist("connection")) || |
| 5241 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5242 | isteq(list[idx].n, ist("keep-alive")) || |
| 5243 | isteq(list[idx].n, ist("upgrade")) || |
| 5244 | isteq(list[idx].n, ist("te")) || |
| 5245 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5246 | continue; |
| 5247 | |
| 5248 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5249 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5250 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5251 | goto realign_again; |
| 5252 | goto full; |
| 5253 | } |
| 5254 | } |
| 5255 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5256 | if (outbuf.data == 9) { |
| 5257 | /* here we have a problem, we have nothing to emit (either we |
| 5258 | * received an empty trailers block followed or we removed its |
| 5259 | * contents above). Because of this we can't send a HEADERS |
| 5260 | * frame, so we have to cheat and instead send an empty DATA |
| 5261 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5262 | */ |
| 5263 | outbuf.area[3] = H2_FT_DATA; |
| 5264 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5265 | } |
| 5266 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5267 | /* update the frame's size */ |
| 5268 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5269 | |
| 5270 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5271 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5272 | h2s->flags |= H2_SF_ES_SENT; |
| 5273 | |
| 5274 | if (h2s->st == H2_SS_OPEN) |
| 5275 | h2s->st = H2_SS_HLOC; |
| 5276 | else |
| 5277 | h2s_close(h2s); |
| 5278 | |
| 5279 | /* OK we could properly deliver the response */ |
| 5280 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5281 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5282 | ret = 0; |
| 5283 | idx = htx_get_head(htx); |
| 5284 | blk = htx_get_blk(htx, idx); |
| 5285 | while (blk != blk_end) { |
| 5286 | ret += htx_get_blksz(blk); |
| 5287 | blk = htx_remove_blk(htx, blk); |
| 5288 | } |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5289 | end: |
| 5290 | return ret; |
| 5291 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5292 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5293 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5294 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5295 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5296 | ret = 0; |
| 5297 | goto end; |
| 5298 | fail: |
| 5299 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5300 | * list etc go here (unrecoverable errors). |
| 5301 | */ |
| 5302 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5303 | ret = 0; |
| 5304 | goto end; |
| 5305 | } |
| 5306 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5307 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5308 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5309 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5310 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5311 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5312 | * returns 0 except for the error above. |
| 5313 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5314 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5315 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5316 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5317 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5318 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5319 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5320 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5321 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5322 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5323 | sw->events |= SUB_RETRY_RECV; |
| 5324 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5325 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5326 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5327 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5328 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5329 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5330 | sw->events |= SUB_RETRY_SEND; |
| 5331 | h2s->send_wait = sw; |
| 5332 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5333 | !LIST_ADDED(&h2s->list)) { |
| 5334 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5335 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5336 | else |
| 5337 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5338 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5339 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5340 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5341 | if (event_type != 0) |
| 5342 | return -1; |
| 5343 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5344 | } |
| 5345 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5346 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5347 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5348 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5349 | * It always returns zero. |
| 5350 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5351 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5352 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5353 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5354 | struct h2s *h2s = cs->ctx; |
| 5355 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5356 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5357 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5358 | BUG_ON(h2s->recv_wait != sw); |
| 5359 | sw->events &= ~SUB_RETRY_RECV; |
| 5360 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5361 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5362 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5363 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5364 | BUG_ON(h2s->send_wait != sw); |
| 5365 | LIST_DEL(&h2s->list); |
| 5366 | LIST_INIT(&h2s->list); |
| 5367 | sw->events &= ~SUB_RETRY_SEND; |
| 5368 | /* We were about to send, make sure it does not happen */ |
| 5369 | if (LIST_ADDED(&h2s->sending_list) && |
| 5370 | h2s->send_wait != &h2s->wait_event) { |
| 5371 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 5372 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5373 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5374 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5375 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5376 | return 0; |
| 5377 | } |
| 5378 | |
| 5379 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5380 | /* Called from the upper layer, to receive data */ |
| 5381 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5382 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5383 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5384 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5385 | struct htx *h2s_htx = NULL; |
| 5386 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5387 | size_t ret = 0; |
| 5388 | |
| 5389 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5390 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5391 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5392 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5393 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5394 | * the HTX is empty. |
| 5395 | */ |
| 5396 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5397 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5398 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5399 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5400 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5401 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5402 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5403 | /* <buf> is empty and the message is small enough, swap the |
| 5404 | * buffers. */ |
| 5405 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5406 | htx_to_buf(buf_htx, buf); |
| 5407 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5408 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5409 | goto end; |
| 5410 | } |
| 5411 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5412 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5413 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5414 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5415 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5416 | if (htx_is_empty(buf_htx)) |
| 5417 | cs->flags |= CS_FL_EOI; |
| 5418 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5419 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5420 | 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] | 5421 | htx_to_buf(buf_htx, buf); |
| 5422 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5423 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5424 | } |
| 5425 | else { |
| 5426 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5427 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5428 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5429 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5430 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5431 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5432 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5433 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5434 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5435 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 5436 | if (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5437 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5438 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5439 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5440 | if (b_size(&h2s->rxbuf)) { |
| 5441 | b_free(&h2s->rxbuf); |
| 5442 | offer_buffers(NULL, tasks_run_queue); |
| 5443 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5444 | } |
| 5445 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5446 | if (ret && h2c->dsi == h2s->id) { |
| 5447 | /* demux is blocking on this stream's buffer */ |
| 5448 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5449 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5450 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5451 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5452 | return ret; |
| 5453 | } |
| 5454 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5455 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5456 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5457 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5458 | static void h2_stop_senders(struct h2c *h2c) |
| 5459 | { |
| 5460 | struct h2s *h2s, *h2s_back; |
| 5461 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5462 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5463 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5464 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5465 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5466 | } |
| 5467 | } |
| 5468 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5469 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5470 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5471 | * flags may be updated on the conn_stream. |
| 5472 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5473 | 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] | 5474 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5475 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5476 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5477 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5478 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5479 | struct htx *htx; |
| 5480 | struct htx_blk *blk; |
| 5481 | enum htx_blk_type btype; |
| 5482 | uint32_t bsize; |
| 5483 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5484 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5485 | /* If we were not just woken because we wanted to send but couldn't, |
| 5486 | * and there's somebody else that is waiting to send, do nothing, |
| 5487 | * we will subscribe later and be put at the end of the list |
| 5488 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5489 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5490 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5491 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5492 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5493 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5494 | /* We couldn't set it to NULL before, because we needed it in case |
| 5495 | * we had to cancel the tasklet |
| 5496 | */ |
| 5497 | h2s->send_wait = NULL; |
| 5498 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5499 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5500 | return 0; |
| 5501 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5502 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5503 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5504 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5505 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5506 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5507 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5508 | if (h2s->id == 0) { |
| 5509 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5510 | |
| 5511 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5512 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5513 | return 0; |
| 5514 | } |
| 5515 | |
| 5516 | eb32_delete(&h2s->by_id); |
| 5517 | h2s->by_id.key = h2s->id = id; |
| 5518 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5519 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5520 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5521 | } |
| 5522 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5523 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5524 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5525 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5526 | idx = htx_get_head(htx); |
| 5527 | blk = htx_get_blk(htx, idx); |
| 5528 | btype = htx_get_blk_type(blk); |
| 5529 | bsize = htx_get_blksz(blk); |
| 5530 | |
| 5531 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5532 | case HTX_BLK_REQ_SL: |
| 5533 | /* start-line before headers */ |
| 5534 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5535 | if (ret > 0) { |
| 5536 | total += ret; |
| 5537 | count -= ret; |
| 5538 | if (ret < bsize) |
| 5539 | goto done; |
| 5540 | } |
| 5541 | break; |
| 5542 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5543 | case HTX_BLK_RES_SL: |
| 5544 | /* start-line before headers */ |
| 5545 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5546 | if (ret > 0) { |
| 5547 | total += ret; |
| 5548 | count -= ret; |
| 5549 | if (ret < bsize) |
| 5550 | goto done; |
| 5551 | } |
| 5552 | break; |
| 5553 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5554 | case HTX_BLK_DATA: |
| 5555 | case HTX_BLK_EOD: |
| 5556 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5557 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5558 | * This EOM necessarily is one before trailers, as the EOM following |
| 5559 | * trailers would have been consumed by the trailers parser. |
| 5560 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5561 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5562 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5563 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5564 | total += ret; |
| 5565 | count -= ret; |
| 5566 | if (ret < bsize) |
| 5567 | goto done; |
| 5568 | } |
| 5569 | break; |
| 5570 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5571 | case HTX_BLK_TLR: |
| 5572 | /* This is the first trailers block, all the subsequent ones AND |
| 5573 | * the EOM will be swallowed by the parser. |
| 5574 | */ |
| 5575 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5576 | if (ret > 0) { |
| 5577 | total += ret; |
| 5578 | count -= ret; |
| 5579 | if (ret < bsize) |
| 5580 | goto done; |
| 5581 | } |
| 5582 | break; |
| 5583 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5584 | default: |
| 5585 | htx_remove_blk(htx, blk); |
| 5586 | total += bsize; |
| 5587 | count -= bsize; |
| 5588 | break; |
| 5589 | } |
| 5590 | } |
| 5591 | goto done; |
| 5592 | } |
| 5593 | |
| 5594 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5595 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5596 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5597 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5598 | ret = -1; |
| 5599 | else |
| 5600 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5601 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5602 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5603 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5604 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5605 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5606 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5607 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5608 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5609 | if (unlikely((int)ret <= 0)) { |
| 5610 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5611 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5612 | break; |
| 5613 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5614 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5615 | total += count; |
| 5616 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5617 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5618 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5619 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5620 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5621 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5622 | break; |
| 5623 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5624 | |
| 5625 | total += ret; |
| 5626 | count -= ret; |
| 5627 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5628 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5629 | break; |
| 5630 | |
| 5631 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5632 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5633 | } |
| 5634 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5635 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5636 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5637 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5638 | * unprocessed trailers, abnormal extra data, ...) |
| 5639 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5640 | total += count; |
| 5641 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5642 | } |
| 5643 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5644 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5645 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5646 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5647 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5648 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5649 | } |
| 5650 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5651 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5652 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5653 | } else { |
| 5654 | b_del(buf, total); |
| 5655 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5656 | |
| 5657 | /* The mux is full, cancel the pending tasks */ |
| 5658 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5659 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5660 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5661 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5662 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5663 | * we read exactly what the caller specified, as with HTX the caller |
| 5664 | * will always give the buffer size, instead of the amount of data |
| 5665 | * available. |
| 5666 | */ |
| 5667 | if (htx && !b_data(buf)) |
| 5668 | total = orig_count; |
| 5669 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5670 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5671 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5672 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5673 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5674 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5675 | /* If we're waiting for flow control, and we got a shutr on the |
| 5676 | * connection, we will never be unlocked, so add an error on |
| 5677 | * the conn_stream. |
| 5678 | */ |
| 5679 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5680 | !b_data(&h2s->h2c->dbuf) && |
| 5681 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5682 | if (cs->flags & CS_FL_EOS) |
| 5683 | cs->flags |= CS_FL_ERROR; |
| 5684 | else |
| 5685 | cs->flags |= CS_FL_ERR_PENDING; |
| 5686 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5687 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5688 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5689 | LIST_DEL_INIT(&h2s->list); |
| 5690 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5691 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5692 | } |
| 5693 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5694 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5695 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5696 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5697 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5698 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5699 | struct eb32_node *node; |
| 5700 | int fctl_cnt = 0; |
| 5701 | int send_cnt = 0; |
| 5702 | int tree_cnt = 0; |
| 5703 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5704 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5705 | |
| 5706 | if (!h2c) |
| 5707 | return; |
| 5708 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5709 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5710 | fctl_cnt++; |
| 5711 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5712 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5713 | send_cnt++; |
| 5714 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5715 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5716 | node = eb32_first(&h2c->streams_by_id); |
| 5717 | while (node) { |
| 5718 | h2s = container_of(node, struct h2s, by_id); |
| 5719 | tree_cnt++; |
| 5720 | if (!h2s->cs) |
| 5721 | orph_cnt++; |
| 5722 | node = eb32_next(node); |
| 5723 | } |
| 5724 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5725 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5726 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5727 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5728 | " .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] | 5729 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5730 | " .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] | 5731 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5732 | 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] | 5733 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5734 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5735 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5736 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5737 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5738 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5739 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5740 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5741 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5742 | |
| 5743 | if (h2s) { |
| 5744 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5745 | h2s, h2s->id, h2s->flags, |
| 5746 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5747 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5748 | h2s->cs); |
| 5749 | if (h2s->cs) |
| 5750 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5751 | h2s->cs->flags, h2s->cs->data); |
| 5752 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5753 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5754 | |
| 5755 | /*******************************************************/ |
| 5756 | /* functions below are dedicated to the config parsers */ |
| 5757 | /*******************************************************/ |
| 5758 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5759 | /* config parser for global "tune.h2.header-table-size" */ |
| 5760 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5761 | struct proxy *defpx, const char *file, int line, |
| 5762 | char **err) |
| 5763 | { |
| 5764 | if (too_many_args(1, args, err, NULL)) |
| 5765 | return -1; |
| 5766 | |
| 5767 | h2_settings_header_table_size = atoi(args[1]); |
| 5768 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5769 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5770 | return -1; |
| 5771 | } |
| 5772 | return 0; |
| 5773 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5774 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5775 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5776 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5777 | struct proxy *defpx, const char *file, int line, |
| 5778 | char **err) |
| 5779 | { |
| 5780 | if (too_many_args(1, args, err, NULL)) |
| 5781 | return -1; |
| 5782 | |
| 5783 | h2_settings_initial_window_size = atoi(args[1]); |
| 5784 | if (h2_settings_initial_window_size < 0) { |
| 5785 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5786 | return -1; |
| 5787 | } |
| 5788 | return 0; |
| 5789 | } |
| 5790 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5791 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5792 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5793 | struct proxy *defpx, const char *file, int line, |
| 5794 | char **err) |
| 5795 | { |
| 5796 | if (too_many_args(1, args, err, NULL)) |
| 5797 | return -1; |
| 5798 | |
| 5799 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5800 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5801 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5802 | return -1; |
| 5803 | } |
| 5804 | return 0; |
| 5805 | } |
| 5806 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5807 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5808 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5809 | struct proxy *defpx, const char *file, int line, |
| 5810 | char **err) |
| 5811 | { |
| 5812 | if (too_many_args(1, args, err, NULL)) |
| 5813 | return -1; |
| 5814 | |
| 5815 | h2_settings_max_frame_size = atoi(args[1]); |
| 5816 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5817 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5818 | return -1; |
| 5819 | } |
| 5820 | return 0; |
| 5821 | } |
| 5822 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5823 | |
| 5824 | /****************************************/ |
| 5825 | /* MUX initialization and instanciation */ |
| 5826 | /***************************************/ |
| 5827 | |
| 5828 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5829 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5830 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5831 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5832 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5833 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5834 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5835 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5836 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5837 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5838 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5839 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5840 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5841 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5842 | .shutr = h2_shutr, |
| 5843 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5844 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5845 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5846 | .name = "H2", |
| 5847 | }; |
| 5848 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5849 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5850 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5851 | { .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] | 5852 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5853 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5854 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5855 | |
| 5856 | /* The mux operations */ |
| 5857 | static const struct mux_ops h2_htx_ops = { |
| 5858 | .init = h2_init, |
| 5859 | .wake = h2_wake, |
| 5860 | .snd_buf = h2_snd_buf, |
| 5861 | .rcv_buf = h2_rcv_buf, |
| 5862 | .subscribe = h2_subscribe, |
| 5863 | .unsubscribe = h2_unsubscribe, |
| 5864 | .attach = h2_attach, |
| 5865 | .get_first_cs = h2_get_first_cs, |
| 5866 | .detach = h2_detach, |
| 5867 | .destroy = h2_destroy, |
| 5868 | .avail_streams = h2_avail_streams, |
| 5869 | .used_streams = h2_used_streams, |
| 5870 | .shutr = h2_shutr, |
| 5871 | .shutw = h2_shutw, |
| 5872 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5873 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5874 | .name = "H2", |
| 5875 | }; |
| 5876 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5877 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5878 | { .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] | 5879 | |
| 5880 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5881 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5882 | /* config keyword parsers */ |
| 5883 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5884 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5885 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5886 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5887 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5888 | { 0, NULL, NULL } |
| 5889 | }}; |
| 5890 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5891 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |