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 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2928 | if (conn->flags & CO_FL_ERROR) |
| 2929 | break; |
| 2930 | |
| 2931 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2932 | flags |= CO_SFL_MSG_MORE; |
| 2933 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2934 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2935 | if (b_data(buf)) { |
| 2936 | 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] | 2937 | if (!ret) { |
| 2938 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2939 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2940 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2941 | sent = 1; |
| 2942 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2943 | if (b_data(buf)) { |
| 2944 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2945 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2946 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2947 | } |
| 2948 | b_free(buf); |
| 2949 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2950 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2951 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2952 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2953 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2954 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2955 | /* wrote at least one byte, the buffer is not full anymore */ |
Christopher Faulet | 0742308 | 2019-10-24 10:31:01 +0200 | [diff] [blame] | 2956 | if (sent) |
| 2957 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2958 | } |
| 2959 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2960 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2961 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2962 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2963 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2964 | /* We're not full anymore, so we can wake any task that are waiting |
| 2965 | * for us. |
| 2966 | */ |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2967 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) |
| 2968 | h2_resume_each_sending_h2s(h2c, &h2c->send_list); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2969 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2970 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2971 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2972 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2973 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2974 | 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] | 2975 | 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] | 2976 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2977 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2978 | } |
| 2979 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2980 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2981 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2982 | { |
| 2983 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2984 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2985 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2986 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2987 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2988 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2989 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2990 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2991 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2992 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2993 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2994 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2995 | /* callback called on any event by the connection handler. |
| 2996 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2997 | * destruction of the connection (which normally doesn not happen in h2). |
| 2998 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2999 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3000 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3001 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3002 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3003 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3004 | h2_process_demux(h2c); |
| 3005 | |
| 3006 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3007 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3008 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3009 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3010 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 3011 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3012 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3013 | |
Willy Tarreau | 318822b | 2020-10-13 18:09:15 +0200 | [diff] [blame] | 3014 | 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] | 3015 | /* frontend is stopping, reload likely in progress, let's try |
| 3016 | * to announce a graceful shutdown if not yet done. We don't |
| 3017 | * care if it fails, it will be tried again later. |
| 3018 | */ |
| 3019 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3020 | if (h2c->last_sid < 0) |
| 3021 | h2c->last_sid = (1U << 31) - 1; |
| 3022 | h2c_send_goaway_error(h2c, NULL); |
| 3023 | } |
| 3024 | } |
| 3025 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3026 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3027 | * If we received early data, and the handshake is done, wake |
| 3028 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3029 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3030 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 3031 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 3032 | struct eb32_node *node; |
| 3033 | struct h2s *h2s; |
| 3034 | |
| 3035 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 3036 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 3037 | |
| 3038 | while (node) { |
| 3039 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 3040 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 3041 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3042 | node = eb32_next(node); |
| 3043 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3044 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3045 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 3046 | if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 3047 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 3048 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 3049 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3050 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3051 | |
| 3052 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3053 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3054 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3055 | return -1; |
| 3056 | } |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3057 | |
| 3058 | /* connections in error must be removed from the idle lists */ |
| 3059 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3060 | LIST_DEL_LOCKED(&conn->list); |
| 3061 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3062 | } |
| 3063 | else if (h2c->st0 == H2_CS_ERROR) { |
| 3064 | /* connections in error must be removed from the idle lists */ |
| 3065 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3066 | LIST_DEL_LOCKED(&conn->list); |
| 3067 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3068 | } |
| 3069 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3070 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3071 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3072 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3073 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3074 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 3075 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3076 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3077 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 3078 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 3079 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3080 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 3081 | if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3082 | if (h2c_may_expire(h2c)) |
| 3083 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3084 | else |
| 3085 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3086 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3087 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3088 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3089 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3090 | return 0; |
| 3091 | } |
| 3092 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3093 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3094 | static int h2_wake(struct connection *conn) |
| 3095 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3096 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3097 | |
| 3098 | return (h2_process(h2c)); |
| 3099 | } |
| 3100 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3101 | /* Connection timeout management. The principle is that if there's no receipt |
| 3102 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3103 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3104 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3105 | * GOAWAY frame. |
| 3106 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3107 | 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] | 3108 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3109 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3110 | int expired = tick_is_expired(t->expire, now_ms); |
| 3111 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3112 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3113 | return t; |
| 3114 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3115 | if (h2c && !h2c_may_expire(h2c)) { |
| 3116 | /* we do still have streams but all of them are idle, waiting |
| 3117 | * for the data layer, so we must not enforce the timeout here. |
| 3118 | */ |
| 3119 | t->expire = TICK_ETERNITY; |
| 3120 | return t; |
| 3121 | } |
| 3122 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3123 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3124 | |
| 3125 | if (!h2c) { |
| 3126 | /* resources were already deleted */ |
| 3127 | return NULL; |
| 3128 | } |
| 3129 | |
| 3130 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3131 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3132 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3133 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3134 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3135 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3136 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3137 | } |
| 3138 | |
| 3139 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3140 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3141 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3142 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3143 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3144 | 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] | 3145 | unsigned int released = 0; |
| 3146 | struct buffer *buf; |
| 3147 | |
| 3148 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3149 | if (b_data(buf)) { |
| 3150 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3151 | if (!ret) |
| 3152 | break; |
| 3153 | b_del(buf, ret); |
| 3154 | if (b_data(buf)) |
| 3155 | break; |
| 3156 | b_free(buf); |
| 3157 | released++; |
| 3158 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3159 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3160 | |
| 3161 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3162 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3163 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3164 | |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3165 | /* in any case this connection must not be considered idle anymore */ |
| 3166 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3167 | LIST_DEL_LOCKED(&h2c->conn->list); |
| 3168 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3169 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3170 | /* either we can release everything now or it will be done later once |
| 3171 | * the last stream closes. |
| 3172 | */ |
| 3173 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3174 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3175 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3176 | return NULL; |
| 3177 | } |
| 3178 | |
| 3179 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3180 | /*******************************************/ |
| 3181 | /* functions below are used by the streams */ |
| 3182 | /*******************************************/ |
| 3183 | |
| 3184 | /* |
| 3185 | * Attach a new stream to a connection |
| 3186 | * (Used for outgoing connections) |
| 3187 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3188 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3189 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3190 | struct conn_stream *cs; |
| 3191 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3192 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3193 | |
| 3194 | cs = cs_new(conn); |
| 3195 | if (!cs) |
| 3196 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3197 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3198 | if (!h2s) { |
| 3199 | cs_free(cs); |
| 3200 | return NULL; |
| 3201 | } |
| 3202 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3205 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3206 | * We have to scan because we may have some orphan streams. It might be |
| 3207 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3208 | * orphans. |
| 3209 | */ |
| 3210 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3211 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3212 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3213 | struct h2s *h2s; |
| 3214 | struct eb32_node *node; |
| 3215 | |
| 3216 | node = eb32_first(&h2c->streams_by_id); |
| 3217 | while (node) { |
| 3218 | h2s = container_of(node, struct h2s, by_id); |
| 3219 | if (h2s->cs) |
| 3220 | return h2s->cs; |
| 3221 | node = eb32_next(node); |
| 3222 | } |
| 3223 | return NULL; |
| 3224 | } |
| 3225 | |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3226 | static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3227 | { |
| 3228 | int ret = 0; |
| 3229 | struct h2c *h2c = conn->ctx; |
| 3230 | |
| 3231 | switch (mux_ctl) { |
| 3232 | case MUX_STATUS: |
| 3233 | /* Only consider the mux to be ready if we're done with |
| 3234 | * the preface and settings, and we had no error. |
| 3235 | */ |
| 3236 | if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR) |
| 3237 | ret |= MUX_STATUS_READY; |
| 3238 | return ret; |
| 3239 | default: |
| 3240 | return -1; |
| 3241 | } |
| 3242 | } |
| 3243 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3244 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3245 | * Destroy the mux and the associated connection, if it is no longer used |
| 3246 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3247 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3248 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3249 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3250 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3251 | 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] | 3252 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3256 | * Detach the stream from the connection and possibly release the connection. |
| 3257 | */ |
| 3258 | static void h2_detach(struct conn_stream *cs) |
| 3259 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3260 | struct h2s *h2s = cs->ctx; |
| 3261 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3262 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3263 | |
| 3264 | cs->ctx = NULL; |
| 3265 | if (!h2s) |
| 3266 | return; |
| 3267 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3268 | /* 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] | 3269 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3270 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3271 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3272 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3273 | /* |
| 3274 | * At this point, the stream_interface is supposed to have called |
| 3275 | * h2_unsubscribe(), so the only way there's still a |
| 3276 | * subscription that came from the stream_interface (as we |
| 3277 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3278 | * without the stream_interface involved) is that we subscribed |
| 3279 | * for sending, we woke the tasklet up and removed the |
| 3280 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3281 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3282 | * run yet. Make sure to handle that by explicitely setting |
| 3283 | * send_wait to NULL, as nothing else will do it for us. |
| 3284 | */ |
| 3285 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3286 | } |
| 3287 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3288 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3289 | h2c = h2s->h2c; |
| 3290 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3291 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3292 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3293 | !h2_frt_has_too_many_cs(h2c)) { |
| 3294 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3295 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3296 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3297 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3298 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3299 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3300 | * an ES or RST frame), so orphan it in this case. |
| 3301 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3302 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3303 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3304 | (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] | 3305 | return; |
| 3306 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3307 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3308 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3309 | /* unblock the connection if it was blocked on this |
| 3310 | * stream. |
| 3311 | */ |
| 3312 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3313 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3314 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3315 | } |
| 3316 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3317 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3318 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3319 | if (h2c->flags & H2_CF_IS_BACK && |
| 3320 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3321 | if (!(h2c->conn->flags & |
| 3322 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3323 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3324 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3325 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3326 | h2c->conn->owner = NULL; |
| 3327 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3328 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3329 | /* 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] | 3330 | h2c->conn->mux->destroy(h2c); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3331 | return; |
| 3332 | } |
| 3333 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3334 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3335 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3336 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3337 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3338 | return; |
| 3339 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3340 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3341 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3342 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3343 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3344 | struct server *srv = objt_server(h2c->conn->target); |
| 3345 | |
| 3346 | if (srv) { |
| 3347 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3348 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3349 | else |
| 3350 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3351 | } |
| 3352 | |
| 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3357 | /* We don't want to close right now unless we're removing the |
| 3358 | * last stream, and either the connection is in error, or it |
| 3359 | * reached the ID already specified in a GOAWAY frame received |
| 3360 | * or sent (as seen by last_sid >= 0). |
| 3361 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3362 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3363 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3364 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3365 | } |
| 3366 | else if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3367 | if (h2c_may_expire(h2c)) |
| 3368 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3369 | else |
| 3370 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3371 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3372 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3373 | } |
| 3374 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3375 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3376 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3377 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3378 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3379 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3380 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3381 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3382 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3383 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3384 | /* a connstream may require us to immediately kill the whole connection |
| 3385 | * for example because of a "tcp-request content reject" rule that is |
| 3386 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3387 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3388 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3389 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3390 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3391 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3392 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3393 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3394 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3395 | /* Nothing was never sent for this stream, so reset with |
| 3396 | * REFUSED_STREAM error to let the client retry the |
| 3397 | * request. |
| 3398 | */ |
| 3399 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3400 | } |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3401 | else { |
| 3402 | /* a final response was already provided, we don't want this |
| 3403 | * stream anymore. This may happen when the server responds |
| 3404 | * before the end of an upload and closes quickly (redirect, |
| 3405 | * deny, ...) |
| 3406 | */ |
| 3407 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3408 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3409 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3410 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3411 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3412 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3413 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3414 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3415 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3416 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3417 | done: |
| 3418 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3419 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3420 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3421 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3422 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3423 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3424 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3425 | h2s->send_wait = sw; |
| 3426 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3427 | h2s->send_wait = sw; |
| 3428 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3429 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3430 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3431 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3432 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3433 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3434 | } |
| 3435 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3436 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3437 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3438 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3439 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3440 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3441 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3442 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3443 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3444 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3445 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3446 | /* we can cleanly close using an empty data frame only after headers */ |
| 3447 | |
| 3448 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3449 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3450 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3451 | |
| 3452 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3453 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3454 | else |
| 3455 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3456 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3457 | /* a connstream may require us to immediately kill the whole connection |
| 3458 | * for example because of a "tcp-request content reject" rule that is |
| 3459 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3460 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3461 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3462 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3463 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3464 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3465 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3466 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3467 | else { |
| 3468 | /* Nothing was never sent for this stream, so reset with |
| 3469 | * REFUSED_STREAM error to let the client retry the |
| 3470 | * request. |
| 3471 | */ |
| 3472 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3473 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3474 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3475 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3476 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3477 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3478 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3479 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3480 | } |
| 3481 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3482 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3483 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3484 | done: |
| 3485 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3486 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3487 | |
| 3488 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3489 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3490 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3491 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3492 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3493 | h2s->send_wait = sw; |
| 3494 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3495 | h2s->send_wait = sw; |
| 3496 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3497 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3498 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3499 | /* let the handler know we want to shutw */ |
| 3500 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3501 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3502 | } |
| 3503 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3504 | /* 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] | 3505 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3506 | * and prevented the last frame from being emitted. |
| 3507 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3508 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3509 | { |
| 3510 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3511 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3512 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3513 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3514 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3515 | h2_do_shutw(h2s); |
| 3516 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3517 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3518 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3519 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3520 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3521 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3522 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3523 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3524 | if (!h2s->cs) { |
| 3525 | h2s_destroy(h2s); |
| 3526 | if (h2c_is_dead(h2c)) |
| 3527 | h2_release(h2c); |
| 3528 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3531 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3532 | } |
| 3533 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3534 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3535 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3536 | { |
| 3537 | struct h2s *h2s = cs->ctx; |
| 3538 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3539 | if (cs->flags & CS_FL_KILL_CONN) |
| 3540 | h2s->flags |= H2_SF_KILL_CONN; |
| 3541 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3542 | if (!mode) |
| 3543 | return; |
| 3544 | |
| 3545 | h2_do_shutr(h2s); |
| 3546 | } |
| 3547 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3548 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3549 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3550 | { |
| 3551 | struct h2s *h2s = cs->ctx; |
| 3552 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3553 | if (cs->flags & CS_FL_KILL_CONN) |
| 3554 | h2s->flags |= H2_SF_KILL_CONN; |
| 3555 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3556 | h2_do_shutw(h2s); |
| 3557 | } |
| 3558 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3559 | /* 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] | 3560 | * HTX request or response depending on the connection's side. Returns a |
| 3561 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3562 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3563 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3564 | * 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] | 3565 | * |
| 3566 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3567 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3568 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3569 | * leaving a hole between the main frame and the beginning of the next one. |
| 3570 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3571 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3572 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3573 | * |
| 3574 | * A buffer at the beginning of processing may look like this : |
| 3575 | * |
| 3576 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3577 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3578 | * `---^---------^-----^--------------^--------------^------^---' |
| 3579 | * | | <-----> | | |
| 3580 | * area | dpl | wrap |
| 3581 | * |<--------------> | |
| 3582 | * | dfl | |
| 3583 | * |<-------------------------------------------------->| |
| 3584 | * head data |
| 3585 | * |
| 3586 | * Padding is automatically overwritten when folding, participating to the |
| 3587 | * hole size after dfl : |
| 3588 | * |
| 3589 | * ,---.------------------------.-----.--------------.------.---. |
| 3590 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3591 | * `---^------------------------^-----^--------------^------^---' |
| 3592 | * | | <-----> | | |
| 3593 | * area | hole | wrap |
| 3594 | * |<-----------------------> | |
| 3595 | * | dfl | |
| 3596 | * |<-------------------------------------------------->| |
| 3597 | * head data |
| 3598 | * |
| 3599 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3600 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3601 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3602 | * |
| 3603 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3604 | * so that the function can update the following flags : |
| 3605 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3606 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3607 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3608 | * |
| 3609 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3610 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3611 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3612 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3613 | 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] | 3614 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3615 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3616 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3617 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3618 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3619 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3620 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3621 | int flen; // header frame len |
| 3622 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3623 | int ret = 0; |
| 3624 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3625 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3626 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3627 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3628 | next_frame: |
| 3629 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3630 | goto leave; // incomplete input frame |
| 3631 | |
| 3632 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3633 | * this case, we'll try to paste it immediately after the initial |
| 3634 | * HEADERS frame payload and kill any possible padding. The initial |
| 3635 | * frame's length will be increased to represent the concatenation |
| 3636 | * of the two frames. The next frame is read from position <tlen> |
| 3637 | * and written at position <flen> (minus padding if some is present). |
| 3638 | */ |
| 3639 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3640 | struct h2_fh hdr; |
| 3641 | int clen; // CONTINUATION frame's payload length |
| 3642 | |
| 3643 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3644 | /* no more data, the buffer may be full, either due to |
| 3645 | * too large a frame or because of too large a hole that |
| 3646 | * we're going to compact at the end. |
| 3647 | */ |
| 3648 | goto leave; |
| 3649 | } |
| 3650 | |
| 3651 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3652 | /* RFC7540#6.10: frame of unexpected type */ |
| 3653 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3654 | goto fail; |
| 3655 | } |
| 3656 | |
| 3657 | if (hdr.sid != h2c->dsi) { |
| 3658 | /* RFC7540#6.10: frame of different stream */ |
| 3659 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3660 | goto fail; |
| 3661 | } |
| 3662 | |
| 3663 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3664 | /* RFC7540#4.2: invalid frame length */ |
| 3665 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3666 | goto fail; |
| 3667 | } |
| 3668 | |
| 3669 | /* detect when we must stop aggragating frames */ |
| 3670 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3671 | |
| 3672 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3673 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3674 | if (clen > hdr.len) |
| 3675 | clen = hdr.len; |
| 3676 | |
| 3677 | /* Move the frame's payload over the padding, hole and frame |
| 3678 | * header. At least one of hole or dpl is null (see diagrams |
| 3679 | * above). The hole moves after the new aggragated frame. |
| 3680 | */ |
| 3681 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3682 | h2c->dfl += clen - h2c->dpl; |
| 3683 | hole += h2c->dpl + 9; |
| 3684 | h2c->dpl = 0; |
| 3685 | goto next_frame; |
| 3686 | } |
| 3687 | |
| 3688 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3689 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3690 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3691 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3692 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3693 | copy = alloc_trash_chunk(); |
| 3694 | if (!copy) { |
| 3695 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3696 | goto fail; |
| 3697 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3698 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3699 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3700 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3701 | } |
| 3702 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3703 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3704 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3705 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3706 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3707 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3708 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3711 | if (flen < 5) { |
| 3712 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3713 | goto fail; |
| 3714 | } |
| 3715 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3716 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3717 | flen -= 5; |
| 3718 | } |
| 3719 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3720 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3721 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3722 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3723 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3724 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3725 | /* we can't retry a failed decompression operation so we must be very |
| 3726 | * careful not to take any risks. In practice the output buffer is |
| 3727 | * always empty except maybe for trailers, in which case we simply have |
| 3728 | * to wait for the upper layer to finish consuming what is available. |
| 3729 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3730 | |
| 3731 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3732 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3733 | if (!htx_is_empty(htx)) { |
| 3734 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3735 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3736 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3737 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3738 | if (b_data(rxbuf)) { |
| 3739 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3740 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3741 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3742 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3743 | rxbuf->head = 0; |
| 3744 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3745 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3746 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3747 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3748 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3749 | sizeof(list)/sizeof(list[0]), tmp); |
| 3750 | if (outlen < 0) { |
| 3751 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3752 | goto fail; |
| 3753 | } |
| 3754 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3755 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3756 | * the parser's state to commit these changes and allow us to later |
| 3757 | * fail solely on the stream if needed. |
| 3758 | */ |
| 3759 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3760 | h2c->dfl = hole = 0; |
| 3761 | h2c->st0 = H2_CS_FRAME_H; |
| 3762 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3763 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3764 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3765 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3766 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3767 | goto trailers; |
| 3768 | |
| 3769 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3770 | if (htx) { |
| 3771 | /* HTX mode */ |
| 3772 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3773 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3774 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3775 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3776 | } else { |
| 3777 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3778 | 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] | 3779 | if (outlen > 0) |
| 3780 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3781 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3782 | |
| 3783 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3784 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3785 | goto fail; |
| 3786 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3787 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3788 | if (msgf & H2_MSGF_BODY) { |
| 3789 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3790 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3791 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3792 | if (htx) |
| 3793 | htx->extra = *body_len; |
| 3794 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3795 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3796 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3797 | } |
| 3798 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3799 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3800 | /* indicate that a HEADERS frame was received for this stream, except |
| 3801 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3802 | * expected. |
| 3803 | */ |
| 3804 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3805 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3806 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3807 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3808 | /* Mark the end of message, either using EOM in HTX or with the |
| 3809 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3810 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3811 | * we must not leave an HTX trailers block not followed by an |
| 3812 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3813 | * 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] | 3814 | */ |
| 3815 | if (htx) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 3816 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3817 | goto fail; |
| 3818 | } |
| 3819 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3820 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3821 | goto fail; |
| 3822 | } |
| 3823 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3824 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3825 | /* success */ |
| 3826 | ret = 1; |
| 3827 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3828 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3829 | /* 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] | 3830 | * move the remaining data over it. |
| 3831 | */ |
| 3832 | if (hole) { |
| 3833 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3834 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3835 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3836 | b_sub(&h2c->dbuf, hole); |
| 3837 | } |
| 3838 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3839 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3840 | /* too large frames */ |
| 3841 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3842 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3843 | } |
| 3844 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3845 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3846 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3847 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3848 | return ret; |
| 3849 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3850 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3851 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3852 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3853 | |
| 3854 | trailers: |
| 3855 | /* This is the last HEADERS frame hence a trailer */ |
| 3856 | |
| 3857 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3858 | /* It's a trailer but it's missing ES flag */ |
| 3859 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3860 | goto fail; |
| 3861 | } |
| 3862 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 3863 | /* Trailers terminate a DATA sequence. In HTX we always handle them. In |
| 3864 | * legacy, when using chunks, we have to emit the 0 CRLF marker first |
| 3865 | * and then handle the trailers. For other modes, the trailers are |
| 3866 | * silently dropped. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3867 | */ |
| 3868 | if (htx) { |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3869 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3870 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3871 | } |
| 3872 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3873 | /* Legacy mode with chunked encoding : we must finalize the |
| 3874 | * data block message emit the trailing CRLF */ |
| 3875 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3876 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3877 | |
| 3878 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3879 | if (outlen > 0) |
| 3880 | b_add(rxbuf, outlen); |
| 3881 | else |
| 3882 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3888 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3889 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3890 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3891 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3892 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3893 | * 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] | 3894 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3895 | * send the current frame, 0 if it couldn't complete, in which case |
| 3896 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3897 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3898 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3899 | * have checked the frame header and ensured that the frame was complete or the |
| 3900 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3901 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3902 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3903 | { |
| 3904 | struct h2c *h2c = h2s->h2c; |
| 3905 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3906 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3907 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3908 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3909 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3910 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3911 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3912 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3913 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3914 | if (!csbuf) { |
| 3915 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3916 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3917 | } |
| 3918 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3919 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3920 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3921 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3922 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3923 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3924 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3925 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3926 | if (flen > b_data(&h2c->dbuf)) { |
| 3927 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3928 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3929 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3930 | } |
| 3931 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3932 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3933 | unsigned int sent; |
| 3934 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3935 | block1 = htx_free_data_space(htx); |
| 3936 | if (!block1) { |
| 3937 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3938 | goto fail; |
| 3939 | } |
| 3940 | if (flen > block1) |
| 3941 | flen = block1; |
| 3942 | |
| 3943 | /* here, flen is the max we can copy into the output buffer */ |
| 3944 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3945 | if (flen > block1) |
| 3946 | flen = block1; |
| 3947 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3948 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3949 | |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3950 | b_del(&h2c->dbuf, sent); |
| 3951 | h2c->dfl -= sent; |
| 3952 | h2c->rcvd_c += sent; |
| 3953 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3954 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3955 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3956 | h2s->body_len -= sent; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3957 | htx->extra = h2s->body_len; |
| 3958 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3959 | |
| 3960 | if (sent < flen) { |
| 3961 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3962 | goto fail; |
| 3963 | } |
| 3964 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3965 | goto try_again; |
| 3966 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3967 | else if (unlikely(b_space_wraps(csbuf) && |
| 3968 | flen + chklen <= b_room(csbuf) && |
| 3969 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3970 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3971 | * not too many data in the buffer, let's defragment it and try |
| 3972 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3973 | */ |
| 3974 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3975 | } |
| 3976 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3977 | /* chunked-encoding requires more room */ |
| 3978 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3979 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3980 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3981 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3982 | (chklen < 1048576) ? 4 : 8; |
| 3983 | chklen += 4; // CRLF, CRLF |
| 3984 | } |
| 3985 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3986 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3987 | if (flen + chklen > b_room(csbuf)) { |
| 3988 | if (chklen >= b_room(csbuf)) { |
| 3989 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3990 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3991 | } |
| 3992 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3996 | /* emit the chunk size */ |
| 3997 | unsigned int chksz = flen; |
| 3998 | char str[10]; |
| 3999 | char *beg; |
| 4000 | |
| 4001 | beg = str + sizeof(str); |
| 4002 | *--beg = '\n'; |
| 4003 | *--beg = '\r'; |
| 4004 | do { |
| 4005 | *--beg = hextab[chksz & 0xF]; |
| 4006 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4007 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4008 | } |
| 4009 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4010 | /* Block1 is the length of the first block before the buffer wraps, |
| 4011 | * block2 is the optional second block to reach the end of the frame. |
| 4012 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4013 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4014 | if (block1 > flen) |
| 4015 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4016 | block2 = flen - block1; |
| 4017 | |
| 4018 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4019 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4020 | |
| 4021 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4022 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4023 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4024 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4025 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4026 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4027 | } |
| 4028 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4029 | /* now mark the input data as consumed (will be deleted from the buffer |
| 4030 | * by the caller when seeing FRAME_A after sending the window update). |
| 4031 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4032 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4033 | h2c->dfl -= flen; |
| 4034 | h2c->rcvd_c += flen; |
| 4035 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 4036 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 4037 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 4038 | h2s->body_len -= flen; |
| 4039 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4040 | if (h2c->dfl > h2c->dpl) { |
| 4041 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4042 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4043 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4044 | } |
| 4045 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 4046 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4047 | /* here we're done with the frame, all the payload (except padding) was |
| 4048 | * transferred. |
| 4049 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4050 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4051 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 4052 | if (htx) { |
| 4053 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 4054 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4055 | goto fail; |
| 4056 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4057 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4058 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4059 | /* emit the trailing 0 CRLF CRLF */ |
| 4060 | if (b_room(csbuf) < 5) { |
| 4061 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4062 | goto fail; |
| 4063 | } |
| 4064 | chklen += 5; |
| 4065 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 4066 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4067 | } |
| 4068 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 4069 | h2c->rcvd_c += h2c->dpl; |
| 4070 | h2c->rcvd_s += h2c->dpl; |
| 4071 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4072 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4073 | if (htx) |
| 4074 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4075 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4076 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4077 | if (htx) |
| 4078 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4079 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4080 | } |
| 4081 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4082 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 4083 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 4084 | * number of bytes sent. The caller must check the stream's status to detect |
| 4085 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4086 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4087 | 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] | 4088 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4089 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4090 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4091 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4092 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4093 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4094 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4095 | int es_now = 0; |
| 4096 | int ret = 0; |
| 4097 | int hdr; |
| 4098 | |
| 4099 | if (h2c_mux_busy(h2c, h2s)) { |
| 4100 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4101 | return 0; |
| 4102 | } |
| 4103 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4104 | /* First, try to parse the H1 response and index it into <list>. |
| 4105 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 4106 | * block does not wrap and we can safely read it this way without |
| 4107 | * having to realign the buffer. |
| 4108 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4109 | 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] | 4110 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4111 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 4112 | /* incomplete or invalid response, this is abnormal coming from |
| 4113 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 4114 | * so that won't be fixed, raise an error now. |
| 4115 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4116 | * FIXME: we should instead add the ability to only return a |
| 4117 | * 502 bad gateway. But in theory this is not supposed to |
| 4118 | * happen. |
| 4119 | */ |
| 4120 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4121 | ret = 0; |
| 4122 | goto end; |
| 4123 | } |
| 4124 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4125 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4126 | |
| 4127 | /* certain statuses have no body or an empty one, regardless of |
| 4128 | * what the headers say. |
| 4129 | */ |
| 4130 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4131 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4132 | h1m->curr_len = h1m->body_len = 0; |
| 4133 | } |
| 4134 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4135 | /* no contents, claim c-len is present and set to zero */ |
| 4136 | h1m->flags &= ~H1_MF_CHNK; |
| 4137 | h1m->flags |= H1_MF_CLEN; |
| 4138 | h1m->curr_len = h1m->body_len = 0; |
| 4139 | } |
| 4140 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4141 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4142 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4143 | if (!h2_get_buf(h2c, mbuf)) { |
| 4144 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4145 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4149 | chunk_reset(&outbuf); |
| 4150 | |
| 4151 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4152 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4153 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4154 | break; |
| 4155 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4156 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4157 | } |
| 4158 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4159 | if (outbuf.size < 9) |
| 4160 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4161 | |
| 4162 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4163 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4164 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4165 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4166 | |
| 4167 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4168 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4169 | /* this is an unparsable response */ |
| 4170 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4171 | ret = 0; |
| 4172 | goto end; |
| 4173 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4174 | |
| 4175 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4176 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4177 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4178 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /* encode all headers, stop at empty name */ |
| 4182 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4183 | /* these ones do not exist in H2 and must be dropped. */ |
| 4184 | if (isteq(list[hdr].n, ist("connection")) || |
| 4185 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4186 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4187 | isteq(list[hdr].n, ist("upgrade")) || |
| 4188 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4189 | continue; |
| 4190 | |
| 4191 | if (isteq(list[hdr].n, ist(""))) |
| 4192 | break; // end |
| 4193 | |
| 4194 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4195 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4196 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4197 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4198 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4203 | 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] | 4204 | es_now = 1; |
| 4205 | |
| 4206 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4207 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4208 | |
| 4209 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4210 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4211 | |
| 4212 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4213 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4214 | |
| 4215 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4216 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4217 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4218 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4219 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4220 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4221 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4222 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4223 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4224 | h2s->flags |= H2_SF_ES_SENT; |
| 4225 | if (h2s->st == H2_SS_OPEN) |
| 4226 | h2s->st = H2_SS_HLOC; |
| 4227 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4228 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4229 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4230 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4231 | /* 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] | 4232 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4233 | 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] | 4234 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4235 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4236 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4237 | |
| 4238 | /* 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] | 4239 | |
| 4240 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4241 | //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] | 4242 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4243 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4244 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4245 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4246 | h1m_init_res(h1m); |
| 4247 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4248 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4249 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4250 | ret = 0; |
| 4251 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4252 | } |
| 4253 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4254 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4255 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4256 | * the number of bytes sent. The caller must check the stream's status to |
| 4257 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4258 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4259 | 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] | 4260 | { |
| 4261 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4262 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4263 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4264 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4265 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4266 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4267 | int es_now = 0; |
| 4268 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4269 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4270 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4271 | |
| 4272 | if (h2c_mux_busy(h2c, h2s)) { |
| 4273 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4274 | goto end; |
| 4275 | } |
| 4276 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4277 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4278 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4279 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4280 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4281 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4282 | goto end; |
| 4283 | } |
| 4284 | |
| 4285 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4286 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4287 | goto end; |
| 4288 | |
| 4289 | chunk_reset(&outbuf); |
| 4290 | |
| 4291 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4292 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4293 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4294 | break; |
| 4295 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4296 | /* If there are pending data in the output buffer, and we have |
| 4297 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4298 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4299 | * is full and wait, to save some slow realign calls. |
| 4300 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4301 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4302 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4303 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4304 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4305 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4306 | goto end; |
| 4307 | } |
| 4308 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4309 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4313 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4314 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4315 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4316 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4317 | goto end; |
| 4318 | } |
| 4319 | |
| 4320 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4321 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4322 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4323 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4324 | |
| 4325 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4326 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4327 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4328 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4329 | break; |
| 4330 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4331 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4332 | if ((long long)size > h1m->curr_len) |
| 4333 | size = h1m->curr_len; |
| 4334 | break; |
| 4335 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4336 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4337 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 4338 | if (ret <= 0) { |
Christopher Faulet | 307f31e | 2020-08-05 14:37:13 +0200 | [diff] [blame] | 4339 | /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ? |
| 4340 | * It should not happen except if opposite connection was closed. |
| 4341 | * Note: check there is at least something to parse to be sure the chunk crlf |
| 4342 | * is truncated. |
| 4343 | */ |
| 4344 | if (max) { |
| 4345 | h1m->err_pos = ofs + max + ret; |
| 4346 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4347 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4348 | goto end; |
| 4349 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4350 | max -= ret; |
| 4351 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4352 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4353 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4354 | } |
| 4355 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4356 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4357 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4358 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 4359 | if (ret <= 0) { |
Christopher Faulet | 307f31e | 2020-08-05 14:37:13 +0200 | [diff] [blame] | 4360 | /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ? |
| 4361 | * It should not happen except if opposite connection was closed. |
| 4362 | * Note: check there is at least something to parse to be sure the chunke size |
| 4363 | * is truncated. |
| 4364 | */ |
| 4365 | if (max) { |
| 4366 | h1m->err_pos = ofs + max + ret; |
| 4367 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4368 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4369 | goto end; |
| 4370 | } |
| 4371 | |
| 4372 | size = chunk; |
| 4373 | h1m->curr_len = chunk; |
| 4374 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4375 | max -= ret; |
| 4376 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4377 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4378 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4379 | if (!size) |
| 4380 | goto send_empty; |
| 4381 | } |
| 4382 | |
| 4383 | /* in MSG_DATA state, continue below */ |
| 4384 | size = h1m->curr_len; |
| 4385 | break; |
| 4386 | } |
| 4387 | |
| 4388 | /* we have in <size> the exact number of bytes we need to copy from |
| 4389 | * the H1 buffer. We need to check this against the connection's and |
| 4390 | * the stream's send windows, and to ensure that this fits in the max |
| 4391 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4392 | * the frame header). The connection's flow control is applied last so |
| 4393 | * that we can use a separate list of streams which are immediately |
| 4394 | * unblocked on window opening. Note: we don't implement padding. |
| 4395 | */ |
| 4396 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4397 | if (size > max) |
| 4398 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4399 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4400 | if (size > h2s_mws(h2s)) |
| 4401 | size = h2s_mws(h2s); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4402 | |
| 4403 | if (size <= 0) { |
| 4404 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4405 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4406 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 55dc084 | 2019-10-22 10:04:39 +0200 | [diff] [blame] | 4407 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4408 | goto end; |
| 4409 | } |
| 4410 | |
| 4411 | if (h2c->mfs && size > h2c->mfs) |
| 4412 | size = h2c->mfs; |
| 4413 | |
| 4414 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4415 | /* It doesn't fit at once. If it at least fits once split and |
| 4416 | * the amount of data to move is low, let's defragment the |
| 4417 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4418 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4419 | if (b_space_wraps(mbuf) && |
| 4420 | (size + 9 <= b_room(mbuf)) && |
| 4421 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4422 | goto realign_again; |
| 4423 | size = outbuf.size - 9; |
| 4424 | } |
| 4425 | |
| 4426 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4427 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4428 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4429 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4430 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4431 | goto end; |
| 4432 | } |
| 4433 | |
| 4434 | if (size > h2c->mws) |
| 4435 | size = h2c->mws; |
| 4436 | |
| 4437 | if (size <= 0) { |
| 4438 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4439 | goto end; |
| 4440 | } |
| 4441 | |
| 4442 | /* copy whatever we can */ |
| 4443 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4444 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4445 | if (ret == 1) |
| 4446 | len2 = 0; |
| 4447 | |
| 4448 | if (!ret || len1 + len2 < size) { |
| 4449 | /* FIXME: must normally never happen */ |
| 4450 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4451 | goto end; |
| 4452 | } |
| 4453 | |
| 4454 | /* limit len1/len2 to size */ |
| 4455 | if (len1 + len2 > size) { |
| 4456 | int sub = len1 + len2 - size; |
| 4457 | |
| 4458 | if (len2 > sub) |
| 4459 | len2 -= sub; |
| 4460 | else { |
| 4461 | sub -= len2; |
| 4462 | len2 = 0; |
| 4463 | len1 -= sub; |
| 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4468 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4469 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4470 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4471 | |
| 4472 | send_empty: |
| 4473 | /* we may need to add END_STREAM */ |
| 4474 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4475 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4476 | * |
| 4477 | * FIXME: what we do here is not correct because we send end_stream |
| 4478 | * before knowing if we'll have to send a HEADERS frame for the |
| 4479 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4480 | * after the end of trailers, so it will be left to the caller to |
| 4481 | * eat it. The right way to do it would be to measure trailers here |
| 4482 | * and to send ES only if there are no trailers. |
| 4483 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4484 | */ |
| 4485 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4486 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4487 | es_now = 1; |
| 4488 | |
| 4489 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4490 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4491 | |
| 4492 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4493 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4494 | |
| 4495 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4496 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4497 | |
| 4498 | /* consume incoming H1 response */ |
| 4499 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4500 | max -= size; |
| 4501 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4502 | total += size; |
| 4503 | h1m->curr_len -= size; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4504 | h2s->sws -= size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4505 | h2c->mws -= size; |
| 4506 | |
| 4507 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4508 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4509 | goto new_frame; |
| 4510 | } |
| 4511 | } |
| 4512 | |
| 4513 | if (es_now) { |
| 4514 | if (h2s->st == H2_SS_OPEN) |
| 4515 | h2s->st = H2_SS_HLOC; |
| 4516 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4517 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4518 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4519 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4520 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4521 | total += max; |
| 4522 | ofs += max; |
| 4523 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4524 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4525 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4526 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4527 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4528 | h2s->flags |= H2_SF_ES_SENT; |
| 4529 | } |
| 4530 | |
| 4531 | end: |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4532 | 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] | 4533 | return total; |
| 4534 | } |
| 4535 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4536 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4537 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4538 | * must check the stream's status to detect any error which might have happened |
| 4539 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4540 | * from the message. The htx message is assumed to be valid since produced from |
| 4541 | * the internal code, hence it contains a start line, an optional series of |
| 4542 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4543 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4544 | */ |
| 4545 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4546 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4547 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4548 | struct h2c *h2c = h2s->h2c; |
| 4549 | struct htx_blk *blk; |
| 4550 | struct htx_blk *blk_end; |
| 4551 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4552 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4553 | struct htx_sl *sl; |
| 4554 | enum htx_blk_type type; |
| 4555 | int es_now = 0; |
| 4556 | int ret = 0; |
| 4557 | int hdr; |
| 4558 | int idx; |
| 4559 | |
| 4560 | if (h2c_mux_busy(h2c, h2s)) { |
| 4561 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4562 | return 0; |
| 4563 | } |
| 4564 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4565 | /* determine the first block which must not be deleted, blk_end may |
| 4566 | * be NULL if all blocks have to be deleted. |
| 4567 | */ |
| 4568 | idx = htx_get_head(htx); |
| 4569 | blk_end = NULL; |
| 4570 | while (idx != -1) { |
| 4571 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4572 | idx = htx_get_next(htx, idx); |
| 4573 | if (type == HTX_BLK_EOH) { |
| 4574 | if (idx != -1) |
| 4575 | blk_end = htx_get_blk(htx, idx); |
| 4576 | break; |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4581 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4582 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4583 | ALREADY_CHECKED(blk); |
| 4584 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4585 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4586 | if (h2s->status < 100 || h2s->status > 999) |
| 4587 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4588 | |
| 4589 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4590 | hdr = 0; |
| 4591 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4592 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4593 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4594 | blk = htx_get_blk(htx, idx); |
| 4595 | type = htx_get_blk_type(blk); |
| 4596 | |
| 4597 | if (type == HTX_BLK_UNUSED) |
| 4598 | continue; |
| 4599 | |
| 4600 | if (type != HTX_BLK_HDR) |
| 4601 | break; |
| 4602 | |
| 4603 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4604 | goto fail; |
| 4605 | |
| 4606 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4607 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4608 | hdr++; |
| 4609 | } |
| 4610 | |
| 4611 | /* marker for end of headers */ |
| 4612 | list[hdr].n = ist(""); |
| 4613 | |
| 4614 | if (h2s->status == 204 || h2s->status == 304) { |
| 4615 | /* no contents, claim c-len is present and set to zero */ |
| 4616 | es_now = 1; |
| 4617 | } |
| 4618 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4619 | mbuf = br_tail(h2c->mbuf); |
| 4620 | retry: |
| 4621 | if (!h2_get_buf(h2c, mbuf)) { |
| 4622 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4623 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4624 | return 0; |
| 4625 | } |
| 4626 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4627 | chunk_reset(&outbuf); |
| 4628 | |
| 4629 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4630 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4631 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4632 | break; |
| 4633 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4634 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | if (outbuf.size < 9) |
| 4638 | goto full; |
| 4639 | |
| 4640 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4641 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4642 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4643 | outbuf.data = 9; |
| 4644 | |
| 4645 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4646 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4647 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4648 | goto realign_again; |
| 4649 | goto full; |
| 4650 | } |
| 4651 | |
| 4652 | /* encode all headers, stop at empty name */ |
| 4653 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4654 | /* these ones do not exist in H2 and must be dropped. */ |
| 4655 | if (isteq(list[hdr].n, ist("connection")) || |
| 4656 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4657 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4658 | isteq(list[hdr].n, ist("upgrade")) || |
| 4659 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4660 | continue; |
| 4661 | |
| 4662 | if (isteq(list[hdr].n, ist(""))) |
| 4663 | break; // end |
| 4664 | |
| 4665 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4666 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4667 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4668 | goto realign_again; |
| 4669 | goto full; |
| 4670 | } |
| 4671 | } |
| 4672 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4673 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4674 | * FIXME: we should also set it when we know for sure that the |
| 4675 | * content-length is zero as well as on 204/304 |
| 4676 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4677 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4678 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4679 | es_now = 1; |
| 4680 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4681 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4682 | es_now = 1; |
| 4683 | |
| 4684 | /* update the frame's size */ |
| 4685 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4686 | |
| 4687 | if (es_now) |
| 4688 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4689 | |
| 4690 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4691 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4692 | |
| 4693 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4694 | * 1xx responses, another HEADERS frame is expected. |
| 4695 | */ |
| 4696 | if (h2s->status >= 200 || h2s->status == 101) |
| 4697 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4698 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4699 | if (es_now) { |
| 4700 | h2s->flags |= H2_SF_ES_SENT; |
| 4701 | if (h2s->st == H2_SS_OPEN) |
| 4702 | h2s->st = H2_SS_HLOC; |
| 4703 | else |
| 4704 | h2s_close(h2s); |
| 4705 | } |
| 4706 | |
| 4707 | /* OK we could properly deliver the response */ |
| 4708 | |
| 4709 | /* remove all header blocks including the EOH and compute the |
| 4710 | * corresponding size. |
| 4711 | * |
| 4712 | * FIXME: We should remove everything when es_now is set. |
| 4713 | */ |
| 4714 | ret = 0; |
| 4715 | idx = htx_get_head(htx); |
| 4716 | blk = htx_get_blk(htx, idx); |
| 4717 | while (blk != blk_end) { |
| 4718 | ret += htx_get_blksz(blk); |
| 4719 | blk = htx_remove_blk(htx, blk); |
| 4720 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4721 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4722 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4723 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4724 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4725 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4726 | end: |
| 4727 | return ret; |
| 4728 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4729 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4730 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4731 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4732 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4733 | ret = 0; |
| 4734 | goto end; |
| 4735 | fail: |
| 4736 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4737 | * list etc go here (unrecoverable errors). |
| 4738 | */ |
| 4739 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4740 | ret = 0; |
| 4741 | goto end; |
| 4742 | } |
| 4743 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4744 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4745 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4746 | * must check the stream's status to detect any error which might have happened |
| 4747 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4748 | * from the message. The htx message is assumed to be valid since produced from |
| 4749 | * the internal code, hence it contains a start line, an optional series of |
| 4750 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4751 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4752 | */ |
| 4753 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4754 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4755 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4756 | struct h2c *h2c = h2s->h2c; |
| 4757 | struct htx_blk *blk; |
| 4758 | struct htx_blk *blk_end; |
| 4759 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4760 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4761 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4762 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4763 | enum htx_blk_type type; |
| 4764 | int es_now = 0; |
| 4765 | int ret = 0; |
| 4766 | int hdr; |
| 4767 | int idx; |
| 4768 | |
| 4769 | if (h2c_mux_busy(h2c, h2s)) { |
| 4770 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4771 | return 0; |
| 4772 | } |
| 4773 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4774 | /* determine the first block which must not be deleted, blk_end may |
| 4775 | * be NULL if all blocks have to be deleted. |
| 4776 | */ |
| 4777 | idx = htx_get_head(htx); |
| 4778 | blk_end = NULL; |
| 4779 | while (idx != -1) { |
| 4780 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4781 | idx = htx_get_next(htx, idx); |
| 4782 | if (type == HTX_BLK_EOH) { |
| 4783 | if (idx != -1) |
| 4784 | blk_end = htx_get_blk(htx, idx); |
| 4785 | break; |
| 4786 | } |
| 4787 | } |
| 4788 | |
| 4789 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4790 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4791 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4792 | ALREADY_CHECKED(blk); |
| 4793 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4794 | meth = htx_sl_req_meth(sl); |
| 4795 | path = htx_sl_req_uri(sl); |
| 4796 | |
| 4797 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4798 | hdr = 0; |
| 4799 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4800 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4801 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4802 | blk = htx_get_blk(htx, idx); |
| 4803 | type = htx_get_blk_type(blk); |
| 4804 | |
| 4805 | if (type == HTX_BLK_UNUSED) |
| 4806 | continue; |
| 4807 | |
| 4808 | if (type != HTX_BLK_HDR) |
| 4809 | break; |
| 4810 | |
| 4811 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4812 | goto fail; |
| 4813 | |
| 4814 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4815 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4816 | hdr++; |
| 4817 | } |
| 4818 | |
| 4819 | /* marker for end of headers */ |
| 4820 | list[hdr].n = ist(""); |
| 4821 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4822 | mbuf = br_tail(h2c->mbuf); |
| 4823 | retry: |
| 4824 | if (!h2_get_buf(h2c, mbuf)) { |
| 4825 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4826 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4827 | return 0; |
| 4828 | } |
| 4829 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4830 | chunk_reset(&outbuf); |
| 4831 | |
| 4832 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4833 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4834 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4835 | break; |
| 4836 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4837 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | if (outbuf.size < 9) |
| 4841 | goto full; |
| 4842 | |
| 4843 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4844 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4845 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4846 | outbuf.data = 9; |
| 4847 | |
| 4848 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4849 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4850 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4851 | goto realign_again; |
| 4852 | goto full; |
| 4853 | } |
| 4854 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4855 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4856 | * - :authority set to the URI part (host:port) |
| 4857 | * - :method set to CONNECT |
| 4858 | * - :scheme and :path omitted |
| 4859 | */ |
| 4860 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4861 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4862 | struct ist scheme; |
| 4863 | |
| 4864 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4865 | scheme = ist("http"); |
| 4866 | else |
| 4867 | scheme = ist("https"); |
| 4868 | |
| 4869 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4870 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4871 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4872 | goto realign_again; |
| 4873 | goto full; |
| 4874 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4875 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4876 | /* encode the path, which necessarily is the second one */ |
| 4877 | if (!hpack_encode_path(&outbuf, path)) { |
| 4878 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4879 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4880 | goto realign_again; |
| 4881 | goto full; |
| 4882 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4883 | |
| 4884 | /* look for the Host header and place it in :authority */ |
| 4885 | auth = ist2(NULL, 0); |
| 4886 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4887 | if (isteq(list[hdr].n, ist(""))) |
| 4888 | break; // end |
| 4889 | |
| 4890 | if (isteq(list[hdr].n, ist("host"))) { |
| 4891 | auth = list[hdr].v; |
| 4892 | break; |
| 4893 | } |
| 4894 | } |
| 4895 | } |
| 4896 | else { |
| 4897 | /* for CONNECT, :authority is taken from the path */ |
| 4898 | auth = path; |
| 4899 | } |
| 4900 | |
| 4901 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4902 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4903 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4904 | goto realign_again; |
| 4905 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | /* encode all headers, stop at empty name */ |
| 4909 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4910 | struct ist n = list[hdr].n; |
| 4911 | struct ist v = list[hdr].v; |
| 4912 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4913 | /* these ones do not exist in H2 and must be dropped. */ |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4914 | if (isteq(n, ist("connection")) || |
| 4915 | isteq(n, ist("host")) || |
| 4916 | isteq(n, ist("proxy-connection")) || |
| 4917 | isteq(n, ist("keep-alive")) || |
| 4918 | isteq(n, ist("upgrade")) || |
| 4919 | isteq(n, ist("transfer-encoding"))) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4920 | continue; |
| 4921 | |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4922 | if (isteq(n, ist("te"))) { |
| 4923 | /* "te" may only be sent with "trailers" if this value |
| 4924 | * is present, otherwise it must be deleted. |
| 4925 | */ |
| 4926 | v = istist(v, ist("trailers")); |
| 4927 | if (!v.ptr || (v.len > 8 && v.ptr[8] != ',')) |
| 4928 | continue; |
| 4929 | v = ist("trailers"); |
| 4930 | } |
| 4931 | |
| 4932 | if (isteq(n, ist(""))) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4933 | break; // end |
| 4934 | |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4935 | if (!hpack_encode_header(&outbuf, n, v)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4936 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4937 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4938 | goto realign_again; |
| 4939 | goto full; |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | /* we may need to add END_STREAM if we have no body : |
| 4944 | * - request already closed, or : |
| 4945 | * - no transfer-encoding, and : |
| 4946 | * - no content-length or content-length:0 |
| 4947 | * Fixme: this doesn't take into account CONNECT requests. |
| 4948 | */ |
| 4949 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4950 | es_now = 1; |
| 4951 | |
| 4952 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4953 | es_now = 1; |
| 4954 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4955 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4956 | es_now = 1; |
| 4957 | |
| 4958 | /* update the frame's size */ |
| 4959 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4960 | |
| 4961 | if (es_now) |
| 4962 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4963 | |
| 4964 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4965 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4966 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4967 | h2s->st = H2_SS_OPEN; |
| 4968 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4969 | if (es_now) { |
| 4970 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4971 | h2s->flags |= H2_SF_ES_SENT; |
| 4972 | h2s->st = H2_SS_HLOC; |
| 4973 | } |
| 4974 | |
| 4975 | /* remove all header blocks including the EOH and compute the |
| 4976 | * corresponding size. |
| 4977 | * |
| 4978 | * FIXME: We should remove everything when es_now is set. |
| 4979 | */ |
| 4980 | ret = 0; |
| 4981 | idx = htx_get_head(htx); |
| 4982 | blk = htx_get_blk(htx, idx); |
| 4983 | while (blk != blk_end) { |
| 4984 | ret += htx_get_blksz(blk); |
| 4985 | blk = htx_remove_blk(htx, blk); |
| 4986 | } |
| 4987 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4988 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4989 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4990 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4991 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4992 | |
| 4993 | end: |
| 4994 | return ret; |
| 4995 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4996 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4997 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4998 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4999 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5000 | ret = 0; |
| 5001 | goto end; |
| 5002 | fail: |
| 5003 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5004 | * list etc go here (unrecoverable errors). |
| 5005 | */ |
| 5006 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5007 | ret = 0; |
| 5008 | goto end; |
| 5009 | } |
| 5010 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5011 | /* 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] | 5012 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 5013 | * caller must check the stream's status to detect any error which might have |
| 5014 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5015 | * 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] | 5016 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5017 | 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] | 5018 | { |
| 5019 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5020 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5021 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5022 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5023 | size_t total = 0; |
| 5024 | int es_now = 0; |
| 5025 | int bsize; /* htx block size */ |
| 5026 | int fsize; /* h2 frame size */ |
| 5027 | struct htx_blk *blk; |
| 5028 | enum htx_blk_type type; |
| 5029 | int idx; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5030 | int trunc_out; /* non-zero if truncated on out buf */ |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5031 | |
| 5032 | if (h2c_mux_busy(h2c, h2s)) { |
| 5033 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5034 | goto end; |
| 5035 | } |
| 5036 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5037 | htx = htx_from_buf(buf); |
| 5038 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5039 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 5040 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 5041 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5042 | */ |
| 5043 | |
| 5044 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5045 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5046 | goto end; |
| 5047 | |
| 5048 | idx = htx_get_head(htx); |
| 5049 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5050 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5051 | bsize = htx_get_blksz(blk); |
| 5052 | fsize = bsize; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5053 | trunc_out = 0; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5054 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5055 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 5056 | if (h2s->flags & H2_SF_ES_SENT) { |
| 5057 | /* ES already sent */ |
| 5058 | htx_remove_blk(htx, blk); |
| 5059 | total++; // EOM counts as one byte |
| 5060 | count--; |
| 5061 | goto end; |
| 5062 | } |
| 5063 | } |
| 5064 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5065 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5066 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5067 | mbuf = br_tail(h2c->mbuf); |
| 5068 | retry: |
| 5069 | if (!h2_get_buf(h2c, mbuf)) { |
| 5070 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5071 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5072 | goto end; |
| 5073 | } |
| 5074 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5075 | /* Perform some optimizations to reduce the number of buffer copies. |
| 5076 | * First, if the mux's buffer is empty and the htx area contains |
| 5077 | * exactly one data block of the same size as the requested count, and |
| 5078 | * this count fits within the frame size, the stream's window size, and |
| 5079 | * the connection's window size, then it's possible to simply swap the |
| 5080 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 5081 | * length to match the entire DATA HTX block in the middle. In this |
| 5082 | * case we perform a true zero-copy operation from end-to-end. This is |
| 5083 | * the situation that happens all the time with large files. Second, if |
| 5084 | * this is not possible, but the mux's output buffer is empty, we still |
| 5085 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 5086 | * making the intermediary buffer's area point to the output buffer's |
| 5087 | * area. In this case we want to skip the HTX header to make sure that |
| 5088 | * copies remain aligned and that this operation remains possible all |
| 5089 | * the time. This goes for headers, data blocks and any data extracted |
| 5090 | * from the HTX blocks. |
| 5091 | */ |
| 5092 | if (unlikely(fsize == count && |
| 5093 | htx->used == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5094 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5095 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5096 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5097 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5098 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 5099 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 5100 | * frame into a buffer containing few data if it needs to be realigned, |
| 5101 | * and that it's also OK to copy few data without realigning. Otherwise |
| 5102 | * 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] | 5103 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5104 | if (fsize + 9 <= b_room(mbuf) && |
| 5105 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 5106 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5107 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5108 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5109 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5110 | goto retry; |
| 5111 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5112 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5113 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5114 | goto end; |
| 5115 | } |
| 5116 | |
| 5117 | /* map an H2 frame to the HTX block so that we can put the |
| 5118 | * frame header there. |
| 5119 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5120 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 5121 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5122 | |
| 5123 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 5124 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5125 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5126 | h2_set_frame_size(outbuf.area, fsize); |
| 5127 | |
| 5128 | /* update windows */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5129 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5130 | h2c->mws -= fsize; |
| 5131 | |
| 5132 | /* and exchange with our old area */ |
| 5133 | buf->area = old_area; |
| 5134 | buf->data = buf->head = 0; |
| 5135 | total += fsize; |
| 5136 | goto end; |
| 5137 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5138 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5139 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5140 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 5141 | |
| 5142 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5143 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5144 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5145 | break; |
| 5146 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5147 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5151 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5152 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5153 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5154 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5155 | goto end; |
| 5156 | } |
| 5157 | |
| 5158 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5159 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5160 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5161 | outbuf.data = 9; |
| 5162 | |
| 5163 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5164 | * the HTX buffer. We need to check this against the connection's and |
| 5165 | * the stream's send windows, and to ensure that this fits in the max |
| 5166 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5167 | * the frame header). The connection's flow control is applied last so |
| 5168 | * that we can use a separate list of streams which are immediately |
| 5169 | * unblocked on window opening. Note: we don't implement padding. |
| 5170 | */ |
| 5171 | |
| 5172 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5173 | * empty frame, thus we force it to zero here. |
| 5174 | */ |
| 5175 | if (type == HTX_BLK_EOM) |
| 5176 | bsize = fsize = 0; |
| 5177 | |
| 5178 | if (!fsize) |
| 5179 | goto send_empty; |
| 5180 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5181 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5182 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5183 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5184 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5185 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5186 | goto end; |
| 5187 | } |
| 5188 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5189 | if (fsize > count) |
| 5190 | fsize = count; |
| 5191 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5192 | if (fsize > h2s_mws(h2s)) |
| 5193 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5194 | |
| 5195 | if (h2c->mfs && fsize > h2c->mfs) |
| 5196 | fsize = h2c->mfs; // >0 |
| 5197 | |
| 5198 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5199 | /* It doesn't fit at once. If it at least fits once split and |
| 5200 | * the amount of data to move is low, let's defragment the |
| 5201 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5202 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5203 | if (b_space_wraps(mbuf) && |
| 5204 | (fsize + 9 <= b_room(mbuf)) && |
| 5205 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5206 | goto realign_again; |
| 5207 | fsize = outbuf.size - 9; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5208 | trunc_out = 1; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5209 | |
| 5210 | if (fsize <= 0) { |
| 5211 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5212 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5213 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5214 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5215 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5216 | goto end; |
| 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | if (h2c->mws <= 0) { |
| 5221 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5222 | goto end; |
| 5223 | } |
| 5224 | |
| 5225 | if (fsize > h2c->mws) |
| 5226 | fsize = h2c->mws; |
| 5227 | |
| 5228 | /* now let's copy this this into the output buffer */ |
| 5229 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5230 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5231 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5232 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5233 | |
| 5234 | send_empty: |
| 5235 | /* update the frame's size */ |
| 5236 | h2_set_frame_size(outbuf.area, fsize); |
| 5237 | |
| 5238 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5239 | * meeting EOM. We should optimize this later. |
| 5240 | */ |
| 5241 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5242 | total++; // EOM counts as one byte |
| 5243 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5244 | es_now = 1; |
| 5245 | } |
| 5246 | |
| 5247 | if (es_now) |
| 5248 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5249 | |
| 5250 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5251 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5252 | |
| 5253 | /* consume incoming HTX block, including EOM */ |
| 5254 | total += fsize; |
| 5255 | if (fsize == bsize) { |
| 5256 | htx_remove_blk(htx, blk); |
| 5257 | if (fsize) |
| 5258 | goto new_frame; |
| 5259 | } else { |
| 5260 | /* we've truncated this block */ |
| 5261 | htx_cut_data_blk(htx, blk, fsize); |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5262 | if (trunc_out) |
| 5263 | goto new_frame; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | if (es_now) { |
| 5267 | if (h2s->st == H2_SS_OPEN) |
| 5268 | h2s->st = H2_SS_HLOC; |
| 5269 | else |
| 5270 | h2s_close(h2s); |
| 5271 | |
| 5272 | h2s->flags |= H2_SF_ES_SENT; |
| 5273 | } |
| 5274 | |
| 5275 | end: |
| 5276 | return total; |
| 5277 | } |
| 5278 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5279 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5280 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5281 | * processed. The caller must check the stream's status to detect any error |
| 5282 | * which might have happened subsequently to a successful send. The htx blocks |
| 5283 | * are automatically removed from the message. The htx message is assumed to be |
| 5284 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5285 | * 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] | 5286 | * as a single frame. The ES flag is always set. |
| 5287 | */ |
| 5288 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5289 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 5290 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5291 | struct h2c *h2c = h2s->h2c; |
| 5292 | struct htx_blk *blk; |
| 5293 | struct htx_blk *blk_end; |
| 5294 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5295 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5296 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5297 | int ret = 0; |
| 5298 | int hdr; |
| 5299 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5300 | |
| 5301 | if (h2c_mux_busy(h2c, h2s)) { |
| 5302 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5303 | goto end; |
| 5304 | } |
| 5305 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5306 | /* determine the first block which must not be deleted, blk_end may |
| 5307 | * be NULL if all blocks have to be deleted. also get trailers. |
| 5308 | */ |
| 5309 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5310 | blk_end = NULL; |
| 5311 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5312 | hdr = 0; |
| 5313 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5314 | blk = htx_get_blk(htx, idx); |
| 5315 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5316 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5317 | if (type == HTX_BLK_UNUSED) |
| 5318 | continue; |
| 5319 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5320 | if (type == HTX_BLK_EOT) { |
| 5321 | if (idx != -1) |
| 5322 | blk_end = blk; |
| 5323 | break; |
| 5324 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5325 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5326 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5327 | |
| 5328 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5329 | goto fail; |
| 5330 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5331 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 5332 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 5333 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5334 | } |
| 5335 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5336 | /* marker for end of trailers */ |
| 5337 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5338 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5339 | mbuf = br_tail(h2c->mbuf); |
| 5340 | retry: |
| 5341 | if (!h2_get_buf(h2c, mbuf)) { |
| 5342 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5343 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5344 | goto end; |
| 5345 | } |
| 5346 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5347 | chunk_reset(&outbuf); |
| 5348 | |
| 5349 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5350 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5351 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5352 | break; |
| 5353 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5354 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | if (outbuf.size < 9) |
| 5358 | goto full; |
| 5359 | |
| 5360 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5361 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5362 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5363 | outbuf.data = 9; |
| 5364 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5365 | /* encode all headers */ |
| 5366 | for (idx = 0; idx < hdr; idx++) { |
| 5367 | /* these ones do not exist in H2 or must not appear in |
| 5368 | * trailers and must be dropped. |
| 5369 | */ |
| 5370 | if (isteq(list[idx].n, ist("host")) || |
| 5371 | isteq(list[idx].n, ist("content-length")) || |
| 5372 | isteq(list[idx].n, ist("connection")) || |
| 5373 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5374 | isteq(list[idx].n, ist("keep-alive")) || |
| 5375 | isteq(list[idx].n, ist("upgrade")) || |
| 5376 | isteq(list[idx].n, ist("te")) || |
| 5377 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5378 | continue; |
| 5379 | |
| 5380 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5381 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5382 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5383 | goto realign_again; |
| 5384 | goto full; |
| 5385 | } |
| 5386 | } |
| 5387 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5388 | if (outbuf.data == 9) { |
| 5389 | /* here we have a problem, we have nothing to emit (either we |
| 5390 | * received an empty trailers block followed or we removed its |
| 5391 | * contents above). Because of this we can't send a HEADERS |
| 5392 | * frame, so we have to cheat and instead send an empty DATA |
| 5393 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5394 | */ |
| 5395 | outbuf.area[3] = H2_FT_DATA; |
| 5396 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5397 | } |
| 5398 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5399 | /* update the frame's size */ |
| 5400 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5401 | |
| 5402 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5403 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5404 | h2s->flags |= H2_SF_ES_SENT; |
| 5405 | |
| 5406 | if (h2s->st == H2_SS_OPEN) |
| 5407 | h2s->st = H2_SS_HLOC; |
| 5408 | else |
| 5409 | h2s_close(h2s); |
| 5410 | |
| 5411 | /* OK we could properly deliver the response */ |
| 5412 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5413 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5414 | ret = 0; |
| 5415 | idx = htx_get_head(htx); |
| 5416 | blk = htx_get_blk(htx, idx); |
| 5417 | while (blk != blk_end) { |
| 5418 | ret += htx_get_blksz(blk); |
| 5419 | blk = htx_remove_blk(htx, blk); |
| 5420 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5421 | |
| 5422 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5423 | ret += htx_get_blksz(blk_end); |
| 5424 | htx_remove_blk(htx, blk_end); |
| 5425 | } |
| 5426 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5427 | end: |
| 5428 | return ret; |
| 5429 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5430 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5431 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5432 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5433 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5434 | ret = 0; |
| 5435 | goto end; |
| 5436 | fail: |
| 5437 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5438 | * list etc go here (unrecoverable errors). |
| 5439 | */ |
| 5440 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5441 | ret = 0; |
| 5442 | goto end; |
| 5443 | } |
| 5444 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5445 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5446 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5447 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5448 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5449 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5450 | * returns 0 except for the error above. |
| 5451 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5452 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5453 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5454 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5455 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5456 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5457 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5458 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5459 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5460 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5461 | sw->events |= SUB_RETRY_RECV; |
| 5462 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5463 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5464 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5465 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5466 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5467 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5468 | sw->events |= SUB_RETRY_SEND; |
| 5469 | h2s->send_wait = sw; |
| 5470 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5471 | !LIST_ADDED(&h2s->list)) { |
| 5472 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5473 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5474 | else |
| 5475 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5476 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5477 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5478 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5479 | if (event_type != 0) |
| 5480 | return -1; |
| 5481 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5482 | } |
| 5483 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5484 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5485 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5486 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5487 | * It always returns zero. |
| 5488 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5489 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5490 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5491 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5492 | struct h2s *h2s = cs->ctx; |
| 5493 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5494 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5495 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5496 | BUG_ON(h2s->recv_wait != sw); |
| 5497 | sw->events &= ~SUB_RETRY_RECV; |
| 5498 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5499 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5500 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5501 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5502 | BUG_ON(h2s->send_wait != sw); |
| 5503 | LIST_DEL(&h2s->list); |
| 5504 | LIST_INIT(&h2s->list); |
| 5505 | sw->events &= ~SUB_RETRY_SEND; |
| 5506 | /* We were about to send, make sure it does not happen */ |
| 5507 | if (LIST_ADDED(&h2s->sending_list) && |
| 5508 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5509 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5510 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5511 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5512 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5513 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5514 | return 0; |
| 5515 | } |
| 5516 | |
| 5517 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5518 | /* Called from the upper layer, to receive data */ |
| 5519 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5520 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5521 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5522 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5523 | struct htx *h2s_htx = NULL; |
| 5524 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5525 | size_t ret = 0; |
| 5526 | |
| 5527 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5528 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5529 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5530 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5531 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5532 | * the HTX is empty. |
| 5533 | */ |
| 5534 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5535 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5536 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5537 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5538 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5539 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5540 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5541 | /* <buf> is empty and the message is small enough, swap the |
| 5542 | * buffers. */ |
| 5543 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5544 | htx_to_buf(buf_htx, buf); |
| 5545 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5546 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5547 | goto end; |
| 5548 | } |
| 5549 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5550 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5551 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5552 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5553 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5554 | if (htx_is_empty(buf_htx)) |
| 5555 | cs->flags |= CS_FL_EOI; |
| 5556 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5557 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5558 | 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] | 5559 | htx_to_buf(buf_htx, buf); |
| 5560 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5561 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5562 | } |
| 5563 | else { |
| 5564 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5565 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5566 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5567 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5568 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5569 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5570 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5571 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5572 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5573 | cs->flags |= CS_FL_EOI; |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 5574 | if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5575 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5576 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5577 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5578 | if (b_size(&h2s->rxbuf)) { |
| 5579 | b_free(&h2s->rxbuf); |
| 5580 | offer_buffers(NULL, tasks_run_queue); |
| 5581 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5582 | } |
| 5583 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5584 | if (ret && h2c->dsi == h2s->id) { |
| 5585 | /* demux is blocking on this stream's buffer */ |
| 5586 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5587 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5588 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5589 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5590 | return ret; |
| 5591 | } |
| 5592 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5593 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5594 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5595 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5596 | static void h2_stop_senders(struct h2c *h2c) |
| 5597 | { |
| 5598 | struct h2s *h2s, *h2s_back; |
| 5599 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5600 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5601 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5602 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5603 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5607 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5608 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5609 | * flags may be updated on the conn_stream. |
| 5610 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5611 | 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] | 5612 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5613 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5614 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5615 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5616 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5617 | struct htx *htx; |
| 5618 | struct htx_blk *blk; |
| 5619 | enum htx_blk_type btype; |
| 5620 | uint32_t bsize; |
| 5621 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5622 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5623 | /* If we were not just woken because we wanted to send but couldn't, |
| 5624 | * and there's somebody else that is waiting to send, do nothing, |
| 5625 | * we will subscribe later and be put at the end of the list |
| 5626 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5627 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5628 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5629 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5630 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5631 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5632 | /* We couldn't set it to NULL before, because we needed it in case |
| 5633 | * we had to cancel the tasklet |
| 5634 | */ |
| 5635 | h2s->send_wait = NULL; |
| 5636 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5637 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5638 | return 0; |
| 5639 | |
Willy Tarreau | 902df22 | 2019-10-31 15:48:18 +0100 | [diff] [blame] | 5640 | if (h2s->h2c->st0 >= H2_CS_ERROR) { |
| 5641 | cs->flags |= CS_FL_ERROR; |
| 5642 | return 0; |
| 5643 | } |
| 5644 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5645 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5646 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5647 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5648 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5649 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5650 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5651 | if (h2s->id == 0) { |
| 5652 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5653 | |
| 5654 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5655 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5656 | return 0; |
| 5657 | } |
| 5658 | |
| 5659 | eb32_delete(&h2s->by_id); |
| 5660 | h2s->by_id.key = h2s->id = id; |
| 5661 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5662 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5663 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5664 | } |
| 5665 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5666 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5667 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5668 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5669 | idx = htx_get_head(htx); |
| 5670 | blk = htx_get_blk(htx, idx); |
| 5671 | btype = htx_get_blk_type(blk); |
| 5672 | bsize = htx_get_blksz(blk); |
| 5673 | |
| 5674 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5675 | case HTX_BLK_REQ_SL: |
| 5676 | /* start-line before headers */ |
| 5677 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5678 | if (ret > 0) { |
| 5679 | total += ret; |
| 5680 | count -= ret; |
| 5681 | if (ret < bsize) |
| 5682 | goto done; |
| 5683 | } |
| 5684 | break; |
| 5685 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5686 | case HTX_BLK_RES_SL: |
| 5687 | /* start-line before headers */ |
| 5688 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5689 | if (ret > 0) { |
| 5690 | total += ret; |
| 5691 | count -= ret; |
| 5692 | if (ret < bsize) |
| 5693 | goto done; |
| 5694 | } |
| 5695 | break; |
| 5696 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5697 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5698 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5699 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5700 | * This EOM necessarily is one before trailers, as the EOM following |
| 5701 | * trailers would have been consumed by the trailers parser. |
| 5702 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5703 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5704 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5705 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5706 | total += ret; |
| 5707 | count -= ret; |
| 5708 | if (ret < bsize) |
| 5709 | goto done; |
| 5710 | } |
| 5711 | break; |
| 5712 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5713 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5714 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5715 | /* This is the first trailers block, all the subsequent ones AND |
| 5716 | * the EOM will be swallowed by the parser. |
| 5717 | */ |
| 5718 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5719 | if (ret > 0) { |
| 5720 | total += ret; |
| 5721 | count -= ret; |
| 5722 | if (ret < bsize) |
| 5723 | goto done; |
| 5724 | } |
| 5725 | break; |
| 5726 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5727 | default: |
| 5728 | htx_remove_blk(htx, blk); |
| 5729 | total += bsize; |
| 5730 | count -= bsize; |
| 5731 | break; |
| 5732 | } |
| 5733 | } |
| 5734 | goto done; |
| 5735 | } |
| 5736 | |
| 5737 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5738 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5739 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5740 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5741 | ret = -1; |
| 5742 | else |
| 5743 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5744 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5745 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5746 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5747 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5748 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5749 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5750 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5751 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5752 | if (unlikely((int)ret <= 0)) { |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 5753 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5754 | break; |
| 5755 | } |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 5756 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5757 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5758 | total += count; |
| 5759 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5760 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5761 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5762 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5763 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5764 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5765 | break; |
| 5766 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5767 | |
| 5768 | total += ret; |
| 5769 | count -= ret; |
| 5770 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5771 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5772 | break; |
| 5773 | |
| 5774 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5775 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5776 | } |
| 5777 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5778 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5779 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5780 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5781 | * unprocessed trailers, abnormal extra data, ...) |
| 5782 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5783 | total += count; |
| 5784 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5787 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5788 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5789 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5790 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5791 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5792 | } |
| 5793 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5794 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5795 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5796 | } else { |
| 5797 | b_del(buf, total); |
| 5798 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5799 | |
| 5800 | /* The mux is full, cancel the pending tasks */ |
| 5801 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5802 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5803 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5804 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5805 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5806 | * we read exactly what the caller specified, as with HTX the caller |
| 5807 | * will always give the buffer size, instead of the amount of data |
| 5808 | * available. |
| 5809 | */ |
| 5810 | if (htx && !b_data(buf)) |
| 5811 | total = orig_count; |
| 5812 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5813 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5814 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5815 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5816 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5817 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5818 | /* If we're waiting for flow control, and we got a shutr on the |
| 5819 | * connection, we will never be unlocked, so add an error on |
| 5820 | * the conn_stream. |
| 5821 | */ |
| 5822 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5823 | !b_data(&h2s->h2c->dbuf) && |
| 5824 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5825 | if (cs->flags & CS_FL_EOS) |
| 5826 | cs->flags |= CS_FL_ERROR; |
| 5827 | else |
| 5828 | cs->flags |= CS_FL_ERR_PENDING; |
| 5829 | } |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5830 | |
| 5831 | if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5832 | /* 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] | 5833 | LIST_DEL_INIT(&h2s->list); |
| 5834 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5835 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5836 | } |
| 5837 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5838 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5839 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5840 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5841 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5842 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5843 | struct eb32_node *node; |
| 5844 | int fctl_cnt = 0; |
| 5845 | int send_cnt = 0; |
| 5846 | int tree_cnt = 0; |
| 5847 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5848 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5849 | |
| 5850 | if (!h2c) |
| 5851 | return; |
| 5852 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5853 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5854 | fctl_cnt++; |
| 5855 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5856 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5857 | send_cnt++; |
| 5858 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5859 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5860 | node = eb32_first(&h2c->streams_by_id); |
| 5861 | while (node) { |
| 5862 | h2s = container_of(node, struct h2s, by_id); |
| 5863 | tree_cnt++; |
| 5864 | if (!h2s->cs) |
| 5865 | orph_cnt++; |
| 5866 | node = eb32_next(node); |
| 5867 | } |
| 5868 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5869 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5870 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5871 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5872 | " .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] | 5873 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5874 | " .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] | 5875 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5876 | 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] | 5877 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5878 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5879 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5880 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5881 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5882 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5883 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5884 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5885 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5886 | |
| 5887 | if (h2s) { |
| 5888 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5889 | h2s, h2s->id, h2s->flags, |
| 5890 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5891 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5892 | h2s->cs); |
| 5893 | if (h2s->cs) |
| 5894 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5895 | h2s->cs->flags, h2s->cs->data); |
| 5896 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5897 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5898 | |
| 5899 | /*******************************************************/ |
| 5900 | /* functions below are dedicated to the config parsers */ |
| 5901 | /*******************************************************/ |
| 5902 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5903 | /* config parser for global "tune.h2.header-table-size" */ |
| 5904 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5905 | struct proxy *defpx, const char *file, int line, |
| 5906 | char **err) |
| 5907 | { |
| 5908 | if (too_many_args(1, args, err, NULL)) |
| 5909 | return -1; |
| 5910 | |
| 5911 | h2_settings_header_table_size = atoi(args[1]); |
| 5912 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5913 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5914 | return -1; |
| 5915 | } |
| 5916 | return 0; |
| 5917 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5918 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5919 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5920 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5921 | struct proxy *defpx, const char *file, int line, |
| 5922 | char **err) |
| 5923 | { |
| 5924 | if (too_many_args(1, args, err, NULL)) |
| 5925 | return -1; |
| 5926 | |
| 5927 | h2_settings_initial_window_size = atoi(args[1]); |
| 5928 | if (h2_settings_initial_window_size < 0) { |
| 5929 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5930 | return -1; |
| 5931 | } |
| 5932 | return 0; |
| 5933 | } |
| 5934 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5935 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5936 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5937 | struct proxy *defpx, const char *file, int line, |
| 5938 | char **err) |
| 5939 | { |
| 5940 | if (too_many_args(1, args, err, NULL)) |
| 5941 | return -1; |
| 5942 | |
| 5943 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5944 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5945 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5946 | return -1; |
| 5947 | } |
| 5948 | return 0; |
| 5949 | } |
| 5950 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5951 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5952 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5953 | struct proxy *defpx, const char *file, int line, |
| 5954 | char **err) |
| 5955 | { |
| 5956 | if (too_many_args(1, args, err, NULL)) |
| 5957 | return -1; |
| 5958 | |
| 5959 | h2_settings_max_frame_size = atoi(args[1]); |
| 5960 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5961 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5962 | return -1; |
| 5963 | } |
| 5964 | return 0; |
| 5965 | } |
| 5966 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5967 | |
| 5968 | /****************************************/ |
| 5969 | /* MUX initialization and instanciation */ |
| 5970 | /***************************************/ |
| 5971 | |
| 5972 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5973 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5974 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5975 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5976 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5977 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5978 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5979 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5980 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5981 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5982 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5983 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5984 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5985 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5986 | .shutr = h2_shutr, |
| 5987 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 5988 | .ctl = h2_ctl, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5989 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5990 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5991 | .name = "H2", |
| 5992 | }; |
| 5993 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5994 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5995 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5996 | { .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] | 5997 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5998 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5999 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6000 | |
| 6001 | /* The mux operations */ |
| 6002 | static const struct mux_ops h2_htx_ops = { |
| 6003 | .init = h2_init, |
| 6004 | .wake = h2_wake, |
| 6005 | .snd_buf = h2_snd_buf, |
| 6006 | .rcv_buf = h2_rcv_buf, |
| 6007 | .subscribe = h2_subscribe, |
| 6008 | .unsubscribe = h2_unsubscribe, |
| 6009 | .attach = h2_attach, |
| 6010 | .get_first_cs = h2_get_first_cs, |
| 6011 | .detach = h2_detach, |
| 6012 | .destroy = h2_destroy, |
| 6013 | .avail_streams = h2_avail_streams, |
| 6014 | .used_streams = h2_used_streams, |
| 6015 | .shutr = h2_shutr, |
| 6016 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 6017 | .ctl = h2_ctl, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6018 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 6019 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6020 | .name = "H2", |
| 6021 | }; |
| 6022 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 6023 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6024 | { .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] | 6025 | |
| 6026 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 6027 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6028 | /* config keyword parsers */ |
| 6029 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 6030 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 6031 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 6032 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 6033 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6034 | { 0, NULL, NULL } |
| 6035 | }}; |
| 6036 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6037 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |