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 */ |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 133 | struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 134 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 135 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 136 | 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] | 137 | }; |
| 138 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 139 | /* H2 stream state, in h2s->st */ |
| 140 | enum h2_ss { |
| 141 | H2_SS_IDLE = 0, // idle |
| 142 | H2_SS_RLOC, // reserved(local) |
| 143 | H2_SS_RREM, // reserved(remote) |
| 144 | H2_SS_OPEN, // open |
| 145 | H2_SS_HREM, // half-closed(remote) |
| 146 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 147 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 148 | H2_SS_CLOSED, // closed |
| 149 | H2_SS_ENTRIES // must be last |
| 150 | } __attribute__((packed)); |
| 151 | |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 152 | #define H2_SS_MASK(state) (1UL << (state)) |
| 153 | #define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE) |
| 154 | #define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC) |
| 155 | #define H2_SS_RREM_BIT (1UL << H2_SS_RREM) |
| 156 | #define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN) |
| 157 | #define H2_SS_HREM_BIT (1UL << H2_SS_HREM) |
| 158 | #define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC) |
| 159 | #define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR) |
| 160 | #define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED) |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 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) |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 172 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list) |
| 173 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list) |
| 174 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl (must be in blocked list) |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 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_* */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 212 | int sws; /* stream window size, to be added to the mux's initial window size */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 306 | |
| 307 | /* Detect a pending read0 for a H2 connection. It happens if a read0 is pending |
| 308 | * on the connection AND if there is no more data in the demux buffer. The |
| 309 | * function returns 1 to report a read0 or 0 otherwise. |
| 310 | */ |
| 311 | static int h2c_read0_pending(struct h2c *h2c) |
| 312 | { |
| 313 | if (conn_xprt_read0_pending(h2c->conn) && !b_data(&h2c->dbuf)) |
| 314 | return 1; |
| 315 | return 0; |
| 316 | } |
| 317 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 318 | /* returns true if the connection is allowed to expire, false otherwise. A |
| 319 | * connection may expire when: |
| 320 | * - it has no stream |
| 321 | * - it has data in the mux buffer |
| 322 | * - it has streams in the blocked list |
| 323 | * - it has streams in the fctl list |
| 324 | * - it has streams in the send list |
| 325 | * Otherwise it means some streams are waiting in the data layer and it should |
| 326 | * not expire. |
| 327 | */ |
| 328 | static inline int h2c_may_expire(const struct h2c *h2c) |
| 329 | { |
| 330 | return eb_is_empty(&h2c->streams_by_id) || |
| 331 | br_data(h2c->mbuf) || |
| 332 | !LIST_ISEMPTY(&h2c->blocked_list) || |
| 333 | !LIST_ISEMPTY(&h2c->fctl_list) || |
| 334 | !LIST_ISEMPTY(&h2c->send_list) || |
| 335 | !LIST_ISEMPTY(&h2c->sending_list); |
| 336 | } |
| 337 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 338 | static __inline int |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 339 | h2c_is_dead(const struct h2c *h2c) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 340 | { |
| 341 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 342 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 343 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 344 | (!(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] | 345 | (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 346 | (conn_xprt_read0_pending(h2c->conn) || |
| 347 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 348 | return 1; |
| 349 | |
| 350 | return 0; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 353 | /*****************************************************/ |
| 354 | /* functions below are for dynamic buffer management */ |
| 355 | /*****************************************************/ |
| 356 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 357 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 358 | * to receive data into the buffer and/or demux pending data. The condition is |
| 359 | * a bit complex due to some API limits for now. The rules are the following : |
| 360 | * - if an error or a shutdown was detected on the connection and the buffer |
| 361 | * is empty, we must not attempt to receive |
| 362 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 363 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 364 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 365 | * regardless of whether the demux buffer is full or not, so that only |
| 366 | * de demux part decides whether or not to block. This is needed because |
| 367 | * the connection API indeed prevents us from re-enabling receipt that is |
| 368 | * already enabled in a polled state, so we must always immediately stop |
| 369 | * as soon as the demux can't proceed so as never to hit an end of read |
| 370 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 371 | * - otherwise must may not attempt |
| 372 | */ |
| 373 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 374 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 375 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 376 | (h2c->st0 >= H2_CS_ERROR || |
| 377 | h2c->conn->flags & CO_FL_ERROR || |
| 378 | conn_xprt_read0_pending(h2c->conn))) |
| 379 | return 0; |
| 380 | |
| 381 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 382 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 383 | return 1; |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 388 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 389 | 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] | 390 | { |
| 391 | if (!h2_recv_allowed(h2c)) |
| 392 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 393 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 394 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 395 | return; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 396 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 400 | /* returns true if the front connection has too many conn_streams attached */ |
| 401 | 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] | 402 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 403 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 406 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 407 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 408 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 409 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 410 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 411 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 412 | { |
| 413 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 414 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 415 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 416 | 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] | 417 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 418 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 419 | return 1; |
| 420 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 421 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 422 | 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] | 423 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 424 | |
| 425 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 426 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 427 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 428 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 429 | return 1; |
| 430 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 431 | |
| 432 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 433 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 434 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 435 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 436 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 437 | return 1; |
| 438 | } |
| 439 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 443 | 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] | 444 | { |
| 445 | struct buffer *buf = NULL; |
| 446 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 447 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 448 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 449 | h2c->buf_wait.target = h2c; |
| 450 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 451 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 452 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 453 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 454 | __conn_xprt_stop_recv(h2c->conn); |
| 455 | } |
| 456 | return buf; |
| 457 | } |
| 458 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 459 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 460 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 461 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 462 | b_free(bptr); |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 463 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 467 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 468 | { |
| 469 | struct buffer *buf; |
| 470 | unsigned int count = 0; |
| 471 | |
| 472 | while (b_size(buf = br_head_pick(h2c->mbuf))) { |
| 473 | b_free(buf); |
| 474 | count++; |
| 475 | } |
| 476 | if (count) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 477 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 480 | /* returns the number of allocatable outgoing streams for the connection taking |
| 481 | * the last_sid and the reserved ones into account. |
| 482 | */ |
| 483 | static inline int h2_streams_left(const struct h2c *h2c) |
| 484 | { |
| 485 | int ret; |
| 486 | |
| 487 | /* consider the number of outgoing streams we're allowed to create before |
| 488 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 489 | * nb_reserved is the number of streams which don't yet have an ID. |
| 490 | */ |
| 491 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 492 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 493 | if (ret < 0) |
| 494 | ret = 0; |
| 495 | return ret; |
| 496 | } |
| 497 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 498 | /* returns the number of streams in use on a connection to figure if it's |
| 499 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 500 | * to know if it was the last one after a detach(). |
| 501 | */ |
| 502 | static int h2_used_streams(struct connection *conn) |
| 503 | { |
| 504 | struct h2c *h2c = conn->ctx; |
| 505 | |
| 506 | return h2c->nb_cs; |
| 507 | } |
| 508 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 509 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 510 | static int h2_avail_streams(struct connection *conn) |
| 511 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 512 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 513 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 514 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 515 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 516 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 517 | * streams on the connection. |
| 518 | */ |
| 519 | if (h2c->last_sid >= 0) |
| 520 | return 0; |
| 521 | |
Willy Tarreau | d63b85d | 2019-10-31 15:10:03 +0100 | [diff] [blame] | 522 | if (h2c->st0 >= H2_CS_ERROR) |
| 523 | return 0; |
| 524 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 525 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 526 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 527 | |
| 528 | /* we must also consider the limit imposed by stream IDs */ |
| 529 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 530 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 531 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 532 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 533 | ret1 = MIN(ret1, ret2); |
| 534 | } |
| 535 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 538 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 539 | /*****************************************************************/ |
| 540 | /* functions below are dedicated to the mux setup and management */ |
| 541 | /*****************************************************************/ |
| 542 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 543 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 544 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 545 | * connections from the fact that the context is still NULL (even during mux |
| 546 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 547 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 548 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 549 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 550 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 551 | { |
| 552 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 553 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 554 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 555 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 556 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 557 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 558 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 559 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 560 | h2c->flags = H2_CF_IS_BACK; |
| 561 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 562 | if (tick_isset(prx->timeout.serverfin)) |
| 563 | h2c->shut_timeout = prx->timeout.serverfin; |
| 564 | } else { |
| 565 | h2c->flags = H2_CF_NONE; |
| 566 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 567 | if (tick_isset(prx->timeout.clientfin)) |
| 568 | h2c->shut_timeout = prx->timeout.clientfin; |
| 569 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 570 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 571 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 572 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 573 | if (tick_isset(h2c->timeout)) { |
| 574 | t = task_new(tid_bit); |
| 575 | if (!t) |
| 576 | goto fail; |
| 577 | |
| 578 | h2c->task = t; |
| 579 | t->process = h2_timeout_task; |
| 580 | t->context = h2c; |
| 581 | t->expire = tick_add(now_ms, h2c->timeout); |
| 582 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 583 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 584 | h2c->wait_event.tasklet = tasklet_new(); |
| 585 | if (!h2c->wait_event.tasklet) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 586 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 587 | h2c->wait_event.tasklet->process = h2_io_cb; |
| 588 | h2c->wait_event.tasklet->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 589 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 590 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 591 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 592 | if (!h2c->ddht) |
| 593 | goto fail; |
| 594 | |
| 595 | /* Initialise the context. */ |
| 596 | h2c->st0 = H2_CS_PREFACE; |
| 597 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 598 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 599 | h2c->max_id = -1; |
| 600 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 601 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 602 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 603 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 604 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 605 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 606 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 607 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 608 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 609 | h2c->dsi = -1; |
| 610 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 611 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 612 | h2c->last_sid = -1; |
| 613 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 614 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 615 | h2c->miw = 65535; /* mux initial window size */ |
| 616 | h2c->mws = 65535; /* mux window size */ |
| 617 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 618 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 619 | LIST_INIT(&h2c->send_list); |
| 620 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 621 | LIST_INIT(&h2c->blocked_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 622 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 623 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 624 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 625 | if (t) |
| 626 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 627 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 628 | if (h2c->flags & H2_CF_IS_BACK) { |
| 629 | /* FIXME: this is temporary, for outgoing connections we need |
| 630 | * to immediately allocate a stream until the code is modified |
| 631 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 632 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 633 | */ |
| 634 | struct h2s *h2s; |
| 635 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 636 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 637 | if (!h2s) |
| 638 | goto fail_stream; |
| 639 | } |
| 640 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 641 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 642 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 643 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 644 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 645 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 646 | fail_stream: |
| 647 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 648 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 649 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 650 | if (h2c->wait_event.tasklet) |
| 651 | tasklet_free(h2c->wait_event.tasklet); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 652 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 653 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 654 | return -1; |
| 655 | } |
| 656 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 657 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 658 | * -1 if no more is allocatable. |
| 659 | */ |
| 660 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 661 | { |
| 662 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 663 | |
| 664 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 665 | id = -1; |
| 666 | return id; |
| 667 | } |
| 668 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 669 | /* returns the stream associated with id <id> or NULL if not found */ |
| 670 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 671 | { |
| 672 | struct eb32_node *node; |
| 673 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 674 | if (id == 0) |
| 675 | return (struct h2s *)h2_closed_stream; |
| 676 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 677 | if (id > h2c->max_id) |
| 678 | return (struct h2s *)h2_idle_stream; |
| 679 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 680 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 681 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 682 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 683 | |
| 684 | return container_of(node, struct h2s, by_id); |
| 685 | } |
| 686 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 687 | /* release function. This one should be called to free all resources allocated |
| 688 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 689 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 690 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 691 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 692 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 693 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 694 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 695 | /* The connection must be aattached to this mux to be released */ |
| 696 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 697 | conn = h2c->conn; |
| 698 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 699 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 700 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 701 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 702 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 703 | LIST_DEL(&h2c->buf_wait.list); |
| 704 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 705 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 706 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 707 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 708 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 709 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 710 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 711 | h2c->task->context = NULL; |
| 712 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 713 | h2c->task = NULL; |
| 714 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 715 | if (h2c->wait_event.tasklet) |
| 716 | tasklet_free(h2c->wait_event.tasklet); |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 717 | if (conn && h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 718 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 719 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 720 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 721 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 724 | if (conn) { |
| 725 | conn->mux = NULL; |
| 726 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 727 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 728 | conn_stop_tracking(conn); |
| 729 | conn_full_close(conn); |
| 730 | if (conn->destroy_cb) |
| 731 | conn->destroy_cb(conn); |
| 732 | conn_free(conn); |
| 733 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 737 | /******************************************************/ |
| 738 | /* functions below are for the H2 protocol processing */ |
| 739 | /******************************************************/ |
| 740 | |
| 741 | /* 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] | 742 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 743 | { |
| 744 | return h2s ? h2s->id : 0; |
| 745 | } |
| 746 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 747 | /* returns the sum of the stream's own window size and the mux's initial |
| 748 | * window, which together form the stream's effective window size. |
| 749 | */ |
| 750 | static inline int h2s_mws(const struct h2s *h2s) |
| 751 | { |
| 752 | return h2s->sws + h2s->h2c->miw; |
| 753 | } |
| 754 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 755 | /* 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] | 756 | 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] | 757 | { |
| 758 | if (h2c->msi < 0) |
| 759 | return 0; |
| 760 | |
| 761 | if (h2c->msi == h2s_id(h2s)) |
| 762 | return 0; |
| 763 | |
| 764 | return 1; |
| 765 | } |
| 766 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 767 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 768 | 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] | 769 | { |
| 770 | h2c->errcode = err; |
| 771 | h2c->st0 = H2_CS_ERROR; |
| 772 | } |
| 773 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 774 | /* marks an error on the stream. It may also update an already closed stream |
| 775 | * (e.g. to report an error after an RST was received). |
| 776 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 777 | 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] | 778 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 779 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 780 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 781 | if (h2s->st < H2_SS_ERROR) |
| 782 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 783 | if (h2s->cs) |
| 784 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 788 | /* attempt to notify the data layer of recv availability */ |
| 789 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 790 | { |
| 791 | struct wait_event *sw; |
| 792 | |
| 793 | if (h2s->recv_wait) { |
| 794 | sw = h2s->recv_wait; |
| 795 | sw->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 796 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 797 | h2s->recv_wait = NULL; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* attempt to notify the data layer of send availability */ |
| 802 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 803 | { |
| 804 | struct wait_event *sw; |
| 805 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 806 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 807 | sw = h2s->send_wait; |
| 808 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 809 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 810 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 814 | /* alerts the data layer, trying to wake it up by all means, following |
| 815 | * this sequence : |
| 816 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 817 | * - if its subscribed to send, then it's woken up for send |
| 818 | * - if it was subscribed to neither, its ->wake() callback is called |
| 819 | * It is safe to call this function with a closed stream which doesn't have a |
| 820 | * conn_stream anymore. |
| 821 | */ |
| 822 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 823 | { |
| 824 | if (h2s->recv_wait || h2s->send_wait) { |
| 825 | h2s_notify_recv(h2s); |
| 826 | h2s_notify_send(h2s); |
| 827 | } |
| 828 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 829 | h2s->cs->data_cb->wake(h2s->cs); |
| 830 | } |
| 831 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 832 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 833 | 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] | 834 | { |
| 835 | uint8_t *out = frame; |
| 836 | |
| 837 | *out = len >> 16; |
| 838 | write_n16(out + 1, len); |
| 839 | } |
| 840 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 841 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 842 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 843 | * 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] | 844 | * available in the buffer's input prior to calling this function. The buffer |
| 845 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 846 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 847 | 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] | 848 | const struct buffer *b, int o) |
| 849 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 850 | 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] | 851 | } |
| 852 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 853 | 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] | 854 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 855 | 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] | 856 | } |
| 857 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 858 | 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] | 859 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 860 | 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] | 861 | } |
| 862 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 863 | 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] | 864 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 865 | 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] | 866 | } |
| 867 | |
| 868 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 869 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 870 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 871 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 872 | * 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] | 873 | * |
| 874 | * b0 b1 b2 b3 b4 b5..b8 |
| 875 | * +----------+---------+--------+----+----+----------------------+ |
| 876 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 877 | * +----------+---------+--------+----+----+----------------------+ |
| 878 | * |
| 879 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 880 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 881 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 882 | * 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] | 883 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 884 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 885 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 886 | 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] | 887 | { |
| 888 | uint64_t w; |
| 889 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 890 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 891 | return 0; |
| 892 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 893 | w = h2_get_n64(b, o + 1); |
| 894 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 895 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 896 | h->ff = w >> 32; |
| 897 | h->ft = w >> 40; |
| 898 | h->len += w >> 48; |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 903 | * h2_peek_frame_hdr() above. |
| 904 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 905 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 906 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 907 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 911 | 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] | 912 | { |
| 913 | int ret; |
| 914 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 915 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 916 | if (ret > 0) |
| 917 | h2_skip_frame_hdr(b); |
| 918 | return ret; |
| 919 | } |
| 920 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 921 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 922 | * its connection if the stream was not yet closed. Please use this exclusively |
| 923 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 924 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 925 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 926 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 927 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 928 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 929 | if (!h2s->id) |
| 930 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 931 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 932 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 933 | h2s_notify_recv(h2s); |
| 934 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 935 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 936 | h2s->st = H2_SS_CLOSED; |
| 937 | } |
| 938 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 939 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 940 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 941 | { |
| 942 | h2s_close(h2s); |
| 943 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 944 | if (b_size(&h2s->rxbuf)) { |
| 945 | b_free(&h2s->rxbuf); |
| 946 | offer_buffers(NULL, tasks_run_queue); |
| 947 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 948 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 949 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 950 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 951 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 952 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 953 | * reference left would be in the h2c send_list/fctl_list, and if |
| 954 | * we're in it, we're getting out anyway |
| 955 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 956 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 957 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 958 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 959 | LIST_DEL_INIT(&h2s->sending_list); |
| 960 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 961 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 962 | pool_free(pool_head_h2s, h2s); |
| 963 | } |
| 964 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 965 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 966 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 967 | * causes of errors can be any failed memory allocation. The caller is |
| 968 | * responsible for checking if the connection may support an extra stream |
| 969 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 970 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 971 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 972 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 973 | struct h2s *h2s; |
| 974 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 975 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 976 | if (!h2s) |
| 977 | goto out; |
| 978 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 979 | h2s->wait_event.tasklet = tasklet_new(); |
| 980 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 981 | pool_free(pool_head_h2s, h2s); |
| 982 | goto out; |
| 983 | } |
| 984 | h2s->send_wait = NULL; |
| 985 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 986 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 987 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 988 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 989 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 990 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 991 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 992 | h2s->cs = NULL; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 993 | h2s->sws = 0; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 994 | h2s->flags = H2_SF_NONE; |
| 995 | h2s->errcode = H2_ERR_NO_ERROR; |
| 996 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 997 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 998 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 999 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1000 | |
| 1001 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1002 | h1m_init_req(&h2s->h1m); |
| 1003 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 1004 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 1005 | } else { |
| 1006 | h1m_init_res(&h2s->h1m); |
| 1007 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 1008 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 1009 | } |
| 1010 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1011 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1012 | if (id > 0) |
| 1013 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 1014 | else |
| 1015 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1016 | |
| 1017 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 1018 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 1019 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1020 | |
| 1021 | return h2s; |
| 1022 | |
| 1023 | out_free_h2s: |
| 1024 | pool_free(pool_head_h2s, h2s); |
| 1025 | out: |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | |
| 1029 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 1030 | * case of memory allocation error. |
| 1031 | */ |
| 1032 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 1033 | { |
| 1034 | struct session *sess = h2c->conn->owner; |
| 1035 | struct conn_stream *cs; |
| 1036 | struct h2s *h2s; |
| 1037 | |
| 1038 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 1039 | goto out; |
| 1040 | |
| 1041 | h2s = h2s_new(h2c, id); |
| 1042 | if (!h2s) |
| 1043 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1044 | |
| 1045 | cs = cs_new(h2c->conn); |
| 1046 | if (!cs) |
| 1047 | goto out_close; |
| 1048 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1049 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1050 | h2s->cs = cs; |
| 1051 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1052 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1053 | |
| 1054 | if (stream_create_from_cs(cs) < 0) |
| 1055 | goto out_free_cs; |
| 1056 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1057 | /* We want the accept date presented to the next stream to be the one |
| 1058 | * we have now, the handshake time to be null (since the next stream |
| 1059 | * is not delayed by a handshake), and the idle time to count since |
| 1060 | * right now. |
| 1061 | */ |
| 1062 | sess->accept_date = date; |
| 1063 | sess->tv_accept = now; |
| 1064 | sess->t_handshake = 0; |
| 1065 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1066 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1067 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1068 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1069 | return h2s; |
| 1070 | |
| 1071 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1072 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1073 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1074 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1075 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1076 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1077 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1078 | sess_log(sess); |
| 1079 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1082 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1083 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1084 | * possible stream ID was reached. |
| 1085 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1086 | 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] | 1087 | { |
| 1088 | struct h2s *h2s = NULL; |
| 1089 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1090 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1091 | goto out; |
| 1092 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1093 | if (h2_streams_left(h2c) < 1) |
| 1094 | goto out; |
| 1095 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1096 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1097 | h2s = h2s_new(h2c, 0); |
| 1098 | if (!h2s) |
| 1099 | goto out; |
| 1100 | |
| 1101 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1102 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1103 | cs->ctx = h2s; |
| 1104 | h2c->nb_cs++; |
| 1105 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1106 | out: |
| 1107 | return h2s; |
| 1108 | } |
| 1109 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1110 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1111 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1112 | * the various settings codes. |
| 1113 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1114 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1115 | { |
| 1116 | struct buffer *res; |
| 1117 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1118 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1119 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1120 | int ret; |
| 1121 | |
| 1122 | if (h2c_mux_busy(h2c, NULL)) { |
| 1123 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1127 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1128 | chunk_memcpy(&buf, |
| 1129 | "\x00\x00\x00" /* length : 0 for now */ |
| 1130 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1131 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1132 | 9); |
| 1133 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1134 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1135 | /* send settings_enable_push=0 */ |
| 1136 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1137 | } |
| 1138 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1139 | if (h2_settings_header_table_size != 4096) { |
| 1140 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1141 | |
| 1142 | write_n32(str + 2, h2_settings_header_table_size); |
| 1143 | chunk_memcat(&buf, str, 6); |
| 1144 | } |
| 1145 | |
| 1146 | if (h2_settings_initial_window_size != 65535) { |
| 1147 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1148 | |
| 1149 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1150 | chunk_memcat(&buf, str, 6); |
| 1151 | } |
| 1152 | |
| 1153 | if (h2_settings_max_concurrent_streams != 0) { |
| 1154 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1155 | |
| 1156 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1157 | * the protocol, so never send this value! |
| 1158 | */ |
| 1159 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1160 | chunk_memcat(&buf, str, 6); |
| 1161 | } |
| 1162 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1163 | mfs = h2_settings_max_frame_size; |
| 1164 | if (mfs > global.tune.bufsize) |
| 1165 | mfs = global.tune.bufsize; |
| 1166 | |
| 1167 | if (!mfs) |
| 1168 | mfs = global.tune.bufsize; |
| 1169 | |
| 1170 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1171 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1172 | |
| 1173 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1174 | * match bufsize - rewrite size, but at the moment it seems |
| 1175 | * that clients don't take care of it. |
| 1176 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1177 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1178 | chunk_memcat(&buf, str, 6); |
| 1179 | } |
| 1180 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1181 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1182 | |
| 1183 | res = br_tail(h2c->mbuf); |
| 1184 | retry: |
| 1185 | if (!h2_get_buf(h2c, res)) { |
| 1186 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1187 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1191 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1192 | if (unlikely(ret <= 0)) { |
| 1193 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1194 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1195 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1196 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1197 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | else { |
| 1201 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | } |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1208 | /* Try to receive a connection preface, then upon success try to send our |
| 1209 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1210 | * missing data. It may return an error in h2c. |
| 1211 | */ |
| 1212 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1213 | { |
| 1214 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1215 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1216 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1217 | 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] | 1218 | |
| 1219 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1220 | if (ret1 < 0) |
| 1221 | sess_log(h2c->conn->owner); |
| 1222 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1223 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1224 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1228 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1229 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1230 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1231 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1232 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1233 | } |
| 1234 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1235 | /* Try to send a connection preface, then upon success try to send our |
| 1236 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1237 | * missing data. It may return an error in h2c. |
| 1238 | */ |
| 1239 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1240 | { |
| 1241 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1242 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1243 | |
| 1244 | if (h2c_mux_busy(h2c, NULL)) { |
| 1245 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1249 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1250 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1251 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1252 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1253 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | if (!b_data(res)) { |
| 1258 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1259 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1260 | if (unlikely(ret <= 0)) { |
| 1261 | if (!ret) { |
| 1262 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1263 | goto retry; |
| 1264 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1265 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1266 | return 0; |
| 1267 | } |
| 1268 | else { |
| 1269 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1273 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1274 | return h2c_send_settings(h2c); |
| 1275 | } |
| 1276 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1277 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1278 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1279 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1280 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1281 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1282 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1283 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1284 | * case so that it is safe to use h2c_error() to report such errors. |
| 1285 | */ |
| 1286 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1287 | { |
| 1288 | struct buffer *res; |
| 1289 | char str[17]; |
| 1290 | int ret; |
| 1291 | |
| 1292 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1293 | return 1; // claim that it worked |
| 1294 | |
| 1295 | if (h2c_mux_busy(h2c, h2s)) { |
| 1296 | if (h2s) |
| 1297 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1298 | else |
| 1299 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1303 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1304 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1305 | |
| 1306 | if (h2c->last_sid < 0) |
| 1307 | h2c->last_sid = h2c->max_id; |
| 1308 | |
| 1309 | write_n32(str + 9, h2c->last_sid); |
| 1310 | write_n32(str + 13, h2c->errcode); |
| 1311 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1312 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1313 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1314 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1315 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1316 | if (h2s) |
| 1317 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1318 | else |
| 1319 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1323 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1324 | if (unlikely(ret <= 0)) { |
| 1325 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1326 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1327 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1328 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1329 | if (h2s) |
| 1330 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1331 | else |
| 1332 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | else { |
| 1336 | /* we cannot report this error using GOAWAY, so we mark |
| 1337 | * it and claim a success. |
| 1338 | */ |
| 1339 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1340 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1341 | return 1; |
| 1342 | } |
| 1343 | } |
| 1344 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1348 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1349 | * during mux operations. This stream must be valid and cannot be closed |
| 1350 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1351 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1352 | * not yet. |
| 1353 | * |
| 1354 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1355 | * to write the message, it subscribes the stream to future notifications. |
| 1356 | */ |
| 1357 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1358 | { |
| 1359 | struct buffer *res; |
| 1360 | char str[13]; |
| 1361 | int ret; |
| 1362 | |
| 1363 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1364 | return 1; |
| 1365 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1366 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1367 | * RST_STREAM in response to a RST_STREAM frame. |
| 1368 | */ |
Willy Tarreau | bf9a20b | 2019-08-06 10:01:40 +0200 | [diff] [blame] | 1369 | if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) { |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1370 | ret = 1; |
| 1371 | goto ignore; |
| 1372 | } |
| 1373 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1374 | if (h2c_mux_busy(h2c, h2s)) { |
| 1375 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1379 | /* len: 4, type: 3, flags: none */ |
| 1380 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1381 | write_n32(str + 5, h2s->id); |
| 1382 | write_n32(str + 9, h2s->errcode); |
| 1383 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1384 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1385 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1386 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1387 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1388 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1392 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1393 | if (unlikely(ret <= 0)) { |
| 1394 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1395 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1396 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1397 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1398 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1399 | return 0; |
| 1400 | } |
| 1401 | else { |
| 1402 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1403 | return 0; |
| 1404 | } |
| 1405 | } |
| 1406 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1407 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1408 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1409 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1410 | return ret; |
| 1411 | } |
| 1412 | |
| 1413 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1414 | * 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] | 1415 | * error code, even if the stream is one of the dummy ones, and will update |
| 1416 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1417 | * |
| 1418 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1419 | * 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] | 1420 | * 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] | 1421 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1422 | */ |
| 1423 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1424 | { |
| 1425 | struct buffer *res; |
| 1426 | char str[13]; |
| 1427 | int ret; |
| 1428 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1429 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1430 | * RST_STREAM in response to a RST_STREAM frame. |
| 1431 | */ |
| 1432 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1433 | ret = 1; |
| 1434 | goto ignore; |
| 1435 | } |
| 1436 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1437 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1438 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1439 | return 0; |
| 1440 | } |
| 1441 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1442 | /* len: 4, type: 3, flags: none */ |
| 1443 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1444 | |
| 1445 | write_n32(str + 5, h2c->dsi); |
| 1446 | write_n32(str + 9, h2s->errcode); |
| 1447 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1448 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1449 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1450 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1451 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1452 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1456 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1457 | if (unlikely(ret <= 0)) { |
| 1458 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1459 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1460 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1461 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1462 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | else { |
| 1466 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1467 | return 0; |
| 1468 | } |
| 1469 | } |
| 1470 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1471 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1472 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1473 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1474 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1477 | return ret; |
| 1478 | } |
| 1479 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1480 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1481 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1482 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1483 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1484 | * message, it subscribes the requesting stream to future notifications. |
| 1485 | */ |
| 1486 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1487 | { |
| 1488 | struct h2c *h2c = h2s->h2c; |
| 1489 | struct buffer *res; |
| 1490 | char str[9]; |
| 1491 | int ret; |
| 1492 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1493 | 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] | 1494 | return 1; |
| 1495 | |
| 1496 | if (h2c_mux_busy(h2c, h2s)) { |
| 1497 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1501 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1502 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1503 | write_n32(str + 5, h2s->id); |
| 1504 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1505 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1506 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1507 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1508 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1509 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1513 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1514 | if (likely(ret > 0)) { |
| 1515 | h2s->flags |= H2_SF_ES_SENT; |
| 1516 | } |
| 1517 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1518 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1519 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1520 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1521 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1522 | return 0; |
| 1523 | } |
| 1524 | else { |
| 1525 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1526 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1527 | } |
| 1528 | return ret; |
| 1529 | } |
| 1530 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1531 | /* 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] | 1532 | * 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] | 1533 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1534 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1535 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1536 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1537 | { |
| 1538 | if (!h2s->cs) { |
| 1539 | /* this stream was already orphaned */ |
| 1540 | h2s_destroy(h2s); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 1544 | if (h2c_read0_pending(h2s->h2c)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1545 | if (h2s->st == H2_SS_OPEN) |
| 1546 | h2s->st = H2_SS_HREM; |
| 1547 | else if (h2s->st == H2_SS_HLOC) |
| 1548 | h2s_close(h2s); |
| 1549 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1550 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1551 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1552 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1553 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1554 | if (h2s->cs->flags & CS_FL_EOS) |
| 1555 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1556 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1557 | if (h2s->st < H2_SS_ERROR) |
| 1558 | h2s->st = H2_SS_ERROR; |
| 1559 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1560 | |
| 1561 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1565 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1566 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1567 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1568 | { |
| 1569 | struct eb32_node *node; |
| 1570 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1571 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1572 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1573 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1574 | while (node) { |
| 1575 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1576 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1577 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1578 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1579 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1580 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1581 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1582 | while (node) { |
| 1583 | h2s = container_of(node, struct h2s, by_id); |
| 1584 | if (h2s->id > 0) |
| 1585 | break; |
| 1586 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1587 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1591 | /* Wake up all blocked streams whose window size has become positive after the |
| 1592 | * mux's initial window was adjusted. This should be done after having processed |
| 1593 | * SETTINGS frames which have updated the mux's initial window size. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1594 | */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1595 | static void h2c_unblock_sfctl(struct h2c *h2c) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1596 | { |
| 1597 | struct h2s *h2s; |
| 1598 | struct eb32_node *node; |
| 1599 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1600 | node = eb32_first(&h2c->streams_by_id); |
| 1601 | while (node) { |
| 1602 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1603 | if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) { |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1604 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1605 | LIST_DEL_INIT(&h2s->list); |
| 1606 | if (h2s->send_wait) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1607 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1608 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1609 | node = eb32_next(node); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1614 | * 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] | 1615 | * return an error in h2c. The caller must have already verified frame length |
| 1616 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1617 | */ |
| 1618 | static int h2c_handle_settings(struct h2c *h2c) |
| 1619 | { |
| 1620 | unsigned int offset; |
| 1621 | int error; |
| 1622 | |
| 1623 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1624 | if (h2c->dfl) { |
| 1625 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1626 | goto fail; |
| 1627 | } |
| 1628 | return 1; |
| 1629 | } |
| 1630 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1631 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1632 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1633 | return 0; |
| 1634 | |
| 1635 | /* parse the frame */ |
| 1636 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1637 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1638 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1639 | |
| 1640 | switch (type) { |
| 1641 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1642 | /* we need to update all existing streams with the |
| 1643 | * difference from the previous iws. |
| 1644 | */ |
| 1645 | if (arg < 0) { // RFC7540#6.5.2 |
| 1646 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1647 | goto fail; |
| 1648 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1649 | h2c->miw = arg; |
| 1650 | break; |
| 1651 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1652 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1653 | error = H2_ERR_PROTOCOL_ERROR; |
| 1654 | goto fail; |
| 1655 | } |
| 1656 | h2c->mfs = arg; |
| 1657 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1658 | case H2_SETTINGS_ENABLE_PUSH: |
| 1659 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1660 | error = H2_ERR_PROTOCOL_ERROR; |
| 1661 | goto fail; |
| 1662 | } |
| 1663 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1664 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1665 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1666 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1667 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1668 | arg = h2_settings_max_concurrent_streams; |
| 1669 | h2c->streams_limit = arg; |
| 1670 | } |
| 1671 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /* need to ACK this frame now */ |
| 1676 | h2c->st0 = H2_CS_FRAME_A; |
| 1677 | return 1; |
| 1678 | fail: |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 1679 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 1680 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1681 | h2c_error(h2c, error); |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1686 | * success or one of the h2_status values. |
| 1687 | */ |
| 1688 | static int h2c_ack_settings(struct h2c *h2c) |
| 1689 | { |
| 1690 | struct buffer *res; |
| 1691 | char str[9]; |
| 1692 | int ret = -1; |
| 1693 | |
| 1694 | if (h2c_mux_busy(h2c, NULL)) { |
| 1695 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1699 | memcpy(str, |
| 1700 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1701 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1702 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1703 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1704 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1705 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1706 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1707 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1708 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1712 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1713 | if (unlikely(ret <= 0)) { |
| 1714 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1715 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1716 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1717 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1718 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1719 | return 0; |
| 1720 | } |
| 1721 | else { |
| 1722 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1723 | return 0; |
| 1724 | } |
| 1725 | } |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1729 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1730 | * 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] | 1731 | * missing data. The caller must have already verified frame length |
| 1732 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1733 | */ |
| 1734 | static int h2c_handle_ping(struct h2c *h2c) |
| 1735 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1736 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1737 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1738 | h2c->st0 = H2_CS_FRAME_A; |
| 1739 | return 1; |
| 1740 | } |
| 1741 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1742 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1743 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1744 | * error in h2c. |
| 1745 | */ |
| 1746 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1747 | { |
| 1748 | struct buffer *res; |
| 1749 | char str[13]; |
| 1750 | int ret = -1; |
| 1751 | |
| 1752 | if (h2c_mux_busy(h2c, NULL)) { |
| 1753 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1757 | /* length: 4, type: 8, flags: none */ |
| 1758 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1759 | write_n32(str + 5, sid); |
| 1760 | write_n32(str + 9, increment); |
| 1761 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1762 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1763 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1764 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1765 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1766 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1770 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1771 | if (unlikely(ret <= 0)) { |
| 1772 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1773 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1774 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1775 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1776 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1777 | return 0; |
| 1778 | } |
| 1779 | else { |
| 1780 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1781 | return 0; |
| 1782 | } |
| 1783 | } |
| 1784 | return ret; |
| 1785 | } |
| 1786 | |
| 1787 | /* try to send pending window update for the connection. It's safe to call it |
| 1788 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1789 | * failure. It may return an error in h2c. |
| 1790 | */ |
| 1791 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1792 | { |
| 1793 | int ret = 1; |
| 1794 | |
| 1795 | if (h2c->rcvd_c <= 0) |
| 1796 | return 1; |
| 1797 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1798 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1799 | /* increase the advertised connection window to 2G on |
| 1800 | * first update. |
| 1801 | */ |
| 1802 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1803 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1804 | } |
| 1805 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1806 | /* send WU for the connection */ |
| 1807 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1808 | if (ret > 0) |
| 1809 | h2c->rcvd_c = 0; |
| 1810 | |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
| 1814 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1815 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1816 | * room or failure. It may return an error in h2c. |
| 1817 | */ |
| 1818 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1819 | { |
| 1820 | int ret = 1; |
| 1821 | |
| 1822 | if (h2c->rcvd_s <= 0) |
| 1823 | return 1; |
| 1824 | |
| 1825 | /* send WU for the stream */ |
| 1826 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1827 | if (ret > 0) |
| 1828 | h2c->rcvd_s = 0; |
| 1829 | |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1833 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1834 | * success, 0 on missing data or one of the h2_status values. |
| 1835 | */ |
| 1836 | static int h2c_ack_ping(struct h2c *h2c) |
| 1837 | { |
| 1838 | struct buffer *res; |
| 1839 | char str[17]; |
| 1840 | int ret = -1; |
| 1841 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1842 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1843 | return 0; |
| 1844 | |
| 1845 | if (h2c_mux_busy(h2c, NULL)) { |
| 1846 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1850 | memcpy(str, |
| 1851 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1852 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1853 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1854 | |
| 1855 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1856 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1857 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1858 | res = br_tail(h2c->mbuf); |
| 1859 | retry: |
| 1860 | if (!h2_get_buf(h2c, res)) { |
| 1861 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1862 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1866 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1867 | if (unlikely(ret <= 0)) { |
| 1868 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1869 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1870 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1871 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1872 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1873 | return 0; |
| 1874 | } |
| 1875 | else { |
| 1876 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1877 | return 0; |
| 1878 | } |
| 1879 | } |
| 1880 | return ret; |
| 1881 | } |
| 1882 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1883 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1884 | * 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] | 1885 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1886 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1887 | */ |
| 1888 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1889 | { |
| 1890 | int32_t inc; |
| 1891 | int error; |
| 1892 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1893 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1894 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1895 | return 0; |
| 1896 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1897 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1898 | |
| 1899 | if (h2c->dsi != 0) { |
| 1900 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1901 | |
| 1902 | /* it's not an error to receive WU on a closed stream */ |
| 1903 | if (h2s->st == H2_SS_CLOSED) |
| 1904 | return 1; |
| 1905 | |
| 1906 | if (!inc) { |
| 1907 | error = H2_ERR_PROTOCOL_ERROR; |
| 1908 | goto strm_err; |
| 1909 | } |
| 1910 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1911 | if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1912 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1913 | goto strm_err; |
| 1914 | } |
| 1915 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1916 | h2s->sws += inc; |
| 1917 | if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1918 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1919 | LIST_DEL_INIT(&h2s->list); |
| 1920 | if (h2s->send_wait) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1921 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | else { |
| 1925 | /* connection window update */ |
| 1926 | if (!inc) { |
| 1927 | error = H2_ERR_PROTOCOL_ERROR; |
| 1928 | goto conn_err; |
| 1929 | } |
| 1930 | |
| 1931 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1932 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1933 | goto conn_err; |
| 1934 | } |
| 1935 | |
| 1936 | h2c->mws += inc; |
| 1937 | } |
| 1938 | |
| 1939 | return 1; |
| 1940 | |
| 1941 | conn_err: |
| 1942 | h2c_error(h2c, error); |
| 1943 | return 0; |
| 1944 | |
| 1945 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1946 | h2s_error(h2s, error); |
| 1947 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1948 | return 0; |
| 1949 | } |
| 1950 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1951 | /* 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] | 1952 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1953 | * have already verified frame length and stream ID validity. Described in |
| 1954 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1955 | */ |
| 1956 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1957 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1958 | int last; |
| 1959 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1960 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1961 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1962 | return 0; |
| 1963 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1964 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1965 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1966 | if (h2c->last_sid < 0) |
| 1967 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1968 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1969 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1972 | /* 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] | 1973 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1974 | * error in h2c. The caller must have already verified frame length and stream |
| 1975 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1976 | */ |
| 1977 | static int h2c_handle_priority(struct h2c *h2c) |
| 1978 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1979 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1980 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1981 | return 0; |
| 1982 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1983 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1984 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1985 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1986 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1987 | } |
| 1988 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1989 | } |
| 1990 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1991 | /* 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] | 1992 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1993 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1994 | */ |
| 1995 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1996 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1997 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1998 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1999 | return 0; |
| 2000 | |
| 2001 | /* late RST, already handled */ |
| 2002 | if (h2s->st == H2_SS_CLOSED) |
| 2003 | return 1; |
| 2004 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2005 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2006 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2007 | |
| 2008 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 2009 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 2010 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | h2s->flags |= H2_SF_RST_RCVD; |
| 2014 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2017 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2018 | * It may return an error in h2c or h2s. The caller must consider that the |
| 2019 | * return value is the new h2s in case one was allocated (most common case). |
| 2020 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2021 | * errors here are reported as connection errors since it's impossible to |
| 2022 | * recover from such errors after the compression context has been altered. |
| 2023 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2024 | 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] | 2025 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2026 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2027 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2028 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2029 | int error; |
| 2030 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2031 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2032 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2033 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2034 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2035 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2036 | |
| 2037 | /* now either the frame is complete or the buffer is complete */ |
| 2038 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2039 | /* The stream exists/existed, this must be a trailers frame */ |
| 2040 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2041 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2042 | /* unrecoverable error ? */ |
| 2043 | if (h2c->st0 >= H2_CS_ERROR) |
| 2044 | goto out; |
| 2045 | |
| 2046 | if (error == 0) |
| 2047 | goto out; // missing data |
| 2048 | |
| 2049 | if (error < 0) { |
| 2050 | /* Failed to decode this frame (e.g. too large request) |
| 2051 | * but the HPACK decompressor is still synchronized. |
| 2052 | */ |
| 2053 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2054 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2055 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2056 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2057 | goto done; |
| 2058 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2059 | /* the connection was already killed by an RST, let's consume |
| 2060 | * the data and send another RST. |
| 2061 | */ |
| 2062 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2063 | h2s = (struct h2s*)h2_error_stream; |
| 2064 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2065 | } |
| 2066 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2067 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2068 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2069 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2070 | goto conn_err; |
| 2071 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2072 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2073 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2074 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2075 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2076 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2077 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2078 | if (h2c->st0 >= H2_CS_ERROR) |
| 2079 | goto out; |
| 2080 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2081 | if (error <= 0) { |
| 2082 | if (error == 0) |
| 2083 | goto out; // missing data |
| 2084 | |
| 2085 | /* Failed to decode this stream (e.g. too large request) |
| 2086 | * but the HPACK decompressor is still synchronized. |
| 2087 | */ |
| 2088 | h2s = (struct h2s*)h2_error_stream; |
| 2089 | goto send_rst; |
| 2090 | } |
| 2091 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2092 | /* 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] | 2093 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2094 | * 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] | 2095 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2096 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2097 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2098 | h2s = (struct h2s*)h2_refused_stream; |
| 2099 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2103 | h2s->rxbuf = rxbuf; |
| 2104 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2105 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2106 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2107 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2108 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2109 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2110 | |
| 2111 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2112 | if (h2s->st == H2_SS_OPEN) |
| 2113 | h2s->st = H2_SS_HREM; |
| 2114 | else |
| 2115 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2116 | } |
| 2117 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2118 | /* update the max stream ID if the request is being processed */ |
| 2119 | if (h2s->id > h2c->max_id) |
| 2120 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2121 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2122 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2123 | |
| 2124 | conn_err: |
| 2125 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2126 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2127 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2128 | out: |
| 2129 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2130 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2131 | |
| 2132 | send_rst: |
| 2133 | /* make the demux send an RST for the current stream. We may only |
| 2134 | * do this if we're certain that the HEADERS frame was properly |
| 2135 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2136 | */ |
| 2137 | h2_release_buf(h2c, &rxbuf); |
| 2138 | h2c->st0 = H2_CS_FRAME_E; |
| 2139 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2140 | } |
| 2141 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2142 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2143 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2144 | * errors here are reported as connection errors since it's impossible to |
| 2145 | * recover from such errors after the compression context has been altered. |
| 2146 | */ |
| 2147 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2148 | { |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2149 | struct buffer rxbuf = BUF_NULL; |
| 2150 | unsigned long long body_len = 0; |
| 2151 | uint32_t flags = 0; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2152 | int error; |
| 2153 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2154 | if (!b_size(&h2c->dbuf)) |
| 2155 | return NULL; // empty buffer |
| 2156 | |
| 2157 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2158 | return NULL; // incomplete frame |
| 2159 | |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2160 | if (h2s->st != H2_SS_CLOSED) { |
| 2161 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
| 2162 | } |
| 2163 | else { |
| 2164 | /* the connection was already killed by an RST, let's consume |
| 2165 | * the data and send another RST. |
| 2166 | */ |
| 2167 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Christopher Faulet | 0342705 | 2019-09-26 16:19:13 +0200 | [diff] [blame] | 2168 | h2s = (struct h2s*)h2_error_stream; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2169 | h2c->st0 = H2_CS_FRAME_E; |
| 2170 | goto send_rst; |
| 2171 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2172 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2173 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2174 | if (h2c->st0 >= H2_CS_ERROR) |
| 2175 | return NULL; |
| 2176 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2177 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2178 | /* RFC7540#5.1 */ |
| 2179 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2180 | h2c->st0 = H2_CS_FRAME_E; |
| 2181 | return NULL; |
| 2182 | } |
| 2183 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2184 | if (error <= 0) { |
| 2185 | if (error == 0) |
| 2186 | return NULL; // missing data |
| 2187 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2188 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2189 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2190 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2191 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2192 | } |
| 2193 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2194 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2195 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2196 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2197 | 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] | 2198 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2199 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2200 | if (h2s->st == H2_SS_OPEN) |
| 2201 | h2s->st = H2_SS_HREM; |
| 2202 | else if (h2s->st == H2_SS_HLOC) |
| 2203 | h2s_close(h2s); |
| 2204 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2205 | |
| 2206 | return h2s; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2207 | |
| 2208 | send_rst: |
| 2209 | /* make the demux send an RST for the current stream. We may only |
| 2210 | * do this if we're certain that the HEADERS frame was properly |
| 2211 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2212 | */ |
| 2213 | h2_release_buf(h2c, &rxbuf); |
| 2214 | h2c->st0 = H2_CS_FRAME_E; |
| 2215 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2216 | } |
| 2217 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2218 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2219 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2220 | */ |
| 2221 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2222 | { |
| 2223 | int error; |
| 2224 | |
| 2225 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2226 | * to signal an end of stream (with the ES flag). |
| 2227 | */ |
| 2228 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2229 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2230 | return 0; // empty buffer |
| 2231 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2232 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2233 | return 0; // incomplete frame |
| 2234 | |
| 2235 | /* now either the frame is complete or the buffer is complete */ |
| 2236 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2237 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2238 | /* RFC7540#6.1 */ |
| 2239 | error = H2_ERR_STREAM_CLOSED; |
| 2240 | goto strm_err; |
| 2241 | } |
| 2242 | |
Christopher Faulet | 4fb65f4 | 2019-06-19 09:25:58 +0200 | [diff] [blame] | 2243 | if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2244 | /* RFC7540#8.1.2 */ |
| 2245 | error = H2_ERR_PROTOCOL_ERROR; |
| 2246 | goto strm_err; |
| 2247 | } |
| 2248 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2249 | if (!h2_frt_transfer_data(h2s)) |
| 2250 | return 0; |
| 2251 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2252 | /* call the upper layers to process the frame, then let the upper layer |
| 2253 | * notify the stream about any change. |
| 2254 | */ |
| 2255 | if (!h2s->cs) { |
Willy Tarreau | c1a2965 | 2019-08-06 10:11:02 +0200 | [diff] [blame] | 2256 | /* The upper layer has already closed, this may happen on |
| 2257 | * 4xx/redirects during POST, or when receiving a response |
| 2258 | * from an H2 server after the client has aborted. |
| 2259 | */ |
| 2260 | error = H2_ERR_CANCEL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2261 | goto strm_err; |
| 2262 | } |
| 2263 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2264 | if (h2c->st0 >= H2_CS_ERROR) |
| 2265 | return 0; |
| 2266 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2267 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2268 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2269 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /* check for completion : the callee will change this to FRAME_A or |
| 2273 | * FRAME_H once done. |
| 2274 | */ |
| 2275 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2276 | return 0; |
| 2277 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2278 | /* last frame */ |
| 2279 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2280 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2281 | if (h2s->st == H2_SS_OPEN) |
| 2282 | h2s->st = H2_SS_HREM; |
| 2283 | else |
| 2284 | h2s_close(h2s); |
| 2285 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2286 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2287 | /* RFC7540#8.1.2 */ |
| 2288 | error = H2_ERR_PROTOCOL_ERROR; |
| 2289 | goto strm_err; |
| 2290 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2293 | return 1; |
| 2294 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2295 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2296 | h2s_error(h2s, error); |
| 2297 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2298 | return 0; |
| 2299 | } |
| 2300 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2301 | /* process Rx frames to be demultiplexed */ |
| 2302 | static void h2_process_demux(struct h2c *h2c) |
| 2303 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2304 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2305 | struct h2_fh hdr; |
| 2306 | unsigned int padlen = 0; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2307 | int32_t old_iw = h2c->miw; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2308 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2309 | if (h2c->st0 >= H2_CS_ERROR) |
| 2310 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2311 | |
| 2312 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2313 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2314 | if (h2c->flags & H2_CF_IS_BACK) |
| 2315 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2316 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2317 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2318 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2319 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2320 | sess_log(h2c->conn->owner); |
| 2321 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2322 | goto fail; |
| 2323 | } |
| 2324 | |
| 2325 | h2c->max_id = 0; |
| 2326 | h2c->st0 = H2_CS_SETTINGS1; |
| 2327 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2328 | |
| 2329 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2330 | /* ensure that what is pending is a valid SETTINGS frame |
| 2331 | * without an ACK. |
| 2332 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2333 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2334 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2335 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2336 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2337 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2338 | sess_log(h2c->conn->owner); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2339 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2340 | goto fail; |
| 2341 | } |
| 2342 | |
| 2343 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2344 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2345 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2346 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2347 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2348 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2349 | goto fail; |
| 2350 | } |
| 2351 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2352 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2353 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2354 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2355 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2356 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2357 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2358 | goto fail; |
| 2359 | } |
| 2360 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2361 | /* that's OK, switch to FRAME_P to process it. This is |
| 2362 | * a SETTINGS frame whose header has already been |
| 2363 | * deleted above. |
| 2364 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2365 | padlen = 0; |
| 2366 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2367 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2368 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2369 | |
| 2370 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2371 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2372 | int ret = 0; |
| 2373 | |
| 2374 | if (h2c->st0 >= H2_CS_ERROR) |
| 2375 | break; |
| 2376 | |
| 2377 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | ab3ebbc | 2019-08-06 15:49:51 +0200 | [diff] [blame] | 2378 | h2c->rcvd_s = 0; |
| 2379 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2380 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2381 | break; |
| 2382 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2383 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2384 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2385 | if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2386 | /* only log if no other stream can report the error */ |
| 2387 | sess_log(h2c->conn->owner); |
| 2388 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2389 | break; |
| 2390 | } |
| 2391 | |
Christopher Faulet | 3d574a5 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2392 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2393 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2394 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2395 | * we read the pad length and drop it from the remaining |
| 2396 | * payload (one byte + the 9 remaining ones = 10 total |
| 2397 | * removed), so we have a frame payload starting after the |
| 2398 | * pad len. Flow controlled frames (DATA) also count the |
| 2399 | * padlen in the flow control, so it must be adjusted. |
| 2400 | */ |
| 2401 | if (hdr.len < 1) { |
| 2402 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2403 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2404 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2405 | goto fail; |
| 2406 | } |
| 2407 | hdr.len--; |
| 2408 | |
| 2409 | if (b_data(&h2c->dbuf) < 10) |
| 2410 | break; // missing padlen |
| 2411 | |
| 2412 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2413 | |
| 2414 | if (padlen > hdr.len) { |
| 2415 | /* RFC7540#6.1 : pad length = length of |
| 2416 | * frame payload or greater => error. |
| 2417 | */ |
| 2418 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2419 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2420 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2421 | goto fail; |
| 2422 | } |
| 2423 | |
| 2424 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2425 | h2c->rcvd_c++; |
| 2426 | h2c->rcvd_s++; |
| 2427 | } |
| 2428 | b_del(&h2c->dbuf, 1); |
| 2429 | } |
| 2430 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2431 | |
| 2432 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2433 | h2c->dfl = hdr.len; |
| 2434 | h2c->dsi = hdr.sid; |
| 2435 | h2c->dft = hdr.ft; |
| 2436 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2437 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2438 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2439 | |
| 2440 | /* check for minimum basic frame format validity */ |
| 2441 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2442 | if (ret != H2_ERR_NO_ERROR) { |
| 2443 | h2c_error(h2c, ret); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2444 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2445 | sess_log(h2c->conn->owner); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2446 | goto fail; |
| 2447 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2448 | } |
| 2449 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2450 | /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here. |
| 2451 | * H2_CS_FRAME_P indicates an incomplete previous operation |
| 2452 | * (most often the first attempt) and requires some validity |
| 2453 | * checks for the frame and the current state. The two other |
| 2454 | * ones are set after completion (or abortion) and must skip |
| 2455 | * validity checks. |
| 2456 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2457 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2458 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2459 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2460 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 2461 | h2c_read0_pending(h2c) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2462 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2463 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2464 | (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] | 2465 | /* we may have to signal the upper layers */ |
| 2466 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2467 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2468 | } |
| 2469 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2470 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2471 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2472 | goto strm_err; |
| 2473 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2474 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2475 | goto valid_frame_type; |
| 2476 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2477 | if (h2s->st == H2_SS_IDLE && |
| 2478 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2479 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2480 | * this state MUST be treated as a connection error |
| 2481 | */ |
| 2482 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2483 | if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2484 | /* only log if no other stream can report the error */ |
| 2485 | sess_log(h2c->conn->owner); |
| 2486 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2487 | break; |
| 2488 | } |
| 2489 | |
Willy Tarreau | 85ee6e8 | 2019-11-24 14:57:53 +0100 | [diff] [blame] | 2490 | if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) { |
| 2491 | /* only PUSH_PROMISE would be permitted here */ |
| 2492 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2493 | break; |
| 2494 | } |
| 2495 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2496 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2497 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2498 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2499 | * this state MUST be treated as a stream error. |
| 2500 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2501 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2502 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2503 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2504 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2505 | else |
| 2506 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2507 | goto strm_err; |
| 2508 | } |
| 2509 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2510 | /* Below the management of frames received in closed state is a |
| 2511 | * bit hackish because the spec makes strong differences between |
| 2512 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2513 | * in both directions. In addition to this, the creation of a |
| 2514 | * new stream reusing the identifier of a closed one will be |
| 2515 | * detected here. Given that we cannot keep track of all closed |
| 2516 | * streams forever, we consider that unknown closed streams were |
| 2517 | * closed on RST received, which allows us to respond with an |
| 2518 | * RST without breaking the connection (eg: to abort a transfer). |
| 2519 | * Some frames have to be silently ignored as well. |
| 2520 | */ |
| 2521 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2522 | 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] | 2523 | /* #5.1.1: The identifier of a newly |
| 2524 | * established stream MUST be numerically |
| 2525 | * greater than all streams that the initiating |
| 2526 | * endpoint has opened or reserved. This |
| 2527 | * governs streams that are opened using a |
| 2528 | * HEADERS frame and streams that are reserved |
| 2529 | * using PUSH_PROMISE. An endpoint that |
| 2530 | * receives an unexpected stream identifier |
| 2531 | * MUST respond with a connection error. |
| 2532 | */ |
| 2533 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2534 | goto strm_err; |
| 2535 | } |
| 2536 | |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2537 | if (h2s->flags & H2_SF_RST_RCVD && |
| 2538 | !(h2_ft_bit(h2c->dft) & (H2_FT_HDR_MASK | H2_FT_RST_STREAM_BIT | H2_FT_PRIORITY_BIT | H2_FT_WINDOW_UPDATE_BIT))) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2539 | /* RFC7540#5.1:closed: an endpoint that |
| 2540 | * receives any frame other than PRIORITY after |
| 2541 | * receiving a RST_STREAM MUST treat that as a |
| 2542 | * stream error of type STREAM_CLOSED. |
| 2543 | * |
| 2544 | * Note that old streams fall into this category |
| 2545 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2546 | * |
| 2547 | * However, we cannot generalize this to all frame types. Those |
| 2548 | * carrying compression state must still be processed before |
| 2549 | * being dropped or we'll desynchronize the decoder. This can |
| 2550 | * happen with request trailers received after sending an |
| 2551 | * RST_STREAM, or with header/trailers responses received after |
| 2552 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2553 | * |
| 2554 | * In addition, since our CLOSED streams always carry the |
| 2555 | * RST_RCVD bit, we don't want to accidently catch valid |
| 2556 | * frames for a closed stream, i.e. RST/PRIO/WU. |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2557 | */ |
| 2558 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2559 | h2c->st0 = H2_CS_FRAME_E; |
| 2560 | goto strm_err; |
| 2561 | } |
| 2562 | |
| 2563 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2564 | * result of sending a RST_STREAM frame, the peer that |
| 2565 | * receives the RST_STREAM might have already sent |
| 2566 | * frames on the stream that cannot be withdrawn. An |
| 2567 | * endpoint MUST ignore frames that it receives on |
| 2568 | * closed streams after it has sent a RST_STREAM |
| 2569 | * frame. An endpoint MAY choose to limit the period |
| 2570 | * over which it ignores frames and treat frames that |
| 2571 | * arrive after this time as being in error. |
| 2572 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2573 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2574 | /* RFC7540#5.1:closed: any frame other than |
| 2575 | * PRIO/WU/RST in this state MUST be treated as |
| 2576 | * a connection error |
| 2577 | */ |
| 2578 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2579 | h2c->dft != H2_FT_PRIORITY && |
| 2580 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2581 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2582 | goto strm_err; |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2587 | #if 0 |
| 2588 | // problem below: it is not possible to completely ignore such |
| 2589 | // streams as we need to maintain the compression state as well |
| 2590 | // and for this we need to completely process these frames (eg: |
| 2591 | // HEADERS frames) as well as counting DATA frames to emit |
| 2592 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2593 | // This is a typical case of layer violation where the |
| 2594 | // transported contents are critical to the connection's |
| 2595 | // validity and must be ignored at the same time :-( |
| 2596 | |
| 2597 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2598 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2599 | */ |
| 2600 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2601 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2602 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2603 | h2c->dfl -= ret; |
| 2604 | ret = h2c->dfl == 0; |
| 2605 | goto strm_err; |
| 2606 | } |
| 2607 | #endif |
| 2608 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2609 | valid_frame_type: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2610 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2611 | case H2_FT_SETTINGS: |
| 2612 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2613 | ret = h2c_handle_settings(h2c); |
| 2614 | |
| 2615 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2616 | ret = h2c_ack_settings(h2c); |
| 2617 | break; |
| 2618 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2619 | case H2_FT_PING: |
| 2620 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2621 | ret = h2c_handle_ping(h2c); |
| 2622 | |
| 2623 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2624 | ret = h2c_ack_ping(h2c); |
| 2625 | break; |
| 2626 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2627 | case H2_FT_WINDOW_UPDATE: |
| 2628 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2629 | ret = h2c_handle_window_update(h2c, h2s); |
| 2630 | break; |
| 2631 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2632 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2633 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2634 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2635 | * frames' parsers consume all following CONTINUATION |
| 2636 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2637 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2638 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2639 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2640 | sess_log(h2c->conn->owner); |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2641 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2642 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2643 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2644 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2645 | if (h2c->flags & H2_CF_IS_BACK) |
| 2646 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2647 | else |
| 2648 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2649 | if (tmp_h2s) { |
| 2650 | h2s = tmp_h2s; |
| 2651 | ret = 1; |
| 2652 | } |
| 2653 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2654 | break; |
| 2655 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2656 | case H2_FT_DATA: |
| 2657 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2658 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2659 | |
| 2660 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2661 | ret = h2c_send_strm_wu(h2c); |
| 2662 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2663 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2664 | case H2_FT_PRIORITY: |
| 2665 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2666 | ret = h2c_handle_priority(h2c); |
| 2667 | break; |
| 2668 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2669 | case H2_FT_RST_STREAM: |
| 2670 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2671 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2672 | break; |
| 2673 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2674 | case H2_FT_GOAWAY: |
| 2675 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2676 | ret = h2c_handle_goaway(h2c); |
| 2677 | break; |
| 2678 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2679 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2680 | default: |
| 2681 | /* drop frames that we ignore. They may be larger than |
| 2682 | * the buffer so we drain all of their contents until |
| 2683 | * we reach the end. |
| 2684 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2685 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2686 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2687 | h2c->dfl -= ret; |
| 2688 | ret = h2c->dfl == 0; |
| 2689 | } |
| 2690 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2691 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2692 | /* We may have to send an RST if not done yet */ |
| 2693 | if (h2s->st == H2_SS_ERROR) |
| 2694 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2695 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2696 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2697 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2698 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2699 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2700 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2701 | break; |
| 2702 | |
| 2703 | if (h2c->st0 != H2_CS_FRAME_H) { |
Christopher Faulet | e12a26f | 2019-09-26 16:38:28 +0200 | [diff] [blame] | 2704 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2705 | b_del(&h2c->dbuf, ret); |
| 2706 | h2c->dfl -= ret; |
| 2707 | if (!h2c->dfl) |
| 2708 | h2c->st0 = H2_CS_FRAME_H; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2709 | } |
| 2710 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2711 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2712 | if (h2c->rcvd_c > 0 && |
| 2713 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2714 | h2c_send_conn_wu(h2c); |
| 2715 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2716 | fail: |
| 2717 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2718 | if (h2s && h2s->cs && |
| 2719 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 2720 | h2c_read0_pending(h2c) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2721 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2722 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2723 | (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] | 2724 | /* we may have to signal the upper layers */ |
| 2725 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2726 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2727 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2728 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2729 | if (old_iw != h2c->miw) |
| 2730 | h2c_unblock_sfctl(h2c); |
| 2731 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2732 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2735 | /* resume each h2s eligible for sending in list head <head> */ |
| 2736 | static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head) |
| 2737 | { |
| 2738 | struct h2s *h2s, *h2s_back; |
| 2739 | |
| 2740 | list_for_each_entry_safe(h2s, h2s_back, head, list) { |
| 2741 | if (h2c->mws <= 0 || |
| 2742 | h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2743 | h2c->st0 >= H2_CS_ERROR) |
| 2744 | break; |
| 2745 | |
| 2746 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | d4e72e2 | 2020-01-10 18:20:15 +0100 | [diff] [blame] | 2747 | |
| 2748 | if (LIST_ADDED(&h2s->sending_list)) |
| 2749 | continue; |
| 2750 | |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2751 | /* For some reason, the upper layer failed to subscribe again, |
| 2752 | * so remove it from the send_list |
| 2753 | */ |
| 2754 | if (!h2s->send_wait) { |
| 2755 | LIST_DEL_INIT(&h2s->list); |
| 2756 | continue; |
| 2757 | } |
| 2758 | |
| 2759 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2760 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
| 2761 | tasklet_wakeup(h2s->send_wait->tasklet); |
| 2762 | } |
| 2763 | } |
| 2764 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2765 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2766 | * the end. |
| 2767 | */ |
| 2768 | static int h2_process_mux(struct h2c *h2c) |
| 2769 | { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2770 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2771 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2772 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2773 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2774 | if (h2c->st0 == H2_CS_ERROR) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2775 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2776 | goto fail; |
| 2777 | } |
| 2778 | h2c->st0 = H2_CS_SETTINGS1; |
| 2779 | } |
| 2780 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2781 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2782 | return 1; |
| 2783 | } |
| 2784 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2785 | /* start by sending possibly pending window updates */ |
Willy Tarreau | 9c497c2 | 2019-08-06 15:39:32 +0200 | [diff] [blame] | 2786 | if (h2c->rcvd_s > 0 && |
| 2787 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2788 | h2c_send_strm_wu(h2c) < 0) |
| 2789 | goto fail; |
| 2790 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2791 | if (h2c->rcvd_c > 0 && |
| 2792 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2793 | h2c_send_conn_wu(h2c) < 0) |
| 2794 | goto fail; |
| 2795 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2796 | /* First we always process the flow control list because the streams |
| 2797 | * waiting there were already elected for immediate emission but were |
| 2798 | * blocked just on this. |
| 2799 | */ |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2800 | h2_resume_each_sending_h2s(h2c, &h2c->fctl_list); |
| 2801 | h2_resume_each_sending_h2s(h2c, &h2c->send_list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2802 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2803 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2804 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2805 | if (h2c->st0 == H2_CS_ERROR) { |
| 2806 | if (h2c->max_id >= 0) { |
| 2807 | h2c_send_goaway_error(h2c, NULL); |
| 2808 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2809 | return 0; |
| 2810 | } |
| 2811 | |
| 2812 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2813 | } |
| 2814 | return 1; |
| 2815 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2816 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2817 | } |
| 2818 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2819 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2820 | /* Attempt to read data, and subscribe if none available. |
| 2821 | * The function returns 1 if data has been received, otherwise zero. |
| 2822 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2823 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2824 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2825 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2826 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2827 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2828 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2829 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2830 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2831 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2832 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2833 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2834 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2835 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2836 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2837 | if (!buf) { |
| 2838 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2839 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2840 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2841 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2842 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2843 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2844 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2845 | /* HTX in use : try to pre-align the buffer like the |
| 2846 | * rxbufs will be to optimize memory copies. We'll make |
| 2847 | * sure that the frame header lands at the end of the |
| 2848 | * HTX block to alias it upon recv. We cannot use the |
| 2849 | * head because rcv_buf() will realign the buffer if |
| 2850 | * it's empty. Thus we cheat and pretend we already |
| 2851 | * have a few bytes there. |
| 2852 | */ |
| 2853 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2854 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2855 | } |
| 2856 | else |
| 2857 | max = b_room(buf); |
| 2858 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2859 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2860 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2861 | else |
| 2862 | ret = 0; |
| 2863 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2864 | |
Willy Tarreau | a8fcdac | 2019-08-02 07:48:47 +0200 | [diff] [blame] | 2865 | if (max && !ret && h2_recv_allowed(h2c)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2866 | 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] | 2867 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2868 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2869 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2870 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2871 | } |
| 2872 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2873 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2874 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2875 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2878 | /* Try to send data if possible. |
| 2879 | * The function returns 1 if data have been sent, otherwise zero. |
| 2880 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2881 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2882 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2883 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2884 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2885 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2886 | |
| 2887 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2888 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2889 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2890 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2891 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2892 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2893 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2894 | } |
| 2895 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2896 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2897 | * pending streams into the existing buffer until it's reportedly full |
| 2898 | * or the end of send requests is reached. Then it tries to send this |
| 2899 | * buffer's contents out, marks it not full if at least one byte could |
| 2900 | * be sent, and tries again. |
| 2901 | * |
| 2902 | * The snd_buf() function normally takes a "flags" argument which may |
| 2903 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2904 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2905 | * connection is streaming lots of data (used to increase TLS record |
| 2906 | * size at the expense of latency). The former can be sent any time |
| 2907 | * there's a buffer full flag, as it indicates at least one stream |
| 2908 | * attempted to send and failed so there are pending data. An |
| 2909 | * alternative would be to set it as long as there's an active stream |
| 2910 | * but that would be problematic for ACKs until we have an absolute |
| 2911 | * guarantee that all waiters have at least one byte to send. The |
| 2912 | * latter should possibly not be set for now. |
| 2913 | */ |
| 2914 | |
| 2915 | done = 0; |
| 2916 | while (!done) { |
| 2917 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2918 | unsigned int released = 0; |
| 2919 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2920 | |
| 2921 | /* fill as much as we can into the current buffer */ |
| 2922 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2923 | done = h2_process_mux(h2c); |
| 2924 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2925 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2926 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2927 | |
Christopher Faulet | 7163351 | 2020-10-22 16:24:58 +0200 | [diff] [blame] | 2928 | if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) || |
| 2929 | (h2c->st0 == H2_CS_ERROR2) || (h2c->flags & H2_CF_GOAWAY_FAILED)) |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2930 | break; |
| 2931 | |
| 2932 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2933 | flags |= CO_SFL_MSG_MORE; |
| 2934 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2935 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2936 | if (b_data(buf)) { |
| 2937 | int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2938 | if (!ret) { |
| 2939 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2940 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2941 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2942 | sent = 1; |
| 2943 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2944 | if (b_data(buf)) { |
| 2945 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2946 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2947 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2948 | } |
| 2949 | b_free(buf); |
| 2950 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2951 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2952 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2953 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2954 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2956 | /* wrote at least one byte, the buffer is not full anymore */ |
Christopher Faulet | 0742308 | 2019-10-24 10:31:01 +0200 | [diff] [blame] | 2957 | if (sent) |
| 2958 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2961 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2962 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2963 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2964 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2965 | /* We're not full anymore, so we can wake any task that are waiting |
| 2966 | * for us. |
| 2967 | */ |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2968 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) |
| 2969 | h2_resume_each_sending_h2s(h2c, &h2c->send_list); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2970 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2971 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2972 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2973 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2974 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2975 | if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2976 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2977 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2978 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2979 | } |
| 2980 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2981 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2982 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2983 | { |
| 2984 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2985 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2986 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2987 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2988 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2989 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2990 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2991 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2992 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2993 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2994 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2995 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2996 | /* callback called on any event by the connection handler. |
| 2997 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2998 | * destruction of the connection (which normally doesn not happen in h2). |
| 2999 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3000 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3001 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3002 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3003 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3004 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3005 | h2_process_demux(h2c); |
| 3006 | |
| 3007 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3008 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3009 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3010 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3011 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 3012 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3013 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3014 | |
Willy Tarreau | 318822b | 2020-10-13 18:09:15 +0200 | [diff] [blame] | 3015 | if (unlikely(h2c->proxy->state == PR_STSTOPPED) && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 3016 | /* frontend is stopping, reload likely in progress, let's try |
| 3017 | * to announce a graceful shutdown if not yet done. We don't |
| 3018 | * care if it fails, it will be tried again later. |
| 3019 | */ |
| 3020 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3021 | if (h2c->last_sid < 0) |
| 3022 | h2c->last_sid = (1U << 31) - 1; |
| 3023 | h2c_send_goaway_error(h2c, NULL); |
| 3024 | } |
| 3025 | } |
| 3026 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3027 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3028 | * If we received early data, and the handshake is done, wake |
| 3029 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3030 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3031 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 3032 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 3033 | struct eb32_node *node; |
| 3034 | struct h2s *h2s; |
| 3035 | |
| 3036 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 3037 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 3038 | |
| 3039 | while (node) { |
| 3040 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 3041 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 3042 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3043 | node = eb32_next(node); |
| 3044 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3045 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3046 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 3047 | if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 3048 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 3049 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 3050 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3051 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3052 | |
| 3053 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3054 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3055 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3056 | return -1; |
| 3057 | } |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3058 | |
| 3059 | /* connections in error must be removed from the idle lists */ |
| 3060 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3061 | LIST_DEL_LOCKED(&conn->list); |
| 3062 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3063 | } |
| 3064 | else if (h2c->st0 == H2_CS_ERROR) { |
| 3065 | /* connections in error must be removed from the idle lists */ |
| 3066 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3067 | LIST_DEL_LOCKED(&conn->list); |
| 3068 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3071 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3072 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3073 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3074 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3075 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 3076 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3077 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3078 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 3079 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 3080 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3081 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 3082 | if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3083 | if (h2c_may_expire(h2c)) |
| 3084 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3085 | else |
| 3086 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3087 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3088 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3089 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3090 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3091 | return 0; |
| 3092 | } |
| 3093 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3094 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3095 | static int h2_wake(struct connection *conn) |
| 3096 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3097 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3098 | |
| 3099 | return (h2_process(h2c)); |
| 3100 | } |
| 3101 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3102 | /* Connection timeout management. The principle is that if there's no receipt |
| 3103 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3104 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3105 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3106 | * GOAWAY frame. |
| 3107 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3108 | 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] | 3109 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3110 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3111 | int expired = tick_is_expired(t->expire, now_ms); |
| 3112 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3113 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3114 | return t; |
| 3115 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3116 | if (h2c && !h2c_may_expire(h2c)) { |
| 3117 | /* we do still have streams but all of them are idle, waiting |
| 3118 | * for the data layer, so we must not enforce the timeout here. |
| 3119 | */ |
| 3120 | t->expire = TICK_ETERNITY; |
| 3121 | return t; |
| 3122 | } |
| 3123 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3124 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3125 | |
| 3126 | if (!h2c) { |
| 3127 | /* resources were already deleted */ |
| 3128 | return NULL; |
| 3129 | } |
| 3130 | |
| 3131 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3132 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3133 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3134 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3135 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3136 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3137 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3138 | } |
| 3139 | |
| 3140 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3141 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3142 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3143 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3144 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3145 | 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] | 3146 | unsigned int released = 0; |
| 3147 | struct buffer *buf; |
| 3148 | |
| 3149 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3150 | if (b_data(buf)) { |
| 3151 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3152 | if (!ret) |
| 3153 | break; |
| 3154 | b_del(buf, ret); |
| 3155 | if (b_data(buf)) |
| 3156 | break; |
| 3157 | b_free(buf); |
| 3158 | released++; |
| 3159 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3160 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3161 | |
| 3162 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3163 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3164 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3165 | |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3166 | /* in any case this connection must not be considered idle anymore */ |
| 3167 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3168 | LIST_DEL_LOCKED(&h2c->conn->list); |
| 3169 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3170 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3171 | /* either we can release everything now or it will be done later once |
| 3172 | * the last stream closes. |
| 3173 | */ |
| 3174 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3175 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3176 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3177 | return NULL; |
| 3178 | } |
| 3179 | |
| 3180 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3181 | /*******************************************/ |
| 3182 | /* functions below are used by the streams */ |
| 3183 | /*******************************************/ |
| 3184 | |
| 3185 | /* |
| 3186 | * Attach a new stream to a connection |
| 3187 | * (Used for outgoing connections) |
| 3188 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3189 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3190 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3191 | struct conn_stream *cs; |
| 3192 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3193 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3194 | |
| 3195 | cs = cs_new(conn); |
| 3196 | if (!cs) |
| 3197 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3198 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3199 | if (!h2s) { |
| 3200 | cs_free(cs); |
| 3201 | return NULL; |
| 3202 | } |
| 3203 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3204 | } |
| 3205 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3206 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3207 | * We have to scan because we may have some orphan streams. It might be |
| 3208 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3209 | * orphans. |
| 3210 | */ |
| 3211 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3212 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3213 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3214 | struct h2s *h2s; |
| 3215 | struct eb32_node *node; |
| 3216 | |
| 3217 | node = eb32_first(&h2c->streams_by_id); |
| 3218 | while (node) { |
| 3219 | h2s = container_of(node, struct h2s, by_id); |
| 3220 | if (h2s->cs) |
| 3221 | return h2s->cs; |
| 3222 | node = eb32_next(node); |
| 3223 | } |
| 3224 | return NULL; |
| 3225 | } |
| 3226 | |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3227 | static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3228 | { |
| 3229 | int ret = 0; |
| 3230 | struct h2c *h2c = conn->ctx; |
| 3231 | |
| 3232 | switch (mux_ctl) { |
| 3233 | case MUX_STATUS: |
| 3234 | /* Only consider the mux to be ready if we're done with |
| 3235 | * the preface and settings, and we had no error. |
| 3236 | */ |
| 3237 | if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR) |
| 3238 | ret |= MUX_STATUS_READY; |
| 3239 | return ret; |
| 3240 | default: |
| 3241 | return -1; |
| 3242 | } |
| 3243 | } |
| 3244 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3245 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3246 | * Destroy the mux and the associated connection, if it is no longer used |
| 3247 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3248 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3249 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3250 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3251 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3252 | 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] | 3253 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3257 | * Detach the stream from the connection and possibly release the connection. |
| 3258 | */ |
| 3259 | static void h2_detach(struct conn_stream *cs) |
| 3260 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3261 | struct h2s *h2s = cs->ctx; |
| 3262 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3263 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3264 | |
| 3265 | cs->ctx = NULL; |
| 3266 | if (!h2s) |
| 3267 | return; |
| 3268 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3269 | /* 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] | 3270 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3271 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3272 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3273 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3274 | /* |
| 3275 | * At this point, the stream_interface is supposed to have called |
| 3276 | * h2_unsubscribe(), so the only way there's still a |
| 3277 | * subscription that came from the stream_interface (as we |
| 3278 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3279 | * without the stream_interface involved) is that we subscribed |
| 3280 | * for sending, we woke the tasklet up and removed the |
| 3281 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3282 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3283 | * run yet. Make sure to handle that by explicitely setting |
| 3284 | * send_wait to NULL, as nothing else will do it for us. |
| 3285 | */ |
| 3286 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3287 | } |
| 3288 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3289 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3290 | h2c = h2s->h2c; |
| 3291 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3292 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3293 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3294 | !h2_frt_has_too_many_cs(h2c)) { |
| 3295 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3296 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3297 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3298 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3299 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3300 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3301 | * an ES or RST frame), so orphan it in this case. |
| 3302 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3303 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3304 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3305 | (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] | 3306 | return; |
| 3307 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3308 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3309 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3310 | /* unblock the connection if it was blocked on this |
| 3311 | * stream. |
| 3312 | */ |
| 3313 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3314 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3315 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3316 | } |
| 3317 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3318 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3319 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3320 | if (h2c->flags & H2_CF_IS_BACK && |
| 3321 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3322 | if (!(h2c->conn->flags & |
| 3323 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3324 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3325 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3326 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3327 | h2c->conn->owner = NULL; |
| 3328 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3329 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3330 | /* The server doesn't want it, let's kill the connection right away */ |
Olivier Houchard | 109ba13 | 2020-02-14 13:23:45 +0100 | [diff] [blame] | 3331 | h2c->conn->mux->destroy(h2c); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3332 | return; |
| 3333 | } |
| 3334 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3335 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3336 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3337 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3338 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3339 | return; |
| 3340 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3341 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3342 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3343 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3344 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3345 | struct server *srv = objt_server(h2c->conn->target); |
| 3346 | |
| 3347 | if (srv) { |
| 3348 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3349 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3350 | else |
| 3351 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3352 | } |
| 3353 | |
| 3354 | } |
| 3355 | } |
| 3356 | } |
| 3357 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3358 | /* We don't want to close right now unless we're removing the |
| 3359 | * last stream, and either the connection is in error, or it |
| 3360 | * reached the ID already specified in a GOAWAY frame received |
| 3361 | * or sent (as seen by last_sid >= 0). |
| 3362 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3363 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3364 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3365 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3366 | } |
| 3367 | else if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3368 | if (h2c_may_expire(h2c)) |
| 3369 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3370 | else |
| 3371 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3372 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3373 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3376 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3377 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3378 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3379 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3380 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3381 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3382 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3383 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3384 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3385 | /* a connstream may require us to immediately kill the whole connection |
| 3386 | * for example because of a "tcp-request content reject" rule that is |
| 3387 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3388 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3389 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3390 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3391 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3392 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3393 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3394 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3395 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3396 | /* Nothing was never sent for this stream, so reset with |
| 3397 | * REFUSED_STREAM error to let the client retry the |
| 3398 | * request. |
| 3399 | */ |
| 3400 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3401 | } |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3402 | else { |
| 3403 | /* a final response was already provided, we don't want this |
| 3404 | * stream anymore. This may happen when the server responds |
| 3405 | * before the end of an upload and closes quickly (redirect, |
| 3406 | * deny, ...) |
| 3407 | */ |
| 3408 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3409 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3410 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3411 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3412 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3413 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3414 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3415 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3416 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3417 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3418 | done: |
| 3419 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3420 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3421 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3422 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3423 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3424 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3425 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3426 | h2s->send_wait = sw; |
| 3427 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3428 | h2s->send_wait = sw; |
| 3429 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3430 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3431 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3432 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3433 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3434 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3435 | } |
| 3436 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3437 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3438 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3439 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3440 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3441 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3442 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3443 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3444 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3445 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3446 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3447 | /* we can cleanly close using an empty data frame only after headers */ |
| 3448 | |
| 3449 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3450 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3451 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3452 | |
| 3453 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3454 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3455 | else |
| 3456 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3457 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3458 | /* a connstream may require us to immediately kill the whole connection |
| 3459 | * for example because of a "tcp-request content reject" rule that is |
| 3460 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3461 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3462 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3463 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3464 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3465 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3466 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3467 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3468 | else { |
| 3469 | /* Nothing was never sent for this stream, so reset with |
| 3470 | * REFUSED_STREAM error to let the client retry the |
| 3471 | * request. |
| 3472 | */ |
| 3473 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3474 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3475 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3476 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3477 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3478 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3479 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3480 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3481 | } |
| 3482 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3483 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3484 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3485 | done: |
| 3486 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3487 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3488 | |
| 3489 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3490 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3491 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3492 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3493 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3494 | h2s->send_wait = sw; |
| 3495 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3496 | h2s->send_wait = sw; |
| 3497 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3498 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3499 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3500 | /* let the handler know we want to shutw */ |
| 3501 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3502 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3503 | } |
| 3504 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3505 | /* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3506 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3507 | * and prevented the last frame from being emitted. |
| 3508 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3509 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3510 | { |
| 3511 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3512 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3513 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3514 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3515 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3516 | h2_do_shutw(h2s); |
| 3517 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3518 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3519 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3520 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3521 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3522 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3523 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3524 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3525 | if (!h2s->cs) { |
| 3526 | h2s_destroy(h2s); |
| 3527 | if (h2c_is_dead(h2c)) |
| 3528 | h2_release(h2c); |
| 3529 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3530 | } |
| 3531 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3532 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3533 | } |
| 3534 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3535 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3536 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3537 | { |
| 3538 | struct h2s *h2s = cs->ctx; |
| 3539 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3540 | if (cs->flags & CS_FL_KILL_CONN) |
| 3541 | h2s->flags |= H2_SF_KILL_CONN; |
| 3542 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3543 | if (!mode) |
| 3544 | return; |
| 3545 | |
| 3546 | h2_do_shutr(h2s); |
| 3547 | } |
| 3548 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3549 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3550 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3551 | { |
| 3552 | struct h2s *h2s = cs->ctx; |
| 3553 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3554 | if (cs->flags & CS_FL_KILL_CONN) |
| 3555 | h2s->flags |= H2_SF_KILL_CONN; |
| 3556 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3557 | h2_do_shutw(h2s); |
| 3558 | } |
| 3559 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3560 | /* 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] | 3561 | * HTX request or response depending on the connection's side. Returns a |
| 3562 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3563 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3564 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3565 | * 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] | 3566 | * |
| 3567 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3568 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3569 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3570 | * leaving a hole between the main frame and the beginning of the next one. |
| 3571 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3572 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3573 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3574 | * |
| 3575 | * A buffer at the beginning of processing may look like this : |
| 3576 | * |
| 3577 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3578 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3579 | * `---^---------^-----^--------------^--------------^------^---' |
| 3580 | * | | <-----> | | |
| 3581 | * area | dpl | wrap |
| 3582 | * |<--------------> | |
| 3583 | * | dfl | |
| 3584 | * |<-------------------------------------------------->| |
| 3585 | * head data |
| 3586 | * |
| 3587 | * Padding is automatically overwritten when folding, participating to the |
| 3588 | * hole size after dfl : |
| 3589 | * |
| 3590 | * ,---.------------------------.-----.--------------.------.---. |
| 3591 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3592 | * `---^------------------------^-----^--------------^------^---' |
| 3593 | * | | <-----> | | |
| 3594 | * area | hole | wrap |
| 3595 | * |<-----------------------> | |
| 3596 | * | dfl | |
| 3597 | * |<-------------------------------------------------->| |
| 3598 | * head data |
| 3599 | * |
| 3600 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3601 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3602 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3603 | * |
| 3604 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3605 | * so that the function can update the following flags : |
| 3606 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3607 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3608 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3609 | * |
| 3610 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3611 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3612 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3613 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3614 | 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] | 3615 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3616 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3617 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3618 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3619 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3620 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3621 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3622 | int flen; // header frame len |
| 3623 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3624 | int ret = 0; |
| 3625 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3626 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3627 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3628 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3629 | next_frame: |
| 3630 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3631 | goto leave; // incomplete input frame |
| 3632 | |
| 3633 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3634 | * this case, we'll try to paste it immediately after the initial |
| 3635 | * HEADERS frame payload and kill any possible padding. The initial |
| 3636 | * frame's length will be increased to represent the concatenation |
| 3637 | * of the two frames. The next frame is read from position <tlen> |
| 3638 | * and written at position <flen> (minus padding if some is present). |
| 3639 | */ |
| 3640 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3641 | struct h2_fh hdr; |
| 3642 | int clen; // CONTINUATION frame's payload length |
| 3643 | |
| 3644 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3645 | /* no more data, the buffer may be full, either due to |
| 3646 | * too large a frame or because of too large a hole that |
| 3647 | * we're going to compact at the end. |
| 3648 | */ |
| 3649 | goto leave; |
| 3650 | } |
| 3651 | |
| 3652 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3653 | /* RFC7540#6.10: frame of unexpected type */ |
| 3654 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3655 | goto fail; |
| 3656 | } |
| 3657 | |
| 3658 | if (hdr.sid != h2c->dsi) { |
| 3659 | /* RFC7540#6.10: frame of different stream */ |
| 3660 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3661 | goto fail; |
| 3662 | } |
| 3663 | |
| 3664 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3665 | /* RFC7540#4.2: invalid frame length */ |
| 3666 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3667 | goto fail; |
| 3668 | } |
| 3669 | |
| 3670 | /* detect when we must stop aggragating frames */ |
| 3671 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3672 | |
| 3673 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3674 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3675 | if (clen > hdr.len) |
| 3676 | clen = hdr.len; |
| 3677 | |
| 3678 | /* Move the frame's payload over the padding, hole and frame |
| 3679 | * header. At least one of hole or dpl is null (see diagrams |
| 3680 | * above). The hole moves after the new aggragated frame. |
| 3681 | */ |
| 3682 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3683 | h2c->dfl += clen - h2c->dpl; |
| 3684 | hole += h2c->dpl + 9; |
| 3685 | h2c->dpl = 0; |
| 3686 | goto next_frame; |
| 3687 | } |
| 3688 | |
| 3689 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3690 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3691 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3692 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3693 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3694 | copy = alloc_trash_chunk(); |
| 3695 | if (!copy) { |
| 3696 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3697 | goto fail; |
| 3698 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3699 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3700 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3701 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3702 | } |
| 3703 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3704 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3705 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3706 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3707 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3708 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3709 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3710 | } |
| 3711 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3712 | if (flen < 5) { |
| 3713 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3714 | goto fail; |
| 3715 | } |
| 3716 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3717 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3718 | flen -= 5; |
| 3719 | } |
| 3720 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3721 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3722 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3723 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3724 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3725 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3726 | /* we can't retry a failed decompression operation so we must be very |
| 3727 | * careful not to take any risks. In practice the output buffer is |
| 3728 | * always empty except maybe for trailers, in which case we simply have |
| 3729 | * to wait for the upper layer to finish consuming what is available. |
| 3730 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3731 | |
| 3732 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3733 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3734 | if (!htx_is_empty(htx)) { |
| 3735 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3736 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3737 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3738 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3739 | if (b_data(rxbuf)) { |
| 3740 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3741 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3742 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3743 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3744 | rxbuf->head = 0; |
| 3745 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3746 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3747 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3748 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3749 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3750 | sizeof(list)/sizeof(list[0]), tmp); |
| 3751 | if (outlen < 0) { |
| 3752 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3753 | goto fail; |
| 3754 | } |
| 3755 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3756 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3757 | * the parser's state to commit these changes and allow us to later |
| 3758 | * fail solely on the stream if needed. |
| 3759 | */ |
| 3760 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3761 | h2c->dfl = hole = 0; |
| 3762 | h2c->st0 = H2_CS_FRAME_H; |
| 3763 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3764 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3765 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3766 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3767 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3768 | goto trailers; |
| 3769 | |
| 3770 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3771 | if (htx) { |
| 3772 | /* HTX mode */ |
| 3773 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3774 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3775 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3776 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3777 | } else { |
| 3778 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3779 | 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] | 3780 | if (outlen > 0) |
| 3781 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3782 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3783 | |
| 3784 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3785 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3786 | goto fail; |
| 3787 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3788 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3789 | if (msgf & H2_MSGF_BODY) { |
| 3790 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3791 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3792 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3793 | if (htx) |
| 3794 | htx->extra = *body_len; |
| 3795 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3796 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3797 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3798 | } |
| 3799 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3800 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3801 | /* indicate that a HEADERS frame was received for this stream, except |
| 3802 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3803 | * expected. |
| 3804 | */ |
| 3805 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3806 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3807 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3808 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3809 | /* Mark the end of message, either using EOM in HTX or with the |
| 3810 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3811 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3812 | * we must not leave an HTX trailers block not followed by an |
| 3813 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3814 | * 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] | 3815 | */ |
| 3816 | if (htx) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 3817 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3818 | goto fail; |
| 3819 | } |
| 3820 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3821 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3822 | goto fail; |
| 3823 | } |
| 3824 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3825 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3826 | /* success */ |
| 3827 | ret = 1; |
| 3828 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3829 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3830 | /* 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] | 3831 | * move the remaining data over it. |
| 3832 | */ |
| 3833 | if (hole) { |
| 3834 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3835 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3836 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3837 | b_sub(&h2c->dbuf, hole); |
| 3838 | } |
| 3839 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3840 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3841 | /* too large frames */ |
| 3842 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3843 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3844 | } |
| 3845 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3846 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3847 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3848 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3849 | return ret; |
| 3850 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3851 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3852 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3853 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3854 | |
| 3855 | trailers: |
| 3856 | /* This is the last HEADERS frame hence a trailer */ |
| 3857 | |
| 3858 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3859 | /* It's a trailer but it's missing ES flag */ |
| 3860 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3861 | goto fail; |
| 3862 | } |
| 3863 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 3864 | /* Trailers terminate a DATA sequence. In HTX we always handle them. In |
| 3865 | * legacy, when using chunks, we have to emit the 0 CRLF marker first |
| 3866 | * and then handle the trailers. For other modes, the trailers are |
| 3867 | * silently dropped. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3868 | */ |
| 3869 | if (htx) { |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3870 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3871 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3872 | } |
| 3873 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3874 | /* Legacy mode with chunked encoding : we must finalize the |
| 3875 | * data block message emit the trailing CRLF */ |
| 3876 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3877 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3878 | |
| 3879 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3880 | if (outlen > 0) |
| 3881 | b_add(rxbuf, outlen); |
| 3882 | else |
| 3883 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3889 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3890 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3891 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3892 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3893 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3894 | * 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] | 3895 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3896 | * send the current frame, 0 if it couldn't complete, in which case |
| 3897 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3898 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3899 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3900 | * have checked the frame header and ensured that the frame was complete or the |
| 3901 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3902 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3903 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3904 | { |
| 3905 | struct h2c *h2c = h2s->h2c; |
| 3906 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3907 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3908 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3909 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3910 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3911 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3912 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3913 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3914 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3915 | if (!csbuf) { |
| 3916 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3917 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3918 | } |
| 3919 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3920 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3921 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3922 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3923 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3924 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3925 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3926 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3927 | if (flen > b_data(&h2c->dbuf)) { |
| 3928 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3929 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3930 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3931 | } |
| 3932 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3933 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3934 | unsigned int sent; |
| 3935 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3936 | block1 = htx_free_data_space(htx); |
| 3937 | if (!block1) { |
| 3938 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3939 | goto fail; |
| 3940 | } |
| 3941 | if (flen > block1) |
| 3942 | flen = block1; |
| 3943 | |
| 3944 | /* here, flen is the max we can copy into the output buffer */ |
| 3945 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3946 | if (flen > block1) |
| 3947 | flen = block1; |
| 3948 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3949 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3950 | |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3951 | b_del(&h2c->dbuf, sent); |
| 3952 | h2c->dfl -= sent; |
| 3953 | h2c->rcvd_c += sent; |
| 3954 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3955 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3956 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3957 | h2s->body_len -= sent; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3958 | htx->extra = h2s->body_len; |
| 3959 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3960 | |
| 3961 | if (sent < flen) { |
| 3962 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3963 | goto fail; |
| 3964 | } |
| 3965 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3966 | goto try_again; |
| 3967 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3968 | else if (unlikely(b_space_wraps(csbuf) && |
| 3969 | flen + chklen <= b_room(csbuf) && |
| 3970 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3971 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3972 | * not too many data in the buffer, let's defragment it and try |
| 3973 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3974 | */ |
| 3975 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3976 | } |
| 3977 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3978 | /* chunked-encoding requires more room */ |
| 3979 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3980 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3981 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3982 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3983 | (chklen < 1048576) ? 4 : 8; |
| 3984 | chklen += 4; // CRLF, CRLF |
| 3985 | } |
| 3986 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3987 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3988 | if (flen + chklen > b_room(csbuf)) { |
| 3989 | if (chklen >= b_room(csbuf)) { |
| 3990 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3991 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3992 | } |
| 3993 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3997 | /* emit the chunk size */ |
| 3998 | unsigned int chksz = flen; |
| 3999 | char str[10]; |
| 4000 | char *beg; |
| 4001 | |
| 4002 | beg = str + sizeof(str); |
| 4003 | *--beg = '\n'; |
| 4004 | *--beg = '\r'; |
| 4005 | do { |
| 4006 | *--beg = hextab[chksz & 0xF]; |
| 4007 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4008 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4009 | } |
| 4010 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4011 | /* Block1 is the length of the first block before the buffer wraps, |
| 4012 | * block2 is the optional second block to reach the end of the frame. |
| 4013 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4014 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4015 | if (block1 > flen) |
| 4016 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4017 | block2 = flen - block1; |
| 4018 | |
| 4019 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4020 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4021 | |
| 4022 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4023 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4024 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4025 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4026 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4027 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4030 | /* now mark the input data as consumed (will be deleted from the buffer |
| 4031 | * by the caller when seeing FRAME_A after sending the window update). |
| 4032 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4033 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4034 | h2c->dfl -= flen; |
| 4035 | h2c->rcvd_c += flen; |
| 4036 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 4037 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 4038 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 4039 | h2s->body_len -= flen; |
| 4040 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4041 | if (h2c->dfl > h2c->dpl) { |
| 4042 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4043 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4044 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4045 | } |
| 4046 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 4047 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4048 | /* here we're done with the frame, all the payload (except padding) was |
| 4049 | * transferred. |
| 4050 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4051 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4052 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 4053 | if (htx) { |
| 4054 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 4055 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4056 | goto fail; |
| 4057 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4058 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4059 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4060 | /* emit the trailing 0 CRLF CRLF */ |
| 4061 | if (b_room(csbuf) < 5) { |
| 4062 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4063 | goto fail; |
| 4064 | } |
| 4065 | chklen += 5; |
| 4066 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 4067 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 4070 | h2c->rcvd_c += h2c->dpl; |
| 4071 | h2c->rcvd_s += h2c->dpl; |
| 4072 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4073 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4074 | if (htx) |
| 4075 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4076 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4077 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4078 | if (htx) |
| 4079 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4080 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4081 | } |
| 4082 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4083 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 4084 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 4085 | * number of bytes sent. The caller must check the stream's status to detect |
| 4086 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4087 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4088 | 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] | 4089 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4090 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4091 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4092 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4093 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4094 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4095 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4096 | int es_now = 0; |
| 4097 | int ret = 0; |
| 4098 | int hdr; |
| 4099 | |
| 4100 | if (h2c_mux_busy(h2c, h2s)) { |
| 4101 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4102 | return 0; |
| 4103 | } |
| 4104 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4105 | /* First, try to parse the H1 response and index it into <list>. |
| 4106 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 4107 | * block does not wrap and we can safely read it this way without |
| 4108 | * having to realign the buffer. |
| 4109 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4110 | 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] | 4111 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4112 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 4113 | /* incomplete or invalid response, this is abnormal coming from |
| 4114 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 4115 | * so that won't be fixed, raise an error now. |
| 4116 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4117 | * FIXME: we should instead add the ability to only return a |
| 4118 | * 502 bad gateway. But in theory this is not supposed to |
| 4119 | * happen. |
| 4120 | */ |
| 4121 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4122 | ret = 0; |
| 4123 | goto end; |
| 4124 | } |
| 4125 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4126 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4127 | |
| 4128 | /* certain statuses have no body or an empty one, regardless of |
| 4129 | * what the headers say. |
| 4130 | */ |
| 4131 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4132 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4133 | h1m->curr_len = h1m->body_len = 0; |
| 4134 | } |
| 4135 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4136 | /* no contents, claim c-len is present and set to zero */ |
| 4137 | h1m->flags &= ~H1_MF_CHNK; |
| 4138 | h1m->flags |= H1_MF_CLEN; |
| 4139 | h1m->curr_len = h1m->body_len = 0; |
| 4140 | } |
| 4141 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4142 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4143 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4144 | if (!h2_get_buf(h2c, mbuf)) { |
| 4145 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4146 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4147 | return 0; |
| 4148 | } |
| 4149 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4150 | chunk_reset(&outbuf); |
| 4151 | |
| 4152 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4153 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4154 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4155 | break; |
| 4156 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4157 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4158 | } |
| 4159 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4160 | if (outbuf.size < 9) |
| 4161 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4162 | |
| 4163 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4164 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4165 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4166 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4167 | |
| 4168 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4169 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4170 | /* this is an unparsable response */ |
| 4171 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4172 | ret = 0; |
| 4173 | goto end; |
| 4174 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4175 | |
| 4176 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4177 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4178 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4179 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | /* encode all headers, stop at empty name */ |
| 4183 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4184 | /* these ones do not exist in H2 and must be dropped. */ |
| 4185 | if (isteq(list[hdr].n, ist("connection")) || |
| 4186 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4187 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4188 | isteq(list[hdr].n, ist("upgrade")) || |
| 4189 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4190 | continue; |
| 4191 | |
| 4192 | if (isteq(list[hdr].n, ist(""))) |
| 4193 | break; // end |
| 4194 | |
| 4195 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4196 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4197 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4198 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4199 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4204 | 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] | 4205 | es_now = 1; |
| 4206 | |
| 4207 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4208 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4209 | |
| 4210 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4211 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4212 | |
| 4213 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4214 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4215 | |
| 4216 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4217 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4218 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4219 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4220 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4221 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4222 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4223 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4224 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4225 | h2s->flags |= H2_SF_ES_SENT; |
| 4226 | if (h2s->st == H2_SS_OPEN) |
| 4227 | h2s->st = H2_SS_HLOC; |
| 4228 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4229 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4230 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4231 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4232 | /* 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] | 4233 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4234 | 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] | 4235 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4236 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4237 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4238 | |
| 4239 | /* 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] | 4240 | |
| 4241 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4242 | //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] | 4243 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4244 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4245 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4246 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4247 | h1m_init_res(h1m); |
| 4248 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4249 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4250 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4251 | ret = 0; |
| 4252 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4253 | } |
| 4254 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4255 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4256 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4257 | * the number of bytes sent. The caller must check the stream's status to |
| 4258 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4259 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4260 | 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] | 4261 | { |
| 4262 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4263 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4264 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4265 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4266 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4267 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4268 | int es_now = 0; |
| 4269 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4270 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4271 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4272 | |
| 4273 | if (h2c_mux_busy(h2c, h2s)) { |
| 4274 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4275 | goto end; |
| 4276 | } |
| 4277 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4278 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4279 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4280 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4281 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4282 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4283 | goto end; |
| 4284 | } |
| 4285 | |
| 4286 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4287 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4288 | goto end; |
| 4289 | |
| 4290 | chunk_reset(&outbuf); |
| 4291 | |
| 4292 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4293 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4294 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4295 | break; |
| 4296 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4297 | /* If there are pending data in the output buffer, and we have |
| 4298 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4299 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4300 | * is full and wait, to save some slow realign calls. |
| 4301 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4302 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4303 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4304 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4305 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4306 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4307 | goto end; |
| 4308 | } |
| 4309 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4310 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4314 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4315 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4316 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4317 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4318 | goto end; |
| 4319 | } |
| 4320 | |
| 4321 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4322 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4323 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4324 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4325 | |
| 4326 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4327 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4328 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4329 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4330 | break; |
| 4331 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4332 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4333 | if ((long long)size > h1m->curr_len) |
| 4334 | size = h1m->curr_len; |
| 4335 | break; |
| 4336 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4337 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4338 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 4339 | if (ret <= 0) { |
Christopher Faulet | 307f31e | 2020-08-05 14:37:13 +0200 | [diff] [blame] | 4340 | /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ? |
| 4341 | * It should not happen except if opposite connection was closed. |
| 4342 | * Note: check there is at least something to parse to be sure the chunk crlf |
| 4343 | * is truncated. |
| 4344 | */ |
| 4345 | if (max) { |
| 4346 | h1m->err_pos = ofs + max + ret; |
| 4347 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4348 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4349 | goto end; |
| 4350 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4351 | max -= ret; |
| 4352 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4353 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4354 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4355 | } |
| 4356 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4357 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4358 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4359 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 4360 | if (ret <= 0) { |
Christopher Faulet | 307f31e | 2020-08-05 14:37:13 +0200 | [diff] [blame] | 4361 | /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ? |
| 4362 | * It should not happen except if opposite connection was closed. |
| 4363 | * Note: check there is at least something to parse to be sure the chunke size |
| 4364 | * is truncated. |
| 4365 | */ |
| 4366 | if (max) { |
| 4367 | h1m->err_pos = ofs + max + ret; |
| 4368 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4369 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4370 | goto end; |
| 4371 | } |
| 4372 | |
| 4373 | size = chunk; |
| 4374 | h1m->curr_len = chunk; |
| 4375 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4376 | max -= ret; |
| 4377 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4378 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4379 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4380 | if (!size) |
| 4381 | goto send_empty; |
| 4382 | } |
| 4383 | |
| 4384 | /* in MSG_DATA state, continue below */ |
| 4385 | size = h1m->curr_len; |
| 4386 | break; |
| 4387 | } |
| 4388 | |
| 4389 | /* we have in <size> the exact number of bytes we need to copy from |
| 4390 | * the H1 buffer. We need to check this against the connection's and |
| 4391 | * the stream's send windows, and to ensure that this fits in the max |
| 4392 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4393 | * the frame header). The connection's flow control is applied last so |
| 4394 | * that we can use a separate list of streams which are immediately |
| 4395 | * unblocked on window opening. Note: we don't implement padding. |
| 4396 | */ |
| 4397 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4398 | if (size > max) |
| 4399 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4400 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4401 | if (size > h2s_mws(h2s)) |
| 4402 | size = h2s_mws(h2s); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4403 | |
| 4404 | if (size <= 0) { |
| 4405 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4406 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4407 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 55dc084 | 2019-10-22 10:04:39 +0200 | [diff] [blame] | 4408 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4409 | goto end; |
| 4410 | } |
| 4411 | |
| 4412 | if (h2c->mfs && size > h2c->mfs) |
| 4413 | size = h2c->mfs; |
| 4414 | |
| 4415 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4416 | /* It doesn't fit at once. If it at least fits once split and |
| 4417 | * the amount of data to move is low, let's defragment the |
| 4418 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4419 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4420 | if (b_space_wraps(mbuf) && |
| 4421 | (size + 9 <= b_room(mbuf)) && |
| 4422 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4423 | goto realign_again; |
| 4424 | size = outbuf.size - 9; |
| 4425 | } |
| 4426 | |
| 4427 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4428 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4429 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4430 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4431 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4432 | goto end; |
| 4433 | } |
| 4434 | |
| 4435 | if (size > h2c->mws) |
| 4436 | size = h2c->mws; |
| 4437 | |
| 4438 | if (size <= 0) { |
| 4439 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4440 | goto end; |
| 4441 | } |
| 4442 | |
| 4443 | /* copy whatever we can */ |
| 4444 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4445 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4446 | if (ret == 1) |
| 4447 | len2 = 0; |
| 4448 | |
| 4449 | if (!ret || len1 + len2 < size) { |
| 4450 | /* FIXME: must normally never happen */ |
| 4451 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4452 | goto end; |
| 4453 | } |
| 4454 | |
| 4455 | /* limit len1/len2 to size */ |
| 4456 | if (len1 + len2 > size) { |
| 4457 | int sub = len1 + len2 - size; |
| 4458 | |
| 4459 | if (len2 > sub) |
| 4460 | len2 -= sub; |
| 4461 | else { |
| 4462 | sub -= len2; |
| 4463 | len2 = 0; |
| 4464 | len1 -= sub; |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4469 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4470 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4471 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4472 | |
| 4473 | send_empty: |
| 4474 | /* we may need to add END_STREAM */ |
| 4475 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4476 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4477 | * |
| 4478 | * FIXME: what we do here is not correct because we send end_stream |
| 4479 | * before knowing if we'll have to send a HEADERS frame for the |
| 4480 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4481 | * after the end of trailers, so it will be left to the caller to |
| 4482 | * eat it. The right way to do it would be to measure trailers here |
| 4483 | * and to send ES only if there are no trailers. |
| 4484 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4485 | */ |
| 4486 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4487 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4488 | es_now = 1; |
| 4489 | |
| 4490 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4491 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4492 | |
| 4493 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4494 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4495 | |
| 4496 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4497 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4498 | |
| 4499 | /* consume incoming H1 response */ |
| 4500 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4501 | max -= size; |
| 4502 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4503 | total += size; |
| 4504 | h1m->curr_len -= size; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4505 | h2s->sws -= size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4506 | h2c->mws -= size; |
| 4507 | |
| 4508 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4509 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4510 | goto new_frame; |
| 4511 | } |
| 4512 | } |
| 4513 | |
| 4514 | if (es_now) { |
| 4515 | if (h2s->st == H2_SS_OPEN) |
| 4516 | h2s->st = H2_SS_HLOC; |
| 4517 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4518 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4519 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4520 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4521 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4522 | total += max; |
| 4523 | ofs += max; |
| 4524 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4525 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4526 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4527 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4528 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4529 | h2s->flags |= H2_SF_ES_SENT; |
| 4530 | } |
| 4531 | |
| 4532 | end: |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4533 | 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(h2s), (unsigned int)b_data(buf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4534 | return total; |
| 4535 | } |
| 4536 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4537 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4538 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4539 | * must check the stream's status to detect any error which might have happened |
| 4540 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4541 | * from the message. The htx message is assumed to be valid since produced from |
| 4542 | * the internal code, hence it contains a start line, an optional series of |
| 4543 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4544 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4545 | */ |
| 4546 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4547 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4548 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4549 | struct h2c *h2c = h2s->h2c; |
| 4550 | struct htx_blk *blk; |
| 4551 | struct htx_blk *blk_end; |
| 4552 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4553 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4554 | struct htx_sl *sl; |
| 4555 | enum htx_blk_type type; |
| 4556 | int es_now = 0; |
| 4557 | int ret = 0; |
| 4558 | int hdr; |
| 4559 | int idx; |
| 4560 | |
| 4561 | if (h2c_mux_busy(h2c, h2s)) { |
| 4562 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4563 | return 0; |
| 4564 | } |
| 4565 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4566 | /* determine the first block which must not be deleted, blk_end may |
| 4567 | * be NULL if all blocks have to be deleted. |
| 4568 | */ |
| 4569 | idx = htx_get_head(htx); |
| 4570 | blk_end = NULL; |
| 4571 | while (idx != -1) { |
| 4572 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4573 | idx = htx_get_next(htx, idx); |
| 4574 | if (type == HTX_BLK_EOH) { |
| 4575 | if (idx != -1) |
| 4576 | blk_end = htx_get_blk(htx, idx); |
| 4577 | break; |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4582 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4583 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4584 | ALREADY_CHECKED(blk); |
| 4585 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4586 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4587 | if (h2s->status < 100 || h2s->status > 999) |
| 4588 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4589 | |
| 4590 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4591 | hdr = 0; |
| 4592 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4593 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4594 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4595 | blk = htx_get_blk(htx, idx); |
| 4596 | type = htx_get_blk_type(blk); |
| 4597 | |
| 4598 | if (type == HTX_BLK_UNUSED) |
| 4599 | continue; |
| 4600 | |
| 4601 | if (type != HTX_BLK_HDR) |
| 4602 | break; |
| 4603 | |
| 4604 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4605 | goto fail; |
| 4606 | |
| 4607 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4608 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4609 | hdr++; |
| 4610 | } |
| 4611 | |
| 4612 | /* marker for end of headers */ |
| 4613 | list[hdr].n = ist(""); |
| 4614 | |
| 4615 | if (h2s->status == 204 || h2s->status == 304) { |
| 4616 | /* no contents, claim c-len is present and set to zero */ |
| 4617 | es_now = 1; |
| 4618 | } |
| 4619 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4620 | mbuf = br_tail(h2c->mbuf); |
| 4621 | retry: |
| 4622 | if (!h2_get_buf(h2c, mbuf)) { |
| 4623 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4624 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4625 | return 0; |
| 4626 | } |
| 4627 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4628 | chunk_reset(&outbuf); |
| 4629 | |
| 4630 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4631 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4632 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4633 | break; |
| 4634 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4635 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | if (outbuf.size < 9) |
| 4639 | goto full; |
| 4640 | |
| 4641 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4642 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4643 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4644 | outbuf.data = 9; |
| 4645 | |
| 4646 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4647 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4648 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4649 | goto realign_again; |
| 4650 | goto full; |
| 4651 | } |
| 4652 | |
| 4653 | /* encode all headers, stop at empty name */ |
| 4654 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4655 | /* these ones do not exist in H2 and must be dropped. */ |
| 4656 | if (isteq(list[hdr].n, ist("connection")) || |
| 4657 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4658 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4659 | isteq(list[hdr].n, ist("upgrade")) || |
| 4660 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4661 | continue; |
| 4662 | |
| 4663 | if (isteq(list[hdr].n, ist(""))) |
| 4664 | break; // end |
| 4665 | |
| 4666 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4667 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4668 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4669 | goto realign_again; |
| 4670 | goto full; |
| 4671 | } |
| 4672 | } |
| 4673 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4674 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4675 | * FIXME: we should also set it when we know for sure that the |
| 4676 | * content-length is zero as well as on 204/304 |
| 4677 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4678 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4679 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4680 | es_now = 1; |
| 4681 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4682 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4683 | es_now = 1; |
| 4684 | |
| 4685 | /* update the frame's size */ |
| 4686 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4687 | |
| 4688 | if (es_now) |
| 4689 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4690 | |
| 4691 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4692 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4693 | |
| 4694 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4695 | * 1xx responses, another HEADERS frame is expected. |
| 4696 | */ |
| 4697 | if (h2s->status >= 200 || h2s->status == 101) |
| 4698 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4699 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4700 | if (es_now) { |
| 4701 | h2s->flags |= H2_SF_ES_SENT; |
| 4702 | if (h2s->st == H2_SS_OPEN) |
| 4703 | h2s->st = H2_SS_HLOC; |
| 4704 | else |
| 4705 | h2s_close(h2s); |
| 4706 | } |
| 4707 | |
| 4708 | /* OK we could properly deliver the response */ |
| 4709 | |
| 4710 | /* remove all header blocks including the EOH and compute the |
| 4711 | * corresponding size. |
| 4712 | * |
| 4713 | * FIXME: We should remove everything when es_now is set. |
| 4714 | */ |
| 4715 | ret = 0; |
| 4716 | idx = htx_get_head(htx); |
| 4717 | blk = htx_get_blk(htx, idx); |
| 4718 | while (blk != blk_end) { |
| 4719 | ret += htx_get_blksz(blk); |
| 4720 | blk = htx_remove_blk(htx, blk); |
| 4721 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4722 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4723 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4724 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4725 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4726 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4727 | end: |
| 4728 | return ret; |
| 4729 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4730 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4731 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4732 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4733 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4734 | ret = 0; |
| 4735 | goto end; |
| 4736 | fail: |
| 4737 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4738 | * list etc go here (unrecoverable errors). |
| 4739 | */ |
| 4740 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4741 | ret = 0; |
| 4742 | goto end; |
| 4743 | } |
| 4744 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4745 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4746 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4747 | * must check the stream's status to detect any error which might have happened |
| 4748 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4749 | * from the message. The htx message is assumed to be valid since produced from |
| 4750 | * the internal code, hence it contains a start line, an optional series of |
| 4751 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4752 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4753 | */ |
| 4754 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4755 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4756 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4757 | struct h2c *h2c = h2s->h2c; |
| 4758 | struct htx_blk *blk; |
| 4759 | struct htx_blk *blk_end; |
| 4760 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4761 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4762 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4763 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4764 | enum htx_blk_type type; |
| 4765 | int es_now = 0; |
| 4766 | int ret = 0; |
| 4767 | int hdr; |
| 4768 | int idx; |
| 4769 | |
| 4770 | if (h2c_mux_busy(h2c, h2s)) { |
| 4771 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4772 | return 0; |
| 4773 | } |
| 4774 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4775 | /* determine the first block which must not be deleted, blk_end may |
| 4776 | * be NULL if all blocks have to be deleted. |
| 4777 | */ |
| 4778 | idx = htx_get_head(htx); |
| 4779 | blk_end = NULL; |
| 4780 | while (idx != -1) { |
| 4781 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4782 | idx = htx_get_next(htx, idx); |
| 4783 | if (type == HTX_BLK_EOH) { |
| 4784 | if (idx != -1) |
| 4785 | blk_end = htx_get_blk(htx, idx); |
| 4786 | break; |
| 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4791 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4792 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4793 | ALREADY_CHECKED(blk); |
| 4794 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4795 | meth = htx_sl_req_meth(sl); |
| 4796 | path = htx_sl_req_uri(sl); |
| 4797 | |
| 4798 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4799 | hdr = 0; |
| 4800 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4801 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4802 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4803 | blk = htx_get_blk(htx, idx); |
| 4804 | type = htx_get_blk_type(blk); |
| 4805 | |
| 4806 | if (type == HTX_BLK_UNUSED) |
| 4807 | continue; |
| 4808 | |
| 4809 | if (type != HTX_BLK_HDR) |
| 4810 | break; |
| 4811 | |
| 4812 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4813 | goto fail; |
| 4814 | |
| 4815 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4816 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4817 | hdr++; |
| 4818 | } |
| 4819 | |
| 4820 | /* marker for end of headers */ |
| 4821 | list[hdr].n = ist(""); |
| 4822 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4823 | mbuf = br_tail(h2c->mbuf); |
| 4824 | retry: |
| 4825 | if (!h2_get_buf(h2c, mbuf)) { |
| 4826 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4827 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4828 | return 0; |
| 4829 | } |
| 4830 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4831 | chunk_reset(&outbuf); |
| 4832 | |
| 4833 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4834 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4835 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4836 | break; |
| 4837 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4838 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | if (outbuf.size < 9) |
| 4842 | goto full; |
| 4843 | |
| 4844 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4845 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4846 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4847 | outbuf.data = 9; |
| 4848 | |
| 4849 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4850 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4851 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4852 | goto realign_again; |
| 4853 | goto full; |
| 4854 | } |
| 4855 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4856 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4857 | * - :authority set to the URI part (host:port) |
| 4858 | * - :method set to CONNECT |
| 4859 | * - :scheme and :path omitted |
| 4860 | */ |
| 4861 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4862 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4863 | struct ist scheme; |
| 4864 | |
| 4865 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4866 | scheme = ist("http"); |
| 4867 | else |
| 4868 | scheme = ist("https"); |
| 4869 | |
| 4870 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4871 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4872 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4873 | goto realign_again; |
| 4874 | goto full; |
| 4875 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4876 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4877 | /* encode the path, which necessarily is the second one */ |
| 4878 | if (!hpack_encode_path(&outbuf, path)) { |
| 4879 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4880 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4881 | goto realign_again; |
| 4882 | goto full; |
| 4883 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4884 | |
| 4885 | /* look for the Host header and place it in :authority */ |
| 4886 | auth = ist2(NULL, 0); |
| 4887 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4888 | if (isteq(list[hdr].n, ist(""))) |
| 4889 | break; // end |
| 4890 | |
| 4891 | if (isteq(list[hdr].n, ist("host"))) { |
| 4892 | auth = list[hdr].v; |
| 4893 | break; |
| 4894 | } |
| 4895 | } |
| 4896 | } |
| 4897 | else { |
| 4898 | /* for CONNECT, :authority is taken from the path */ |
| 4899 | auth = path; |
| 4900 | } |
| 4901 | |
| 4902 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4903 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4904 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4905 | goto realign_again; |
| 4906 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | /* encode all headers, stop at empty name */ |
| 4910 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4911 | struct ist n = list[hdr].n; |
| 4912 | struct ist v = list[hdr].v; |
| 4913 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4914 | /* these ones do not exist in H2 and must be dropped. */ |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4915 | if (isteq(n, ist("connection")) || |
| 4916 | isteq(n, ist("host")) || |
| 4917 | isteq(n, ist("proxy-connection")) || |
| 4918 | isteq(n, ist("keep-alive")) || |
| 4919 | isteq(n, ist("upgrade")) || |
| 4920 | isteq(n, ist("transfer-encoding"))) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4921 | continue; |
| 4922 | |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4923 | if (isteq(n, ist("te"))) { |
| 4924 | /* "te" may only be sent with "trailers" if this value |
| 4925 | * is present, otherwise it must be deleted. |
| 4926 | */ |
| 4927 | v = istist(v, ist("trailers")); |
| 4928 | if (!v.ptr || (v.len > 8 && v.ptr[8] != ',')) |
| 4929 | continue; |
| 4930 | v = ist("trailers"); |
| 4931 | } |
| 4932 | |
| 4933 | if (isteq(n, ist(""))) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4934 | break; // end |
| 4935 | |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4936 | if (!hpack_encode_header(&outbuf, n, v)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4937 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4938 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4939 | goto realign_again; |
| 4940 | goto full; |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | /* we may need to add END_STREAM if we have no body : |
| 4945 | * - request already closed, or : |
| 4946 | * - no transfer-encoding, and : |
| 4947 | * - no content-length or content-length:0 |
| 4948 | * Fixme: this doesn't take into account CONNECT requests. |
| 4949 | */ |
| 4950 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4951 | es_now = 1; |
| 4952 | |
| 4953 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4954 | es_now = 1; |
| 4955 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4956 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4957 | es_now = 1; |
| 4958 | |
| 4959 | /* update the frame's size */ |
| 4960 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4961 | |
| 4962 | if (es_now) |
| 4963 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4964 | |
| 4965 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4966 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4967 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4968 | h2s->st = H2_SS_OPEN; |
| 4969 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4970 | if (es_now) { |
| 4971 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4972 | h2s->flags |= H2_SF_ES_SENT; |
| 4973 | h2s->st = H2_SS_HLOC; |
| 4974 | } |
| 4975 | |
| 4976 | /* remove all header blocks including the EOH and compute the |
| 4977 | * corresponding size. |
| 4978 | * |
| 4979 | * FIXME: We should remove everything when es_now is set. |
| 4980 | */ |
| 4981 | ret = 0; |
| 4982 | idx = htx_get_head(htx); |
| 4983 | blk = htx_get_blk(htx, idx); |
| 4984 | while (blk != blk_end) { |
| 4985 | ret += htx_get_blksz(blk); |
| 4986 | blk = htx_remove_blk(htx, blk); |
| 4987 | } |
| 4988 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4989 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4990 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4991 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4992 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4993 | |
| 4994 | end: |
| 4995 | return ret; |
| 4996 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4997 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4998 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4999 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5000 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5001 | ret = 0; |
| 5002 | goto end; |
| 5003 | fail: |
| 5004 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5005 | * list etc go here (unrecoverable errors). |
| 5006 | */ |
| 5007 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5008 | ret = 0; |
| 5009 | goto end; |
| 5010 | } |
| 5011 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5012 | /* 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] | 5013 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 5014 | * caller must check the stream's status to detect any error which might have |
| 5015 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5016 | * consumed, or zero if nothing done. Note that EOM count for 1 byte. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5017 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5018 | 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] | 5019 | { |
| 5020 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5021 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5022 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5023 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5024 | size_t total = 0; |
| 5025 | int es_now = 0; |
| 5026 | int bsize; /* htx block size */ |
| 5027 | int fsize; /* h2 frame size */ |
| 5028 | struct htx_blk *blk; |
| 5029 | enum htx_blk_type type; |
| 5030 | int idx; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5031 | int trunc_out; /* non-zero if truncated on out buf */ |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5032 | |
| 5033 | if (h2c_mux_busy(h2c, h2s)) { |
| 5034 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5035 | goto end; |
| 5036 | } |
| 5037 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5038 | htx = htx_from_buf(buf); |
| 5039 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5040 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 5041 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 5042 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5043 | */ |
| 5044 | |
| 5045 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5046 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5047 | goto end; |
| 5048 | |
| 5049 | idx = htx_get_head(htx); |
| 5050 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5051 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5052 | bsize = htx_get_blksz(blk); |
| 5053 | fsize = bsize; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5054 | trunc_out = 0; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5055 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5056 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 5057 | if (h2s->flags & H2_SF_ES_SENT) { |
| 5058 | /* ES already sent */ |
| 5059 | htx_remove_blk(htx, blk); |
| 5060 | total++; // EOM counts as one byte |
| 5061 | count--; |
| 5062 | goto end; |
| 5063 | } |
| 5064 | } |
| 5065 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5066 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5067 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5068 | mbuf = br_tail(h2c->mbuf); |
| 5069 | retry: |
| 5070 | if (!h2_get_buf(h2c, mbuf)) { |
| 5071 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5072 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5073 | goto end; |
| 5074 | } |
| 5075 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5076 | /* Perform some optimizations to reduce the number of buffer copies. |
| 5077 | * First, if the mux's buffer is empty and the htx area contains |
| 5078 | * exactly one data block of the same size as the requested count, and |
| 5079 | * this count fits within the frame size, the stream's window size, and |
| 5080 | * the connection's window size, then it's possible to simply swap the |
| 5081 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 5082 | * length to match the entire DATA HTX block in the middle. In this |
| 5083 | * case we perform a true zero-copy operation from end-to-end. This is |
| 5084 | * the situation that happens all the time with large files. Second, if |
| 5085 | * this is not possible, but the mux's output buffer is empty, we still |
| 5086 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 5087 | * making the intermediary buffer's area point to the output buffer's |
| 5088 | * area. In this case we want to skip the HTX header to make sure that |
| 5089 | * copies remain aligned and that this operation remains possible all |
| 5090 | * the time. This goes for headers, data blocks and any data extracted |
| 5091 | * from the HTX blocks. |
| 5092 | */ |
| 5093 | if (unlikely(fsize == count && |
| 5094 | htx->used == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5095 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5096 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5097 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5098 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5099 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 5100 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 5101 | * frame into a buffer containing few data if it needs to be realigned, |
| 5102 | * and that it's also OK to copy few data without realigning. Otherwise |
| 5103 | * 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] | 5104 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5105 | if (fsize + 9 <= b_room(mbuf) && |
| 5106 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 5107 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5108 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5109 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5110 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5111 | goto retry; |
| 5112 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5113 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5114 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5115 | goto end; |
| 5116 | } |
| 5117 | |
| 5118 | /* map an H2 frame to the HTX block so that we can put the |
| 5119 | * frame header there. |
| 5120 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5121 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 5122 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5123 | |
| 5124 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 5125 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5126 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5127 | h2_set_frame_size(outbuf.area, fsize); |
| 5128 | |
| 5129 | /* update windows */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5130 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5131 | h2c->mws -= fsize; |
| 5132 | |
| 5133 | /* and exchange with our old area */ |
| 5134 | buf->area = old_area; |
| 5135 | buf->data = buf->head = 0; |
| 5136 | total += fsize; |
| 5137 | goto end; |
| 5138 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5139 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5140 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5141 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 5142 | |
| 5143 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5144 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5145 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5146 | break; |
| 5147 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5148 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5152 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5153 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5154 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5155 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5156 | goto end; |
| 5157 | } |
| 5158 | |
| 5159 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5160 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5161 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5162 | outbuf.data = 9; |
| 5163 | |
| 5164 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5165 | * the HTX buffer. We need to check this against the connection's and |
| 5166 | * the stream's send windows, and to ensure that this fits in the max |
| 5167 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5168 | * the frame header). The connection's flow control is applied last so |
| 5169 | * that we can use a separate list of streams which are immediately |
| 5170 | * unblocked on window opening. Note: we don't implement padding. |
| 5171 | */ |
| 5172 | |
| 5173 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5174 | * empty frame, thus we force it to zero here. |
| 5175 | */ |
| 5176 | if (type == HTX_BLK_EOM) |
| 5177 | bsize = fsize = 0; |
| 5178 | |
| 5179 | if (!fsize) |
| 5180 | goto send_empty; |
| 5181 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5182 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5183 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5184 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5185 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5186 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5187 | goto end; |
| 5188 | } |
| 5189 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5190 | if (fsize > count) |
| 5191 | fsize = count; |
| 5192 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5193 | if (fsize > h2s_mws(h2s)) |
| 5194 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5195 | |
| 5196 | if (h2c->mfs && fsize > h2c->mfs) |
| 5197 | fsize = h2c->mfs; // >0 |
| 5198 | |
| 5199 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5200 | /* It doesn't fit at once. If it at least fits once split and |
| 5201 | * the amount of data to move is low, let's defragment the |
| 5202 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5203 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5204 | if (b_space_wraps(mbuf) && |
| 5205 | (fsize + 9 <= b_room(mbuf)) && |
| 5206 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5207 | goto realign_again; |
| 5208 | fsize = outbuf.size - 9; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5209 | trunc_out = 1; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5210 | |
| 5211 | if (fsize <= 0) { |
| 5212 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5213 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5214 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5215 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5216 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5217 | goto end; |
| 5218 | } |
| 5219 | } |
| 5220 | |
| 5221 | if (h2c->mws <= 0) { |
| 5222 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5223 | goto end; |
| 5224 | } |
| 5225 | |
| 5226 | if (fsize > h2c->mws) |
| 5227 | fsize = h2c->mws; |
| 5228 | |
| 5229 | /* now let's copy this this into the output buffer */ |
| 5230 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5231 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5232 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5233 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5234 | |
| 5235 | send_empty: |
| 5236 | /* update the frame's size */ |
| 5237 | h2_set_frame_size(outbuf.area, fsize); |
| 5238 | |
| 5239 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5240 | * meeting EOM. We should optimize this later. |
| 5241 | */ |
| 5242 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5243 | total++; // EOM counts as one byte |
| 5244 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5245 | es_now = 1; |
| 5246 | } |
| 5247 | |
| 5248 | if (es_now) |
| 5249 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5250 | |
| 5251 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5252 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5253 | |
| 5254 | /* consume incoming HTX block, including EOM */ |
| 5255 | total += fsize; |
| 5256 | if (fsize == bsize) { |
| 5257 | htx_remove_blk(htx, blk); |
| 5258 | if (fsize) |
| 5259 | goto new_frame; |
| 5260 | } else { |
| 5261 | /* we've truncated this block */ |
| 5262 | htx_cut_data_blk(htx, blk, fsize); |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5263 | if (trunc_out) |
| 5264 | goto new_frame; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | if (es_now) { |
| 5268 | if (h2s->st == H2_SS_OPEN) |
| 5269 | h2s->st = H2_SS_HLOC; |
| 5270 | else |
| 5271 | h2s_close(h2s); |
| 5272 | |
| 5273 | h2s->flags |= H2_SF_ES_SENT; |
| 5274 | } |
| 5275 | |
| 5276 | end: |
| 5277 | return total; |
| 5278 | } |
| 5279 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5280 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5281 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5282 | * processed. The caller must check the stream's status to detect any error |
| 5283 | * which might have happened subsequently to a successful send. The htx blocks |
| 5284 | * are automatically removed from the message. The htx message is assumed to be |
| 5285 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5286 | * 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] | 5287 | * as a single frame. The ES flag is always set. |
| 5288 | */ |
| 5289 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5290 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 5291 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5292 | struct h2c *h2c = h2s->h2c; |
| 5293 | struct htx_blk *blk; |
| 5294 | struct htx_blk *blk_end; |
| 5295 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5296 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5297 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5298 | int ret = 0; |
| 5299 | int hdr; |
| 5300 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5301 | |
| 5302 | if (h2c_mux_busy(h2c, h2s)) { |
| 5303 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5304 | goto end; |
| 5305 | } |
| 5306 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5307 | /* determine the first block which must not be deleted, blk_end may |
| 5308 | * be NULL if all blocks have to be deleted. also get trailers. |
| 5309 | */ |
| 5310 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5311 | blk_end = NULL; |
| 5312 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5313 | hdr = 0; |
| 5314 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5315 | blk = htx_get_blk(htx, idx); |
| 5316 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5317 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5318 | if (type == HTX_BLK_UNUSED) |
| 5319 | continue; |
| 5320 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5321 | if (type == HTX_BLK_EOT) { |
| 5322 | if (idx != -1) |
| 5323 | blk_end = blk; |
| 5324 | break; |
| 5325 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5326 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5327 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5328 | |
| 5329 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5330 | goto fail; |
| 5331 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5332 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 5333 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 5334 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5335 | } |
| 5336 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5337 | /* marker for end of trailers */ |
| 5338 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5339 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5340 | mbuf = br_tail(h2c->mbuf); |
| 5341 | retry: |
| 5342 | if (!h2_get_buf(h2c, mbuf)) { |
| 5343 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5344 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5345 | goto end; |
| 5346 | } |
| 5347 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5348 | chunk_reset(&outbuf); |
| 5349 | |
| 5350 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5351 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5352 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5353 | break; |
| 5354 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5355 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | if (outbuf.size < 9) |
| 5359 | goto full; |
| 5360 | |
| 5361 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5362 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5363 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5364 | outbuf.data = 9; |
| 5365 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5366 | /* encode all headers */ |
| 5367 | for (idx = 0; idx < hdr; idx++) { |
| 5368 | /* these ones do not exist in H2 or must not appear in |
| 5369 | * trailers and must be dropped. |
| 5370 | */ |
| 5371 | if (isteq(list[idx].n, ist("host")) || |
| 5372 | isteq(list[idx].n, ist("content-length")) || |
| 5373 | isteq(list[idx].n, ist("connection")) || |
| 5374 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5375 | isteq(list[idx].n, ist("keep-alive")) || |
| 5376 | isteq(list[idx].n, ist("upgrade")) || |
| 5377 | isteq(list[idx].n, ist("te")) || |
| 5378 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5379 | continue; |
| 5380 | |
| 5381 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5382 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5383 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5384 | goto realign_again; |
| 5385 | goto full; |
| 5386 | } |
| 5387 | } |
| 5388 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5389 | if (outbuf.data == 9) { |
| 5390 | /* here we have a problem, we have nothing to emit (either we |
| 5391 | * received an empty trailers block followed or we removed its |
| 5392 | * contents above). Because of this we can't send a HEADERS |
| 5393 | * frame, so we have to cheat and instead send an empty DATA |
| 5394 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5395 | */ |
| 5396 | outbuf.area[3] = H2_FT_DATA; |
| 5397 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5398 | } |
| 5399 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5400 | /* update the frame's size */ |
| 5401 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5402 | |
| 5403 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5404 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5405 | h2s->flags |= H2_SF_ES_SENT; |
| 5406 | |
| 5407 | if (h2s->st == H2_SS_OPEN) |
| 5408 | h2s->st = H2_SS_HLOC; |
| 5409 | else |
| 5410 | h2s_close(h2s); |
| 5411 | |
| 5412 | /* OK we could properly deliver the response */ |
| 5413 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5414 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5415 | ret = 0; |
| 5416 | idx = htx_get_head(htx); |
| 5417 | blk = htx_get_blk(htx, idx); |
| 5418 | while (blk != blk_end) { |
| 5419 | ret += htx_get_blksz(blk); |
| 5420 | blk = htx_remove_blk(htx, blk); |
| 5421 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5422 | |
| 5423 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5424 | ret += htx_get_blksz(blk_end); |
| 5425 | htx_remove_blk(htx, blk_end); |
| 5426 | } |
| 5427 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5428 | end: |
| 5429 | return ret; |
| 5430 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5431 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5432 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5433 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5434 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5435 | ret = 0; |
| 5436 | goto end; |
| 5437 | fail: |
| 5438 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5439 | * list etc go here (unrecoverable errors). |
| 5440 | */ |
| 5441 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5442 | ret = 0; |
| 5443 | goto end; |
| 5444 | } |
| 5445 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5446 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5447 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5448 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5449 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5450 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5451 | * returns 0 except for the error above. |
| 5452 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5453 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5454 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5455 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5456 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5457 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5458 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5459 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5460 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5461 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5462 | sw->events |= SUB_RETRY_RECV; |
| 5463 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5464 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5465 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5466 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5467 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5468 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5469 | sw->events |= SUB_RETRY_SEND; |
| 5470 | h2s->send_wait = sw; |
| 5471 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5472 | !LIST_ADDED(&h2s->list)) { |
| 5473 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5474 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5475 | else |
| 5476 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5477 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5478 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5479 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5480 | if (event_type != 0) |
| 5481 | return -1; |
| 5482 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5485 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5486 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5487 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5488 | * It always returns zero. |
| 5489 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5490 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5491 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5492 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5493 | struct h2s *h2s = cs->ctx; |
| 5494 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5495 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5496 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5497 | BUG_ON(h2s->recv_wait != sw); |
| 5498 | sw->events &= ~SUB_RETRY_RECV; |
| 5499 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5500 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5501 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5502 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5503 | BUG_ON(h2s->send_wait != sw); |
| 5504 | LIST_DEL(&h2s->list); |
| 5505 | LIST_INIT(&h2s->list); |
| 5506 | sw->events &= ~SUB_RETRY_SEND; |
| 5507 | /* We were about to send, make sure it does not happen */ |
| 5508 | if (LIST_ADDED(&h2s->sending_list) && |
| 5509 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5510 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5511 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5512 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5513 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5514 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5519 | /* Called from the upper layer, to receive data */ |
| 5520 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5521 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5522 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5523 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5524 | struct htx *h2s_htx = NULL; |
| 5525 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5526 | size_t ret = 0; |
| 5527 | |
| 5528 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5529 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5530 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5531 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5532 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5533 | * the HTX is empty. |
| 5534 | */ |
| 5535 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5536 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5537 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5538 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5539 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5540 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5541 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5542 | /* <buf> is empty and the message is small enough, swap the |
| 5543 | * buffers. */ |
| 5544 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5545 | htx_to_buf(buf_htx, buf); |
| 5546 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5547 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5548 | goto end; |
| 5549 | } |
| 5550 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5551 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5552 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5553 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5554 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5555 | if (htx_is_empty(buf_htx)) |
| 5556 | cs->flags |= CS_FL_EOI; |
| 5557 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5558 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5559 | 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] | 5560 | htx_to_buf(buf_htx, buf); |
| 5561 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5562 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5563 | } |
| 5564 | else { |
| 5565 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5566 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5567 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5568 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5569 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5570 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5571 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5572 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5573 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5574 | cs->flags |= CS_FL_EOI; |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 5575 | if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5576 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5577 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5578 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5579 | if (b_size(&h2s->rxbuf)) { |
| 5580 | b_free(&h2s->rxbuf); |
| 5581 | offer_buffers(NULL, tasks_run_queue); |
| 5582 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5583 | } |
| 5584 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5585 | if (ret && h2c->dsi == h2s->id) { |
| 5586 | /* demux is blocking on this stream's buffer */ |
| 5587 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5588 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5589 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5590 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5591 | return ret; |
| 5592 | } |
| 5593 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5594 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5595 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5596 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5597 | static void h2_stop_senders(struct h2c *h2c) |
| 5598 | { |
| 5599 | struct h2s *h2s, *h2s_back; |
| 5600 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5601 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5602 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5603 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5604 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5605 | } |
| 5606 | } |
| 5607 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5608 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5609 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5610 | * flags may be updated on the conn_stream. |
| 5611 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5612 | 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] | 5613 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5614 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5615 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5616 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5617 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5618 | struct htx *htx; |
| 5619 | struct htx_blk *blk; |
| 5620 | enum htx_blk_type btype; |
| 5621 | uint32_t bsize; |
| 5622 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5623 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5624 | /* If we were not just woken because we wanted to send but couldn't, |
| 5625 | * and there's somebody else that is waiting to send, do nothing, |
| 5626 | * we will subscribe later and be put at the end of the list |
| 5627 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5628 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5629 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5630 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5631 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5632 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5633 | /* We couldn't set it to NULL before, because we needed it in case |
| 5634 | * we had to cancel the tasklet |
| 5635 | */ |
| 5636 | h2s->send_wait = NULL; |
| 5637 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5638 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5639 | return 0; |
| 5640 | |
Willy Tarreau | 902df22 | 2019-10-31 15:48:18 +0100 | [diff] [blame] | 5641 | if (h2s->h2c->st0 >= H2_CS_ERROR) { |
| 5642 | cs->flags |= CS_FL_ERROR; |
| 5643 | return 0; |
| 5644 | } |
| 5645 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5646 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5647 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5648 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5649 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5650 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5651 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5652 | if (h2s->id == 0) { |
| 5653 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5654 | |
| 5655 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5656 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5657 | return 0; |
| 5658 | } |
| 5659 | |
| 5660 | eb32_delete(&h2s->by_id); |
| 5661 | h2s->by_id.key = h2s->id = id; |
| 5662 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5663 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5664 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5665 | } |
| 5666 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5667 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5668 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5669 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5670 | idx = htx_get_head(htx); |
| 5671 | blk = htx_get_blk(htx, idx); |
| 5672 | btype = htx_get_blk_type(blk); |
| 5673 | bsize = htx_get_blksz(blk); |
| 5674 | |
| 5675 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5676 | case HTX_BLK_REQ_SL: |
| 5677 | /* start-line before headers */ |
| 5678 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5679 | if (ret > 0) { |
| 5680 | total += ret; |
| 5681 | count -= ret; |
| 5682 | if (ret < bsize) |
| 5683 | goto done; |
| 5684 | } |
| 5685 | break; |
| 5686 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5687 | case HTX_BLK_RES_SL: |
| 5688 | /* start-line before headers */ |
| 5689 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5690 | if (ret > 0) { |
| 5691 | total += ret; |
| 5692 | count -= ret; |
| 5693 | if (ret < bsize) |
| 5694 | goto done; |
| 5695 | } |
| 5696 | break; |
| 5697 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5698 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5699 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5700 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5701 | * This EOM necessarily is one before trailers, as the EOM following |
| 5702 | * trailers would have been consumed by the trailers parser. |
| 5703 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5704 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5705 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5706 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5707 | total += ret; |
| 5708 | count -= ret; |
| 5709 | if (ret < bsize) |
| 5710 | goto done; |
| 5711 | } |
| 5712 | break; |
| 5713 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5714 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5715 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5716 | /* This is the first trailers block, all the subsequent ones AND |
| 5717 | * the EOM will be swallowed by the parser. |
| 5718 | */ |
| 5719 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5720 | if (ret > 0) { |
| 5721 | total += ret; |
| 5722 | count -= ret; |
| 5723 | if (ret < bsize) |
| 5724 | goto done; |
| 5725 | } |
| 5726 | break; |
| 5727 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5728 | default: |
| 5729 | htx_remove_blk(htx, blk); |
| 5730 | total += bsize; |
| 5731 | count -= bsize; |
| 5732 | break; |
| 5733 | } |
| 5734 | } |
| 5735 | goto done; |
| 5736 | } |
| 5737 | |
| 5738 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5739 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5740 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5741 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5742 | ret = -1; |
| 5743 | else |
| 5744 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5745 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5746 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5747 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5748 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5749 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5750 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5751 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5752 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5753 | if (unlikely((int)ret <= 0)) { |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 5754 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5755 | break; |
| 5756 | } |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 5757 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5758 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5759 | total += count; |
| 5760 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5761 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5762 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5763 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5764 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5765 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5766 | break; |
| 5767 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5768 | |
| 5769 | total += ret; |
| 5770 | count -= ret; |
| 5771 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5772 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5773 | break; |
| 5774 | |
| 5775 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5776 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5777 | } |
| 5778 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5779 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5780 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5781 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5782 | * unprocessed trailers, abnormal extra data, ...) |
| 5783 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5784 | total += count; |
| 5785 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5788 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5789 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5790 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5791 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5792 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5793 | } |
| 5794 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5795 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5796 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5797 | } else { |
| 5798 | b_del(buf, total); |
| 5799 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5800 | |
| 5801 | /* The mux is full, cancel the pending tasks */ |
| 5802 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5803 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5804 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5805 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5806 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5807 | * we read exactly what the caller specified, as with HTX the caller |
| 5808 | * will always give the buffer size, instead of the amount of data |
| 5809 | * available. |
| 5810 | */ |
| 5811 | if (htx && !b_data(buf)) |
| 5812 | total = orig_count; |
| 5813 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5814 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5815 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5816 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5817 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5818 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5819 | /* If we're waiting for flow control, and we got a shutr on the |
| 5820 | * connection, we will never be unlocked, so add an error on |
| 5821 | * the conn_stream. |
| 5822 | */ |
| 5823 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5824 | !b_data(&h2s->h2c->dbuf) && |
| 5825 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5826 | if (cs->flags & CS_FL_EOS) |
| 5827 | cs->flags |= CS_FL_ERROR; |
| 5828 | else |
| 5829 | cs->flags |= CS_FL_ERR_PENDING; |
| 5830 | } |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5831 | |
| 5832 | if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5833 | /* Ok we managed to send something, leave the send_list if we were still there */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5834 | LIST_DEL_INIT(&h2s->list); |
| 5835 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5836 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5837 | } |
| 5838 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5839 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5840 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5841 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5842 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5843 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5844 | struct eb32_node *node; |
| 5845 | int fctl_cnt = 0; |
| 5846 | int send_cnt = 0; |
| 5847 | int tree_cnt = 0; |
| 5848 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5849 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5850 | |
| 5851 | if (!h2c) |
| 5852 | return; |
| 5853 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5854 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5855 | fctl_cnt++; |
| 5856 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5857 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5858 | send_cnt++; |
| 5859 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5860 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5861 | node = eb32_first(&h2c->streams_by_id); |
| 5862 | while (node) { |
| 5863 | h2s = container_of(node, struct h2s, by_id); |
| 5864 | tree_cnt++; |
| 5865 | if (!h2s->cs) |
| 5866 | orph_cnt++; |
| 5867 | node = eb32_next(node); |
| 5868 | } |
| 5869 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5870 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5871 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5872 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5873 | " .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] | 5874 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5875 | " .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] | 5876 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5877 | 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] | 5878 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5879 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5880 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5881 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5882 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5883 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5884 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5885 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5886 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5887 | |
| 5888 | if (h2s) { |
| 5889 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5890 | h2s, h2s->id, h2s->flags, |
| 5891 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5892 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5893 | h2s->cs); |
| 5894 | if (h2s->cs) |
| 5895 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5896 | h2s->cs->flags, h2s->cs->data); |
| 5897 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5898 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5899 | |
| 5900 | /*******************************************************/ |
| 5901 | /* functions below are dedicated to the config parsers */ |
| 5902 | /*******************************************************/ |
| 5903 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5904 | /* config parser for global "tune.h2.header-table-size" */ |
| 5905 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5906 | struct proxy *defpx, const char *file, int line, |
| 5907 | char **err) |
| 5908 | { |
| 5909 | if (too_many_args(1, args, err, NULL)) |
| 5910 | return -1; |
| 5911 | |
| 5912 | h2_settings_header_table_size = atoi(args[1]); |
| 5913 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5914 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5915 | return -1; |
| 5916 | } |
| 5917 | return 0; |
| 5918 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5919 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5920 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5921 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5922 | struct proxy *defpx, const char *file, int line, |
| 5923 | char **err) |
| 5924 | { |
| 5925 | if (too_many_args(1, args, err, NULL)) |
| 5926 | return -1; |
| 5927 | |
| 5928 | h2_settings_initial_window_size = atoi(args[1]); |
| 5929 | if (h2_settings_initial_window_size < 0) { |
| 5930 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5931 | return -1; |
| 5932 | } |
| 5933 | return 0; |
| 5934 | } |
| 5935 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5936 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5937 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5938 | struct proxy *defpx, const char *file, int line, |
| 5939 | char **err) |
| 5940 | { |
| 5941 | if (too_many_args(1, args, err, NULL)) |
| 5942 | return -1; |
| 5943 | |
| 5944 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5945 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5946 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5947 | return -1; |
| 5948 | } |
| 5949 | return 0; |
| 5950 | } |
| 5951 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5952 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5953 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5954 | struct proxy *defpx, const char *file, int line, |
| 5955 | char **err) |
| 5956 | { |
| 5957 | if (too_many_args(1, args, err, NULL)) |
| 5958 | return -1; |
| 5959 | |
| 5960 | h2_settings_max_frame_size = atoi(args[1]); |
| 5961 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5962 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5963 | return -1; |
| 5964 | } |
| 5965 | return 0; |
| 5966 | } |
| 5967 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5968 | |
| 5969 | /****************************************/ |
| 5970 | /* MUX initialization and instanciation */ |
| 5971 | /***************************************/ |
| 5972 | |
| 5973 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5974 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5975 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5976 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5977 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5978 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5979 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5980 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5981 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5982 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5983 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5984 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5985 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5986 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5987 | .shutr = h2_shutr, |
| 5988 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 5989 | .ctl = h2_ctl, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5990 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5991 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5992 | .name = "H2", |
| 5993 | }; |
| 5994 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5995 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5996 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5997 | { .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] | 5998 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5999 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 6000 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6001 | |
| 6002 | /* The mux operations */ |
| 6003 | static const struct mux_ops h2_htx_ops = { |
| 6004 | .init = h2_init, |
| 6005 | .wake = h2_wake, |
| 6006 | .snd_buf = h2_snd_buf, |
| 6007 | .rcv_buf = h2_rcv_buf, |
| 6008 | .subscribe = h2_subscribe, |
| 6009 | .unsubscribe = h2_unsubscribe, |
| 6010 | .attach = h2_attach, |
| 6011 | .get_first_cs = h2_get_first_cs, |
| 6012 | .detach = h2_detach, |
| 6013 | .destroy = h2_destroy, |
| 6014 | .avail_streams = h2_avail_streams, |
| 6015 | .used_streams = h2_used_streams, |
| 6016 | .shutr = h2_shutr, |
| 6017 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 6018 | .ctl = h2_ctl, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6019 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 6020 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6021 | .name = "H2", |
| 6022 | }; |
| 6023 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 6024 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6025 | { .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] | 6026 | |
| 6027 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 6028 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6029 | /* config keyword parsers */ |
| 6030 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 6031 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 6032 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 6033 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 6034 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6035 | { 0, NULL, NULL } |
| 6036 | }}; |
| 6037 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6038 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |