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 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 306 | /* returns true if the connection is allowed to expire, false otherwise. A |
| 307 | * connection may expire when: |
| 308 | * - it has no stream |
| 309 | * - it has data in the mux buffer |
| 310 | * - it has streams in the blocked list |
| 311 | * - it has streams in the fctl list |
| 312 | * - it has streams in the send list |
| 313 | * Otherwise it means some streams are waiting in the data layer and it should |
| 314 | * not expire. |
| 315 | */ |
| 316 | static inline int h2c_may_expire(const struct h2c *h2c) |
| 317 | { |
| 318 | return eb_is_empty(&h2c->streams_by_id) || |
| 319 | br_data(h2c->mbuf) || |
| 320 | !LIST_ISEMPTY(&h2c->blocked_list) || |
| 321 | !LIST_ISEMPTY(&h2c->fctl_list) || |
| 322 | !LIST_ISEMPTY(&h2c->send_list) || |
| 323 | !LIST_ISEMPTY(&h2c->sending_list); |
| 324 | } |
| 325 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 326 | static __inline int |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 327 | h2c_is_dead(const struct h2c *h2c) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 328 | { |
| 329 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 330 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 331 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 332 | (!(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] | 333 | (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 334 | (conn_xprt_read0_pending(h2c->conn) || |
| 335 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 336 | return 1; |
| 337 | |
| 338 | return 0; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 341 | /*****************************************************/ |
| 342 | /* functions below are for dynamic buffer management */ |
| 343 | /*****************************************************/ |
| 344 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 345 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 346 | * to receive data into the buffer and/or demux pending data. The condition is |
| 347 | * a bit complex due to some API limits for now. The rules are the following : |
| 348 | * - if an error or a shutdown was detected on the connection and the buffer |
| 349 | * is empty, we must not attempt to receive |
| 350 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 351 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 352 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 353 | * regardless of whether the demux buffer is full or not, so that only |
| 354 | * de demux part decides whether or not to block. This is needed because |
| 355 | * the connection API indeed prevents us from re-enabling receipt that is |
| 356 | * already enabled in a polled state, so we must always immediately stop |
| 357 | * as soon as the demux can't proceed so as never to hit an end of read |
| 358 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 359 | * - otherwise must may not attempt |
| 360 | */ |
| 361 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 362 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 363 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 364 | (h2c->st0 >= H2_CS_ERROR || |
| 365 | h2c->conn->flags & CO_FL_ERROR || |
| 366 | conn_xprt_read0_pending(h2c->conn))) |
| 367 | return 0; |
| 368 | |
| 369 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 370 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 371 | return 1; |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 376 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 377 | 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] | 378 | { |
| 379 | if (!h2_recv_allowed(h2c)) |
| 380 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 381 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 382 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 383 | return; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 384 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 388 | /* returns true if the front connection has too many conn_streams attached */ |
| 389 | 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] | 390 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 391 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 394 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 395 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 396 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 397 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 398 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 399 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 400 | { |
| 401 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 402 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 403 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 404 | 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] | 405 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 406 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 407 | return 1; |
| 408 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 409 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 410 | 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] | 411 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 412 | |
| 413 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 414 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 415 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 416 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 417 | return 1; |
| 418 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 419 | |
| 420 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 421 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 422 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 423 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 424 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 425 | return 1; |
| 426 | } |
| 427 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 431 | 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] | 432 | { |
| 433 | struct buffer *buf = NULL; |
| 434 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 435 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 436 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 437 | h2c->buf_wait.target = h2c; |
| 438 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 439 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 440 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 441 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 442 | __conn_xprt_stop_recv(h2c->conn); |
| 443 | } |
| 444 | return buf; |
| 445 | } |
| 446 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 447 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 448 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 449 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 450 | b_free(bptr); |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 451 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 455 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 456 | { |
| 457 | struct buffer *buf; |
| 458 | unsigned int count = 0; |
| 459 | |
| 460 | while (b_size(buf = br_head_pick(h2c->mbuf))) { |
| 461 | b_free(buf); |
| 462 | count++; |
| 463 | } |
| 464 | if (count) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 465 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 468 | /* returns the number of allocatable outgoing streams for the connection taking |
| 469 | * the last_sid and the reserved ones into account. |
| 470 | */ |
| 471 | static inline int h2_streams_left(const struct h2c *h2c) |
| 472 | { |
| 473 | int ret; |
| 474 | |
| 475 | /* consider the number of outgoing streams we're allowed to create before |
| 476 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 477 | * nb_reserved is the number of streams which don't yet have an ID. |
| 478 | */ |
| 479 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 480 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 481 | if (ret < 0) |
| 482 | ret = 0; |
| 483 | return ret; |
| 484 | } |
| 485 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 486 | /* returns the number of streams in use on a connection to figure if it's |
| 487 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 488 | * to know if it was the last one after a detach(). |
| 489 | */ |
| 490 | static int h2_used_streams(struct connection *conn) |
| 491 | { |
| 492 | struct h2c *h2c = conn->ctx; |
| 493 | |
| 494 | return h2c->nb_cs; |
| 495 | } |
| 496 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 497 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 498 | static int h2_avail_streams(struct connection *conn) |
| 499 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 500 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 501 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 502 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 503 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 504 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 505 | * streams on the connection. |
| 506 | */ |
| 507 | if (h2c->last_sid >= 0) |
| 508 | return 0; |
| 509 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 510 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 511 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 512 | |
| 513 | /* we must also consider the limit imposed by stream IDs */ |
| 514 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 515 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 516 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 517 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 518 | ret1 = MIN(ret1, ret2); |
| 519 | } |
| 520 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 523 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 524 | /*****************************************************************/ |
| 525 | /* functions below are dedicated to the mux setup and management */ |
| 526 | /*****************************************************************/ |
| 527 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 528 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 529 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 530 | * connections from the fact that the context is still NULL (even during mux |
| 531 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 532 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 533 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 534 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 535 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 536 | { |
| 537 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 538 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 539 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 540 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 541 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 542 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 543 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 544 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 545 | h2c->flags = H2_CF_IS_BACK; |
| 546 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 547 | if (tick_isset(prx->timeout.serverfin)) |
| 548 | h2c->shut_timeout = prx->timeout.serverfin; |
| 549 | } else { |
| 550 | h2c->flags = H2_CF_NONE; |
| 551 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 552 | if (tick_isset(prx->timeout.clientfin)) |
| 553 | h2c->shut_timeout = prx->timeout.clientfin; |
| 554 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 555 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 556 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 557 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 558 | if (tick_isset(h2c->timeout)) { |
| 559 | t = task_new(tid_bit); |
| 560 | if (!t) |
| 561 | goto fail; |
| 562 | |
| 563 | h2c->task = t; |
| 564 | t->process = h2_timeout_task; |
| 565 | t->context = h2c; |
| 566 | t->expire = tick_add(now_ms, h2c->timeout); |
| 567 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 568 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 569 | h2c->wait_event.tasklet = tasklet_new(); |
| 570 | if (!h2c->wait_event.tasklet) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 571 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 572 | h2c->wait_event.tasklet->process = h2_io_cb; |
| 573 | h2c->wait_event.tasklet->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 574 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 575 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 576 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 577 | if (!h2c->ddht) |
| 578 | goto fail; |
| 579 | |
| 580 | /* Initialise the context. */ |
| 581 | h2c->st0 = H2_CS_PREFACE; |
| 582 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 583 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 584 | h2c->max_id = -1; |
| 585 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 586 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 587 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 588 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 589 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 590 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 591 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 592 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 593 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 594 | h2c->dsi = -1; |
| 595 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 596 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 597 | h2c->last_sid = -1; |
| 598 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 599 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 600 | h2c->miw = 65535; /* mux initial window size */ |
| 601 | h2c->mws = 65535; /* mux window size */ |
| 602 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 603 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 604 | LIST_INIT(&h2c->send_list); |
| 605 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 606 | LIST_INIT(&h2c->blocked_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 607 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 608 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 609 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 610 | if (t) |
| 611 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 612 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 613 | if (h2c->flags & H2_CF_IS_BACK) { |
| 614 | /* FIXME: this is temporary, for outgoing connections we need |
| 615 | * to immediately allocate a stream until the code is modified |
| 616 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 617 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 618 | */ |
| 619 | struct h2s *h2s; |
| 620 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 621 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 622 | if (!h2s) |
| 623 | goto fail_stream; |
| 624 | } |
| 625 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 626 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 627 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 628 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 629 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 630 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 631 | fail_stream: |
| 632 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 633 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 634 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 635 | if (h2c->wait_event.tasklet) |
| 636 | tasklet_free(h2c->wait_event.tasklet); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 637 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 638 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 639 | return -1; |
| 640 | } |
| 641 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 642 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 643 | * -1 if no more is allocatable. |
| 644 | */ |
| 645 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 646 | { |
| 647 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 648 | |
| 649 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 650 | id = -1; |
| 651 | return id; |
| 652 | } |
| 653 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 654 | /* returns the stream associated with id <id> or NULL if not found */ |
| 655 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 656 | { |
| 657 | struct eb32_node *node; |
| 658 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 659 | if (id == 0) |
| 660 | return (struct h2s *)h2_closed_stream; |
| 661 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 662 | if (id > h2c->max_id) |
| 663 | return (struct h2s *)h2_idle_stream; |
| 664 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 665 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 666 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 667 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 668 | |
| 669 | return container_of(node, struct h2s, by_id); |
| 670 | } |
| 671 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 672 | /* release function. This one should be called to free all resources allocated |
| 673 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 674 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 675 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 676 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 677 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 678 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 679 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 680 | /* The connection must be aattached to this mux to be released */ |
| 681 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 682 | conn = h2c->conn; |
| 683 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 684 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 685 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 686 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 687 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 688 | LIST_DEL(&h2c->buf_wait.list); |
| 689 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 690 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 691 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 692 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 693 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 694 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 695 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 696 | h2c->task->context = NULL; |
| 697 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 698 | h2c->task = NULL; |
| 699 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 700 | if (h2c->wait_event.tasklet) |
| 701 | tasklet_free(h2c->wait_event.tasklet); |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 702 | if (conn && h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 703 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 704 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 705 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 706 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 709 | if (conn) { |
| 710 | conn->mux = NULL; |
| 711 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 712 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 713 | conn_stop_tracking(conn); |
| 714 | conn_full_close(conn); |
| 715 | if (conn->destroy_cb) |
| 716 | conn->destroy_cb(conn); |
| 717 | conn_free(conn); |
| 718 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 722 | /******************************************************/ |
| 723 | /* functions below are for the H2 protocol processing */ |
| 724 | /******************************************************/ |
| 725 | |
| 726 | /* 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] | 727 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 728 | { |
| 729 | return h2s ? h2s->id : 0; |
| 730 | } |
| 731 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 732 | /* returns the sum of the stream's own window size and the mux's initial |
| 733 | * window, which together form the stream's effective window size. |
| 734 | */ |
| 735 | static inline int h2s_mws(const struct h2s *h2s) |
| 736 | { |
| 737 | return h2s->sws + h2s->h2c->miw; |
| 738 | } |
| 739 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 740 | /* 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] | 741 | 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] | 742 | { |
| 743 | if (h2c->msi < 0) |
| 744 | return 0; |
| 745 | |
| 746 | if (h2c->msi == h2s_id(h2s)) |
| 747 | return 0; |
| 748 | |
| 749 | return 1; |
| 750 | } |
| 751 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 752 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 753 | 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] | 754 | { |
| 755 | h2c->errcode = err; |
| 756 | h2c->st0 = H2_CS_ERROR; |
| 757 | } |
| 758 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 759 | /* marks an error on the stream. It may also update an already closed stream |
| 760 | * (e.g. to report an error after an RST was received). |
| 761 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 762 | 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] | 763 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 764 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 765 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 766 | if (h2s->st < H2_SS_ERROR) |
| 767 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 768 | if (h2s->cs) |
| 769 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 773 | /* attempt to notify the data layer of recv availability */ |
| 774 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 775 | { |
| 776 | struct wait_event *sw; |
| 777 | |
| 778 | if (h2s->recv_wait) { |
| 779 | sw = h2s->recv_wait; |
| 780 | sw->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 781 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 782 | h2s->recv_wait = NULL; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /* attempt to notify the data layer of send availability */ |
| 787 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 788 | { |
| 789 | struct wait_event *sw; |
| 790 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 791 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 792 | sw = h2s->send_wait; |
| 793 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 794 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 795 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 799 | /* alerts the data layer, trying to wake it up by all means, following |
| 800 | * this sequence : |
| 801 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 802 | * - if its subscribed to send, then it's woken up for send |
| 803 | * - if it was subscribed to neither, its ->wake() callback is called |
| 804 | * It is safe to call this function with a closed stream which doesn't have a |
| 805 | * conn_stream anymore. |
| 806 | */ |
| 807 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 808 | { |
| 809 | if (h2s->recv_wait || h2s->send_wait) { |
| 810 | h2s_notify_recv(h2s); |
| 811 | h2s_notify_send(h2s); |
| 812 | } |
| 813 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 814 | h2s->cs->data_cb->wake(h2s->cs); |
| 815 | } |
| 816 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 817 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 818 | 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] | 819 | { |
| 820 | uint8_t *out = frame; |
| 821 | |
| 822 | *out = len >> 16; |
| 823 | write_n16(out + 1, len); |
| 824 | } |
| 825 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 826 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 827 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 828 | * 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] | 829 | * available in the buffer's input prior to calling this function. The buffer |
| 830 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 831 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 832 | 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] | 833 | const struct buffer *b, int o) |
| 834 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 835 | 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] | 836 | } |
| 837 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 838 | 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] | 839 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 840 | 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] | 841 | } |
| 842 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 843 | 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] | 844 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 845 | 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] | 846 | } |
| 847 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 848 | 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] | 849 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 850 | 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] | 851 | } |
| 852 | |
| 853 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 854 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 855 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 856 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 857 | * 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] | 858 | * |
| 859 | * b0 b1 b2 b3 b4 b5..b8 |
| 860 | * +----------+---------+--------+----+----+----------------------+ |
| 861 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 862 | * +----------+---------+--------+----+----+----------------------+ |
| 863 | * |
| 864 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 865 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 866 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 867 | * 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] | 868 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 869 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 870 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 871 | 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] | 872 | { |
| 873 | uint64_t w; |
| 874 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 875 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 876 | return 0; |
| 877 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 878 | w = h2_get_n64(b, o + 1); |
| 879 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 880 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 881 | h->ff = w >> 32; |
| 882 | h->ft = w >> 40; |
| 883 | h->len += w >> 48; |
| 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 888 | * h2_peek_frame_hdr() above. |
| 889 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 890 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 891 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 892 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 896 | 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] | 897 | { |
| 898 | int ret; |
| 899 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 900 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 901 | if (ret > 0) |
| 902 | h2_skip_frame_hdr(b); |
| 903 | return ret; |
| 904 | } |
| 905 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 906 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 907 | * its connection if the stream was not yet closed. Please use this exclusively |
| 908 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 909 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 910 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 911 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 912 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 913 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 914 | if (!h2s->id) |
| 915 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 916 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 917 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 918 | h2s_notify_recv(h2s); |
| 919 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 920 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 921 | h2s->st = H2_SS_CLOSED; |
| 922 | } |
| 923 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 924 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 925 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 926 | { |
| 927 | h2s_close(h2s); |
| 928 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 929 | if (b_size(&h2s->rxbuf)) { |
| 930 | b_free(&h2s->rxbuf); |
| 931 | offer_buffers(NULL, tasks_run_queue); |
| 932 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 933 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 934 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 935 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 936 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 937 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 938 | * reference left would be in the h2c send_list/fctl_list, and if |
| 939 | * we're in it, we're getting out anyway |
| 940 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 941 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 942 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 943 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 944 | LIST_DEL_INIT(&h2s->sending_list); |
| 945 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 946 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 947 | pool_free(pool_head_h2s, h2s); |
| 948 | } |
| 949 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 950 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 951 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 952 | * causes of errors can be any failed memory allocation. The caller is |
| 953 | * responsible for checking if the connection may support an extra stream |
| 954 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 955 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 956 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 957 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 958 | struct h2s *h2s; |
| 959 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 960 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 961 | if (!h2s) |
| 962 | goto out; |
| 963 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 964 | h2s->wait_event.tasklet = tasklet_new(); |
| 965 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 966 | pool_free(pool_head_h2s, h2s); |
| 967 | goto out; |
| 968 | } |
| 969 | h2s->send_wait = NULL; |
| 970 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 971 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 972 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 973 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 974 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 975 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 976 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 977 | h2s->cs = NULL; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 978 | h2s->sws = 0; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 979 | h2s->flags = H2_SF_NONE; |
| 980 | h2s->errcode = H2_ERR_NO_ERROR; |
| 981 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 982 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 983 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 984 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 985 | |
| 986 | if (h2c->flags & H2_CF_IS_BACK) { |
| 987 | h1m_init_req(&h2s->h1m); |
| 988 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 989 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 990 | } else { |
| 991 | h1m_init_res(&h2s->h1m); |
| 992 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 993 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 994 | } |
| 995 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 996 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 997 | if (id > 0) |
| 998 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 999 | else |
| 1000 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1001 | |
| 1002 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 1003 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 1004 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1005 | |
| 1006 | return h2s; |
| 1007 | |
| 1008 | out_free_h2s: |
| 1009 | pool_free(pool_head_h2s, h2s); |
| 1010 | out: |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | |
| 1014 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 1015 | * case of memory allocation error. |
| 1016 | */ |
| 1017 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 1018 | { |
| 1019 | struct session *sess = h2c->conn->owner; |
| 1020 | struct conn_stream *cs; |
| 1021 | struct h2s *h2s; |
| 1022 | |
| 1023 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 1024 | goto out; |
| 1025 | |
| 1026 | h2s = h2s_new(h2c, id); |
| 1027 | if (!h2s) |
| 1028 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1029 | |
| 1030 | cs = cs_new(h2c->conn); |
| 1031 | if (!cs) |
| 1032 | goto out_close; |
| 1033 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1034 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1035 | h2s->cs = cs; |
| 1036 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1037 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1038 | |
| 1039 | if (stream_create_from_cs(cs) < 0) |
| 1040 | goto out_free_cs; |
| 1041 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1042 | /* We want the accept date presented to the next stream to be the one |
| 1043 | * we have now, the handshake time to be null (since the next stream |
| 1044 | * is not delayed by a handshake), and the idle time to count since |
| 1045 | * right now. |
| 1046 | */ |
| 1047 | sess->accept_date = date; |
| 1048 | sess->tv_accept = now; |
| 1049 | sess->t_handshake = 0; |
| 1050 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1051 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1052 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1053 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1054 | return h2s; |
| 1055 | |
| 1056 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1057 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1058 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1059 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1060 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1061 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1062 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1063 | sess_log(sess); |
| 1064 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1067 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1068 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1069 | * possible stream ID was reached. |
| 1070 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1071 | 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] | 1072 | { |
| 1073 | struct h2s *h2s = NULL; |
| 1074 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1075 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1076 | goto out; |
| 1077 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1078 | if (h2_streams_left(h2c) < 1) |
| 1079 | goto out; |
| 1080 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1081 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1082 | h2s = h2s_new(h2c, 0); |
| 1083 | if (!h2s) |
| 1084 | goto out; |
| 1085 | |
| 1086 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1087 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1088 | cs->ctx = h2s; |
| 1089 | h2c->nb_cs++; |
| 1090 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1091 | out: |
| 1092 | return h2s; |
| 1093 | } |
| 1094 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1095 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1096 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1097 | * the various settings codes. |
| 1098 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1099 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1100 | { |
| 1101 | struct buffer *res; |
| 1102 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1103 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1104 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1105 | int ret; |
| 1106 | |
| 1107 | if (h2c_mux_busy(h2c, NULL)) { |
| 1108 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1112 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1113 | chunk_memcpy(&buf, |
| 1114 | "\x00\x00\x00" /* length : 0 for now */ |
| 1115 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1116 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1117 | 9); |
| 1118 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1119 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1120 | /* send settings_enable_push=0 */ |
| 1121 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1122 | } |
| 1123 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1124 | if (h2_settings_header_table_size != 4096) { |
| 1125 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1126 | |
| 1127 | write_n32(str + 2, h2_settings_header_table_size); |
| 1128 | chunk_memcat(&buf, str, 6); |
| 1129 | } |
| 1130 | |
| 1131 | if (h2_settings_initial_window_size != 65535) { |
| 1132 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1133 | |
| 1134 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1135 | chunk_memcat(&buf, str, 6); |
| 1136 | } |
| 1137 | |
| 1138 | if (h2_settings_max_concurrent_streams != 0) { |
| 1139 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1140 | |
| 1141 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1142 | * the protocol, so never send this value! |
| 1143 | */ |
| 1144 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1145 | chunk_memcat(&buf, str, 6); |
| 1146 | } |
| 1147 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1148 | mfs = h2_settings_max_frame_size; |
| 1149 | if (mfs > global.tune.bufsize) |
| 1150 | mfs = global.tune.bufsize; |
| 1151 | |
| 1152 | if (!mfs) |
| 1153 | mfs = global.tune.bufsize; |
| 1154 | |
| 1155 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1156 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1157 | |
| 1158 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1159 | * match bufsize - rewrite size, but at the moment it seems |
| 1160 | * that clients don't take care of it. |
| 1161 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1162 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1163 | chunk_memcat(&buf, str, 6); |
| 1164 | } |
| 1165 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1166 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1167 | |
| 1168 | res = br_tail(h2c->mbuf); |
| 1169 | retry: |
| 1170 | if (!h2_get_buf(h2c, res)) { |
| 1171 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1172 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1176 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1177 | if (unlikely(ret <= 0)) { |
| 1178 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1179 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1180 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1181 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1182 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1183 | return 0; |
| 1184 | } |
| 1185 | else { |
| 1186 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | } |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1193 | /* Try to receive a connection preface, then upon success try to send our |
| 1194 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1195 | * missing data. It may return an error in h2c. |
| 1196 | */ |
| 1197 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1198 | { |
| 1199 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1200 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1201 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1202 | 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] | 1203 | |
| 1204 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1205 | if (ret1 < 0) |
| 1206 | sess_log(h2c->conn->owner); |
| 1207 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1208 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1209 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1213 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1214 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1215 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1216 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1217 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1218 | } |
| 1219 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1220 | /* Try to send a connection preface, then upon success try to send our |
| 1221 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1222 | * missing data. It may return an error in h2c. |
| 1223 | */ |
| 1224 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1225 | { |
| 1226 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1227 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1228 | |
| 1229 | if (h2c_mux_busy(h2c, NULL)) { |
| 1230 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1234 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1235 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1236 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1237 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1238 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | if (!b_data(res)) { |
| 1243 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1244 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1245 | if (unlikely(ret <= 0)) { |
| 1246 | if (!ret) { |
| 1247 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1248 | goto retry; |
| 1249 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1250 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1251 | return 0; |
| 1252 | } |
| 1253 | else { |
| 1254 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1258 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1259 | return h2c_send_settings(h2c); |
| 1260 | } |
| 1261 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1262 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1263 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1264 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1265 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1266 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1267 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1268 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1269 | * case so that it is safe to use h2c_error() to report such errors. |
| 1270 | */ |
| 1271 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1272 | { |
| 1273 | struct buffer *res; |
| 1274 | char str[17]; |
| 1275 | int ret; |
| 1276 | |
| 1277 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1278 | return 1; // claim that it worked |
| 1279 | |
| 1280 | if (h2c_mux_busy(h2c, h2s)) { |
| 1281 | if (h2s) |
| 1282 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1283 | else |
| 1284 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1288 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1289 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1290 | |
| 1291 | if (h2c->last_sid < 0) |
| 1292 | h2c->last_sid = h2c->max_id; |
| 1293 | |
| 1294 | write_n32(str + 9, h2c->last_sid); |
| 1295 | write_n32(str + 13, h2c->errcode); |
| 1296 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1297 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1298 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1299 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1300 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1301 | if (h2s) |
| 1302 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1303 | else |
| 1304 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1308 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1309 | if (unlikely(ret <= 0)) { |
| 1310 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1311 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1312 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1313 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1314 | if (h2s) |
| 1315 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1316 | else |
| 1317 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1318 | return 0; |
| 1319 | } |
| 1320 | else { |
| 1321 | /* we cannot report this error using GOAWAY, so we mark |
| 1322 | * it and claim a success. |
| 1323 | */ |
| 1324 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1325 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1326 | return 1; |
| 1327 | } |
| 1328 | } |
| 1329 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1330 | return ret; |
| 1331 | } |
| 1332 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1333 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1334 | * during mux operations. This stream must be valid and cannot be closed |
| 1335 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1336 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1337 | * not yet. |
| 1338 | * |
| 1339 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1340 | * to write the message, it subscribes the stream to future notifications. |
| 1341 | */ |
| 1342 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1343 | { |
| 1344 | struct buffer *res; |
| 1345 | char str[13]; |
| 1346 | int ret; |
| 1347 | |
| 1348 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1349 | return 1; |
| 1350 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1351 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1352 | * RST_STREAM in response to a RST_STREAM frame. |
| 1353 | */ |
Willy Tarreau | bf9a20b | 2019-08-06 10:01:40 +0200 | [diff] [blame] | 1354 | if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) { |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1355 | ret = 1; |
| 1356 | goto ignore; |
| 1357 | } |
| 1358 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1359 | if (h2c_mux_busy(h2c, h2s)) { |
| 1360 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1364 | /* len: 4, type: 3, flags: none */ |
| 1365 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1366 | write_n32(str + 5, h2s->id); |
| 1367 | write_n32(str + 9, h2s->errcode); |
| 1368 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1369 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1370 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1371 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1372 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1373 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1377 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1378 | if (unlikely(ret <= 0)) { |
| 1379 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1380 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1381 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1382 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1383 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | else { |
| 1387 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1388 | return 0; |
| 1389 | } |
| 1390 | } |
| 1391 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1392 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1393 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1394 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1395 | return ret; |
| 1396 | } |
| 1397 | |
| 1398 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1399 | * 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] | 1400 | * error code, even if the stream is one of the dummy ones, and will update |
| 1401 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1402 | * |
| 1403 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1404 | * 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] | 1405 | * 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] | 1406 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1407 | */ |
| 1408 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1409 | { |
| 1410 | struct buffer *res; |
| 1411 | char str[13]; |
| 1412 | int ret; |
| 1413 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1414 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1415 | * RST_STREAM in response to a RST_STREAM frame. |
| 1416 | */ |
| 1417 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1418 | ret = 1; |
| 1419 | goto ignore; |
| 1420 | } |
| 1421 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1422 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1423 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1424 | return 0; |
| 1425 | } |
| 1426 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1427 | /* len: 4, type: 3, flags: none */ |
| 1428 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1429 | |
| 1430 | write_n32(str + 5, h2c->dsi); |
| 1431 | write_n32(str + 9, h2s->errcode); |
| 1432 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1433 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1434 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1435 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1436 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1437 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
| 1440 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1441 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1442 | if (unlikely(ret <= 0)) { |
| 1443 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1444 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1445 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1446 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1447 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1448 | return 0; |
| 1449 | } |
| 1450 | else { |
| 1451 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | } |
| 1455 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1456 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1457 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1458 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1459 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1462 | return ret; |
| 1463 | } |
| 1464 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1465 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1466 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1467 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1468 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1469 | * message, it subscribes the requesting stream to future notifications. |
| 1470 | */ |
| 1471 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1472 | { |
| 1473 | struct h2c *h2c = h2s->h2c; |
| 1474 | struct buffer *res; |
| 1475 | char str[9]; |
| 1476 | int ret; |
| 1477 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1478 | 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] | 1479 | return 1; |
| 1480 | |
| 1481 | if (h2c_mux_busy(h2c, h2s)) { |
| 1482 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1486 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1487 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1488 | write_n32(str + 5, h2s->id); |
| 1489 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1490 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1491 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1492 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1493 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1494 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1498 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1499 | if (likely(ret > 0)) { |
| 1500 | h2s->flags |= H2_SF_ES_SENT; |
| 1501 | } |
| 1502 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1503 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1504 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1505 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1506 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1507 | return 0; |
| 1508 | } |
| 1509 | else { |
| 1510 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1511 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1512 | } |
| 1513 | return ret; |
| 1514 | } |
| 1515 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1516 | /* 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] | 1517 | * 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] | 1518 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1519 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1520 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1521 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1522 | { |
| 1523 | if (!h2s->cs) { |
| 1524 | /* this stream was already orphaned */ |
| 1525 | h2s_destroy(h2s); |
| 1526 | return; |
| 1527 | } |
| 1528 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1529 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1530 | if (h2s->st == H2_SS_OPEN) |
| 1531 | h2s->st = H2_SS_HREM; |
| 1532 | else if (h2s->st == H2_SS_HLOC) |
| 1533 | h2s_close(h2s); |
| 1534 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1535 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1536 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1537 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1538 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1539 | if (h2s->cs->flags & CS_FL_EOS) |
| 1540 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1541 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1542 | if (h2s->st < H2_SS_ERROR) |
| 1543 | h2s->st = H2_SS_ERROR; |
| 1544 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1545 | |
| 1546 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1550 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1551 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1552 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1553 | { |
| 1554 | struct eb32_node *node; |
| 1555 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1556 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1557 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1558 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1559 | while (node) { |
| 1560 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1561 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1562 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1563 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1564 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1565 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1566 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1567 | while (node) { |
| 1568 | h2s = container_of(node, struct h2s, by_id); |
| 1569 | if (h2s->id > 0) |
| 1570 | break; |
| 1571 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1572 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1576 | /* Wake up all blocked streams whose window size has become positive after the |
| 1577 | * mux's initial window was adjusted. This should be done after having processed |
| 1578 | * SETTINGS frames which have updated the mux's initial window size. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1579 | */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1580 | static void h2c_unblock_sfctl(struct h2c *h2c) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1581 | { |
| 1582 | struct h2s *h2s; |
| 1583 | struct eb32_node *node; |
| 1584 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1585 | node = eb32_first(&h2c->streams_by_id); |
| 1586 | while (node) { |
| 1587 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1588 | if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) { |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1589 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1590 | LIST_DEL_INIT(&h2s->list); |
| 1591 | if (h2s->send_wait) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1592 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1593 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1594 | node = eb32_next(node); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1599 | * 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] | 1600 | * return an error in h2c. The caller must have already verified frame length |
| 1601 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1602 | */ |
| 1603 | static int h2c_handle_settings(struct h2c *h2c) |
| 1604 | { |
| 1605 | unsigned int offset; |
| 1606 | int error; |
| 1607 | |
| 1608 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1609 | if (h2c->dfl) { |
| 1610 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1611 | goto fail; |
| 1612 | } |
| 1613 | return 1; |
| 1614 | } |
| 1615 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1616 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1617 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1618 | return 0; |
| 1619 | |
| 1620 | /* parse the frame */ |
| 1621 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1622 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1623 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1624 | |
| 1625 | switch (type) { |
| 1626 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1627 | /* we need to update all existing streams with the |
| 1628 | * difference from the previous iws. |
| 1629 | */ |
| 1630 | if (arg < 0) { // RFC7540#6.5.2 |
| 1631 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1632 | goto fail; |
| 1633 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1634 | h2c->miw = arg; |
| 1635 | break; |
| 1636 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1637 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1638 | error = H2_ERR_PROTOCOL_ERROR; |
| 1639 | goto fail; |
| 1640 | } |
| 1641 | h2c->mfs = arg; |
| 1642 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1643 | case H2_SETTINGS_ENABLE_PUSH: |
| 1644 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1645 | error = H2_ERR_PROTOCOL_ERROR; |
| 1646 | goto fail; |
| 1647 | } |
| 1648 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1649 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1650 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1651 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1652 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1653 | arg = h2_settings_max_concurrent_streams; |
| 1654 | h2c->streams_limit = arg; |
| 1655 | } |
| 1656 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | /* need to ACK this frame now */ |
| 1661 | h2c->st0 = H2_CS_FRAME_A; |
| 1662 | return 1; |
| 1663 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1664 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1665 | h2c_error(h2c, error); |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1670 | * success or one of the h2_status values. |
| 1671 | */ |
| 1672 | static int h2c_ack_settings(struct h2c *h2c) |
| 1673 | { |
| 1674 | struct buffer *res; |
| 1675 | char str[9]; |
| 1676 | int ret = -1; |
| 1677 | |
| 1678 | if (h2c_mux_busy(h2c, NULL)) { |
| 1679 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1683 | memcpy(str, |
| 1684 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1685 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1686 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1687 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1688 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1689 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1690 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1691 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1692 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1696 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1697 | if (unlikely(ret <= 0)) { |
| 1698 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1699 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1700 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1701 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1702 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1703 | return 0; |
| 1704 | } |
| 1705 | else { |
| 1706 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1707 | return 0; |
| 1708 | } |
| 1709 | } |
| 1710 | return ret; |
| 1711 | } |
| 1712 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1713 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1714 | * 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] | 1715 | * missing data. The caller must have already verified frame length |
| 1716 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1717 | */ |
| 1718 | static int h2c_handle_ping(struct h2c *h2c) |
| 1719 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1720 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1721 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1722 | h2c->st0 = H2_CS_FRAME_A; |
| 1723 | return 1; |
| 1724 | } |
| 1725 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1726 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1727 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1728 | * error in h2c. |
| 1729 | */ |
| 1730 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1731 | { |
| 1732 | struct buffer *res; |
| 1733 | char str[13]; |
| 1734 | int ret = -1; |
| 1735 | |
| 1736 | if (h2c_mux_busy(h2c, NULL)) { |
| 1737 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1741 | /* length: 4, type: 8, flags: none */ |
| 1742 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1743 | write_n32(str + 5, sid); |
| 1744 | write_n32(str + 9, increment); |
| 1745 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1746 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1747 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1748 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1749 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1750 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1754 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1755 | if (unlikely(ret <= 0)) { |
| 1756 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1757 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1758 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1759 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1760 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1761 | return 0; |
| 1762 | } |
| 1763 | else { |
| 1764 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1765 | return 0; |
| 1766 | } |
| 1767 | } |
| 1768 | return ret; |
| 1769 | } |
| 1770 | |
| 1771 | /* try to send pending window update for the connection. It's safe to call it |
| 1772 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1773 | * failure. It may return an error in h2c. |
| 1774 | */ |
| 1775 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1776 | { |
| 1777 | int ret = 1; |
| 1778 | |
| 1779 | if (h2c->rcvd_c <= 0) |
| 1780 | return 1; |
| 1781 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1782 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1783 | /* increase the advertised connection window to 2G on |
| 1784 | * first update. |
| 1785 | */ |
| 1786 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1787 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1788 | } |
| 1789 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1790 | /* send WU for the connection */ |
| 1791 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1792 | if (ret > 0) |
| 1793 | h2c->rcvd_c = 0; |
| 1794 | |
| 1795 | return ret; |
| 1796 | } |
| 1797 | |
| 1798 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1799 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1800 | * room or failure. It may return an error in h2c. |
| 1801 | */ |
| 1802 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1803 | { |
| 1804 | int ret = 1; |
| 1805 | |
| 1806 | if (h2c->rcvd_s <= 0) |
| 1807 | return 1; |
| 1808 | |
| 1809 | /* send WU for the stream */ |
| 1810 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1811 | if (ret > 0) |
| 1812 | h2c->rcvd_s = 0; |
| 1813 | |
| 1814 | return ret; |
| 1815 | } |
| 1816 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1817 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1818 | * success, 0 on missing data or one of the h2_status values. |
| 1819 | */ |
| 1820 | static int h2c_ack_ping(struct h2c *h2c) |
| 1821 | { |
| 1822 | struct buffer *res; |
| 1823 | char str[17]; |
| 1824 | int ret = -1; |
| 1825 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1826 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1827 | return 0; |
| 1828 | |
| 1829 | if (h2c_mux_busy(h2c, NULL)) { |
| 1830 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1834 | memcpy(str, |
| 1835 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1836 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1837 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1838 | |
| 1839 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1840 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1841 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1842 | res = br_tail(h2c->mbuf); |
| 1843 | retry: |
| 1844 | if (!h2_get_buf(h2c, res)) { |
| 1845 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1846 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1850 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1851 | if (unlikely(ret <= 0)) { |
| 1852 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1853 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1854 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1855 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1856 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1857 | return 0; |
| 1858 | } |
| 1859 | else { |
| 1860 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1861 | return 0; |
| 1862 | } |
| 1863 | } |
| 1864 | return ret; |
| 1865 | } |
| 1866 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1867 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1868 | * 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] | 1869 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1870 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1871 | */ |
| 1872 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1873 | { |
| 1874 | int32_t inc; |
| 1875 | int error; |
| 1876 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1877 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1878 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1879 | return 0; |
| 1880 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1881 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1882 | |
| 1883 | if (h2c->dsi != 0) { |
| 1884 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1885 | |
| 1886 | /* it's not an error to receive WU on a closed stream */ |
| 1887 | if (h2s->st == H2_SS_CLOSED) |
| 1888 | return 1; |
| 1889 | |
| 1890 | if (!inc) { |
| 1891 | error = H2_ERR_PROTOCOL_ERROR; |
| 1892 | goto strm_err; |
| 1893 | } |
| 1894 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1895 | if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1896 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1897 | goto strm_err; |
| 1898 | } |
| 1899 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1900 | h2s->sws += inc; |
| 1901 | if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1902 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1903 | LIST_DEL_INIT(&h2s->list); |
| 1904 | if (h2s->send_wait) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1905 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | else { |
| 1909 | /* connection window update */ |
| 1910 | if (!inc) { |
| 1911 | error = H2_ERR_PROTOCOL_ERROR; |
| 1912 | goto conn_err; |
| 1913 | } |
| 1914 | |
| 1915 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1916 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1917 | goto conn_err; |
| 1918 | } |
| 1919 | |
| 1920 | h2c->mws += inc; |
| 1921 | } |
| 1922 | |
| 1923 | return 1; |
| 1924 | |
| 1925 | conn_err: |
| 1926 | h2c_error(h2c, error); |
| 1927 | return 0; |
| 1928 | |
| 1929 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1930 | h2s_error(h2s, error); |
| 1931 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1932 | return 0; |
| 1933 | } |
| 1934 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1935 | /* 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] | 1936 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1937 | * have already verified frame length and stream ID validity. Described in |
| 1938 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1939 | */ |
| 1940 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1941 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1942 | int last; |
| 1943 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1944 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1945 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1946 | return 0; |
| 1947 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1948 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1949 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1950 | if (h2c->last_sid < 0) |
| 1951 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1952 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1953 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1954 | } |
| 1955 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1956 | /* 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] | 1957 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1958 | * error in h2c. The caller must have already verified frame length and stream |
| 1959 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1960 | */ |
| 1961 | static int h2c_handle_priority(struct h2c *h2c) |
| 1962 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1963 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1964 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1965 | return 0; |
| 1966 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1967 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1968 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1969 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1970 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1971 | } |
| 1972 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1973 | } |
| 1974 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1975 | /* 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] | 1976 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1977 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1978 | */ |
| 1979 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1980 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1981 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1982 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1983 | return 0; |
| 1984 | |
| 1985 | /* late RST, already handled */ |
| 1986 | if (h2s->st == H2_SS_CLOSED) |
| 1987 | return 1; |
| 1988 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1989 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1990 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1991 | |
| 1992 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1993 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1994 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | h2s->flags |= H2_SF_RST_RCVD; |
| 1998 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2001 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2002 | * It may return an error in h2c or h2s. The caller must consider that the |
| 2003 | * return value is the new h2s in case one was allocated (most common case). |
| 2004 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2005 | * errors here are reported as connection errors since it's impossible to |
| 2006 | * recover from such errors after the compression context has been altered. |
| 2007 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2008 | 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] | 2009 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2010 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2011 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2012 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2013 | int error; |
| 2014 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2015 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2016 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2017 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2018 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2019 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2020 | |
| 2021 | /* now either the frame is complete or the buffer is complete */ |
| 2022 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2023 | /* The stream exists/existed, this must be a trailers frame */ |
| 2024 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2025 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2026 | /* unrecoverable error ? */ |
| 2027 | if (h2c->st0 >= H2_CS_ERROR) |
| 2028 | goto out; |
| 2029 | |
| 2030 | if (error == 0) |
| 2031 | goto out; // missing data |
| 2032 | |
| 2033 | if (error < 0) { |
| 2034 | /* Failed to decode this frame (e.g. too large request) |
| 2035 | * but the HPACK decompressor is still synchronized. |
| 2036 | */ |
| 2037 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2038 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2039 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2040 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2041 | goto done; |
| 2042 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2043 | /* the connection was already killed by an RST, let's consume |
| 2044 | * the data and send another RST. |
| 2045 | */ |
| 2046 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2047 | h2s = (struct h2s*)h2_error_stream; |
| 2048 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2049 | } |
| 2050 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2051 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2052 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2053 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2054 | goto conn_err; |
| 2055 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2056 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2057 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2058 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2059 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2060 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2061 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2062 | if (h2c->st0 >= H2_CS_ERROR) |
| 2063 | goto out; |
| 2064 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2065 | if (error <= 0) { |
| 2066 | if (error == 0) |
| 2067 | goto out; // missing data |
| 2068 | |
| 2069 | /* Failed to decode this stream (e.g. too large request) |
| 2070 | * but the HPACK decompressor is still synchronized. |
| 2071 | */ |
| 2072 | h2s = (struct h2s*)h2_error_stream; |
| 2073 | goto send_rst; |
| 2074 | } |
| 2075 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2076 | /* 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] | 2077 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2078 | * 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] | 2079 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2080 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2081 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2082 | h2s = (struct h2s*)h2_refused_stream; |
| 2083 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2087 | h2s->rxbuf = rxbuf; |
| 2088 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2089 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2090 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2091 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2092 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2093 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2094 | |
| 2095 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2096 | if (h2s->st == H2_SS_OPEN) |
| 2097 | h2s->st = H2_SS_HREM; |
| 2098 | else |
| 2099 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2102 | /* update the max stream ID if the request is being processed */ |
| 2103 | if (h2s->id > h2c->max_id) |
| 2104 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2105 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2106 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2107 | |
| 2108 | conn_err: |
| 2109 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2110 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2111 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2112 | out: |
| 2113 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2114 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2115 | |
| 2116 | send_rst: |
| 2117 | /* make the demux send an RST for the current stream. We may only |
| 2118 | * do this if we're certain that the HEADERS frame was properly |
| 2119 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2120 | */ |
| 2121 | h2_release_buf(h2c, &rxbuf); |
| 2122 | h2c->st0 = H2_CS_FRAME_E; |
| 2123 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2124 | } |
| 2125 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2126 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2127 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2128 | * errors here are reported as connection errors since it's impossible to |
| 2129 | * recover from such errors after the compression context has been altered. |
| 2130 | */ |
| 2131 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2132 | { |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2133 | struct buffer rxbuf = BUF_NULL; |
| 2134 | unsigned long long body_len = 0; |
| 2135 | uint32_t flags = 0; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2136 | int error; |
| 2137 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2138 | if (!b_size(&h2c->dbuf)) |
| 2139 | return NULL; // empty buffer |
| 2140 | |
| 2141 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2142 | return NULL; // incomplete frame |
| 2143 | |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2144 | if (h2s->st != H2_SS_CLOSED) { |
| 2145 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
| 2146 | } |
| 2147 | else { |
| 2148 | /* the connection was already killed by an RST, let's consume |
| 2149 | * the data and send another RST. |
| 2150 | */ |
| 2151 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Christopher Faulet | 0342705 | 2019-09-26 16:19:13 +0200 | [diff] [blame] | 2152 | h2s = (struct h2s*)h2_error_stream; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2153 | h2c->st0 = H2_CS_FRAME_E; |
| 2154 | goto send_rst; |
| 2155 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2156 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2157 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2158 | if (h2c->st0 >= H2_CS_ERROR) |
| 2159 | return NULL; |
| 2160 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2161 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2162 | /* RFC7540#5.1 */ |
| 2163 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2164 | h2c->st0 = H2_CS_FRAME_E; |
| 2165 | return NULL; |
| 2166 | } |
| 2167 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2168 | if (error <= 0) { |
| 2169 | if (error == 0) |
| 2170 | return NULL; // missing data |
| 2171 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2172 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2173 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2174 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2175 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2178 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2179 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2180 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2181 | 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] | 2182 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2183 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2184 | if (h2s->st == H2_SS_OPEN) |
| 2185 | h2s->st = H2_SS_HREM; |
| 2186 | else if (h2s->st == H2_SS_HLOC) |
| 2187 | h2s_close(h2s); |
| 2188 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2189 | |
| 2190 | return h2s; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2191 | |
| 2192 | send_rst: |
| 2193 | /* make the demux send an RST for the current stream. We may only |
| 2194 | * do this if we're certain that the HEADERS frame was properly |
| 2195 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2196 | */ |
| 2197 | h2_release_buf(h2c, &rxbuf); |
| 2198 | h2c->st0 = H2_CS_FRAME_E; |
| 2199 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2202 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2203 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2204 | */ |
| 2205 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2206 | { |
| 2207 | int error; |
| 2208 | |
| 2209 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2210 | * to signal an end of stream (with the ES flag). |
| 2211 | */ |
| 2212 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2213 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2214 | return 0; // empty buffer |
| 2215 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2216 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2217 | return 0; // incomplete frame |
| 2218 | |
| 2219 | /* now either the frame is complete or the buffer is complete */ |
| 2220 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2221 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2222 | /* RFC7540#6.1 */ |
| 2223 | error = H2_ERR_STREAM_CLOSED; |
| 2224 | goto strm_err; |
| 2225 | } |
| 2226 | |
Christopher Faulet | 4fb65f4 | 2019-06-19 09:25:58 +0200 | [diff] [blame] | 2227 | 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] | 2228 | /* RFC7540#8.1.2 */ |
| 2229 | error = H2_ERR_PROTOCOL_ERROR; |
| 2230 | goto strm_err; |
| 2231 | } |
| 2232 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2233 | if (!h2_frt_transfer_data(h2s)) |
| 2234 | return 0; |
| 2235 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2236 | /* call the upper layers to process the frame, then let the upper layer |
| 2237 | * notify the stream about any change. |
| 2238 | */ |
| 2239 | if (!h2s->cs) { |
Willy Tarreau | c1a2965 | 2019-08-06 10:11:02 +0200 | [diff] [blame] | 2240 | /* The upper layer has already closed, this may happen on |
| 2241 | * 4xx/redirects during POST, or when receiving a response |
| 2242 | * from an H2 server after the client has aborted. |
| 2243 | */ |
| 2244 | error = H2_ERR_CANCEL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2245 | goto strm_err; |
| 2246 | } |
| 2247 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2248 | if (h2c->st0 >= H2_CS_ERROR) |
| 2249 | return 0; |
| 2250 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2251 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2252 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2253 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /* check for completion : the callee will change this to FRAME_A or |
| 2257 | * FRAME_H once done. |
| 2258 | */ |
| 2259 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2260 | return 0; |
| 2261 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2262 | /* last frame */ |
| 2263 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2264 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2265 | if (h2s->st == H2_SS_OPEN) |
| 2266 | h2s->st = H2_SS_HREM; |
| 2267 | else |
| 2268 | h2s_close(h2s); |
| 2269 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2270 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2271 | /* RFC7540#8.1.2 */ |
| 2272 | error = H2_ERR_PROTOCOL_ERROR; |
| 2273 | goto strm_err; |
| 2274 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2277 | return 1; |
| 2278 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2279 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2280 | h2s_error(h2s, error); |
| 2281 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2282 | return 0; |
| 2283 | } |
| 2284 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2285 | /* process Rx frames to be demultiplexed */ |
| 2286 | static void h2_process_demux(struct h2c *h2c) |
| 2287 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2288 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2289 | struct h2_fh hdr; |
| 2290 | unsigned int padlen = 0; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2291 | int32_t old_iw = h2c->miw; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2292 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2293 | if (h2c->st0 >= H2_CS_ERROR) |
| 2294 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2295 | |
| 2296 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2297 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2298 | if (h2c->flags & H2_CF_IS_BACK) |
| 2299 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2300 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2301 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2302 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2303 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2304 | sess_log(h2c->conn->owner); |
| 2305 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2306 | goto fail; |
| 2307 | } |
| 2308 | |
| 2309 | h2c->max_id = 0; |
| 2310 | h2c->st0 = H2_CS_SETTINGS1; |
| 2311 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2312 | |
| 2313 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2314 | /* ensure that what is pending is a valid SETTINGS frame |
| 2315 | * without an ACK. |
| 2316 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2317 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2318 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2319 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2320 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2321 | sess_log(h2c->conn->owner); |
| 2322 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2323 | goto fail; |
| 2324 | } |
| 2325 | |
| 2326 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2327 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2328 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2329 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2330 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2331 | goto fail; |
| 2332 | } |
| 2333 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2334 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2335 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2336 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2337 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2338 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2339 | goto fail; |
| 2340 | } |
| 2341 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2342 | /* that's OK, switch to FRAME_P to process it. This is |
| 2343 | * a SETTINGS frame whose header has already been |
| 2344 | * deleted above. |
| 2345 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2346 | padlen = 0; |
| 2347 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2348 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2349 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2350 | |
| 2351 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2352 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2353 | int ret = 0; |
| 2354 | |
| 2355 | if (h2c->st0 >= H2_CS_ERROR) |
| 2356 | break; |
| 2357 | |
| 2358 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | ab3ebbc | 2019-08-06 15:49:51 +0200 | [diff] [blame] | 2359 | h2c->rcvd_s = 0; |
| 2360 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2361 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2362 | break; |
| 2363 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2364 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2365 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2366 | if (!h2c->nb_streams) { |
| 2367 | /* only log if no other stream can report the error */ |
| 2368 | sess_log(h2c->conn->owner); |
| 2369 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2370 | break; |
| 2371 | } |
| 2372 | |
Christopher Faulet | 3d574a5 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2373 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2374 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2375 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2376 | * we read the pad length and drop it from the remaining |
| 2377 | * payload (one byte + the 9 remaining ones = 10 total |
| 2378 | * removed), so we have a frame payload starting after the |
| 2379 | * pad len. Flow controlled frames (DATA) also count the |
| 2380 | * padlen in the flow control, so it must be adjusted. |
| 2381 | */ |
| 2382 | if (hdr.len < 1) { |
| 2383 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2384 | sess_log(h2c->conn->owner); |
| 2385 | goto fail; |
| 2386 | } |
| 2387 | hdr.len--; |
| 2388 | |
| 2389 | if (b_data(&h2c->dbuf) < 10) |
| 2390 | break; // missing padlen |
| 2391 | |
| 2392 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2393 | |
| 2394 | if (padlen > hdr.len) { |
| 2395 | /* RFC7540#6.1 : pad length = length of |
| 2396 | * frame payload or greater => error. |
| 2397 | */ |
| 2398 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2399 | sess_log(h2c->conn->owner); |
| 2400 | goto fail; |
| 2401 | } |
| 2402 | |
| 2403 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2404 | h2c->rcvd_c++; |
| 2405 | h2c->rcvd_s++; |
| 2406 | } |
| 2407 | b_del(&h2c->dbuf, 1); |
| 2408 | } |
| 2409 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2410 | |
| 2411 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2412 | h2c->dfl = hdr.len; |
| 2413 | h2c->dsi = hdr.sid; |
| 2414 | h2c->dft = hdr.ft; |
| 2415 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2416 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2417 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2418 | |
| 2419 | /* check for minimum basic frame format validity */ |
| 2420 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2421 | if (ret != H2_ERR_NO_ERROR) { |
| 2422 | h2c_error(h2c, ret); |
| 2423 | sess_log(h2c->conn->owner); |
| 2424 | goto fail; |
| 2425 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2428 | /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here. |
| 2429 | * H2_CS_FRAME_P indicates an incomplete previous operation |
| 2430 | * (most often the first attempt) and requires some validity |
| 2431 | * checks for the frame and the current state. The two other |
| 2432 | * ones are set after completion (or abortion) and must skip |
| 2433 | * validity checks. |
| 2434 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2435 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2436 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2437 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2438 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2439 | conn_xprt_read0_pending(h2c->conn) || |
| 2440 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2441 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2442 | (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] | 2443 | /* we may have to signal the upper layers */ |
| 2444 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2445 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2446 | } |
| 2447 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2448 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2449 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2450 | goto strm_err; |
| 2451 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2452 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2453 | goto valid_frame_type; |
| 2454 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2455 | if (h2s->st == H2_SS_IDLE && |
| 2456 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2457 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2458 | * this state MUST be treated as a connection error |
| 2459 | */ |
| 2460 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2461 | if (!h2c->nb_streams) { |
| 2462 | /* only log if no other stream can report the error */ |
| 2463 | sess_log(h2c->conn->owner); |
| 2464 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2465 | break; |
| 2466 | } |
| 2467 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2468 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2469 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2470 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2471 | * this state MUST be treated as a stream error. |
| 2472 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2473 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2474 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2475 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2476 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2477 | else |
| 2478 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2479 | goto strm_err; |
| 2480 | } |
| 2481 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2482 | /* Below the management of frames received in closed state is a |
| 2483 | * bit hackish because the spec makes strong differences between |
| 2484 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2485 | * in both directions. In addition to this, the creation of a |
| 2486 | * new stream reusing the identifier of a closed one will be |
| 2487 | * detected here. Given that we cannot keep track of all closed |
| 2488 | * streams forever, we consider that unknown closed streams were |
| 2489 | * closed on RST received, which allows us to respond with an |
| 2490 | * RST without breaking the connection (eg: to abort a transfer). |
| 2491 | * Some frames have to be silently ignored as well. |
| 2492 | */ |
| 2493 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2494 | 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] | 2495 | /* #5.1.1: The identifier of a newly |
| 2496 | * established stream MUST be numerically |
| 2497 | * greater than all streams that the initiating |
| 2498 | * endpoint has opened or reserved. This |
| 2499 | * governs streams that are opened using a |
| 2500 | * HEADERS frame and streams that are reserved |
| 2501 | * using PUSH_PROMISE. An endpoint that |
| 2502 | * receives an unexpected stream identifier |
| 2503 | * MUST respond with a connection error. |
| 2504 | */ |
| 2505 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2506 | goto strm_err; |
| 2507 | } |
| 2508 | |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2509 | if (h2s->flags & H2_SF_RST_RCVD && |
| 2510 | !(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] | 2511 | /* RFC7540#5.1:closed: an endpoint that |
| 2512 | * receives any frame other than PRIORITY after |
| 2513 | * receiving a RST_STREAM MUST treat that as a |
| 2514 | * stream error of type STREAM_CLOSED. |
| 2515 | * |
| 2516 | * Note that old streams fall into this category |
| 2517 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2518 | * |
| 2519 | * However, we cannot generalize this to all frame types. Those |
| 2520 | * carrying compression state must still be processed before |
| 2521 | * being dropped or we'll desynchronize the decoder. This can |
| 2522 | * happen with request trailers received after sending an |
| 2523 | * RST_STREAM, or with header/trailers responses received after |
| 2524 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2525 | * |
| 2526 | * In addition, since our CLOSED streams always carry the |
| 2527 | * RST_RCVD bit, we don't want to accidently catch valid |
| 2528 | * frames for a closed stream, i.e. RST/PRIO/WU. |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2529 | */ |
| 2530 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2531 | h2c->st0 = H2_CS_FRAME_E; |
| 2532 | goto strm_err; |
| 2533 | } |
| 2534 | |
| 2535 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2536 | * result of sending a RST_STREAM frame, the peer that |
| 2537 | * receives the RST_STREAM might have already sent |
| 2538 | * frames on the stream that cannot be withdrawn. An |
| 2539 | * endpoint MUST ignore frames that it receives on |
| 2540 | * closed streams after it has sent a RST_STREAM |
| 2541 | * frame. An endpoint MAY choose to limit the period |
| 2542 | * over which it ignores frames and treat frames that |
| 2543 | * arrive after this time as being in error. |
| 2544 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2545 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2546 | /* RFC7540#5.1:closed: any frame other than |
| 2547 | * PRIO/WU/RST in this state MUST be treated as |
| 2548 | * a connection error |
| 2549 | */ |
| 2550 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2551 | h2c->dft != H2_FT_PRIORITY && |
| 2552 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2553 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2554 | goto strm_err; |
| 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2559 | #if 0 |
| 2560 | // problem below: it is not possible to completely ignore such |
| 2561 | // streams as we need to maintain the compression state as well |
| 2562 | // and for this we need to completely process these frames (eg: |
| 2563 | // HEADERS frames) as well as counting DATA frames to emit |
| 2564 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2565 | // This is a typical case of layer violation where the |
| 2566 | // transported contents are critical to the connection's |
| 2567 | // validity and must be ignored at the same time :-( |
| 2568 | |
| 2569 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2570 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2571 | */ |
| 2572 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2573 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2574 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2575 | h2c->dfl -= ret; |
| 2576 | ret = h2c->dfl == 0; |
| 2577 | goto strm_err; |
| 2578 | } |
| 2579 | #endif |
| 2580 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2581 | valid_frame_type: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2582 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2583 | case H2_FT_SETTINGS: |
| 2584 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2585 | ret = h2c_handle_settings(h2c); |
| 2586 | |
| 2587 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2588 | ret = h2c_ack_settings(h2c); |
| 2589 | break; |
| 2590 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2591 | case H2_FT_PING: |
| 2592 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2593 | ret = h2c_handle_ping(h2c); |
| 2594 | |
| 2595 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2596 | ret = h2c_ack_ping(h2c); |
| 2597 | break; |
| 2598 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2599 | case H2_FT_WINDOW_UPDATE: |
| 2600 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2601 | ret = h2c_handle_window_update(h2c, h2s); |
| 2602 | break; |
| 2603 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2604 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2605 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2606 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2607 | * frames' parsers consume all following CONTINUATION |
| 2608 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2609 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2610 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2611 | sess_log(h2c->conn->owner); |
| 2612 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2613 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2614 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2615 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2616 | if (h2c->flags & H2_CF_IS_BACK) |
| 2617 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2618 | else |
| 2619 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2620 | if (tmp_h2s) { |
| 2621 | h2s = tmp_h2s; |
| 2622 | ret = 1; |
| 2623 | } |
| 2624 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2625 | break; |
| 2626 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2627 | case H2_FT_DATA: |
| 2628 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2629 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2630 | |
| 2631 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2632 | ret = h2c_send_strm_wu(h2c); |
| 2633 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2634 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2635 | case H2_FT_PRIORITY: |
| 2636 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2637 | ret = h2c_handle_priority(h2c); |
| 2638 | break; |
| 2639 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2640 | case H2_FT_RST_STREAM: |
| 2641 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2642 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2643 | break; |
| 2644 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2645 | case H2_FT_GOAWAY: |
| 2646 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2647 | ret = h2c_handle_goaway(h2c); |
| 2648 | break; |
| 2649 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2650 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2651 | default: |
| 2652 | /* drop frames that we ignore. They may be larger than |
| 2653 | * the buffer so we drain all of their contents until |
| 2654 | * we reach the end. |
| 2655 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2656 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2657 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2658 | h2c->dfl -= ret; |
| 2659 | ret = h2c->dfl == 0; |
| 2660 | } |
| 2661 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2662 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2663 | /* We may have to send an RST if not done yet */ |
| 2664 | if (h2s->st == H2_SS_ERROR) |
| 2665 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2666 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2667 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2668 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2670 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2671 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2672 | break; |
| 2673 | |
| 2674 | if (h2c->st0 != H2_CS_FRAME_H) { |
Christopher Faulet | e12a26f | 2019-09-26 16:38:28 +0200 | [diff] [blame] | 2675 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2676 | b_del(&h2c->dbuf, ret); |
| 2677 | h2c->dfl -= ret; |
| 2678 | if (!h2c->dfl) |
| 2679 | h2c->st0 = H2_CS_FRAME_H; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2680 | } |
| 2681 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2682 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2683 | if (h2c->rcvd_c > 0 && |
| 2684 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2685 | h2c_send_conn_wu(h2c); |
| 2686 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2687 | fail: |
| 2688 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2689 | if (h2s && h2s->cs && |
| 2690 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2691 | conn_xprt_read0_pending(h2c->conn) || |
| 2692 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2693 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2694 | (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] | 2695 | /* we may have to signal the upper layers */ |
| 2696 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2697 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2698 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2699 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2700 | if (old_iw != h2c->miw) |
| 2701 | h2c_unblock_sfctl(h2c); |
| 2702 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2703 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2707 | * the end. |
| 2708 | */ |
| 2709 | static int h2_process_mux(struct h2c *h2c) |
| 2710 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2711 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2712 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2713 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2714 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2715 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2716 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2717 | if (h2c->st0 == H2_CS_ERROR) { |
| 2718 | h2c->st0 = H2_CS_ERROR2; |
| 2719 | sess_log(h2c->conn->owner); |
| 2720 | } |
| 2721 | goto fail; |
| 2722 | } |
| 2723 | h2c->st0 = H2_CS_SETTINGS1; |
| 2724 | } |
| 2725 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2726 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2727 | return 1; |
| 2728 | } |
| 2729 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2730 | /* start by sending possibly pending window updates */ |
Willy Tarreau | 9c497c2 | 2019-08-06 15:39:32 +0200 | [diff] [blame] | 2731 | if (h2c->rcvd_s > 0 && |
| 2732 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2733 | h2c_send_strm_wu(h2c) < 0) |
| 2734 | goto fail; |
| 2735 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2736 | if (h2c->rcvd_c > 0 && |
| 2737 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2738 | h2c_send_conn_wu(h2c) < 0) |
| 2739 | goto fail; |
| 2740 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2741 | /* First we always process the flow control list because the streams |
| 2742 | * waiting there were already elected for immediate emission but were |
| 2743 | * blocked just on this. |
| 2744 | */ |
| 2745 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2746 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2747 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2748 | h2c->st0 >= H2_CS_ERROR) |
| 2749 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2750 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2751 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2752 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2753 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2754 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2755 | /* For some reason, the upper layer failed to subsribe again, |
| 2756 | * so remove it from the send_list |
| 2757 | */ |
| 2758 | if (!h2s->send_wait) { |
| 2759 | LIST_DEL_INIT(&h2s->list); |
| 2760 | continue; |
| 2761 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2762 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2763 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2764 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2765 | } |
| 2766 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2767 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2768 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2769 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2770 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2771 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2772 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2773 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2774 | /* For some reason, the upper layer failed to subsribe again, |
| 2775 | * so remove it from the send_list |
| 2776 | */ |
| 2777 | if (!h2s->send_wait) { |
| 2778 | LIST_DEL_INIT(&h2s->list); |
| 2779 | continue; |
| 2780 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2781 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2782 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2783 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2784 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2787 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2788 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2789 | if (h2c->st0 == H2_CS_ERROR) { |
| 2790 | if (h2c->max_id >= 0) { |
| 2791 | h2c_send_goaway_error(h2c, NULL); |
| 2792 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2793 | return 0; |
| 2794 | } |
| 2795 | |
| 2796 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2797 | } |
| 2798 | return 1; |
| 2799 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2800 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2801 | } |
| 2802 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2803 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2804 | /* Attempt to read data, and subscribe if none available. |
| 2805 | * The function returns 1 if data has been received, otherwise zero. |
| 2806 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2807 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2808 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2809 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2810 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2811 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2812 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2813 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2814 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2815 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2816 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2817 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2818 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2819 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2820 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2821 | if (!buf) { |
| 2822 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2823 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2824 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2825 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2826 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2827 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2828 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2829 | /* HTX in use : try to pre-align the buffer like the |
| 2830 | * rxbufs will be to optimize memory copies. We'll make |
| 2831 | * sure that the frame header lands at the end of the |
| 2832 | * HTX block to alias it upon recv. We cannot use the |
| 2833 | * head because rcv_buf() will realign the buffer if |
| 2834 | * it's empty. Thus we cheat and pretend we already |
| 2835 | * have a few bytes there. |
| 2836 | */ |
| 2837 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2838 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2839 | } |
| 2840 | else |
| 2841 | max = b_room(buf); |
| 2842 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2843 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2844 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2845 | else |
| 2846 | ret = 0; |
| 2847 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2848 | |
Willy Tarreau | a8fcdac | 2019-08-02 07:48:47 +0200 | [diff] [blame] | 2849 | if (max && !ret && h2_recv_allowed(h2c)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2850 | 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] | 2851 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2852 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2853 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2854 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2857 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2858 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2859 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2860 | } |
| 2861 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2862 | /* Try to send data if possible. |
| 2863 | * The function returns 1 if data have been sent, otherwise zero. |
| 2864 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2865 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2866 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2867 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2868 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2869 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2870 | |
| 2871 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2872 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2873 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2874 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2875 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2876 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2877 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2878 | } |
| 2879 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2880 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2881 | * pending streams into the existing buffer until it's reportedly full |
| 2882 | * or the end of send requests is reached. Then it tries to send this |
| 2883 | * buffer's contents out, marks it not full if at least one byte could |
| 2884 | * be sent, and tries again. |
| 2885 | * |
| 2886 | * The snd_buf() function normally takes a "flags" argument which may |
| 2887 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2888 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2889 | * connection is streaming lots of data (used to increase TLS record |
| 2890 | * size at the expense of latency). The former can be sent any time |
| 2891 | * there's a buffer full flag, as it indicates at least one stream |
| 2892 | * attempted to send and failed so there are pending data. An |
| 2893 | * alternative would be to set it as long as there's an active stream |
| 2894 | * but that would be problematic for ACKs until we have an absolute |
| 2895 | * guarantee that all waiters have at least one byte to send. The |
| 2896 | * latter should possibly not be set for now. |
| 2897 | */ |
| 2898 | |
| 2899 | done = 0; |
| 2900 | while (!done) { |
| 2901 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2902 | unsigned int released = 0; |
| 2903 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2904 | |
| 2905 | /* fill as much as we can into the current buffer */ |
| 2906 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2907 | done = h2_process_mux(h2c); |
| 2908 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2909 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2910 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2911 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2912 | if (conn->flags & CO_FL_ERROR) |
| 2913 | break; |
| 2914 | |
| 2915 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2916 | flags |= CO_SFL_MSG_MORE; |
| 2917 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2918 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2919 | if (b_data(buf)) { |
| 2920 | 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] | 2921 | if (!ret) { |
| 2922 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2923 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2924 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2925 | sent = 1; |
| 2926 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2927 | if (b_data(buf)) { |
| 2928 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2929 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2930 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2931 | } |
| 2932 | b_free(buf); |
| 2933 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2934 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2935 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2936 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2937 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2938 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2939 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2940 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2941 | } |
| 2942 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2943 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2944 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2945 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2946 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2947 | /* We're not full anymore, so we can wake any task that are waiting |
| 2948 | * for us. |
| 2949 | */ |
Willy Tarreau | c51d47c | 2019-09-25 07:57:31 +0200 | [diff] [blame] | 2950 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2951 | struct h2s *h2s; |
| 2952 | |
| 2953 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2954 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2955 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2956 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2957 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2958 | continue; |
| 2959 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2960 | /* For some reason, the upper layer failed to subsribe again, |
| 2961 | * so remove it from the send_list |
| 2962 | */ |
| 2963 | if (!h2s->send_wait) { |
| 2964 | LIST_DEL_INIT(&h2s->list); |
| 2965 | continue; |
| 2966 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2967 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2968 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2969 | tasklet_wakeup(h2s->send_wait->tasklet); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2970 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2971 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2972 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2973 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2974 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2975 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2976 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2977 | 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] | 2978 | 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] | 2979 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2980 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2981 | } |
| 2982 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2983 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2984 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2985 | { |
| 2986 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2987 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2988 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2989 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2990 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2991 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2992 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2993 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2994 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2995 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2996 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2997 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2998 | /* callback called on any event by the connection handler. |
| 2999 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 3000 | * destruction of the connection (which normally doesn not happen in h2). |
| 3001 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3002 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3003 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3004 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3005 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3006 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3007 | h2_process_demux(h2c); |
| 3008 | |
| 3009 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3010 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3011 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3012 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3013 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 3014 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3015 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3016 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 3017 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 3018 | /* frontend is stopping, reload likely in progress, let's try |
| 3019 | * to announce a graceful shutdown if not yet done. We don't |
| 3020 | * care if it fails, it will be tried again later. |
| 3021 | */ |
| 3022 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3023 | if (h2c->last_sid < 0) |
| 3024 | h2c->last_sid = (1U << 31) - 1; |
| 3025 | h2c_send_goaway_error(h2c, NULL); |
| 3026 | } |
| 3027 | } |
| 3028 | |
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 we received early data, and the handshake is done, wake |
| 3031 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3032 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3033 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 3034 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 3035 | struct eb32_node *node; |
| 3036 | struct h2s *h2s; |
| 3037 | |
| 3038 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 3039 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 3040 | |
| 3041 | while (node) { |
| 3042 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 3043 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 3044 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3045 | node = eb32_next(node); |
| 3046 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3047 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3048 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 3049 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 3050 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 3051 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 3052 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3053 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3054 | |
| 3055 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3056 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3057 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3058 | return -1; |
| 3059 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3062 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3063 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3064 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3065 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3066 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 3067 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3068 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3069 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 3070 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 3071 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3072 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 3073 | if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3074 | if (h2c_may_expire(h2c)) |
| 3075 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3076 | else |
| 3077 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3078 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3079 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3080 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3081 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3082 | return 0; |
| 3083 | } |
| 3084 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3085 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3086 | static int h2_wake(struct connection *conn) |
| 3087 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3088 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3089 | |
| 3090 | return (h2_process(h2c)); |
| 3091 | } |
| 3092 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3093 | /* Connection timeout management. The principle is that if there's no receipt |
| 3094 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3095 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3096 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3097 | * GOAWAY frame. |
| 3098 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3099 | 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] | 3100 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3101 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3102 | int expired = tick_is_expired(t->expire, now_ms); |
| 3103 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3104 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3105 | return t; |
| 3106 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3107 | if (h2c && !h2c_may_expire(h2c)) { |
| 3108 | /* we do still have streams but all of them are idle, waiting |
| 3109 | * for the data layer, so we must not enforce the timeout here. |
| 3110 | */ |
| 3111 | t->expire = TICK_ETERNITY; |
| 3112 | return t; |
| 3113 | } |
| 3114 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3115 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3116 | |
| 3117 | if (!h2c) { |
| 3118 | /* resources were already deleted */ |
| 3119 | return NULL; |
| 3120 | } |
| 3121 | |
| 3122 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3123 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3124 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3125 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3126 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3127 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3128 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3129 | } |
| 3130 | |
| 3131 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3132 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3133 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3134 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3135 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3136 | 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] | 3137 | unsigned int released = 0; |
| 3138 | struct buffer *buf; |
| 3139 | |
| 3140 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3141 | if (b_data(buf)) { |
| 3142 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3143 | if (!ret) |
| 3144 | break; |
| 3145 | b_del(buf, ret); |
| 3146 | if (b_data(buf)) |
| 3147 | break; |
| 3148 | b_free(buf); |
| 3149 | released++; |
| 3150 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3151 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3152 | |
| 3153 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3154 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3155 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3156 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3157 | /* either we can release everything now or it will be done later once |
| 3158 | * the last stream closes. |
| 3159 | */ |
| 3160 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3161 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3162 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3163 | return NULL; |
| 3164 | } |
| 3165 | |
| 3166 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3167 | /*******************************************/ |
| 3168 | /* functions below are used by the streams */ |
| 3169 | /*******************************************/ |
| 3170 | |
| 3171 | /* |
| 3172 | * Attach a new stream to a connection |
| 3173 | * (Used for outgoing connections) |
| 3174 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3175 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3176 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3177 | struct conn_stream *cs; |
| 3178 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3179 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3180 | |
| 3181 | cs = cs_new(conn); |
| 3182 | if (!cs) |
| 3183 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3184 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3185 | if (!h2s) { |
| 3186 | cs_free(cs); |
| 3187 | return NULL; |
| 3188 | } |
| 3189 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3192 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3193 | * We have to scan because we may have some orphan streams. It might be |
| 3194 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3195 | * orphans. |
| 3196 | */ |
| 3197 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3198 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3199 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3200 | struct h2s *h2s; |
| 3201 | struct eb32_node *node; |
| 3202 | |
| 3203 | node = eb32_first(&h2c->streams_by_id); |
| 3204 | while (node) { |
| 3205 | h2s = container_of(node, struct h2s, by_id); |
| 3206 | if (h2s->cs) |
| 3207 | return h2s->cs; |
| 3208 | node = eb32_next(node); |
| 3209 | } |
| 3210 | return NULL; |
| 3211 | } |
| 3212 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3213 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3214 | * Destroy the mux and the associated connection, if it is no longer used |
| 3215 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3216 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3217 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3218 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3219 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3220 | 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] | 3221 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3225 | * Detach the stream from the connection and possibly release the connection. |
| 3226 | */ |
| 3227 | static void h2_detach(struct conn_stream *cs) |
| 3228 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3229 | struct h2s *h2s = cs->ctx; |
| 3230 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3231 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3232 | |
| 3233 | cs->ctx = NULL; |
| 3234 | if (!h2s) |
| 3235 | return; |
| 3236 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3237 | /* 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] | 3238 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3239 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3240 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3241 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3242 | /* |
| 3243 | * At this point, the stream_interface is supposed to have called |
| 3244 | * h2_unsubscribe(), so the only way there's still a |
| 3245 | * subscription that came from the stream_interface (as we |
| 3246 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3247 | * without the stream_interface involved) is that we subscribed |
| 3248 | * for sending, we woke the tasklet up and removed the |
| 3249 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3250 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3251 | * run yet. Make sure to handle that by explicitely setting |
| 3252 | * send_wait to NULL, as nothing else will do it for us. |
| 3253 | */ |
| 3254 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3255 | } |
| 3256 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3257 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3258 | h2c = h2s->h2c; |
| 3259 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3260 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3261 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3262 | !h2_frt_has_too_many_cs(h2c)) { |
| 3263 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3264 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3265 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3266 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3267 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3268 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3269 | * an ES or RST frame), so orphan it in this case. |
| 3270 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3271 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3272 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3273 | (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] | 3274 | return; |
| 3275 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3276 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3277 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3278 | /* unblock the connection if it was blocked on this |
| 3279 | * stream. |
| 3280 | */ |
| 3281 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3282 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3283 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3286 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3287 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3288 | if (h2c->flags & H2_CF_IS_BACK && |
| 3289 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3290 | if (!(h2c->conn->flags & |
| 3291 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3292 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3293 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3294 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3295 | h2c->conn->owner = NULL; |
| 3296 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3297 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3298 | /* The server doesn't want it, let's kill the connection right away */ |
| 3299 | h2c->conn->mux->destroy(h2c->conn); |
| 3300 | return; |
| 3301 | } |
| 3302 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3303 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3304 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3305 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3306 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3307 | return; |
| 3308 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3309 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3310 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3311 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3312 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3313 | struct server *srv = objt_server(h2c->conn->target); |
| 3314 | |
| 3315 | if (srv) { |
| 3316 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3317 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3318 | else |
| 3319 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3320 | } |
| 3321 | |
| 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3326 | /* We don't want to close right now unless we're removing the |
| 3327 | * last stream, and either the connection is in error, or it |
| 3328 | * reached the ID already specified in a GOAWAY frame received |
| 3329 | * or sent (as seen by last_sid >= 0). |
| 3330 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3331 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3332 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3333 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3334 | } |
| 3335 | else if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3336 | if (h2c_may_expire(h2c)) |
| 3337 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3338 | else |
| 3339 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3340 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3341 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3342 | } |
| 3343 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3344 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3345 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3346 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3347 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3348 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3349 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3350 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3351 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3352 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3353 | /* a connstream may require us to immediately kill the whole connection |
| 3354 | * for example because of a "tcp-request content reject" rule that is |
| 3355 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3356 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3357 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3358 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3359 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3360 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3361 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3362 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3363 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3364 | /* Nothing was never sent for this stream, so reset with |
| 3365 | * REFUSED_STREAM error to let the client retry the |
| 3366 | * request. |
| 3367 | */ |
| 3368 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3369 | } |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3370 | else { |
| 3371 | /* a final response was already provided, we don't want this |
| 3372 | * stream anymore. This may happen when the server responds |
| 3373 | * before the end of an upload and closes quickly (redirect, |
| 3374 | * deny, ...) |
| 3375 | */ |
| 3376 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3377 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3378 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3379 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3380 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3381 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3382 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3383 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3384 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3385 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3386 | done: |
| 3387 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3388 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3389 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3390 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3391 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3392 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3393 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3394 | h2s->send_wait = sw; |
| 3395 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3396 | h2s->send_wait = sw; |
| 3397 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3398 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3399 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3400 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3401 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3402 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3403 | } |
| 3404 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3405 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3406 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3407 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3408 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3409 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3410 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3411 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3412 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3413 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3414 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3415 | /* we can cleanly close using an empty data frame only after headers */ |
| 3416 | |
| 3417 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3418 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3419 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3420 | |
| 3421 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3422 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3423 | else |
| 3424 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3425 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3426 | /* a connstream may require us to immediately kill the whole connection |
| 3427 | * for example because of a "tcp-request content reject" rule that is |
| 3428 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3429 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3430 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3431 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3432 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3433 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3434 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3435 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3436 | else { |
| 3437 | /* Nothing was never sent for this stream, so reset with |
| 3438 | * REFUSED_STREAM error to let the client retry the |
| 3439 | * request. |
| 3440 | */ |
| 3441 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3442 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3443 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3444 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3445 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3446 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3447 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3448 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3451 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3452 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3453 | done: |
| 3454 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3455 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3456 | |
| 3457 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3458 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3459 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3460 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3461 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3462 | h2s->send_wait = sw; |
| 3463 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3464 | h2s->send_wait = sw; |
| 3465 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3466 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3467 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3468 | /* let the handler know we want to shutw */ |
| 3469 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3470 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3471 | } |
| 3472 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3473 | /* 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] | 3474 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3475 | * and prevented the last frame from being emitted. |
| 3476 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3477 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3478 | { |
| 3479 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3480 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3481 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3482 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3483 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3484 | h2_do_shutw(h2s); |
| 3485 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3486 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3487 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3488 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3489 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3490 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3491 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3492 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3493 | if (!h2s->cs) { |
| 3494 | h2s_destroy(h2s); |
| 3495 | if (h2c_is_dead(h2c)) |
| 3496 | h2_release(h2c); |
| 3497 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3498 | } |
| 3499 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3500 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3503 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3504 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3505 | { |
| 3506 | struct h2s *h2s = cs->ctx; |
| 3507 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3508 | if (cs->flags & CS_FL_KILL_CONN) |
| 3509 | h2s->flags |= H2_SF_KILL_CONN; |
| 3510 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3511 | if (!mode) |
| 3512 | return; |
| 3513 | |
| 3514 | h2_do_shutr(h2s); |
| 3515 | } |
| 3516 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3517 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3518 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3519 | { |
| 3520 | struct h2s *h2s = cs->ctx; |
| 3521 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3522 | if (cs->flags & CS_FL_KILL_CONN) |
| 3523 | h2s->flags |= H2_SF_KILL_CONN; |
| 3524 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3525 | h2_do_shutw(h2s); |
| 3526 | } |
| 3527 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3528 | /* 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] | 3529 | * HTX request or response depending on the connection's side. Returns a |
| 3530 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3531 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3532 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3533 | * 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] | 3534 | * |
| 3535 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3536 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3537 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3538 | * leaving a hole between the main frame and the beginning of the next one. |
| 3539 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3540 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3541 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3542 | * |
| 3543 | * A buffer at the beginning of processing may look like this : |
| 3544 | * |
| 3545 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3546 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3547 | * `---^---------^-----^--------------^--------------^------^---' |
| 3548 | * | | <-----> | | |
| 3549 | * area | dpl | wrap |
| 3550 | * |<--------------> | |
| 3551 | * | dfl | |
| 3552 | * |<-------------------------------------------------->| |
| 3553 | * head data |
| 3554 | * |
| 3555 | * Padding is automatically overwritten when folding, participating to the |
| 3556 | * hole size after dfl : |
| 3557 | * |
| 3558 | * ,---.------------------------.-----.--------------.------.---. |
| 3559 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3560 | * `---^------------------------^-----^--------------^------^---' |
| 3561 | * | | <-----> | | |
| 3562 | * area | hole | wrap |
| 3563 | * |<-----------------------> | |
| 3564 | * | dfl | |
| 3565 | * |<-------------------------------------------------->| |
| 3566 | * head data |
| 3567 | * |
| 3568 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3569 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3570 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3571 | * |
| 3572 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3573 | * so that the function can update the following flags : |
| 3574 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3575 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3576 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3577 | * |
| 3578 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3579 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3580 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3581 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3582 | 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] | 3583 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3584 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3585 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3586 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3587 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3588 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3589 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3590 | int flen; // header frame len |
| 3591 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3592 | int ret = 0; |
| 3593 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3594 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3595 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3596 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3597 | next_frame: |
| 3598 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3599 | goto leave; // incomplete input frame |
| 3600 | |
| 3601 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3602 | * this case, we'll try to paste it immediately after the initial |
| 3603 | * HEADERS frame payload and kill any possible padding. The initial |
| 3604 | * frame's length will be increased to represent the concatenation |
| 3605 | * of the two frames. The next frame is read from position <tlen> |
| 3606 | * and written at position <flen> (minus padding if some is present). |
| 3607 | */ |
| 3608 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3609 | struct h2_fh hdr; |
| 3610 | int clen; // CONTINUATION frame's payload length |
| 3611 | |
| 3612 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3613 | /* no more data, the buffer may be full, either due to |
| 3614 | * too large a frame or because of too large a hole that |
| 3615 | * we're going to compact at the end. |
| 3616 | */ |
| 3617 | goto leave; |
| 3618 | } |
| 3619 | |
| 3620 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3621 | /* RFC7540#6.10: frame of unexpected type */ |
| 3622 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3623 | goto fail; |
| 3624 | } |
| 3625 | |
| 3626 | if (hdr.sid != h2c->dsi) { |
| 3627 | /* RFC7540#6.10: frame of different stream */ |
| 3628 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3629 | goto fail; |
| 3630 | } |
| 3631 | |
| 3632 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3633 | /* RFC7540#4.2: invalid frame length */ |
| 3634 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3635 | goto fail; |
| 3636 | } |
| 3637 | |
| 3638 | /* detect when we must stop aggragating frames */ |
| 3639 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3640 | |
| 3641 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3642 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3643 | if (clen > hdr.len) |
| 3644 | clen = hdr.len; |
| 3645 | |
| 3646 | /* Move the frame's payload over the padding, hole and frame |
| 3647 | * header. At least one of hole or dpl is null (see diagrams |
| 3648 | * above). The hole moves after the new aggragated frame. |
| 3649 | */ |
| 3650 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3651 | h2c->dfl += clen - h2c->dpl; |
| 3652 | hole += h2c->dpl + 9; |
| 3653 | h2c->dpl = 0; |
| 3654 | goto next_frame; |
| 3655 | } |
| 3656 | |
| 3657 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3658 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3659 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3660 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3661 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3662 | copy = alloc_trash_chunk(); |
| 3663 | if (!copy) { |
| 3664 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3665 | goto fail; |
| 3666 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3667 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3668 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3669 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3672 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3673 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3674 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3675 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3676 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3677 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3678 | } |
| 3679 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3680 | if (flen < 5) { |
| 3681 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3682 | goto fail; |
| 3683 | } |
| 3684 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3685 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3686 | flen -= 5; |
| 3687 | } |
| 3688 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3689 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3690 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3691 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3692 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3693 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3694 | /* we can't retry a failed decompression operation so we must be very |
| 3695 | * careful not to take any risks. In practice the output buffer is |
| 3696 | * always empty except maybe for trailers, in which case we simply have |
| 3697 | * to wait for the upper layer to finish consuming what is available. |
| 3698 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3699 | |
| 3700 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3701 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3702 | if (!htx_is_empty(htx)) { |
| 3703 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3704 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3705 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3706 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3707 | if (b_data(rxbuf)) { |
| 3708 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3709 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3710 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3711 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3712 | rxbuf->head = 0; |
| 3713 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3714 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3715 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3716 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3717 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3718 | sizeof(list)/sizeof(list[0]), tmp); |
| 3719 | if (outlen < 0) { |
| 3720 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3721 | goto fail; |
| 3722 | } |
| 3723 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3724 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3725 | * the parser's state to commit these changes and allow us to later |
| 3726 | * fail solely on the stream if needed. |
| 3727 | */ |
| 3728 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3729 | h2c->dfl = hole = 0; |
| 3730 | h2c->st0 = H2_CS_FRAME_H; |
| 3731 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3732 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3733 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3734 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3735 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3736 | goto trailers; |
| 3737 | |
| 3738 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3739 | if (htx) { |
| 3740 | /* HTX mode */ |
| 3741 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3742 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3743 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3744 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3745 | } else { |
| 3746 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3747 | 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] | 3748 | if (outlen > 0) |
| 3749 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3750 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3751 | |
| 3752 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3753 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3754 | goto fail; |
| 3755 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3756 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3757 | if (msgf & H2_MSGF_BODY) { |
| 3758 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3759 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3760 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3761 | if (htx) |
| 3762 | htx->extra = *body_len; |
| 3763 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3764 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3765 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3766 | } |
| 3767 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3768 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3769 | /* indicate that a HEADERS frame was received for this stream, except |
| 3770 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3771 | * expected. |
| 3772 | */ |
| 3773 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3774 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3775 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3776 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3777 | /* Mark the end of message, either using EOM in HTX or with the |
| 3778 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3779 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3780 | * we must not leave an HTX trailers block not followed by an |
| 3781 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3782 | * 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] | 3783 | */ |
| 3784 | if (htx) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 3785 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3786 | goto fail; |
| 3787 | } |
| 3788 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3789 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3790 | goto fail; |
| 3791 | } |
| 3792 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3793 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3794 | /* success */ |
| 3795 | ret = 1; |
| 3796 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3797 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3798 | /* 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] | 3799 | * move the remaining data over it. |
| 3800 | */ |
| 3801 | if (hole) { |
| 3802 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3803 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3804 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3805 | b_sub(&h2c->dbuf, hole); |
| 3806 | } |
| 3807 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3808 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3809 | /* too large frames */ |
| 3810 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3811 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3812 | } |
| 3813 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3814 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3815 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3816 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3817 | return ret; |
| 3818 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3819 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3820 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3821 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3822 | |
| 3823 | trailers: |
| 3824 | /* This is the last HEADERS frame hence a trailer */ |
| 3825 | |
| 3826 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3827 | /* It's a trailer but it's missing ES flag */ |
| 3828 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3829 | goto fail; |
| 3830 | } |
| 3831 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 3832 | /* Trailers terminate a DATA sequence. In HTX we always handle them. In |
| 3833 | * legacy, when using chunks, we have to emit the 0 CRLF marker first |
| 3834 | * and then handle the trailers. For other modes, the trailers are |
| 3835 | * silently dropped. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3836 | */ |
| 3837 | if (htx) { |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3838 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3839 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3840 | } |
| 3841 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3842 | /* Legacy mode with chunked encoding : we must finalize the |
| 3843 | * data block message emit the trailing CRLF */ |
| 3844 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3845 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3846 | |
| 3847 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3848 | if (outlen > 0) |
| 3849 | b_add(rxbuf, outlen); |
| 3850 | else |
| 3851 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3855 | } |
| 3856 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3857 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3858 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3859 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3860 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3861 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3862 | * 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] | 3863 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3864 | * send the current frame, 0 if it couldn't complete, in which case |
| 3865 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3866 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3867 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3868 | * have checked the frame header and ensured that the frame was complete or the |
| 3869 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3870 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3871 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3872 | { |
| 3873 | struct h2c *h2c = h2s->h2c; |
| 3874 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3875 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3876 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3877 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3878 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3879 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3880 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3881 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3882 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3883 | if (!csbuf) { |
| 3884 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3885 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3886 | } |
| 3887 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3888 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3889 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3890 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3891 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3892 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3893 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3894 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3895 | if (flen > b_data(&h2c->dbuf)) { |
| 3896 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3897 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3898 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3901 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3902 | unsigned int sent; |
| 3903 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3904 | block1 = htx_free_data_space(htx); |
| 3905 | if (!block1) { |
| 3906 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3907 | goto fail; |
| 3908 | } |
| 3909 | if (flen > block1) |
| 3910 | flen = block1; |
| 3911 | |
| 3912 | /* here, flen is the max we can copy into the output buffer */ |
| 3913 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3914 | if (flen > block1) |
| 3915 | flen = block1; |
| 3916 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3917 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3918 | |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3919 | b_del(&h2c->dbuf, sent); |
| 3920 | h2c->dfl -= sent; |
| 3921 | h2c->rcvd_c += sent; |
| 3922 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3923 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3924 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3925 | h2s->body_len -= sent; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3926 | htx->extra = h2s->body_len; |
| 3927 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3928 | |
| 3929 | if (sent < flen) { |
| 3930 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3931 | goto fail; |
| 3932 | } |
| 3933 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3934 | goto try_again; |
| 3935 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3936 | else if (unlikely(b_space_wraps(csbuf) && |
| 3937 | flen + chklen <= b_room(csbuf) && |
| 3938 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3939 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3940 | * not too many data in the buffer, let's defragment it and try |
| 3941 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3942 | */ |
| 3943 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3944 | } |
| 3945 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3946 | /* chunked-encoding requires more room */ |
| 3947 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3948 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3949 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3950 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3951 | (chklen < 1048576) ? 4 : 8; |
| 3952 | chklen += 4; // CRLF, CRLF |
| 3953 | } |
| 3954 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3955 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3956 | if (flen + chklen > b_room(csbuf)) { |
| 3957 | if (chklen >= b_room(csbuf)) { |
| 3958 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3959 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3960 | } |
| 3961 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3965 | /* emit the chunk size */ |
| 3966 | unsigned int chksz = flen; |
| 3967 | char str[10]; |
| 3968 | char *beg; |
| 3969 | |
| 3970 | beg = str + sizeof(str); |
| 3971 | *--beg = '\n'; |
| 3972 | *--beg = '\r'; |
| 3973 | do { |
| 3974 | *--beg = hextab[chksz & 0xF]; |
| 3975 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3976 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3977 | } |
| 3978 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3979 | /* Block1 is the length of the first block before the buffer wraps, |
| 3980 | * block2 is the optional second block to reach the end of the frame. |
| 3981 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3982 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3983 | if (block1 > flen) |
| 3984 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3985 | block2 = flen - block1; |
| 3986 | |
| 3987 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3988 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3989 | |
| 3990 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3991 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3992 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3993 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3994 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3995 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3996 | } |
| 3997 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3998 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3999 | * by the caller when seeing FRAME_A after sending the window update). |
| 4000 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4001 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4002 | h2c->dfl -= flen; |
| 4003 | h2c->rcvd_c += flen; |
| 4004 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 4005 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 4006 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 4007 | h2s->body_len -= flen; |
| 4008 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4009 | if (h2c->dfl > h2c->dpl) { |
| 4010 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4011 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4012 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 4015 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4016 | /* here we're done with the frame, all the payload (except padding) was |
| 4017 | * transferred. |
| 4018 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4019 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4020 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 4021 | if (htx) { |
| 4022 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 4023 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4024 | goto fail; |
| 4025 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4026 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4027 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4028 | /* emit the trailing 0 CRLF CRLF */ |
| 4029 | if (b_room(csbuf) < 5) { |
| 4030 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4031 | goto fail; |
| 4032 | } |
| 4033 | chklen += 5; |
| 4034 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 4035 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4036 | } |
| 4037 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 4038 | h2c->rcvd_c += h2c->dpl; |
| 4039 | h2c->rcvd_s += h2c->dpl; |
| 4040 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4041 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4042 | if (htx) |
| 4043 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4044 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4045 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4046 | if (htx) |
| 4047 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4048 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4051 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 4052 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 4053 | * number of bytes sent. The caller must check the stream's status to detect |
| 4054 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4055 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4056 | 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] | 4057 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4058 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4059 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4060 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4061 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4062 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4063 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4064 | int es_now = 0; |
| 4065 | int ret = 0; |
| 4066 | int hdr; |
| 4067 | |
| 4068 | if (h2c_mux_busy(h2c, h2s)) { |
| 4069 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4070 | return 0; |
| 4071 | } |
| 4072 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4073 | /* First, try to parse the H1 response and index it into <list>. |
| 4074 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 4075 | * block does not wrap and we can safely read it this way without |
| 4076 | * having to realign the buffer. |
| 4077 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4078 | 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] | 4079 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4080 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 4081 | /* incomplete or invalid response, this is abnormal coming from |
| 4082 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 4083 | * so that won't be fixed, raise an error now. |
| 4084 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4085 | * FIXME: we should instead add the ability to only return a |
| 4086 | * 502 bad gateway. But in theory this is not supposed to |
| 4087 | * happen. |
| 4088 | */ |
| 4089 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4090 | ret = 0; |
| 4091 | goto end; |
| 4092 | } |
| 4093 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4094 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4095 | |
| 4096 | /* certain statuses have no body or an empty one, regardless of |
| 4097 | * what the headers say. |
| 4098 | */ |
| 4099 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4100 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4101 | h1m->curr_len = h1m->body_len = 0; |
| 4102 | } |
| 4103 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4104 | /* no contents, claim c-len is present and set to zero */ |
| 4105 | h1m->flags &= ~H1_MF_CHNK; |
| 4106 | h1m->flags |= H1_MF_CLEN; |
| 4107 | h1m->curr_len = h1m->body_len = 0; |
| 4108 | } |
| 4109 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4110 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4111 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4112 | if (!h2_get_buf(h2c, mbuf)) { |
| 4113 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4114 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4115 | return 0; |
| 4116 | } |
| 4117 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4118 | chunk_reset(&outbuf); |
| 4119 | |
| 4120 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4121 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4122 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4123 | break; |
| 4124 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4125 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4128 | if (outbuf.size < 9) |
| 4129 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4130 | |
| 4131 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4132 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4133 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4134 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4135 | |
| 4136 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4137 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4138 | /* this is an unparsable response */ |
| 4139 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4140 | ret = 0; |
| 4141 | goto end; |
| 4142 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4143 | |
| 4144 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4145 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4146 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4147 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | /* encode all headers, stop at empty name */ |
| 4151 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4152 | /* these ones do not exist in H2 and must be dropped. */ |
| 4153 | if (isteq(list[hdr].n, ist("connection")) || |
| 4154 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4155 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4156 | isteq(list[hdr].n, ist("upgrade")) || |
| 4157 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4158 | continue; |
| 4159 | |
| 4160 | if (isteq(list[hdr].n, ist(""))) |
| 4161 | break; // end |
| 4162 | |
| 4163 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4164 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4165 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4166 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4167 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4172 | 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] | 4173 | es_now = 1; |
| 4174 | |
| 4175 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4176 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4177 | |
| 4178 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4179 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4180 | |
| 4181 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4182 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4183 | |
| 4184 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4185 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4186 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4187 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4188 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4189 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4190 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4191 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4192 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4193 | h2s->flags |= H2_SF_ES_SENT; |
| 4194 | if (h2s->st == H2_SS_OPEN) |
| 4195 | h2s->st = H2_SS_HLOC; |
| 4196 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4197 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4198 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4199 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4200 | /* 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] | 4201 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4202 | 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] | 4203 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4204 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4205 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4206 | |
| 4207 | /* 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] | 4208 | |
| 4209 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4210 | //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] | 4211 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4212 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4213 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4214 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4215 | h1m_init_res(h1m); |
| 4216 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4217 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4218 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4219 | ret = 0; |
| 4220 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4221 | } |
| 4222 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4223 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4224 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4225 | * the number of bytes sent. The caller must check the stream's status to |
| 4226 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4227 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4228 | 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] | 4229 | { |
| 4230 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4231 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4232 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4233 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4234 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4235 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4236 | int es_now = 0; |
| 4237 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4238 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4239 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4240 | |
| 4241 | if (h2c_mux_busy(h2c, h2s)) { |
| 4242 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4243 | goto end; |
| 4244 | } |
| 4245 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4246 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4247 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4248 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4249 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4250 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4251 | goto end; |
| 4252 | } |
| 4253 | |
| 4254 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4255 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4256 | goto end; |
| 4257 | |
| 4258 | chunk_reset(&outbuf); |
| 4259 | |
| 4260 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4261 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4262 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4263 | break; |
| 4264 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4265 | /* If there are pending data in the output buffer, and we have |
| 4266 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4267 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4268 | * is full and wait, to save some slow realign calls. |
| 4269 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4270 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4271 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4272 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4273 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4274 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4275 | goto end; |
| 4276 | } |
| 4277 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4278 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4282 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4283 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4284 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4285 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4286 | goto end; |
| 4287 | } |
| 4288 | |
| 4289 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4290 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4291 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4292 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4293 | |
| 4294 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4295 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4296 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4297 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4298 | break; |
| 4299 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4300 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4301 | if ((long long)size > h1m->curr_len) |
| 4302 | size = h1m->curr_len; |
| 4303 | break; |
| 4304 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4305 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4306 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4307 | if (!ret) |
| 4308 | goto end; |
| 4309 | |
| 4310 | if (ret < 0) { |
| 4311 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4312 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4313 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4314 | goto end; |
| 4315 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4316 | max -= ret; |
| 4317 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4318 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4319 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4320 | } |
| 4321 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4322 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4323 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4324 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4325 | if (!ret) |
| 4326 | goto end; |
| 4327 | |
| 4328 | if (ret < 0) { |
| 4329 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4330 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4331 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4332 | goto end; |
| 4333 | } |
| 4334 | |
| 4335 | size = chunk; |
| 4336 | h1m->curr_len = chunk; |
| 4337 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4338 | max -= ret; |
| 4339 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4340 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4341 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4342 | if (!size) |
| 4343 | goto send_empty; |
| 4344 | } |
| 4345 | |
| 4346 | /* in MSG_DATA state, continue below */ |
| 4347 | size = h1m->curr_len; |
| 4348 | break; |
| 4349 | } |
| 4350 | |
| 4351 | /* we have in <size> the exact number of bytes we need to copy from |
| 4352 | * the H1 buffer. We need to check this against the connection's and |
| 4353 | * the stream's send windows, and to ensure that this fits in the max |
| 4354 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4355 | * the frame header). The connection's flow control is applied last so |
| 4356 | * that we can use a separate list of streams which are immediately |
| 4357 | * unblocked on window opening. Note: we don't implement padding. |
| 4358 | */ |
| 4359 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4360 | if (size > max) |
| 4361 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4362 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4363 | if (size > h2s_mws(h2s)) |
| 4364 | size = h2s_mws(h2s); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4365 | |
| 4366 | if (size <= 0) { |
| 4367 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4368 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4369 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4370 | goto end; |
| 4371 | } |
| 4372 | |
| 4373 | if (h2c->mfs && size > h2c->mfs) |
| 4374 | size = h2c->mfs; |
| 4375 | |
| 4376 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4377 | /* It doesn't fit at once. If it at least fits once split and |
| 4378 | * the amount of data to move is low, let's defragment the |
| 4379 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4380 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4381 | if (b_space_wraps(mbuf) && |
| 4382 | (size + 9 <= b_room(mbuf)) && |
| 4383 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4384 | goto realign_again; |
| 4385 | size = outbuf.size - 9; |
| 4386 | } |
| 4387 | |
| 4388 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4389 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4390 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4391 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4392 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4393 | goto end; |
| 4394 | } |
| 4395 | |
| 4396 | if (size > h2c->mws) |
| 4397 | size = h2c->mws; |
| 4398 | |
| 4399 | if (size <= 0) { |
| 4400 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4401 | goto end; |
| 4402 | } |
| 4403 | |
| 4404 | /* copy whatever we can */ |
| 4405 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4406 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4407 | if (ret == 1) |
| 4408 | len2 = 0; |
| 4409 | |
| 4410 | if (!ret || len1 + len2 < size) { |
| 4411 | /* FIXME: must normally never happen */ |
| 4412 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4413 | goto end; |
| 4414 | } |
| 4415 | |
| 4416 | /* limit len1/len2 to size */ |
| 4417 | if (len1 + len2 > size) { |
| 4418 | int sub = len1 + len2 - size; |
| 4419 | |
| 4420 | if (len2 > sub) |
| 4421 | len2 -= sub; |
| 4422 | else { |
| 4423 | sub -= len2; |
| 4424 | len2 = 0; |
| 4425 | len1 -= sub; |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4430 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4431 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4432 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4433 | |
| 4434 | send_empty: |
| 4435 | /* we may need to add END_STREAM */ |
| 4436 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4437 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4438 | * |
| 4439 | * FIXME: what we do here is not correct because we send end_stream |
| 4440 | * before knowing if we'll have to send a HEADERS frame for the |
| 4441 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4442 | * after the end of trailers, so it will be left to the caller to |
| 4443 | * eat it. The right way to do it would be to measure trailers here |
| 4444 | * and to send ES only if there are no trailers. |
| 4445 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4446 | */ |
| 4447 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4448 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4449 | es_now = 1; |
| 4450 | |
| 4451 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4452 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4453 | |
| 4454 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4455 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4456 | |
| 4457 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4458 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4459 | |
| 4460 | /* consume incoming H1 response */ |
| 4461 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4462 | max -= size; |
| 4463 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4464 | total += size; |
| 4465 | h1m->curr_len -= size; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4466 | h2s->sws -= size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4467 | h2c->mws -= size; |
| 4468 | |
| 4469 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4470 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4471 | goto new_frame; |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | if (es_now) { |
| 4476 | if (h2s->st == H2_SS_OPEN) |
| 4477 | h2s->st = H2_SS_HLOC; |
| 4478 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4479 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4480 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4481 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4482 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4483 | total += max; |
| 4484 | ofs += max; |
| 4485 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4486 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4487 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4488 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4489 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4490 | h2s->flags |= H2_SF_ES_SENT; |
| 4491 | } |
| 4492 | |
| 4493 | end: |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4494 | 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] | 4495 | return total; |
| 4496 | } |
| 4497 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4498 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4499 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4500 | * must check the stream's status to detect any error which might have happened |
| 4501 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4502 | * from the message. The htx message is assumed to be valid since produced from |
| 4503 | * the internal code, hence it contains a start line, an optional series of |
| 4504 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4505 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4506 | */ |
| 4507 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4508 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4509 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4510 | struct h2c *h2c = h2s->h2c; |
| 4511 | struct htx_blk *blk; |
| 4512 | struct htx_blk *blk_end; |
| 4513 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4514 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4515 | struct htx_sl *sl; |
| 4516 | enum htx_blk_type type; |
| 4517 | int es_now = 0; |
| 4518 | int ret = 0; |
| 4519 | int hdr; |
| 4520 | int idx; |
| 4521 | |
| 4522 | if (h2c_mux_busy(h2c, h2s)) { |
| 4523 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4524 | return 0; |
| 4525 | } |
| 4526 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4527 | /* determine the first block which must not be deleted, blk_end may |
| 4528 | * be NULL if all blocks have to be deleted. |
| 4529 | */ |
| 4530 | idx = htx_get_head(htx); |
| 4531 | blk_end = NULL; |
| 4532 | while (idx != -1) { |
| 4533 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4534 | idx = htx_get_next(htx, idx); |
| 4535 | if (type == HTX_BLK_EOH) { |
| 4536 | if (idx != -1) |
| 4537 | blk_end = htx_get_blk(htx, idx); |
| 4538 | break; |
| 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4543 | blk = htx_get_head_blk(htx); |
| 4544 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
| 4545 | ALREADY_CHECKED(blk); |
| 4546 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4547 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4548 | if (h2s->status < 100 || h2s->status > 999) |
| 4549 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4550 | |
| 4551 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4552 | hdr = 0; |
| 4553 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4554 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4555 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4556 | blk = htx_get_blk(htx, idx); |
| 4557 | type = htx_get_blk_type(blk); |
| 4558 | |
| 4559 | if (type == HTX_BLK_UNUSED) |
| 4560 | continue; |
| 4561 | |
| 4562 | if (type != HTX_BLK_HDR) |
| 4563 | break; |
| 4564 | |
| 4565 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4566 | goto fail; |
| 4567 | |
| 4568 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4569 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4570 | hdr++; |
| 4571 | } |
| 4572 | |
| 4573 | /* marker for end of headers */ |
| 4574 | list[hdr].n = ist(""); |
| 4575 | |
| 4576 | if (h2s->status == 204 || h2s->status == 304) { |
| 4577 | /* no contents, claim c-len is present and set to zero */ |
| 4578 | es_now = 1; |
| 4579 | } |
| 4580 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4581 | mbuf = br_tail(h2c->mbuf); |
| 4582 | retry: |
| 4583 | if (!h2_get_buf(h2c, mbuf)) { |
| 4584 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4585 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4586 | return 0; |
| 4587 | } |
| 4588 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4589 | chunk_reset(&outbuf); |
| 4590 | |
| 4591 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4592 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4593 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4594 | break; |
| 4595 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4596 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | if (outbuf.size < 9) |
| 4600 | goto full; |
| 4601 | |
| 4602 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4603 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4604 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4605 | outbuf.data = 9; |
| 4606 | |
| 4607 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4608 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4609 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4610 | goto realign_again; |
| 4611 | goto full; |
| 4612 | } |
| 4613 | |
| 4614 | /* encode all headers, stop at empty name */ |
| 4615 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4616 | /* these ones do not exist in H2 and must be dropped. */ |
| 4617 | if (isteq(list[hdr].n, ist("connection")) || |
| 4618 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4619 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4620 | isteq(list[hdr].n, ist("upgrade")) || |
| 4621 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4622 | continue; |
| 4623 | |
| 4624 | if (isteq(list[hdr].n, ist(""))) |
| 4625 | break; // end |
| 4626 | |
| 4627 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4628 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4629 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4630 | goto realign_again; |
| 4631 | goto full; |
| 4632 | } |
| 4633 | } |
| 4634 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4635 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4636 | * FIXME: we should also set it when we know for sure that the |
| 4637 | * content-length is zero as well as on 204/304 |
| 4638 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4639 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4640 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4641 | es_now = 1; |
| 4642 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4643 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4644 | es_now = 1; |
| 4645 | |
| 4646 | /* update the frame's size */ |
| 4647 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4648 | |
| 4649 | if (es_now) |
| 4650 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4651 | |
| 4652 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4653 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4654 | |
| 4655 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4656 | * 1xx responses, another HEADERS frame is expected. |
| 4657 | */ |
| 4658 | if (h2s->status >= 200 || h2s->status == 101) |
| 4659 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4660 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4661 | if (es_now) { |
| 4662 | h2s->flags |= H2_SF_ES_SENT; |
| 4663 | if (h2s->st == H2_SS_OPEN) |
| 4664 | h2s->st = H2_SS_HLOC; |
| 4665 | else |
| 4666 | h2s_close(h2s); |
| 4667 | } |
| 4668 | |
| 4669 | /* OK we could properly deliver the response */ |
| 4670 | |
| 4671 | /* remove all header blocks including the EOH and compute the |
| 4672 | * corresponding size. |
| 4673 | * |
| 4674 | * FIXME: We should remove everything when es_now is set. |
| 4675 | */ |
| 4676 | ret = 0; |
| 4677 | idx = htx_get_head(htx); |
| 4678 | blk = htx_get_blk(htx, idx); |
| 4679 | while (blk != blk_end) { |
| 4680 | ret += htx_get_blksz(blk); |
| 4681 | blk = htx_remove_blk(htx, blk); |
| 4682 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4683 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4684 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4685 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4686 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4687 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4688 | end: |
| 4689 | return ret; |
| 4690 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4691 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4692 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4693 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4694 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4695 | ret = 0; |
| 4696 | goto end; |
| 4697 | fail: |
| 4698 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4699 | * list etc go here (unrecoverable errors). |
| 4700 | */ |
| 4701 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4702 | ret = 0; |
| 4703 | goto end; |
| 4704 | } |
| 4705 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4706 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4707 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4708 | * must check the stream's status to detect any error which might have happened |
| 4709 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4710 | * from the message. The htx message is assumed to be valid since produced from |
| 4711 | * the internal code, hence it contains a start line, an optional series of |
| 4712 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4713 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4714 | */ |
| 4715 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4716 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4717 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4718 | struct h2c *h2c = h2s->h2c; |
| 4719 | struct htx_blk *blk; |
| 4720 | struct htx_blk *blk_end; |
| 4721 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4722 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4723 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4724 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4725 | enum htx_blk_type type; |
| 4726 | int es_now = 0; |
| 4727 | int ret = 0; |
| 4728 | int hdr; |
| 4729 | int idx; |
| 4730 | |
| 4731 | if (h2c_mux_busy(h2c, h2s)) { |
| 4732 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4733 | return 0; |
| 4734 | } |
| 4735 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4736 | /* determine the first block which must not be deleted, blk_end may |
| 4737 | * be NULL if all blocks have to be deleted. |
| 4738 | */ |
| 4739 | idx = htx_get_head(htx); |
| 4740 | blk_end = NULL; |
| 4741 | while (idx != -1) { |
| 4742 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4743 | idx = htx_get_next(htx, idx); |
| 4744 | if (type == HTX_BLK_EOH) { |
| 4745 | if (idx != -1) |
| 4746 | blk_end = htx_get_blk(htx, idx); |
| 4747 | break; |
| 4748 | } |
| 4749 | } |
| 4750 | |
| 4751 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4752 | blk = htx_get_head_blk(htx); |
| 4753 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 4754 | ALREADY_CHECKED(blk); |
| 4755 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4756 | meth = htx_sl_req_meth(sl); |
| 4757 | path = htx_sl_req_uri(sl); |
| 4758 | |
| 4759 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4760 | hdr = 0; |
| 4761 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4762 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4763 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4764 | blk = htx_get_blk(htx, idx); |
| 4765 | type = htx_get_blk_type(blk); |
| 4766 | |
| 4767 | if (type == HTX_BLK_UNUSED) |
| 4768 | continue; |
| 4769 | |
| 4770 | if (type != HTX_BLK_HDR) |
| 4771 | break; |
| 4772 | |
| 4773 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4774 | goto fail; |
| 4775 | |
| 4776 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4777 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4778 | hdr++; |
| 4779 | } |
| 4780 | |
| 4781 | /* marker for end of headers */ |
| 4782 | list[hdr].n = ist(""); |
| 4783 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4784 | mbuf = br_tail(h2c->mbuf); |
| 4785 | retry: |
| 4786 | if (!h2_get_buf(h2c, mbuf)) { |
| 4787 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4788 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4789 | return 0; |
| 4790 | } |
| 4791 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4792 | chunk_reset(&outbuf); |
| 4793 | |
| 4794 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4795 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4796 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4797 | break; |
| 4798 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4799 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | if (outbuf.size < 9) |
| 4803 | goto full; |
| 4804 | |
| 4805 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4806 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4807 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4808 | outbuf.data = 9; |
| 4809 | |
| 4810 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4811 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4812 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4813 | goto realign_again; |
| 4814 | goto full; |
| 4815 | } |
| 4816 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4817 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4818 | * - :authority set to the URI part (host:port) |
| 4819 | * - :method set to CONNECT |
| 4820 | * - :scheme and :path omitted |
| 4821 | */ |
| 4822 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4823 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4824 | struct ist scheme; |
| 4825 | |
| 4826 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4827 | scheme = ist("http"); |
| 4828 | else |
| 4829 | scheme = ist("https"); |
| 4830 | |
| 4831 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4832 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4833 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4834 | goto realign_again; |
| 4835 | goto full; |
| 4836 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4837 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4838 | /* encode the path, which necessarily is the second one */ |
| 4839 | if (!hpack_encode_path(&outbuf, path)) { |
| 4840 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4841 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4842 | goto realign_again; |
| 4843 | goto full; |
| 4844 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4845 | |
| 4846 | /* look for the Host header and place it in :authority */ |
| 4847 | auth = ist2(NULL, 0); |
| 4848 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4849 | if (isteq(list[hdr].n, ist(""))) |
| 4850 | break; // end |
| 4851 | |
| 4852 | if (isteq(list[hdr].n, ist("host"))) { |
| 4853 | auth = list[hdr].v; |
| 4854 | break; |
| 4855 | } |
| 4856 | } |
| 4857 | } |
| 4858 | else { |
| 4859 | /* for CONNECT, :authority is taken from the path */ |
| 4860 | auth = path; |
| 4861 | } |
| 4862 | |
| 4863 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4864 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4865 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4866 | goto realign_again; |
| 4867 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | /* encode all headers, stop at empty name */ |
| 4871 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4872 | /* these ones do not exist in H2 and must be dropped. */ |
| 4873 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4874 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4875 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4876 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4877 | isteq(list[hdr].n, ist("upgrade")) || |
| 4878 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4879 | continue; |
| 4880 | |
| 4881 | if (isteq(list[hdr].n, ist(""))) |
| 4882 | break; // end |
| 4883 | |
| 4884 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4885 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4886 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4887 | goto realign_again; |
| 4888 | goto full; |
| 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | /* we may need to add END_STREAM if we have no body : |
| 4893 | * - request already closed, or : |
| 4894 | * - no transfer-encoding, and : |
| 4895 | * - no content-length or content-length:0 |
| 4896 | * Fixme: this doesn't take into account CONNECT requests. |
| 4897 | */ |
| 4898 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4899 | es_now = 1; |
| 4900 | |
| 4901 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4902 | es_now = 1; |
| 4903 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4904 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4905 | es_now = 1; |
| 4906 | |
| 4907 | /* update the frame's size */ |
| 4908 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4909 | |
| 4910 | if (es_now) |
| 4911 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4912 | |
| 4913 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4914 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4915 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4916 | h2s->st = H2_SS_OPEN; |
| 4917 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4918 | if (es_now) { |
| 4919 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4920 | h2s->flags |= H2_SF_ES_SENT; |
| 4921 | h2s->st = H2_SS_HLOC; |
| 4922 | } |
| 4923 | |
| 4924 | /* remove all header blocks including the EOH and compute the |
| 4925 | * corresponding size. |
| 4926 | * |
| 4927 | * FIXME: We should remove everything when es_now is set. |
| 4928 | */ |
| 4929 | ret = 0; |
| 4930 | idx = htx_get_head(htx); |
| 4931 | blk = htx_get_blk(htx, idx); |
| 4932 | while (blk != blk_end) { |
| 4933 | ret += htx_get_blksz(blk); |
| 4934 | blk = htx_remove_blk(htx, blk); |
| 4935 | } |
| 4936 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4937 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4938 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4939 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4940 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4941 | |
| 4942 | end: |
| 4943 | return ret; |
| 4944 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4945 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4946 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4947 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4948 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4949 | ret = 0; |
| 4950 | goto end; |
| 4951 | fail: |
| 4952 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4953 | * list etc go here (unrecoverable errors). |
| 4954 | */ |
| 4955 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4956 | ret = 0; |
| 4957 | goto end; |
| 4958 | } |
| 4959 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4960 | /* 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] | 4961 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4962 | * caller must check the stream's status to detect any error which might have |
| 4963 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4964 | * 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] | 4965 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4966 | 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] | 4967 | { |
| 4968 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4969 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4970 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4971 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4972 | size_t total = 0; |
| 4973 | int es_now = 0; |
| 4974 | int bsize; /* htx block size */ |
| 4975 | int fsize; /* h2 frame size */ |
| 4976 | struct htx_blk *blk; |
| 4977 | enum htx_blk_type type; |
| 4978 | int idx; |
| 4979 | |
| 4980 | if (h2c_mux_busy(h2c, h2s)) { |
| 4981 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4982 | goto end; |
| 4983 | } |
| 4984 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4985 | htx = htx_from_buf(buf); |
| 4986 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4987 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 4988 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 4989 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4990 | */ |
| 4991 | |
| 4992 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4993 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4994 | goto end; |
| 4995 | |
| 4996 | idx = htx_get_head(htx); |
| 4997 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4998 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4999 | bsize = htx_get_blksz(blk); |
| 5000 | fsize = bsize; |
| 5001 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5002 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 5003 | if (h2s->flags & H2_SF_ES_SENT) { |
| 5004 | /* ES already sent */ |
| 5005 | htx_remove_blk(htx, blk); |
| 5006 | total++; // EOM counts as one byte |
| 5007 | count--; |
| 5008 | goto end; |
| 5009 | } |
| 5010 | } |
| 5011 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5012 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5013 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5014 | mbuf = br_tail(h2c->mbuf); |
| 5015 | retry: |
| 5016 | if (!h2_get_buf(h2c, mbuf)) { |
| 5017 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5018 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5019 | goto end; |
| 5020 | } |
| 5021 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5022 | /* Perform some optimizations to reduce the number of buffer copies. |
| 5023 | * First, if the mux's buffer is empty and the htx area contains |
| 5024 | * exactly one data block of the same size as the requested count, and |
| 5025 | * this count fits within the frame size, the stream's window size, and |
| 5026 | * the connection's window size, then it's possible to simply swap the |
| 5027 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 5028 | * length to match the entire DATA HTX block in the middle. In this |
| 5029 | * case we perform a true zero-copy operation from end-to-end. This is |
| 5030 | * the situation that happens all the time with large files. Second, if |
| 5031 | * this is not possible, but the mux's output buffer is empty, we still |
| 5032 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 5033 | * making the intermediary buffer's area point to the output buffer's |
| 5034 | * area. In this case we want to skip the HTX header to make sure that |
| 5035 | * copies remain aligned and that this operation remains possible all |
| 5036 | * the time. This goes for headers, data blocks and any data extracted |
| 5037 | * from the HTX blocks. |
| 5038 | */ |
| 5039 | if (unlikely(fsize == count && |
| 5040 | htx->used == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5041 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5042 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5043 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5044 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5045 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 5046 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 5047 | * frame into a buffer containing few data if it needs to be realigned, |
| 5048 | * and that it's also OK to copy few data without realigning. Otherwise |
| 5049 | * 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] | 5050 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5051 | if (fsize + 9 <= b_room(mbuf) && |
| 5052 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 5053 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5054 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5055 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5056 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5057 | goto retry; |
| 5058 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5059 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5060 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5061 | goto end; |
| 5062 | } |
| 5063 | |
| 5064 | /* map an H2 frame to the HTX block so that we can put the |
| 5065 | * frame header there. |
| 5066 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5067 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 5068 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5069 | |
| 5070 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 5071 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5072 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5073 | h2_set_frame_size(outbuf.area, fsize); |
| 5074 | |
| 5075 | /* update windows */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5076 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5077 | h2c->mws -= fsize; |
| 5078 | |
| 5079 | /* and exchange with our old area */ |
| 5080 | buf->area = old_area; |
| 5081 | buf->data = buf->head = 0; |
| 5082 | total += fsize; |
| 5083 | goto end; |
| 5084 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5085 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5086 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5087 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 5088 | |
| 5089 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5090 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5091 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5092 | break; |
| 5093 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5094 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5098 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5099 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5100 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5101 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5102 | goto end; |
| 5103 | } |
| 5104 | |
| 5105 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5106 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5107 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5108 | outbuf.data = 9; |
| 5109 | |
| 5110 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5111 | * the HTX buffer. We need to check this against the connection's and |
| 5112 | * the stream's send windows, and to ensure that this fits in the max |
| 5113 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5114 | * the frame header). The connection's flow control is applied last so |
| 5115 | * that we can use a separate list of streams which are immediately |
| 5116 | * unblocked on window opening. Note: we don't implement padding. |
| 5117 | */ |
| 5118 | |
| 5119 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5120 | * empty frame, thus we force it to zero here. |
| 5121 | */ |
| 5122 | if (type == HTX_BLK_EOM) |
| 5123 | bsize = fsize = 0; |
| 5124 | |
| 5125 | if (!fsize) |
| 5126 | goto send_empty; |
| 5127 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5128 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5129 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5130 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5131 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5132 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5133 | goto end; |
| 5134 | } |
| 5135 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5136 | if (fsize > count) |
| 5137 | fsize = count; |
| 5138 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5139 | if (fsize > h2s_mws(h2s)) |
| 5140 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5141 | |
| 5142 | if (h2c->mfs && fsize > h2c->mfs) |
| 5143 | fsize = h2c->mfs; // >0 |
| 5144 | |
| 5145 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5146 | /* It doesn't fit at once. If it at least fits once split and |
| 5147 | * the amount of data to move is low, let's defragment the |
| 5148 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5149 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5150 | if (b_space_wraps(mbuf) && |
| 5151 | (fsize + 9 <= b_room(mbuf)) && |
| 5152 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5153 | goto realign_again; |
| 5154 | fsize = outbuf.size - 9; |
| 5155 | |
| 5156 | if (fsize <= 0) { |
| 5157 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5158 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5159 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5160 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5161 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5162 | goto end; |
| 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | if (h2c->mws <= 0) { |
| 5167 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5168 | goto end; |
| 5169 | } |
| 5170 | |
| 5171 | if (fsize > h2c->mws) |
| 5172 | fsize = h2c->mws; |
| 5173 | |
| 5174 | /* now let's copy this this into the output buffer */ |
| 5175 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5176 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5177 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5178 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5179 | |
| 5180 | send_empty: |
| 5181 | /* update the frame's size */ |
| 5182 | h2_set_frame_size(outbuf.area, fsize); |
| 5183 | |
| 5184 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5185 | * meeting EOM. We should optimize this later. |
| 5186 | */ |
| 5187 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5188 | total++; // EOM counts as one byte |
| 5189 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5190 | es_now = 1; |
| 5191 | } |
| 5192 | |
| 5193 | if (es_now) |
| 5194 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5195 | |
| 5196 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5197 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5198 | |
| 5199 | /* consume incoming HTX block, including EOM */ |
| 5200 | total += fsize; |
| 5201 | if (fsize == bsize) { |
| 5202 | htx_remove_blk(htx, blk); |
| 5203 | if (fsize) |
| 5204 | goto new_frame; |
| 5205 | } else { |
| 5206 | /* we've truncated this block */ |
| 5207 | htx_cut_data_blk(htx, blk, fsize); |
| 5208 | } |
| 5209 | |
| 5210 | if (es_now) { |
| 5211 | if (h2s->st == H2_SS_OPEN) |
| 5212 | h2s->st = H2_SS_HLOC; |
| 5213 | else |
| 5214 | h2s_close(h2s); |
| 5215 | |
| 5216 | h2s->flags |= H2_SF_ES_SENT; |
| 5217 | } |
| 5218 | |
| 5219 | end: |
| 5220 | return total; |
| 5221 | } |
| 5222 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5223 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5224 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5225 | * processed. The caller must check the stream's status to detect any error |
| 5226 | * which might have happened subsequently to a successful send. The htx blocks |
| 5227 | * are automatically removed from the message. The htx message is assumed to be |
| 5228 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5229 | * 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] | 5230 | * as a single frame. The ES flag is always set. |
| 5231 | */ |
| 5232 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5233 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 5234 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5235 | struct h2c *h2c = h2s->h2c; |
| 5236 | struct htx_blk *blk; |
| 5237 | struct htx_blk *blk_end; |
| 5238 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5239 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5240 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5241 | int ret = 0; |
| 5242 | int hdr; |
| 5243 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5244 | |
| 5245 | if (h2c_mux_busy(h2c, h2s)) { |
| 5246 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5247 | goto end; |
| 5248 | } |
| 5249 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5250 | /* determine the first block which must not be deleted, blk_end may |
| 5251 | * be NULL if all blocks have to be deleted. also get trailers. |
| 5252 | */ |
| 5253 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5254 | blk_end = NULL; |
| 5255 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5256 | hdr = 0; |
| 5257 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5258 | blk = htx_get_blk(htx, idx); |
| 5259 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5260 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5261 | if (type == HTX_BLK_UNUSED) |
| 5262 | continue; |
| 5263 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5264 | if (type == HTX_BLK_EOT) { |
| 5265 | if (idx != -1) |
| 5266 | blk_end = blk; |
| 5267 | break; |
| 5268 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5269 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5270 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5271 | |
| 5272 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5273 | goto fail; |
| 5274 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5275 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 5276 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 5277 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5278 | } |
| 5279 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5280 | /* marker for end of trailers */ |
| 5281 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5282 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5283 | mbuf = br_tail(h2c->mbuf); |
| 5284 | retry: |
| 5285 | if (!h2_get_buf(h2c, mbuf)) { |
| 5286 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5287 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5288 | goto end; |
| 5289 | } |
| 5290 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5291 | chunk_reset(&outbuf); |
| 5292 | |
| 5293 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5294 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5295 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5296 | break; |
| 5297 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5298 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | if (outbuf.size < 9) |
| 5302 | goto full; |
| 5303 | |
| 5304 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5305 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5306 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5307 | outbuf.data = 9; |
| 5308 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5309 | /* encode all headers */ |
| 5310 | for (idx = 0; idx < hdr; idx++) { |
| 5311 | /* these ones do not exist in H2 or must not appear in |
| 5312 | * trailers and must be dropped. |
| 5313 | */ |
| 5314 | if (isteq(list[idx].n, ist("host")) || |
| 5315 | isteq(list[idx].n, ist("content-length")) || |
| 5316 | isteq(list[idx].n, ist("connection")) || |
| 5317 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5318 | isteq(list[idx].n, ist("keep-alive")) || |
| 5319 | isteq(list[idx].n, ist("upgrade")) || |
| 5320 | isteq(list[idx].n, ist("te")) || |
| 5321 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5322 | continue; |
| 5323 | |
| 5324 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5325 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5326 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5327 | goto realign_again; |
| 5328 | goto full; |
| 5329 | } |
| 5330 | } |
| 5331 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5332 | if (outbuf.data == 9) { |
| 5333 | /* here we have a problem, we have nothing to emit (either we |
| 5334 | * received an empty trailers block followed or we removed its |
| 5335 | * contents above). Because of this we can't send a HEADERS |
| 5336 | * frame, so we have to cheat and instead send an empty DATA |
| 5337 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5338 | */ |
| 5339 | outbuf.area[3] = H2_FT_DATA; |
| 5340 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5341 | } |
| 5342 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5343 | /* update the frame's size */ |
| 5344 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5345 | |
| 5346 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5347 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5348 | h2s->flags |= H2_SF_ES_SENT; |
| 5349 | |
| 5350 | if (h2s->st == H2_SS_OPEN) |
| 5351 | h2s->st = H2_SS_HLOC; |
| 5352 | else |
| 5353 | h2s_close(h2s); |
| 5354 | |
| 5355 | /* OK we could properly deliver the response */ |
| 5356 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5357 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5358 | ret = 0; |
| 5359 | idx = htx_get_head(htx); |
| 5360 | blk = htx_get_blk(htx, idx); |
| 5361 | while (blk != blk_end) { |
| 5362 | ret += htx_get_blksz(blk); |
| 5363 | blk = htx_remove_blk(htx, blk); |
| 5364 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5365 | |
| 5366 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5367 | ret += htx_get_blksz(blk_end); |
| 5368 | htx_remove_blk(htx, blk_end); |
| 5369 | } |
| 5370 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5371 | end: |
| 5372 | return ret; |
| 5373 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5374 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5375 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5376 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5377 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5378 | ret = 0; |
| 5379 | goto end; |
| 5380 | fail: |
| 5381 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5382 | * list etc go here (unrecoverable errors). |
| 5383 | */ |
| 5384 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5385 | ret = 0; |
| 5386 | goto end; |
| 5387 | } |
| 5388 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5389 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5390 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5391 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5392 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5393 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5394 | * returns 0 except for the error above. |
| 5395 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5396 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5397 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5398 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5399 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5400 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5401 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5402 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5403 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5404 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5405 | sw->events |= SUB_RETRY_RECV; |
| 5406 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5407 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5408 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5409 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5410 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5411 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5412 | sw->events |= SUB_RETRY_SEND; |
| 5413 | h2s->send_wait = sw; |
| 5414 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5415 | !LIST_ADDED(&h2s->list)) { |
| 5416 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5417 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5418 | else |
| 5419 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5420 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5421 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5422 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5423 | if (event_type != 0) |
| 5424 | return -1; |
| 5425 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5428 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5429 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5430 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5431 | * It always returns zero. |
| 5432 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5433 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5434 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5435 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5436 | struct h2s *h2s = cs->ctx; |
| 5437 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5438 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5439 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5440 | BUG_ON(h2s->recv_wait != sw); |
| 5441 | sw->events &= ~SUB_RETRY_RECV; |
| 5442 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5443 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5444 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5445 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5446 | BUG_ON(h2s->send_wait != sw); |
| 5447 | LIST_DEL(&h2s->list); |
| 5448 | LIST_INIT(&h2s->list); |
| 5449 | sw->events &= ~SUB_RETRY_SEND; |
| 5450 | /* We were about to send, make sure it does not happen */ |
| 5451 | if (LIST_ADDED(&h2s->sending_list) && |
| 5452 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5453 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5454 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5455 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5456 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5457 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5458 | return 0; |
| 5459 | } |
| 5460 | |
| 5461 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5462 | /* Called from the upper layer, to receive data */ |
| 5463 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5464 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5465 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5466 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5467 | struct htx *h2s_htx = NULL; |
| 5468 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5469 | size_t ret = 0; |
| 5470 | |
| 5471 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5472 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5473 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5474 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5475 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5476 | * the HTX is empty. |
| 5477 | */ |
| 5478 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5479 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5480 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5481 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5482 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5483 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5484 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5485 | /* <buf> is empty and the message is small enough, swap the |
| 5486 | * buffers. */ |
| 5487 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5488 | htx_to_buf(buf_htx, buf); |
| 5489 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5490 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5491 | goto end; |
| 5492 | } |
| 5493 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5494 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5495 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5496 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5497 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5498 | if (htx_is_empty(buf_htx)) |
| 5499 | cs->flags |= CS_FL_EOI; |
| 5500 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5501 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5502 | 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] | 5503 | htx_to_buf(buf_htx, buf); |
| 5504 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5505 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5506 | } |
| 5507 | else { |
| 5508 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5509 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5510 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5511 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5512 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5513 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5514 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5515 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5516 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5517 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 5518 | if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5519 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5520 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5521 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5522 | if (b_size(&h2s->rxbuf)) { |
| 5523 | b_free(&h2s->rxbuf); |
| 5524 | offer_buffers(NULL, tasks_run_queue); |
| 5525 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5526 | } |
| 5527 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5528 | if (ret && h2c->dsi == h2s->id) { |
| 5529 | /* demux is blocking on this stream's buffer */ |
| 5530 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5531 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5532 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5533 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5534 | return ret; |
| 5535 | } |
| 5536 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5537 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5538 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5539 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5540 | static void h2_stop_senders(struct h2c *h2c) |
| 5541 | { |
| 5542 | struct h2s *h2s, *h2s_back; |
| 5543 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5544 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5545 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5546 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5547 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5548 | } |
| 5549 | } |
| 5550 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5551 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5552 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5553 | * flags may be updated on the conn_stream. |
| 5554 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5555 | 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] | 5556 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5557 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5558 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5559 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5560 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5561 | struct htx *htx; |
| 5562 | struct htx_blk *blk; |
| 5563 | enum htx_blk_type btype; |
| 5564 | uint32_t bsize; |
| 5565 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5566 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5567 | /* If we were not just woken because we wanted to send but couldn't, |
| 5568 | * and there's somebody else that is waiting to send, do nothing, |
| 5569 | * we will subscribe later and be put at the end of the list |
| 5570 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5571 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5572 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5573 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5574 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5575 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5576 | /* We couldn't set it to NULL before, because we needed it in case |
| 5577 | * we had to cancel the tasklet |
| 5578 | */ |
| 5579 | h2s->send_wait = NULL; |
| 5580 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5581 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5582 | return 0; |
| 5583 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5584 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5585 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5586 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5587 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5588 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5589 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5590 | if (h2s->id == 0) { |
| 5591 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5592 | |
| 5593 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5594 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5595 | return 0; |
| 5596 | } |
| 5597 | |
| 5598 | eb32_delete(&h2s->by_id); |
| 5599 | h2s->by_id.key = h2s->id = id; |
| 5600 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5601 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5602 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5603 | } |
| 5604 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5605 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5606 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5607 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5608 | idx = htx_get_head(htx); |
| 5609 | blk = htx_get_blk(htx, idx); |
| 5610 | btype = htx_get_blk_type(blk); |
| 5611 | bsize = htx_get_blksz(blk); |
| 5612 | |
| 5613 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5614 | case HTX_BLK_REQ_SL: |
| 5615 | /* start-line before headers */ |
| 5616 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5617 | if (ret > 0) { |
| 5618 | total += ret; |
| 5619 | count -= ret; |
| 5620 | if (ret < bsize) |
| 5621 | goto done; |
| 5622 | } |
| 5623 | break; |
| 5624 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5625 | case HTX_BLK_RES_SL: |
| 5626 | /* start-line before headers */ |
| 5627 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5628 | if (ret > 0) { |
| 5629 | total += ret; |
| 5630 | count -= ret; |
| 5631 | if (ret < bsize) |
| 5632 | goto done; |
| 5633 | } |
| 5634 | break; |
| 5635 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5636 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5637 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5638 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5639 | * This EOM necessarily is one before trailers, as the EOM following |
| 5640 | * trailers would have been consumed by the trailers parser. |
| 5641 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5642 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5643 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5644 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5645 | total += ret; |
| 5646 | count -= ret; |
| 5647 | if (ret < bsize) |
| 5648 | goto done; |
| 5649 | } |
| 5650 | break; |
| 5651 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5652 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5653 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5654 | /* This is the first trailers block, all the subsequent ones AND |
| 5655 | * the EOM will be swallowed by the parser. |
| 5656 | */ |
| 5657 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5658 | if (ret > 0) { |
| 5659 | total += ret; |
| 5660 | count -= ret; |
| 5661 | if (ret < bsize) |
| 5662 | goto done; |
| 5663 | } |
| 5664 | break; |
| 5665 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5666 | default: |
| 5667 | htx_remove_blk(htx, blk); |
| 5668 | total += bsize; |
| 5669 | count -= bsize; |
| 5670 | break; |
| 5671 | } |
| 5672 | } |
| 5673 | goto done; |
| 5674 | } |
| 5675 | |
| 5676 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5677 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5678 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5679 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5680 | ret = -1; |
| 5681 | else |
| 5682 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5683 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5684 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5685 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5686 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5687 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5688 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5689 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5690 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5691 | if (unlikely((int)ret <= 0)) { |
| 5692 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5693 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5694 | break; |
| 5695 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5696 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5697 | total += count; |
| 5698 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5699 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5700 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5701 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5702 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5703 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5704 | break; |
| 5705 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5706 | |
| 5707 | total += ret; |
| 5708 | count -= ret; |
| 5709 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5710 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5711 | break; |
| 5712 | |
| 5713 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5714 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5717 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5718 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5719 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5720 | * unprocessed trailers, abnormal extra data, ...) |
| 5721 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5722 | total += count; |
| 5723 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5724 | } |
| 5725 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5726 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5727 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5728 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5729 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5730 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5731 | } |
| 5732 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5733 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5734 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5735 | } else { |
| 5736 | b_del(buf, total); |
| 5737 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5738 | |
| 5739 | /* The mux is full, cancel the pending tasks */ |
| 5740 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5741 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5742 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5743 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5744 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5745 | * we read exactly what the caller specified, as with HTX the caller |
| 5746 | * will always give the buffer size, instead of the amount of data |
| 5747 | * available. |
| 5748 | */ |
| 5749 | if (htx && !b_data(buf)) |
| 5750 | total = orig_count; |
| 5751 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5752 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5753 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5754 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5755 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5756 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5757 | /* If we're waiting for flow control, and we got a shutr on the |
| 5758 | * connection, we will never be unlocked, so add an error on |
| 5759 | * the conn_stream. |
| 5760 | */ |
| 5761 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5762 | !b_data(&h2s->h2c->dbuf) && |
| 5763 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5764 | if (cs->flags & CS_FL_EOS) |
| 5765 | cs->flags |= CS_FL_ERROR; |
| 5766 | else |
| 5767 | cs->flags |= CS_FL_ERR_PENDING; |
| 5768 | } |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5769 | |
| 5770 | if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5771 | /* 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] | 5772 | LIST_DEL_INIT(&h2s->list); |
| 5773 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5774 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5775 | } |
| 5776 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5777 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5778 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5779 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5780 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5781 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5782 | struct eb32_node *node; |
| 5783 | int fctl_cnt = 0; |
| 5784 | int send_cnt = 0; |
| 5785 | int tree_cnt = 0; |
| 5786 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5787 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5788 | |
| 5789 | if (!h2c) |
| 5790 | return; |
| 5791 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5792 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5793 | fctl_cnt++; |
| 5794 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5795 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5796 | send_cnt++; |
| 5797 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5798 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5799 | node = eb32_first(&h2c->streams_by_id); |
| 5800 | while (node) { |
| 5801 | h2s = container_of(node, struct h2s, by_id); |
| 5802 | tree_cnt++; |
| 5803 | if (!h2s->cs) |
| 5804 | orph_cnt++; |
| 5805 | node = eb32_next(node); |
| 5806 | } |
| 5807 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5808 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5809 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5810 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5811 | " .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] | 5812 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5813 | " .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] | 5814 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5815 | 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] | 5816 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5817 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5818 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5819 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5820 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5821 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5822 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5823 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5824 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5825 | |
| 5826 | if (h2s) { |
| 5827 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5828 | h2s, h2s->id, h2s->flags, |
| 5829 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5830 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5831 | h2s->cs); |
| 5832 | if (h2s->cs) |
| 5833 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5834 | h2s->cs->flags, h2s->cs->data); |
| 5835 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5836 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5837 | |
| 5838 | /*******************************************************/ |
| 5839 | /* functions below are dedicated to the config parsers */ |
| 5840 | /*******************************************************/ |
| 5841 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5842 | /* config parser for global "tune.h2.header-table-size" */ |
| 5843 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5844 | struct proxy *defpx, const char *file, int line, |
| 5845 | char **err) |
| 5846 | { |
| 5847 | if (too_many_args(1, args, err, NULL)) |
| 5848 | return -1; |
| 5849 | |
| 5850 | h2_settings_header_table_size = atoi(args[1]); |
| 5851 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5852 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5853 | return -1; |
| 5854 | } |
| 5855 | return 0; |
| 5856 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5857 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5858 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5859 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5860 | struct proxy *defpx, const char *file, int line, |
| 5861 | char **err) |
| 5862 | { |
| 5863 | if (too_many_args(1, args, err, NULL)) |
| 5864 | return -1; |
| 5865 | |
| 5866 | h2_settings_initial_window_size = atoi(args[1]); |
| 5867 | if (h2_settings_initial_window_size < 0) { |
| 5868 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5869 | return -1; |
| 5870 | } |
| 5871 | return 0; |
| 5872 | } |
| 5873 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5874 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5875 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5876 | struct proxy *defpx, const char *file, int line, |
| 5877 | char **err) |
| 5878 | { |
| 5879 | if (too_many_args(1, args, err, NULL)) |
| 5880 | return -1; |
| 5881 | |
| 5882 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5883 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5884 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5885 | return -1; |
| 5886 | } |
| 5887 | return 0; |
| 5888 | } |
| 5889 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5890 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5891 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5892 | struct proxy *defpx, const char *file, int line, |
| 5893 | char **err) |
| 5894 | { |
| 5895 | if (too_many_args(1, args, err, NULL)) |
| 5896 | return -1; |
| 5897 | |
| 5898 | h2_settings_max_frame_size = atoi(args[1]); |
| 5899 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5900 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5901 | return -1; |
| 5902 | } |
| 5903 | return 0; |
| 5904 | } |
| 5905 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5906 | |
| 5907 | /****************************************/ |
| 5908 | /* MUX initialization and instanciation */ |
| 5909 | /***************************************/ |
| 5910 | |
| 5911 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5912 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5913 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5914 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5915 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5916 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5917 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5918 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5919 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5920 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5921 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5922 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5923 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5924 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5925 | .shutr = h2_shutr, |
| 5926 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5927 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5928 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5929 | .name = "H2", |
| 5930 | }; |
| 5931 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5932 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5933 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5934 | { .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] | 5935 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5936 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5937 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5938 | |
| 5939 | /* The mux operations */ |
| 5940 | static const struct mux_ops h2_htx_ops = { |
| 5941 | .init = h2_init, |
| 5942 | .wake = h2_wake, |
| 5943 | .snd_buf = h2_snd_buf, |
| 5944 | .rcv_buf = h2_rcv_buf, |
| 5945 | .subscribe = h2_subscribe, |
| 5946 | .unsubscribe = h2_unsubscribe, |
| 5947 | .attach = h2_attach, |
| 5948 | .get_first_cs = h2_get_first_cs, |
| 5949 | .detach = h2_detach, |
| 5950 | .destroy = h2_destroy, |
| 5951 | .avail_streams = h2_avail_streams, |
| 5952 | .used_streams = h2_used_streams, |
| 5953 | .shutr = h2_shutr, |
| 5954 | .shutw = h2_shutw, |
| 5955 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5956 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5957 | .name = "H2", |
| 5958 | }; |
| 5959 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5960 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5961 | { .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] | 5962 | |
| 5963 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5964 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5965 | /* config keyword parsers */ |
| 5966 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5967 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5968 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5969 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5970 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5971 | { 0, NULL, NULL } |
| 5972 | }}; |
| 5973 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5974 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |