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 | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
| 67 | #define H2_CF_RCVD_SHUT 0x00020000 // a recv() attempt already failed on a shutdown |
| 68 | #define H2_CF_END_REACHED 0x00040000 // pending data too short with RCVD_SHUT present |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 70 | /* H2 connection state, in h2c->st0 */ |
| 71 | enum h2_cs { |
| 72 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 73 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 74 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 75 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 76 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 77 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 78 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 79 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 80 | H2_CS_ENTRIES // must be last |
| 81 | } __attribute__((packed)); |
| 82 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 84 | /* 32 buffers: one for the ring's root, rest for the mbuf itself */ |
| 85 | #define H2C_MBUF_CNT 32 |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 86 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 87 | /* H2 connection descriptor */ |
| 88 | struct h2c { |
| 89 | struct connection *conn; |
| 90 | |
| 91 | enum h2_cs st0; /* mux state */ |
| 92 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 93 | |
| 94 | /* 16 bit hole here */ |
| 95 | uint32_t flags; /* connection flags: H2_CF_* */ |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 96 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 97 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 98 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 99 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 100 | |
| 101 | /* states for the demux direction */ |
| 102 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 103 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 104 | |
| 105 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 106 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 107 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 108 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 109 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 110 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 111 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 112 | |
| 113 | /* states for the mux direction */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 114 | struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 115 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 116 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 117 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 118 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 119 | /* 16 bit hole here */ |
| 120 | int32_t miw; /* mux initial window size for all new streams */ |
| 121 | int32_t mws; /* mux window size. Can be negative. */ |
| 122 | int32_t mfs; /* mux's max frame size */ |
| 123 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 124 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 125 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 126 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 127 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 128 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 129 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 130 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 131 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 132 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 133 | struct list send_list; /* list of blocked streams requesting to send */ |
| 134 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 135 | 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] | 136 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 137 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 138 | 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] | 139 | }; |
| 140 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 141 | /* H2 stream state, in h2s->st */ |
| 142 | enum h2_ss { |
| 143 | H2_SS_IDLE = 0, // idle |
| 144 | H2_SS_RLOC, // reserved(local) |
| 145 | H2_SS_RREM, // reserved(remote) |
| 146 | H2_SS_OPEN, // open |
| 147 | H2_SS_HREM, // half-closed(remote) |
| 148 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 149 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 150 | H2_SS_CLOSED, // closed |
| 151 | H2_SS_ENTRIES // must be last |
| 152 | } __attribute__((packed)); |
| 153 | |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 154 | #define H2_SS_MASK(state) (1UL << (state)) |
| 155 | #define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE) |
| 156 | #define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC) |
| 157 | #define H2_SS_RREM_BIT (1UL << H2_SS_RREM) |
| 158 | #define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN) |
| 159 | #define H2_SS_HREM_BIT (1UL << H2_SS_HREM) |
| 160 | #define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC) |
| 161 | #define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR) |
| 162 | #define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED) |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 163 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 164 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 165 | #define H2_SF_NONE 0x00000000 |
| 166 | #define H2_SF_ES_RCVD 0x00000001 |
| 167 | #define H2_SF_ES_SENT 0x00000002 |
| 168 | |
| 169 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 170 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 171 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 172 | /* stream flags indicating the reason the stream is blocked */ |
| 173 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 174 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list) |
| 175 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list) |
| 176 | #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] | 177 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 178 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 179 | /* stream flags indicating how data is supposed to be sent */ |
| 180 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 181 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 182 | |
| 183 | /* step we're currently in when sending chunks. This is needed because we may |
| 184 | * have to transfer chunks as large as a full buffer so there's no room left |
| 185 | * for size nor crlf around. |
| 186 | */ |
| 187 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 188 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 189 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 190 | |
| 191 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 192 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 193 | #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] | 194 | #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] | 195 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 196 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 197 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 198 | #define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy) |
| 199 | #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] | 200 | #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] | 201 | |
| 202 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 203 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 204 | * it is being processed in the internal HTTP representation (H1 for now). |
| 205 | */ |
| 206 | struct h2s { |
| 207 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 208 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 209 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 210 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 211 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 212 | int32_t id; /* stream ID */ |
| 213 | uint32_t flags; /* H2_SF_* */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 214 | 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] | 215 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 216 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 217 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 218 | 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] | 219 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 220 | 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] | 221 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 222 | 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] | 223 | 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] | 224 | 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] | 225 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 226 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 227 | /* descriptor for an h2 frame header */ |
| 228 | struct h2_fh { |
| 229 | uint32_t len; /* length, host order, 24 bits */ |
| 230 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 231 | uint8_t ft; /* frame type */ |
| 232 | uint8_t ff; /* frame flags */ |
| 233 | }; |
| 234 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 235 | /* the h2c connection pool */ |
| 236 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 237 | |
| 238 | /* the h2s stream pool */ |
| 239 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 240 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 241 | /* The default connection window size is 65535, it may only be enlarged using |
| 242 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 243 | * we'll pretend we already received the difference between the two to send |
| 244 | * an equivalent window update to enlarge it to 2G-1. |
| 245 | */ |
| 246 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 247 | |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 248 | /* maximum amount of data we're OK with re-aligning for buffer optimizations */ |
| 249 | #define MAX_DATA_REALIGN 1024 |
| 250 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 251 | /* a few settings from the global section */ |
| 252 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 253 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 254 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 255 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 256 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 257 | /* a dmumy closed stream */ |
| 258 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 259 | .cs = NULL, |
| 260 | .h2c = NULL, |
| 261 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 262 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 263 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 264 | .id = 0, |
| 265 | }; |
| 266 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 267 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 268 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 269 | .cs = NULL, |
| 270 | .h2c = NULL, |
| 271 | .st = H2_SS_CLOSED, |
| 272 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 273 | .flags = 0, |
| 274 | .id = 0, |
| 275 | }; |
| 276 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 277 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 278 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 279 | .cs = NULL, |
| 280 | .h2c = NULL, |
| 281 | .st = H2_SS_CLOSED, |
| 282 | .errcode = H2_ERR_REFUSED_STREAM, |
| 283 | .flags = 0, |
| 284 | .id = 0, |
| 285 | }; |
| 286 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 287 | /* and a dummy idle stream for use with any unannounced stream */ |
| 288 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 289 | .cs = NULL, |
| 290 | .h2c = NULL, |
| 291 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 292 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 293 | .id = 0, |
| 294 | }; |
| 295 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 296 | 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] | 297 | static int h2_send(struct h2c *h2c); |
| 298 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 299 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 300 | 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] | 301 | 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] | 302 | 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] | 303 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 304 | 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] | 305 | 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] | 306 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 307 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 308 | |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 309 | /* Detect a pending read0 for a H2 connection. It happens if a read0 was |
| 310 | * already reported on a previous xprt->rcvbuf() AND a frame parser failed |
| 311 | * to parse pending data, confirming no more progress is possible because |
| 312 | * we're facing a truncated frame. The function returns 1 to report a read0 |
| 313 | * or 0 otherwise. |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 314 | */ |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 315 | static inline int h2c_read0_pending(struct h2c *h2c) |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 316 | { |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 317 | return !!(h2c->flags & H2_CF_END_REACHED); |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 320 | /* returns true if the connection is allowed to expire, false otherwise. A |
| 321 | * connection may expire when: |
| 322 | * - it has no stream |
| 323 | * - it has data in the mux buffer |
| 324 | * - it has streams in the blocked list |
| 325 | * - it has streams in the fctl list |
| 326 | * - it has streams in the send list |
| 327 | * Otherwise it means some streams are waiting in the data layer and it should |
| 328 | * not expire. |
| 329 | */ |
| 330 | static inline int h2c_may_expire(const struct h2c *h2c) |
| 331 | { |
| 332 | return eb_is_empty(&h2c->streams_by_id) || |
| 333 | br_data(h2c->mbuf) || |
| 334 | !LIST_ISEMPTY(&h2c->blocked_list) || |
| 335 | !LIST_ISEMPTY(&h2c->fctl_list) || |
| 336 | !LIST_ISEMPTY(&h2c->send_list) || |
| 337 | !LIST_ISEMPTY(&h2c->sending_list); |
| 338 | } |
| 339 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 340 | static __inline int |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 341 | h2c_is_dead(const struct h2c *h2c) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 342 | { |
| 343 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 344 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 345 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 346 | (!(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] | 347 | (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 348 | (conn_xprt_read0_pending(h2c->conn) || |
| 349 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 350 | return 1; |
| 351 | |
| 352 | return 0; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 355 | /*****************************************************/ |
| 356 | /* functions below are for dynamic buffer management */ |
| 357 | /*****************************************************/ |
| 358 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 359 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 360 | * to receive data into the buffer and/or demux pending data. The condition is |
| 361 | * a bit complex due to some API limits for now. The rules are the following : |
| 362 | * - if an error or a shutdown was detected on the connection and the buffer |
| 363 | * is empty, we must not attempt to receive |
| 364 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 365 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 366 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 367 | * regardless of whether the demux buffer is full or not, so that only |
| 368 | * de demux part decides whether or not to block. This is needed because |
| 369 | * the connection API indeed prevents us from re-enabling receipt that is |
| 370 | * already enabled in a polled state, so we must always immediately stop |
| 371 | * as soon as the demux can't proceed so as never to hit an end of read |
| 372 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 373 | * - otherwise must may not attempt |
| 374 | */ |
| 375 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 376 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 377 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 378 | (h2c->st0 >= H2_CS_ERROR || |
| 379 | h2c->conn->flags & CO_FL_ERROR || |
| 380 | conn_xprt_read0_pending(h2c->conn))) |
| 381 | return 0; |
| 382 | |
| 383 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 384 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 385 | return 1; |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 390 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 391 | 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] | 392 | { |
| 393 | if (!h2_recv_allowed(h2c)) |
| 394 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 395 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 396 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 397 | return; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 398 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 402 | /* returns true if the front connection has too many conn_streams attached */ |
| 403 | 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] | 404 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 405 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 408 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 409 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 410 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 411 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 412 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 413 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 414 | { |
| 415 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 416 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 417 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 418 | 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] | 419 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 420 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 421 | return 1; |
| 422 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 423 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 424 | 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] | 425 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 426 | |
| 427 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 428 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 429 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 430 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 431 | return 1; |
| 432 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 433 | |
| 434 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 435 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 436 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 437 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 438 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 439 | return 1; |
| 440 | } |
| 441 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 445 | 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] | 446 | { |
| 447 | struct buffer *buf = NULL; |
| 448 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 449 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 450 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 451 | h2c->buf_wait.target = h2c; |
| 452 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 453 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 454 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 455 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 456 | __conn_xprt_stop_recv(h2c->conn); |
| 457 | } |
| 458 | return buf; |
| 459 | } |
| 460 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 461 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 462 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 463 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 464 | b_free(bptr); |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 465 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 469 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 470 | { |
| 471 | struct buffer *buf; |
| 472 | unsigned int count = 0; |
| 473 | |
| 474 | while (b_size(buf = br_head_pick(h2c->mbuf))) { |
| 475 | b_free(buf); |
| 476 | count++; |
| 477 | } |
| 478 | if (count) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 479 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 482 | /* returns the number of allocatable outgoing streams for the connection taking |
| 483 | * the last_sid and the reserved ones into account. |
| 484 | */ |
| 485 | static inline int h2_streams_left(const struct h2c *h2c) |
| 486 | { |
| 487 | int ret; |
| 488 | |
| 489 | /* consider the number of outgoing streams we're allowed to create before |
| 490 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 491 | * nb_reserved is the number of streams which don't yet have an ID. |
| 492 | */ |
| 493 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 494 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 495 | if (ret < 0) |
| 496 | ret = 0; |
| 497 | return ret; |
| 498 | } |
| 499 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 500 | /* returns the number of streams in use on a connection to figure if it's |
| 501 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 502 | * to know if it was the last one after a detach(). |
| 503 | */ |
| 504 | static int h2_used_streams(struct connection *conn) |
| 505 | { |
| 506 | struct h2c *h2c = conn->ctx; |
| 507 | |
| 508 | return h2c->nb_cs; |
| 509 | } |
| 510 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 511 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 512 | static int h2_avail_streams(struct connection *conn) |
| 513 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 514 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 515 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 516 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 517 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 518 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 519 | * streams on the connection. |
| 520 | */ |
| 521 | if (h2c->last_sid >= 0) |
| 522 | return 0; |
| 523 | |
Willy Tarreau | d63b85d | 2019-10-31 15:10:03 +0100 | [diff] [blame] | 524 | if (h2c->st0 >= H2_CS_ERROR) |
| 525 | return 0; |
| 526 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 527 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 528 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 529 | |
| 530 | /* we must also consider the limit imposed by stream IDs */ |
| 531 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 532 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 533 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 534 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 535 | ret1 = MIN(ret1, ret2); |
| 536 | } |
| 537 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 540 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 541 | /*****************************************************************/ |
| 542 | /* functions below are dedicated to the mux setup and management */ |
| 543 | /*****************************************************************/ |
| 544 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 545 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 546 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 547 | * connections from the fact that the context is still NULL (even during mux |
| 548 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 549 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 550 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 551 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 552 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 553 | { |
| 554 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 555 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 556 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 557 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 558 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 559 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 560 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 561 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 562 | h2c->flags = H2_CF_IS_BACK; |
| 563 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 564 | if (tick_isset(prx->timeout.serverfin)) |
| 565 | h2c->shut_timeout = prx->timeout.serverfin; |
| 566 | } else { |
| 567 | h2c->flags = H2_CF_NONE; |
| 568 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 569 | if (tick_isset(prx->timeout.clientfin)) |
| 570 | h2c->shut_timeout = prx->timeout.clientfin; |
| 571 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 572 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 573 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 574 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 575 | if (tick_isset(h2c->timeout)) { |
| 576 | t = task_new(tid_bit); |
| 577 | if (!t) |
| 578 | goto fail; |
| 579 | |
| 580 | h2c->task = t; |
| 581 | t->process = h2_timeout_task; |
| 582 | t->context = h2c; |
| 583 | t->expire = tick_add(now_ms, h2c->timeout); |
| 584 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 585 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 586 | h2c->wait_event.tasklet = tasklet_new(); |
| 587 | if (!h2c->wait_event.tasklet) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 588 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 589 | h2c->wait_event.tasklet->process = h2_io_cb; |
| 590 | h2c->wait_event.tasklet->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 591 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 592 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 593 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 594 | if (!h2c->ddht) |
| 595 | goto fail; |
| 596 | |
| 597 | /* Initialise the context. */ |
| 598 | h2c->st0 = H2_CS_PREFACE; |
| 599 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 600 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 601 | h2c->max_id = -1; |
| 602 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 603 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 604 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 605 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 606 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 607 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 608 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 609 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 610 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 611 | h2c->dsi = -1; |
| 612 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 613 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 614 | h2c->last_sid = -1; |
| 615 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 616 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 617 | h2c->miw = 65535; /* mux initial window size */ |
| 618 | h2c->mws = 65535; /* mux window size */ |
| 619 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 620 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 621 | LIST_INIT(&h2c->send_list); |
| 622 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 623 | LIST_INIT(&h2c->blocked_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 624 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 625 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 626 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 627 | if (t) |
| 628 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 629 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 630 | if (h2c->flags & H2_CF_IS_BACK) { |
| 631 | /* FIXME: this is temporary, for outgoing connections we need |
| 632 | * to immediately allocate a stream until the code is modified |
| 633 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 634 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 635 | */ |
| 636 | struct h2s *h2s; |
| 637 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 638 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 639 | if (!h2s) |
| 640 | goto fail_stream; |
| 641 | } |
| 642 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 643 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 644 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 645 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 646 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 647 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 648 | fail_stream: |
| 649 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 650 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 651 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 652 | if (h2c->wait_event.tasklet) |
| 653 | tasklet_free(h2c->wait_event.tasklet); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 654 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 655 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 659 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 660 | * -1 if no more is allocatable. |
| 661 | */ |
| 662 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 663 | { |
| 664 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 665 | |
| 666 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 667 | id = -1; |
| 668 | return id; |
| 669 | } |
| 670 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 671 | /* returns the stream associated with id <id> or NULL if not found */ |
| 672 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 673 | { |
| 674 | struct eb32_node *node; |
| 675 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 676 | if (id == 0) |
| 677 | return (struct h2s *)h2_closed_stream; |
| 678 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 679 | if (id > h2c->max_id) |
| 680 | return (struct h2s *)h2_idle_stream; |
| 681 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 682 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 683 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 684 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 685 | |
| 686 | return container_of(node, struct h2s, by_id); |
| 687 | } |
| 688 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 689 | /* release function. This one should be called to free all resources allocated |
| 690 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 691 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 692 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 693 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 694 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 695 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 696 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 697 | /* The connection must be aattached to this mux to be released */ |
| 698 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 699 | conn = h2c->conn; |
| 700 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 701 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 702 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 703 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 704 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 705 | LIST_DEL(&h2c->buf_wait.list); |
| 706 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 707 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 708 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 709 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 710 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 711 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 712 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 713 | h2c->task->context = NULL; |
| 714 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 715 | h2c->task = NULL; |
| 716 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 717 | if (h2c->wait_event.tasklet) |
| 718 | tasklet_free(h2c->wait_event.tasklet); |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 719 | if (conn && h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 720 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 721 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 722 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 723 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 726 | if (conn) { |
| 727 | conn->mux = NULL; |
| 728 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 729 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 730 | conn_stop_tracking(conn); |
| 731 | conn_full_close(conn); |
| 732 | if (conn->destroy_cb) |
| 733 | conn->destroy_cb(conn); |
| 734 | conn_free(conn); |
| 735 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 739 | /******************************************************/ |
| 740 | /* functions below are for the H2 protocol processing */ |
| 741 | /******************************************************/ |
| 742 | |
| 743 | /* 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] | 744 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 745 | { |
| 746 | return h2s ? h2s->id : 0; |
| 747 | } |
| 748 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 749 | /* returns the sum of the stream's own window size and the mux's initial |
| 750 | * window, which together form the stream's effective window size. |
| 751 | */ |
| 752 | static inline int h2s_mws(const struct h2s *h2s) |
| 753 | { |
| 754 | return h2s->sws + h2s->h2c->miw; |
| 755 | } |
| 756 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 757 | /* 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] | 758 | 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] | 759 | { |
| 760 | if (h2c->msi < 0) |
| 761 | return 0; |
| 762 | |
| 763 | if (h2c->msi == h2s_id(h2s)) |
| 764 | return 0; |
| 765 | |
| 766 | return 1; |
| 767 | } |
| 768 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 769 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 770 | 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] | 771 | { |
| 772 | h2c->errcode = err; |
| 773 | h2c->st0 = H2_CS_ERROR; |
| 774 | } |
| 775 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 776 | /* marks an error on the stream. It may also update an already closed stream |
| 777 | * (e.g. to report an error after an RST was received). |
| 778 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 779 | 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] | 780 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 781 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 782 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 783 | if (h2s->st < H2_SS_ERROR) |
| 784 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 785 | if (h2s->cs) |
| 786 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 790 | /* attempt to notify the data layer of recv availability */ |
| 791 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 792 | { |
| 793 | struct wait_event *sw; |
| 794 | |
| 795 | if (h2s->recv_wait) { |
| 796 | sw = h2s->recv_wait; |
| 797 | sw->events &= ~SUB_RETRY_RECV; |
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 | h2s->recv_wait = NULL; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /* attempt to notify the data layer of send availability */ |
| 804 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 805 | { |
| 806 | struct wait_event *sw; |
| 807 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 808 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 809 | sw = h2s->send_wait; |
| 810 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 811 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 812 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 816 | /* alerts the data layer, trying to wake it up by all means, following |
| 817 | * this sequence : |
| 818 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 819 | * - if its subscribed to send, then it's woken up for send |
| 820 | * - if it was subscribed to neither, its ->wake() callback is called |
| 821 | * It is safe to call this function with a closed stream which doesn't have a |
| 822 | * conn_stream anymore. |
| 823 | */ |
| 824 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 825 | { |
| 826 | if (h2s->recv_wait || h2s->send_wait) { |
| 827 | h2s_notify_recv(h2s); |
| 828 | h2s_notify_send(h2s); |
| 829 | } |
| 830 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 831 | h2s->cs->data_cb->wake(h2s->cs); |
| 832 | } |
| 833 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 834 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 835 | 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] | 836 | { |
| 837 | uint8_t *out = frame; |
| 838 | |
| 839 | *out = len >> 16; |
| 840 | write_n16(out + 1, len); |
| 841 | } |
| 842 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 843 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 844 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 845 | * 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] | 846 | * available in the buffer's input prior to calling this function. The buffer |
| 847 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 848 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 849 | 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] | 850 | const struct buffer *b, int o) |
| 851 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 852 | 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] | 853 | } |
| 854 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 855 | 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] | 856 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 857 | 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] | 858 | } |
| 859 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 860 | 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] | 861 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 862 | 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] | 863 | } |
| 864 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 865 | 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] | 866 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 867 | 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] | 868 | } |
| 869 | |
| 870 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 871 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 872 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 873 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 874 | * 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] | 875 | * |
| 876 | * b0 b1 b2 b3 b4 b5..b8 |
| 877 | * +----------+---------+--------+----+----+----------------------+ |
| 878 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 879 | * +----------+---------+--------+----+----+----------------------+ |
| 880 | * |
| 881 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 882 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 883 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 884 | * 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] | 885 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 886 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 887 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 888 | 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] | 889 | { |
| 890 | uint64_t w; |
| 891 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 892 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 893 | return 0; |
| 894 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 895 | w = h2_get_n64(b, o + 1); |
| 896 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 897 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 898 | h->ff = w >> 32; |
| 899 | h->ft = w >> 40; |
| 900 | h->len += w >> 48; |
| 901 | return 1; |
| 902 | } |
| 903 | |
| 904 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 905 | * h2_peek_frame_hdr() above. |
| 906 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 907 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 908 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 909 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 913 | 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] | 914 | { |
| 915 | int ret; |
| 916 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 917 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 918 | if (ret > 0) |
| 919 | h2_skip_frame_hdr(b); |
| 920 | return ret; |
| 921 | } |
| 922 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 923 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 924 | * its connection if the stream was not yet closed. Please use this exclusively |
| 925 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 926 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 927 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 928 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 929 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 930 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 931 | if (!h2s->id) |
| 932 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 933 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 934 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 935 | h2s_notify_recv(h2s); |
| 936 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 937 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 938 | h2s->st = H2_SS_CLOSED; |
| 939 | } |
| 940 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 941 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 942 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 943 | { |
| 944 | h2s_close(h2s); |
| 945 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 946 | if (b_size(&h2s->rxbuf)) { |
| 947 | b_free(&h2s->rxbuf); |
| 948 | offer_buffers(NULL, tasks_run_queue); |
| 949 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 950 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 951 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 952 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 953 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 954 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 955 | * reference left would be in the h2c send_list/fctl_list, and if |
| 956 | * we're in it, we're getting out anyway |
| 957 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 958 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 959 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 960 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 961 | LIST_DEL_INIT(&h2s->sending_list); |
| 962 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 963 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 964 | pool_free(pool_head_h2s, h2s); |
| 965 | } |
| 966 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 967 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 968 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 969 | * causes of errors can be any failed memory allocation. The caller is |
| 970 | * responsible for checking if the connection may support an extra stream |
| 971 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 972 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 973 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 974 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 975 | struct h2s *h2s; |
| 976 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 977 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 978 | if (!h2s) |
| 979 | goto out; |
| 980 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 981 | h2s->wait_event.tasklet = tasklet_new(); |
| 982 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 983 | pool_free(pool_head_h2s, h2s); |
| 984 | goto out; |
| 985 | } |
| 986 | h2s->send_wait = NULL; |
| 987 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 988 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 989 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 990 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 991 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 992 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 993 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 994 | h2s->cs = NULL; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 995 | h2s->sws = 0; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 996 | h2s->flags = H2_SF_NONE; |
| 997 | h2s->errcode = H2_ERR_NO_ERROR; |
| 998 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 999 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1000 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 1001 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1002 | |
| 1003 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1004 | h1m_init_req(&h2s->h1m); |
| 1005 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 1006 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 1007 | } else { |
| 1008 | h1m_init_res(&h2s->h1m); |
| 1009 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 1010 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 1011 | } |
| 1012 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1013 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1014 | if (id > 0) |
| 1015 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 1016 | else |
| 1017 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1018 | |
| 1019 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 1020 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 1021 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1022 | |
| 1023 | return h2s; |
| 1024 | |
| 1025 | out_free_h2s: |
| 1026 | pool_free(pool_head_h2s, h2s); |
| 1027 | out: |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | |
| 1031 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 1032 | * case of memory allocation error. |
| 1033 | */ |
| 1034 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 1035 | { |
| 1036 | struct session *sess = h2c->conn->owner; |
| 1037 | struct conn_stream *cs; |
| 1038 | struct h2s *h2s; |
| 1039 | |
| 1040 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 1041 | goto out; |
| 1042 | |
| 1043 | h2s = h2s_new(h2c, id); |
| 1044 | if (!h2s) |
| 1045 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1046 | |
| 1047 | cs = cs_new(h2c->conn); |
| 1048 | if (!cs) |
| 1049 | goto out_close; |
| 1050 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1051 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1052 | h2s->cs = cs; |
| 1053 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1054 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1055 | |
| 1056 | if (stream_create_from_cs(cs) < 0) |
| 1057 | goto out_free_cs; |
| 1058 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1059 | /* We want the accept date presented to the next stream to be the one |
| 1060 | * we have now, the handshake time to be null (since the next stream |
| 1061 | * is not delayed by a handshake), and the idle time to count since |
| 1062 | * right now. |
| 1063 | */ |
| 1064 | sess->accept_date = date; |
| 1065 | sess->tv_accept = now; |
| 1066 | sess->t_handshake = 0; |
| 1067 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1068 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1069 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1070 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1071 | return h2s; |
| 1072 | |
| 1073 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1074 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1075 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1076 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1077 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1078 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1079 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1080 | sess_log(sess); |
| 1081 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1084 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1085 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1086 | * possible stream ID was reached. |
| 1087 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1088 | 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] | 1089 | { |
| 1090 | struct h2s *h2s = NULL; |
| 1091 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1092 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1093 | goto out; |
| 1094 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1095 | if (h2_streams_left(h2c) < 1) |
| 1096 | goto out; |
| 1097 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1098 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1099 | h2s = h2s_new(h2c, 0); |
| 1100 | if (!h2s) |
| 1101 | goto out; |
| 1102 | |
| 1103 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1104 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1105 | cs->ctx = h2s; |
| 1106 | h2c->nb_cs++; |
| 1107 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1108 | out: |
| 1109 | return h2s; |
| 1110 | } |
| 1111 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1112 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1113 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1114 | * the various settings codes. |
| 1115 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1116 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1117 | { |
| 1118 | struct buffer *res; |
| 1119 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1120 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1121 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1122 | int ret; |
| 1123 | |
| 1124 | if (h2c_mux_busy(h2c, NULL)) { |
| 1125 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1129 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1130 | chunk_memcpy(&buf, |
| 1131 | "\x00\x00\x00" /* length : 0 for now */ |
| 1132 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1133 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1134 | 9); |
| 1135 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1136 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1137 | /* send settings_enable_push=0 */ |
| 1138 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1139 | } |
| 1140 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1141 | if (h2_settings_header_table_size != 4096) { |
| 1142 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1143 | |
| 1144 | write_n32(str + 2, h2_settings_header_table_size); |
| 1145 | chunk_memcat(&buf, str, 6); |
| 1146 | } |
| 1147 | |
| 1148 | if (h2_settings_initial_window_size != 65535) { |
| 1149 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1150 | |
| 1151 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1152 | chunk_memcat(&buf, str, 6); |
| 1153 | } |
| 1154 | |
| 1155 | if (h2_settings_max_concurrent_streams != 0) { |
| 1156 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1157 | |
| 1158 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1159 | * the protocol, so never send this value! |
| 1160 | */ |
| 1161 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1162 | chunk_memcat(&buf, str, 6); |
| 1163 | } |
| 1164 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1165 | mfs = h2_settings_max_frame_size; |
| 1166 | if (mfs > global.tune.bufsize) |
| 1167 | mfs = global.tune.bufsize; |
| 1168 | |
| 1169 | if (!mfs) |
| 1170 | mfs = global.tune.bufsize; |
| 1171 | |
| 1172 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1173 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1174 | |
| 1175 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1176 | * match bufsize - rewrite size, but at the moment it seems |
| 1177 | * that clients don't take care of it. |
| 1178 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1179 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1180 | chunk_memcat(&buf, str, 6); |
| 1181 | } |
| 1182 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1183 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1184 | |
| 1185 | res = br_tail(h2c->mbuf); |
| 1186 | retry: |
| 1187 | if (!h2_get_buf(h2c, res)) { |
| 1188 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1189 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1193 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1194 | if (unlikely(ret <= 0)) { |
| 1195 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1196 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1197 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1198 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1199 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | else { |
| 1203 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | } |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1210 | /* Try to receive a connection preface, then upon success try to send our |
| 1211 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1212 | * missing data. It may return an error in h2c. |
| 1213 | */ |
| 1214 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1215 | { |
| 1216 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1217 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1218 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1219 | 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] | 1220 | |
| 1221 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1222 | if (ret1 < 0) |
| 1223 | sess_log(h2c->conn->owner); |
| 1224 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1225 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1226 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1230 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1231 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1232 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1233 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1234 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1237 | /* Try to send a connection preface, then upon success try to send our |
| 1238 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1239 | * missing data. It may return an error in h2c. |
| 1240 | */ |
| 1241 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1242 | { |
| 1243 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1244 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1245 | |
| 1246 | if (h2c_mux_busy(h2c, NULL)) { |
| 1247 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1251 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1252 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1253 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1254 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1255 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | if (!b_data(res)) { |
| 1260 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1261 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1262 | if (unlikely(ret <= 0)) { |
| 1263 | if (!ret) { |
| 1264 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1265 | goto retry; |
| 1266 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1267 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1268 | return 0; |
| 1269 | } |
| 1270 | else { |
| 1271 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1275 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1276 | return h2c_send_settings(h2c); |
| 1277 | } |
| 1278 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1279 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1280 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1281 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1282 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1283 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1284 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1285 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1286 | * case so that it is safe to use h2c_error() to report such errors. |
| 1287 | */ |
| 1288 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1289 | { |
| 1290 | struct buffer *res; |
| 1291 | char str[17]; |
| 1292 | int ret; |
| 1293 | |
| 1294 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1295 | return 1; // claim that it worked |
| 1296 | |
| 1297 | if (h2c_mux_busy(h2c, h2s)) { |
| 1298 | if (h2s) |
| 1299 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1300 | else |
| 1301 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1305 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1306 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1307 | |
| 1308 | if (h2c->last_sid < 0) |
| 1309 | h2c->last_sid = h2c->max_id; |
| 1310 | |
| 1311 | write_n32(str + 9, h2c->last_sid); |
| 1312 | write_n32(str + 13, h2c->errcode); |
| 1313 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1314 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1315 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1316 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1317 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1318 | if (h2s) |
| 1319 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1320 | else |
| 1321 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1325 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1326 | if (unlikely(ret <= 0)) { |
| 1327 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1328 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1329 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1330 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1331 | if (h2s) |
| 1332 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1333 | else |
| 1334 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | else { |
| 1338 | /* we cannot report this error using GOAWAY, so we mark |
| 1339 | * it and claim a success. |
| 1340 | */ |
| 1341 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1342 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1343 | return 1; |
| 1344 | } |
| 1345 | } |
| 1346 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1347 | return ret; |
| 1348 | } |
| 1349 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1350 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1351 | * during mux operations. This stream must be valid and cannot be closed |
| 1352 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1353 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1354 | * not yet. |
| 1355 | * |
| 1356 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1357 | * to write the message, it subscribes the stream to future notifications. |
| 1358 | */ |
| 1359 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1360 | { |
| 1361 | struct buffer *res; |
| 1362 | char str[13]; |
| 1363 | int ret; |
| 1364 | |
| 1365 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1366 | return 1; |
| 1367 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1368 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1369 | * RST_STREAM in response to a RST_STREAM frame. |
| 1370 | */ |
Willy Tarreau | bf9a20b | 2019-08-06 10:01:40 +0200 | [diff] [blame] | 1371 | if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) { |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1372 | ret = 1; |
| 1373 | goto ignore; |
| 1374 | } |
| 1375 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1376 | if (h2c_mux_busy(h2c, h2s)) { |
| 1377 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1381 | /* len: 4, type: 3, flags: none */ |
| 1382 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1383 | write_n32(str + 5, h2s->id); |
| 1384 | write_n32(str + 9, h2s->errcode); |
| 1385 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1386 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1387 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1388 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1389 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1390 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1394 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1395 | if (unlikely(ret <= 0)) { |
| 1396 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1397 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1398 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1399 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1400 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | else { |
| 1404 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1405 | return 0; |
| 1406 | } |
| 1407 | } |
| 1408 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1409 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1410 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1411 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1412 | return ret; |
| 1413 | } |
| 1414 | |
| 1415 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1416 | * 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] | 1417 | * error code, even if the stream is one of the dummy ones, and will update |
| 1418 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1419 | * |
| 1420 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1421 | * 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] | 1422 | * 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] | 1423 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1424 | */ |
| 1425 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1426 | { |
| 1427 | struct buffer *res; |
| 1428 | char str[13]; |
| 1429 | int ret; |
| 1430 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1431 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1432 | * RST_STREAM in response to a RST_STREAM frame. |
| 1433 | */ |
| 1434 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1435 | ret = 1; |
| 1436 | goto ignore; |
| 1437 | } |
| 1438 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1439 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1440 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1441 | return 0; |
| 1442 | } |
| 1443 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1444 | /* len: 4, type: 3, flags: none */ |
| 1445 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1446 | |
| 1447 | write_n32(str + 5, h2c->dsi); |
| 1448 | write_n32(str + 9, h2s->errcode); |
| 1449 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1450 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1451 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1452 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1453 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1454 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1455 | return 0; |
| 1456 | } |
| 1457 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1458 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1459 | if (unlikely(ret <= 0)) { |
| 1460 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1461 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1462 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1463 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1464 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1465 | return 0; |
| 1466 | } |
| 1467 | else { |
| 1468 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | } |
| 1472 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1473 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1474 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1475 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1476 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1479 | return ret; |
| 1480 | } |
| 1481 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1482 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1483 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1484 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1485 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1486 | * message, it subscribes the requesting stream to future notifications. |
| 1487 | */ |
| 1488 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1489 | { |
| 1490 | struct h2c *h2c = h2s->h2c; |
| 1491 | struct buffer *res; |
| 1492 | char str[9]; |
| 1493 | int ret; |
| 1494 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1495 | 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] | 1496 | return 1; |
| 1497 | |
| 1498 | if (h2c_mux_busy(h2c, h2s)) { |
| 1499 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1503 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1504 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1505 | write_n32(str + 5, h2s->id); |
| 1506 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1507 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1508 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1509 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1510 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1511 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1515 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1516 | if (likely(ret > 0)) { |
| 1517 | h2s->flags |= H2_SF_ES_SENT; |
| 1518 | } |
| 1519 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1520 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1521 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1522 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1523 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1524 | return 0; |
| 1525 | } |
| 1526 | else { |
| 1527 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1528 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1529 | } |
| 1530 | return ret; |
| 1531 | } |
| 1532 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1533 | /* 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] | 1534 | * 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] | 1535 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1536 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1537 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1538 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1539 | { |
| 1540 | if (!h2s->cs) { |
| 1541 | /* this stream was already orphaned */ |
| 1542 | h2s_destroy(h2s); |
| 1543 | return; |
| 1544 | } |
| 1545 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 1546 | if (h2c_read0_pending(h2s->h2c)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1547 | if (h2s->st == H2_SS_OPEN) |
| 1548 | h2s->st = H2_SS_HREM; |
| 1549 | else if (h2s->st == H2_SS_HLOC) |
| 1550 | h2s_close(h2s); |
| 1551 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1552 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1553 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1554 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1555 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1556 | if (h2s->cs->flags & CS_FL_EOS) |
| 1557 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1558 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1559 | if (h2s->st < H2_SS_ERROR) |
| 1560 | h2s->st = H2_SS_ERROR; |
| 1561 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1562 | |
| 1563 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1567 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1568 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1569 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1570 | { |
| 1571 | struct eb32_node *node; |
| 1572 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1573 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1574 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1575 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1576 | while (node) { |
| 1577 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1578 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1579 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1580 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1581 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1582 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1583 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1584 | while (node) { |
| 1585 | h2s = container_of(node, struct h2s, by_id); |
| 1586 | if (h2s->id > 0) |
| 1587 | break; |
| 1588 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1589 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1593 | /* Wake up all blocked streams whose window size has become positive after the |
| 1594 | * mux's initial window was adjusted. This should be done after having processed |
| 1595 | * SETTINGS frames which have updated the mux's initial window size. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1596 | */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1597 | static void h2c_unblock_sfctl(struct h2c *h2c) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1598 | { |
| 1599 | struct h2s *h2s; |
| 1600 | struct eb32_node *node; |
| 1601 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1602 | node = eb32_first(&h2c->streams_by_id); |
| 1603 | while (node) { |
| 1604 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1605 | if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) { |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1606 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1607 | LIST_DEL_INIT(&h2s->list); |
| 1608 | if (h2s->send_wait) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1609 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1610 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1611 | node = eb32_next(node); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1616 | * 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] | 1617 | * return an error in h2c. The caller must have already verified frame length |
| 1618 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1619 | */ |
| 1620 | static int h2c_handle_settings(struct h2c *h2c) |
| 1621 | { |
| 1622 | unsigned int offset; |
| 1623 | int error; |
| 1624 | |
| 1625 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1626 | if (h2c->dfl) { |
| 1627 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1628 | goto fail; |
| 1629 | } |
| 1630 | return 1; |
| 1631 | } |
| 1632 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1633 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1634 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1635 | return 0; |
| 1636 | |
| 1637 | /* parse the frame */ |
| 1638 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1639 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1640 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1641 | |
| 1642 | switch (type) { |
| 1643 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1644 | /* we need to update all existing streams with the |
| 1645 | * difference from the previous iws. |
| 1646 | */ |
| 1647 | if (arg < 0) { // RFC7540#6.5.2 |
| 1648 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1649 | goto fail; |
| 1650 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1651 | h2c->miw = arg; |
| 1652 | break; |
| 1653 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1654 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1655 | error = H2_ERR_PROTOCOL_ERROR; |
| 1656 | goto fail; |
| 1657 | } |
| 1658 | h2c->mfs = arg; |
| 1659 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1660 | case H2_SETTINGS_ENABLE_PUSH: |
| 1661 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1662 | error = H2_ERR_PROTOCOL_ERROR; |
| 1663 | goto fail; |
| 1664 | } |
| 1665 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1666 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1667 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1668 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1669 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1670 | arg = h2_settings_max_concurrent_streams; |
| 1671 | h2c->streams_limit = arg; |
| 1672 | } |
| 1673 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | /* need to ACK this frame now */ |
| 1678 | h2c->st0 = H2_CS_FRAME_A; |
| 1679 | return 1; |
| 1680 | fail: |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 1681 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 1682 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1683 | h2c_error(h2c, error); |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
| 1687 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1688 | * success or one of the h2_status values. |
| 1689 | */ |
| 1690 | static int h2c_ack_settings(struct h2c *h2c) |
| 1691 | { |
| 1692 | struct buffer *res; |
| 1693 | char str[9]; |
| 1694 | int ret = -1; |
| 1695 | |
| 1696 | if (h2c_mux_busy(h2c, NULL)) { |
| 1697 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1701 | memcpy(str, |
| 1702 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1703 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1704 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1705 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1706 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1707 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1708 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1709 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1710 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1714 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1715 | if (unlikely(ret <= 0)) { |
| 1716 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1717 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1718 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1719 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1720 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1721 | return 0; |
| 1722 | } |
| 1723 | else { |
| 1724 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1725 | return 0; |
| 1726 | } |
| 1727 | } |
| 1728 | return ret; |
| 1729 | } |
| 1730 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1731 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1732 | * 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] | 1733 | * missing data. The caller must have already verified frame length |
| 1734 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1735 | */ |
| 1736 | static int h2c_handle_ping(struct h2c *h2c) |
| 1737 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1738 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1739 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1740 | h2c->st0 = H2_CS_FRAME_A; |
| 1741 | return 1; |
| 1742 | } |
| 1743 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1744 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1745 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1746 | * error in h2c. |
| 1747 | */ |
| 1748 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1749 | { |
| 1750 | struct buffer *res; |
| 1751 | char str[13]; |
| 1752 | int ret = -1; |
| 1753 | |
| 1754 | if (h2c_mux_busy(h2c, NULL)) { |
| 1755 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1759 | /* length: 4, type: 8, flags: none */ |
| 1760 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1761 | write_n32(str + 5, sid); |
| 1762 | write_n32(str + 9, increment); |
| 1763 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1764 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1765 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1766 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1767 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1768 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1772 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1773 | if (unlikely(ret <= 0)) { |
| 1774 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1775 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1776 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1777 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1778 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1779 | return 0; |
| 1780 | } |
| 1781 | else { |
| 1782 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1783 | return 0; |
| 1784 | } |
| 1785 | } |
| 1786 | return ret; |
| 1787 | } |
| 1788 | |
| 1789 | /* try to send pending window update for the connection. It's safe to call it |
| 1790 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1791 | * failure. It may return an error in h2c. |
| 1792 | */ |
| 1793 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1794 | { |
| 1795 | int ret = 1; |
| 1796 | |
| 1797 | if (h2c->rcvd_c <= 0) |
| 1798 | return 1; |
| 1799 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1800 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1801 | /* increase the advertised connection window to 2G on |
| 1802 | * first update. |
| 1803 | */ |
| 1804 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1805 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1806 | } |
| 1807 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1808 | /* send WU for the connection */ |
| 1809 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1810 | if (ret > 0) |
| 1811 | h2c->rcvd_c = 0; |
| 1812 | |
| 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1817 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1818 | * room or failure. It may return an error in h2c. |
| 1819 | */ |
| 1820 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1821 | { |
| 1822 | int ret = 1; |
| 1823 | |
| 1824 | if (h2c->rcvd_s <= 0) |
| 1825 | return 1; |
| 1826 | |
| 1827 | /* send WU for the stream */ |
| 1828 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1829 | if (ret > 0) |
| 1830 | h2c->rcvd_s = 0; |
| 1831 | |
| 1832 | return ret; |
| 1833 | } |
| 1834 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1835 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1836 | * success, 0 on missing data or one of the h2_status values. |
| 1837 | */ |
| 1838 | static int h2c_ack_ping(struct h2c *h2c) |
| 1839 | { |
| 1840 | struct buffer *res; |
| 1841 | char str[17]; |
| 1842 | int ret = -1; |
| 1843 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1844 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1845 | return 0; |
| 1846 | |
| 1847 | if (h2c_mux_busy(h2c, NULL)) { |
| 1848 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1852 | memcpy(str, |
| 1853 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1854 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1855 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1856 | |
| 1857 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1858 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1859 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1860 | res = br_tail(h2c->mbuf); |
| 1861 | retry: |
| 1862 | if (!h2_get_buf(h2c, res)) { |
| 1863 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1864 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1868 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1869 | if (unlikely(ret <= 0)) { |
| 1870 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1871 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1872 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1873 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1874 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1875 | return 0; |
| 1876 | } |
| 1877 | else { |
| 1878 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1879 | return 0; |
| 1880 | } |
| 1881 | } |
| 1882 | return ret; |
| 1883 | } |
| 1884 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1885 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1886 | * 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] | 1887 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1888 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1889 | */ |
| 1890 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1891 | { |
| 1892 | int32_t inc; |
| 1893 | int error; |
| 1894 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1895 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1896 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1897 | return 0; |
| 1898 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1899 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1900 | |
| 1901 | if (h2c->dsi != 0) { |
| 1902 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1903 | |
| 1904 | /* it's not an error to receive WU on a closed stream */ |
| 1905 | if (h2s->st == H2_SS_CLOSED) |
| 1906 | return 1; |
| 1907 | |
| 1908 | if (!inc) { |
| 1909 | error = H2_ERR_PROTOCOL_ERROR; |
| 1910 | goto strm_err; |
| 1911 | } |
| 1912 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1913 | if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1914 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1915 | goto strm_err; |
| 1916 | } |
| 1917 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1918 | h2s->sws += inc; |
| 1919 | if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1920 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 1921 | LIST_DEL_INIT(&h2s->list); |
| 1922 | if (h2s->send_wait) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1923 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | else { |
| 1927 | /* connection window update */ |
| 1928 | if (!inc) { |
| 1929 | error = H2_ERR_PROTOCOL_ERROR; |
| 1930 | goto conn_err; |
| 1931 | } |
| 1932 | |
| 1933 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1934 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1935 | goto conn_err; |
| 1936 | } |
| 1937 | |
| 1938 | h2c->mws += inc; |
| 1939 | } |
| 1940 | |
| 1941 | return 1; |
| 1942 | |
| 1943 | conn_err: |
| 1944 | h2c_error(h2c, error); |
| 1945 | return 0; |
| 1946 | |
| 1947 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1948 | h2s_error(h2s, error); |
| 1949 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1950 | return 0; |
| 1951 | } |
| 1952 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1953 | /* 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] | 1954 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1955 | * have already verified frame length and stream ID validity. Described in |
| 1956 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1957 | */ |
| 1958 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1959 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1960 | int last; |
| 1961 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1962 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1963 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1964 | return 0; |
| 1965 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1966 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1967 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1968 | if (h2c->last_sid < 0) |
| 1969 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1970 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1971 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1972 | } |
| 1973 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1974 | /* 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] | 1975 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1976 | * error in h2c. The caller must have already verified frame length and stream |
| 1977 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1978 | */ |
| 1979 | static int h2c_handle_priority(struct h2c *h2c) |
| 1980 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1981 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1982 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1983 | return 0; |
| 1984 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1985 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1986 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1987 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1988 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1989 | } |
| 1990 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1993 | /* 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] | 1994 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1995 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1996 | */ |
| 1997 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1998 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1999 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2000 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2001 | return 0; |
| 2002 | |
| 2003 | /* late RST, already handled */ |
| 2004 | if (h2s->st == H2_SS_CLOSED) |
| 2005 | return 1; |
| 2006 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2007 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2008 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2009 | |
| 2010 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 2011 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 2012 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | h2s->flags |= H2_SF_RST_RCVD; |
| 2016 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2017 | } |
| 2018 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2019 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2020 | * It may return an error in h2c or h2s. The caller must consider that the |
| 2021 | * return value is the new h2s in case one was allocated (most common case). |
| 2022 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2023 | * errors here are reported as connection errors since it's impossible to |
| 2024 | * recover from such errors after the compression context has been altered. |
| 2025 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2026 | 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] | 2027 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2028 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2029 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2030 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2031 | int error; |
| 2032 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2033 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2034 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2035 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2036 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2037 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2038 | |
| 2039 | /* now either the frame is complete or the buffer is complete */ |
| 2040 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2041 | /* The stream exists/existed, this must be a trailers frame */ |
| 2042 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2043 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2044 | /* unrecoverable error ? */ |
| 2045 | if (h2c->st0 >= H2_CS_ERROR) |
| 2046 | goto out; |
| 2047 | |
| 2048 | if (error == 0) |
| 2049 | goto out; // missing data |
| 2050 | |
| 2051 | if (error < 0) { |
| 2052 | /* Failed to decode this frame (e.g. too large request) |
| 2053 | * but the HPACK decompressor is still synchronized. |
| 2054 | */ |
| 2055 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2056 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2057 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2058 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2059 | goto done; |
| 2060 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2061 | /* the connection was already killed by an RST, let's consume |
| 2062 | * the data and send another RST. |
| 2063 | */ |
| 2064 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2065 | h2s = (struct h2s*)h2_error_stream; |
| 2066 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2067 | } |
| 2068 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2069 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2070 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2071 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2072 | goto conn_err; |
| 2073 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2074 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2075 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2076 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2077 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2078 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2079 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2080 | if (h2c->st0 >= H2_CS_ERROR) |
| 2081 | goto out; |
| 2082 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2083 | if (error <= 0) { |
| 2084 | if (error == 0) |
| 2085 | goto out; // missing data |
| 2086 | |
| 2087 | /* Failed to decode this stream (e.g. too large request) |
| 2088 | * but the HPACK decompressor is still synchronized. |
| 2089 | */ |
| 2090 | h2s = (struct h2s*)h2_error_stream; |
| 2091 | goto send_rst; |
| 2092 | } |
| 2093 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2094 | /* 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] | 2095 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2096 | * 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] | 2097 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2098 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2099 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2100 | h2s = (struct h2s*)h2_refused_stream; |
| 2101 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2105 | h2s->rxbuf = rxbuf; |
| 2106 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2107 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2108 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2109 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2110 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2111 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2112 | |
| 2113 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2114 | if (h2s->st == H2_SS_OPEN) |
| 2115 | h2s->st = H2_SS_HREM; |
| 2116 | else |
| 2117 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2120 | /* update the max stream ID if the request is being processed */ |
| 2121 | if (h2s->id > h2c->max_id) |
| 2122 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2123 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2124 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2125 | |
| 2126 | conn_err: |
| 2127 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2128 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2129 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2130 | out: |
| 2131 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2132 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2133 | |
| 2134 | send_rst: |
| 2135 | /* make the demux send an RST for the current stream. We may only |
| 2136 | * do this if we're certain that the HEADERS frame was properly |
| 2137 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2138 | */ |
| 2139 | h2_release_buf(h2c, &rxbuf); |
| 2140 | h2c->st0 = H2_CS_FRAME_E; |
| 2141 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2142 | } |
| 2143 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2144 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2145 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2146 | * errors here are reported as connection errors since it's impossible to |
| 2147 | * recover from such errors after the compression context has been altered. |
| 2148 | */ |
| 2149 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2150 | { |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2151 | struct buffer rxbuf = BUF_NULL; |
| 2152 | unsigned long long body_len = 0; |
| 2153 | uint32_t flags = 0; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2154 | int error; |
| 2155 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2156 | if (!b_size(&h2c->dbuf)) |
| 2157 | return NULL; // empty buffer |
| 2158 | |
| 2159 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2160 | return NULL; // incomplete frame |
| 2161 | |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2162 | if (h2s->st != H2_SS_CLOSED) { |
| 2163 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
| 2164 | } |
| 2165 | else { |
| 2166 | /* the connection was already killed by an RST, let's consume |
| 2167 | * the data and send another RST. |
| 2168 | */ |
| 2169 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Christopher Faulet | 0342705 | 2019-09-26 16:19:13 +0200 | [diff] [blame] | 2170 | h2s = (struct h2s*)h2_error_stream; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2171 | h2c->st0 = H2_CS_FRAME_E; |
| 2172 | goto send_rst; |
| 2173 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2174 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2175 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2176 | if (h2c->st0 >= H2_CS_ERROR) |
| 2177 | return NULL; |
| 2178 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2179 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2180 | /* RFC7540#5.1 */ |
| 2181 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2182 | h2c->st0 = H2_CS_FRAME_E; |
| 2183 | return NULL; |
| 2184 | } |
| 2185 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2186 | if (error <= 0) { |
| 2187 | if (error == 0) |
| 2188 | return NULL; // missing data |
| 2189 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2190 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2191 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2192 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2193 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2196 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2197 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2198 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2199 | 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] | 2200 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2201 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2202 | if (h2s->st == H2_SS_OPEN) |
| 2203 | h2s->st = H2_SS_HREM; |
| 2204 | else if (h2s->st == H2_SS_HLOC) |
| 2205 | h2s_close(h2s); |
| 2206 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2207 | |
| 2208 | return h2s; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame] | 2209 | |
| 2210 | send_rst: |
| 2211 | /* make the demux send an RST for the current stream. We may only |
| 2212 | * do this if we're certain that the HEADERS frame was properly |
| 2213 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2214 | */ |
| 2215 | h2_release_buf(h2c, &rxbuf); |
| 2216 | h2c->st0 = H2_CS_FRAME_E; |
| 2217 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2218 | } |
| 2219 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2220 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2221 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2222 | */ |
| 2223 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2224 | { |
| 2225 | int error; |
| 2226 | |
| 2227 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2228 | * to signal an end of stream (with the ES flag). |
| 2229 | */ |
| 2230 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2231 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2232 | return 0; // empty buffer |
| 2233 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2234 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2235 | return 0; // incomplete frame |
| 2236 | |
| 2237 | /* now either the frame is complete or the buffer is complete */ |
| 2238 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2239 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2240 | /* RFC7540#6.1 */ |
| 2241 | error = H2_ERR_STREAM_CLOSED; |
| 2242 | goto strm_err; |
| 2243 | } |
| 2244 | |
Christopher Faulet | 4fb65f4 | 2019-06-19 09:25:58 +0200 | [diff] [blame] | 2245 | 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] | 2246 | /* RFC7540#8.1.2 */ |
| 2247 | error = H2_ERR_PROTOCOL_ERROR; |
| 2248 | goto strm_err; |
| 2249 | } |
| 2250 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2251 | if (!h2_frt_transfer_data(h2s)) |
| 2252 | return 0; |
| 2253 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2254 | /* call the upper layers to process the frame, then let the upper layer |
| 2255 | * notify the stream about any change. |
| 2256 | */ |
| 2257 | if (!h2s->cs) { |
Willy Tarreau | c1a2965 | 2019-08-06 10:11:02 +0200 | [diff] [blame] | 2258 | /* The upper layer has already closed, this may happen on |
| 2259 | * 4xx/redirects during POST, or when receiving a response |
| 2260 | * from an H2 server after the client has aborted. |
| 2261 | */ |
| 2262 | error = H2_ERR_CANCEL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2263 | goto strm_err; |
| 2264 | } |
| 2265 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2266 | if (h2c->st0 >= H2_CS_ERROR) |
| 2267 | return 0; |
| 2268 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2269 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2270 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2271 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /* check for completion : the callee will change this to FRAME_A or |
| 2275 | * FRAME_H once done. |
| 2276 | */ |
| 2277 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2278 | return 0; |
| 2279 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2280 | /* last frame */ |
| 2281 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2282 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2283 | if (h2s->st == H2_SS_OPEN) |
| 2284 | h2s->st = H2_SS_HREM; |
| 2285 | else |
| 2286 | h2s_close(h2s); |
| 2287 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2288 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2289 | /* RFC7540#8.1.2 */ |
| 2290 | error = H2_ERR_PROTOCOL_ERROR; |
| 2291 | goto strm_err; |
| 2292 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2295 | return 1; |
| 2296 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2297 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2298 | h2s_error(h2s, error); |
| 2299 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2300 | return 0; |
| 2301 | } |
| 2302 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2303 | /* process Rx frames to be demultiplexed */ |
| 2304 | static void h2_process_demux(struct h2c *h2c) |
| 2305 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2306 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2307 | struct h2_fh hdr; |
| 2308 | unsigned int padlen = 0; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2309 | int32_t old_iw = h2c->miw; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2310 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2311 | if (h2c->st0 >= H2_CS_ERROR) |
| 2312 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2313 | |
| 2314 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2315 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2316 | if (h2c->flags & H2_CF_IS_BACK) |
| 2317 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2318 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2319 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2320 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2321 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2322 | sess_log(h2c->conn->owner); |
| 2323 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2324 | goto fail; |
| 2325 | } |
| 2326 | |
| 2327 | h2c->max_id = 0; |
| 2328 | h2c->st0 = H2_CS_SETTINGS1; |
| 2329 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2330 | |
| 2331 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2332 | /* ensure that what is pending is a valid SETTINGS frame |
| 2333 | * without an ACK. |
| 2334 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2335 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2336 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2337 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2338 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2339 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2340 | sess_log(h2c->conn->owner); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2341 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2342 | goto fail; |
| 2343 | } |
| 2344 | |
| 2345 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2346 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2347 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2348 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2349 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2350 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2351 | goto fail; |
| 2352 | } |
| 2353 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2354 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2355 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2356 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2357 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2358 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2359 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2360 | goto fail; |
| 2361 | } |
| 2362 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2363 | /* that's OK, switch to FRAME_P to process it. This is |
| 2364 | * a SETTINGS frame whose header has already been |
| 2365 | * deleted above. |
| 2366 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2367 | padlen = 0; |
| 2368 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2369 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2370 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2371 | |
| 2372 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2373 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2374 | int ret = 0; |
| 2375 | |
| 2376 | if (h2c->st0 >= H2_CS_ERROR) |
| 2377 | break; |
| 2378 | |
| 2379 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | ab3ebbc | 2019-08-06 15:49:51 +0200 | [diff] [blame] | 2380 | h2c->rcvd_s = 0; |
| 2381 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2382 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2383 | break; |
| 2384 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2385 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2386 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2387 | if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2388 | /* only log if no other stream can report the error */ |
| 2389 | sess_log(h2c->conn->owner); |
| 2390 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2391 | break; |
| 2392 | } |
| 2393 | |
Christopher Faulet | 3d574a5 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2394 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2395 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2396 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2397 | * we read the pad length and drop it from the remaining |
| 2398 | * payload (one byte + the 9 remaining ones = 10 total |
| 2399 | * removed), so we have a frame payload starting after the |
| 2400 | * pad len. Flow controlled frames (DATA) also count the |
| 2401 | * padlen in the flow control, so it must be adjusted. |
| 2402 | */ |
| 2403 | if (hdr.len < 1) { |
| 2404 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2405 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2406 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2407 | goto fail; |
| 2408 | } |
| 2409 | hdr.len--; |
| 2410 | |
| 2411 | if (b_data(&h2c->dbuf) < 10) |
| 2412 | break; // missing padlen |
| 2413 | |
| 2414 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2415 | |
| 2416 | if (padlen > hdr.len) { |
| 2417 | /* RFC7540#6.1 : pad length = length of |
| 2418 | * frame payload or greater => error. |
| 2419 | */ |
| 2420 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2421 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2422 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2423 | goto fail; |
| 2424 | } |
| 2425 | |
| 2426 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2427 | h2c->rcvd_c++; |
| 2428 | h2c->rcvd_s++; |
| 2429 | } |
| 2430 | b_del(&h2c->dbuf, 1); |
| 2431 | } |
| 2432 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2433 | |
| 2434 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2435 | h2c->dfl = hdr.len; |
| 2436 | h2c->dsi = hdr.sid; |
| 2437 | h2c->dft = hdr.ft; |
| 2438 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2439 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2440 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2441 | |
| 2442 | /* check for minimum basic frame format validity */ |
| 2443 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2444 | if (ret != H2_ERR_NO_ERROR) { |
| 2445 | h2c_error(h2c, ret); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2446 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2447 | sess_log(h2c->conn->owner); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2448 | goto fail; |
| 2449 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2450 | } |
| 2451 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2452 | /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here. |
| 2453 | * H2_CS_FRAME_P indicates an incomplete previous operation |
| 2454 | * (most often the first attempt) and requires some validity |
| 2455 | * checks for the frame and the current state. The two other |
| 2456 | * ones are set after completion (or abortion) and must skip |
| 2457 | * validity checks. |
| 2458 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2459 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2460 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2461 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2462 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 2463 | h2c_read0_pending(h2c) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2464 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2465 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2466 | (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] | 2467 | /* we may have to signal the upper layers */ |
| 2468 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2469 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2470 | } |
| 2471 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2472 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2473 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2474 | goto strm_err; |
| 2475 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2476 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2477 | goto valid_frame_type; |
| 2478 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2479 | if (h2s->st == H2_SS_IDLE && |
| 2480 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2481 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2482 | * this state MUST be treated as a connection error |
| 2483 | */ |
| 2484 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2485 | if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2486 | /* only log if no other stream can report the error */ |
| 2487 | sess_log(h2c->conn->owner); |
| 2488 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2489 | break; |
| 2490 | } |
| 2491 | |
Willy Tarreau | 85ee6e8 | 2019-11-24 14:57:53 +0100 | [diff] [blame] | 2492 | if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) { |
| 2493 | /* only PUSH_PROMISE would be permitted here */ |
| 2494 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2495 | break; |
| 2496 | } |
| 2497 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2498 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2499 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2500 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2501 | * this state MUST be treated as a stream error. |
| 2502 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2503 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2504 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2505 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2506 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2507 | else |
| 2508 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2509 | goto strm_err; |
| 2510 | } |
| 2511 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2512 | /* Below the management of frames received in closed state is a |
| 2513 | * bit hackish because the spec makes strong differences between |
| 2514 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2515 | * in both directions. In addition to this, the creation of a |
| 2516 | * new stream reusing the identifier of a closed one will be |
| 2517 | * detected here. Given that we cannot keep track of all closed |
| 2518 | * streams forever, we consider that unknown closed streams were |
| 2519 | * closed on RST received, which allows us to respond with an |
| 2520 | * RST without breaking the connection (eg: to abort a transfer). |
| 2521 | * Some frames have to be silently ignored as well. |
| 2522 | */ |
| 2523 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2524 | 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] | 2525 | /* #5.1.1: The identifier of a newly |
| 2526 | * established stream MUST be numerically |
| 2527 | * greater than all streams that the initiating |
| 2528 | * endpoint has opened or reserved. This |
| 2529 | * governs streams that are opened using a |
| 2530 | * HEADERS frame and streams that are reserved |
| 2531 | * using PUSH_PROMISE. An endpoint that |
| 2532 | * receives an unexpected stream identifier |
| 2533 | * MUST respond with a connection error. |
| 2534 | */ |
| 2535 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2536 | goto strm_err; |
| 2537 | } |
| 2538 | |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2539 | if (h2s->flags & H2_SF_RST_RCVD && |
| 2540 | !(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] | 2541 | /* RFC7540#5.1:closed: an endpoint that |
| 2542 | * receives any frame other than PRIORITY after |
| 2543 | * receiving a RST_STREAM MUST treat that as a |
| 2544 | * stream error of type STREAM_CLOSED. |
| 2545 | * |
| 2546 | * Note that old streams fall into this category |
| 2547 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2548 | * |
| 2549 | * However, we cannot generalize this to all frame types. Those |
| 2550 | * carrying compression state must still be processed before |
| 2551 | * being dropped or we'll desynchronize the decoder. This can |
| 2552 | * happen with request trailers received after sending an |
| 2553 | * RST_STREAM, or with header/trailers responses received after |
| 2554 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | eb5be9d | 2019-09-26 08:47:15 +0200 | [diff] [blame] | 2555 | * |
| 2556 | * In addition, since our CLOSED streams always carry the |
| 2557 | * RST_RCVD bit, we don't want to accidently catch valid |
| 2558 | * frames for a closed stream, i.e. RST/PRIO/WU. |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2559 | */ |
| 2560 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2561 | h2c->st0 = H2_CS_FRAME_E; |
| 2562 | goto strm_err; |
| 2563 | } |
| 2564 | |
| 2565 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2566 | * result of sending a RST_STREAM frame, the peer that |
| 2567 | * receives the RST_STREAM might have already sent |
| 2568 | * frames on the stream that cannot be withdrawn. An |
| 2569 | * endpoint MUST ignore frames that it receives on |
| 2570 | * closed streams after it has sent a RST_STREAM |
| 2571 | * frame. An endpoint MAY choose to limit the period |
| 2572 | * over which it ignores frames and treat frames that |
| 2573 | * arrive after this time as being in error. |
| 2574 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2575 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2576 | /* RFC7540#5.1:closed: any frame other than |
| 2577 | * PRIO/WU/RST in this state MUST be treated as |
| 2578 | * a connection error |
| 2579 | */ |
| 2580 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2581 | h2c->dft != H2_FT_PRIORITY && |
| 2582 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2583 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2584 | goto strm_err; |
| 2585 | } |
| 2586 | } |
| 2587 | } |
| 2588 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2589 | #if 0 |
| 2590 | // problem below: it is not possible to completely ignore such |
| 2591 | // streams as we need to maintain the compression state as well |
| 2592 | // and for this we need to completely process these frames (eg: |
| 2593 | // HEADERS frames) as well as counting DATA frames to emit |
| 2594 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2595 | // This is a typical case of layer violation where the |
| 2596 | // transported contents are critical to the connection's |
| 2597 | // validity and must be ignored at the same time :-( |
| 2598 | |
| 2599 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2600 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2601 | */ |
| 2602 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2603 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2604 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2605 | h2c->dfl -= ret; |
| 2606 | ret = h2c->dfl == 0; |
| 2607 | goto strm_err; |
| 2608 | } |
| 2609 | #endif |
| 2610 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2611 | valid_frame_type: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2612 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2613 | case H2_FT_SETTINGS: |
| 2614 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2615 | ret = h2c_handle_settings(h2c); |
| 2616 | |
| 2617 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2618 | ret = h2c_ack_settings(h2c); |
| 2619 | break; |
| 2620 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2621 | case H2_FT_PING: |
| 2622 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2623 | ret = h2c_handle_ping(h2c); |
| 2624 | |
| 2625 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2626 | ret = h2c_ack_ping(h2c); |
| 2627 | break; |
| 2628 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2629 | case H2_FT_WINDOW_UPDATE: |
| 2630 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2631 | ret = h2c_handle_window_update(h2c, h2s); |
| 2632 | break; |
| 2633 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2634 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2635 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2636 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2637 | * frames' parsers consume all following CONTINUATION |
| 2638 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2639 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2640 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2641 | if (!(h2c->flags & H2_CF_IS_BACK)) |
| 2642 | sess_log(h2c->conn->owner); |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2643 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2645 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2646 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2647 | if (h2c->flags & H2_CF_IS_BACK) |
| 2648 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2649 | else |
| 2650 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2651 | if (tmp_h2s) { |
| 2652 | h2s = tmp_h2s; |
| 2653 | ret = 1; |
| 2654 | } |
| 2655 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2656 | break; |
| 2657 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2658 | case H2_FT_DATA: |
| 2659 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2660 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2661 | |
| 2662 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2663 | ret = h2c_send_strm_wu(h2c); |
| 2664 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2665 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2666 | case H2_FT_PRIORITY: |
| 2667 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2668 | ret = h2c_handle_priority(h2c); |
| 2669 | break; |
| 2670 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2671 | case H2_FT_RST_STREAM: |
| 2672 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2673 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2674 | break; |
| 2675 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2676 | case H2_FT_GOAWAY: |
| 2677 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2678 | ret = h2c_handle_goaway(h2c); |
| 2679 | break; |
| 2680 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2681 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2682 | default: |
| 2683 | /* drop frames that we ignore. They may be larger than |
| 2684 | * the buffer so we drain all of their contents until |
| 2685 | * we reach the end. |
| 2686 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2687 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2688 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2689 | h2c->dfl -= ret; |
| 2690 | ret = h2c->dfl == 0; |
| 2691 | } |
| 2692 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2693 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2694 | /* We may have to send an RST if not done yet */ |
| 2695 | if (h2s->st == H2_SS_ERROR) |
| 2696 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2697 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2698 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2699 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2700 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2701 | /* error or missing data condition met above ? */ |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2702 | if (ret <= 0) { |
| 2703 | if (h2c->flags & H2_CF_RCVD_SHUT) |
| 2704 | h2c->flags |= H2_CF_END_REACHED; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2705 | break; |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2706 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2707 | |
| 2708 | if (h2c->st0 != H2_CS_FRAME_H) { |
Christopher Faulet | e12a26f | 2019-09-26 16:38:28 +0200 | [diff] [blame] | 2709 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2710 | b_del(&h2c->dbuf, ret); |
| 2711 | h2c->dfl -= ret; |
| 2712 | if (!h2c->dfl) |
| 2713 | h2c->st0 = H2_CS_FRAME_H; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2714 | } |
| 2715 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2716 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2717 | if (h2c->rcvd_c > 0 && |
| 2718 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2719 | h2c_send_conn_wu(h2c); |
| 2720 | |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2721 | done: |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2722 | if (h2s && h2s->cs && |
| 2723 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 2724 | h2c_read0_pending(h2c) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2725 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2726 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2727 | (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] | 2728 | /* we may have to signal the upper layers */ |
| 2729 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2730 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2731 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2733 | if (old_iw != h2c->miw) |
| 2734 | h2c_unblock_sfctl(h2c); |
| 2735 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2736 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2737 | return; |
| 2738 | |
| 2739 | fail: |
| 2740 | /* we can go here on missing data, blocked response or error, but we |
| 2741 | * need to check if we've met a short read condition. |
| 2742 | */ |
| 2743 | if (h2c->flags & H2_CF_RCVD_SHUT) |
| 2744 | h2c->flags |= H2_CF_END_REACHED; |
| 2745 | goto done; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2748 | /* resume each h2s eligible for sending in list head <head> */ |
| 2749 | static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head) |
| 2750 | { |
| 2751 | struct h2s *h2s, *h2s_back; |
| 2752 | |
| 2753 | list_for_each_entry_safe(h2s, h2s_back, head, list) { |
| 2754 | if (h2c->mws <= 0 || |
| 2755 | h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2756 | h2c->st0 >= H2_CS_ERROR) |
| 2757 | break; |
| 2758 | |
| 2759 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | d4e72e2 | 2020-01-10 18:20:15 +0100 | [diff] [blame] | 2760 | |
| 2761 | if (LIST_ADDED(&h2s->sending_list)) |
| 2762 | continue; |
| 2763 | |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2764 | /* For some reason, the upper layer failed to subscribe again, |
| 2765 | * so remove it from the send_list |
| 2766 | */ |
| 2767 | if (!h2s->send_wait) { |
| 2768 | LIST_DEL_INIT(&h2s->list); |
| 2769 | continue; |
| 2770 | } |
| 2771 | |
| 2772 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2773 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
| 2774 | tasklet_wakeup(h2s->send_wait->tasklet); |
| 2775 | } |
| 2776 | } |
| 2777 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2778 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2779 | * the end. |
| 2780 | */ |
| 2781 | static int h2_process_mux(struct h2c *h2c) |
| 2782 | { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2783 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2784 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2785 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2786 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 21178a5 | 2019-10-23 11:06:35 +0200 | [diff] [blame] | 2787 | if (h2c->st0 == H2_CS_ERROR) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2788 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2789 | goto fail; |
| 2790 | } |
| 2791 | h2c->st0 = H2_CS_SETTINGS1; |
| 2792 | } |
| 2793 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2794 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2795 | return 1; |
| 2796 | } |
| 2797 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2798 | /* start by sending possibly pending window updates */ |
Willy Tarreau | 9c497c2 | 2019-08-06 15:39:32 +0200 | [diff] [blame] | 2799 | if (h2c->rcvd_s > 0 && |
| 2800 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2801 | h2c_send_strm_wu(h2c) < 0) |
| 2802 | goto fail; |
| 2803 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2804 | if (h2c->rcvd_c > 0 && |
| 2805 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2806 | h2c_send_conn_wu(h2c) < 0) |
| 2807 | goto fail; |
| 2808 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2809 | /* First we always process the flow control list because the streams |
| 2810 | * waiting there were already elected for immediate emission but were |
| 2811 | * blocked just on this. |
| 2812 | */ |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2813 | h2_resume_each_sending_h2s(h2c, &h2c->fctl_list); |
| 2814 | h2_resume_each_sending_h2s(h2c, &h2c->send_list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2815 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2816 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2817 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2818 | if (h2c->st0 == H2_CS_ERROR) { |
| 2819 | if (h2c->max_id >= 0) { |
| 2820 | h2c_send_goaway_error(h2c, NULL); |
| 2821 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2826 | } |
| 2827 | return 1; |
| 2828 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2829 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2830 | } |
| 2831 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2832 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2833 | /* Attempt to read data, and subscribe if none available. |
| 2834 | * The function returns 1 if data has been received, otherwise zero. |
| 2835 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2836 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2837 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2838 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2839 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2840 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2841 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2842 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2843 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2844 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2845 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2846 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2847 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2848 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2849 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2850 | if (!buf) { |
| 2851 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2852 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2853 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2854 | |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2855 | if (h2c->flags & H2_CF_RCVD_SHUT) |
| 2856 | return 0; |
| 2857 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2858 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2859 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2860 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2861 | /* HTX in use : try to pre-align the buffer like the |
| 2862 | * rxbufs will be to optimize memory copies. We'll make |
| 2863 | * sure that the frame header lands at the end of the |
| 2864 | * HTX block to alias it upon recv. We cannot use the |
| 2865 | * head because rcv_buf() will realign the buffer if |
| 2866 | * it's empty. Thus we cheat and pretend we already |
| 2867 | * have a few bytes there. |
| 2868 | */ |
| 2869 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2870 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2871 | } |
| 2872 | else |
| 2873 | max = b_room(buf); |
| 2874 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2875 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2876 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2877 | else |
| 2878 | ret = 0; |
| 2879 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2880 | |
Willy Tarreau | e70050e | 2021-02-05 11:41:46 +0100 | [diff] [blame^] | 2881 | if (max && !ret) { |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2882 | if (conn_xprt_read0_pending(h2c->conn)) |
| 2883 | h2c->flags |= H2_CF_RCVD_SHUT; |
Willy Tarreau | e70050e | 2021-02-05 11:41:46 +0100 | [diff] [blame^] | 2884 | else if (h2_recv_allowed(h2c)) |
Willy Tarreau | 3b94d98 | 2021-01-20 10:53:13 +0100 | [diff] [blame] | 2885 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event); |
| 2886 | } |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2887 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2888 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2889 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2890 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2891 | } |
| 2892 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2893 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2894 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2895 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2896 | } |
| 2897 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2898 | /* Try to send data if possible. |
| 2899 | * The function returns 1 if data have been sent, otherwise zero. |
| 2900 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2901 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2902 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2903 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2904 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2905 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2906 | |
| 2907 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2908 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2909 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2910 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2911 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2912 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2913 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2914 | } |
| 2915 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2916 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2917 | * pending streams into the existing buffer until it's reportedly full |
| 2918 | * or the end of send requests is reached. Then it tries to send this |
| 2919 | * buffer's contents out, marks it not full if at least one byte could |
| 2920 | * be sent, and tries again. |
| 2921 | * |
| 2922 | * The snd_buf() function normally takes a "flags" argument which may |
| 2923 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2924 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2925 | * connection is streaming lots of data (used to increase TLS record |
| 2926 | * size at the expense of latency). The former can be sent any time |
| 2927 | * there's a buffer full flag, as it indicates at least one stream |
| 2928 | * attempted to send and failed so there are pending data. An |
| 2929 | * alternative would be to set it as long as there's an active stream |
| 2930 | * but that would be problematic for ACKs until we have an absolute |
| 2931 | * guarantee that all waiters have at least one byte to send. The |
| 2932 | * latter should possibly not be set for now. |
| 2933 | */ |
| 2934 | |
| 2935 | done = 0; |
| 2936 | while (!done) { |
| 2937 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2938 | unsigned int released = 0; |
| 2939 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2940 | |
| 2941 | /* fill as much as we can into the current buffer */ |
| 2942 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2943 | done = h2_process_mux(h2c); |
| 2944 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2945 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2946 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2947 | |
Christopher Faulet | 7163351 | 2020-10-22 16:24:58 +0200 | [diff] [blame] | 2948 | if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) || |
| 2949 | (h2c->st0 == H2_CS_ERROR2) || (h2c->flags & H2_CF_GOAWAY_FAILED)) |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2950 | break; |
| 2951 | |
| 2952 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2953 | flags |= CO_SFL_MSG_MORE; |
| 2954 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2955 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2956 | if (b_data(buf)) { |
| 2957 | 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] | 2958 | if (!ret) { |
| 2959 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2960 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2961 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2962 | sent = 1; |
| 2963 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2964 | if (b_data(buf)) { |
| 2965 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2966 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2967 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2968 | } |
| 2969 | b_free(buf); |
| 2970 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2971 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2972 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2973 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2974 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2975 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2976 | /* wrote at least one byte, the buffer is not full anymore */ |
Christopher Faulet | 0742308 | 2019-10-24 10:31:01 +0200 | [diff] [blame] | 2977 | if (sent) |
| 2978 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2979 | } |
| 2980 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2981 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2982 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2983 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2984 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2985 | /* We're not full anymore, so we can wake any task that are waiting |
| 2986 | * for us. |
| 2987 | */ |
Willy Tarreau | fa9a040 | 2020-01-10 17:01:29 +0100 | [diff] [blame] | 2988 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) |
| 2989 | h2_resume_each_sending_h2s(h2c, &h2c->send_list); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2990 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2991 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2992 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2993 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2994 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2995 | 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] | 2996 | 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] | 2997 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2998 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2999 | } |
| 3000 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3001 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 3002 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 3003 | { |
| 3004 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3005 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 3006 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3007 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3008 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3009 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3010 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 3011 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3012 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3013 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3014 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3015 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3016 | /* callback called on any event by the connection handler. |
| 3017 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 3018 | * destruction of the connection (which normally doesn not happen in h2). |
| 3019 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3020 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3021 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3022 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | e70050e | 2021-02-05 11:41:46 +0100 | [diff] [blame^] | 3024 | if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) && |
| 3025 | (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3026 | h2_process_demux(h2c); |
| 3027 | |
| 3028 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3029 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3030 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3031 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3032 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 3033 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3034 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 3035 | |
Willy Tarreau | 318822b | 2020-10-13 18:09:15 +0200 | [diff] [blame] | 3036 | if (unlikely(h2c->proxy->state == PR_STSTOPPED) && !(h2c->flags & H2_CF_IS_BACK)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 3037 | /* frontend is stopping, reload likely in progress, let's try |
| 3038 | * to announce a graceful shutdown if not yet done. We don't |
| 3039 | * care if it fails, it will be tried again later. |
| 3040 | */ |
| 3041 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3042 | if (h2c->last_sid < 0) |
| 3043 | h2c->last_sid = (1U << 31) - 1; |
| 3044 | h2c_send_goaway_error(h2c, NULL); |
| 3045 | } |
| 3046 | } |
| 3047 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3048 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3049 | * If we received early data, and the handshake is done, wake |
| 3050 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3051 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3052 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 3053 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 3054 | struct eb32_node *node; |
| 3055 | struct h2s *h2s; |
| 3056 | |
| 3057 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 3058 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 3059 | |
| 3060 | while (node) { |
| 3061 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 3062 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 3063 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3064 | node = eb32_next(node); |
| 3065 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3066 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3067 | |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 3068 | if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 3069 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 3070 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 3071 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3072 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3073 | |
| 3074 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3075 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3076 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3077 | return -1; |
| 3078 | } |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3079 | |
| 3080 | /* connections in error must be removed from the idle lists */ |
| 3081 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3082 | LIST_DEL_LOCKED(&conn->list); |
| 3083 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3084 | } |
| 3085 | else if (h2c->st0 == H2_CS_ERROR) { |
| 3086 | /* connections in error must be removed from the idle lists */ |
| 3087 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3088 | LIST_DEL_LOCKED(&conn->list); |
| 3089 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3092 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3093 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3094 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3095 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3096 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 3097 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3098 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3099 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 3100 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 3101 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3102 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 3103 | if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3104 | if (h2c_may_expire(h2c)) |
| 3105 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3106 | else |
| 3107 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3108 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3109 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3110 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3111 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3112 | return 0; |
| 3113 | } |
| 3114 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3115 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3116 | static int h2_wake(struct connection *conn) |
| 3117 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3118 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3119 | |
| 3120 | return (h2_process(h2c)); |
| 3121 | } |
| 3122 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3123 | /* Connection timeout management. The principle is that if there's no receipt |
| 3124 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3125 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3126 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3127 | * GOAWAY frame. |
| 3128 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3129 | 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] | 3130 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3131 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3132 | int expired = tick_is_expired(t->expire, now_ms); |
| 3133 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3134 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3135 | return t; |
| 3136 | |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3137 | if (h2c && !h2c_may_expire(h2c)) { |
| 3138 | /* we do still have streams but all of them are idle, waiting |
| 3139 | * for the data layer, so we must not enforce the timeout here. |
| 3140 | */ |
| 3141 | t->expire = TICK_ETERNITY; |
| 3142 | return t; |
| 3143 | } |
| 3144 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3145 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3146 | |
| 3147 | if (!h2c) { |
| 3148 | /* resources were already deleted */ |
| 3149 | return NULL; |
| 3150 | } |
| 3151 | |
| 3152 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3153 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3154 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3156 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3157 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3158 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3159 | } |
| 3160 | |
| 3161 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3162 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3163 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3164 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3165 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3166 | 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] | 3167 | unsigned int released = 0; |
| 3168 | struct buffer *buf; |
| 3169 | |
| 3170 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3171 | if (b_data(buf)) { |
| 3172 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3173 | if (!ret) |
| 3174 | break; |
| 3175 | b_del(buf, ret); |
| 3176 | if (b_data(buf)) |
| 3177 | break; |
| 3178 | b_free(buf); |
| 3179 | released++; |
| 3180 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3181 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3182 | |
| 3183 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3184 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3185 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3186 | |
Willy Tarreau | ab66e15 | 2019-10-31 15:36:30 +0100 | [diff] [blame] | 3187 | /* in any case this connection must not be considered idle anymore */ |
| 3188 | HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3189 | LIST_DEL_LOCKED(&h2c->conn->list); |
| 3190 | HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]); |
| 3191 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3192 | /* either we can release everything now or it will be done later once |
| 3193 | * the last stream closes. |
| 3194 | */ |
| 3195 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3196 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3197 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3198 | return NULL; |
| 3199 | } |
| 3200 | |
| 3201 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3202 | /*******************************************/ |
| 3203 | /* functions below are used by the streams */ |
| 3204 | /*******************************************/ |
| 3205 | |
| 3206 | /* |
| 3207 | * Attach a new stream to a connection |
| 3208 | * (Used for outgoing connections) |
| 3209 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3210 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3211 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3212 | struct conn_stream *cs; |
| 3213 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3214 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3215 | |
| 3216 | cs = cs_new(conn); |
| 3217 | if (!cs) |
| 3218 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3219 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3220 | if (!h2s) { |
| 3221 | cs_free(cs); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3225 | } |
| 3226 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3227 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3228 | * We have to scan because we may have some orphan streams. It might be |
| 3229 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3230 | * orphans. |
| 3231 | */ |
| 3232 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3233 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3234 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3235 | struct h2s *h2s; |
| 3236 | struct eb32_node *node; |
| 3237 | |
| 3238 | node = eb32_first(&h2c->streams_by_id); |
| 3239 | while (node) { |
| 3240 | h2s = container_of(node, struct h2s, by_id); |
| 3241 | if (h2s->cs) |
| 3242 | return h2s->cs; |
| 3243 | node = eb32_next(node); |
| 3244 | } |
| 3245 | return NULL; |
| 3246 | } |
| 3247 | |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3248 | static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3249 | { |
| 3250 | int ret = 0; |
| 3251 | struct h2c *h2c = conn->ctx; |
| 3252 | |
| 3253 | switch (mux_ctl) { |
| 3254 | case MUX_STATUS: |
| 3255 | /* Only consider the mux to be ready if we're done with |
| 3256 | * the preface and settings, and we had no error. |
| 3257 | */ |
| 3258 | if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR) |
| 3259 | ret |= MUX_STATUS_READY; |
| 3260 | return ret; |
| 3261 | default: |
| 3262 | return -1; |
| 3263 | } |
| 3264 | } |
| 3265 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3266 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3267 | * Destroy the mux and the associated connection, if it is no longer used |
| 3268 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3269 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3270 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3271 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3272 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3273 | 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] | 3274 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3278 | * Detach the stream from the connection and possibly release the connection. |
| 3279 | */ |
| 3280 | static void h2_detach(struct conn_stream *cs) |
| 3281 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3282 | struct h2s *h2s = cs->ctx; |
| 3283 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3284 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3285 | |
| 3286 | cs->ctx = NULL; |
| 3287 | if (!h2s) |
| 3288 | return; |
| 3289 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3290 | /* 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] | 3291 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3292 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3293 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3294 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3295 | /* |
| 3296 | * At this point, the stream_interface is supposed to have called |
| 3297 | * h2_unsubscribe(), so the only way there's still a |
| 3298 | * subscription that came from the stream_interface (as we |
| 3299 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3300 | * without the stream_interface involved) is that we subscribed |
| 3301 | * for sending, we woke the tasklet up and removed the |
| 3302 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3303 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3304 | * run yet. Make sure to handle that by explicitely setting |
| 3305 | * send_wait to NULL, as nothing else will do it for us. |
| 3306 | */ |
| 3307 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3308 | } |
| 3309 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3310 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3311 | h2c = h2s->h2c; |
| 3312 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3313 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3314 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3315 | !h2_frt_has_too_many_cs(h2c)) { |
| 3316 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3317 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3318 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3319 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3320 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3321 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3322 | * an ES or RST frame), so orphan it in this case. |
| 3323 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3324 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3325 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3326 | (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] | 3327 | return; |
| 3328 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3329 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3330 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3331 | /* unblock the connection if it was blocked on this |
| 3332 | * stream. |
| 3333 | */ |
| 3334 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3335 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3336 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3337 | } |
| 3338 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3339 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3340 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3341 | if (h2c->flags & H2_CF_IS_BACK && |
| 3342 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3343 | if (!(h2c->conn->flags & |
| 3344 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3345 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3346 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3347 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3348 | h2c->conn->owner = NULL; |
| 3349 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3350 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3351 | /* The server doesn't want it, let's kill the connection right away */ |
Olivier Houchard | 109ba13 | 2020-02-14 13:23:45 +0100 | [diff] [blame] | 3352 | h2c->conn->mux->destroy(h2c); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3353 | return; |
| 3354 | } |
| 3355 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3356 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3357 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3358 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3359 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3360 | return; |
| 3361 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3362 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3363 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3364 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3365 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3366 | struct server *srv = objt_server(h2c->conn->target); |
| 3367 | |
| 3368 | if (srv) { |
| 3369 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3370 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3371 | else |
| 3372 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3373 | } |
| 3374 | |
| 3375 | } |
| 3376 | } |
| 3377 | } |
| 3378 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3379 | /* We don't want to close right now unless we're removing the |
| 3380 | * last stream, and either the connection is in error, or it |
| 3381 | * reached the ID already specified in a GOAWAY frame received |
| 3382 | * or sent (as seen by last_sid >= 0). |
| 3383 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3384 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3385 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3386 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3387 | } |
| 3388 | else if (h2c->task) { |
Willy Tarreau | 534d8f9 | 2019-10-01 10:12:00 +0200 | [diff] [blame] | 3389 | if (h2c_may_expire(h2c)) |
| 3390 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3391 | else |
| 3392 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3393 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3394 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3397 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3398 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3399 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3400 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3401 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3402 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3403 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3404 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3405 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3406 | /* a connstream may require us to immediately kill the whole connection |
| 3407 | * for example because of a "tcp-request content reject" rule that is |
| 3408 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3409 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3410 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3411 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3412 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3413 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3414 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3415 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3416 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3417 | /* Nothing was never sent for this stream, so reset with |
| 3418 | * REFUSED_STREAM error to let the client retry the |
| 3419 | * request. |
| 3420 | */ |
| 3421 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3422 | } |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3423 | else { |
| 3424 | /* a final response was already provided, we don't want this |
| 3425 | * stream anymore. This may happen when the server responds |
| 3426 | * before the end of an upload and closes quickly (redirect, |
| 3427 | * deny, ...) |
| 3428 | */ |
| 3429 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3430 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3431 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3432 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3433 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3434 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3435 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3436 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3437 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3438 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3439 | done: |
| 3440 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3441 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3442 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3443 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3444 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3445 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3446 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3447 | h2s->send_wait = sw; |
| 3448 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3449 | h2s->send_wait = sw; |
| 3450 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3451 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3452 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3453 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3454 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3455 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3456 | } |
| 3457 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3458 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3459 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3460 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3461 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3462 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3463 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3464 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3465 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3466 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3467 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3468 | /* we can cleanly close using an empty data frame only after headers */ |
| 3469 | |
| 3470 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3471 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3472 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3473 | |
| 3474 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3475 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3476 | else |
| 3477 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3478 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3479 | /* a connstream may require us to immediately kill the whole connection |
| 3480 | * for example because of a "tcp-request content reject" rule that is |
| 3481 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3482 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3483 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3484 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3485 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3486 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3487 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3488 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3489 | else { |
| 3490 | /* Nothing was never sent for this stream, so reset with |
| 3491 | * REFUSED_STREAM error to let the client retry the |
| 3492 | * request. |
| 3493 | */ |
| 3494 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3495 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3496 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3497 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3498 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3499 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3500 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3501 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3502 | } |
| 3503 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3504 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3505 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3506 | done: |
| 3507 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3508 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3509 | |
| 3510 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3511 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3512 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3513 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3514 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3515 | h2s->send_wait = sw; |
| 3516 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3517 | h2s->send_wait = sw; |
| 3518 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3519 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3520 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3521 | /* let the handler know we want to shutw */ |
| 3522 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3523 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3526 | /* 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] | 3527 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3528 | * and prevented the last frame from being emitted. |
| 3529 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3530 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3531 | { |
| 3532 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3533 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3534 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3535 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3536 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3537 | h2_do_shutw(h2s); |
| 3538 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3539 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3540 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3541 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3542 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3543 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3544 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3545 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3546 | if (!h2s->cs) { |
| 3547 | h2s_destroy(h2s); |
| 3548 | if (h2c_is_dead(h2c)) |
| 3549 | h2_release(h2c); |
| 3550 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3551 | } |
| 3552 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3553 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3554 | } |
| 3555 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3556 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3557 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3558 | { |
| 3559 | struct h2s *h2s = cs->ctx; |
| 3560 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3561 | if (cs->flags & CS_FL_KILL_CONN) |
| 3562 | h2s->flags |= H2_SF_KILL_CONN; |
| 3563 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3564 | if (!mode) |
| 3565 | return; |
| 3566 | |
| 3567 | h2_do_shutr(h2s); |
| 3568 | } |
| 3569 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3570 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3571 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3572 | { |
| 3573 | struct h2s *h2s = cs->ctx; |
| 3574 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3575 | if (cs->flags & CS_FL_KILL_CONN) |
| 3576 | h2s->flags |= H2_SF_KILL_CONN; |
| 3577 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3578 | h2_do_shutw(h2s); |
| 3579 | } |
| 3580 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3581 | /* 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] | 3582 | * HTX request or response depending on the connection's side. Returns a |
| 3583 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3584 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3585 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3586 | * 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] | 3587 | * |
| 3588 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3589 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3590 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3591 | * leaving a hole between the main frame and the beginning of the next one. |
| 3592 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3593 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3594 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3595 | * |
| 3596 | * A buffer at the beginning of processing may look like this : |
| 3597 | * |
| 3598 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3599 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3600 | * `---^---------^-----^--------------^--------------^------^---' |
| 3601 | * | | <-----> | | |
| 3602 | * area | dpl | wrap |
| 3603 | * |<--------------> | |
| 3604 | * | dfl | |
| 3605 | * |<-------------------------------------------------->| |
| 3606 | * head data |
| 3607 | * |
| 3608 | * Padding is automatically overwritten when folding, participating to the |
| 3609 | * hole size after dfl : |
| 3610 | * |
| 3611 | * ,---.------------------------.-----.--------------.------.---. |
| 3612 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3613 | * `---^------------------------^-----^--------------^------^---' |
| 3614 | * | | <-----> | | |
| 3615 | * area | hole | wrap |
| 3616 | * |<-----------------------> | |
| 3617 | * | dfl | |
| 3618 | * |<-------------------------------------------------->| |
| 3619 | * head data |
| 3620 | * |
| 3621 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3622 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3623 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3624 | * |
| 3625 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3626 | * so that the function can update the following flags : |
| 3627 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3628 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3629 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3630 | * |
| 3631 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3632 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3633 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3634 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3635 | 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] | 3636 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3637 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3638 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3639 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3640 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3641 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3642 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3643 | int flen; // header frame len |
| 3644 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3645 | int ret = 0; |
| 3646 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3647 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3648 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3649 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3650 | next_frame: |
| 3651 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3652 | goto leave; // incomplete input frame |
| 3653 | |
| 3654 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3655 | * this case, we'll try to paste it immediately after the initial |
| 3656 | * HEADERS frame payload and kill any possible padding. The initial |
| 3657 | * frame's length will be increased to represent the concatenation |
| 3658 | * of the two frames. The next frame is read from position <tlen> |
| 3659 | * and written at position <flen> (minus padding if some is present). |
| 3660 | */ |
| 3661 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3662 | struct h2_fh hdr; |
| 3663 | int clen; // CONTINUATION frame's payload length |
| 3664 | |
| 3665 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3666 | /* no more data, the buffer may be full, either due to |
| 3667 | * too large a frame or because of too large a hole that |
| 3668 | * we're going to compact at the end. |
| 3669 | */ |
| 3670 | goto leave; |
| 3671 | } |
| 3672 | |
| 3673 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3674 | /* RFC7540#6.10: frame of unexpected type */ |
| 3675 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3676 | goto fail; |
| 3677 | } |
| 3678 | |
| 3679 | if (hdr.sid != h2c->dsi) { |
| 3680 | /* RFC7540#6.10: frame of different stream */ |
| 3681 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3682 | goto fail; |
| 3683 | } |
| 3684 | |
| 3685 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3686 | /* RFC7540#4.2: invalid frame length */ |
| 3687 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3688 | goto fail; |
| 3689 | } |
| 3690 | |
| 3691 | /* detect when we must stop aggragating frames */ |
| 3692 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3693 | |
| 3694 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3695 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3696 | if (clen > hdr.len) |
| 3697 | clen = hdr.len; |
| 3698 | |
| 3699 | /* Move the frame's payload over the padding, hole and frame |
| 3700 | * header. At least one of hole or dpl is null (see diagrams |
| 3701 | * above). The hole moves after the new aggragated frame. |
| 3702 | */ |
| 3703 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3704 | h2c->dfl += clen - h2c->dpl; |
| 3705 | hole += h2c->dpl + 9; |
| 3706 | h2c->dpl = 0; |
| 3707 | goto next_frame; |
| 3708 | } |
| 3709 | |
| 3710 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3711 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3712 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3713 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3714 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3715 | copy = alloc_trash_chunk(); |
| 3716 | if (!copy) { |
| 3717 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3718 | goto fail; |
| 3719 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3720 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3721 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3722 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3723 | } |
| 3724 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3725 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3726 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3727 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3728 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3729 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3730 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3731 | } |
| 3732 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3733 | if (flen < 5) { |
| 3734 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3735 | goto fail; |
| 3736 | } |
| 3737 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3738 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3739 | flen -= 5; |
| 3740 | } |
| 3741 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3742 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3743 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3744 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3745 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3746 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3747 | /* we can't retry a failed decompression operation so we must be very |
| 3748 | * careful not to take any risks. In practice the output buffer is |
| 3749 | * always empty except maybe for trailers, in which case we simply have |
| 3750 | * to wait for the upper layer to finish consuming what is available. |
| 3751 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3752 | |
| 3753 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3754 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3755 | if (!htx_is_empty(htx)) { |
| 3756 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3757 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3758 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3759 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3760 | if (b_data(rxbuf)) { |
| 3761 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3762 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3763 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3764 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3765 | rxbuf->head = 0; |
| 3766 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3767 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3768 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3769 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3770 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3771 | sizeof(list)/sizeof(list[0]), tmp); |
| 3772 | if (outlen < 0) { |
| 3773 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3774 | goto fail; |
| 3775 | } |
| 3776 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3777 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3778 | * the parser's state to commit these changes and allow us to later |
| 3779 | * fail solely on the stream if needed. |
| 3780 | */ |
| 3781 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3782 | h2c->dfl = hole = 0; |
| 3783 | h2c->st0 = H2_CS_FRAME_H; |
| 3784 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3785 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3786 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3787 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3788 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3789 | goto trailers; |
| 3790 | |
| 3791 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3792 | if (htx) { |
| 3793 | /* HTX mode */ |
| 3794 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3795 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3796 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3797 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3798 | } else { |
| 3799 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3800 | 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] | 3801 | if (outlen > 0) |
| 3802 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3803 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3804 | |
| 3805 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3806 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3807 | goto fail; |
| 3808 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3809 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3810 | if (msgf & H2_MSGF_BODY) { |
| 3811 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3812 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3813 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3814 | if (htx) |
| 3815 | htx->extra = *body_len; |
| 3816 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3817 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3818 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3819 | } |
| 3820 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3821 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3822 | /* indicate that a HEADERS frame was received for this stream, except |
| 3823 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3824 | * expected. |
| 3825 | */ |
| 3826 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3827 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3828 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3829 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3830 | /* Mark the end of message, either using EOM in HTX or with the |
| 3831 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3832 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3833 | * we must not leave an HTX trailers block not followed by an |
| 3834 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3835 | * 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] | 3836 | */ |
| 3837 | if (htx) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 3838 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3839 | goto fail; |
| 3840 | } |
| 3841 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3842 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3843 | goto fail; |
| 3844 | } |
| 3845 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3846 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3847 | /* success */ |
| 3848 | ret = 1; |
| 3849 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3850 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3851 | /* 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] | 3852 | * move the remaining data over it. |
| 3853 | */ |
| 3854 | if (hole) { |
| 3855 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3856 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3857 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3858 | b_sub(&h2c->dbuf, hole); |
| 3859 | } |
| 3860 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3861 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3862 | /* too large frames */ |
| 3863 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3864 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3867 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3868 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3869 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3870 | return ret; |
| 3871 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3872 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3873 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3874 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3875 | |
| 3876 | trailers: |
| 3877 | /* This is the last HEADERS frame hence a trailer */ |
| 3878 | |
| 3879 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3880 | /* It's a trailer but it's missing ES flag */ |
| 3881 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3882 | goto fail; |
| 3883 | } |
| 3884 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 3885 | /* Trailers terminate a DATA sequence. In HTX we always handle them. In |
| 3886 | * legacy, when using chunks, we have to emit the 0 CRLF marker first |
| 3887 | * and then handle the trailers. For other modes, the trailers are |
| 3888 | * silently dropped. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3889 | */ |
| 3890 | if (htx) { |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3891 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3892 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3893 | } |
| 3894 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3895 | /* Legacy mode with chunked encoding : we must finalize the |
| 3896 | * data block message emit the trailing CRLF */ |
| 3897 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3898 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3899 | |
| 3900 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3901 | if (outlen > 0) |
| 3902 | b_add(rxbuf, outlen); |
| 3903 | else |
| 3904 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3908 | } |
| 3909 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3910 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3911 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3912 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3913 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3914 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3915 | * 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] | 3916 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3917 | * send the current frame, 0 if it couldn't complete, in which case |
| 3918 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3919 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3920 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3921 | * have checked the frame header and ensured that the frame was complete or the |
| 3922 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3923 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3924 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3925 | { |
| 3926 | struct h2c *h2c = h2s->h2c; |
| 3927 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3928 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3929 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3930 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3931 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3932 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3933 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3934 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3935 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3936 | if (!csbuf) { |
| 3937 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3938 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3939 | } |
| 3940 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3941 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3942 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3943 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3944 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3945 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3946 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3947 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3948 | if (flen > b_data(&h2c->dbuf)) { |
| 3949 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3950 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3951 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3952 | } |
| 3953 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3954 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3955 | unsigned int sent; |
| 3956 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3957 | block1 = htx_free_data_space(htx); |
| 3958 | if (!block1) { |
| 3959 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3960 | goto fail; |
| 3961 | } |
| 3962 | if (flen > block1) |
| 3963 | flen = block1; |
| 3964 | |
| 3965 | /* here, flen is the max we can copy into the output buffer */ |
| 3966 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3967 | if (flen > block1) |
| 3968 | flen = block1; |
| 3969 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3970 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3971 | |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3972 | b_del(&h2c->dbuf, sent); |
| 3973 | h2c->dfl -= sent; |
| 3974 | h2c->rcvd_c += sent; |
| 3975 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3976 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3977 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3978 | h2s->body_len -= sent; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3979 | htx->extra = h2s->body_len; |
| 3980 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3981 | |
| 3982 | if (sent < flen) { |
| 3983 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3984 | goto fail; |
| 3985 | } |
| 3986 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3987 | goto try_again; |
| 3988 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3989 | else if (unlikely(b_space_wraps(csbuf) && |
| 3990 | flen + chklen <= b_room(csbuf) && |
| 3991 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3992 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3993 | * not too many data in the buffer, let's defragment it and try |
| 3994 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3995 | */ |
| 3996 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3997 | } |
| 3998 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3999 | /* chunked-encoding requires more room */ |
| 4000 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4001 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4002 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 4003 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 4004 | (chklen < 1048576) ? 4 : 8; |
| 4005 | chklen += 4; // CRLF, CRLF |
| 4006 | } |
| 4007 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4008 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4009 | if (flen + chklen > b_room(csbuf)) { |
| 4010 | if (chklen >= b_room(csbuf)) { |
| 4011 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4012 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4013 | } |
| 4014 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4018 | /* emit the chunk size */ |
| 4019 | unsigned int chksz = flen; |
| 4020 | char str[10]; |
| 4021 | char *beg; |
| 4022 | |
| 4023 | beg = str + sizeof(str); |
| 4024 | *--beg = '\n'; |
| 4025 | *--beg = '\r'; |
| 4026 | do { |
| 4027 | *--beg = hextab[chksz & 0xF]; |
| 4028 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4029 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4030 | } |
| 4031 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4032 | /* Block1 is the length of the first block before the buffer wraps, |
| 4033 | * block2 is the optional second block to reach the end of the frame. |
| 4034 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4035 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4036 | if (block1 > flen) |
| 4037 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4038 | block2 = flen - block1; |
| 4039 | |
| 4040 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4041 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4042 | |
| 4043 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4044 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4045 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4046 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4047 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4048 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4051 | /* now mark the input data as consumed (will be deleted from the buffer |
| 4052 | * by the caller when seeing FRAME_A after sending the window update). |
| 4053 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4054 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4055 | h2c->dfl -= flen; |
| 4056 | h2c->rcvd_c += flen; |
| 4057 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 4058 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 4059 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 4060 | h2s->body_len -= flen; |
| 4061 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4062 | if (h2c->dfl > h2c->dpl) { |
| 4063 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4064 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4065 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4066 | } |
| 4067 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 4068 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 4069 | /* here we're done with the frame, all the payload (except padding) was |
| 4070 | * transferred. |
| 4071 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4072 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4073 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 4074 | if (htx) { |
| 4075 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 4076 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4077 | goto fail; |
| 4078 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 4079 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4080 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 4081 | /* emit the trailing 0 CRLF CRLF */ |
| 4082 | if (b_room(csbuf) < 5) { |
| 4083 | h2c->flags |= H2_CF_DEM_SFULL; |
| 4084 | goto fail; |
| 4085 | } |
| 4086 | chklen += 5; |
| 4087 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 4088 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 4089 | } |
| 4090 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 4091 | h2c->rcvd_c += h2c->dpl; |
| 4092 | h2c->rcvd_s += h2c->dpl; |
| 4093 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4094 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4095 | if (htx) |
| 4096 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 4097 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4098 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4099 | if (htx) |
| 4100 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4101 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4104 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 4105 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 4106 | * number of bytes sent. The caller must check the stream's status to detect |
| 4107 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4108 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4109 | 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] | 4110 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4111 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4112 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4113 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4114 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4115 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4116 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4117 | int es_now = 0; |
| 4118 | int ret = 0; |
| 4119 | int hdr; |
| 4120 | |
| 4121 | if (h2c_mux_busy(h2c, h2s)) { |
| 4122 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4123 | return 0; |
| 4124 | } |
| 4125 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4126 | /* First, try to parse the H1 response and index it into <list>. |
| 4127 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 4128 | * block does not wrap and we can safely read it this way without |
| 4129 | * having to realign the buffer. |
| 4130 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4131 | 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] | 4132 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4133 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 4134 | /* incomplete or invalid response, this is abnormal coming from |
| 4135 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 4136 | * so that won't be fixed, raise an error now. |
| 4137 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4138 | * FIXME: we should instead add the ability to only return a |
| 4139 | * 502 bad gateway. But in theory this is not supposed to |
| 4140 | * happen. |
| 4141 | */ |
| 4142 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4143 | ret = 0; |
| 4144 | goto end; |
| 4145 | } |
| 4146 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4147 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4148 | |
| 4149 | /* certain statuses have no body or an empty one, regardless of |
| 4150 | * what the headers say. |
| 4151 | */ |
| 4152 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4153 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4154 | h1m->curr_len = h1m->body_len = 0; |
| 4155 | } |
| 4156 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4157 | /* no contents, claim c-len is present and set to zero */ |
| 4158 | h1m->flags &= ~H1_MF_CHNK; |
| 4159 | h1m->flags |= H1_MF_CLEN; |
| 4160 | h1m->curr_len = h1m->body_len = 0; |
| 4161 | } |
| 4162 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4163 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4164 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4165 | if (!h2_get_buf(h2c, mbuf)) { |
| 4166 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4167 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4168 | return 0; |
| 4169 | } |
| 4170 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4171 | chunk_reset(&outbuf); |
| 4172 | |
| 4173 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4174 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4175 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4176 | break; |
| 4177 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4178 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4181 | if (outbuf.size < 9) |
| 4182 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4183 | |
| 4184 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4185 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4186 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4187 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4188 | |
| 4189 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4190 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4191 | /* this is an unparsable response */ |
| 4192 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4193 | ret = 0; |
| 4194 | goto end; |
| 4195 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4196 | |
| 4197 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4198 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4199 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4200 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | /* encode all headers, stop at empty name */ |
| 4204 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4205 | /* these ones do not exist in H2 and must be dropped. */ |
| 4206 | if (isteq(list[hdr].n, ist("connection")) || |
| 4207 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4208 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4209 | isteq(list[hdr].n, ist("upgrade")) || |
| 4210 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4211 | continue; |
| 4212 | |
| 4213 | if (isteq(list[hdr].n, ist(""))) |
| 4214 | break; // end |
| 4215 | |
| 4216 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4217 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4218 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4219 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4220 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4225 | 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] | 4226 | es_now = 1; |
| 4227 | |
| 4228 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4229 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4230 | |
| 4231 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4232 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4233 | |
| 4234 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4235 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4236 | |
| 4237 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4238 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4239 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4240 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4241 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4242 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4243 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4244 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4245 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4246 | h2s->flags |= H2_SF_ES_SENT; |
| 4247 | if (h2s->st == H2_SS_OPEN) |
| 4248 | h2s->st = H2_SS_HLOC; |
| 4249 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4250 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4251 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4252 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4253 | /* 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] | 4254 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4255 | 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] | 4256 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4257 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4258 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4259 | |
| 4260 | /* 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] | 4261 | |
| 4262 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4263 | //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] | 4264 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4265 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4266 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4267 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4268 | h1m_init_res(h1m); |
| 4269 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4270 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4271 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4272 | ret = 0; |
| 4273 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4274 | } |
| 4275 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4276 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4277 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4278 | * the number of bytes sent. The caller must check the stream's status to |
| 4279 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4280 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4281 | 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] | 4282 | { |
| 4283 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4284 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4285 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4286 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4287 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4288 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4289 | int es_now = 0; |
| 4290 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4291 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4292 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4293 | |
| 4294 | if (h2c_mux_busy(h2c, h2s)) { |
| 4295 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4296 | goto end; |
| 4297 | } |
| 4298 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4299 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4300 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4301 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4302 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4303 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4304 | goto end; |
| 4305 | } |
| 4306 | |
| 4307 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4308 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4309 | goto end; |
| 4310 | |
| 4311 | chunk_reset(&outbuf); |
| 4312 | |
| 4313 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4314 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4315 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4316 | break; |
| 4317 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4318 | /* If there are pending data in the output buffer, and we have |
| 4319 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4320 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4321 | * is full and wait, to save some slow realign calls. |
| 4322 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4323 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4324 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4325 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4326 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4327 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4328 | goto end; |
| 4329 | } |
| 4330 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4331 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4335 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4336 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4337 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4338 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4339 | goto end; |
| 4340 | } |
| 4341 | |
| 4342 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4343 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4344 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4345 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4346 | |
| 4347 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4348 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4349 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4350 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4351 | break; |
| 4352 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4353 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4354 | if ((long long)size > h1m->curr_len) |
| 4355 | size = h1m->curr_len; |
| 4356 | break; |
| 4357 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4358 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4359 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 4360 | if (ret <= 0) { |
Christopher Faulet | 307f31e | 2020-08-05 14:37:13 +0200 | [diff] [blame] | 4361 | /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ? |
| 4362 | * It should not happen except if opposite connection was closed. |
| 4363 | * Note: check there is at least something to parse to be sure the chunk crlf |
| 4364 | * is truncated. |
| 4365 | */ |
| 4366 | if (max) { |
| 4367 | h1m->err_pos = ofs + max + ret; |
| 4368 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4369 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4370 | goto end; |
| 4371 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4372 | max -= ret; |
| 4373 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4374 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4375 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4378 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4379 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4380 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 4381 | if (ret <= 0) { |
Christopher Faulet | 307f31e | 2020-08-05 14:37:13 +0200 | [diff] [blame] | 4382 | /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ? |
| 4383 | * It should not happen except if opposite connection was closed. |
| 4384 | * Note: check there is at least something to parse to be sure the chunke size |
| 4385 | * is truncated. |
| 4386 | */ |
| 4387 | if (max) { |
| 4388 | h1m->err_pos = ofs + max + ret; |
| 4389 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4390 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4391 | goto end; |
| 4392 | } |
| 4393 | |
| 4394 | size = chunk; |
| 4395 | h1m->curr_len = chunk; |
| 4396 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4397 | max -= ret; |
| 4398 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4399 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4400 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4401 | if (!size) |
| 4402 | goto send_empty; |
| 4403 | } |
| 4404 | |
| 4405 | /* in MSG_DATA state, continue below */ |
| 4406 | size = h1m->curr_len; |
| 4407 | break; |
| 4408 | } |
| 4409 | |
| 4410 | /* we have in <size> the exact number of bytes we need to copy from |
| 4411 | * the H1 buffer. We need to check this against the connection's and |
| 4412 | * the stream's send windows, and to ensure that this fits in the max |
| 4413 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4414 | * the frame header). The connection's flow control is applied last so |
| 4415 | * that we can use a separate list of streams which are immediately |
| 4416 | * unblocked on window opening. Note: we don't implement padding. |
| 4417 | */ |
| 4418 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4419 | if (size > max) |
| 4420 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4421 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4422 | if (size > h2s_mws(h2s)) |
| 4423 | size = h2s_mws(h2s); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4424 | |
| 4425 | if (size <= 0) { |
| 4426 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4427 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4428 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 55dc084 | 2019-10-22 10:04:39 +0200 | [diff] [blame] | 4429 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4430 | goto end; |
| 4431 | } |
| 4432 | |
| 4433 | if (h2c->mfs && size > h2c->mfs) |
| 4434 | size = h2c->mfs; |
| 4435 | |
| 4436 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4437 | /* It doesn't fit at once. If it at least fits once split and |
| 4438 | * the amount of data to move is low, let's defragment the |
| 4439 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4440 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4441 | if (b_space_wraps(mbuf) && |
| 4442 | (size + 9 <= b_room(mbuf)) && |
| 4443 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4444 | goto realign_again; |
| 4445 | size = outbuf.size - 9; |
| 4446 | } |
| 4447 | |
| 4448 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4449 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4450 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4451 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4452 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4453 | goto end; |
| 4454 | } |
| 4455 | |
| 4456 | if (size > h2c->mws) |
| 4457 | size = h2c->mws; |
| 4458 | |
| 4459 | if (size <= 0) { |
| 4460 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4461 | goto end; |
| 4462 | } |
| 4463 | |
| 4464 | /* copy whatever we can */ |
| 4465 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4466 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4467 | if (ret == 1) |
| 4468 | len2 = 0; |
| 4469 | |
| 4470 | if (!ret || len1 + len2 < size) { |
| 4471 | /* FIXME: must normally never happen */ |
| 4472 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4473 | goto end; |
| 4474 | } |
| 4475 | |
| 4476 | /* limit len1/len2 to size */ |
| 4477 | if (len1 + len2 > size) { |
| 4478 | int sub = len1 + len2 - size; |
| 4479 | |
| 4480 | if (len2 > sub) |
| 4481 | len2 -= sub; |
| 4482 | else { |
| 4483 | sub -= len2; |
| 4484 | len2 = 0; |
| 4485 | len1 -= sub; |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4490 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4491 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4492 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4493 | |
| 4494 | send_empty: |
| 4495 | /* we may need to add END_STREAM */ |
| 4496 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4497 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4498 | * |
| 4499 | * FIXME: what we do here is not correct because we send end_stream |
| 4500 | * before knowing if we'll have to send a HEADERS frame for the |
| 4501 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4502 | * after the end of trailers, so it will be left to the caller to |
| 4503 | * eat it. The right way to do it would be to measure trailers here |
| 4504 | * and to send ES only if there are no trailers. |
| 4505 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4506 | */ |
| 4507 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4508 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4509 | es_now = 1; |
| 4510 | |
| 4511 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4512 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4513 | |
| 4514 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4515 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4516 | |
| 4517 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4518 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4519 | |
| 4520 | /* consume incoming H1 response */ |
| 4521 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4522 | max -= size; |
| 4523 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4524 | total += size; |
| 4525 | h1m->curr_len -= size; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4526 | h2s->sws -= size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4527 | h2c->mws -= size; |
| 4528 | |
| 4529 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4530 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4531 | goto new_frame; |
| 4532 | } |
| 4533 | } |
| 4534 | |
| 4535 | if (es_now) { |
| 4536 | if (h2s->st == H2_SS_OPEN) |
| 4537 | h2s->st = H2_SS_HLOC; |
| 4538 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4539 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4540 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4541 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4542 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4543 | total += max; |
| 4544 | ofs += max; |
| 4545 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4546 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4547 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4548 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4549 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4550 | h2s->flags |= H2_SF_ES_SENT; |
| 4551 | } |
| 4552 | |
| 4553 | end: |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4554 | 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] | 4555 | return total; |
| 4556 | } |
| 4557 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4558 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4559 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4560 | * must check the stream's status to detect any error which might have happened |
| 4561 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4562 | * from the message. The htx message is assumed to be valid since produced from |
| 4563 | * the internal code, hence it contains a start line, an optional series of |
| 4564 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4565 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4566 | */ |
| 4567 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4568 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4569 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4570 | struct h2c *h2c = h2s->h2c; |
| 4571 | struct htx_blk *blk; |
| 4572 | struct htx_blk *blk_end; |
| 4573 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4574 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4575 | struct htx_sl *sl; |
| 4576 | enum htx_blk_type type; |
| 4577 | int es_now = 0; |
| 4578 | int ret = 0; |
| 4579 | int hdr; |
| 4580 | int idx; |
| 4581 | |
| 4582 | if (h2c_mux_busy(h2c, h2s)) { |
| 4583 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4584 | return 0; |
| 4585 | } |
| 4586 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4587 | /* determine the first block which must not be deleted, blk_end may |
| 4588 | * be NULL if all blocks have to be deleted. |
| 4589 | */ |
| 4590 | idx = htx_get_head(htx); |
| 4591 | blk_end = NULL; |
| 4592 | while (idx != -1) { |
| 4593 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4594 | idx = htx_get_next(htx, idx); |
| 4595 | if (type == HTX_BLK_EOH) { |
| 4596 | if (idx != -1) |
| 4597 | blk_end = htx_get_blk(htx, idx); |
| 4598 | break; |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4603 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4604 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4605 | ALREADY_CHECKED(blk); |
| 4606 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4607 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4608 | if (h2s->status < 100 || h2s->status > 999) |
| 4609 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4610 | |
| 4611 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4612 | hdr = 0; |
| 4613 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4614 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4615 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4616 | blk = htx_get_blk(htx, idx); |
| 4617 | type = htx_get_blk_type(blk); |
| 4618 | |
| 4619 | if (type == HTX_BLK_UNUSED) |
| 4620 | continue; |
| 4621 | |
| 4622 | if (type != HTX_BLK_HDR) |
| 4623 | break; |
| 4624 | |
| 4625 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4626 | goto fail; |
| 4627 | |
| 4628 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4629 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4630 | hdr++; |
| 4631 | } |
| 4632 | |
| 4633 | /* marker for end of headers */ |
| 4634 | list[hdr].n = ist(""); |
| 4635 | |
| 4636 | if (h2s->status == 204 || h2s->status == 304) { |
| 4637 | /* no contents, claim c-len is present and set to zero */ |
| 4638 | es_now = 1; |
| 4639 | } |
| 4640 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4641 | mbuf = br_tail(h2c->mbuf); |
| 4642 | retry: |
| 4643 | if (!h2_get_buf(h2c, mbuf)) { |
| 4644 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4645 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4646 | return 0; |
| 4647 | } |
| 4648 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4649 | chunk_reset(&outbuf); |
| 4650 | |
| 4651 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4652 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4653 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4654 | break; |
| 4655 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4656 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | if (outbuf.size < 9) |
| 4660 | goto full; |
| 4661 | |
| 4662 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4663 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4664 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4665 | outbuf.data = 9; |
| 4666 | |
| 4667 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4668 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4669 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4670 | goto realign_again; |
| 4671 | goto full; |
| 4672 | } |
| 4673 | |
| 4674 | /* encode all headers, stop at empty name */ |
| 4675 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4676 | /* these ones do not exist in H2 and must be dropped. */ |
| 4677 | if (isteq(list[hdr].n, ist("connection")) || |
| 4678 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4679 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4680 | isteq(list[hdr].n, ist("upgrade")) || |
| 4681 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4682 | continue; |
| 4683 | |
| 4684 | if (isteq(list[hdr].n, ist(""))) |
| 4685 | break; // end |
| 4686 | |
| 4687 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4688 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4689 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4690 | goto realign_again; |
| 4691 | goto full; |
| 4692 | } |
| 4693 | } |
| 4694 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4695 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4696 | * FIXME: we should also set it when we know for sure that the |
| 4697 | * content-length is zero as well as on 204/304 |
| 4698 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4699 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4700 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4701 | es_now = 1; |
| 4702 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4703 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4704 | es_now = 1; |
| 4705 | |
| 4706 | /* update the frame's size */ |
| 4707 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4708 | |
| 4709 | if (es_now) |
| 4710 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4711 | |
| 4712 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4713 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4714 | |
| 4715 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4716 | * 1xx responses, another HEADERS frame is expected. |
| 4717 | */ |
| 4718 | if (h2s->status >= 200 || h2s->status == 101) |
| 4719 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4721 | if (es_now) { |
| 4722 | h2s->flags |= H2_SF_ES_SENT; |
| 4723 | if (h2s->st == H2_SS_OPEN) |
| 4724 | h2s->st = H2_SS_HLOC; |
| 4725 | else |
| 4726 | h2s_close(h2s); |
| 4727 | } |
| 4728 | |
| 4729 | /* OK we could properly deliver the response */ |
| 4730 | |
| 4731 | /* remove all header blocks including the EOH and compute the |
| 4732 | * corresponding size. |
| 4733 | * |
| 4734 | * FIXME: We should remove everything when es_now is set. |
| 4735 | */ |
| 4736 | ret = 0; |
| 4737 | idx = htx_get_head(htx); |
| 4738 | blk = htx_get_blk(htx, idx); |
| 4739 | while (blk != blk_end) { |
| 4740 | ret += htx_get_blksz(blk); |
| 4741 | blk = htx_remove_blk(htx, blk); |
| 4742 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4743 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4744 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4745 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4746 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4747 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4748 | end: |
| 4749 | return ret; |
| 4750 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4751 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4752 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4753 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4754 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4755 | ret = 0; |
| 4756 | goto end; |
| 4757 | fail: |
| 4758 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4759 | * list etc go here (unrecoverable errors). |
| 4760 | */ |
| 4761 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4762 | ret = 0; |
| 4763 | goto end; |
| 4764 | } |
| 4765 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4766 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4767 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4768 | * must check the stream's status to detect any error which might have happened |
| 4769 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4770 | * from the message. The htx message is assumed to be valid since produced from |
| 4771 | * the internal code, hence it contains a start line, an optional series of |
| 4772 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4773 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4774 | */ |
| 4775 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4776 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4777 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4778 | struct h2c *h2c = h2s->h2c; |
| 4779 | struct htx_blk *blk; |
| 4780 | struct htx_blk *blk_end; |
| 4781 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4782 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4783 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4784 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4785 | enum htx_blk_type type; |
| 4786 | int es_now = 0; |
| 4787 | int ret = 0; |
| 4788 | int hdr; |
| 4789 | int idx; |
| 4790 | |
| 4791 | if (h2c_mux_busy(h2c, h2s)) { |
| 4792 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4793 | return 0; |
| 4794 | } |
| 4795 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4796 | /* determine the first block which must not be deleted, blk_end may |
| 4797 | * be NULL if all blocks have to be deleted. |
| 4798 | */ |
| 4799 | idx = htx_get_head(htx); |
| 4800 | blk_end = NULL; |
| 4801 | while (idx != -1) { |
| 4802 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4803 | idx = htx_get_next(htx, idx); |
| 4804 | if (type == HTX_BLK_EOH) { |
| 4805 | if (idx != -1) |
| 4806 | blk_end = htx_get_blk(htx, idx); |
| 4807 | break; |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4812 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4813 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4814 | ALREADY_CHECKED(blk); |
| 4815 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4816 | meth = htx_sl_req_meth(sl); |
| 4817 | path = htx_sl_req_uri(sl); |
| 4818 | |
| 4819 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4820 | hdr = 0; |
| 4821 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4822 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4823 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4824 | blk = htx_get_blk(htx, idx); |
| 4825 | type = htx_get_blk_type(blk); |
| 4826 | |
| 4827 | if (type == HTX_BLK_UNUSED) |
| 4828 | continue; |
| 4829 | |
| 4830 | if (type != HTX_BLK_HDR) |
| 4831 | break; |
| 4832 | |
| 4833 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4834 | goto fail; |
| 4835 | |
| 4836 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4837 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4838 | hdr++; |
| 4839 | } |
| 4840 | |
| 4841 | /* marker for end of headers */ |
| 4842 | list[hdr].n = ist(""); |
| 4843 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4844 | mbuf = br_tail(h2c->mbuf); |
| 4845 | retry: |
| 4846 | if (!h2_get_buf(h2c, mbuf)) { |
| 4847 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4848 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4849 | return 0; |
| 4850 | } |
| 4851 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4852 | chunk_reset(&outbuf); |
| 4853 | |
| 4854 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4855 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4856 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4857 | break; |
| 4858 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4859 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | if (outbuf.size < 9) |
| 4863 | goto full; |
| 4864 | |
| 4865 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4866 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4867 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4868 | outbuf.data = 9; |
| 4869 | |
| 4870 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4871 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4872 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4873 | goto realign_again; |
| 4874 | goto full; |
| 4875 | } |
| 4876 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4877 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4878 | * - :authority set to the URI part (host:port) |
| 4879 | * - :method set to CONNECT |
| 4880 | * - :scheme and :path omitted |
| 4881 | */ |
| 4882 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4883 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4884 | struct ist scheme; |
| 4885 | |
| 4886 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4887 | scheme = ist("http"); |
| 4888 | else |
| 4889 | scheme = ist("https"); |
| 4890 | |
| 4891 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4892 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4893 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4894 | goto realign_again; |
| 4895 | goto full; |
| 4896 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4897 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4898 | /* encode the path, which necessarily is the second one */ |
| 4899 | if (!hpack_encode_path(&outbuf, path)) { |
| 4900 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4901 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4902 | goto realign_again; |
| 4903 | goto full; |
| 4904 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4905 | |
| 4906 | /* look for the Host header and place it in :authority */ |
| 4907 | auth = ist2(NULL, 0); |
| 4908 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4909 | if (isteq(list[hdr].n, ist(""))) |
| 4910 | break; // end |
| 4911 | |
| 4912 | if (isteq(list[hdr].n, ist("host"))) { |
| 4913 | auth = list[hdr].v; |
| 4914 | break; |
| 4915 | } |
| 4916 | } |
| 4917 | } |
| 4918 | else { |
| 4919 | /* for CONNECT, :authority is taken from the path */ |
| 4920 | auth = path; |
| 4921 | } |
| 4922 | |
| 4923 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4924 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4925 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4926 | goto realign_again; |
| 4927 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4928 | } |
| 4929 | |
| 4930 | /* encode all headers, stop at empty name */ |
| 4931 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4932 | struct ist n = list[hdr].n; |
| 4933 | struct ist v = list[hdr].v; |
| 4934 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4935 | /* these ones do not exist in H2 and must be dropped. */ |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4936 | if (isteq(n, ist("connection")) || |
| 4937 | isteq(n, ist("host")) || |
| 4938 | isteq(n, ist("proxy-connection")) || |
| 4939 | isteq(n, ist("keep-alive")) || |
| 4940 | isteq(n, ist("upgrade")) || |
| 4941 | isteq(n, ist("transfer-encoding"))) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4942 | continue; |
| 4943 | |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4944 | if (isteq(n, ist("te"))) { |
| 4945 | /* "te" may only be sent with "trailers" if this value |
| 4946 | * is present, otherwise it must be deleted. |
| 4947 | */ |
| 4948 | v = istist(v, ist("trailers")); |
| 4949 | if (!v.ptr || (v.len > 8 && v.ptr[8] != ',')) |
| 4950 | continue; |
| 4951 | v = ist("trailers"); |
| 4952 | } |
| 4953 | |
| 4954 | if (isteq(n, ist(""))) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4955 | break; // end |
| 4956 | |
Willy Tarreau | 4928b6b | 2020-01-24 09:07:53 +0100 | [diff] [blame] | 4957 | if (!hpack_encode_header(&outbuf, n, v)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4958 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4959 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4960 | goto realign_again; |
| 4961 | goto full; |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | /* we may need to add END_STREAM if we have no body : |
| 4966 | * - request already closed, or : |
| 4967 | * - no transfer-encoding, and : |
| 4968 | * - no content-length or content-length:0 |
| 4969 | * Fixme: this doesn't take into account CONNECT requests. |
| 4970 | */ |
| 4971 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4972 | es_now = 1; |
| 4973 | |
| 4974 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4975 | es_now = 1; |
| 4976 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4977 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4978 | es_now = 1; |
| 4979 | |
| 4980 | /* update the frame's size */ |
| 4981 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4982 | |
| 4983 | if (es_now) |
| 4984 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4985 | |
| 4986 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4987 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4988 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4989 | h2s->st = H2_SS_OPEN; |
| 4990 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4991 | if (es_now) { |
| 4992 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4993 | h2s->flags |= H2_SF_ES_SENT; |
| 4994 | h2s->st = H2_SS_HLOC; |
| 4995 | } |
| 4996 | |
| 4997 | /* remove all header blocks including the EOH and compute the |
| 4998 | * corresponding size. |
| 4999 | * |
| 5000 | * FIXME: We should remove everything when es_now is set. |
| 5001 | */ |
| 5002 | ret = 0; |
| 5003 | idx = htx_get_head(htx); |
| 5004 | blk = htx_get_blk(htx, idx); |
| 5005 | while (blk != blk_end) { |
| 5006 | ret += htx_get_blksz(blk); |
| 5007 | blk = htx_remove_blk(htx, blk); |
| 5008 | } |
| 5009 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 5010 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5011 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5012 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 5013 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5014 | |
| 5015 | end: |
| 5016 | return ret; |
| 5017 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5018 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5019 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5020 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5021 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5022 | ret = 0; |
| 5023 | goto end; |
| 5024 | fail: |
| 5025 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5026 | * list etc go here (unrecoverable errors). |
| 5027 | */ |
| 5028 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5029 | ret = 0; |
| 5030 | goto end; |
| 5031 | } |
| 5032 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5033 | /* 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] | 5034 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 5035 | * caller must check the stream's status to detect any error which might have |
| 5036 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5037 | * 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] | 5038 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5039 | 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] | 5040 | { |
| 5041 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5042 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5043 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5044 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5045 | size_t total = 0; |
| 5046 | int es_now = 0; |
| 5047 | int bsize; /* htx block size */ |
| 5048 | int fsize; /* h2 frame size */ |
| 5049 | struct htx_blk *blk; |
| 5050 | enum htx_blk_type type; |
| 5051 | int idx; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5052 | int trunc_out; /* non-zero if truncated on out buf */ |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5053 | |
| 5054 | if (h2c_mux_busy(h2c, h2s)) { |
| 5055 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5056 | goto end; |
| 5057 | } |
| 5058 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5059 | htx = htx_from_buf(buf); |
| 5060 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5061 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 5062 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 5063 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5064 | */ |
| 5065 | |
| 5066 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5067 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5068 | goto end; |
| 5069 | |
| 5070 | idx = htx_get_head(htx); |
| 5071 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5072 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5073 | bsize = htx_get_blksz(blk); |
| 5074 | fsize = bsize; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5075 | trunc_out = 0; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5076 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 5077 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 5078 | if (h2s->flags & H2_SF_ES_SENT) { |
| 5079 | /* ES already sent */ |
| 5080 | htx_remove_blk(htx, blk); |
| 5081 | total++; // EOM counts as one byte |
| 5082 | count--; |
| 5083 | goto end; |
| 5084 | } |
| 5085 | } |
| 5086 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5087 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5088 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5089 | mbuf = br_tail(h2c->mbuf); |
| 5090 | retry: |
| 5091 | if (!h2_get_buf(h2c, mbuf)) { |
| 5092 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5093 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5094 | goto end; |
| 5095 | } |
| 5096 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5097 | /* Perform some optimizations to reduce the number of buffer copies. |
| 5098 | * First, if the mux's buffer is empty and the htx area contains |
| 5099 | * exactly one data block of the same size as the requested count, and |
| 5100 | * this count fits within the frame size, the stream's window size, and |
| 5101 | * the connection's window size, then it's possible to simply swap the |
| 5102 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 5103 | * length to match the entire DATA HTX block in the middle. In this |
| 5104 | * case we perform a true zero-copy operation from end-to-end. This is |
| 5105 | * the situation that happens all the time with large files. Second, if |
| 5106 | * this is not possible, but the mux's output buffer is empty, we still |
| 5107 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 5108 | * making the intermediary buffer's area point to the output buffer's |
| 5109 | * area. In this case we want to skip the HTX header to make sure that |
| 5110 | * copies remain aligned and that this operation remains possible all |
| 5111 | * the time. This goes for headers, data blocks and any data extracted |
| 5112 | * from the HTX blocks. |
| 5113 | */ |
| 5114 | if (unlikely(fsize == count && |
| 5115 | htx->used == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5116 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5117 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5118 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5119 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5120 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 5121 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 5122 | * frame into a buffer containing few data if it needs to be realigned, |
| 5123 | * and that it's also OK to copy few data without realigning. Otherwise |
| 5124 | * 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] | 5125 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5126 | if (fsize + 9 <= b_room(mbuf) && |
| 5127 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 5128 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5129 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5130 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5131 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5132 | goto retry; |
| 5133 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5134 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5135 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5136 | goto end; |
| 5137 | } |
| 5138 | |
| 5139 | /* map an H2 frame to the HTX block so that we can put the |
| 5140 | * frame header there. |
| 5141 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5142 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 5143 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5144 | |
| 5145 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 5146 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5147 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5148 | h2_set_frame_size(outbuf.area, fsize); |
| 5149 | |
| 5150 | /* update windows */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5151 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5152 | h2c->mws -= fsize; |
| 5153 | |
| 5154 | /* and exchange with our old area */ |
| 5155 | buf->area = old_area; |
| 5156 | buf->data = buf->head = 0; |
| 5157 | total += fsize; |
| 5158 | goto end; |
| 5159 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5160 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5161 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5162 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 5163 | |
| 5164 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5165 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5166 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5167 | break; |
| 5168 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5169 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5173 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5174 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5175 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5176 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5177 | goto end; |
| 5178 | } |
| 5179 | |
| 5180 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5181 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5182 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5183 | outbuf.data = 9; |
| 5184 | |
| 5185 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5186 | * the HTX buffer. We need to check this against the connection's and |
| 5187 | * the stream's send windows, and to ensure that this fits in the max |
| 5188 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5189 | * the frame header). The connection's flow control is applied last so |
| 5190 | * that we can use a separate list of streams which are immediately |
| 5191 | * unblocked on window opening. Note: we don't implement padding. |
| 5192 | */ |
| 5193 | |
| 5194 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5195 | * empty frame, thus we force it to zero here. |
| 5196 | */ |
| 5197 | if (type == HTX_BLK_EOM) |
| 5198 | bsize = fsize = 0; |
| 5199 | |
| 5200 | if (!fsize) |
| 5201 | goto send_empty; |
| 5202 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5203 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5204 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5205 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5206 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5207 | LIST_ADDQ(&h2c->blocked_list, &h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5208 | goto end; |
| 5209 | } |
| 5210 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5211 | if (fsize > count) |
| 5212 | fsize = count; |
| 5213 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5214 | if (fsize > h2s_mws(h2s)) |
| 5215 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5216 | |
| 5217 | if (h2c->mfs && fsize > h2c->mfs) |
| 5218 | fsize = h2c->mfs; // >0 |
| 5219 | |
| 5220 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5221 | /* It doesn't fit at once. If it at least fits once split and |
| 5222 | * the amount of data to move is low, let's defragment the |
| 5223 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5224 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5225 | if (b_space_wraps(mbuf) && |
| 5226 | (fsize + 9 <= b_room(mbuf)) && |
| 5227 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5228 | goto realign_again; |
| 5229 | fsize = outbuf.size - 9; |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5230 | trunc_out = 1; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5231 | |
| 5232 | if (fsize <= 0) { |
| 5233 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5234 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5235 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5236 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5237 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5238 | goto end; |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | if (h2c->mws <= 0) { |
| 5243 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5244 | goto end; |
| 5245 | } |
| 5246 | |
| 5247 | if (fsize > h2c->mws) |
| 5248 | fsize = h2c->mws; |
| 5249 | |
| 5250 | /* now let's copy this this into the output buffer */ |
| 5251 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5252 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5253 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5254 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5255 | |
| 5256 | send_empty: |
| 5257 | /* update the frame's size */ |
| 5258 | h2_set_frame_size(outbuf.area, fsize); |
| 5259 | |
| 5260 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5261 | * meeting EOM. We should optimize this later. |
| 5262 | */ |
| 5263 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5264 | total++; // EOM counts as one byte |
| 5265 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5266 | es_now = 1; |
| 5267 | } |
| 5268 | |
| 5269 | if (es_now) |
| 5270 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5271 | |
| 5272 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5273 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5274 | |
| 5275 | /* consume incoming HTX block, including EOM */ |
| 5276 | total += fsize; |
| 5277 | if (fsize == bsize) { |
| 5278 | htx_remove_blk(htx, blk); |
| 5279 | if (fsize) |
| 5280 | goto new_frame; |
| 5281 | } else { |
| 5282 | /* we've truncated this block */ |
| 5283 | htx_cut_data_blk(htx, blk, fsize); |
Willy Tarreau | 5fc1cfa | 2020-01-14 11:42:59 +0100 | [diff] [blame] | 5284 | if (trunc_out) |
| 5285 | goto new_frame; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | if (es_now) { |
| 5289 | if (h2s->st == H2_SS_OPEN) |
| 5290 | h2s->st = H2_SS_HLOC; |
| 5291 | else |
| 5292 | h2s_close(h2s); |
| 5293 | |
| 5294 | h2s->flags |= H2_SF_ES_SENT; |
| 5295 | } |
| 5296 | |
| 5297 | end: |
| 5298 | return total; |
| 5299 | } |
| 5300 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5301 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5302 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5303 | * processed. The caller must check the stream's status to detect any error |
| 5304 | * which might have happened subsequently to a successful send. The htx blocks |
| 5305 | * are automatically removed from the message. The htx message is assumed to be |
| 5306 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5307 | * 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] | 5308 | * as a single frame. The ES flag is always set. |
| 5309 | */ |
| 5310 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5311 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 5312 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5313 | struct h2c *h2c = h2s->h2c; |
| 5314 | struct htx_blk *blk; |
| 5315 | struct htx_blk *blk_end; |
| 5316 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5317 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5318 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5319 | int ret = 0; |
| 5320 | int hdr; |
| 5321 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5322 | |
| 5323 | if (h2c_mux_busy(h2c, h2s)) { |
| 5324 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5325 | goto end; |
| 5326 | } |
| 5327 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5328 | /* determine the first block which must not be deleted, blk_end may |
| 5329 | * be NULL if all blocks have to be deleted. also get trailers. |
| 5330 | */ |
| 5331 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5332 | blk_end = NULL; |
| 5333 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5334 | hdr = 0; |
| 5335 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5336 | blk = htx_get_blk(htx, idx); |
| 5337 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5338 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5339 | if (type == HTX_BLK_UNUSED) |
| 5340 | continue; |
| 5341 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5342 | if (type == HTX_BLK_EOT) { |
| 5343 | if (idx != -1) |
| 5344 | blk_end = blk; |
| 5345 | break; |
| 5346 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5347 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5348 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5349 | |
| 5350 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5351 | goto fail; |
| 5352 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5353 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 5354 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 5355 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5356 | } |
| 5357 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5358 | /* marker for end of trailers */ |
| 5359 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5360 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5361 | mbuf = br_tail(h2c->mbuf); |
| 5362 | retry: |
| 5363 | if (!h2_get_buf(h2c, mbuf)) { |
| 5364 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5365 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5366 | goto end; |
| 5367 | } |
| 5368 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5369 | chunk_reset(&outbuf); |
| 5370 | |
| 5371 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5372 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5373 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5374 | break; |
| 5375 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5376 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | if (outbuf.size < 9) |
| 5380 | goto full; |
| 5381 | |
| 5382 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5383 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5384 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5385 | outbuf.data = 9; |
| 5386 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5387 | /* encode all headers */ |
| 5388 | for (idx = 0; idx < hdr; idx++) { |
| 5389 | /* these ones do not exist in H2 or must not appear in |
| 5390 | * trailers and must be dropped. |
| 5391 | */ |
| 5392 | if (isteq(list[idx].n, ist("host")) || |
| 5393 | isteq(list[idx].n, ist("content-length")) || |
| 5394 | isteq(list[idx].n, ist("connection")) || |
| 5395 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5396 | isteq(list[idx].n, ist("keep-alive")) || |
| 5397 | isteq(list[idx].n, ist("upgrade")) || |
| 5398 | isteq(list[idx].n, ist("te")) || |
| 5399 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5400 | continue; |
| 5401 | |
| 5402 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5403 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5404 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5405 | goto realign_again; |
| 5406 | goto full; |
| 5407 | } |
| 5408 | } |
| 5409 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5410 | if (outbuf.data == 9) { |
| 5411 | /* here we have a problem, we have nothing to emit (either we |
| 5412 | * received an empty trailers block followed or we removed its |
| 5413 | * contents above). Because of this we can't send a HEADERS |
| 5414 | * frame, so we have to cheat and instead send an empty DATA |
| 5415 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5416 | */ |
| 5417 | outbuf.area[3] = H2_FT_DATA; |
| 5418 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5419 | } |
| 5420 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5421 | /* update the frame's size */ |
| 5422 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5423 | |
| 5424 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5425 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5426 | h2s->flags |= H2_SF_ES_SENT; |
| 5427 | |
| 5428 | if (h2s->st == H2_SS_OPEN) |
| 5429 | h2s->st = H2_SS_HLOC; |
| 5430 | else |
| 5431 | h2s_close(h2s); |
| 5432 | |
| 5433 | /* OK we could properly deliver the response */ |
| 5434 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5435 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5436 | ret = 0; |
| 5437 | idx = htx_get_head(htx); |
| 5438 | blk = htx_get_blk(htx, idx); |
| 5439 | while (blk != blk_end) { |
| 5440 | ret += htx_get_blksz(blk); |
| 5441 | blk = htx_remove_blk(htx, blk); |
| 5442 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5443 | |
| 5444 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5445 | ret += htx_get_blksz(blk_end); |
| 5446 | htx_remove_blk(htx, blk_end); |
| 5447 | } |
| 5448 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5449 | end: |
| 5450 | return ret; |
| 5451 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5452 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5453 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5454 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5455 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5456 | ret = 0; |
| 5457 | goto end; |
| 5458 | fail: |
| 5459 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5460 | * list etc go here (unrecoverable errors). |
| 5461 | */ |
| 5462 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5463 | ret = 0; |
| 5464 | goto end; |
| 5465 | } |
| 5466 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5467 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5468 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5469 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5470 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5471 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5472 | * returns 0 except for the error above. |
| 5473 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5474 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5475 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5476 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5477 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5478 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5479 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5480 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5481 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5482 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5483 | sw->events |= SUB_RETRY_RECV; |
| 5484 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5485 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5486 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5487 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5488 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5489 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5490 | sw->events |= SUB_RETRY_SEND; |
| 5491 | h2s->send_wait = sw; |
| 5492 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5493 | !LIST_ADDED(&h2s->list)) { |
| 5494 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5495 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5496 | else |
| 5497 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5498 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5499 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5500 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5501 | if (event_type != 0) |
| 5502 | return -1; |
| 5503 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5504 | } |
| 5505 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5506 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5507 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5508 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5509 | * It always returns zero. |
| 5510 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5511 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5512 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5513 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5514 | struct h2s *h2s = cs->ctx; |
| 5515 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5516 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5517 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5518 | BUG_ON(h2s->recv_wait != sw); |
| 5519 | sw->events &= ~SUB_RETRY_RECV; |
| 5520 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5521 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5522 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5523 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5524 | BUG_ON(h2s->send_wait != sw); |
| 5525 | LIST_DEL(&h2s->list); |
| 5526 | LIST_INIT(&h2s->list); |
| 5527 | sw->events &= ~SUB_RETRY_SEND; |
| 5528 | /* We were about to send, make sure it does not happen */ |
| 5529 | if (LIST_ADDED(&h2s->sending_list) && |
| 5530 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5531 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5532 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5533 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5534 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5535 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5536 | return 0; |
| 5537 | } |
| 5538 | |
| 5539 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5540 | /* Called from the upper layer, to receive data */ |
| 5541 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5542 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5543 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5544 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5545 | struct htx *h2s_htx = NULL; |
| 5546 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5547 | size_t ret = 0; |
| 5548 | |
| 5549 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5550 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5551 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5552 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5553 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5554 | * the HTX is empty. |
| 5555 | */ |
| 5556 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5557 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5558 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5559 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5560 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5561 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5562 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5563 | /* <buf> is empty and the message is small enough, swap the |
| 5564 | * buffers. */ |
| 5565 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5566 | htx_to_buf(buf_htx, buf); |
| 5567 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5568 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5569 | goto end; |
| 5570 | } |
| 5571 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5572 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5573 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5574 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5575 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5576 | if (htx_is_empty(buf_htx)) |
| 5577 | cs->flags |= CS_FL_EOI; |
| 5578 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5579 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5580 | 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] | 5581 | htx_to_buf(buf_htx, buf); |
| 5582 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5583 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5584 | } |
| 5585 | else { |
| 5586 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5587 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5588 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5589 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5590 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5591 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5592 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5593 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5594 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5595 | cs->flags |= CS_FL_EOI; |
Christopher Faulet | 11b1053 | 2020-10-08 15:38:41 +0200 | [diff] [blame] | 5596 | if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5597 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5598 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5599 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5600 | if (b_size(&h2s->rxbuf)) { |
| 5601 | b_free(&h2s->rxbuf); |
| 5602 | offer_buffers(NULL, tasks_run_queue); |
| 5603 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5604 | } |
| 5605 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5606 | if (ret && h2c->dsi == h2s->id) { |
| 5607 | /* demux is blocking on this stream's buffer */ |
| 5608 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5609 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5610 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5611 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5612 | return ret; |
| 5613 | } |
| 5614 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5615 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5616 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5617 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5618 | static void h2_stop_senders(struct h2c *h2c) |
| 5619 | { |
| 5620 | struct h2s *h2s, *h2s_back; |
| 5621 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5622 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5623 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5624 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5625 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5626 | } |
| 5627 | } |
| 5628 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5629 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5630 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5631 | * flags may be updated on the conn_stream. |
| 5632 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5633 | 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] | 5634 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5635 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5636 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5637 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5638 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5639 | struct htx *htx; |
| 5640 | struct htx_blk *blk; |
| 5641 | enum htx_blk_type btype; |
| 5642 | uint32_t bsize; |
| 5643 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5644 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5645 | /* If we were not just woken because we wanted to send but couldn't, |
| 5646 | * and there's somebody else that is waiting to send, do nothing, |
| 5647 | * we will subscribe later and be put at the end of the list |
| 5648 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5649 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5650 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5651 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5652 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5653 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5654 | /* We couldn't set it to NULL before, because we needed it in case |
| 5655 | * we had to cancel the tasklet |
| 5656 | */ |
| 5657 | h2s->send_wait = NULL; |
| 5658 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5659 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5660 | return 0; |
| 5661 | |
Willy Tarreau | 902df22 | 2019-10-31 15:48:18 +0100 | [diff] [blame] | 5662 | if (h2s->h2c->st0 >= H2_CS_ERROR) { |
| 5663 | cs->flags |= CS_FL_ERROR; |
| 5664 | return 0; |
| 5665 | } |
| 5666 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5667 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5668 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5669 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5670 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5671 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5672 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5673 | if (h2s->id == 0) { |
| 5674 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5675 | |
| 5676 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5677 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5678 | return 0; |
| 5679 | } |
| 5680 | |
| 5681 | eb32_delete(&h2s->by_id); |
| 5682 | h2s->by_id.key = h2s->id = id; |
| 5683 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5684 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5685 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5686 | } |
| 5687 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5688 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5689 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5690 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5691 | idx = htx_get_head(htx); |
| 5692 | blk = htx_get_blk(htx, idx); |
| 5693 | btype = htx_get_blk_type(blk); |
| 5694 | bsize = htx_get_blksz(blk); |
| 5695 | |
| 5696 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5697 | case HTX_BLK_REQ_SL: |
| 5698 | /* start-line before headers */ |
| 5699 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5700 | if (ret > 0) { |
| 5701 | total += ret; |
| 5702 | count -= ret; |
| 5703 | if (ret < bsize) |
| 5704 | goto done; |
| 5705 | } |
| 5706 | break; |
| 5707 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5708 | case HTX_BLK_RES_SL: |
| 5709 | /* start-line before headers */ |
| 5710 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5711 | if (ret > 0) { |
| 5712 | total += ret; |
| 5713 | count -= ret; |
| 5714 | if (ret < bsize) |
| 5715 | goto done; |
| 5716 | } |
| 5717 | break; |
| 5718 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5719 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5720 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5721 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5722 | * This EOM necessarily is one before trailers, as the EOM following |
| 5723 | * trailers would have been consumed by the trailers parser. |
| 5724 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5725 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5726 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5727 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5728 | total += ret; |
| 5729 | count -= ret; |
| 5730 | if (ret < bsize) |
| 5731 | goto done; |
| 5732 | } |
| 5733 | break; |
| 5734 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5735 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5736 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5737 | /* This is the first trailers block, all the subsequent ones AND |
| 5738 | * the EOM will be swallowed by the parser. |
| 5739 | */ |
| 5740 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5741 | if (ret > 0) { |
| 5742 | total += ret; |
| 5743 | count -= ret; |
| 5744 | if (ret < bsize) |
| 5745 | goto done; |
| 5746 | } |
| 5747 | break; |
| 5748 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5749 | default: |
| 5750 | htx_remove_blk(htx, blk); |
| 5751 | total += bsize; |
| 5752 | count -= bsize; |
| 5753 | break; |
| 5754 | } |
| 5755 | } |
| 5756 | goto done; |
| 5757 | } |
| 5758 | |
| 5759 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5760 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5761 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5762 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5763 | ret = -1; |
| 5764 | else |
| 5765 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5766 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5767 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5768 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5769 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5770 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5771 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5772 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5773 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5774 | if (unlikely((int)ret <= 0)) { |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 5775 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5776 | break; |
| 5777 | } |
Christopher Faulet | 6ad7cd9 | 2020-07-23 14:50:36 +0200 | [diff] [blame] | 5778 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5779 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5780 | total += count; |
| 5781 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5782 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5783 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5784 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5785 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5786 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5787 | break; |
| 5788 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5789 | |
| 5790 | total += ret; |
| 5791 | count -= ret; |
| 5792 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5793 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5794 | break; |
| 5795 | |
| 5796 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5797 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5798 | } |
| 5799 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5800 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5801 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5802 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5803 | * unprocessed trailers, abnormal extra data, ...) |
| 5804 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5805 | total += count; |
| 5806 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5807 | } |
| 5808 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5809 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5810 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5811 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5812 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5813 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5814 | } |
| 5815 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5816 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5817 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5818 | } else { |
| 5819 | b_del(buf, total); |
| 5820 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5821 | |
| 5822 | /* The mux is full, cancel the pending tasks */ |
| 5823 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5824 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5825 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5826 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5827 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5828 | * we read exactly what the caller specified, as with HTX the caller |
| 5829 | * will always give the buffer size, instead of the amount of data |
| 5830 | * available. |
| 5831 | */ |
| 5832 | if (htx && !b_data(buf)) |
| 5833 | total = orig_count; |
| 5834 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5835 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5836 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5837 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5838 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5839 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5840 | /* If we're waiting for flow control, and we got a shutr on the |
| 5841 | * connection, we will never be unlocked, so add an error on |
| 5842 | * the conn_stream. |
| 5843 | */ |
| 5844 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5845 | !b_data(&h2s->h2c->dbuf) && |
| 5846 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5847 | if (cs->flags & CS_FL_EOS) |
| 5848 | cs->flags |= CS_FL_ERROR; |
| 5849 | else |
| 5850 | cs->flags |= CS_FL_ERR_PENDING; |
| 5851 | } |
Willy Tarreau | bda92dd | 2019-10-02 10:49:59 +0200 | [diff] [blame] | 5852 | |
| 5853 | if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5854 | /* 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] | 5855 | LIST_DEL_INIT(&h2s->list); |
| 5856 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5857 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5858 | } |
| 5859 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5860 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5861 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5862 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5863 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5864 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5865 | struct eb32_node *node; |
| 5866 | int fctl_cnt = 0; |
| 5867 | int send_cnt = 0; |
| 5868 | int tree_cnt = 0; |
| 5869 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5870 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5871 | |
| 5872 | if (!h2c) |
| 5873 | return; |
| 5874 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5875 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5876 | fctl_cnt++; |
| 5877 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5878 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5879 | send_cnt++; |
| 5880 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5881 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5882 | node = eb32_first(&h2c->streams_by_id); |
| 5883 | while (node) { |
| 5884 | h2s = container_of(node, struct h2s, by_id); |
| 5885 | tree_cnt++; |
| 5886 | if (!h2s->cs) |
| 5887 | orph_cnt++; |
| 5888 | node = eb32_next(node); |
| 5889 | } |
| 5890 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5891 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5892 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5893 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5894 | " .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] | 5895 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5896 | " .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] | 5897 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5898 | 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] | 5899 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5900 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5901 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5902 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5903 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5904 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5905 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5906 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5907 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5908 | |
| 5909 | if (h2s) { |
| 5910 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5911 | h2s, h2s->id, h2s->flags, |
| 5912 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5913 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5914 | h2s->cs); |
| 5915 | if (h2s->cs) |
| 5916 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5917 | h2s->cs->flags, h2s->cs->data); |
| 5918 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5919 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5920 | |
| 5921 | /*******************************************************/ |
| 5922 | /* functions below are dedicated to the config parsers */ |
| 5923 | /*******************************************************/ |
| 5924 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5925 | /* config parser for global "tune.h2.header-table-size" */ |
| 5926 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5927 | struct proxy *defpx, const char *file, int line, |
| 5928 | char **err) |
| 5929 | { |
| 5930 | if (too_many_args(1, args, err, NULL)) |
| 5931 | return -1; |
| 5932 | |
| 5933 | h2_settings_header_table_size = atoi(args[1]); |
| 5934 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5935 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5936 | return -1; |
| 5937 | } |
| 5938 | return 0; |
| 5939 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5940 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5941 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5942 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5943 | struct proxy *defpx, const char *file, int line, |
| 5944 | char **err) |
| 5945 | { |
| 5946 | if (too_many_args(1, args, err, NULL)) |
| 5947 | return -1; |
| 5948 | |
| 5949 | h2_settings_initial_window_size = atoi(args[1]); |
| 5950 | if (h2_settings_initial_window_size < 0) { |
| 5951 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5952 | return -1; |
| 5953 | } |
| 5954 | return 0; |
| 5955 | } |
| 5956 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5957 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5958 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5959 | struct proxy *defpx, const char *file, int line, |
| 5960 | char **err) |
| 5961 | { |
| 5962 | if (too_many_args(1, args, err, NULL)) |
| 5963 | return -1; |
| 5964 | |
| 5965 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5966 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5967 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5968 | return -1; |
| 5969 | } |
| 5970 | return 0; |
| 5971 | } |
| 5972 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5973 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5974 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5975 | struct proxy *defpx, const char *file, int line, |
| 5976 | char **err) |
| 5977 | { |
| 5978 | if (too_many_args(1, args, err, NULL)) |
| 5979 | return -1; |
| 5980 | |
| 5981 | h2_settings_max_frame_size = atoi(args[1]); |
| 5982 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5983 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5984 | return -1; |
| 5985 | } |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5989 | |
| 5990 | /****************************************/ |
| 5991 | /* MUX initialization and instanciation */ |
| 5992 | /***************************************/ |
| 5993 | |
| 5994 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5995 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5996 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5997 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5998 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5999 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 6000 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 6001 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6002 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 6003 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6004 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 6005 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 6006 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 6007 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6008 | .shutr = h2_shutr, |
| 6009 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 6010 | .ctl = h2_ctl, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 6011 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 6012 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6013 | .name = "H2", |
| 6014 | }; |
| 6015 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 6016 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 6017 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 6018 | { .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] | 6019 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6020 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 6021 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6022 | |
| 6023 | /* The mux operations */ |
| 6024 | static const struct mux_ops h2_htx_ops = { |
| 6025 | .init = h2_init, |
| 6026 | .wake = h2_wake, |
| 6027 | .snd_buf = h2_snd_buf, |
| 6028 | .rcv_buf = h2_rcv_buf, |
| 6029 | .subscribe = h2_subscribe, |
| 6030 | .unsubscribe = h2_unsubscribe, |
| 6031 | .attach = h2_attach, |
| 6032 | .get_first_cs = h2_get_first_cs, |
| 6033 | .detach = h2_detach, |
| 6034 | .destroy = h2_destroy, |
| 6035 | .avail_streams = h2_avail_streams, |
| 6036 | .used_streams = h2_used_streams, |
| 6037 | .shutr = h2_shutr, |
| 6038 | .shutw = h2_shutw, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 6039 | .ctl = h2_ctl, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6040 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 6041 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6042 | .name = "H2", |
| 6043 | }; |
| 6044 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 6045 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 6046 | { .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] | 6047 | |
| 6048 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 6049 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6050 | /* config keyword parsers */ |
| 6051 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 6052 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 6053 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 6054 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 6055 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 6056 | { 0, NULL, NULL } |
| 6057 | }}; |
| 6058 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6059 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |