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 | d63b85d | 2019-10-31 15:10:03 +0100 | [diff] [blame] | 510 | if (h2c->st0 >= H2_CS_ERROR) |
| 511 | return 0; |
| 512 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 513 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 514 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 515 | |
| 516 | /* we must also consider the limit imposed by stream IDs */ |
| 517 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 518 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 519 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 520 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 521 | ret1 = MIN(ret1, ret2); |
| 522 | } |
| 523 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 526 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 527 | /*****************************************************************/ |
| 528 | /* functions below are dedicated to the mux setup and management */ |
| 529 | /*****************************************************************/ |
| 530 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 531 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 532 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 533 | * connections from the fact that the context is still NULL (even during mux |
| 534 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 535 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 536 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 537 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 538 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 539 | { |
| 540 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 541 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 542 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 543 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 544 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 545 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 546 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 547 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 548 | h2c->flags = H2_CF_IS_BACK; |
| 549 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 550 | if (tick_isset(prx->timeout.serverfin)) |
| 551 | h2c->shut_timeout = prx->timeout.serverfin; |
| 552 | } else { |
| 553 | h2c->flags = H2_CF_NONE; |
| 554 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 555 | if (tick_isset(prx->timeout.clientfin)) |
| 556 | h2c->shut_timeout = prx->timeout.clientfin; |
| 557 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 558 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 559 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 560 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 561 | if (tick_isset(h2c->timeout)) { |
| 562 | t = task_new(tid_bit); |
| 563 | if (!t) |
| 564 | goto fail; |
| 565 | |
| 566 | h2c->task = t; |
| 567 | t->process = h2_timeout_task; |
| 568 | t->context = h2c; |
| 569 | t->expire = tick_add(now_ms, h2c->timeout); |
| 570 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 571 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 572 | h2c->wait_event.tasklet = tasklet_new(); |
| 573 | if (!h2c->wait_event.tasklet) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 574 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 575 | h2c->wait_event.tasklet->process = h2_io_cb; |
| 576 | h2c->wait_event.tasklet->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 577 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 578 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 579 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 580 | if (!h2c->ddht) |
| 581 | goto fail; |
| 582 | |
| 583 | /* Initialise the context. */ |
| 584 | h2c->st0 = H2_CS_PREFACE; |
| 585 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 586 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 587 | h2c->max_id = -1; |
| 588 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 589 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 590 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 591 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 592 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 593 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 594 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 595 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 596 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 597 | h2c->dsi = -1; |
| 598 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 599 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 600 | h2c->last_sid = -1; |
| 601 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 602 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 603 | h2c->miw = 65535; /* mux initial window size */ |
| 604 | h2c->mws = 65535; /* mux window size */ |
| 605 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 606 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 607 | LIST_INIT(&h2c->send_list); |
| 608 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 609 | LIST_INIT(&h2c->blocked_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 610 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 611 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 612 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 613 | if (t) |
| 614 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 615 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 616 | if (h2c->flags & H2_CF_IS_BACK) { |
| 617 | /* FIXME: this is temporary, for outgoing connections we need |
| 618 | * to immediately allocate a stream until the code is modified |
| 619 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 620 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 621 | */ |
| 622 | struct h2s *h2s; |
| 623 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 624 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 625 | if (!h2s) |
| 626 | goto fail_stream; |
| 627 | } |
| 628 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 629 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 630 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 631 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 632 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 633 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 634 | fail_stream: |
| 635 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 636 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 637 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 638 | if (h2c->wait_event.tasklet) |
| 639 | tasklet_free(h2c->wait_event.tasklet); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 640 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 641 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 642 | return -1; |
| 643 | } |
| 644 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 645 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 646 | * -1 if no more is allocatable. |
| 647 | */ |
| 648 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 649 | { |
| 650 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 651 | |
| 652 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 653 | id = -1; |
| 654 | return id; |
| 655 | } |
| 656 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 657 | /* returns the stream associated with id <id> or NULL if not found */ |
| 658 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 659 | { |
| 660 | struct eb32_node *node; |
| 661 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 662 | if (id == 0) |
| 663 | return (struct h2s *)h2_closed_stream; |
| 664 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 665 | if (id > h2c->max_id) |
| 666 | return (struct h2s *)h2_idle_stream; |
| 667 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 668 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 669 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 670 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 671 | |
| 672 | return container_of(node, struct h2s, by_id); |
| 673 | } |
| 674 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 675 | /* release function. This one should be called to free all resources allocated |
| 676 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 677 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 678 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 679 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 680 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 681 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 682 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 683 | /* The connection must be aattached to this mux to be released */ |
| 684 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 685 | conn = h2c->conn; |
| 686 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 687 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 688 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 689 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 690 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 691 | LIST_DEL(&h2c->buf_wait.list); |
| 692 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 693 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 694 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 695 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 696 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 697 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 698 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 699 | h2c->task->context = NULL; |
| 700 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 701 | h2c->task = NULL; |
| 702 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 703 | if (h2c->wait_event.tasklet) |
| 704 | tasklet_free(h2c->wait_event.tasklet); |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 705 | if (conn && h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 706 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 707 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 708 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 709 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 712 | if (conn) { |
| 713 | conn->mux = NULL; |
| 714 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 715 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 716 | conn_stop_tracking(conn); |
| 717 | conn_full_close(conn); |
| 718 | if (conn->destroy_cb) |
| 719 | conn->destroy_cb(conn); |
| 720 | conn_free(conn); |
| 721 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 725 | /******************************************************/ |
| 726 | /* functions below are for the H2 protocol processing */ |
| 727 | /******************************************************/ |
| 728 | |
| 729 | /* 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] | 730 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 731 | { |
| 732 | return h2s ? h2s->id : 0; |
| 733 | } |
| 734 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 735 | /* returns the sum of the stream's own window size and the mux's initial |
| 736 | * window, which together form the stream's effective window size. |
| 737 | */ |
| 738 | static inline int h2s_mws(const struct h2s *h2s) |
| 739 | { |
| 740 | return h2s->sws + h2s->h2c->miw; |
| 741 | } |
| 742 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 743 | /* 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] | 744 | 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] | 745 | { |
| 746 | if (h2c->msi < 0) |
| 747 | return 0; |
| 748 | |
| 749 | if (h2c->msi == h2s_id(h2s)) |
| 750 | return 0; |
| 751 | |
| 752 | return 1; |
| 753 | } |
| 754 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 755 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 756 | 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] | 757 | { |
| 758 | h2c->errcode = err; |
| 759 | h2c->st0 = H2_CS_ERROR; |
| 760 | } |
| 761 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 762 | /* marks an error on the stream. It may also update an already closed stream |
| 763 | * (e.g. to report an error after an RST was received). |
| 764 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 765 | 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] | 766 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 767 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 768 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 769 | if (h2s->st < H2_SS_ERROR) |
| 770 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 771 | if (h2s->cs) |
| 772 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 776 | /* attempt to notify the data layer of recv availability */ |
| 777 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 778 | { |
| 779 | struct wait_event *sw; |
| 780 | |
| 781 | if (h2s->recv_wait) { |
| 782 | sw = h2s->recv_wait; |
| 783 | sw->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 784 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 785 | h2s->recv_wait = NULL; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* attempt to notify the data layer of send availability */ |
| 790 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 791 | { |
| 792 | struct wait_event *sw; |
| 793 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 794 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 795 | sw = h2s->send_wait; |
| 796 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 797 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 798 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 802 | /* alerts the data layer, trying to wake it up by all means, following |
| 803 | * this sequence : |
| 804 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 805 | * - if its subscribed to send, then it's woken up for send |
| 806 | * - if it was subscribed to neither, its ->wake() callback is called |
| 807 | * It is safe to call this function with a closed stream which doesn't have a |
| 808 | * conn_stream anymore. |
| 809 | */ |
| 810 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 811 | { |
| 812 | if (h2s->recv_wait || h2s->send_wait) { |
| 813 | h2s_notify_recv(h2s); |
| 814 | h2s_notify_send(h2s); |
| 815 | } |
| 816 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 817 | h2s->cs->data_cb->wake(h2s->cs); |
| 818 | } |
| 819 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 820 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 821 | 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] | 822 | { |
| 823 | uint8_t *out = frame; |
| 824 | |
| 825 | *out = len >> 16; |
| 826 | write_n16(out + 1, len); |
| 827 | } |
| 828 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 829 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 830 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 831 | * 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] | 832 | * available in the buffer's input prior to calling this function. The buffer |
| 833 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 834 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 835 | 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] | 836 | const struct buffer *b, int o) |
| 837 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 838 | 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] | 839 | } |
| 840 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 841 | 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] | 842 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 843 | 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] | 844 | } |
| 845 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 846 | 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] | 847 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 848 | 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] | 849 | } |
| 850 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 851 | 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] | 852 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 853 | 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] | 854 | } |
| 855 | |
| 856 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 857 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 858 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 859 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 860 | * 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] | 861 | * |
| 862 | * b0 b1 b2 b3 b4 b5..b8 |
| 863 | * +----------+---------+--------+----+----+----------------------+ |
| 864 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 865 | * +----------+---------+--------+----+----+----------------------+ |
| 866 | * |
| 867 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 868 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 869 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 870 | * 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] | 871 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 872 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 873 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 874 | 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] | 875 | { |
| 876 | uint64_t w; |
| 877 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 878 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 879 | return 0; |
| 880 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 881 | w = h2_get_n64(b, o + 1); |
| 882 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 883 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 884 | h->ff = w >> 32; |
| 885 | h->ft = w >> 40; |
| 886 | h->len += w >> 48; |
| 887 | return 1; |
| 888 | } |
| 889 | |
| 890 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 891 | * h2_peek_frame_hdr() above. |
| 892 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 893 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 894 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 895 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 899 | 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] | 900 | { |
| 901 | int ret; |
| 902 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 903 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 904 | if (ret > 0) |
| 905 | h2_skip_frame_hdr(b); |
| 906 | return ret; |
| 907 | } |
| 908 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 909 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 910 | * its connection if the stream was not yet closed. Please use this exclusively |
| 911 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 912 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 913 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 914 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 915 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 916 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 917 | if (!h2s->id) |
| 918 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 919 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 920 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 921 | h2s_notify_recv(h2s); |
| 922 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 923 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 924 | h2s->st = H2_SS_CLOSED; |
| 925 | } |
| 926 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 927 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 928 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 929 | { |
| 930 | h2s_close(h2s); |
| 931 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 932 | if (b_size(&h2s->rxbuf)) { |
| 933 | b_free(&h2s->rxbuf); |
| 934 | offer_buffers(NULL, tasks_run_queue); |
| 935 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 936 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 937 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 938 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 939 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 940 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 941 | * reference left would be in the h2c send_list/fctl_list, and if |
| 942 | * we're in it, we're getting out anyway |
| 943 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 944 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 945 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 946 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 947 | LIST_DEL_INIT(&h2s->sending_list); |
| 948 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 949 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 950 | pool_free(pool_head_h2s, h2s); |
| 951 | } |
| 952 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 953 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 954 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 955 | * causes of errors can be any failed memory allocation. The caller is |
| 956 | * responsible for checking if the connection may support an extra stream |
| 957 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 958 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 959 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 960 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 961 | struct h2s *h2s; |
| 962 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 963 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 964 | if (!h2s) |
| 965 | goto out; |
| 966 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 967 | h2s->wait_event.tasklet = tasklet_new(); |
| 968 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 969 | pool_free(pool_head_h2s, h2s); |
| 970 | goto out; |
| 971 | } |
| 972 | h2s->send_wait = NULL; |
| 973 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 974 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 975 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 976 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 977 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 978 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 979 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 980 | h2s->cs = NULL; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 981 | h2s->sws = 0; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 982 | h2s->flags = H2_SF_NONE; |
| 983 | h2s->errcode = H2_ERR_NO_ERROR; |
| 984 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 985 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 986 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 987 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 988 | |
| 989 | if (h2c->flags & H2_CF_IS_BACK) { |
| 990 | h1m_init_req(&h2s->h1m); |
| 991 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 992 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 993 | } else { |
| 994 | h1m_init_res(&h2s->h1m); |
| 995 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 996 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 997 | } |
| 998 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 999 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1000 | if (id > 0) |
| 1001 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 1002 | else |
| 1003 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1004 | |
| 1005 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 1006 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 1007 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1008 | |
| 1009 | return h2s; |
| 1010 | |
| 1011 | out_free_h2s: |
| 1012 | pool_free(pool_head_h2s, h2s); |
| 1013 | out: |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 1018 | * case of memory allocation error. |
| 1019 | */ |
| 1020 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 1021 | { |
| 1022 | struct session *sess = h2c->conn->owner; |
| 1023 | struct conn_stream *cs; |
| 1024 | struct h2s *h2s; |
| 1025 | |
| 1026 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 1027 | goto out; |
| 1028 | |
| 1029 | h2s = h2s_new(h2c, id); |
| 1030 | if (!h2s) |
| 1031 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1032 | |
| 1033 | cs = cs_new(h2c->conn); |
| 1034 | if (!cs) |
| 1035 | goto out_close; |
| 1036 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1037 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1038 | h2s->cs = cs; |
| 1039 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1040 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1041 | |
| 1042 | if (stream_create_from_cs(cs) < 0) |
| 1043 | goto out_free_cs; |
| 1044 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1045 | /* We want the accept date presented to the next stream to be the one |
| 1046 | * we have now, the handshake time to be null (since the next stream |
| 1047 | * is not delayed by a handshake), and the idle time to count since |
| 1048 | * right now. |
| 1049 | */ |
| 1050 | sess->accept_date = date; |
| 1051 | sess->tv_accept = now; |
| 1052 | sess->t_handshake = 0; |
| 1053 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1054 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1055 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1056 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1057 | return h2s; |
| 1058 | |
| 1059 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1060 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1061 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1062 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1063 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1064 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1065 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1066 | sess_log(sess); |
| 1067 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1070 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1071 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1072 | * possible stream ID was reached. |
| 1073 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1074 | 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] | 1075 | { |
| 1076 | struct h2s *h2s = NULL; |
| 1077 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1078 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1079 | goto out; |
| 1080 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1081 | if (h2_streams_left(h2c) < 1) |
| 1082 | goto out; |
| 1083 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1084 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1085 | h2s = h2s_new(h2c, 0); |
| 1086 | if (!h2s) |
| 1087 | goto out; |
| 1088 | |
| 1089 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1090 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1091 | cs->ctx = h2s; |
| 1092 | h2c->nb_cs++; |
| 1093 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1094 | out: |
| 1095 | return h2s; |
| 1096 | } |
| 1097 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1098 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1099 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1100 | * the various settings codes. |
| 1101 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1102 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1103 | { |
| 1104 | struct buffer *res; |
| 1105 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1106 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1107 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1108 | int ret; |
| 1109 | |
| 1110 | if (h2c_mux_busy(h2c, NULL)) { |
| 1111 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1115 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1116 | chunk_memcpy(&buf, |
| 1117 | "\x00\x00\x00" /* length : 0 for now */ |
| 1118 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1119 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1120 | 9); |
| 1121 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1122 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1123 | /* send settings_enable_push=0 */ |
| 1124 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1125 | } |
| 1126 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1127 | if (h2_settings_header_table_size != 4096) { |
| 1128 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1129 | |
| 1130 | write_n32(str + 2, h2_settings_header_table_size); |
| 1131 | chunk_memcat(&buf, str, 6); |
| 1132 | } |
| 1133 | |
| 1134 | if (h2_settings_initial_window_size != 65535) { |
| 1135 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1136 | |
| 1137 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1138 | chunk_memcat(&buf, str, 6); |
| 1139 | } |
| 1140 | |
| 1141 | if (h2_settings_max_concurrent_streams != 0) { |
| 1142 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1143 | |
| 1144 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1145 | * the protocol, so never send this value! |
| 1146 | */ |
| 1147 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1148 | chunk_memcat(&buf, str, 6); |
| 1149 | } |
| 1150 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1151 | mfs = h2_settings_max_frame_size; |
| 1152 | if (mfs > global.tune.bufsize) |
| 1153 | mfs = global.tune.bufsize; |
| 1154 | |
| 1155 | if (!mfs) |
| 1156 | mfs = global.tune.bufsize; |
| 1157 | |
| 1158 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1159 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1160 | |
| 1161 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1162 | * match bufsize - rewrite size, but at the moment it seems |
| 1163 | * that clients don't take care of it. |
| 1164 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1165 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1166 | chunk_memcat(&buf, str, 6); |
| 1167 | } |
| 1168 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1169 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1170 | |
| 1171 | res = br_tail(h2c->mbuf); |
| 1172 | retry: |
| 1173 | if (!h2_get_buf(h2c, res)) { |
| 1174 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1175 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1179 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1180 | if (unlikely(ret <= 0)) { |
| 1181 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1182 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1183 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1184 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1185 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1186 | return 0; |
| 1187 | } |
| 1188 | else { |
| 1189 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | } |
| 1193 | return ret; |
| 1194 | } |
| 1195 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1196 | /* Try to receive a connection preface, then upon success try to send our |
| 1197 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1198 | * missing data. It may return an error in h2c. |
| 1199 | */ |
| 1200 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1201 | { |
| 1202 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1203 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1204 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1205 | 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] | 1206 | |
| 1207 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1208 | if (ret1 < 0) |
| 1209 | sess_log(h2c->conn->owner); |
| 1210 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1211 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1212 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1216 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1217 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1218 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1219 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1220 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1223 | /* Try to send a connection preface, then upon success try to send our |
| 1224 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1225 | * missing data. It may return an error in h2c. |
| 1226 | */ |
| 1227 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1228 | { |
| 1229 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1230 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1231 | |
| 1232 | if (h2c_mux_busy(h2c, NULL)) { |
| 1233 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1237 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1238 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1239 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1240 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1241 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | if (!b_data(res)) { |
| 1246 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1247 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1248 | if (unlikely(ret <= 0)) { |
| 1249 | if (!ret) { |
| 1250 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1251 | goto retry; |
| 1252 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1253 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | else { |
| 1257 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1261 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1262 | return h2c_send_settings(h2c); |
| 1263 | } |
| 1264 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1265 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1266 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1267 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1268 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1269 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1270 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1271 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1272 | * case so that it is safe to use h2c_error() to report such errors. |
| 1273 | */ |
| 1274 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1275 | { |
| 1276 | struct buffer *res; |
| 1277 | char str[17]; |
| 1278 | int ret; |
| 1279 | |
| 1280 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1281 | return 1; // claim that it worked |
| 1282 | |
| 1283 | if (h2c_mux_busy(h2c, h2s)) { |
| 1284 | if (h2s) |
| 1285 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1286 | else |
| 1287 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1291 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1292 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1293 | |
| 1294 | if (h2c->last_sid < 0) |
| 1295 | h2c->last_sid = h2c->max_id; |
| 1296 | |
| 1297 | write_n32(str + 9, h2c->last_sid); |
| 1298 | write_n32(str + 13, h2c->errcode); |
| 1299 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1300 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1301 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1302 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1303 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1304 | if (h2s) |
| 1305 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1306 | else |
| 1307 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1311 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1312 | if (unlikely(ret <= 0)) { |
| 1313 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1314 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1315 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1316 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1317 | if (h2s) |
| 1318 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1319 | else |
| 1320 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1321 | return 0; |
| 1322 | } |
| 1323 | else { |
| 1324 | /* we cannot report this error using GOAWAY, so we mark |
| 1325 | * it and claim a success. |
| 1326 | */ |
| 1327 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1328 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1329 | return 1; |
| 1330 | } |
| 1331 | } |
| 1332 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1333 | return ret; |
| 1334 | } |
| 1335 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1336 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1337 | * during mux operations. This stream must be valid and cannot be closed |
| 1338 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1339 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1340 | * not yet. |
| 1341 | * |
| 1342 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1343 | * to write the message, it subscribes the stream to future notifications. |
| 1344 | */ |
| 1345 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1346 | { |
| 1347 | struct buffer *res; |
| 1348 | char str[13]; |
| 1349 | int ret; |
| 1350 | |
| 1351 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1352 | return 1; |
| 1353 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1354 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1355 | * RST_STREAM in response to a RST_STREAM frame. |
| 1356 | */ |
Willy Tarreau | bf9a20b | 2019-08-06 10:01:40 +0200 | [diff] [blame] | 1357 | if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) { |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1358 | ret = 1; |
| 1359 | goto ignore; |
| 1360 | } |
| 1361 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1362 | if (h2c_mux_busy(h2c, h2s)) { |
| 1363 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1367 | /* len: 4, type: 3, flags: none */ |
| 1368 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1369 | write_n32(str + 5, h2s->id); |
| 1370 | write_n32(str + 9, h2s->errcode); |
| 1371 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1372 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1373 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1374 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1375 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1376 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1380 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1381 | if (unlikely(ret <= 0)) { |
| 1382 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1383 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1384 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1385 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1386 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1387 | return 0; |
| 1388 | } |
| 1389 | else { |
| 1390 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1395 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1396 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1397 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1398 | return ret; |
| 1399 | } |
| 1400 | |
| 1401 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1402 | * 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] | 1403 | * error code, even if the stream is one of the dummy ones, and will update |
| 1404 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1405 | * |
| 1406 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1407 | * 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] | 1408 | * 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] | 1409 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1410 | */ |
| 1411 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1412 | { |
| 1413 | struct buffer *res; |
| 1414 | char str[13]; |
| 1415 | int ret; |
| 1416 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1417 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1418 | * RST_STREAM in response to a RST_STREAM frame. |
| 1419 | */ |
| 1420 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1421 | ret = 1; |
| 1422 | goto ignore; |
| 1423 | } |
| 1424 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1425 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1426 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
| 1429 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1430 | /* len: 4, type: 3, flags: none */ |
| 1431 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1432 | |
| 1433 | write_n32(str + 5, h2c->dsi); |
| 1434 | write_n32(str + 9, h2s->errcode); |
| 1435 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1436 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1437 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1438 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1439 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1440 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1441 | return 0; |
| 1442 | } |
| 1443 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1444 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1445 | if (unlikely(ret <= 0)) { |
| 1446 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1447 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1448 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1449 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1450 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
| 1453 | else { |
| 1454 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1459 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1460 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1461 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1462 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1463 | } |
| 1464 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1465 | return ret; |
| 1466 | } |
| 1467 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1468 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1469 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1470 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1471 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1472 | * message, it subscribes the requesting stream to future notifications. |
| 1473 | */ |
| 1474 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1475 | { |
| 1476 | struct h2c *h2c = h2s->h2c; |
| 1477 | struct buffer *res; |
| 1478 | char str[9]; |
| 1479 | int ret; |
| 1480 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1481 | 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] | 1482 | return 1; |
| 1483 | |
| 1484 | if (h2c_mux_busy(h2c, h2s)) { |
| 1485 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1489 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1490 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1491 | write_n32(str + 5, h2s->id); |
| 1492 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1493 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1494 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1495 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1496 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1497 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1501 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1502 | if (likely(ret > 0)) { |
| 1503 | h2s->flags |= H2_SF_ES_SENT; |
| 1504 | } |
| 1505 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1506 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1507 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1508 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1509 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1510 | return 0; |
| 1511 | } |
| 1512 | else { |
| 1513 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1514 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1515 | } |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1519 | /* 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] | 1520 | * 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] | 1521 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1522 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1523 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1524 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1525 | { |
| 1526 | if (!h2s->cs) { |
| 1527 | /* this stream was already orphaned */ |
| 1528 | h2s_destroy(h2s); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1532 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1533 | if (h2s->st == H2_SS_OPEN) |
| 1534 | h2s->st = H2_SS_HREM; |
| 1535 | else if (h2s->st == H2_SS_HLOC) |
| 1536 | h2s_close(h2s); |
| 1537 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1538 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1539 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1540 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1541 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1542 | if (h2s->cs->flags & CS_FL_EOS) |
| 1543 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1544 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1545 | if (h2s->st < H2_SS_ERROR) |
| 1546 | h2s->st = H2_SS_ERROR; |
| 1547 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1548 | |
| 1549 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1553 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1554 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1555 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1556 | { |
| 1557 | struct eb32_node *node; |
| 1558 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1559 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1560 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1561 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1562 | while (node) { |
| 1563 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1564 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1565 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1566 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1567 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1568 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1569 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1570 | while (node) { |
| 1571 | h2s = container_of(node, struct h2s, by_id); |
| 1572 | if (h2s->id > 0) |
| 1573 | break; |
| 1574 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1575 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1579 | /* Wake up all blocked streams whose window size has become positive after the |
| 1580 | * mux's initial window was adjusted. This should be done after having processed |
| 1581 | * SETTINGS frames which have updated the mux's initial window size. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1582 | */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1583 | static void h2c_unblock_sfctl(struct h2c *h2c) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1584 | { |
| 1585 | struct h2s *h2s; |
| 1586 | struct eb32_node *node; |
| 1587 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1588 | node = eb32_first(&h2c->streams_by_id); |
| 1589 | while (node) { |
| 1590 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1591 | if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) { |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1592 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1593 | LIST_DEL_INIT(&h2s->list); |
| 1594 | if (h2s->send_wait) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1595 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1596 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1597 | node = eb32_next(node); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1602 | * 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] | 1603 | * return an error in h2c. The caller must have already verified frame length |
| 1604 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1605 | */ |
| 1606 | static int h2c_handle_settings(struct h2c *h2c) |
| 1607 | { |
| 1608 | unsigned int offset; |
| 1609 | int error; |
| 1610 | |
| 1611 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1612 | if (h2c->dfl) { |
| 1613 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1614 | goto fail; |
| 1615 | } |
| 1616 | return 1; |
| 1617 | } |
| 1618 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1619 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1620 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1621 | return 0; |
| 1622 | |
| 1623 | /* parse the frame */ |
| 1624 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1625 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1626 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1627 | |
| 1628 | switch (type) { |
| 1629 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1630 | /* we need to update all existing streams with the |
| 1631 | * difference from the previous iws. |
| 1632 | */ |
| 1633 | if (arg < 0) { // RFC7540#6.5.2 |
| 1634 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1635 | goto fail; |
| 1636 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1637 | h2c->miw = arg; |
| 1638 | break; |
| 1639 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1640 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1641 | error = H2_ERR_PROTOCOL_ERROR; |
| 1642 | goto fail; |
| 1643 | } |
| 1644 | h2c->mfs = arg; |
| 1645 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1646 | case H2_SETTINGS_ENABLE_PUSH: |
| 1647 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1648 | error = H2_ERR_PROTOCOL_ERROR; |
| 1649 | goto fail; |
| 1650 | } |
| 1651 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1652 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1653 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1654 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1655 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1656 | arg = h2_settings_max_concurrent_streams; |
| 1657 | h2c->streams_limit = arg; |
| 1658 | } |
| 1659 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | /* need to ACK this frame now */ |
| 1664 | h2c->st0 = H2_CS_FRAME_A; |
| 1665 | return 1; |
| 1666 | fail: |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 1667 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 1668 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1669 | h2c_error(h2c, error); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1674 | * success or one of the h2_status values. |
| 1675 | */ |
| 1676 | static int h2c_ack_settings(struct h2c *h2c) |
| 1677 | { |
| 1678 | struct buffer *res; |
| 1679 | char str[9]; |
| 1680 | int ret = -1; |
| 1681 | |
| 1682 | if (h2c_mux_busy(h2c, NULL)) { |
| 1683 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1687 | memcpy(str, |
| 1688 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1689 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1690 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1691 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1692 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1693 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1694 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1695 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1696 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1697 | return 0; |
| 1698 | } |
| 1699 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1700 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1701 | if (unlikely(ret <= 0)) { |
| 1702 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1703 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1704 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1705 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1706 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1707 | return 0; |
| 1708 | } |
| 1709 | else { |
| 1710 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1711 | return 0; |
| 1712 | } |
| 1713 | } |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1717 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1718 | * 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] | 1719 | * missing data. The caller must have already verified frame length |
| 1720 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1721 | */ |
| 1722 | static int h2c_handle_ping(struct h2c *h2c) |
| 1723 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1724 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1725 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1726 | h2c->st0 = H2_CS_FRAME_A; |
| 1727 | return 1; |
| 1728 | } |
| 1729 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1730 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1731 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1732 | * error in h2c. |
| 1733 | */ |
| 1734 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1735 | { |
| 1736 | struct buffer *res; |
| 1737 | char str[13]; |
| 1738 | int ret = -1; |
| 1739 | |
| 1740 | if (h2c_mux_busy(h2c, NULL)) { |
| 1741 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1745 | /* length: 4, type: 8, flags: none */ |
| 1746 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1747 | write_n32(str + 5, sid); |
| 1748 | write_n32(str + 9, increment); |
| 1749 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1750 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1751 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1752 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1753 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1754 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1758 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1759 | if (unlikely(ret <= 0)) { |
| 1760 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1761 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1762 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1763 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1764 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1765 | return 0; |
| 1766 | } |
| 1767 | else { |
| 1768 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | } |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
| 1775 | /* try to send pending window update for the connection. It's safe to call it |
| 1776 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1777 | * failure. It may return an error in h2c. |
| 1778 | */ |
| 1779 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1780 | { |
| 1781 | int ret = 1; |
| 1782 | |
| 1783 | if (h2c->rcvd_c <= 0) |
| 1784 | return 1; |
| 1785 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1786 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1787 | /* increase the advertised connection window to 2G on |
| 1788 | * first update. |
| 1789 | */ |
| 1790 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1791 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1792 | } |
| 1793 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1794 | /* send WU for the connection */ |
| 1795 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1796 | if (ret > 0) |
| 1797 | h2c->rcvd_c = 0; |
| 1798 | |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1803 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1804 | * room or failure. It may return an error in h2c. |
| 1805 | */ |
| 1806 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1807 | { |
| 1808 | int ret = 1; |
| 1809 | |
| 1810 | if (h2c->rcvd_s <= 0) |
| 1811 | return 1; |
| 1812 | |
| 1813 | /* send WU for the stream */ |
| 1814 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1815 | if (ret > 0) |
| 1816 | h2c->rcvd_s = 0; |
| 1817 | |
| 1818 | return ret; |
| 1819 | } |
| 1820 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1821 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1822 | * success, 0 on missing data or one of the h2_status values. |
| 1823 | */ |
| 1824 | static int h2c_ack_ping(struct h2c *h2c) |
| 1825 | { |
| 1826 | struct buffer *res; |
| 1827 | char str[17]; |
| 1828 | int ret = -1; |
| 1829 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1830 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1831 | return 0; |
| 1832 | |
| 1833 | if (h2c_mux_busy(h2c, NULL)) { |
| 1834 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1838 | memcpy(str, |
| 1839 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1840 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1841 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1842 | |
| 1843 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1844 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1845 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1846 | res = br_tail(h2c->mbuf); |
| 1847 | retry: |
| 1848 | if (!h2_get_buf(h2c, res)) { |
| 1849 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1850 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1854 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1855 | if (unlikely(ret <= 0)) { |
| 1856 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1857 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1858 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1859 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1860 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1861 | return 0; |
| 1862 | } |
| 1863 | else { |
| 1864 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1865 | return 0; |
| 1866 | } |
| 1867 | } |
| 1868 | return ret; |
| 1869 | } |
| 1870 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1871 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1872 | * 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] | 1873 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1874 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1875 | */ |
| 1876 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1877 | { |
| 1878 | int32_t inc; |
| 1879 | int error; |
| 1880 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1881 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1882 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1883 | return 0; |
| 1884 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1885 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1886 | |
| 1887 | if (h2c->dsi != 0) { |
| 1888 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1889 | |
| 1890 | /* it's not an error to receive WU on a closed stream */ |
| 1891 | if (h2s->st == H2_SS_CLOSED) |
| 1892 | return 1; |
| 1893 | |
| 1894 | if (!inc) { |
| 1895 | error = H2_ERR_PROTOCOL_ERROR; |
| 1896 | goto strm_err; |
| 1897 | } |
| 1898 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1899 | if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1900 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1901 | goto strm_err; |
| 1902 | } |
| 1903 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1904 | h2s->sws += inc; |
| 1905 | if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1906 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1907 | LIST_DEL_INIT(&h2s->list); |
| 1908 | if (h2s->send_wait) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1909 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | else { |
| 1913 | /* connection window update */ |
| 1914 | if (!inc) { |
| 1915 | error = H2_ERR_PROTOCOL_ERROR; |
| 1916 | goto conn_err; |
| 1917 | } |
| 1918 | |
| 1919 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1920 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1921 | goto conn_err; |
| 1922 | } |
| 1923 | |
| 1924 | h2c->mws += inc; |
| 1925 | } |
| 1926 | |
| 1927 | return 1; |
| 1928 | |
| 1929 | conn_err: |
| 1930 | h2c_error(h2c, error); |
| 1931 | return 0; |
| 1932 | |
| 1933 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1934 | h2s_error(h2s, error); |
| 1935 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1939 | /* 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] | 1940 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1941 | * have already verified frame length and stream ID validity. Described in |
| 1942 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1943 | */ |
| 1944 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1945 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1946 | int last; |
| 1947 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1948 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1949 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1950 | return 0; |
| 1951 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1952 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1953 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1954 | if (h2c->last_sid < 0) |
| 1955 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1956 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1957 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1960 | /* 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] | 1961 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1962 | * error in h2c. The caller must have already verified frame length and stream |
| 1963 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1964 | */ |
| 1965 | static int h2c_handle_priority(struct h2c *h2c) |
| 1966 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1967 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1968 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1969 | return 0; |
| 1970 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1971 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1972 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1973 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1974 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1975 | } |
| 1976 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1977 | } |
| 1978 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1979 | /* 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] | 1980 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1981 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1982 | */ |
| 1983 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1984 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1985 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1986 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1987 | return 0; |
| 1988 | |
| 1989 | /* late RST, already handled */ |
| 1990 | if (h2s->st == H2_SS_CLOSED) |
| 1991 | return 1; |
| 1992 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1993 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1994 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1995 | |
| 1996 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1997 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1998 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | h2s->flags |= H2_SF_RST_RCVD; |
| 2002 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2005 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2006 | * It may return an error in h2c or h2s. The caller must consider that the |
| 2007 | * return value is the new h2s in case one was allocated (most common case). |
| 2008 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2009 | * errors here are reported as connection errors since it's impossible to |
| 2010 | * recover from such errors after the compression context has been altered. |
| 2011 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2012 | 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] | 2013 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2014 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2015 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2016 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2017 | int error; |
| 2018 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2019 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2020 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2021 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2022 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2023 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2024 | |
| 2025 | /* now either the frame is complete or the buffer is complete */ |
| 2026 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2027 | /* The stream exists/existed, this must be a trailers frame */ |
| 2028 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2029 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2030 | /* unrecoverable error ? */ |
| 2031 | if (h2c->st0 >= H2_CS_ERROR) |
| 2032 | goto out; |
| 2033 | |
| 2034 | if (error == 0) |
| 2035 | goto out; // missing data |
| 2036 | |
| 2037 | if (error < 0) { |
| 2038 | /* Failed to decode this frame (e.g. too large request) |
| 2039 | * but the HPACK decompressor is still synchronized. |
| 2040 | */ |
| 2041 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2042 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2043 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2044 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2045 | goto done; |
| 2046 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2047 | /* the connection was already killed by an RST, let's consume |
| 2048 | * the data and send another RST. |
| 2049 | */ |
| 2050 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2051 | h2s = (struct h2s*)h2_error_stream; |
| 2052 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2053 | } |
| 2054 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2055 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2056 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2057 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2058 | goto conn_err; |
| 2059 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2060 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2061 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2062 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2063 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2064 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2065 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2066 | if (h2c->st0 >= H2_CS_ERROR) |
| 2067 | goto out; |
| 2068 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2069 | if (error <= 0) { |
| 2070 | if (error == 0) |
| 2071 | goto out; // missing data |
| 2072 | |
| 2073 | /* Failed to decode this stream (e.g. too large request) |
| 2074 | * but the HPACK decompressor is still synchronized. |
| 2075 | */ |
| 2076 | h2s = (struct h2s*)h2_error_stream; |
| 2077 | goto send_rst; |
| 2078 | } |
| 2079 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2080 | /* 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] | 2081 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2082 | * 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] | 2083 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2084 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2085 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2086 | h2s = (struct h2s*)h2_refused_stream; |
| 2087 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2091 | h2s->rxbuf = rxbuf; |
| 2092 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2093 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2094 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2095 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2096 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2097 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2098 | |
| 2099 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2100 | if (h2s->st == H2_SS_OPEN) |
| 2101 | h2s->st = H2_SS_HREM; |
| 2102 | else |
| 2103 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2104 | } |
| 2105 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2106 | /* update the max stream ID if the request is being processed */ |
| 2107 | if (h2s->id > h2c->max_id) |
| 2108 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2109 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2110 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2111 | |
| 2112 | conn_err: |
| 2113 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2114 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2115 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2116 | out: |
| 2117 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2118 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2119 | |
| 2120 | send_rst: |
| 2121 | /* make the demux send an RST for the current stream. We may only |
| 2122 | * do this if we're certain that the HEADERS frame was properly |
| 2123 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2124 | */ |
| 2125 | h2_release_buf(h2c, &rxbuf); |
| 2126 | h2c->st0 = H2_CS_FRAME_E; |
| 2127 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2128 | } |
| 2129 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2130 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2131 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2132 | * errors here are reported as connection errors since it's impossible to |
| 2133 | * recover from such errors after the compression context has been altered. |
| 2134 | */ |
| 2135 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2136 | { |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2137 | struct buffer rxbuf = BUF_NULL; |
| 2138 | unsigned long long body_len = 0; |
| 2139 | uint32_t flags = 0; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2140 | int error; |
| 2141 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2142 | if (!b_size(&h2c->dbuf)) |
| 2143 | return NULL; // empty buffer |
| 2144 | |
| 2145 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2146 | return NULL; // incomplete frame |
| 2147 | |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2148 | if (h2s->st != H2_SS_CLOSED) { |
| 2149 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
| 2150 | } |
| 2151 | else { |
| 2152 | /* the connection was already killed by an RST, let's consume |
| 2153 | * the data and send another RST. |
| 2154 | */ |
| 2155 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Christopher Faulet | 0342705 | 2019-09-26 16:19:13 +0200 | [diff] [blame] | 2156 | h2s = (struct h2s*)h2_error_stream; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2157 | h2c->st0 = H2_CS_FRAME_E; |
| 2158 | goto send_rst; |
| 2159 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2160 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2161 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2162 | if (h2c->st0 >= H2_CS_ERROR) |
| 2163 | return NULL; |
| 2164 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2165 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2166 | /* RFC7540#5.1 */ |
| 2167 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2168 | h2c->st0 = H2_CS_FRAME_E; |
| 2169 | return NULL; |
| 2170 | } |
| 2171 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2172 | if (error <= 0) { |
| 2173 | if (error == 0) |
| 2174 | return NULL; // missing data |
| 2175 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2176 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2177 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2178 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2179 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2182 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2183 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2184 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2185 | 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] | 2186 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2187 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2188 | if (h2s->st == H2_SS_OPEN) |
| 2189 | h2s->st = H2_SS_HREM; |
| 2190 | else if (h2s->st == H2_SS_HLOC) |
| 2191 | h2s_close(h2s); |
| 2192 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2193 | |
| 2194 | return h2s; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2195 | |
| 2196 | send_rst: |
| 2197 | /* make the demux send an RST for the current stream. We may only |
| 2198 | * do this if we're certain that the HEADERS frame was properly |
| 2199 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2200 | */ |
| 2201 | h2_release_buf(h2c, &rxbuf); |
| 2202 | h2c->st0 = H2_CS_FRAME_E; |
| 2203 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2204 | } |
| 2205 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2206 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2207 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2208 | */ |
| 2209 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2210 | { |
| 2211 | int error; |
| 2212 | |
| 2213 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2214 | * to signal an end of stream (with the ES flag). |
| 2215 | */ |
| 2216 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2217 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2218 | return 0; // empty buffer |
| 2219 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2220 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2221 | return 0; // incomplete frame |
| 2222 | |
| 2223 | /* now either the frame is complete or the buffer is complete */ |
| 2224 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2225 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2226 | /* RFC7540#6.1 */ |
| 2227 | error = H2_ERR_STREAM_CLOSED; |
| 2228 | goto strm_err; |
| 2229 | } |
| 2230 | |
Christopher Faulet | 4fb65f4 | 2019-06-19 09:25:58 +0200 | [diff] [blame] | 2231 | 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] | 2232 | /* RFC7540#8.1.2 */ |
| 2233 | error = H2_ERR_PROTOCOL_ERROR; |
| 2234 | goto strm_err; |
| 2235 | } |
| 2236 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2237 | if (!h2_frt_transfer_data(h2s)) |
| 2238 | return 0; |
| 2239 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2240 | /* call the upper layers to process the frame, then let the upper layer |
| 2241 | * notify the stream about any change. |
| 2242 | */ |
| 2243 | if (!h2s->cs) { |
Willy Tarreau | c1a2965 | 2019-08-06 10:11:02 +0200 | [diff] [blame] | 2244 | /* The upper layer has already closed, this may happen on |
| 2245 | * 4xx/redirects during POST, or when receiving a response |
| 2246 | * from an H2 server after the client has aborted. |
| 2247 | */ |
| 2248 | error = H2_ERR_CANCEL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2249 | goto strm_err; |
| 2250 | } |
| 2251 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2252 | if (h2c->st0 >= H2_CS_ERROR) |
| 2253 | return 0; |
| 2254 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2255 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2256 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2257 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | /* check for completion : the callee will change this to FRAME_A or |
| 2261 | * FRAME_H once done. |
| 2262 | */ |
| 2263 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2264 | return 0; |
| 2265 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2266 | /* last frame */ |
| 2267 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2268 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2269 | if (h2s->st == H2_SS_OPEN) |
| 2270 | h2s->st = H2_SS_HREM; |
| 2271 | else |
| 2272 | h2s_close(h2s); |
| 2273 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2274 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2275 | /* RFC7540#8.1.2 */ |
| 2276 | error = H2_ERR_PROTOCOL_ERROR; |
| 2277 | goto strm_err; |
| 2278 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2279 | } |
| 2280 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2281 | return 1; |
| 2282 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2283 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2284 | h2s_error(h2s, error); |
| 2285 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2286 | return 0; |
| 2287 | } |
| 2288 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2289 | /* process Rx frames to be demultiplexed */ |
| 2290 | static void h2_process_demux(struct h2c *h2c) |
| 2291 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2292 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2293 | struct h2_fh hdr; |
| 2294 | unsigned int padlen = 0; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2295 | int32_t old_iw = h2c->miw; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2296 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2297 | if (h2c->st0 >= H2_CS_ERROR) |
| 2298 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2299 | |
| 2300 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2301 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2302 | if (h2c->flags & H2_CF_IS_BACK) |
| 2303 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2304 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2305 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2306 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2307 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2308 | sess_log(h2c->conn->owner); |
| 2309 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2310 | goto fail; |
| 2311 | } |
| 2312 | |
| 2313 | h2c->max_id = 0; |
| 2314 | h2c->st0 = H2_CS_SETTINGS1; |
| 2315 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2316 | |
| 2317 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2318 | /* ensure that what is pending is a valid SETTINGS frame |
| 2319 | * without an ACK. |
| 2320 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2321 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2322 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2323 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2324 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2325 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2326 | sess_log(h2c->conn->owner); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2327 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2328 | goto fail; |
| 2329 | } |
| 2330 | |
| 2331 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2332 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2333 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2334 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2335 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2336 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2337 | goto fail; |
| 2338 | } |
| 2339 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2340 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2341 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2342 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2343 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2344 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2345 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2346 | goto fail; |
| 2347 | } |
| 2348 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2349 | /* that's OK, switch to FRAME_P to process it. This is |
| 2350 | * a SETTINGS frame whose header has already been |
| 2351 | * deleted above. |
| 2352 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2353 | padlen = 0; |
| 2354 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2355 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2356 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2357 | |
| 2358 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2359 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2360 | int ret = 0; |
| 2361 | |
| 2362 | if (h2c->st0 >= H2_CS_ERROR) |
| 2363 | break; |
| 2364 | |
| 2365 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | ab3ebbc | 2019-08-06 15:49:51 +0200 | [diff] [blame] | 2366 | h2c->rcvd_s = 0; |
| 2367 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2368 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2369 | break; |
| 2370 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2371 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2372 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2373 | if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2374 | /* only log if no other stream can report the error */ |
| 2375 | sess_log(h2c->conn->owner); |
| 2376 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2377 | break; |
| 2378 | } |
| 2379 | |
Christopher Faulet | 3d574a5 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2380 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2381 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2382 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2383 | * we read the pad length and drop it from the remaining |
| 2384 | * payload (one byte + the 9 remaining ones = 10 total |
| 2385 | * removed), so we have a frame payload starting after the |
| 2386 | * pad len. Flow controlled frames (DATA) also count the |
| 2387 | * padlen in the flow control, so it must be adjusted. |
| 2388 | */ |
| 2389 | if (hdr.len < 1) { |
| 2390 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2391 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2392 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2393 | goto fail; |
| 2394 | } |
| 2395 | hdr.len--; |
| 2396 | |
| 2397 | if (b_data(&h2c->dbuf) < 10) |
| 2398 | break; // missing padlen |
| 2399 | |
| 2400 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2401 | |
| 2402 | if (padlen > hdr.len) { |
| 2403 | /* RFC7540#6.1 : pad length = length of |
| 2404 | * frame payload or greater => error. |
| 2405 | */ |
| 2406 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2407 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2408 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2409 | goto fail; |
| 2410 | } |
| 2411 | |
| 2412 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2413 | h2c->rcvd_c++; |
| 2414 | h2c->rcvd_s++; |
| 2415 | } |
| 2416 | b_del(&h2c->dbuf, 1); |
| 2417 | } |
| 2418 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2419 | |
| 2420 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2421 | h2c->dfl = hdr.len; |
| 2422 | h2c->dsi = hdr.sid; |
| 2423 | h2c->dft = hdr.ft; |
| 2424 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2425 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2426 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* check for minimum basic frame format validity */ |
| 2429 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2430 | if (ret != H2_ERR_NO_ERROR) { |
| 2431 | h2c_error(h2c, ret); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2432 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2433 | sess_log(h2c->conn->owner); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2434 | goto fail; |
| 2435 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2436 | } |
| 2437 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2438 | /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here. |
| 2439 | * H2_CS_FRAME_P indicates an incomplete previous operation |
| 2440 | * (most often the first attempt) and requires some validity |
| 2441 | * checks for the frame and the current state. The two other |
| 2442 | * ones are set after completion (or abortion) and must skip |
| 2443 | * validity checks. |
| 2444 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2445 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2446 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2447 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2448 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2449 | conn_xprt_read0_pending(h2c->conn) || |
| 2450 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2451 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2452 | (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] | 2453 | /* we may have to signal the upper layers */ |
| 2454 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2455 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2456 | } |
| 2457 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2458 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2459 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2460 | goto strm_err; |
| 2461 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2462 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2463 | goto valid_frame_type; |
| 2464 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2465 | if (h2s->st == H2_SS_IDLE && |
| 2466 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2467 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2468 | * this state MUST be treated as a connection error |
| 2469 | */ |
| 2470 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2471 | if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2472 | /* only log if no other stream can report the error */ |
| 2473 | sess_log(h2c->conn->owner); |
| 2474 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2475 | break; |
| 2476 | } |
| 2477 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2478 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2479 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2480 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2481 | * this state MUST be treated as a stream error. |
| 2482 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2483 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2484 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2485 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2486 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2487 | else |
| 2488 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2489 | goto strm_err; |
| 2490 | } |
| 2491 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2492 | /* Below the management of frames received in closed state is a |
| 2493 | * bit hackish because the spec makes strong differences between |
| 2494 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2495 | * in both directions. In addition to this, the creation of a |
| 2496 | * new stream reusing the identifier of a closed one will be |
| 2497 | * detected here. Given that we cannot keep track of all closed |
| 2498 | * streams forever, we consider that unknown closed streams were |
| 2499 | * closed on RST received, which allows us to respond with an |
| 2500 | * RST without breaking the connection (eg: to abort a transfer). |
| 2501 | * Some frames have to be silently ignored as well. |
| 2502 | */ |
| 2503 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2504 | 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] | 2505 | /* #5.1.1: The identifier of a newly |
| 2506 | * established stream MUST be numerically |
| 2507 | * greater than all streams that the initiating |
| 2508 | * endpoint has opened or reserved. This |
| 2509 | * governs streams that are opened using a |
| 2510 | * HEADERS frame and streams that are reserved |
| 2511 | * using PUSH_PROMISE. An endpoint that |
| 2512 | * receives an unexpected stream identifier |
| 2513 | * MUST respond with a connection error. |
| 2514 | */ |
| 2515 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2516 | goto strm_err; |
| 2517 | } |
| 2518 | |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2519 | if (h2s->flags & H2_SF_RST_RCVD && |
| 2520 | !(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] | 2521 | /* RFC7540#5.1:closed: an endpoint that |
| 2522 | * receives any frame other than PRIORITY after |
| 2523 | * receiving a RST_STREAM MUST treat that as a |
| 2524 | * stream error of type STREAM_CLOSED. |
| 2525 | * |
| 2526 | * Note that old streams fall into this category |
| 2527 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2528 | * |
| 2529 | * However, we cannot generalize this to all frame types. Those |
| 2530 | * carrying compression state must still be processed before |
| 2531 | * being dropped or we'll desynchronize the decoder. This can |
| 2532 | * happen with request trailers received after sending an |
| 2533 | * RST_STREAM, or with header/trailers responses received after |
| 2534 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2535 | * |
| 2536 | * In addition, since our CLOSED streams always carry the |
| 2537 | * RST_RCVD bit, we don't want to accidently catch valid |
| 2538 | * frames for a closed stream, i.e. RST/PRIO/WU. |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2539 | */ |
| 2540 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2541 | h2c->st0 = H2_CS_FRAME_E; |
| 2542 | goto strm_err; |
| 2543 | } |
| 2544 | |
| 2545 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2546 | * result of sending a RST_STREAM frame, the peer that |
| 2547 | * receives the RST_STREAM might have already sent |
| 2548 | * frames on the stream that cannot be withdrawn. An |
| 2549 | * endpoint MUST ignore frames that it receives on |
| 2550 | * closed streams after it has sent a RST_STREAM |
| 2551 | * frame. An endpoint MAY choose to limit the period |
| 2552 | * over which it ignores frames and treat frames that |
| 2553 | * arrive after this time as being in error. |
| 2554 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2555 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2556 | /* RFC7540#5.1:closed: any frame other than |
| 2557 | * PRIO/WU/RST in this state MUST be treated as |
| 2558 | * a connection error |
| 2559 | */ |
| 2560 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2561 | h2c->dft != H2_FT_PRIORITY && |
| 2562 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2563 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2564 | goto strm_err; |
| 2565 | } |
| 2566 | } |
| 2567 | } |
| 2568 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2569 | #if 0 |
| 2570 | // problem below: it is not possible to completely ignore such |
| 2571 | // streams as we need to maintain the compression state as well |
| 2572 | // and for this we need to completely process these frames (eg: |
| 2573 | // HEADERS frames) as well as counting DATA frames to emit |
| 2574 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2575 | // This is a typical case of layer violation where the |
| 2576 | // transported contents are critical to the connection's |
| 2577 | // validity and must be ignored at the same time :-( |
| 2578 | |
| 2579 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2580 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2581 | */ |
| 2582 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2583 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2584 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2585 | h2c->dfl -= ret; |
| 2586 | ret = h2c->dfl == 0; |
| 2587 | goto strm_err; |
| 2588 | } |
| 2589 | #endif |
| 2590 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2591 | valid_frame_type: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2592 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2593 | case H2_FT_SETTINGS: |
| 2594 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2595 | ret = h2c_handle_settings(h2c); |
| 2596 | |
| 2597 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2598 | ret = h2c_ack_settings(h2c); |
| 2599 | break; |
| 2600 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2601 | case H2_FT_PING: |
| 2602 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2603 | ret = h2c_handle_ping(h2c); |
| 2604 | |
| 2605 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2606 | ret = h2c_ack_ping(h2c); |
| 2607 | break; |
| 2608 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2609 | case H2_FT_WINDOW_UPDATE: |
| 2610 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2611 | ret = h2c_handle_window_update(h2c, h2s); |
| 2612 | break; |
| 2613 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2614 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2615 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2616 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2617 | * frames' parsers consume all following CONTINUATION |
| 2618 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2619 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2620 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2621 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2622 | sess_log(h2c->conn->owner); |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2623 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2624 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2625 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2626 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2627 | if (h2c->flags & H2_CF_IS_BACK) |
| 2628 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2629 | else |
| 2630 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2631 | if (tmp_h2s) { |
| 2632 | h2s = tmp_h2s; |
| 2633 | ret = 1; |
| 2634 | } |
| 2635 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2636 | break; |
| 2637 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2638 | case H2_FT_DATA: |
| 2639 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2640 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2641 | |
| 2642 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2643 | ret = h2c_send_strm_wu(h2c); |
| 2644 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2645 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2646 | case H2_FT_PRIORITY: |
| 2647 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2648 | ret = h2c_handle_priority(h2c); |
| 2649 | break; |
| 2650 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2651 | case H2_FT_RST_STREAM: |
| 2652 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2653 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2654 | break; |
| 2655 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2656 | case H2_FT_GOAWAY: |
| 2657 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2658 | ret = h2c_handle_goaway(h2c); |
| 2659 | break; |
| 2660 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2661 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2662 | default: |
| 2663 | /* drop frames that we ignore. They may be larger than |
| 2664 | * the buffer so we drain all of their contents until |
| 2665 | * we reach the end. |
| 2666 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2667 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2668 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2669 | h2c->dfl -= ret; |
| 2670 | ret = h2c->dfl == 0; |
| 2671 | } |
| 2672 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2673 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2674 | /* We may have to send an RST if not done yet */ |
| 2675 | if (h2s->st == H2_SS_ERROR) |
| 2676 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2677 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2678 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2679 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2680 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2681 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2682 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2683 | break; |
| 2684 | |
| 2685 | if (h2c->st0 != H2_CS_FRAME_H) { |
Christopher Faulet | e12a26f | 2019-09-26 16:38:28 +0200 | [diff] [blame] | 2686 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2687 | b_del(&h2c->dbuf, ret); |
| 2688 | h2c->dfl -= ret; |
| 2689 | if (!h2c->dfl) |
| 2690 | h2c->st0 = H2_CS_FRAME_H; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2691 | } |
| 2692 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2693 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2694 | if (h2c->rcvd_c > 0 && |
| 2695 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2696 | h2c_send_conn_wu(h2c); |
| 2697 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2698 | fail: |
| 2699 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2700 | if (h2s && h2s->cs && |
| 2701 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2702 | conn_xprt_read0_pending(h2c->conn) || |
| 2703 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2704 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2705 | (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] | 2706 | /* we may have to signal the upper layers */ |
| 2707 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2708 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2709 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2710 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2711 | if (old_iw != h2c->miw) |
| 2712 | h2c_unblock_sfctl(h2c); |
| 2713 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2714 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2718 | * the end. |
| 2719 | */ |
| 2720 | static int h2_process_mux(struct h2c *h2c) |
| 2721 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2722 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2723 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2724 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2725 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2726 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2727 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2728 | if (h2c->st0 == H2_CS_ERROR) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2729 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2730 | goto fail; |
| 2731 | } |
| 2732 | h2c->st0 = H2_CS_SETTINGS1; |
| 2733 | } |
| 2734 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2735 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2736 | return 1; |
| 2737 | } |
| 2738 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2739 | /* start by sending possibly pending window updates */ |
Willy Tarreau | 9c497c2 | 2019-08-06 15:39:32 +0200 | [diff] [blame] | 2740 | if (h2c->rcvd_s > 0 && |
| 2741 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2742 | h2c_send_strm_wu(h2c) < 0) |
| 2743 | goto fail; |
| 2744 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2745 | if (h2c->rcvd_c > 0 && |
| 2746 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2747 | h2c_send_conn_wu(h2c) < 0) |
| 2748 | goto fail; |
| 2749 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2750 | /* First we always process the flow control list because the streams |
| 2751 | * waiting there were already elected for immediate emission but were |
| 2752 | * blocked just on this. |
| 2753 | */ |
| 2754 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2755 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2756 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2757 | h2c->st0 >= H2_CS_ERROR) |
| 2758 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2759 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2760 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2761 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2762 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2763 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2764 | /* For some reason, the upper layer failed to subsribe again, |
| 2765 | * so remove it from the send_list |
| 2766 | */ |
| 2767 | if (!h2s->send_wait) { |
| 2768 | LIST_DEL_INIT(&h2s->list); |
| 2769 | continue; |
| 2770 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2771 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2772 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2773 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2776 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2777 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2778 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2780 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2781 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2782 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2783 | /* For some reason, the upper layer failed to subsribe again, |
| 2784 | * so remove it from the send_list |
| 2785 | */ |
| 2786 | if (!h2s->send_wait) { |
| 2787 | LIST_DEL_INIT(&h2s->list); |
| 2788 | continue; |
| 2789 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2790 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2791 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2792 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2793 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2794 | } |
| 2795 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2796 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2797 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2798 | if (h2c->st0 == H2_CS_ERROR) { |
| 2799 | if (h2c->max_id >= 0) { |
| 2800 | h2c_send_goaway_error(h2c, NULL); |
| 2801 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
| 2805 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2806 | } |
| 2807 | return 1; |
| 2808 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2809 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2810 | } |
| 2811 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2812 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2813 | /* Attempt to read data, and subscribe if none available. |
| 2814 | * The function returns 1 if data has been received, otherwise zero. |
| 2815 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2816 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2817 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2818 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2819 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2820 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2821 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2822 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2823 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2824 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2825 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2826 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2827 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2828 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2829 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2830 | if (!buf) { |
| 2831 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2832 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2833 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2834 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2835 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2836 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2837 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2838 | /* HTX in use : try to pre-align the buffer like the |
| 2839 | * rxbufs will be to optimize memory copies. We'll make |
| 2840 | * sure that the frame header lands at the end of the |
| 2841 | * HTX block to alias it upon recv. We cannot use the |
| 2842 | * head because rcv_buf() will realign the buffer if |
| 2843 | * it's empty. Thus we cheat and pretend we already |
| 2844 | * have a few bytes there. |
| 2845 | */ |
| 2846 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2847 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2848 | } |
| 2849 | else |
| 2850 | max = b_room(buf); |
| 2851 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2852 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2853 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2854 | else |
| 2855 | ret = 0; |
| 2856 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2857 | |
Willy Tarreau | a8fcdac | 2019-08-02 07:48:47 +0200 | [diff] [blame] | 2858 | if (max && !ret && h2_recv_allowed(h2c)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2859 | 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] | 2860 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2861 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2862 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2863 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2864 | } |
| 2865 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2866 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2867 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2868 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2869 | } |
| 2870 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2871 | /* Try to send data if possible. |
| 2872 | * The function returns 1 if data have been sent, otherwise zero. |
| 2873 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2874 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2875 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2876 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2877 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2878 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2879 | |
| 2880 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2881 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2882 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2883 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2884 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2885 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2886 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2887 | } |
| 2888 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2889 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2890 | * pending streams into the existing buffer until it's reportedly full |
| 2891 | * or the end of send requests is reached. Then it tries to send this |
| 2892 | * buffer's contents out, marks it not full if at least one byte could |
| 2893 | * be sent, and tries again. |
| 2894 | * |
| 2895 | * The snd_buf() function normally takes a "flags" argument which may |
| 2896 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2897 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2898 | * connection is streaming lots of data (used to increase TLS record |
| 2899 | * size at the expense of latency). The former can be sent any time |
| 2900 | * there's a buffer full flag, as it indicates at least one stream |
| 2901 | * attempted to send and failed so there are pending data. An |
| 2902 | * alternative would be to set it as long as there's an active stream |
| 2903 | * but that would be problematic for ACKs until we have an absolute |
| 2904 | * guarantee that all waiters have at least one byte to send. The |
| 2905 | * latter should possibly not be set for now. |
| 2906 | */ |
| 2907 | |
| 2908 | done = 0; |
| 2909 | while (!done) { |
| 2910 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2911 | unsigned int released = 0; |
| 2912 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2913 | |
| 2914 | /* fill as much as we can into the current buffer */ |
| 2915 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2916 | done = h2_process_mux(h2c); |
| 2917 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2918 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2919 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2921 | if (conn->flags & CO_FL_ERROR) |
| 2922 | break; |
| 2923 | |
| 2924 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2925 | flags |= CO_SFL_MSG_MORE; |
| 2926 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2927 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2928 | if (b_data(buf)) { |
| 2929 | 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] | 2930 | if (!ret) { |
| 2931 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2932 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2933 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2934 | sent = 1; |
| 2935 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2936 | if (b_data(buf)) { |
| 2937 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2938 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2939 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2940 | } |
| 2941 | b_free(buf); |
| 2942 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2943 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2944 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2945 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2946 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2947 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2948 | /* wrote at least one byte, the buffer is not full anymore */ |
Christopher Faulet | 0742308 | 2019-10-24 10:31:01 +0200 | [diff] [blame] | 2949 | if (sent) |
| 2950 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2951 | } |
| 2952 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2953 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2954 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2955 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2956 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2957 | /* We're not full anymore, so we can wake any task that are waiting |
| 2958 | * for us. |
| 2959 | */ |
Willy Tarreau | c51d47c | 2019-09-25 07:57:31 +0200 | [diff] [blame] | 2960 | 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] | 2961 | struct h2s *h2s; |
| 2962 | |
| 2963 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2964 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2965 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2966 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2967 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2968 | continue; |
| 2969 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2970 | /* For some reason, the upper layer failed to subsribe again, |
| 2971 | * so remove it from the send_list |
| 2972 | */ |
| 2973 | if (!h2s->send_wait) { |
| 2974 | LIST_DEL_INIT(&h2s->list); |
| 2975 | continue; |
| 2976 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2977 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2978 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2979 | tasklet_wakeup(h2s->send_wait->tasklet); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2980 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2981 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2982 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2983 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2984 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2985 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2986 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2987 | 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] | 2988 | 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] | 2989 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2990 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2991 | } |
| 2992 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2993 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2994 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2995 | { |
| 2996 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2997 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2998 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2999 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3000 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3001 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3002 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 3003 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3004 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3005 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3006 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3007 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3008 | /* callback called on any event by the connection handler. |
| 3009 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 3010 | * destruction of the connection (which normally doesn not happen in h2). |
| 3011 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3012 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3013 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3014 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3015 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3016 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3017 | h2_process_demux(h2c); |
| 3018 | |
| 3019 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3020 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3021 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3022 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3023 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 3024 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3025 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3026 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 3027 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 3028 | /* frontend is stopping, reload likely in progress, let's try |
| 3029 | * to announce a graceful shutdown if not yet done. We don't |
| 3030 | * care if it fails, it will be tried again later. |
| 3031 | */ |
| 3032 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3033 | if (h2c->last_sid < 0) |
| 3034 | h2c->last_sid = (1U << 31) - 1; |
| 3035 | h2c_send_goaway_error(h2c, NULL); |
| 3036 | } |
| 3037 | } |
| 3038 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3039 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3040 | * If we received early data, and the handshake is done, wake |
| 3041 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3042 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3043 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 3044 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 3045 | struct eb32_node *node; |
| 3046 | struct h2s *h2s; |
| 3047 | |
| 3048 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 3049 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 3050 | |
| 3051 | while (node) { |
| 3052 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 3053 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 3054 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3055 | node = eb32_next(node); |
| 3056 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3057 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3058 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 3059 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 3060 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 3061 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 3062 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3063 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3064 | |
| 3065 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3066 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3067 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3068 | return -1; |
| 3069 | } |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3070 | |
| 3071 | /* connections in error must be removed from the idle lists */ |
| 3072 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3073 | LIST_DEL_LOCKED(&conn->list); |
| 3074 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3075 | } |
| 3076 | else if (h2c->st0 == H2_CS_ERROR) { |
| 3077 | /* connections in error must be removed from the idle lists */ |
| 3078 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3079 | LIST_DEL_LOCKED(&conn->list); |
| 3080 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3081 | } |
| 3082 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3083 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3084 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3085 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3086 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3087 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 3088 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3089 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3090 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 3091 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 3092 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3093 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 3094 | if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3095 | if (h2c_may_expire(h2c)) |
| 3096 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3097 | else |
| 3098 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3099 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3100 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3101 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3102 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3103 | return 0; |
| 3104 | } |
| 3105 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3106 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3107 | static int h2_wake(struct connection *conn) |
| 3108 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3109 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3110 | |
| 3111 | return (h2_process(h2c)); |
| 3112 | } |
| 3113 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3114 | /* Connection timeout management. The principle is that if there's no receipt |
| 3115 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3116 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3117 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3118 | * GOAWAY frame. |
| 3119 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3120 | 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] | 3121 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3122 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3123 | int expired = tick_is_expired(t->expire, now_ms); |
| 3124 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3125 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3126 | return t; |
| 3127 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3128 | if (h2c && !h2c_may_expire(h2c)) { |
| 3129 | /* we do still have streams but all of them are idle, waiting |
| 3130 | * for the data layer, so we must not enforce the timeout here. |
| 3131 | */ |
| 3132 | t->expire = TICK_ETERNITY; |
| 3133 | return t; |
| 3134 | } |
| 3135 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3136 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3137 | |
| 3138 | if (!h2c) { |
| 3139 | /* resources were already deleted */ |
| 3140 | return NULL; |
| 3141 | } |
| 3142 | |
| 3143 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3144 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3145 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3146 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3147 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3148 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3149 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3150 | } |
| 3151 | |
| 3152 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3153 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3154 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3155 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3156 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3157 | 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] | 3158 | unsigned int released = 0; |
| 3159 | struct buffer *buf; |
| 3160 | |
| 3161 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3162 | if (b_data(buf)) { |
| 3163 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3164 | if (!ret) |
| 3165 | break; |
| 3166 | b_del(buf, ret); |
| 3167 | if (b_data(buf)) |
| 3168 | break; |
| 3169 | b_free(buf); |
| 3170 | released++; |
| 3171 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3172 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3173 | |
| 3174 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3175 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3176 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3177 | |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3178 | /* in any case this connection must not be considered idle anymore */ |
| 3179 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3180 | LIST_DEL_LOCKED(&h2c->conn->list); |
| 3181 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3182 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3183 | /* either we can release everything now or it will be done later once |
| 3184 | * the last stream closes. |
| 3185 | */ |
| 3186 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3187 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3189 | return NULL; |
| 3190 | } |
| 3191 | |
| 3192 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3193 | /*******************************************/ |
| 3194 | /* functions below are used by the streams */ |
| 3195 | /*******************************************/ |
| 3196 | |
| 3197 | /* |
| 3198 | * Attach a new stream to a connection |
| 3199 | * (Used for outgoing connections) |
| 3200 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3201 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3202 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3203 | struct conn_stream *cs; |
| 3204 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3205 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3206 | |
| 3207 | cs = cs_new(conn); |
| 3208 | if (!cs) |
| 3209 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3210 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3211 | if (!h2s) { |
| 3212 | cs_free(cs); |
| 3213 | return NULL; |
| 3214 | } |
| 3215 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3216 | } |
| 3217 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3218 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3219 | * We have to scan because we may have some orphan streams. It might be |
| 3220 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3221 | * orphans. |
| 3222 | */ |
| 3223 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3224 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3225 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3226 | struct h2s *h2s; |
| 3227 | struct eb32_node *node; |
| 3228 | |
| 3229 | node = eb32_first(&h2c->streams_by_id); |
| 3230 | while (node) { |
| 3231 | h2s = container_of(node, struct h2s, by_id); |
| 3232 | if (h2s->cs) |
| 3233 | return h2s->cs; |
| 3234 | node = eb32_next(node); |
| 3235 | } |
| 3236 | return NULL; |
| 3237 | } |
| 3238 | |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3239 | static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3240 | { |
| 3241 | int ret = 0; |
| 3242 | struct h2c *h2c = conn->ctx; |
| 3243 | |
| 3244 | switch (mux_ctl) { |
| 3245 | case MUX_STATUS: |
| 3246 | /* Only consider the mux to be ready if we're done with |
| 3247 | * the preface and settings, and we had no error. |
| 3248 | */ |
| 3249 | if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR) |
| 3250 | ret |= MUX_STATUS_READY; |
| 3251 | return ret; |
| 3252 | default: |
| 3253 | return -1; |
| 3254 | } |
| 3255 | } |
| 3256 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3257 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3258 | * Destroy the mux and the associated connection, if it is no longer used |
| 3259 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3260 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3261 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3262 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3263 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3264 | 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] | 3265 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3269 | * Detach the stream from the connection and possibly release the connection. |
| 3270 | */ |
| 3271 | static void h2_detach(struct conn_stream *cs) |
| 3272 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3273 | struct h2s *h2s = cs->ctx; |
| 3274 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3275 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3276 | |
| 3277 | cs->ctx = NULL; |
| 3278 | if (!h2s) |
| 3279 | return; |
| 3280 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3281 | /* 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] | 3282 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3283 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3284 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3285 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3286 | /* |
| 3287 | * At this point, the stream_interface is supposed to have called |
| 3288 | * h2_unsubscribe(), so the only way there's still a |
| 3289 | * subscription that came from the stream_interface (as we |
| 3290 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3291 | * without the stream_interface involved) is that we subscribed |
| 3292 | * for sending, we woke the tasklet up and removed the |
| 3293 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3294 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3295 | * run yet. Make sure to handle that by explicitely setting |
| 3296 | * send_wait to NULL, as nothing else will do it for us. |
| 3297 | */ |
| 3298 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3301 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3302 | h2c = h2s->h2c; |
| 3303 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3304 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3305 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3306 | !h2_frt_has_too_many_cs(h2c)) { |
| 3307 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3308 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3309 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3310 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3311 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3312 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3313 | * an ES or RST frame), so orphan it in this case. |
| 3314 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3315 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3316 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3317 | (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] | 3318 | return; |
| 3319 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3320 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3321 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3322 | /* unblock the connection if it was blocked on this |
| 3323 | * stream. |
| 3324 | */ |
| 3325 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3326 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3327 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3328 | } |
| 3329 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3330 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3331 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3332 | if (h2c->flags & H2_CF_IS_BACK && |
| 3333 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3334 | if (!(h2c->conn->flags & |
| 3335 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3336 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3337 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3338 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3339 | h2c->conn->owner = NULL; |
| 3340 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3341 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3342 | /* The server doesn't want it, let's kill the connection right away */ |
| 3343 | h2c->conn->mux->destroy(h2c->conn); |
| 3344 | return; |
| 3345 | } |
| 3346 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3347 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3348 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3349 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3350 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3351 | return; |
| 3352 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3353 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3354 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3355 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3356 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3357 | struct server *srv = objt_server(h2c->conn->target); |
| 3358 | |
| 3359 | if (srv) { |
| 3360 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3361 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3362 | else |
| 3363 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3364 | } |
| 3365 | |
| 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3370 | /* We don't want to close right now unless we're removing the |
| 3371 | * last stream, and either the connection is in error, or it |
| 3372 | * reached the ID already specified in a GOAWAY frame received |
| 3373 | * or sent (as seen by last_sid >= 0). |
| 3374 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3375 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3376 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3377 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3378 | } |
| 3379 | else if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3380 | if (h2c_may_expire(h2c)) |
| 3381 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3382 | else |
| 3383 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3384 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3385 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3388 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3389 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3390 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3391 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3392 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3393 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3394 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3395 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3396 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3397 | /* a connstream may require us to immediately kill the whole connection |
| 3398 | * for example because of a "tcp-request content reject" rule that is |
| 3399 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3400 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3401 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3402 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3403 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3404 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3405 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3406 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3407 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3408 | /* Nothing was never sent for this stream, so reset with |
| 3409 | * REFUSED_STREAM error to let the client retry the |
| 3410 | * request. |
| 3411 | */ |
| 3412 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3413 | } |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3414 | else { |
| 3415 | /* a final response was already provided, we don't want this |
| 3416 | * stream anymore. This may happen when the server responds |
| 3417 | * before the end of an upload and closes quickly (redirect, |
| 3418 | * deny, ...) |
| 3419 | */ |
| 3420 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3421 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3422 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3423 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3424 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3425 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3426 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3427 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3428 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3429 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3430 | done: |
| 3431 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3432 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3433 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3434 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3435 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3436 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3437 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3438 | h2s->send_wait = sw; |
| 3439 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3440 | h2s->send_wait = sw; |
| 3441 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3442 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3443 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3444 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3445 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3446 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3449 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3450 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3451 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3452 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3453 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3454 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3455 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3456 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3457 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3458 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3459 | /* we can cleanly close using an empty data frame only after headers */ |
| 3460 | |
| 3461 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3462 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3463 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3464 | |
| 3465 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3466 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3467 | else |
| 3468 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3469 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3470 | /* a connstream may require us to immediately kill the whole connection |
| 3471 | * for example because of a "tcp-request content reject" rule that is |
| 3472 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3473 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3474 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3475 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3476 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3477 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3478 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3479 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3480 | else { |
| 3481 | /* Nothing was never sent for this stream, so reset with |
| 3482 | * REFUSED_STREAM error to let the client retry the |
| 3483 | * request. |
| 3484 | */ |
| 3485 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3486 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3487 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3488 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3489 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3490 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3491 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3492 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3493 | } |
| 3494 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3495 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3496 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3497 | done: |
| 3498 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3499 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3500 | |
| 3501 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3502 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3503 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3504 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3505 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3506 | h2s->send_wait = sw; |
| 3507 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3508 | h2s->send_wait = sw; |
| 3509 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3510 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3511 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3512 | /* let the handler know we want to shutw */ |
| 3513 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3514 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3515 | } |
| 3516 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3517 | /* 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] | 3518 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3519 | * and prevented the last frame from being emitted. |
| 3520 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3521 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3522 | { |
| 3523 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3524 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3525 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3526 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3527 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3528 | h2_do_shutw(h2s); |
| 3529 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3530 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3531 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3532 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3533 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3534 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3535 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3536 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3537 | if (!h2s->cs) { |
| 3538 | h2s_destroy(h2s); |
| 3539 | if (h2c_is_dead(h2c)) |
| 3540 | h2_release(h2c); |
| 3541 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3542 | } |
| 3543 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3544 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3545 | } |
| 3546 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3547 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3548 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3549 | { |
| 3550 | struct h2s *h2s = cs->ctx; |
| 3551 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3552 | if (cs->flags & CS_FL_KILL_CONN) |
| 3553 | h2s->flags |= H2_SF_KILL_CONN; |
| 3554 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3555 | if (!mode) |
| 3556 | return; |
| 3557 | |
| 3558 | h2_do_shutr(h2s); |
| 3559 | } |
| 3560 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3561 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3562 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3563 | { |
| 3564 | struct h2s *h2s = cs->ctx; |
| 3565 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3566 | if (cs->flags & CS_FL_KILL_CONN) |
| 3567 | h2s->flags |= H2_SF_KILL_CONN; |
| 3568 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3569 | h2_do_shutw(h2s); |
| 3570 | } |
| 3571 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3572 | /* 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] | 3573 | * HTX request or response depending on the connection's side. Returns a |
| 3574 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3575 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3576 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3577 | * 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] | 3578 | * |
| 3579 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3580 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3581 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3582 | * leaving a hole between the main frame and the beginning of the next one. |
| 3583 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3584 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3585 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3586 | * |
| 3587 | * A buffer at the beginning of processing may look like this : |
| 3588 | * |
| 3589 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3590 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3591 | * `---^---------^-----^--------------^--------------^------^---' |
| 3592 | * | | <-----> | | |
| 3593 | * area | dpl | wrap |
| 3594 | * |<--------------> | |
| 3595 | * | dfl | |
| 3596 | * |<-------------------------------------------------->| |
| 3597 | * head data |
| 3598 | * |
| 3599 | * Padding is automatically overwritten when folding, participating to the |
| 3600 | * hole size after dfl : |
| 3601 | * |
| 3602 | * ,---.------------------------.-----.--------------.------.---. |
| 3603 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3604 | * `---^------------------------^-----^--------------^------^---' |
| 3605 | * | | <-----> | | |
| 3606 | * area | hole | wrap |
| 3607 | * |<-----------------------> | |
| 3608 | * | dfl | |
| 3609 | * |<-------------------------------------------------->| |
| 3610 | * head data |
| 3611 | * |
| 3612 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3613 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3614 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3615 | * |
| 3616 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3617 | * so that the function can update the following flags : |
| 3618 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3619 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3620 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3621 | * |
| 3622 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3623 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3624 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3625 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3626 | 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] | 3627 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3628 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3629 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3630 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3631 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3632 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3633 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3634 | int flen; // header frame len |
| 3635 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3636 | int ret = 0; |
| 3637 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3638 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3639 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3640 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3641 | next_frame: |
| 3642 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3643 | goto leave; // incomplete input frame |
| 3644 | |
| 3645 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3646 | * this case, we'll try to paste it immediately after the initial |
| 3647 | * HEADERS frame payload and kill any possible padding. The initial |
| 3648 | * frame's length will be increased to represent the concatenation |
| 3649 | * of the two frames. The next frame is read from position <tlen> |
| 3650 | * and written at position <flen> (minus padding if some is present). |
| 3651 | */ |
| 3652 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3653 | struct h2_fh hdr; |
| 3654 | int clen; // CONTINUATION frame's payload length |
| 3655 | |
| 3656 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3657 | /* no more data, the buffer may be full, either due to |
| 3658 | * too large a frame or because of too large a hole that |
| 3659 | * we're going to compact at the end. |
| 3660 | */ |
| 3661 | goto leave; |
| 3662 | } |
| 3663 | |
| 3664 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3665 | /* RFC7540#6.10: frame of unexpected type */ |
| 3666 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3667 | goto fail; |
| 3668 | } |
| 3669 | |
| 3670 | if (hdr.sid != h2c->dsi) { |
| 3671 | /* RFC7540#6.10: frame of different stream */ |
| 3672 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3673 | goto fail; |
| 3674 | } |
| 3675 | |
| 3676 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3677 | /* RFC7540#4.2: invalid frame length */ |
| 3678 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3679 | goto fail; |
| 3680 | } |
| 3681 | |
| 3682 | /* detect when we must stop aggragating frames */ |
| 3683 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3684 | |
| 3685 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3686 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3687 | if (clen > hdr.len) |
| 3688 | clen = hdr.len; |
| 3689 | |
| 3690 | /* Move the frame's payload over the padding, hole and frame |
| 3691 | * header. At least one of hole or dpl is null (see diagrams |
| 3692 | * above). The hole moves after the new aggragated frame. |
| 3693 | */ |
| 3694 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3695 | h2c->dfl += clen - h2c->dpl; |
| 3696 | hole += h2c->dpl + 9; |
| 3697 | h2c->dpl = 0; |
| 3698 | goto next_frame; |
| 3699 | } |
| 3700 | |
| 3701 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3702 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3703 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3704 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3705 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3706 | copy = alloc_trash_chunk(); |
| 3707 | if (!copy) { |
| 3708 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3709 | goto fail; |
| 3710 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3711 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3712 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3713 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3716 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3717 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3718 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3719 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3720 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3721 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3722 | } |
| 3723 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3724 | if (flen < 5) { |
| 3725 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3726 | goto fail; |
| 3727 | } |
| 3728 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3729 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3730 | flen -= 5; |
| 3731 | } |
| 3732 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3733 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3734 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3735 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3736 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3737 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3738 | /* we can't retry a failed decompression operation so we must be very |
| 3739 | * careful not to take any risks. In practice the output buffer is |
| 3740 | * always empty except maybe for trailers, in which case we simply have |
| 3741 | * to wait for the upper layer to finish consuming what is available. |
| 3742 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3743 | |
| 3744 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3745 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3746 | if (!htx_is_empty(htx)) { |
| 3747 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3748 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3749 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3750 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3751 | if (b_data(rxbuf)) { |
| 3752 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3753 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3754 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3755 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3756 | rxbuf->head = 0; |
| 3757 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3758 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3759 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3760 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3761 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3762 | sizeof(list)/sizeof(list[0]), tmp); |
| 3763 | if (outlen < 0) { |
| 3764 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3765 | goto fail; |
| 3766 | } |
| 3767 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3768 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3769 | * the parser's state to commit these changes and allow us to later |
| 3770 | * fail solely on the stream if needed. |
| 3771 | */ |
| 3772 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3773 | h2c->dfl = hole = 0; |
| 3774 | h2c->st0 = H2_CS_FRAME_H; |
| 3775 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3776 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3777 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3778 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3779 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3780 | goto trailers; |
| 3781 | |
| 3782 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3783 | if (htx) { |
| 3784 | /* HTX mode */ |
| 3785 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3786 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3787 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3788 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3789 | } else { |
| 3790 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3791 | 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] | 3792 | if (outlen > 0) |
| 3793 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3794 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3795 | |
| 3796 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3797 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3798 | goto fail; |
| 3799 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3800 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3801 | if (msgf & H2_MSGF_BODY) { |
| 3802 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3803 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3804 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3805 | if (htx) |
| 3806 | htx->extra = *body_len; |
| 3807 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3808 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3809 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3810 | } |
| 3811 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3812 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3813 | /* indicate that a HEADERS frame was received for this stream, except |
| 3814 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3815 | * expected. |
| 3816 | */ |
| 3817 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3818 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3819 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3820 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3821 | /* Mark the end of message, either using EOM in HTX or with the |
| 3822 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3823 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3824 | * we must not leave an HTX trailers block not followed by an |
| 3825 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3826 | * 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] | 3827 | */ |
| 3828 | if (htx) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 3829 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3830 | goto fail; |
| 3831 | } |
| 3832 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3833 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3834 | goto fail; |
| 3835 | } |
| 3836 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3837 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3838 | /* success */ |
| 3839 | ret = 1; |
| 3840 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3841 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3842 | /* 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] | 3843 | * move the remaining data over it. |
| 3844 | */ |
| 3845 | if (hole) { |
| 3846 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3847 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3848 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3849 | b_sub(&h2c->dbuf, hole); |
| 3850 | } |
| 3851 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3852 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3853 | /* too large frames */ |
| 3854 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3855 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3856 | } |
| 3857 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3858 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3859 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3860 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3861 | return ret; |
| 3862 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3863 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3864 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3865 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3866 | |
| 3867 | trailers: |
| 3868 | /* This is the last HEADERS frame hence a trailer */ |
| 3869 | |
| 3870 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3871 | /* It's a trailer but it's missing ES flag */ |
| 3872 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3873 | goto fail; |
| 3874 | } |
| 3875 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 3876 | /* Trailers terminate a DATA sequence. In HTX we always handle them. In |
| 3877 | * legacy, when using chunks, we have to emit the 0 CRLF marker first |
| 3878 | * and then handle the trailers. For other modes, the trailers are |
| 3879 | * silently dropped. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3880 | */ |
| 3881 | if (htx) { |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3882 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3883 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3884 | } |
| 3885 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3886 | /* Legacy mode with chunked encoding : we must finalize the |
| 3887 | * data block message emit the trailing CRLF */ |
| 3888 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3889 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3890 | |
| 3891 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3892 | if (outlen > 0) |
| 3893 | b_add(rxbuf, outlen); |
| 3894 | else |
| 3895 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3901 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3902 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3903 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3904 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3905 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3906 | * 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] | 3907 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3908 | * send the current frame, 0 if it couldn't complete, in which case |
| 3909 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3910 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3911 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3912 | * have checked the frame header and ensured that the frame was complete or the |
| 3913 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3914 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3915 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3916 | { |
| 3917 | struct h2c *h2c = h2s->h2c; |
| 3918 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3919 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3920 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3921 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3922 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3923 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3924 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3925 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3926 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3927 | if (!csbuf) { |
| 3928 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3929 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3930 | } |
| 3931 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3932 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3933 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3934 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3935 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3936 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3937 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3938 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3939 | if (flen > b_data(&h2c->dbuf)) { |
| 3940 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3941 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3942 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3943 | } |
| 3944 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3945 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3946 | unsigned int sent; |
| 3947 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3948 | block1 = htx_free_data_space(htx); |
| 3949 | if (!block1) { |
| 3950 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3951 | goto fail; |
| 3952 | } |
| 3953 | if (flen > block1) |
| 3954 | flen = block1; |
| 3955 | |
| 3956 | /* here, flen is the max we can copy into the output buffer */ |
| 3957 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3958 | if (flen > block1) |
| 3959 | flen = block1; |
| 3960 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3961 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3962 | |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3963 | b_del(&h2c->dbuf, sent); |
| 3964 | h2c->dfl -= sent; |
| 3965 | h2c->rcvd_c += sent; |
| 3966 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3967 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3968 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3969 | h2s->body_len -= sent; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3970 | htx->extra = h2s->body_len; |
| 3971 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3972 | |
| 3973 | if (sent < flen) { |
| 3974 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3975 | goto fail; |
| 3976 | } |
| 3977 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3978 | goto try_again; |
| 3979 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3980 | else if (unlikely(b_space_wraps(csbuf) && |
| 3981 | flen + chklen <= b_room(csbuf) && |
| 3982 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3983 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3984 | * not too many data in the buffer, let's defragment it and try |
| 3985 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3986 | */ |
| 3987 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3988 | } |
| 3989 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3990 | /* chunked-encoding requires more room */ |
| 3991 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3992 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3993 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3994 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3995 | (chklen < 1048576) ? 4 : 8; |
| 3996 | chklen += 4; // CRLF, CRLF |
| 3997 | } |
| 3998 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3999 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4000 | if (flen + chklen > b_room(csbuf)) { |
| 4001 | if (chklen >= b_room(csbuf)) { |
| 4002 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4003 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4004 | } |
| 4005 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4009 | /* emit the chunk size */ |
| 4010 | unsigned int chksz = flen; |
| 4011 | char str[10]; |
| 4012 | char *beg; |
| 4013 | |
| 4014 | beg = str + sizeof(str); |
| 4015 | *--beg = '\n'; |
| 4016 | *--beg = '\r'; |
| 4017 | do { |
| 4018 | *--beg = hextab[chksz & 0xF]; |
| 4019 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4020 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4021 | } |
| 4022 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4023 | /* Block1 is the length of the first block before the buffer wraps, |
| 4024 | * block2 is the optional second block to reach the end of the frame. |
| 4025 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4026 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4027 | if (block1 > flen) |
| 4028 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4029 | block2 = flen - block1; |
| 4030 | |
| 4031 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4032 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4033 | |
| 4034 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4035 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4036 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4037 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4038 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4039 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4040 | } |
| 4041 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4042 | /* now mark the input data as consumed (will be deleted from the buffer |
| 4043 | * by the caller when seeing FRAME_A after sending the window update). |
| 4044 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4045 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4046 | h2c->dfl -= flen; |
| 4047 | h2c->rcvd_c += flen; |
| 4048 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 4049 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 4050 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 4051 | h2s->body_len -= flen; |
| 4052 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4053 | if (h2c->dfl > h2c->dpl) { |
| 4054 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4055 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4056 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 4059 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4060 | /* here we're done with the frame, all the payload (except padding) was |
| 4061 | * transferred. |
| 4062 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4063 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4064 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 4065 | if (htx) { |
| 4066 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 4067 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4068 | goto fail; |
| 4069 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4070 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4071 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4072 | /* emit the trailing 0 CRLF CRLF */ |
| 4073 | if (b_room(csbuf) < 5) { |
| 4074 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4075 | goto fail; |
| 4076 | } |
| 4077 | chklen += 5; |
| 4078 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 4079 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4080 | } |
| 4081 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 4082 | h2c->rcvd_c += h2c->dpl; |
| 4083 | h2c->rcvd_s += h2c->dpl; |
| 4084 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4085 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4086 | if (htx) |
| 4087 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4088 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4089 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4090 | if (htx) |
| 4091 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4092 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4093 | } |
| 4094 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4095 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 4096 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 4097 | * number of bytes sent. The caller must check the stream's status to detect |
| 4098 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4099 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4100 | 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] | 4101 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4102 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4103 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4104 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4105 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4106 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4107 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4108 | int es_now = 0; |
| 4109 | int ret = 0; |
| 4110 | int hdr; |
| 4111 | |
| 4112 | if (h2c_mux_busy(h2c, h2s)) { |
| 4113 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4114 | return 0; |
| 4115 | } |
| 4116 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4117 | /* First, try to parse the H1 response and index it into <list>. |
| 4118 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 4119 | * block does not wrap and we can safely read it this way without |
| 4120 | * having to realign the buffer. |
| 4121 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4122 | 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] | 4123 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4124 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 4125 | /* incomplete or invalid response, this is abnormal coming from |
| 4126 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 4127 | * so that won't be fixed, raise an error now. |
| 4128 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4129 | * FIXME: we should instead add the ability to only return a |
| 4130 | * 502 bad gateway. But in theory this is not supposed to |
| 4131 | * happen. |
| 4132 | */ |
| 4133 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4134 | ret = 0; |
| 4135 | goto end; |
| 4136 | } |
| 4137 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4138 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4139 | |
| 4140 | /* certain statuses have no body or an empty one, regardless of |
| 4141 | * what the headers say. |
| 4142 | */ |
| 4143 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4144 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4145 | h1m->curr_len = h1m->body_len = 0; |
| 4146 | } |
| 4147 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4148 | /* no contents, claim c-len is present and set to zero */ |
| 4149 | h1m->flags &= ~H1_MF_CHNK; |
| 4150 | h1m->flags |= H1_MF_CLEN; |
| 4151 | h1m->curr_len = h1m->body_len = 0; |
| 4152 | } |
| 4153 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4154 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4155 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4156 | if (!h2_get_buf(h2c, mbuf)) { |
| 4157 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4158 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4162 | chunk_reset(&outbuf); |
| 4163 | |
| 4164 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4165 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4166 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4167 | break; |
| 4168 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4169 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4170 | } |
| 4171 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4172 | if (outbuf.size < 9) |
| 4173 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4174 | |
| 4175 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4176 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4177 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4178 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4179 | |
| 4180 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4181 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4182 | /* this is an unparsable response */ |
| 4183 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4184 | ret = 0; |
| 4185 | goto end; |
| 4186 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4187 | |
| 4188 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4189 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4190 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4191 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /* encode all headers, stop at empty name */ |
| 4195 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4196 | /* these ones do not exist in H2 and must be dropped. */ |
| 4197 | if (isteq(list[hdr].n, ist("connection")) || |
| 4198 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4199 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4200 | isteq(list[hdr].n, ist("upgrade")) || |
| 4201 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4202 | continue; |
| 4203 | |
| 4204 | if (isteq(list[hdr].n, ist(""))) |
| 4205 | break; // end |
| 4206 | |
| 4207 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4208 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4209 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4210 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4211 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4216 | 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] | 4217 | es_now = 1; |
| 4218 | |
| 4219 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4220 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4221 | |
| 4222 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4223 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4224 | |
| 4225 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4226 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4227 | |
| 4228 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4229 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4230 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4231 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4232 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4233 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4234 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4235 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4236 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4237 | h2s->flags |= H2_SF_ES_SENT; |
| 4238 | if (h2s->st == H2_SS_OPEN) |
| 4239 | h2s->st = H2_SS_HLOC; |
| 4240 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4241 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4242 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4243 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4244 | /* 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] | 4245 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4246 | 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] | 4247 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4248 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4249 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4250 | |
| 4251 | /* 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] | 4252 | |
| 4253 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4254 | //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] | 4255 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4256 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4257 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4258 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4259 | h1m_init_res(h1m); |
| 4260 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4261 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4262 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4263 | ret = 0; |
| 4264 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4265 | } |
| 4266 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4267 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4268 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4269 | * the number of bytes sent. The caller must check the stream's status to |
| 4270 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4271 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4272 | 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] | 4273 | { |
| 4274 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4275 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4276 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4277 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4278 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4279 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4280 | int es_now = 0; |
| 4281 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4282 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4283 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4284 | |
| 4285 | if (h2c_mux_busy(h2c, h2s)) { |
| 4286 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4287 | goto end; |
| 4288 | } |
| 4289 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4290 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4291 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4292 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4293 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4294 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4295 | goto end; |
| 4296 | } |
| 4297 | |
| 4298 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4299 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4300 | goto end; |
| 4301 | |
| 4302 | chunk_reset(&outbuf); |
| 4303 | |
| 4304 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4305 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4306 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4307 | break; |
| 4308 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4309 | /* If there are pending data in the output buffer, and we have |
| 4310 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4311 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4312 | * is full and wait, to save some slow realign calls. |
| 4313 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4314 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4315 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4316 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4317 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4318 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4319 | goto end; |
| 4320 | } |
| 4321 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4322 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4326 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4327 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4328 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4329 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4330 | goto end; |
| 4331 | } |
| 4332 | |
| 4333 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4334 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4335 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4336 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4337 | |
| 4338 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4339 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4340 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4341 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4342 | break; |
| 4343 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4344 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4345 | if ((long long)size > h1m->curr_len) |
| 4346 | size = h1m->curr_len; |
| 4347 | break; |
| 4348 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4349 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4350 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4351 | if (!ret) |
| 4352 | goto end; |
| 4353 | |
| 4354 | if (ret < 0) { |
| 4355 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4356 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4357 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4358 | goto end; |
| 4359 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4360 | max -= ret; |
| 4361 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4362 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4363 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4364 | } |
| 4365 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4366 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4367 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4368 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4369 | if (!ret) |
| 4370 | goto end; |
| 4371 | |
| 4372 | if (ret < 0) { |
| 4373 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4374 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4375 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4376 | goto end; |
| 4377 | } |
| 4378 | |
| 4379 | size = chunk; |
| 4380 | h1m->curr_len = chunk; |
| 4381 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4382 | max -= ret; |
| 4383 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4384 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4385 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4386 | if (!size) |
| 4387 | goto send_empty; |
| 4388 | } |
| 4389 | |
| 4390 | /* in MSG_DATA state, continue below */ |
| 4391 | size = h1m->curr_len; |
| 4392 | break; |
| 4393 | } |
| 4394 | |
| 4395 | /* we have in <size> the exact number of bytes we need to copy from |
| 4396 | * the H1 buffer. We need to check this against the connection's and |
| 4397 | * the stream's send windows, and to ensure that this fits in the max |
| 4398 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4399 | * the frame header). The connection's flow control is applied last so |
| 4400 | * that we can use a separate list of streams which are immediately |
| 4401 | * unblocked on window opening. Note: we don't implement padding. |
| 4402 | */ |
| 4403 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4404 | if (size > max) |
| 4405 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4406 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4407 | if (size > h2s_mws(h2s)) |
| 4408 | size = h2s_mws(h2s); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4409 | |
| 4410 | if (size <= 0) { |
| 4411 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4412 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4413 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 55dc084 | 2019-10-22 10:04:39 +0200 | [diff] [blame] | 4414 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4415 | goto end; |
| 4416 | } |
| 4417 | |
| 4418 | if (h2c->mfs && size > h2c->mfs) |
| 4419 | size = h2c->mfs; |
| 4420 | |
| 4421 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4422 | /* It doesn't fit at once. If it at least fits once split and |
| 4423 | * the amount of data to move is low, let's defragment the |
| 4424 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4425 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4426 | if (b_space_wraps(mbuf) && |
| 4427 | (size + 9 <= b_room(mbuf)) && |
| 4428 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4429 | goto realign_again; |
| 4430 | size = outbuf.size - 9; |
| 4431 | } |
| 4432 | |
| 4433 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4434 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4435 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4436 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4437 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4438 | goto end; |
| 4439 | } |
| 4440 | |
| 4441 | if (size > h2c->mws) |
| 4442 | size = h2c->mws; |
| 4443 | |
| 4444 | if (size <= 0) { |
| 4445 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4446 | goto end; |
| 4447 | } |
| 4448 | |
| 4449 | /* copy whatever we can */ |
| 4450 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4451 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4452 | if (ret == 1) |
| 4453 | len2 = 0; |
| 4454 | |
| 4455 | if (!ret || len1 + len2 < size) { |
| 4456 | /* FIXME: must normally never happen */ |
| 4457 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4458 | goto end; |
| 4459 | } |
| 4460 | |
| 4461 | /* limit len1/len2 to size */ |
| 4462 | if (len1 + len2 > size) { |
| 4463 | int sub = len1 + len2 - size; |
| 4464 | |
| 4465 | if (len2 > sub) |
| 4466 | len2 -= sub; |
| 4467 | else { |
| 4468 | sub -= len2; |
| 4469 | len2 = 0; |
| 4470 | len1 -= sub; |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4475 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4476 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4477 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4478 | |
| 4479 | send_empty: |
| 4480 | /* we may need to add END_STREAM */ |
| 4481 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4482 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4483 | * |
| 4484 | * FIXME: what we do here is not correct because we send end_stream |
| 4485 | * before knowing if we'll have to send a HEADERS frame for the |
| 4486 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4487 | * after the end of trailers, so it will be left to the caller to |
| 4488 | * eat it. The right way to do it would be to measure trailers here |
| 4489 | * and to send ES only if there are no trailers. |
| 4490 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4491 | */ |
| 4492 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4493 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4494 | es_now = 1; |
| 4495 | |
| 4496 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4497 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4498 | |
| 4499 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4500 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4501 | |
| 4502 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4503 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4504 | |
| 4505 | /* consume incoming H1 response */ |
| 4506 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4507 | max -= size; |
| 4508 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4509 | total += size; |
| 4510 | h1m->curr_len -= size; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4511 | h2s->sws -= size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4512 | h2c->mws -= size; |
| 4513 | |
| 4514 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4515 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4516 | goto new_frame; |
| 4517 | } |
| 4518 | } |
| 4519 | |
| 4520 | if (es_now) { |
| 4521 | if (h2s->st == H2_SS_OPEN) |
| 4522 | h2s->st = H2_SS_HLOC; |
| 4523 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4524 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4525 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4526 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4527 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4528 | total += max; |
| 4529 | ofs += max; |
| 4530 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4531 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4532 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4533 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4534 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4535 | h2s->flags |= H2_SF_ES_SENT; |
| 4536 | } |
| 4537 | |
| 4538 | end: |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4539 | 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] | 4540 | return total; |
| 4541 | } |
| 4542 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4543 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4544 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4545 | * must check the stream's status to detect any error which might have happened |
| 4546 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4547 | * from the message. The htx message is assumed to be valid since produced from |
| 4548 | * the internal code, hence it contains a start line, an optional series of |
| 4549 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4550 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4551 | */ |
| 4552 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4553 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4554 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4555 | struct h2c *h2c = h2s->h2c; |
| 4556 | struct htx_blk *blk; |
| 4557 | struct htx_blk *blk_end; |
| 4558 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4559 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4560 | struct htx_sl *sl; |
| 4561 | enum htx_blk_type type; |
| 4562 | int es_now = 0; |
| 4563 | int ret = 0; |
| 4564 | int hdr; |
| 4565 | int idx; |
| 4566 | |
| 4567 | if (h2c_mux_busy(h2c, h2s)) { |
| 4568 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4569 | return 0; |
| 4570 | } |
| 4571 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4572 | /* determine the first block which must not be deleted, blk_end may |
| 4573 | * be NULL if all blocks have to be deleted. |
| 4574 | */ |
| 4575 | idx = htx_get_head(htx); |
| 4576 | blk_end = NULL; |
| 4577 | while (idx != -1) { |
| 4578 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4579 | idx = htx_get_next(htx, idx); |
| 4580 | if (type == HTX_BLK_EOH) { |
| 4581 | if (idx != -1) |
| 4582 | blk_end = htx_get_blk(htx, idx); |
| 4583 | break; |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4588 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4589 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4590 | ALREADY_CHECKED(blk); |
| 4591 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4592 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4593 | if (h2s->status < 100 || h2s->status > 999) |
| 4594 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4595 | |
| 4596 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4597 | hdr = 0; |
| 4598 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4599 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4600 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4601 | blk = htx_get_blk(htx, idx); |
| 4602 | type = htx_get_blk_type(blk); |
| 4603 | |
| 4604 | if (type == HTX_BLK_UNUSED) |
| 4605 | continue; |
| 4606 | |
| 4607 | if (type != HTX_BLK_HDR) |
| 4608 | break; |
| 4609 | |
| 4610 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4611 | goto fail; |
| 4612 | |
| 4613 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4614 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4615 | hdr++; |
| 4616 | } |
| 4617 | |
| 4618 | /* marker for end of headers */ |
| 4619 | list[hdr].n = ist(""); |
| 4620 | |
| 4621 | if (h2s->status == 204 || h2s->status == 304) { |
| 4622 | /* no contents, claim c-len is present and set to zero */ |
| 4623 | es_now = 1; |
| 4624 | } |
| 4625 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4626 | mbuf = br_tail(h2c->mbuf); |
| 4627 | retry: |
| 4628 | if (!h2_get_buf(h2c, mbuf)) { |
| 4629 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4630 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4631 | return 0; |
| 4632 | } |
| 4633 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4634 | chunk_reset(&outbuf); |
| 4635 | |
| 4636 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4637 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4638 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4639 | break; |
| 4640 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4641 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | if (outbuf.size < 9) |
| 4645 | goto full; |
| 4646 | |
| 4647 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4648 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4649 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4650 | outbuf.data = 9; |
| 4651 | |
| 4652 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4653 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4654 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4655 | goto realign_again; |
| 4656 | goto full; |
| 4657 | } |
| 4658 | |
| 4659 | /* encode all headers, stop at empty name */ |
| 4660 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4661 | /* these ones do not exist in H2 and must be dropped. */ |
| 4662 | if (isteq(list[hdr].n, ist("connection")) || |
| 4663 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4664 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4665 | isteq(list[hdr].n, ist("upgrade")) || |
| 4666 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4667 | continue; |
| 4668 | |
| 4669 | if (isteq(list[hdr].n, ist(""))) |
| 4670 | break; // end |
| 4671 | |
| 4672 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4673 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4674 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4675 | goto realign_again; |
| 4676 | goto full; |
| 4677 | } |
| 4678 | } |
| 4679 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4680 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4681 | * FIXME: we should also set it when we know for sure that the |
| 4682 | * content-length is zero as well as on 204/304 |
| 4683 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4684 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4685 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4686 | es_now = 1; |
| 4687 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4688 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4689 | es_now = 1; |
| 4690 | |
| 4691 | /* update the frame's size */ |
| 4692 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4693 | |
| 4694 | if (es_now) |
| 4695 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4696 | |
| 4697 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4698 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4699 | |
| 4700 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4701 | * 1xx responses, another HEADERS frame is expected. |
| 4702 | */ |
| 4703 | if (h2s->status >= 200 || h2s->status == 101) |
| 4704 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4705 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4706 | if (es_now) { |
| 4707 | h2s->flags |= H2_SF_ES_SENT; |
| 4708 | if (h2s->st == H2_SS_OPEN) |
| 4709 | h2s->st = H2_SS_HLOC; |
| 4710 | else |
| 4711 | h2s_close(h2s); |
| 4712 | } |
| 4713 | |
| 4714 | /* OK we could properly deliver the response */ |
| 4715 | |
| 4716 | /* remove all header blocks including the EOH and compute the |
| 4717 | * corresponding size. |
| 4718 | * |
| 4719 | * FIXME: We should remove everything when es_now is set. |
| 4720 | */ |
| 4721 | ret = 0; |
| 4722 | idx = htx_get_head(htx); |
| 4723 | blk = htx_get_blk(htx, idx); |
| 4724 | while (blk != blk_end) { |
| 4725 | ret += htx_get_blksz(blk); |
| 4726 | blk = htx_remove_blk(htx, blk); |
| 4727 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4728 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4729 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4730 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4731 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4732 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4733 | end: |
| 4734 | return ret; |
| 4735 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4736 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4737 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4738 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4739 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4740 | ret = 0; |
| 4741 | goto end; |
| 4742 | fail: |
| 4743 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4744 | * list etc go here (unrecoverable errors). |
| 4745 | */ |
| 4746 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4747 | ret = 0; |
| 4748 | goto end; |
| 4749 | } |
| 4750 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4751 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4752 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4753 | * must check the stream's status to detect any error which might have happened |
| 4754 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4755 | * from the message. The htx message is assumed to be valid since produced from |
| 4756 | * the internal code, hence it contains a start line, an optional series of |
| 4757 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4758 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4759 | */ |
| 4760 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4761 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4762 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4763 | struct h2c *h2c = h2s->h2c; |
| 4764 | struct htx_blk *blk; |
| 4765 | struct htx_blk *blk_end; |
| 4766 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4767 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4768 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4769 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4770 | enum htx_blk_type type; |
| 4771 | int es_now = 0; |
| 4772 | int ret = 0; |
| 4773 | int hdr; |
| 4774 | int idx; |
| 4775 | |
| 4776 | if (h2c_mux_busy(h2c, h2s)) { |
| 4777 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4778 | return 0; |
| 4779 | } |
| 4780 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4781 | /* determine the first block which must not be deleted, blk_end may |
| 4782 | * be NULL if all blocks have to be deleted. |
| 4783 | */ |
| 4784 | idx = htx_get_head(htx); |
| 4785 | blk_end = NULL; |
| 4786 | while (idx != -1) { |
| 4787 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4788 | idx = htx_get_next(htx, idx); |
| 4789 | if (type == HTX_BLK_EOH) { |
| 4790 | if (idx != -1) |
| 4791 | blk_end = htx_get_blk(htx, idx); |
| 4792 | break; |
| 4793 | } |
| 4794 | } |
| 4795 | |
| 4796 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4797 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4798 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4799 | ALREADY_CHECKED(blk); |
| 4800 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4801 | meth = htx_sl_req_meth(sl); |
| 4802 | path = htx_sl_req_uri(sl); |
| 4803 | |
| 4804 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4805 | hdr = 0; |
| 4806 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4807 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4808 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4809 | blk = htx_get_blk(htx, idx); |
| 4810 | type = htx_get_blk_type(blk); |
| 4811 | |
| 4812 | if (type == HTX_BLK_UNUSED) |
| 4813 | continue; |
| 4814 | |
| 4815 | if (type != HTX_BLK_HDR) |
| 4816 | break; |
| 4817 | |
| 4818 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4819 | goto fail; |
| 4820 | |
| 4821 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4822 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4823 | hdr++; |
| 4824 | } |
| 4825 | |
| 4826 | /* marker for end of headers */ |
| 4827 | list[hdr].n = ist(""); |
| 4828 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4829 | mbuf = br_tail(h2c->mbuf); |
| 4830 | retry: |
| 4831 | if (!h2_get_buf(h2c, mbuf)) { |
| 4832 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4833 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4834 | return 0; |
| 4835 | } |
| 4836 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4837 | chunk_reset(&outbuf); |
| 4838 | |
| 4839 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4840 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4841 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4842 | break; |
| 4843 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4844 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | if (outbuf.size < 9) |
| 4848 | goto full; |
| 4849 | |
| 4850 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4851 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4852 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4853 | outbuf.data = 9; |
| 4854 | |
| 4855 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4856 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4857 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4858 | goto realign_again; |
| 4859 | goto full; |
| 4860 | } |
| 4861 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4862 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4863 | * - :authority set to the URI part (host:port) |
| 4864 | * - :method set to CONNECT |
| 4865 | * - :scheme and :path omitted |
| 4866 | */ |
| 4867 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4868 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4869 | struct ist scheme; |
| 4870 | |
| 4871 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4872 | scheme = ist("http"); |
| 4873 | else |
| 4874 | scheme = ist("https"); |
| 4875 | |
| 4876 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4877 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4878 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4879 | goto realign_again; |
| 4880 | goto full; |
| 4881 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4882 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4883 | /* encode the path, which necessarily is the second one */ |
| 4884 | if (!hpack_encode_path(&outbuf, path)) { |
| 4885 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4886 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4887 | goto realign_again; |
| 4888 | goto full; |
| 4889 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4890 | |
| 4891 | /* look for the Host header and place it in :authority */ |
| 4892 | auth = ist2(NULL, 0); |
| 4893 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4894 | if (isteq(list[hdr].n, ist(""))) |
| 4895 | break; // end |
| 4896 | |
| 4897 | if (isteq(list[hdr].n, ist("host"))) { |
| 4898 | auth = list[hdr].v; |
| 4899 | break; |
| 4900 | } |
| 4901 | } |
| 4902 | } |
| 4903 | else { |
| 4904 | /* for CONNECT, :authority is taken from the path */ |
| 4905 | auth = path; |
| 4906 | } |
| 4907 | |
| 4908 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4909 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4910 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4911 | goto realign_again; |
| 4912 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | /* encode all headers, stop at empty name */ |
| 4916 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4917 | /* these ones do not exist in H2 and must be dropped. */ |
| 4918 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4919 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4920 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4921 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4922 | isteq(list[hdr].n, ist("upgrade")) || |
| 4923 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4924 | continue; |
| 4925 | |
| 4926 | if (isteq(list[hdr].n, ist(""))) |
| 4927 | break; // end |
| 4928 | |
| 4929 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4930 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4931 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4932 | goto realign_again; |
| 4933 | goto full; |
| 4934 | } |
| 4935 | } |
| 4936 | |
| 4937 | /* we may need to add END_STREAM if we have no body : |
| 4938 | * - request already closed, or : |
| 4939 | * - no transfer-encoding, and : |
| 4940 | * - no content-length or content-length:0 |
| 4941 | * Fixme: this doesn't take into account CONNECT requests. |
| 4942 | */ |
| 4943 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4944 | es_now = 1; |
| 4945 | |
| 4946 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4947 | es_now = 1; |
| 4948 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4949 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4950 | es_now = 1; |
| 4951 | |
| 4952 | /* update the frame's size */ |
| 4953 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4954 | |
| 4955 | if (es_now) |
| 4956 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4957 | |
| 4958 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4959 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4960 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4961 | h2s->st = H2_SS_OPEN; |
| 4962 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4963 | if (es_now) { |
| 4964 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4965 | h2s->flags |= H2_SF_ES_SENT; |
| 4966 | h2s->st = H2_SS_HLOC; |
| 4967 | } |
| 4968 | |
| 4969 | /* remove all header blocks including the EOH and compute the |
| 4970 | * corresponding size. |
| 4971 | * |
| 4972 | * FIXME: We should remove everything when es_now is set. |
| 4973 | */ |
| 4974 | ret = 0; |
| 4975 | idx = htx_get_head(htx); |
| 4976 | blk = htx_get_blk(htx, idx); |
| 4977 | while (blk != blk_end) { |
| 4978 | ret += htx_get_blksz(blk); |
| 4979 | blk = htx_remove_blk(htx, blk); |
| 4980 | } |
| 4981 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4982 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4983 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4984 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4985 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4986 | |
| 4987 | end: |
| 4988 | return ret; |
| 4989 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4990 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4991 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4992 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4993 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4994 | ret = 0; |
| 4995 | goto end; |
| 4996 | fail: |
| 4997 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4998 | * list etc go here (unrecoverable errors). |
| 4999 | */ |
| 5000 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5001 | ret = 0; |
| 5002 | goto end; |
| 5003 | } |
| 5004 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5005 | /* 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] | 5006 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 5007 | * caller must check the stream's status to detect any error which might have |
| 5008 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5009 | * 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] | 5010 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5011 | 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] | 5012 | { |
| 5013 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5014 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5015 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5016 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5017 | size_t total = 0; |
| 5018 | int es_now = 0; |
| 5019 | int bsize; /* htx block size */ |
| 5020 | int fsize; /* h2 frame size */ |
| 5021 | struct htx_blk *blk; |
| 5022 | enum htx_blk_type type; |
| 5023 | int idx; |
| 5024 | |
| 5025 | if (h2c_mux_busy(h2c, h2s)) { |
| 5026 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5027 | goto end; |
| 5028 | } |
| 5029 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5030 | htx = htx_from_buf(buf); |
| 5031 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5032 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 5033 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 5034 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5035 | */ |
| 5036 | |
| 5037 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5038 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5039 | goto end; |
| 5040 | |
| 5041 | idx = htx_get_head(htx); |
| 5042 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5043 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5044 | bsize = htx_get_blksz(blk); |
| 5045 | fsize = bsize; |
| 5046 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5047 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 5048 | if (h2s->flags & H2_SF_ES_SENT) { |
| 5049 | /* ES already sent */ |
| 5050 | htx_remove_blk(htx, blk); |
| 5051 | total++; // EOM counts as one byte |
| 5052 | count--; |
| 5053 | goto end; |
| 5054 | } |
| 5055 | } |
| 5056 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5057 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5058 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5059 | mbuf = br_tail(h2c->mbuf); |
| 5060 | retry: |
| 5061 | if (!h2_get_buf(h2c, mbuf)) { |
| 5062 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5063 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5064 | goto end; |
| 5065 | } |
| 5066 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5067 | /* Perform some optimizations to reduce the number of buffer copies. |
| 5068 | * First, if the mux's buffer is empty and the htx area contains |
| 5069 | * exactly one data block of the same size as the requested count, and |
| 5070 | * this count fits within the frame size, the stream's window size, and |
| 5071 | * the connection's window size, then it's possible to simply swap the |
| 5072 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 5073 | * length to match the entire DATA HTX block in the middle. In this |
| 5074 | * case we perform a true zero-copy operation from end-to-end. This is |
| 5075 | * the situation that happens all the time with large files. Second, if |
| 5076 | * this is not possible, but the mux's output buffer is empty, we still |
| 5077 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 5078 | * making the intermediary buffer's area point to the output buffer's |
| 5079 | * area. In this case we want to skip the HTX header to make sure that |
| 5080 | * copies remain aligned and that this operation remains possible all |
| 5081 | * the time. This goes for headers, data blocks and any data extracted |
| 5082 | * from the HTX blocks. |
| 5083 | */ |
| 5084 | if (unlikely(fsize == count && |
| 5085 | htx->used == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5086 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5087 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5088 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5089 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5090 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 5091 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 5092 | * frame into a buffer containing few data if it needs to be realigned, |
| 5093 | * and that it's also OK to copy few data without realigning. Otherwise |
| 5094 | * 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] | 5095 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5096 | if (fsize + 9 <= b_room(mbuf) && |
| 5097 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 5098 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5099 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5100 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5101 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5102 | goto retry; |
| 5103 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5104 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5105 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5106 | goto end; |
| 5107 | } |
| 5108 | |
| 5109 | /* map an H2 frame to the HTX block so that we can put the |
| 5110 | * frame header there. |
| 5111 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5112 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 5113 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5114 | |
| 5115 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 5116 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5117 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5118 | h2_set_frame_size(outbuf.area, fsize); |
| 5119 | |
| 5120 | /* update windows */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5121 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5122 | h2c->mws -= fsize; |
| 5123 | |
| 5124 | /* and exchange with our old area */ |
| 5125 | buf->area = old_area; |
| 5126 | buf->data = buf->head = 0; |
| 5127 | total += fsize; |
| 5128 | goto end; |
| 5129 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5130 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5131 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5132 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 5133 | |
| 5134 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5135 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5136 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5137 | break; |
| 5138 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5139 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5143 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5144 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5145 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5146 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5147 | goto end; |
| 5148 | } |
| 5149 | |
| 5150 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5151 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5152 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5153 | outbuf.data = 9; |
| 5154 | |
| 5155 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5156 | * the HTX buffer. We need to check this against the connection's and |
| 5157 | * the stream's send windows, and to ensure that this fits in the max |
| 5158 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5159 | * the frame header). The connection's flow control is applied last so |
| 5160 | * that we can use a separate list of streams which are immediately |
| 5161 | * unblocked on window opening. Note: we don't implement padding. |
| 5162 | */ |
| 5163 | |
| 5164 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5165 | * empty frame, thus we force it to zero here. |
| 5166 | */ |
| 5167 | if (type == HTX_BLK_EOM) |
| 5168 | bsize = fsize = 0; |
| 5169 | |
| 5170 | if (!fsize) |
| 5171 | goto send_empty; |
| 5172 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5173 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5174 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5175 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5176 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5177 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5178 | goto end; |
| 5179 | } |
| 5180 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5181 | if (fsize > count) |
| 5182 | fsize = count; |
| 5183 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5184 | if (fsize > h2s_mws(h2s)) |
| 5185 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5186 | |
| 5187 | if (h2c->mfs && fsize > h2c->mfs) |
| 5188 | fsize = h2c->mfs; // >0 |
| 5189 | |
| 5190 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5191 | /* It doesn't fit at once. If it at least fits once split and |
| 5192 | * the amount of data to move is low, let's defragment the |
| 5193 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5194 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5195 | if (b_space_wraps(mbuf) && |
| 5196 | (fsize + 9 <= b_room(mbuf)) && |
| 5197 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5198 | goto realign_again; |
| 5199 | fsize = outbuf.size - 9; |
| 5200 | |
| 5201 | if (fsize <= 0) { |
| 5202 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5203 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5204 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5205 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5206 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5207 | goto end; |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | if (h2c->mws <= 0) { |
| 5212 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5213 | goto end; |
| 5214 | } |
| 5215 | |
| 5216 | if (fsize > h2c->mws) |
| 5217 | fsize = h2c->mws; |
| 5218 | |
| 5219 | /* now let's copy this this into the output buffer */ |
| 5220 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5221 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5222 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5223 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5224 | |
| 5225 | send_empty: |
| 5226 | /* update the frame's size */ |
| 5227 | h2_set_frame_size(outbuf.area, fsize); |
| 5228 | |
| 5229 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5230 | * meeting EOM. We should optimize this later. |
| 5231 | */ |
| 5232 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5233 | total++; // EOM counts as one byte |
| 5234 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5235 | es_now = 1; |
| 5236 | } |
| 5237 | |
| 5238 | if (es_now) |
| 5239 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5240 | |
| 5241 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5242 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5243 | |
| 5244 | /* consume incoming HTX block, including EOM */ |
| 5245 | total += fsize; |
| 5246 | if (fsize == bsize) { |
| 5247 | htx_remove_blk(htx, blk); |
| 5248 | if (fsize) |
| 5249 | goto new_frame; |
| 5250 | } else { |
| 5251 | /* we've truncated this block */ |
| 5252 | htx_cut_data_blk(htx, blk, fsize); |
| 5253 | } |
| 5254 | |
| 5255 | if (es_now) { |
| 5256 | if (h2s->st == H2_SS_OPEN) |
| 5257 | h2s->st = H2_SS_HLOC; |
| 5258 | else |
| 5259 | h2s_close(h2s); |
| 5260 | |
| 5261 | h2s->flags |= H2_SF_ES_SENT; |
| 5262 | } |
| 5263 | |
| 5264 | end: |
| 5265 | return total; |
| 5266 | } |
| 5267 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5268 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5269 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5270 | * processed. The caller must check the stream's status to detect any error |
| 5271 | * which might have happened subsequently to a successful send. The htx blocks |
| 5272 | * are automatically removed from the message. The htx message is assumed to be |
| 5273 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5274 | * 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] | 5275 | * as a single frame. The ES flag is always set. |
| 5276 | */ |
| 5277 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5278 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 5279 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5280 | struct h2c *h2c = h2s->h2c; |
| 5281 | struct htx_blk *blk; |
| 5282 | struct htx_blk *blk_end; |
| 5283 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5284 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5285 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5286 | int ret = 0; |
| 5287 | int hdr; |
| 5288 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5289 | |
| 5290 | if (h2c_mux_busy(h2c, h2s)) { |
| 5291 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5292 | goto end; |
| 5293 | } |
| 5294 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5295 | /* determine the first block which must not be deleted, blk_end may |
| 5296 | * be NULL if all blocks have to be deleted. also get trailers. |
| 5297 | */ |
| 5298 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5299 | blk_end = NULL; |
| 5300 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5301 | hdr = 0; |
| 5302 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5303 | blk = htx_get_blk(htx, idx); |
| 5304 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5305 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5306 | if (type == HTX_BLK_UNUSED) |
| 5307 | continue; |
| 5308 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5309 | if (type == HTX_BLK_EOT) { |
| 5310 | if (idx != -1) |
| 5311 | blk_end = blk; |
| 5312 | break; |
| 5313 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5314 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5315 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5316 | |
| 5317 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5318 | goto fail; |
| 5319 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5320 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 5321 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 5322 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5323 | } |
| 5324 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5325 | /* marker for end of trailers */ |
| 5326 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5327 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5328 | mbuf = br_tail(h2c->mbuf); |
| 5329 | retry: |
| 5330 | if (!h2_get_buf(h2c, mbuf)) { |
| 5331 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5332 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5333 | goto end; |
| 5334 | } |
| 5335 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5336 | chunk_reset(&outbuf); |
| 5337 | |
| 5338 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5339 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5340 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5341 | break; |
| 5342 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5343 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | if (outbuf.size < 9) |
| 5347 | goto full; |
| 5348 | |
| 5349 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5350 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5351 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5352 | outbuf.data = 9; |
| 5353 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5354 | /* encode all headers */ |
| 5355 | for (idx = 0; idx < hdr; idx++) { |
| 5356 | /* these ones do not exist in H2 or must not appear in |
| 5357 | * trailers and must be dropped. |
| 5358 | */ |
| 5359 | if (isteq(list[idx].n, ist("host")) || |
| 5360 | isteq(list[idx].n, ist("content-length")) || |
| 5361 | isteq(list[idx].n, ist("connection")) || |
| 5362 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5363 | isteq(list[idx].n, ist("keep-alive")) || |
| 5364 | isteq(list[idx].n, ist("upgrade")) || |
| 5365 | isteq(list[idx].n, ist("te")) || |
| 5366 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5367 | continue; |
| 5368 | |
| 5369 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5370 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5371 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5372 | goto realign_again; |
| 5373 | goto full; |
| 5374 | } |
| 5375 | } |
| 5376 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5377 | if (outbuf.data == 9) { |
| 5378 | /* here we have a problem, we have nothing to emit (either we |
| 5379 | * received an empty trailers block followed or we removed its |
| 5380 | * contents above). Because of this we can't send a HEADERS |
| 5381 | * frame, so we have to cheat and instead send an empty DATA |
| 5382 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5383 | */ |
| 5384 | outbuf.area[3] = H2_FT_DATA; |
| 5385 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5386 | } |
| 5387 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5388 | /* update the frame's size */ |
| 5389 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5390 | |
| 5391 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5392 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5393 | h2s->flags |= H2_SF_ES_SENT; |
| 5394 | |
| 5395 | if (h2s->st == H2_SS_OPEN) |
| 5396 | h2s->st = H2_SS_HLOC; |
| 5397 | else |
| 5398 | h2s_close(h2s); |
| 5399 | |
| 5400 | /* OK we could properly deliver the response */ |
| 5401 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5402 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5403 | ret = 0; |
| 5404 | idx = htx_get_head(htx); |
| 5405 | blk = htx_get_blk(htx, idx); |
| 5406 | while (blk != blk_end) { |
| 5407 | ret += htx_get_blksz(blk); |
| 5408 | blk = htx_remove_blk(htx, blk); |
| 5409 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5410 | |
| 5411 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5412 | ret += htx_get_blksz(blk_end); |
| 5413 | htx_remove_blk(htx, blk_end); |
| 5414 | } |
| 5415 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5416 | end: |
| 5417 | return ret; |
| 5418 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5419 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5420 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5421 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5422 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5423 | ret = 0; |
| 5424 | goto end; |
| 5425 | fail: |
| 5426 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5427 | * list etc go here (unrecoverable errors). |
| 5428 | */ |
| 5429 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5430 | ret = 0; |
| 5431 | goto end; |
| 5432 | } |
| 5433 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5434 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5435 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5436 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5437 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5438 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5439 | * returns 0 except for the error above. |
| 5440 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5441 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5442 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5443 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5444 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5445 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5446 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5447 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5448 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5449 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5450 | sw->events |= SUB_RETRY_RECV; |
| 5451 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5452 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5453 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5454 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5455 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5456 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5457 | sw->events |= SUB_RETRY_SEND; |
| 5458 | h2s->send_wait = sw; |
| 5459 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5460 | !LIST_ADDED(&h2s->list)) { |
| 5461 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5462 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5463 | else |
| 5464 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5465 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5466 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5467 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5468 | if (event_type != 0) |
| 5469 | return -1; |
| 5470 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5471 | } |
| 5472 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5473 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5474 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5475 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5476 | * It always returns zero. |
| 5477 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5478 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5479 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5480 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5481 | struct h2s *h2s = cs->ctx; |
| 5482 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5483 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5484 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5485 | BUG_ON(h2s->recv_wait != sw); |
| 5486 | sw->events &= ~SUB_RETRY_RECV; |
| 5487 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5488 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5489 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5490 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5491 | BUG_ON(h2s->send_wait != sw); |
| 5492 | LIST_DEL(&h2s->list); |
| 5493 | LIST_INIT(&h2s->list); |
| 5494 | sw->events &= ~SUB_RETRY_SEND; |
| 5495 | /* We were about to send, make sure it does not happen */ |
| 5496 | if (LIST_ADDED(&h2s->sending_list) && |
| 5497 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5498 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5499 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5500 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5501 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5502 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5503 | return 0; |
| 5504 | } |
| 5505 | |
| 5506 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5507 | /* Called from the upper layer, to receive data */ |
| 5508 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5509 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5510 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5511 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5512 | struct htx *h2s_htx = NULL; |
| 5513 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5514 | size_t ret = 0; |
| 5515 | |
| 5516 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5517 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5518 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5519 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5520 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5521 | * the HTX is empty. |
| 5522 | */ |
| 5523 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5524 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5525 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5526 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5527 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5528 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5529 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5530 | /* <buf> is empty and the message is small enough, swap the |
| 5531 | * buffers. */ |
| 5532 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5533 | htx_to_buf(buf_htx, buf); |
| 5534 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5535 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5536 | goto end; |
| 5537 | } |
| 5538 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5539 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5540 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5541 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5542 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5543 | if (htx_is_empty(buf_htx)) |
| 5544 | cs->flags |= CS_FL_EOI; |
| 5545 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5546 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5547 | 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] | 5548 | htx_to_buf(buf_htx, buf); |
| 5549 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5550 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5551 | } |
| 5552 | else { |
| 5553 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5554 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5555 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5556 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5557 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5558 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5559 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5560 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5561 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5562 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 5563 | if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5564 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5565 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5566 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5567 | if (b_size(&h2s->rxbuf)) { |
| 5568 | b_free(&h2s->rxbuf); |
| 5569 | offer_buffers(NULL, tasks_run_queue); |
| 5570 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5571 | } |
| 5572 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5573 | if (ret && h2c->dsi == h2s->id) { |
| 5574 | /* demux is blocking on this stream's buffer */ |
| 5575 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5576 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5577 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5578 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5579 | return ret; |
| 5580 | } |
| 5581 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5582 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5583 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5584 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5585 | static void h2_stop_senders(struct h2c *h2c) |
| 5586 | { |
| 5587 | struct h2s *h2s, *h2s_back; |
| 5588 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5589 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5590 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5591 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5592 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5593 | } |
| 5594 | } |
| 5595 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5596 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5597 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5598 | * flags may be updated on the conn_stream. |
| 5599 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5600 | 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] | 5601 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5602 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5603 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5604 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5605 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5606 | struct htx *htx; |
| 5607 | struct htx_blk *blk; |
| 5608 | enum htx_blk_type btype; |
| 5609 | uint32_t bsize; |
| 5610 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5611 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5612 | /* If we were not just woken because we wanted to send but couldn't, |
| 5613 | * and there's somebody else that is waiting to send, do nothing, |
| 5614 | * we will subscribe later and be put at the end of the list |
| 5615 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5616 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5617 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5618 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5619 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5620 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5621 | /* We couldn't set it to NULL before, because we needed it in case |
| 5622 | * we had to cancel the tasklet |
| 5623 | */ |
| 5624 | h2s->send_wait = NULL; |
| 5625 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5626 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5627 | return 0; |
| 5628 | |
Willy Tarreau | 902df22 | 2019-10-31 15:48:18 +0100 | [diff] [blame] | 5629 | if (h2s->h2c->st0 >= H2_CS_ERROR) { |
| 5630 | cs->flags |= CS_FL_ERROR; |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5634 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5635 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5636 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5637 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5638 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5639 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5640 | if (h2s->id == 0) { |
| 5641 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5642 | |
| 5643 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5644 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5645 | return 0; |
| 5646 | } |
| 5647 | |
| 5648 | eb32_delete(&h2s->by_id); |
| 5649 | h2s->by_id.key = h2s->id = id; |
| 5650 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5651 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5652 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5653 | } |
| 5654 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5655 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5656 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5657 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5658 | idx = htx_get_head(htx); |
| 5659 | blk = htx_get_blk(htx, idx); |
| 5660 | btype = htx_get_blk_type(blk); |
| 5661 | bsize = htx_get_blksz(blk); |
| 5662 | |
| 5663 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5664 | case HTX_BLK_REQ_SL: |
| 5665 | /* start-line before headers */ |
| 5666 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5667 | if (ret > 0) { |
| 5668 | total += ret; |
| 5669 | count -= ret; |
| 5670 | if (ret < bsize) |
| 5671 | goto done; |
| 5672 | } |
| 5673 | break; |
| 5674 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5675 | case HTX_BLK_RES_SL: |
| 5676 | /* start-line before headers */ |
| 5677 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5678 | if (ret > 0) { |
| 5679 | total += ret; |
| 5680 | count -= ret; |
| 5681 | if (ret < bsize) |
| 5682 | goto done; |
| 5683 | } |
| 5684 | break; |
| 5685 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5686 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5687 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5688 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5689 | * This EOM necessarily is one before trailers, as the EOM following |
| 5690 | * trailers would have been consumed by the trailers parser. |
| 5691 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5692 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5693 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5694 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5695 | total += ret; |
| 5696 | count -= ret; |
| 5697 | if (ret < bsize) |
| 5698 | goto done; |
| 5699 | } |
| 5700 | break; |
| 5701 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5702 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5703 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5704 | /* This is the first trailers block, all the subsequent ones AND |
| 5705 | * the EOM will be swallowed by the parser. |
| 5706 | */ |
| 5707 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5708 | if (ret > 0) { |
| 5709 | total += ret; |
| 5710 | count -= ret; |
| 5711 | if (ret < bsize) |
| 5712 | goto done; |
| 5713 | } |
| 5714 | break; |
| 5715 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5716 | default: |
| 5717 | htx_remove_blk(htx, blk); |
| 5718 | total += bsize; |
| 5719 | count -= bsize; |
| 5720 | break; |
| 5721 | } |
| 5722 | } |
| 5723 | goto done; |
| 5724 | } |
| 5725 | |
| 5726 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5727 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5728 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5729 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5730 | ret = -1; |
| 5731 | else |
| 5732 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5733 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5734 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5735 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5736 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5737 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5738 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5739 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5740 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5741 | if (unlikely((int)ret <= 0)) { |
| 5742 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5743 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5744 | break; |
| 5745 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5746 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5747 | total += count; |
| 5748 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5749 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5750 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5751 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5752 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5753 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5754 | break; |
| 5755 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5756 | |
| 5757 | total += ret; |
| 5758 | count -= ret; |
| 5759 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5760 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5761 | break; |
| 5762 | |
| 5763 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5764 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5765 | } |
| 5766 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5767 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5768 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5769 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5770 | * unprocessed trailers, abnormal extra data, ...) |
| 5771 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5772 | total += count; |
| 5773 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5774 | } |
| 5775 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5776 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5777 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5778 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5779 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5780 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5781 | } |
| 5782 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5783 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5784 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5785 | } else { |
| 5786 | b_del(buf, total); |
| 5787 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5788 | |
| 5789 | /* The mux is full, cancel the pending tasks */ |
| 5790 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5791 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5792 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5793 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5794 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5795 | * we read exactly what the caller specified, as with HTX the caller |
| 5796 | * will always give the buffer size, instead of the amount of data |
| 5797 | * available. |
| 5798 | */ |
| 5799 | if (htx && !b_data(buf)) |
| 5800 | total = orig_count; |
| 5801 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5802 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5803 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5804 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5805 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5806 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5807 | /* If we're waiting for flow control, and we got a shutr on the |
| 5808 | * connection, we will never be unlocked, so add an error on |
| 5809 | * the conn_stream. |
| 5810 | */ |
| 5811 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5812 | !b_data(&h2s->h2c->dbuf) && |
| 5813 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5814 | if (cs->flags & CS_FL_EOS) |
| 5815 | cs->flags |= CS_FL_ERROR; |
| 5816 | else |
| 5817 | cs->flags |= CS_FL_ERR_PENDING; |
| 5818 | } |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5819 | |
| 5820 | if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5821 | /* 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] | 5822 | LIST_DEL_INIT(&h2s->list); |
| 5823 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5824 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5825 | } |
| 5826 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5827 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5828 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5829 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5830 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5831 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5832 | struct eb32_node *node; |
| 5833 | int fctl_cnt = 0; |
| 5834 | int send_cnt = 0; |
| 5835 | int tree_cnt = 0; |
| 5836 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5837 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5838 | |
| 5839 | if (!h2c) |
| 5840 | return; |
| 5841 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5842 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5843 | fctl_cnt++; |
| 5844 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5845 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5846 | send_cnt++; |
| 5847 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5848 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5849 | node = eb32_first(&h2c->streams_by_id); |
| 5850 | while (node) { |
| 5851 | h2s = container_of(node, struct h2s, by_id); |
| 5852 | tree_cnt++; |
| 5853 | if (!h2s->cs) |
| 5854 | orph_cnt++; |
| 5855 | node = eb32_next(node); |
| 5856 | } |
| 5857 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5858 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5859 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5860 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5861 | " .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] | 5862 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5863 | " .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] | 5864 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5865 | 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] | 5866 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5867 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5868 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5869 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5870 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5871 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5872 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5873 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5874 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5875 | |
| 5876 | if (h2s) { |
| 5877 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5878 | h2s, h2s->id, h2s->flags, |
| 5879 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5880 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5881 | h2s->cs); |
| 5882 | if (h2s->cs) |
| 5883 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5884 | h2s->cs->flags, h2s->cs->data); |
| 5885 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5886 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5887 | |
| 5888 | /*******************************************************/ |
| 5889 | /* functions below are dedicated to the config parsers */ |
| 5890 | /*******************************************************/ |
| 5891 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5892 | /* config parser for global "tune.h2.header-table-size" */ |
| 5893 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5894 | struct proxy *defpx, const char *file, int line, |
| 5895 | char **err) |
| 5896 | { |
| 5897 | if (too_many_args(1, args, err, NULL)) |
| 5898 | return -1; |
| 5899 | |
| 5900 | h2_settings_header_table_size = atoi(args[1]); |
| 5901 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5902 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5903 | return -1; |
| 5904 | } |
| 5905 | return 0; |
| 5906 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5907 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5908 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5909 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5910 | struct proxy *defpx, const char *file, int line, |
| 5911 | char **err) |
| 5912 | { |
| 5913 | if (too_many_args(1, args, err, NULL)) |
| 5914 | return -1; |
| 5915 | |
| 5916 | h2_settings_initial_window_size = atoi(args[1]); |
| 5917 | if (h2_settings_initial_window_size < 0) { |
| 5918 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5919 | return -1; |
| 5920 | } |
| 5921 | return 0; |
| 5922 | } |
| 5923 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5924 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5925 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5926 | struct proxy *defpx, const char *file, int line, |
| 5927 | char **err) |
| 5928 | { |
| 5929 | if (too_many_args(1, args, err, NULL)) |
| 5930 | return -1; |
| 5931 | |
| 5932 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5933 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5934 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5935 | return -1; |
| 5936 | } |
| 5937 | return 0; |
| 5938 | } |
| 5939 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5940 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5941 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5942 | struct proxy *defpx, const char *file, int line, |
| 5943 | char **err) |
| 5944 | { |
| 5945 | if (too_many_args(1, args, err, NULL)) |
| 5946 | return -1; |
| 5947 | |
| 5948 | h2_settings_max_frame_size = atoi(args[1]); |
| 5949 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5950 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5951 | return -1; |
| 5952 | } |
| 5953 | return 0; |
| 5954 | } |
| 5955 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5956 | |
| 5957 | /****************************************/ |
| 5958 | /* MUX initialization and instanciation */ |
| 5959 | /***************************************/ |
| 5960 | |
| 5961 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5962 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5963 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5964 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5965 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5966 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5967 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5968 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5969 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5970 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5971 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5972 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5973 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5974 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5975 | .shutr = h2_shutr, |
| 5976 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 5977 | .ctl = h2_ctl, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5978 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5979 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5980 | .name = "H2", |
| 5981 | }; |
| 5982 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5983 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5984 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5985 | { .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] | 5986 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5987 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5988 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5989 | |
| 5990 | /* The mux operations */ |
| 5991 | static const struct mux_ops h2_htx_ops = { |
| 5992 | .init = h2_init, |
| 5993 | .wake = h2_wake, |
| 5994 | .snd_buf = h2_snd_buf, |
| 5995 | .rcv_buf = h2_rcv_buf, |
| 5996 | .subscribe = h2_subscribe, |
| 5997 | .unsubscribe = h2_unsubscribe, |
| 5998 | .attach = h2_attach, |
| 5999 | .get_first_cs = h2_get_first_cs, |
| 6000 | .detach = h2_detach, |
| 6001 | .destroy = h2_destroy, |
| 6002 | .avail_streams = h2_avail_streams, |
| 6003 | .used_streams = h2_used_streams, |
| 6004 | .shutr = h2_shutr, |
| 6005 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 6006 | .ctl = h2_ctl, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6007 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 6008 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6009 | .name = "H2", |
| 6010 | }; |
| 6011 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 6012 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6013 | { .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] | 6014 | |
| 6015 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 6016 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6017 | /* config keyword parsers */ |
| 6018 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 6019 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 6020 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 6021 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 6022 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6023 | { 0, NULL, NULL } |
| 6024 | }}; |
| 6025 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6026 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |